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
3 changes: 3 additions & 0 deletions Cargo.lock

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

196 changes: 196 additions & 0 deletions crates/engine/tree/tests/firehose_live_tracing_amsterdam_validation.rs
Original file line number Diff line number Diff line change
@@ -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::<Withdrawal>::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<EthereumNode>,
validator: NodeHelperType<EthereumNode>,
buffer: firehose_tracer::InMemoryBuffer,
withdrawals: Arc<Mutex<Vec<Withdrawal>>>,
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<TransactionRequest>,
withdrawals: Vec<Withdrawal>,
) -> 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()
}
17 changes: 17 additions & 0 deletions crates/firehose-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Loading
Loading