From bf800986d3cb65aaf30158920dd04957f0fd850d Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Tue, 17 Sep 2024 05:28:35 +0800 Subject: [PATCH] chore: use MIN_TRANSACTION_GAS replace magic number (#10910) --- crates/blockchain-tree/src/blockchain_tree.rs | 10 +++++----- crates/chain-state/src/test_utils.rs | 12 ++++++------ crates/chainspec/src/constants.rs | 2 ++ crates/chainspec/src/lib.rs | 1 + crates/exex/exex/src/backfill/test_utils.rs | 14 +++++++------- crates/net/eth-wire-types/src/transactions.rs | 5 +++-- crates/optimism/evm/src/execute.rs | 10 +++++----- crates/primitives/src/transaction/mod.rs | 3 ++- crates/rpc/rpc-eth-api/src/helpers/call.rs | 5 ++--- crates/rpc/rpc-server-types/src/constants.rs | 2 -- 10 files changed, 33 insertions(+), 31 deletions(-) diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 0468d662e790..398aa098b174 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -1378,7 +1378,7 @@ mod tests { use alloy_primitives::{keccak256, Address, B256}; use assert_matches::assert_matches; use linked_hash_set::LinkedHashSet; - use reth_chainspec::{ChainSpecBuilder, MAINNET}; + use reth_chainspec::{ChainSpecBuilder, MAINNET, MIN_TRANSACTION_GAS}; use reth_consensus::test_utils::TestConsensus; use reth_db::tables; use reth_db_api::transaction::DbTxMut; @@ -1558,13 +1558,13 @@ mod tests { provider_rw.commit().unwrap(); } - let single_tx_cost = U256::from(EIP1559_INITIAL_BASE_FEE * 21_000); + let single_tx_cost = U256::from(EIP1559_INITIAL_BASE_FEE * MIN_TRANSACTION_GAS); let mock_tx = |nonce: u64| -> TransactionSignedEcRecovered { TransactionSigned::from_transaction_and_signature( Transaction::Eip1559(TxEip1559 { chain_id: chain_spec.chain.id(), nonce, - gas_limit: 21_000, + gas_limit: MIN_TRANSACTION_GAS as u128, to: Address::ZERO.into(), max_fee_per_gas: EIP1559_INITIAL_BASE_FEE as u128, ..Default::default() @@ -1587,7 +1587,7 @@ mod tests { Receipt { tx_type: tx.tx_type(), success: true, - cumulative_gas_used: (idx as u64 + 1) * 21_000, + cumulative_gas_used: (idx as u64 + 1) * MIN_TRANSACTION_GAS, ..Default::default() } .with_bloom() @@ -1602,7 +1602,7 @@ mod tests { header: Header { number, parent_hash: parent.unwrap_or_default(), - gas_used: body.len() as u64 * 21_000, + gas_used: body.len() as u64 * MIN_TRANSACTION_GAS, gas_limit: chain_spec.max_gas_limit, mix_hash: B256::random(), base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE), diff --git a/crates/chain-state/src/test_utils.rs b/crates/chain-state/src/test_utils.rs index d101b309a92a..3e38aead26e2 100644 --- a/crates/chain-state/src/test_utils.rs +++ b/crates/chain-state/src/test_utils.rs @@ -5,7 +5,7 @@ use crate::{ use alloy_signer::SignerSync; use alloy_signer_local::PrivateKeySigner; use rand::{thread_rng, Rng}; -use reth_chainspec::{ChainSpec, EthereumHardfork}; +use reth_chainspec::{ChainSpec, EthereumHardfork, MIN_TRANSACTION_GAS}; use reth_execution_types::{Chain, ExecutionOutcome}; use reth_primitives::{ constants::{EIP1559_INITIAL_BASE_FEE, EMPTY_ROOT_HASH}, @@ -72,7 +72,7 @@ impl TestBlockBuilder { /// Gas cost of a single transaction generated by the block builder. pub fn single_tx_cost() -> U256 { - U256::from(EIP1559_INITIAL_BASE_FEE * 21_000) + U256::from(EIP1559_INITIAL_BASE_FEE * MIN_TRANSACTION_GAS) } /// Generates a random [`SealedBlockWithSenders`]. @@ -87,7 +87,7 @@ impl TestBlockBuilder { let tx = Transaction::Eip1559(TxEip1559 { chain_id: self.chain_spec.chain.id(), nonce, - gas_limit: 21_000, + gas_limit: MIN_TRANSACTION_GAS as u128, to: Address::random().into(), max_fee_per_gas: EIP1559_INITIAL_BASE_FEE as u128, max_priority_fee_per_gas: 1, @@ -125,7 +125,7 @@ impl TestBlockBuilder { Receipt { tx_type: tx.tx_type(), success: true, - cumulative_gas_used: (idx as u64 + 1) * 21_000, + cumulative_gas_used: (idx as u64 + 1) * MIN_TRANSACTION_GAS, ..Default::default() } .with_bloom() @@ -137,7 +137,7 @@ impl TestBlockBuilder { let header = Header { number, parent_hash, - gas_used: transactions.len() as u64 * 21_000, + gas_used: transactions.len() as u64 * MIN_TRANSACTION_GAS, gas_limit: self.chain_spec.max_gas_limit, mix_hash: B256::random(), base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE), @@ -261,7 +261,7 @@ impl TestBlockBuilder { .map(|(idx, tx)| Receipt { tx_type: tx.tx_type(), success: true, - cumulative_gas_used: (idx as u64 + 1) * 21_000, + cumulative_gas_used: (idx as u64 + 1) * MIN_TRANSACTION_GAS, ..Default::default() }) .collect::>(); diff --git a/crates/chainspec/src/constants.rs b/crates/chainspec/src/constants.rs index 7026e76ff438..2e22b2299a4b 100644 --- a/crates/chainspec/src/constants.rs +++ b/crates/chainspec/src/constants.rs @@ -1,6 +1,8 @@ use crate::spec::DepositContract; use alloy_primitives::{address, b256}; +/// Gas per transaction not creating a contract. +pub const MIN_TRANSACTION_GAS: u64 = 21_000u64; /// Deposit contract address: `0x00000000219ab540356cbb839cbe05303d7705fa` pub(crate) const MAINNET_DEPOSIT_CONTRACT: DepositContract = DepositContract::new( address!("00000000219ab540356cbb839cbe05303d7705fa"), diff --git a/crates/chainspec/src/lib.rs b/crates/chainspec/src/lib.rs index 42e5f08fed98..424b2b77c28c 100644 --- a/crates/chainspec/src/lib.rs +++ b/crates/chainspec/src/lib.rs @@ -13,6 +13,7 @@ extern crate alloc; /// Chain specific constants pub(crate) mod constants; +pub use constants::MIN_TRANSACTION_GAS; mod api; /// The chain info module. diff --git a/crates/exex/exex/src/backfill/test_utils.rs b/crates/exex/exex/src/backfill/test_utils.rs index 26f92ea87f09..c567315763f9 100644 --- a/crates/exex/exex/src/backfill/test_utils.rs +++ b/crates/exex/exex/src/backfill/test_utils.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use eyre::OptionExt; -use reth_chainspec::{ChainSpec, ChainSpecBuilder, EthereumHardfork, MAINNET}; +use reth_chainspec::{ChainSpec, ChainSpecBuilder, EthereumHardfork, MAINNET, MIN_TRANSACTION_GAS}; use reth_evm::execute::{ BatchExecutor, BlockExecutionInput, BlockExecutionOutput, BlockExecutorProvider, Executor, }; @@ -98,8 +98,8 @@ fn blocks( ), difficulty: chain_spec.fork(EthereumHardfork::Paris).ttd().expect("Paris TTD"), number: 1, - gas_limit: 21000, - gas_used: 21000, + gas_limit: MIN_TRANSACTION_GAS, + gas_used: MIN_TRANSACTION_GAS, ..Default::default() }, body: vec![sign_tx_with_key_pair( @@ -107,7 +107,7 @@ fn blocks( Transaction::Eip2930(TxEip2930 { chain_id: chain_spec.chain.id(), nonce: 0, - gas_limit: 21000, + gas_limit: MIN_TRANSACTION_GAS as u128, gas_price: 1_500_000_000, to: TxKind::Call(Address::ZERO), value: U256::from(0.1 * ETH_TO_WEI as f64), @@ -128,8 +128,8 @@ fn blocks( ), difficulty: chain_spec.fork(EthereumHardfork::Paris).ttd().expect("Paris TTD"), number: 2, - gas_limit: 21000, - gas_used: 21000, + gas_limit: MIN_TRANSACTION_GAS, + gas_used: MIN_TRANSACTION_GAS, ..Default::default() }, body: vec![sign_tx_with_key_pair( @@ -137,7 +137,7 @@ fn blocks( Transaction::Eip2930(TxEip2930 { chain_id: chain_spec.chain.id(), nonce: 1, - gas_limit: 21000, + gas_limit: MIN_TRANSACTION_GAS as u128, gas_price: 1_500_000_000, to: TxKind::Call(Address::ZERO), value: U256::from(0.1 * ETH_TO_WEI as f64), diff --git a/crates/net/eth-wire-types/src/transactions.rs b/crates/net/eth-wire-types/src/transactions.rs index 8d837f824ffe..53291b1ae034 100644 --- a/crates/net/eth-wire-types/src/transactions.rs +++ b/crates/net/eth-wire-types/src/transactions.rs @@ -77,6 +77,7 @@ impl FromIterator for PooledTransactions { mod tests { use crate::{message::RequestPair, GetPooledTransactions, PooledTransactions}; use alloy_rlp::{Decodable, Encodable}; + use reth_chainspec::MIN_TRANSACTION_GAS; use reth_primitives::{ hex, PooledTransactionsElement, Signature, Transaction, TransactionSigned, TxEip1559, TxKind, TxLegacy, U256, @@ -283,7 +284,7 @@ mod tests { nonce: 26u64, max_priority_fee_per_gas: 1500000000, max_fee_per_gas: 1500000013, - gas_limit: 21000, + gas_limit: MIN_TRANSACTION_GAS as u128, to: TxKind::Call(hex!("61815774383099e24810ab832a5b2a5425c154d5").into()), value: U256::from(3000000000000000000u64), input: Default::default(), @@ -422,7 +423,7 @@ mod tests { nonce: 26u64, max_priority_fee_per_gas: 1500000000, max_fee_per_gas: 1500000013, - gas_limit: 21000, + gas_limit: MIN_TRANSACTION_GAS as u128, to: TxKind::Call(hex!("61815774383099e24810ab832a5b2a5425c154d5").into()), value: U256::from(3000000000000000000u64), input: Default::default(), diff --git a/crates/optimism/evm/src/execute.rs b/crates/optimism/evm/src/execute.rs index d921b817659e..c8874685c822 100644 --- a/crates/optimism/evm/src/execute.rs +++ b/crates/optimism/evm/src/execute.rs @@ -534,7 +534,7 @@ mod tests { use super::*; use crate::OpChainSpec; use alloy_primitives::{b256, Address, StorageKey, StorageValue}; - use reth_chainspec::ChainSpecBuilder; + use reth_chainspec::{ChainSpecBuilder, MIN_TRANSACTION_GAS}; use reth_primitives::{ Account, Block, Signature, Transaction, TransactionSigned, TxEip1559, BASE_MAINNET, }; @@ -608,7 +608,7 @@ mod tests { Transaction::Eip1559(TxEip1559 { chain_id: chain_spec.chain.id(), nonce: 0, - gas_limit: 21_000, + gas_limit: MIN_TRANSACTION_GAS as u128, to: addr.into(), ..Default::default() }), @@ -619,7 +619,7 @@ mod tests { Transaction::Deposit(reth_primitives::TxDeposit { from: addr, to: addr.into(), - gas_limit: 21_000, + gas_limit: MIN_TRANSACTION_GAS as u128, ..Default::default() }), Signature::default(), @@ -692,7 +692,7 @@ mod tests { Transaction::Eip1559(TxEip1559 { chain_id: chain_spec.chain.id(), nonce: 0, - gas_limit: 21_000, + gas_limit: MIN_TRANSACTION_GAS as u128, to: addr.into(), ..Default::default() }), @@ -703,7 +703,7 @@ mod tests { Transaction::Deposit(reth_primitives::TxDeposit { from: addr, to: addr.into(), - gas_limit: 21_000, + gas_limit: MIN_TRANSACTION_GAS as u128, ..Default::default() }), Signature::optimism_deposit_tx_signature(), diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 68b279241371..50eba62bfca1 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -1706,6 +1706,7 @@ mod tests { }; use alloy_primitives::{address, b256, bytes}; use alloy_rlp::{Decodable, Encodable, Error as RlpError}; + use reth_chainspec::MIN_TRANSACTION_GAS; use reth_codecs::Compact; use std::str::FromStr; @@ -1860,7 +1861,7 @@ mod tests { nonce: 26, max_priority_fee_per_gas: 1500000000, max_fee_per_gas: 1500000013, - gas_limit: 21000, + gas_limit: MIN_TRANSACTION_GAS as u128, to: Address::from_slice(&hex!("61815774383099e24810ab832a5b2a5425c154d5")[..]).into(), value: U256::from(3000000000000000000u64), input: Default::default(), diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs index d32f0104b33f..99b90d6729f2 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/call.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs @@ -4,6 +4,7 @@ use crate::{AsEthApiError, FromEthApiError, FromEvmError, IntoEthApiError}; use alloy_primitives::{Bytes, TxKind, B256, U256}; use futures::Future; +use reth_chainspec::MIN_TRANSACTION_GAS; use reth_evm::{ConfigureEvm, ConfigureEvmEnv}; use reth_primitives::{ revm_primitives::{ @@ -24,9 +25,7 @@ use reth_rpc_eth_types::{ }, EthApiError, RevertError, RpcInvalidTransactionError, StateCacheDb, }; -use reth_rpc_server_types::constants::gas_oracle::{ - CALL_STIPEND_GAS, ESTIMATE_GAS_ERROR_RATIO, MIN_TRANSACTION_GAS, -}; +use reth_rpc_server_types::constants::gas_oracle::{CALL_STIPEND_GAS, ESTIMATE_GAS_ERROR_RATIO}; use reth_rpc_types::{ simulate::{SimBlock, SimulatedBlock}, state::{EvmOverrides, StateOverride}, diff --git a/crates/rpc/rpc-server-types/src/constants.rs b/crates/rpc/rpc-server-types/src/constants.rs index 1fad38a75834..d6742584a59a 100644 --- a/crates/rpc/rpc-server-types/src/constants.rs +++ b/crates/rpc/rpc-server-types/src/constants.rs @@ -82,8 +82,6 @@ pub mod gas_oracle { /// for more complex calls. pub const RPC_DEFAULT_GAS_CAP: u64 = 50_000_000; - /// Gas per transaction not creating a contract. - pub const MIN_TRANSACTION_GAS: u64 = 21_000u64; /// Allowed error ratio for gas estimation /// Taken from Geth's implementation in order to pass the hive tests ///