Skip to content

Commit

Permalink
fix(anvil): correctly set hardfork-specific block fields (#9202)
Browse files Browse the repository at this point in the history
* fix(anvil): correctly set hardfork-specific block fields

* fmt
  • Loading branch information
klkvr authored Oct 26, 2024
1 parent 6913a3d commit 1229278
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
24 changes: 10 additions & 14 deletions crates/anvil/core/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -34,27 +34,19 @@ impl Block {
///
/// Note: if the `impersonate-tx` feature is enabled this will also accept
/// `MaybeImpersonatedTransaction`.
pub fn new<T>(
partial_header: PartialHeader,
transactions: impl IntoIterator<Item = T>,
ommers: Vec<Header>,
) -> Self
pub fn new<T>(partial_header: PartialHeader, transactions: impl IntoIterator<Item = T>) -> Self
where
T: Into<Transaction>,
{
let transactions: Vec<_> = transactions.into_iter().map(Into::into).collect();
let mut encoded_ommers: Vec<u8> = 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()));

Self {
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,
Expand All @@ -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![],
}
}
}
Expand All @@ -100,6 +92,8 @@ pub struct PartialHeader {
pub parent_beacon_block_root: Option<B256>,
pub nonce: B64,
pub base_fee: Option<u64>,
pub withdrawals_root: Option<B256>,
pub requests_hash: Option<B256>,
}

impl From<Header> for PartialHeader {
Expand All @@ -122,6 +116,8 @@ impl From<Header> 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,
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions crates/anvil/src/eth/backend/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ 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},
Expand Down Expand Up @@ -134,7 +134,9 @@ impl<DB: Db + ?Sized, V: TransactionValidator> 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 };

Expand Down Expand Up @@ -208,7 +210,6 @@ impl<DB: Db + ?Sized, V: TransactionValidator> TransactionExecutor<'_, DB, V> {
transactions.push(transaction.pending_transaction.transaction.clone());
}

let ommers: Vec<Header> = Vec::new();
let receipts_root =
trie::ordered_trie_root(receipts.iter().map(Encodable2718::encoded_2718));

Expand All @@ -227,12 +228,14 @@ impl<DB: Db + ?Sized, V: TransactionValidator> 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 }
}
Expand Down
9 changes: 3 additions & 6 deletions crates/anvil/src/eth/backend/mem/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl BlockchainStorage {
excess_blob_gas: env.block.get_blob_excess_gas(),
..Default::default()
};
let block = Block::new::<MaybeImpersonatedTransaction>(partial_header, vec![], vec![]);
let block = Block::new::<MaybeImpersonatedTransaction>(partial_header, vec![]);
let genesis_hash = block.header.hash_slow();
let best_hash = genesis_hash;
let best_number: U64 = U64::from(0u64);
Expand Down Expand Up @@ -693,11 +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::<MaybeImpersonatedTransaction>(
partial_header.clone(),
vec![tx.clone()],
vec![],
);
let block =
Block::new::<MaybeImpersonatedTransaction>(partial_header.clone(), vec![tx.clone()]);
let block_hash = block.header.hash_slow();
dump_storage.blocks.insert(block_hash, block);

Expand Down

0 comments on commit 1229278

Please sign in to comment.