diff --git a/Cargo.lock b/Cargo.lock index aa99411ae58..d63fb44c568 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9535,10 +9535,13 @@ name = "reth-firehose-tests" version = "2.11.0" dependencies = [ "alloy-consensus", + "alloy-eip7928", "alloy-eips", "alloy-genesis", "alloy-primitives", "alloy-rlp", + "alloy-signer", + "alloy-signer-local", "base64 0.22.1", "evm2", "eyre", diff --git a/crates/engine/tree/tests/firehose_live_tracing_amsterdam_validation.rs b/crates/engine/tree/tests/firehose_live_tracing_amsterdam_validation.rs new file mode 100644 index 00000000000..2bfa4a3017b --- /dev/null +++ b/crates/engine/tree/tests/firehose_live_tracing_amsterdam_validation.rs @@ -0,0 +1,196 @@ +//! Regression test: a second node validates Amsterdam blocks on the Firehose-traced engine path. +//! +//! While the tracer is installed, the engine validates every payload on the traced path. On an +//! Amsterdam block that path must build the block access list, so post-execution validation can +//! check it against the header's `block_access_list_hash`. The check passes only when the traced +//! path builds the same access list as the block builder did. Unlike +//! `firehose_live_tracing_amsterdam`, the blocks come from another node, so the validating node +//! sees each block only through `engine_newPayload`. +//! +//! Each block below covers one case where the traced executor's extra state reads or balance +//! events could make its access list differ from the builder's. +//! +//! It lives in its own integration-test binary because it installs the process-wide tracer. + +use std::sync::{Arc, Mutex}; + +use alloy_eips::eip4895::Withdrawal; +use alloy_primitives::{address, hex, keccak256, Address, Bytes, TxKind, U256}; +use alloy_rpc_types_engine::PayloadStatusEnum; +use alloy_rpc_types_eth::{TransactionInput, TransactionRequest}; +use reth_chainspec::EthereumHardfork; +use reth_e2e_test_utils::{ + eth_payload_attributes, test_chain_spec, transaction::TransactionTestContext, wallet::Wallet, + E2ETestSetupExt, NodeHelperType, +}; +use reth_firehose::init_tracer_with_buffer; +use reth_firehose_tests::parse_fire_block_for; +use reth_node_ethereum::EthereumNode; + +/// Recipient of the withdrawal block's withdrawals. +const WITHDRAWAL_RECIPIENT: Address = address!("0x00000000000000000000000000000000000000aa"); + +#[tokio::test(flavor = "multi_thread")] +async fn traced_engine_path_accepts_amsterdam_blocks() -> eyre::Result<()> { + let buffer = init_tracer_with_buffer(1, Some(0), Some(0), Some(0)); + + // The builder's next payload takes its withdrawals from here. + let withdrawals = Arc::new(Mutex::new(Vec::::new())); + let chain_spec = test_chain_spec(EthereumHardfork::Amsterdam); + let generator_withdrawals = Arc::clone(&withdrawals); + let (mut nodes, wallet) = EthereumNode::test_setup_for(EthereumHardfork::Amsterdam) + .with_num_nodes(2) + .with_attributes_generator(move |timestamp| { + let mut attributes = eth_payload_attributes(&*chain_spec, timestamp); + attributes.withdrawals = Some(generator_withdrawals.lock().unwrap().clone()); + attributes + }) + .build() + .await?; + let validator = nodes.pop().unwrap(); + let builder = nodes.pop().unwrap(); + let mut chain = Chain { builder, validator, buffer, withdrawals, wallet, nonce: 0 }; + + // A plain transfer. + let tx = chain.tx(TxKind::Call(Address::with_last_byte(1)), U256::from(1), Bytes::new(), 1); + chain.block("transfer", vec![tx], Vec::new()).await?; + + // A zero priority fee leaves the coinbase untouched. + let tx = chain.tx(TxKind::Call(Address::with_last_byte(1)), U256::from(1), Bytes::new(), 0); + chain.block("zero priority fee", vec![tx], Vec::new()).await?; + + // Nonzero withdrawals, two to one address, and a zero-amount one. + let withdrawal = + |index, address, amount| Withdrawal { index, validator_index: index, address, amount }; + let block_withdrawals = vec![ + withdrawal(0, WITHDRAWAL_RECIPIENT, 1_000), + withdrawal(1, WITHDRAWAL_RECIPIENT, 2_000), + withdrawal(2, Address::with_last_byte(0xbb), 0), + withdrawal(3, Address::with_last_byte(0xcc), 3_000), + ]; + chain.block("withdrawals", Vec::new(), block_withdrawals).await?; + + // A reverted transaction (init code `REVERT(0, 0)`) and one that runs out of gas (init code + // loops forever). + let reverted = chain.tx(TxKind::Create, U256::ZERO, hex!("60006000fd").into(), 1); + let out_of_gas = chain.tx(TxKind::Create, U256::ZERO, hex!("5b600056").into(), 1); + chain.block("reverted and out of gas", vec![reverted, out_of_gas], Vec::new()).await?; + + // A CREATE that deploys the one-byte runtime code 0xff. + let create = chain.tx(TxKind::Create, U256::ZERO, hex!("60ff60005360016000f3").into(), 1); + chain.block("create", vec![create], Vec::new()).await?; + + // A block with no transactions. + chain.block("empty", Vec::new(), Vec::new()).await?; + + // CREATE and SELFDESTRUCT in one transaction: the init code sends the endowment to 0x0. + let tx = chain.tx(TxKind::Create, U256::from(7), hex!("6000ff").into(), 1); + chain.block("create and selfdestruct", vec![tx], Vec::new()).await?; + + // Many transactions from one sender. At 30 or more transactions the engine converts them on + // rayon, and on the parallel BAL path it sends them in completion order. The traced path + // executes them one after another, so it must get them in block order, or a later nonce + // runs first and the block fails with "nonce too high". Seen on Sepolia block 11856337. + let txs = (0..64) + .map(|_| chain.tx(TxKind::Call(Address::with_last_byte(1)), U256::from(1), Bytes::new(), 1)) + .collect(); + chain.block("one sender, many transactions", txs, Vec::new()).await?; + + Ok(()) +} + +/// Two nodes: one builds each block, the other validates it on the traced path. +struct Chain { + builder: NodeHelperType, + validator: NodeHelperType, + buffer: firehose_tracer::InMemoryBuffer, + withdrawals: Arc>>, + wallet: Wallet, + nonce: u64, +} + +impl Chain { + /// Signs the wallet's next transaction. + fn tx( + &mut self, + to: TxKind, + value: U256, + input: Bytes, + priority_fee: u128, + ) -> TransactionRequest { + let request = TransactionRequest { + nonce: Some(self.nonce), + chain_id: Some(1), + gas: Some(100_000), + max_fee_per_gas: Some(1_000_000_000_000), + max_priority_fee_per_gas: Some(priority_fee), + to: Some(to), + value: Some(value), + input: TransactionInput::new(input), + ..Default::default() + }; + self.nonce += 1; + request + } + + /// Builds a block with `txs` and `withdrawals`, then checks that the validator accepts it on + /// the traced path and emits it once with its block access list hash. + async fn block( + &mut self, + case: &str, + txs: Vec, + withdrawals: Vec, + ) -> eyre::Result<()> { + let tx_count = txs.len(); + for request in txs { + let raw = + TransactionTestContext::sign_tx_bytes(self.wallet.inner.clone(), request).await; + self.builder.rpc.inject_tx(raw).await?; + } + *self.withdrawals.lock().unwrap() = withdrawals; + + let payload = self.builder.new_payload().await?; + let block = payload.block().clone(); + let hash = block.hash(); + assert!(block.header().block_access_list_hash.is_some(), "{case}: not an Amsterdam block"); + assert_eq!(block.body().transactions.len(), tx_count, "{case}: builder dropped a tx"); + + let before = fire_block_count(&self.buffer.get_bytes()); + let status = self + .validator + .inner + .add_ons_handle + .beacon_engine_handle + .new_payload(payload.clone().into()) + .await?; + assert_eq!(status.status, PayloadStatusEnum::Valid, "{case}: traced path rejected it"); + let output = self.buffer.get_bytes(); + assert_eq!( + fire_block_count(&output), + before + 1, + "{case}: traced path did not emit the block once" + ); + let emitted = parse_fire_block_for(&output, block.header().number)?; + assert_eq!(emitted.hash, hash.to_vec(), "{case}: emitted another block"); + let header = emitted.header.as_ref().expect("emitted block has a header"); + let bal_hash = block.header().block_access_list_hash.expect("checked above"); + assert_eq!( + header.block_access_list_hash.as_deref(), + Some(bal_hash.as_slice()), + "{case}: emitted the wrong BAL hash" + ); + let bal_rlp = header.block_access_list_rlp.as_deref().expect("emitted no BAL RLP"); + assert_eq!(keccak256(bal_rlp), bal_hash, "{case}: BAL RLP does not hash to the header's"); + assert_eq!(header.slot_number, block.header().slot_number, "{case}: wrong slot number"); + + self.validator.update_forkchoice(hash, hash).await?; + self.builder.submit_payload(payload).await?; + self.builder.update_forkchoice(hash, hash).await?; + Ok(()) + } +} + +/// Counts the `FIRE BLOCK` lines in the tracer output. +fn fire_block_count(output: &[u8]) -> usize { + String::from_utf8_lossy(output).lines().filter(|line| line.starts_with("FIRE BLOCK ")).count() +} diff --git a/crates/firehose-tests/Cargo.toml b/crates/firehose-tests/Cargo.toml index f1013b3bc88..73fd0a7ada5 100644 --- a/crates/firehose-tests/Cargo.toml +++ b/crates/firehose-tests/Cargo.toml @@ -41,6 +41,11 @@ prost = "0.14" serde = { workspace = true, features = ["derive"] } serde_json.workspace = true +[dev-dependencies] +alloy-eip7928.workspace = true +alloy-signer.workspace = true +alloy-signer-local.workspace = true + [[test]] name = "prestate" path = "tests/prestate.rs" @@ -52,3 +57,15 @@ path = "tests/batch_executor.rs" [[test]] name = "staged_sync_bal" path = "tests/staged_sync_bal.rs" + +[[test]] +name = "setup_guards" +path = "tests/setup_guards.rs" + +[[test]] +name = "pipeline_trace" +path = "tests/pipeline_trace.rs" + +[[test]] +name = "bal_pipeline" +path = "tests/bal_pipeline.rs" diff --git a/crates/firehose-tests/tests/bal_pipeline.rs b/crates/firehose-tests/tests/bal_pipeline.rs new file mode 100644 index 00000000000..aee80207e98 --- /dev/null +++ b/crates/firehose-tests/tests/bal_pipeline.rs @@ -0,0 +1,174 @@ +//! EIP-7928 on the pipeline path: `run_wrapped_block` rebuilds the block access list during +//! re-execution, so its block access index must advance exactly as the stock +//! `BasicBlockExecutor::execute_one` advances it. This test runs a multi-transaction Amsterdam +//! block through the stock executor first to get the reference hash, then through +//! `run_wrapped_block`, and checks that the rebuilt list matches that hash and is emitted as RLP. +//! The single-transaction fixtures cannot catch an index that is off for later transactions. + +use std::{path::PathBuf, sync::Arc}; + +use alloy_consensus::{ + constants::EMPTY_WITHDRAWALS, proofs::calculate_transaction_root, Header, SignableTransaction, + TxEip1559, EMPTY_OMMER_ROOT_HASH, +}; +use alloy_eips::eip4895::Withdrawals; +use alloy_primitives::{address, hex, keccak256, Address, Bytes, TxKind, B256, U256}; +use alloy_signer::SignerSync; +use alloy_signer_local::PrivateKeySigner; +use evm2::evm::InMemoryDB; +use reth_chainspec::ChainSpec; +use reth_ethereum_primitives::{Block, BlockBody, EthPrimitives, TransactionSigned}; +use reth_evm::execute::{BasicBlockExecutor, Executor}; +use reth_evm_ethereum::EthEvmConfig; +use reth_firehose::{ + record_block_access_list, run_wrapped_block, FirehoseBlockTracer, NoPostTxExtras, NoPreTxAdjust, +}; +use reth_firehose_tests::prestate::{parse_fire_block_for, seed_cache_db, Prestate}; +use reth_primitives_traits::{Block as _, RecoveredBlock}; + +const RECIPIENT: Address = address!("0x000000000000000000000000000000000000beef"); + +/// Runtime code: `SSTORE(1, 0x2a)`. +const RUNTIME: [u8; 6] = hex!("602a60015500"); + +#[test] +fn pipeline_rebuilds_bal_of_a_block_with_transactions() { + let signer = PrivateKeySigner::from_bytes(&B256::repeat_byte(0x42)).unwrap(); + let sender = signer.address(); + + // Amsterdam genesis from the prestate fixture, with the sender funded. + let case = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/cases/amsterdam_block_access_list/prestate.json"); + let mut prestate: Prestate = serde_json::from_slice(&std::fs::read(case).unwrap()).unwrap(); + prestate.genesis.alloc.insert( + sender, + alloy_genesis::GenesisAccount::default().with_balance(U256::from(10u128.pow(20))), + ); + let chain_spec = Arc::new(ChainSpec::from(prestate.genesis.clone())); + + // Init code: `SSTORE(0, 7)`, then return `RUNTIME`. + let mut initcode = hex!("6007600055" "6006601160003960066000f3").to_vec(); + initcode.extend_from_slice(&RUNTIME); + let created = sender.create(1); + + let txs = [ + (TxKind::Call(RECIPIENT), U256::from(10u128.pow(18)), Bytes::new()), + (TxKind::Create, U256::from(5), Bytes::from(initcode)), + (TxKind::Call(created), U256::ZERO, Bytes::new()), + ]; + let transactions: Vec = txs + .into_iter() + .enumerate() + .map(|(nonce, (to, value, input))| { + let tx = TxEip1559 { + chain_id: prestate.genesis.config.chain_id, + nonce: nonce as u64, + gas_limit: 1_000_000, + max_fee_per_gas: 1_000_000_000, + max_priority_fee_per_gas: 1, + to, + value, + input, + ..Default::default() + }; + let signature = signer.sign_hash_sync(&tx.signature_hash()).unwrap(); + TransactionSigned::new_unhashed(tx.into(), signature) + }) + .collect(); + + // Stock executor, with a placeholder hash that only arms its BAL builder. + let placeholder = block(&prestate, &transactions, B256::repeat_byte(0x11)); + let mut stock = + BasicBlockExecutor::new(EthEvmConfig::new(chain_spec.clone()), state(&prestate)); + let stock_result = + stock.execute_one(&placeholder).expect("the stock executor executes the block"); + assert!( + stock_result.receipts.iter().all(|r| r.success), + "every transaction must succeed, or the block tests less than it claims" + ); + let expected = alloy_eip7928::compute_block_access_list_hash( + &stock.take_bal().expect("the stock executor builds a BAL"), + ); + + // The same block, now declaring the real hash, through the Firehose pipeline path. + let block = block(&prestate, &transactions, expected); + let (tracer, buffer) = firehose_tracer::Tracer::with_buffer( + firehose_tracer::config::Config::default(), + firehose_tracer::config::ChainConfig { + chain_id: prestate.genesis.config.chain_id, + shanghai_time: Some(0), + cancun_time: Some(0), + prague_time: Some(0), + verkle_time: None, + }, + "reth-firehose-tests", + env!("CARGO_PKG_VERSION"), + ); + let mut guard = FirehoseBlockTracer::start_local::( + Box::new(tracer), + block.sealed_block(), + None, + ); + run_wrapped_block( + &EthEvmConfig::new(chain_spec), + state(&prestate), + &block, + &mut guard, + NoPreTxAdjust, + NoPostTxExtras, + ) + .and_then(|(output, bal)| { + record_block_access_list(&block, bal.as_ref(), &mut guard)?; + Ok(output) + }) + .expect("the rebuilt BAL must match the stock executor's hash"); + guard.mark_verified(); + + let emitted = parse_fire_block_for(&buffer.get_bytes(), block.header().number).unwrap(); + let header = emitted.header.expect("emitted block has a header"); + assert_eq!(header.block_access_list_hash.as_deref(), Some(expected.as_slice())); + let rlp = header.block_access_list_rlp.expect("BAL RLP is emitted"); + assert_eq!(keccak256(&rlp), expected, "the emitted RLP must hash to the header's BAL hash"); + assert_eq!(emitted.transaction_traces.len(), 3); +} + +fn state(prestate: &Prestate) -> InMemoryDB { + let mut db = InMemoryDB::default(); + seed_cache_db(&mut db, &prestate.genesis).unwrap(); + db +} + +fn block( + prestate: &Prestate, + transactions: &[TransactionSigned], + bal_hash: B256, +) -> RecoveredBlock { + let ctx = &prestate.context; + let header = Header { + parent_hash: B256::ZERO, + ommers_hash: EMPTY_OMMER_ROOT_HASH, + beneficiary: ctx.miner, + transactions_root: calculate_transaction_root(transactions), + withdrawals_root: Some(EMPTY_WITHDRAWALS), + number: ctx.number, + timestamp: ctx.timestamp, + gas_limit: ctx.gas_limit, + base_fee_per_gas: ctx.base_fee_per_gas.map(|v| v as u64), + parent_beacon_block_root: Some(B256::ZERO), + blob_gas_used: Some(0), + excess_blob_gas: Some(0), + slot_number: Some(1), + block_access_list_hash: Some(bal_hash), + ..Default::default() + }; + Block { + header, + body: BlockBody { + transactions: transactions.to_vec(), + ommers: vec![], + withdrawals: Some(Withdrawals::default()), + }, + } + .try_into_recovered() + .unwrap() +} diff --git a/crates/firehose-tests/tests/pipeline_trace.rs b/crates/firehose-tests/tests/pipeline_trace.rs new file mode 100644 index 00000000000..1605848a991 --- /dev/null +++ b/crates/firehose-tests/tests/pipeline_trace.rs @@ -0,0 +1,41 @@ +//! Regression test: the pipeline path emits the same Firehose block as the golden. +//! +//! The prestate goldens come from `run_wrapped_block` with a local tracer. Staged sync instead +//! runs [`Executor::execute_and_trace_one`] with the process-wide tracer and flushes the block +//! only in `into_state` or on the next block. This test runs a prestate block through that path +//! and checks the emitted block against the same golden. +//! +//! It lives in its own integration-test binary because it installs the process-wide tracer. + +use std::path::PathBuf; + +use reth_evm::execute::Executor; +use reth_evm_ethereum::EthEvmConfig; +use reth_firehose::FirehoseBlockExecutor; +use reth_firehose_tests::{ + assert_block_equals_golden, load_prestate, parse_fire_block_for, LoadedPrestate, +}; + +#[test] +fn pipeline_executor_matches_the_golden() { + let case = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/cases/nop_transfer"); + let LoadedPrestate { prestate, chain_spec, block, db } = + load_prestate(&case).expect("loading the nop_transfer prestate"); + + let config = &prestate.genesis.config; + let buffer = reth_firehose::init_tracer_with_buffer( + config.chain_id, + config.shanghai_time, + config.cancun_time, + config.prague_time, + ); + let mut executor = FirehoseBlockExecutor::new(EthEvmConfig::new(chain_spec), db); + executor.execute_and_trace_one(&block).expect("execute_and_trace_one succeeds"); + // Reaching `into_state` confirms the last block passed validation, which flushes it. + let _ = executor.into_state(); + + let fire_block = parse_fire_block_for(&buffer.get_bytes(), block.header().number) + .expect("the traced block is emitted"); + assert_block_equals_golden(&fire_block, &case.join("block.2099.binpb")) + .expect("the pipeline block matches the golden"); +} diff --git a/crates/firehose-tests/tests/setup_guards.rs b/crates/firehose-tests/tests/setup_guards.rs new file mode 100644 index 00000000000..d55c890895f --- /dev/null +++ b/crates/firehose-tests/tests/setup_guards.rs @@ -0,0 +1,24 @@ +//! Setup guard: fails with a clear message when system calls stop reaching the inspector. +//! +//! evm2 runs system calls without the inspector unless the executor opts in with +//! `set_inspect_system_calls(true)`. When that opt-in is lost, the Firehose block has no system +//! calls and every later ordinal shifts, which a golden diff shows only as an opaque mismatch. + +use std::path::PathBuf; + +use reth_firehose_tests::run_prestate; + +/// `nop_transfer` is a Cancun block whose header carries a `parentBeaconBlockRoot`, so it runs the +/// EIP-4788 beacon root system call. +#[test] +fn cancun_block_emits_beacon_root_system_call() { + let case = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/cases/nop_transfer"); + let outcome = run_prestate(&case).expect("running the nop_transfer prestate"); + + assert!( + !outcome.block.system_calls.is_empty(), + "the Firehose block of a Cancun block has no system calls, so the EIP-4788 beacon root \ + call did not reach the inspector; check that FirehoseWrappedExecutor calls \ + set_inspect_system_calls(true)", + ); +}