Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/engine/tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 54 additions & 6 deletions crates/engine/tree/src/tree/payload_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests only cover firehose_bal_rlp, not this line.

Add a test that has the node download a block from a peer that serves no BAL, with the tracer on, and checks that the emitted header has the BAL bytes.

That's the case this PR fixes, and the test should fail without it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 8ad0429: crates/engine/tree/tests/firehose_live_tracing_bal_without_sidecar.rs. With the tracer on, the node downloads an Amsterdam block from a peer that serves no BAL. The test checks that the emitted header carries the BAL RLP and that it hashes to the header's block_access_list_hash. With the payload_validator.rs changes reverted, it fails with emitted no BAL RLP. With them, it passes.

.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,
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -2461,3 +2471,41 @@ struct ExecutedBal {
/// evm2 form, shared with the executed block so consumers can reuse it.
evm: Arc<EvmBal>,
}

/// 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<Vec<u8>> {
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::<AccountChanges>::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);
}
}
Original file line number Diff line number Diff line change
@@ -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::<EthPrimitives>::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(())
}
Loading