Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supertrait alloy_consensus::Transaction to RPC TransactionResponse #1387

Merged
merged 9 commits into from
Sep 30, 2024

Conversation

emhane
Copy link
Member

@emhane emhane commented Sep 26, 2024

Motivation

Closes #1344

Solution

  • Unifies getters for mutually exclusive values max_fee_per_gas and gas_price in trait alloy_consensus::Transaction. Removes getter for deprecated name gas_price from trait alloy_consensus::Transaction.
  • Adds alloy_consensus::Transaction as super trait of TransactionResponse
  • Removes redundant methods from TransactionResponse

Follow up would be to manually implement de-/serialisation for alloy_rpc_types_eth::Transaction, and remove trait methods of TransactionResponse: to, gas_price, max_fee_per_gas and transaction_type, which are just matching the values returned by alloy_consensus::Transaction, with the right label w.r.t. to the json object standard for the transaction type.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@emhane emhane added the debt Tech debt which needs to be addressed label Sep 26, 2024
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes the fee functions, which is a bit confusing, seems like we want all of those optional because these are tx specific

Comment on lines 57 to 59
/// Return the max priority fee per gas if the transaction is an EIP-1559 transaction, and
/// otherwise return the gas price.
fn max_priority_fee_per_gas(&self) -> u128;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this no longer an option?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed redundancy within the consensus Transaction trait. gas_price has been deprecated in favour of max fee per gas and max priority fee per gas. checking if this returns an option is a bad way to check if this is a pre-eip1559 tx, and instead trait method Transaction::ty should be used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be an option because not every tx type has this

Comment on lines 354 to 359
fn max_fee_per_gas(&self) -> u128 {
self.max_fee_per_gas.unwrap_or(self.gas_price.unwrap_or_default())
}

fn transaction_index(&self) -> Option<u64> {
self.transaction_index
fn max_priority_fee_per_gas(&self) -> u128 {
self.max_fee_per_gas.unwrap_or(self.gas_price.unwrap_or_default())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not great but I guess it should never happen and will get cleaned up eventually once we have #1165

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will get cleaned up by manual impl of serialisation for the the alloy_rpc_types_eth::Transaction type. then we don't need to have one field each for the essentially mutually exclusive fields max fee per gas and gas price, we just need the field max fee per gas, and the manual serialisation impl will just check if self.ty() < 2 then use gasPrice as label, otherwise use maxBaseFee

@@ -47,20 +47,16 @@ pub trait Transaction: any::Any + Send + Sync + 'static {
/// Get `gas_limit`.
fn gas_limit(&self) -> u64;

/// Get `gas_price`.
fn gas_price(&self) -> Option<u128>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's our motivation for removing this? I can see how it's redundant from execution perspective, though it is an actual separate property of transaction which is important from RPC PoV

I think it's pretty much a question on how we expect this trait to be consumed and whether separate getters might be useful

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the gas price field is never used on tx types since max fee per gas was introduced

}

fn transaction_type(&self) -> Option<u8> {
self.transaction_type
fn to(&self) -> Option<Address> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed? given that we already have to on consensus Transaction

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consensus Transaction returns TxKind. requiring every caller of to in rpc context to do this conversion to Option<Address> isn't good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine? the conversion is as simple as calling kind.to()

and we can probably change to type to TxKind on rpc types as it's done in consensus serde

#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))]
pub to: TxKind,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following matze's directive, prio of this pr should be not to change the API. pls open an issue.

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep the functions as they were before, because this issue was intended to unify the traits. we can still discuss changes separately, but this is now becoming a blocker and I'd like to make this change now and discuss nuances later.

@emhane emhane requested a review from mattsse September 28, 2024 15:54
crates/network-primitives/src/traits.rs Outdated Show resolved Hide resolved
Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
@mattsse mattsse merged commit b69c161 into main Sep 30, 2024
26 checks passed
@mattsse mattsse deleted the emhane/tx-traits branch September 30, 2024 08:05
lwedge99 pushed a commit to sentioxyz/alloy that referenced this pull request Oct 8, 2024
alloy-rs#1387)

* Add supertrait alloy_consensus::Transaction to RPC TransactionResponse

* Feature gate alloy_consensus::Transaction impl for WithOtherFields

* Enable alloy-consensus/serde to impl TransactionResponse for WithOtherFields

* Revert unify gas price and max fee per gas in alloy_consensus::Transaction

* Simplify syntax

* Fix lint

* Fix bug max prio fee per gas getter impl

* Fix typo

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>

---------

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debt Tech debt which needs to be addressed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Unify Transaction and TransactionResponse trait
3 participants