diff --git a/Cargo.lock b/Cargo.lock index aa99411ae58..3be4969cb88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8933,6 +8933,7 @@ dependencies = [ "reth-firehose-tests", "reth-lru", "reth-metrics", + "reth-network", "reth-network-p2p", "reth-node-ethereum", "reth-payload-builder", diff --git a/crates/engine/tree/Cargo.toml b/crates/engine/tree/Cargo.toml index c57529b84a7..c8b645aa4d3 100644 --- a/crates/engine/tree/Cargo.toml +++ b/crates/engine/tree/Cargo.toml @@ -93,6 +93,8 @@ reth-tracing.workspace = true reth-node-ethereum.workspace = true reth-e2e-test-utils.workspace = true reth-firehose-tests.workspace = true +reth-network = { workspace = true, features = ["test-utils"] } +reth-provider = { workspace = true, features = ["test-utils"] } assert_matches.workspace = true eyre.workspace = true diff --git a/crates/engine/tree/src/tree/payload_validator.rs b/crates/engine/tree/src/tree/payload_validator.rs index ebddc0b8123..79401aea721 100644 --- a/crates/engine/tree/src/tree/payload_validator.rs +++ b/crates/engine/tree/src/tree/payload_validator.rs @@ -846,6 +846,18 @@ where .ok() }; + // Firehose live path: the EIP-7928 block access list RLP for the emitted header, taken + // before `validate_post_execution` consumes the built list. + let fh_bal_rlp = fh_tracer + .is_some() + .then(|| { + firehose_bal_rlp( + decoded_bal.as_ref().map(|bal| bal.as_raw_bal().as_raw().as_ref()), + built_bal.as_ref(), + ) + }) + .flatten(); + ensure_ok_post_block!( self.validate_post_execution( &block, @@ -968,12 +980,10 @@ where // fallible step so any earlier failure instead drops the guard (emitting // `on_block_end(Some(err))`) and discards the block. if let Some(mut guard) = fh_tracer.take() { - // EIP-7928: patch in the block access list RLP bytes carried by the payload sidecar - // (already hash-verified above via `bal_path_eligible`/execution). Unlike the rest of - // the header, this isn't known at `FirehoseBlockTracer::start` time, so it has to be - // patched into the already-buffered block via `Tracer::block_mut` before flush. - if let Some(bal_bytes) = - decoded_bal.as_ref().map(|bal| bal.as_raw_bal().as_raw().to_vec()) && + // EIP-7928: patch in the block access list RLP (see `firehose_bal_rlp`). Unlike the + // rest of the header, this isn't known at `FirehoseBlockTracer::start` time, so it has + // to be patched into the already-buffered block via `Tracer::block_mut` before flush. + if let Some(bal_bytes) = fh_bal_rlp && let Some(header) = guard.tracer_mut().block_mut().and_then(|block| block.header.as_mut()) { @@ -2461,3 +2471,41 @@ struct ExecutedBal { /// evm2 form, shared with the executed block so consumers can reuse it. evm: Arc, } + +/// Picks the EIP-7928 block access list RLP for the Firehose header: the payload's sidecar when +/// there is one, else the list this execution built. +/// +/// A block the engine downloads can carry a `block_access_list_hash` and no sidecar. The built +/// list is checked against the header hash in `validate_post_execution` before the Firehose +/// flush, so the emitted bytes match the header either way. +fn firehose_bal_rlp(sidecar: Option<&[u8]>, built: Option<&BlockAccessList>) -> Option> { + sidecar.map(<[u8]>::to_vec).or_else(|| built.map(alloy_rlp::encode)) +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_eip7928::AccountChanges; + + fn built() -> BlockAccessList { + vec![AccountChanges::new(Address::repeat_byte(1))] + } + + #[test] + fn firehose_bal_rlp_prefers_the_sidecar() { + // The empty-list RLP: different bytes from the built list, so the sidecar must win. + let sidecar = alloy_rlp::encode(Vec::::new()); + assert_eq!(firehose_bal_rlp(Some(&sidecar), Some(&built())), Some(sidecar)); + } + + #[test] + fn firehose_bal_rlp_uses_the_built_list_without_a_sidecar() { + let built = built(); + assert_eq!(firehose_bal_rlp(None, Some(&built)), Some(alloy_rlp::encode(&built))); + } + + #[test] + fn firehose_bal_rlp_is_none_without_a_list() { + assert_eq!(firehose_bal_rlp(None, None), None); + } +} diff --git a/crates/engine/tree/tests/firehose_live_tracing_bal_without_sidecar.rs b/crates/engine/tree/tests/firehose_live_tracing_bal_without_sidecar.rs new file mode 100644 index 00000000000..3d6277bfd77 --- /dev/null +++ b/crates/engine/tree/tests/firehose_live_tracing_bal_without_sidecar.rs @@ -0,0 +1,97 @@ +//! Regression test: the tracer emits the block access list of a downloaded Amsterdam block that +//! arrives without its BAL sidecar. +//! +//! A peer serves the block body but no BAL, so the engine downloads the block, executes it, builds +//! its access list, and checks that list against the header's `block_access_list_hash`. The +//! emitted Firehose header must then carry the RLP of the built list. Without the fix, the +//! validator passes only a sidecar's BAL to the tracer, and the header has no BAL RLP. +//! +//! It lives in its own integration-test binary because it installs the process-wide tracer. + +use std::{sync::Arc, time::Duration}; + +use alloy_primitives::keccak256; +use alloy_rpc_types_engine::{ForkchoiceState, PayloadStatusEnum}; +use futures::StreamExt; +use reth_chainspec::EthereumHardfork; +use reth_e2e_test_utils::{test_chain_spec, transaction::TransactionTestContext, E2ETestSetupExt}; +use reth_engine_primitives::ConsensusEngineEvent; +use reth_ethereum_primitives::EthPrimitives; +use reth_firehose::init_tracer_with_buffer; +use reth_firehose_tests::parse_fire_block_for; +use reth_network::{test_utils::Testnet, Peers}; +use reth_node_ethereum::EthereumNode; +use reth_payload_primitives::BuiltPayload; +use reth_provider::test_utils::MockEthProvider; + +#[tokio::test(flavor = "multi_thread")] +async fn emits_the_built_bal_of_a_block_downloaded_without_a_sidecar() -> eyre::Result<()> { + let buffer = init_tracer_with_buffer(1, Some(0), Some(0), Some(0)); + let chain_spec = test_chain_spec(EthereumHardfork::Amsterdam); + + let (mut node, wallet) = EthereumNode::test_setup(1, chain_spec.clone()).build_single().await?; + + // Build a valid Amsterdam block without submitting it to the engine. + let raw_tx = TransactionTestContext::transfer_tx_bytes(1, wallet.inner).await; + node.rpc.inject_tx(raw_tx).await?; + let payload = node.new_payload().await?; + let bal = payload.block_access_list().cloned().expect("an Amsterdam payload has a BAL"); + let block = payload.block().clone(); + let block_number = block.header().number; + let block_hash = block.hash(); + let bal_hash = block.header().block_access_list_hash.expect("an Amsterdam header has one"); + let genesis_hash = node.block_hash(0); + + // The peer serves the block but not its BAL, so the node downloads it without a sidecar. + let peer_provider = Arc::new( + MockEthProvider::::new() + .with_chain_spec((*chain_spec).clone()) + .with_genesis_block(), + ); + peer_provider.add_block(block_hash, block.into_block()); + + let testnet = Testnet::create_with(1, peer_provider).await.with_request_handlers().spawn(); + let [peer] = testnet.peers_array(); + + node.inner.network.add_peer(*peer.peer_id(), peer.local_addr()); + assert_eq!(node.network.next_session_established().await, Some(*peer.peer_id())); + + let mut engine_events = node.inner.add_ons_handle.consensus_engine_events().new_listener(); + let state = ForkchoiceState { + head_block_hash: block_hash, + safe_block_hash: genesis_hash, + finalized_block_hash: genesis_hash, + }; + let response = + node.inner.add_ons_handle.beacon_engine_handle.fork_choice_updated(state, None).await?; + assert_eq!(response.payload_status.status, PayloadStatusEnum::Syncing); + + tokio::time::timeout(Duration::from_secs(20), async { + while let Some(event) = engine_events.next().await { + match event { + ConsensusEngineEvent::CanonicalBlockAdded(block, _) + if block.recovered_block().hash() == block_hash => + { + return + } + ConsensusEngineEvent::InvalidBlock { block, error } + if block.hash() == block_hash => + { + panic!("downloaded block is invalid: {error}") + } + _ => {} + } + } + panic!("consensus engine event stream ended") + }) + .await?; + + let emitted = parse_fire_block_for(&buffer.get_bytes(), block_number)?; + assert_eq!(emitted.hash, block_hash.to_vec(), "emitted another block"); + let header = emitted.header.as_ref().expect("emitted block has a header"); + let bal_rlp = header.block_access_list_rlp.as_deref().expect("emitted no BAL RLP"); + assert_eq!(bal_rlp, bal.as_ref(), "emitted another BAL"); + assert_eq!(keccak256(bal_rlp), bal_hash, "BAL RLP does not hash to the header's"); + + Ok(()) +}