Skip to content

Commit

Permalink
chore: use MIN_TRANSACTION_GAS replace magic number (#10910)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg authored Sep 16, 2024
1 parent 8b61b9b commit bf80098
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 31 deletions.
10 changes: 5 additions & 5 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions crates/chain-state/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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`].
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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),
Expand Down Expand Up @@ -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::<Vec<_>>();
Expand Down
2 changes: 2 additions & 0 deletions crates/chainspec/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
1 change: 1 addition & 0 deletions crates/chainspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions crates/exex/exex/src/backfill/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -98,16 +98,16 @@ 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(
key_pair,
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),
Expand All @@ -128,16 +128,16 @@ 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(
key_pair,
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),
Expand Down
5 changes: 3 additions & 2 deletions crates/net/eth-wire-types/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl FromIterator<PooledTransactionsElement> 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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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()
}),
Expand All @@ -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(),
Expand Down Expand Up @@ -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()
}),
Expand All @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 2 additions & 3 deletions crates/rpc/rpc-eth-api/src/helpers/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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},
Expand Down
2 changes: 0 additions & 2 deletions crates/rpc/rpc-server-types/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <https://github.com/ethereum/go-ethereum/blob/a5a4fa7032bb248f5a7c40f4e8df2b131c4186a4/internal/ethapi/api.go#L56>
Expand Down

0 comments on commit bf80098

Please sign in to comment.