From 0317f2129171862e73db7b7228ad32105c993435 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Sat, 26 Oct 2024 23:52:50 +0400 Subject: [PATCH 1/2] fix(anvil): correctly set hardfork-specific block fields --- crates/anvil/core/src/eth/block.rs | 24 +++++++++------------ crates/anvil/src/eth/backend/executor.rs | 15 ++++++++----- crates/anvil/src/eth/backend/mem/storage.rs | 3 +-- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/anvil/core/src/eth/block.rs b/crates/anvil/core/src/eth/block.rs index 0dc386b01b1d..c9f9048b8199 100644 --- a/crates/anvil/core/src/eth/block.rs +++ b/crates/anvil/core/src/eth/block.rs @@ -2,7 +2,7 @@ use super::{ transaction::{TransactionInfo, TypedReceipt}, trie, }; -use alloy_consensus::Header; +use alloy_consensus::{Header, EMPTY_OMMER_ROOT_HASH}; use alloy_eips::eip2718::Encodable2718; use alloy_primitives::{Address, Bloom, Bytes, B256, B64, U256}; use alloy_rlp::{RlpDecodable, RlpEncodable}; @@ -34,19 +34,11 @@ impl Block { /// /// Note: if the `impersonate-tx` feature is enabled this will also accept /// `MaybeImpersonatedTransaction`. - pub fn new( - partial_header: PartialHeader, - transactions: impl IntoIterator, - ommers: Vec
, - ) -> Self + pub fn new(partial_header: PartialHeader, transactions: impl IntoIterator) -> Self where T: Into, { let transactions: Vec<_> = transactions.into_iter().map(Into::into).collect(); - let mut encoded_ommers: Vec = Vec::new(); - alloy_rlp::encode_list(&ommers, &mut encoded_ommers); - let ommers_hash = - B256::from_slice(alloy_primitives::utils::keccak256(encoded_ommers).as_slice()); let transactions_root = trie::ordered_trie_root(transactions.iter().map(|r| r.encoded_2718())); @@ -54,7 +46,7 @@ impl Block { header: Header { parent_hash: partial_header.parent_hash, beneficiary: partial_header.beneficiary, - ommers_hash, + ommers_hash: EMPTY_OMMER_ROOT_HASH, state_root: partial_header.state_root, transactions_root, receipts_root: partial_header.receipts_root, @@ -66,16 +58,16 @@ impl Block { timestamp: partial_header.timestamp, extra_data: partial_header.extra_data, mix_hash: partial_header.mix_hash, - withdrawals_root: None, + withdrawals_root: partial_header.withdrawals_root, blob_gas_used: partial_header.blob_gas_used, excess_blob_gas: partial_header.excess_blob_gas, parent_beacon_block_root: partial_header.parent_beacon_block_root, nonce: partial_header.nonce, base_fee_per_gas: partial_header.base_fee, - requests_hash: None, + requests_hash: partial_header.requests_hash, }, transactions, - ommers, + ommers: vec![], } } } @@ -100,6 +92,8 @@ pub struct PartialHeader { pub parent_beacon_block_root: Option, pub nonce: B64, pub base_fee: Option, + pub withdrawals_root: Option, + pub requests_hash: Option, } impl From
for PartialHeader { @@ -122,6 +116,8 @@ impl From
for PartialHeader { blob_gas_used: value.blob_gas_used, excess_blob_gas: value.excess_blob_gas, parent_beacon_block_root: value.parent_beacon_block_root, + requests_hash: value.requests_hash, + withdrawals_root: value.withdrawals_root, } } } diff --git a/crates/anvil/src/eth/backend/executor.rs b/crates/anvil/src/eth/backend/executor.rs index 3ceee8b04e94..4129571480ae 100644 --- a/crates/anvil/src/eth/backend/executor.rs +++ b/crates/anvil/src/eth/backend/executor.rs @@ -8,8 +8,10 @@ use crate::{ mem::inspector::Inspector, PrecompileFactory, }; -use alloy_consensus::{Header, Receipt, ReceiptWithBloom}; -use alloy_eips::eip2718::Encodable2718; +use alloy_consensus::{ + constants::EMPTY_WITHDRAWALS, Receipt, ReceiptWithBloom, +}; +use alloy_eips::{eip2718::Encodable2718, eip7685::EMPTY_REQUESTS_HASH}; use alloy_primitives::{Bloom, BloomInput, Log, B256}; use anvil_core::eth::{ block::{Block, BlockInfo, PartialHeader}, @@ -134,7 +136,9 @@ impl TransactionExecutor<'_, DB, V> { None }; + let is_shanghai = self.cfg_env.handler_cfg.spec_id >= SpecId::SHANGHAI; let is_cancun = self.cfg_env.handler_cfg.spec_id >= SpecId::CANCUN; + let is_prague = self.cfg_env.handler_cfg.spec_id >= SpecId::PRAGUE; let excess_blob_gas = if is_cancun { self.block_env.get_blob_excess_gas() } else { None }; let mut cumulative_blob_gas_used = if is_cancun { Some(0u64) } else { None }; @@ -208,7 +212,6 @@ impl TransactionExecutor<'_, DB, V> { transactions.push(transaction.pending_transaction.transaction.clone()); } - let ommers: Vec
= Vec::new(); let receipts_root = trie::ordered_trie_root(receipts.iter().map(Encodable2718::encoded_2718)); @@ -227,12 +230,14 @@ impl TransactionExecutor<'_, DB, V> { mix_hash: Default::default(), nonce: Default::default(), base_fee, - parent_beacon_block_root: Default::default(), + parent_beacon_block_root: is_cancun.then_some(Default::default()), blob_gas_used: cumulative_blob_gas_used, excess_blob_gas, + withdrawals_root: is_shanghai.then_some(EMPTY_WITHDRAWALS), + requests_hash: is_prague.then_some(EMPTY_REQUESTS_HASH), }; - let block = Block::new(partial_header, transactions.clone(), ommers); + let block = Block::new(partial_header, transactions.clone()); let block = BlockInfo { block, transactions: transaction_infos, receipts }; ExecutedTransactions { block, included, invalid } } diff --git a/crates/anvil/src/eth/backend/mem/storage.rs b/crates/anvil/src/eth/backend/mem/storage.rs index 942ce5a9d23c..43dea81fad9d 100644 --- a/crates/anvil/src/eth/backend/mem/storage.rs +++ b/crates/anvil/src/eth/backend/mem/storage.rs @@ -274,7 +274,7 @@ impl BlockchainStorage { excess_blob_gas: env.block.get_blob_excess_gas(), ..Default::default() }; - let block = Block::new::(partial_header, vec![], vec![]); + let block = Block::new::(partial_header, vec![]); let genesis_hash = block.header.hash_slow(); let best_hash = genesis_hash; let best_number: U64 = U64::from(0u64); @@ -696,7 +696,6 @@ mod tests { let block = Block::new::( partial_header.clone(), vec![tx.clone()], - vec![], ); let block_hash = block.header.hash_slow(); dump_storage.blocks.insert(block_hash, block); From 9fa8329864728dcd32daaf70e704b9000589e27b Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Sat, 26 Oct 2024 23:56:05 +0400 Subject: [PATCH 2/2] fmt --- crates/anvil/src/eth/backend/executor.rs | 4 +--- crates/anvil/src/eth/backend/mem/storage.rs | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/anvil/src/eth/backend/executor.rs b/crates/anvil/src/eth/backend/executor.rs index 4129571480ae..a00afd962878 100644 --- a/crates/anvil/src/eth/backend/executor.rs +++ b/crates/anvil/src/eth/backend/executor.rs @@ -8,9 +8,7 @@ use crate::{ mem::inspector::Inspector, PrecompileFactory, }; -use alloy_consensus::{ - constants::EMPTY_WITHDRAWALS, Receipt, ReceiptWithBloom, -}; +use alloy_consensus::{constants::EMPTY_WITHDRAWALS, Receipt, ReceiptWithBloom}; use alloy_eips::{eip2718::Encodable2718, eip7685::EMPTY_REQUESTS_HASH}; use alloy_primitives::{Bloom, BloomInput, Log, B256}; use anvil_core::eth::{ diff --git a/crates/anvil/src/eth/backend/mem/storage.rs b/crates/anvil/src/eth/backend/mem/storage.rs index 43dea81fad9d..2105666a4e91 100644 --- a/crates/anvil/src/eth/backend/mem/storage.rs +++ b/crates/anvil/src/eth/backend/mem/storage.rs @@ -693,10 +693,8 @@ mod tests { let bytes_first = &mut &hex::decode("f86b02843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba00eb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5aea03a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18").unwrap()[..]; let tx: MaybeImpersonatedTransaction = TypedTransaction::decode(&mut &bytes_first[..]).unwrap().into(); - let block = Block::new::( - partial_header.clone(), - vec![tx.clone()], - ); + let block = + Block::new::(partial_header.clone(), vec![tx.clone()]); let block_hash = block.header.hash_slow(); dump_storage.blocks.insert(block_hash, block);