From b40ce6f7f7ecba4f959d0d72a579f5a593aa9921 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Fri, 5 May 2023 13:33:01 +0200 Subject: [PATCH] fix: correct effective_gas_price (#2573) --- crates/primitives/src/transaction/mod.rs | 35 +++++++++++++++++++++- crates/rpc/rpc/src/eth/api/fees.rs | 2 +- crates/rpc/rpc/src/eth/api/transactions.rs | 5 +--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index acda972bf7..0cf9997fdd 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -369,7 +369,7 @@ impl Transaction { /// transactions is returned. /// /// If the `max_fee_per_gas` is less than the base fee, `None` returned. - pub fn effective_gas_price(&self, base_fee: Option) -> Option { + pub fn effective_gas_tip(&self, base_fee: Option) -> Option { if let Some(base_fee) = base_fee { let max_fee_per_gas = self.max_fee_per_gas(); if max_fee_per_gas < base_fee as u128 { @@ -400,6 +400,19 @@ impl Transaction { } } + /// Returns the effective gas price for the given base fee. + /// + /// If the transaction is a legacy or EIP2930 transaction, the gas price is returned. + pub fn effective_gas_price(&self, base_fee: Option) -> u128 { + let dynamic_tx = match self { + Transaction::Legacy(tx) => return tx.gas_price, + Transaction::Eip2930(tx) => return tx.gas_price, + Transaction::Eip1559(dynamic_tx) => dynamic_tx, + }; + + dynamic_tx.effective_gas_price(base_fee) + } + /// Returns the effective miner gas tip cap (`gasTipCap`) for the given base fee. /// /// Returns `None` if the basefee is higher than the [Transaction::max_fee_per_gas]. @@ -633,6 +646,26 @@ impl Encodable for Transaction { } } +impl TxEip1559 { + /// Returns the effective gas price for the given `base_fee`. + pub 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 - 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 + } + } + } + } +} + /// Whether or not the transaction is a contract creation. #[derive_arbitrary(compact, rlp)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)] diff --git a/crates/rpc/rpc/src/eth/api/fees.rs b/crates/rpc/rpc/src/eth/api/fees.rs index 5027a76432..ac5174e918 100644 --- a/crates/rpc/rpc/src/eth/api/fees.rs +++ b/crates/rpc/rpc/src/eth/api/fees.rs @@ -129,7 +129,7 @@ where let mut sorter = Vec::with_capacity(transactions.len()); for transaction in transactions.iter() { let reward = transaction - .effective_gas_price(header.base_fee_per_gas) + .effective_gas_tip(header.base_fee_per_gas) .ok_or(InvalidTransactionError::FeeCapTooLow)?; sorter.push(TxGasAndReward { gas_used: header.gas_used as u128, reward }) diff --git a/crates/rpc/rpc/src/eth/api/transactions.rs b/crates/rpc/rpc/src/eth/api/transactions.rs index 7302d36866..000cf83e67 100644 --- a/crates/rpc/rpc/src/eth/api/transactions.rs +++ b/crates/rpc/rpc/src/eth/api/transactions.rs @@ -596,10 +596,7 @@ where gas_used: Some(U256::from(gas_used)), contract_address: None, logs: Vec::with_capacity(receipt.logs.len()), - effective_gas_price: transaction - .effective_gas_price(meta.base_fee) - .map(U128::from) - .unwrap_or(U128::ZERO), + effective_gas_price: U128::from(transaction.effective_gas_price(meta.base_fee)), transaction_type: tx.transaction.tx_type().into(), // TODO pre-byzantium receipts have a post-transaction state root state_root: None,