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 trait method Transaction::effective_gas_price #1640

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>) -> 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 {
Expand Down Expand Up @@ -202,6 +184,20 @@ impl Transaction for TxEip1559 {
self.max_priority_fee_per_gas
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
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 {
true
}
Expand Down
4 changes: 4 additions & 0 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ impl Transaction for TxEip2930 {
self.gas_price
}

fn effective_gas_price(&self, _base_fee: Option<u64>) -> u128 {
self.gas_price
}

fn is_dynamic_fee(&self) -> bool {
false
}
Expand Down
43 changes: 25 additions & 18 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ impl Transaction for TxEip4844Variant {
}
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> 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(),
Expand Down Expand Up @@ -436,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<u64>) -> 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 {
Expand Down Expand Up @@ -607,6 +596,20 @@ impl Transaction for TxEip4844 {
self.max_priority_fee_per_gas
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
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 {
true
}
Expand Down Expand Up @@ -795,6 +798,10 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.priority_fee_or_price()
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
self.tx.effective_gas_price(base_fee)
}

fn is_dynamic_fee(&self) -> bool {
self.tx.is_dynamic_fee()
}
Expand Down
32 changes: 14 additions & 18 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>) -> 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 {
Expand Down Expand Up @@ -200,6 +182,20 @@ impl Transaction for TxEip7702 {
self.max_priority_fee_per_gas
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
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 {
true
}
Expand Down
10 changes: 10 additions & 0 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ impl Transaction for TxEnvelope {
}
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> 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(),
Expand Down
4 changes: 4 additions & 0 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ impl Transaction for TxLegacy {
self.gas_price
}

fn effective_gas_price(&self, _base_fee: Option<u64>) -> u128 {
self.gas_price
}

fn is_dynamic_fee(&self) -> bool {
false
}
Expand Down
9 changes: 9 additions & 0 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>) -> u128;

/// Returns the effective tip for this transaction.
///
/// For EIP-1559 transactions: `min(max_fee_per_gas - base_fee, max_priority_fee_per_gas)`.
Expand Down Expand Up @@ -256,6 +261,10 @@ impl<T: Transaction> Transaction for alloy_serde::WithOtherFields<T> {
self.inner.priority_fee_or_price()
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
self.inner.effective_gas_price(base_fee)
}

fn kind(&self) -> TxKind {
self.inner.kind()
}
Expand Down
10 changes: 10 additions & 0 deletions crates/consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ impl Transaction for TypedTransaction {
}
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> 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(),
Expand Down
14 changes: 14 additions & 0 deletions crates/network/src/any/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>) -> 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(),
Expand Down Expand Up @@ -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<u64>) -> 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(),
Expand Down
27 changes: 27 additions & 0 deletions crates/network/src/any/unknowns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ 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<u64>) -> u128 {
if let Some(gas_price) = self.gas_price() {
return gas_price;
}

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 {
self.fields.get_deserialized::<U128>("maxFeePerGas").is_some()
|| self.fields.get_deserialized::<U128>("maxFeePerBlobGas").is_some()
Expand Down Expand Up @@ -267,6 +290,10 @@ impl alloy_consensus::Transaction for UnknownTxEnvelope {
self.inner.priority_fee_or_price()
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
self.inner.effective_gas_price(base_fee)
}

fn is_dynamic_fee(&self) -> bool {
self.inner.is_dynamic_fee()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ impl<T: TransactionTrait> TransactionTrait for Transaction<T> {
self.inner.priority_fee_or_price()
}

fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
self.inner.effective_gas_price(base_fee)
}

fn is_dynamic_fee(&self) -> bool {
self.inner.is_dynamic_fee()
}
Expand Down