From eff2ecb20204e5d1f52daf0885e5103de85fdd7f Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Tue, 12 Nov 2024 15:52:53 +0100 Subject: [PATCH 1/4] Add trait method Transaction::effective_gas_price --- crates/consensus/src/transaction/eip1559.rs | 4 +++ crates/consensus/src/transaction/eip2930.rs | 4 +++ crates/consensus/src/transaction/eip4844.rs | 15 +++++++++++ crates/consensus/src/transaction/eip7702.rs | 4 +++ crates/consensus/src/transaction/envelope.rs | 10 ++++++++ crates/consensus/src/transaction/legacy.rs | 4 +++ crates/consensus/src/transaction/mod.rs | 9 +++++++ crates/consensus/src/transaction/typed.rs | 10 ++++++++ crates/network/src/any/either.rs | 14 +++++++++++ crates/network/src/any/unknowns.rs | 26 ++++++++++++++++++++ crates/rpc-types-eth/src/transaction/mod.rs | 4 +++ 11 files changed, 104 insertions(+) diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index f684205e4c1..f6de7bb2179 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -202,6 +202,10 @@ impl Transaction for TxEip1559 { self.max_priority_fee_per_gas } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.effective_gas_price(base_fee) + } + fn is_dynamic_fee(&self) -> bool { true } diff --git a/crates/consensus/src/transaction/eip2930.rs b/crates/consensus/src/transaction/eip2930.rs index cf719bdabff..91cadb6f68a 100644 --- a/crates/consensus/src/transaction/eip2930.rs +++ b/crates/consensus/src/transaction/eip2930.rs @@ -152,6 +152,10 @@ impl Transaction for TxEip2930 { self.gas_price } + fn effective_gas_price(&self, _base_fee: Option) -> u128 { + self.gas_price + } + fn is_dynamic_fee(&self) -> bool { false } diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index c97408edf69..6f91de1808d 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -186,6 +186,13 @@ impl Transaction for TxEip4844Variant { } } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + match self { + Self::TxEip4844(tx) => tx.effective_gas_price(base_fee), + Self::TxEip4844WithSidecar(tx) => tx.effective_gas_price(base_fee), + } + } + fn is_dynamic_fee(&self) -> bool { match self { Self::TxEip4844(tx) => tx.is_dynamic_fee(), @@ -607,6 +614,10 @@ impl Transaction for TxEip4844 { self.max_priority_fee_per_gas } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.effective_gas_price(base_fee) + } + fn is_dynamic_fee(&self) -> bool { true } @@ -795,6 +806,10 @@ impl Transaction for TxEip4844WithSidecar { self.tx.priority_fee_or_price() } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.tx.effective_gas_price(base_fee) + } + fn is_dynamic_fee(&self) -> bool { self.tx.is_dynamic_fee() } diff --git a/crates/consensus/src/transaction/eip7702.rs b/crates/consensus/src/transaction/eip7702.rs index a5291f69a79..7ddcc25eb01 100644 --- a/crates/consensus/src/transaction/eip7702.rs +++ b/crates/consensus/src/transaction/eip7702.rs @@ -200,6 +200,10 @@ impl Transaction for TxEip7702 { self.max_priority_fee_per_gas } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.effective_gas_price(base_fee) + } + fn is_dynamic_fee(&self) -> bool { true } diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index 7425c58dfeb..87cc6e67ce1 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -477,6 +477,16 @@ impl Transaction for TxEnvelope { } } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + match self { + Self::Legacy(tx) => tx.tx().effective_gas_price(base_fee), + Self::Eip2930(tx) => tx.tx().effective_gas_price(base_fee), + Self::Eip1559(tx) => tx.tx().effective_gas_price(base_fee), + Self::Eip4844(tx) => tx.tx().effective_gas_price(base_fee), + Self::Eip7702(tx) => tx.tx().effective_gas_price(base_fee), + } + } + fn is_dynamic_fee(&self) -> bool { match self { Self::Legacy(tx) => tx.tx().is_dynamic_fee(), diff --git a/crates/consensus/src/transaction/legacy.rs b/crates/consensus/src/transaction/legacy.rs index b3f4f618e01..059b0e97bb0 100644 --- a/crates/consensus/src/transaction/legacy.rs +++ b/crates/consensus/src/transaction/legacy.rs @@ -261,6 +261,10 @@ impl Transaction for TxLegacy { self.gas_price } + fn effective_gas_price(&self, _base_fee: Option) -> u128 { + self.gas_price + } + fn is_dynamic_fee(&self) -> bool { false } diff --git a/crates/consensus/src/transaction/mod.rs b/crates/consensus/src/transaction/mod.rs index 3cf7aaf0e7a..4c00d5638ee 100644 --- a/crates/consensus/src/transaction/mod.rs +++ b/crates/consensus/src/transaction/mod.rs @@ -94,6 +94,11 @@ pub trait Transaction: fmt::Debug + any::Any + Send + Sync + 'static { /// non-EIP-1559 transactions. fn priority_fee_or_price(&self) -> u128; + /// Returns the effective gas price for the given base fee. + /// + /// If the transaction is a legacy or EIP2930 transaction, the gas price is returned. + fn effective_gas_price(&self, base_fee: Option) -> u128; + /// Returns the effective tip for this transaction. /// /// For EIP-1559 transactions: `min(max_fee_per_gas - base_fee, max_priority_fee_per_gas)`. @@ -256,6 +261,10 @@ impl Transaction for alloy_serde::WithOtherFields { self.inner.priority_fee_or_price() } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.inner.effective_gas_price(base_fee) + } + fn kind(&self) -> TxKind { self.inner.kind() } diff --git a/crates/consensus/src/transaction/typed.rs b/crates/consensus/src/transaction/typed.rs index ea4f7ffaebe..ddbe8b0f867 100644 --- a/crates/consensus/src/transaction/typed.rs +++ b/crates/consensus/src/transaction/typed.rs @@ -223,6 +223,16 @@ impl Transaction for TypedTransaction { } } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + match self { + Self::Legacy(tx) => tx.effective_gas_price(base_fee), + Self::Eip2930(tx) => tx.effective_gas_price(base_fee), + Self::Eip1559(tx) => tx.effective_gas_price(base_fee), + Self::Eip4844(tx) => tx.effective_gas_price(base_fee), + Self::Eip7702(tx) => tx.effective_gas_price(base_fee), + } + } + fn is_dynamic_fee(&self) -> bool { match self { Self::Legacy(tx) => tx.is_dynamic_fee(), diff --git a/crates/network/src/any/either.rs b/crates/network/src/any/either.rs index 0adc08eac14..801573bfd92 100644 --- a/crates/network/src/any/either.rs +++ b/crates/network/src/any/either.rs @@ -112,6 +112,13 @@ impl TransactionTrait for AnyTypedTransaction { self.max_priority_fee_per_gas().or_else(|| self.gas_price()).unwrap_or_default() } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + match self { + Self::Ethereum(inner) => inner.effective_gas_price(base_fee), + Self::Unknown(inner) => inner.effective_gas_price(base_fee), + } + } + fn is_dynamic_fee(&self) -> bool { match self { Self::Ethereum(inner) => inner.is_dynamic_fee(), @@ -280,6 +287,13 @@ impl TransactionTrait for AnyTxEnvelope { self.max_priority_fee_per_gas().or_else(|| self.gas_price()).unwrap_or_default() } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + match self { + Self::Ethereum(inner) => inner.effective_gas_price(base_fee), + Self::Unknown(inner) => inner.effective_gas_price(base_fee), + } + } + fn is_dynamic_fee(&self) -> bool { match self { Self::Ethereum(inner) => inner.is_dynamic_fee(), diff --git a/crates/network/src/any/unknowns.rs b/crates/network/src/any/unknowns.rs index 5e52012bfa4..5ad8df5b91e 100644 --- a/crates/network/src/any/unknowns.rs +++ b/crates/network/src/any/unknowns.rs @@ -146,6 +146,28 @@ impl alloy_consensus::Transaction for UnknownTypedTransaction { self.gas_price().or(self.max_priority_fee_per_gas()).unwrap_or_default() } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.gas_price().unwrap_or(match base_fee { + None => self.max_fee_per_gas(), + Some(base_fee) => { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let max_fee = self.max_fee_per_gas(); + if max_fee == 0 { + return 0; + } + let Some(max_prio_fee) = self.max_priority_fee_per_gas() else { return max_fee }; + let tip = max_fee.saturating_sub(base_fee as u128); + if tip > max_prio_fee { + max_prio_fee + base_fee as u128 + } else { + // otherwise return the max fee per gas + max_fee + } + } + }) + } + fn is_dynamic_fee(&self) -> bool { self.fields.get_deserialized::("maxFeePerGas").is_some() || self.fields.get_deserialized::("maxFeePerBlobGas").is_some() @@ -267,6 +289,10 @@ impl alloy_consensus::Transaction for UnknownTxEnvelope { self.inner.priority_fee_or_price() } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.inner.effective_gas_price(base_fee) + } + fn is_dynamic_fee(&self) -> bool { self.inner.is_dynamic_fee() } diff --git a/crates/rpc-types-eth/src/transaction/mod.rs b/crates/rpc-types-eth/src/transaction/mod.rs index 84a98d65d10..a552ad2ac5c 100644 --- a/crates/rpc-types-eth/src/transaction/mod.rs +++ b/crates/rpc-types-eth/src/transaction/mod.rs @@ -214,6 +214,10 @@ impl TransactionTrait for Transaction { self.inner.priority_fee_or_price() } + fn effective_gas_price(&self, base_fee: Option) -> u128 { + self.inner.effective_gas_price(base_fee) + } + fn is_dynamic_fee(&self) -> bool { self.inner.is_dynamic_fee() } From 9ff5644084d6814be0584d54cd9a4d41f2c0b5ce Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Tue, 12 Nov 2024 16:17:27 +0100 Subject: [PATCH 2/4] Fix syntax --- crates/network/src/any/unknowns.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/network/src/any/unknowns.rs b/crates/network/src/any/unknowns.rs index 5ad8df5b91e..12cc197cb5c 100644 --- a/crates/network/src/any/unknowns.rs +++ b/crates/network/src/any/unknowns.rs @@ -147,7 +147,11 @@ impl alloy_consensus::Transaction for UnknownTypedTransaction { } fn effective_gas_price(&self, base_fee: Option) -> u128 { - self.gas_price().unwrap_or(match base_fee { + if let Some(gas_price) = self.gas_price() { + return gas_price; + } + + match base_fee { None => self.max_fee_per_gas(), Some(base_fee) => { // if the tip is greater than the max priority fee per gas, set it to the max @@ -165,7 +169,7 @@ impl alloy_consensus::Transaction for UnknownTypedTransaction { max_fee } } - }) + } } fn is_dynamic_fee(&self) -> bool { From f9754aadad2e30709d87c997108c3bc41ec8611f Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Tue, 12 Nov 2024 16:19:21 +0100 Subject: [PATCH 3/4] Remove redundancy --- crates/consensus/src/transaction/eip1559.rs | 33 +++++++++------------ crates/consensus/src/transaction/eip4844.rs | 33 +++++++++------------ crates/consensus/src/transaction/eip7702.rs | 33 +++++++++------------ 3 files changed, 42 insertions(+), 57 deletions(-) diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index f6de7bb2179..9b1ab0a3288 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -70,24 +70,6 @@ pub struct TxEip1559 { } impl TxEip1559 { - /// Returns the effective gas price for the given `base_fee`. - pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } - } - } - } - /// Get the transaction type #[doc(alias = "transaction_type")] pub(crate) const fn tx_type() -> TxType { @@ -203,7 +185,20 @@ impl Transaction for TxEip1559 { } fn effective_gas_price(&self, base_fee: Option) -> u128 { - self.effective_gas_price(base_fee) + match base_fee { + None => self.max_fee_per_gas, + Some(base_fee) => { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas + } + } + } } fn is_dynamic_fee(&self) -> bool { diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index 6f91de1808d..cd89cd7508c 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -443,24 +443,6 @@ pub struct TxEip4844 { } impl TxEip4844 { - /// Returns the effective gas price for the given `base_fee`. - pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } - } - } - } - /// Returns the total gas for all blobs in this transaction. #[inline] pub fn blob_gas(&self) -> u64 { @@ -615,7 +597,20 @@ impl Transaction for TxEip4844 { } fn effective_gas_price(&self, base_fee: Option) -> u128 { - self.effective_gas_price(base_fee) + match base_fee { + None => self.max_fee_per_gas, + Some(base_fee) => { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas + } + } + } } fn is_dynamic_fee(&self) -> bool { diff --git a/crates/consensus/src/transaction/eip7702.rs b/crates/consensus/src/transaction/eip7702.rs index 7ddcc25eb01..6cc5d5012c7 100644 --- a/crates/consensus/src/transaction/eip7702.rs +++ b/crates/consensus/src/transaction/eip7702.rs @@ -80,24 +80,6 @@ pub struct TxEip7702 { } impl TxEip7702 { - /// Returns the effective gas price for the given `base_fee`. - pub const fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } - } - } - } - /// Get the transaction type. #[doc(alias = "transaction_type")] pub const fn tx_type() -> TxType { @@ -201,7 +183,20 @@ impl Transaction for TxEip7702 { } fn effective_gas_price(&self, base_fee: Option) -> u128 { - self.effective_gas_price(base_fee) + match base_fee { + None => self.max_fee_per_gas, + Some(base_fee) => { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas + } + } + } } fn is_dynamic_fee(&self) -> bool { From 7a8ea7853daf0ca532063837e45bc244d2314b7c Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Tue, 12 Nov 2024 17:01:56 +0100 Subject: [PATCH 4/4] Fix clippy warnings --- crates/consensus/src/transaction/eip1559.rs | 23 +++++++------- crates/consensus/src/transaction/eip4844.rs | 23 +++++++------- crates/consensus/src/transaction/eip7702.rs | 23 +++++++------- crates/network/src/any/unknowns.rs | 33 ++++++++++----------- 4 files changed, 45 insertions(+), 57 deletions(-) diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index 9b1ab0a3288..25661166afa 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -185,20 +185,17 @@ impl Transaction for TxEip1559 { } fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } + base_fee.map_or(self.max_fee_per_gas, |base_fee| { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas } - } + }) } fn is_dynamic_fee(&self) -> bool { diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index cd89cd7508c..b94c0582525 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -597,20 +597,17 @@ impl Transaction for TxEip4844 { } fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } + base_fee.map_or(self.max_fee_per_gas, |base_fee| { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas } - } + }) } fn is_dynamic_fee(&self) -> bool { diff --git a/crates/consensus/src/transaction/eip7702.rs b/crates/consensus/src/transaction/eip7702.rs index 6cc5d5012c7..fab83dcb662 100644 --- a/crates/consensus/src/transaction/eip7702.rs +++ b/crates/consensus/src/transaction/eip7702.rs @@ -183,20 +183,17 @@ impl Transaction for TxEip7702 { } fn effective_gas_price(&self, base_fee: Option) -> u128 { - match base_fee { - None => self.max_fee_per_gas, - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); - if tip > self.max_priority_fee_per_gas { - self.max_priority_fee_per_gas + base_fee as u128 - } else { - // otherwise return the max fee per gas - self.max_fee_per_gas - } + base_fee.map_or(self.max_fee_per_gas, |base_fee| { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let tip = self.max_fee_per_gas.saturating_sub(base_fee as u128); + if tip > self.max_priority_fee_per_gas { + self.max_priority_fee_per_gas + base_fee as u128 + } else { + // otherwise return the max fee per gas + self.max_fee_per_gas } - } + }) } fn is_dynamic_fee(&self) -> bool { diff --git a/crates/network/src/any/unknowns.rs b/crates/network/src/any/unknowns.rs index 12cc197cb5c..53479b7118e 100644 --- a/crates/network/src/any/unknowns.rs +++ b/crates/network/src/any/unknowns.rs @@ -151,25 +151,22 @@ impl alloy_consensus::Transaction for UnknownTypedTransaction { return gas_price; } - match base_fee { - None => self.max_fee_per_gas(), - Some(base_fee) => { - // if the tip is greater than the max priority fee per gas, set it to the max - // priority fee per gas + base fee - let max_fee = self.max_fee_per_gas(); - if max_fee == 0 { - return 0; - } - let Some(max_prio_fee) = self.max_priority_fee_per_gas() else { return max_fee }; - let tip = max_fee.saturating_sub(base_fee as u128); - if tip > max_prio_fee { - max_prio_fee + base_fee as u128 - } else { - // otherwise return the max fee per gas - max_fee - } + base_fee.map_or(self.max_fee_per_gas(), |base_fee| { + // if the tip is greater than the max priority fee per gas, set it to the max + // priority fee per gas + base fee + let max_fee = self.max_fee_per_gas(); + if max_fee == 0 { + return 0; } - } + let Some(max_prio_fee) = self.max_priority_fee_per_gas() else { return max_fee }; + let tip = max_fee.saturating_sub(base_fee as u128); + if tip > max_prio_fee { + max_prio_fee + base_fee as u128 + } else { + // otherwise return the max fee per gas + max_fee + } + }) } fn is_dynamic_fee(&self) -> bool {