diff --git a/crates/firehose/src/inspector/tests.rs b/crates/firehose/src/inspector/tests.rs index 369043840ec..44423af427f 100644 --- a/crates/firehose/src/inspector/tests.rs +++ b/crates/firehose/src/inspector/tests.rs @@ -2,6 +2,7 @@ //! [`support`]; [`scenario`] holds the SELFDESTRUCT harness [`selfdestruct`] drives. mod balance; +mod call_data; mod gas; mod gas_boundary; mod native_log; diff --git a/crates/firehose/src/inspector/tests/call_data.rs b/crates/firehose/src/inspector/tests/call_data.rs new file mode 100644 index 00000000000..ca25716a457 --- /dev/null +++ b/crates/firehose/src/inspector/tests/call_data.rs @@ -0,0 +1,119 @@ +//! What `firehose-tracer` keeps of a call's KECCAK256 preimages, input and return data. The +//! tracer owns these rules; these tests pin them end to end through the reth inspector, so a +//! tracer bump that changes FIRE output for downstream consumers fails here first. + +use super::support::*; +use crate::inspector::*; +use evm2::SpecId; + +const CONTRACT: Address = Address::repeat_byte(0xa1); + +/// The `identity` precompile: it returns its input, so one call moves the same number of bytes +/// as input and as return data. +const IDENTITY_PRECOMPILE: Address = + Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]); + +/// `MSTORE(0, word)` then `KECCAK256(0, 32)`, leaving the hash on the stack. +fn op_keccak_word(word: B256) -> Vec { + let mut code = Vec::new(); + code.extend(push(word.as_slice())); + code.extend(push(&[0])); + code.push(0x52); // MSTORE + code.extend(push(&[32])); + code.extend(push(&[0])); + code.push(0x20); // KECCAK256 + code +} + +/// A preimage whose hash is a written storage slot explains that slot and is kept. A preimage +/// whose hash is never used as a slot is dropped (firehose-tracer 5.5.0 keccak preimage filter). +#[test] +fn keccak_preimage_is_kept_only_when_it_explains_a_written_slot() { + let slot_word = B256::repeat_byte(0x01); + let unused_word = B256::repeat_byte(0x02); + + let mut code = op_keccak_word(slot_word); + code.extend(push(&[1])); + code.push(0x90); // SWAP1: SSTORE(key = hash, value = 1) + code.push(0x55); // SSTORE + code.extend(op_keccak_word(unused_word)); + code.push(0x50); // POP + + let block = drive_tx( + SpecId::PRAGUE, + &[(SENDER, balance_account(1_000_000)), (CONTRACT, code_account(&code, 0))], + CONTRACT, + 0, + ); + + let call = &block.transaction_traces[0].calls[0]; + let slot_hash = alloy_primitives::hex::encode(alloy_primitives::keccak256(slot_word)); + let unused_hash = alloy_primitives::hex::encode(alloy_primitives::keccak256(unused_word)); + + assert_eq!( + call.keccak_preimages.get(&slot_hash), + Some(&alloy_primitives::hex::encode(slot_word)), + "the preimage of a written storage slot must be kept" + ); + assert!( + !call.keccak_preimages.contains_key(&unused_hash), + "a preimage that explains no written slot must be dropped" + ); +} + +/// Number of identity calls the root call makes, each passing 1 MiB of input and getting +/// 1 MiB back. 55 MiB goes past both per-transaction limits (50 MiB input, 25 MiB return data). +const IDENTITY_CALLS: usize = 55; +const MIB: usize = 1024 * 1024; + +/// Past 50 MiB of internal call input, later calls keep only their 4-byte selector. Past 25 MiB +/// of return data, later calls keep none. The first internal call and the root call keep all of +/// theirs. +#[test] +fn call_input_and_return_data_are_truncated_past_the_per_tx_limits() { + // Memory word 0 starts with the selector 0xdeadbeef; writing the last word grows memory to + // 1 MiB once, so every CALL below passes the same 1 MiB without further expansion cost. + let mut code = Vec::new(); + code.extend(push(&[0xde, 0xad, 0xbe, 0xef])); + code.extend(push(&[0xe0])); + code.push(0x1b); // SHL + code.extend(push(&[0])); + code.push(0x52); // MSTORE + code.extend(push(&[1])); + code.extend(push(&((MIB - 32) as u32).to_be_bytes()[1..])); + code.push(0x52); // MSTORE + for _ in 0..IDENTITY_CALLS { + code.extend(push(&(MIB as u32).to_be_bytes()[1..])); // retLength + code.extend(push(&[0])); // retOffset + code.extend(push(&(MIB as u32).to_be_bytes()[1..])); // argsLength + code.extend(push(&[0])); // argsOffset + code.extend(push(&[0])); // value + code.extend(push(IDENTITY_PRECOMPILE.as_slice())); + code.push(0x5a); // GAS + code.push(0xf1); // CALL + code.push(0x50); // POP + } + + let block = drive_txs( + SpecId::PRAGUE, + &[(SENDER, balance_account(1_000_000)), (CONTRACT, code_account(&code, 0))], + &[DriveTx::call(CONTRACT, 0).with_gas(30_000_000)], + ); + + let calls = &block.transaction_traces[0].calls; + assert_eq!(calls.len(), IDENTITY_CALLS + 1, "root call plus one call per identity CALL"); + assert!(calls.iter().skip(1).all(|call| !call.status_failed), "all calls succeed"); + + let first = &calls[1]; + assert_eq!(first.input.len(), MIB, "the first internal call keeps its full input"); + assert_eq!(first.return_data.len(), MIB, "the first internal call keeps its return data"); + + let last = &calls[IDENTITY_CALLS]; + // Compare lengths first: a failing `assert_eq!` on a 1 MiB vector prints megabytes. + assert_eq!(last.input.len(), 4, "past 50 MiB only the selector stays"); + assert_eq!(last.input, [0xde, 0xad, 0xbe, 0xef]); + assert_eq!(last.return_data.len(), 0, "past 25 MiB no return data stays"); + assert!(last.input_truncated && last.return_data_truncated, "the cuts must be flagged"); + assert!(!first.input_truncated && !first.return_data_truncated); + assert!(!calls[0].input_truncated && !calls[0].return_data_truncated, "root call is not cut"); +} diff --git a/crates/firehose/src/mapper.rs b/crates/firehose/src/mapper.rs index 96915ad1381..2f67b4c3f26 100644 --- a/crates/firehose/src/mapper.rs +++ b/crates/firehose/src/mapper.rs @@ -376,3 +376,317 @@ fn map_signed_authorization( s: B256::new(auth.s().to_be_bytes::<32>()), } } + +#[cfg(test)] +mod tests { + use super::*; + use alloy_consensus::{ + constants::EMPTY_WITHDRAWALS, EthereumTypedTransaction, Header, Receipt, TxEip1559, + TxEip2930, TxLegacy, TxReceipt, + }; + use alloy_eips::{ + eip2930::{AccessList, AccessListItem}, + eip4895::{Withdrawal, Withdrawals}, + }; + use alloy_genesis::GenesisAccount; + use alloy_primitives::{address, b256, bytes, Signature, TxKind}; + use reth_ethereum_primitives::{Block, BlockBody}; + use reth_primitives_traits::SealedBlock; + use std::collections::BTreeMap; + + fn test_sig() -> Signature { + Signature::test_signature() + } + + fn seal_block(header: Header, body: BlockBody) -> SealedBlock { + SealedBlock::seal_slow(Block { header, body }) + } + + #[test] + fn u64_to_trimmed_bytes_strips_leading_zeros() { + assert_eq!(u64_to_trimmed_bytes(0), Bytes::copy_from_slice(&[])); + assert_eq!(u64_to_trimmed_bytes(1), bytes!("01")); + assert_eq!(u64_to_trimmed_bytes(255), bytes!("ff")); + assert_eq!(u64_to_trimmed_bytes(256), bytes!("0100")); + // EIP-155 legacy V for chain_id=1, y_parity=0: 1*2+35+0 = 37 + assert_eq!(u64_to_trimmed_bytes(37), bytes!("25")); + } + + #[test] + fn to_block_data_maps_header_fields() { + let parent = B256::repeat_byte(0x11); + let state_root = B256::repeat_byte(0x22); + let coinbase = address!("0x00000000000000000000000000000000000000aa"); + let extra = bytes!("deadbeef"); + let header = Header { + parent_hash: parent, + beneficiary: coinbase, + state_root, + number: 42, + gas_limit: 30_000_000, + gas_used: 21_000, + timestamp: 1_700_000_000, + extra_data: extra.clone(), + base_fee_per_gas: Some(7), + withdrawals_root: Some(EMPTY_WITHDRAWALS), + blob_gas_used: Some(0), + excess_blob_gas: Some(0), + parent_beacon_block_root: Some(B256::repeat_byte(0x33)), + ..Default::default() + }; + let body = BlockBody { + transactions: vec![], + ommers: vec![], + withdrawals: Some(Withdrawals::new(vec![])), + }; + let sealed = seal_block(header, body); + let data = to_block_data(&sealed); + + assert_eq!(data.number, 42); + assert_eq!(data.hash, sealed.hash()); + assert_eq!(data.parent_hash, parent); + assert_eq!(data.coinbase, coinbase); + assert_eq!(data.root, state_root); + assert_eq!(data.gas_limit, 30_000_000); + assert_eq!(data.gas_used, 21_000); + assert_eq!(data.time, 1_700_000_000); + assert_eq!(data.extra, extra); + assert_eq!(data.base_fee, Some(U256::from(7u64))); + assert_eq!(data.withdrawals_root, Some(EMPTY_WITHDRAWALS)); + assert_eq!(data.blob_gas_used, Some(0)); + assert_eq!(data.excess_blob_gas, Some(0)); + assert_eq!(data.parent_beacon_root, Some(B256::repeat_byte(0x33))); + assert!(data.uncles.is_empty()); + assert!(data.withdrawals.is_empty()); + assert!(data.size > 0, "RLP size of a sealed block must be non-zero"); + assert_eq!(data.slot_number, None); + assert_eq!(data.tx_dependency, None); + } + + #[test] + fn to_block_data_maps_withdrawals_and_uncles() { + let withdrawal = Withdrawal { + index: 7, + validator_index: 3, + address: address!("0x00000000000000000000000000000000000000bb"), + amount: 32_000_000_000, + }; + let uncle_header = Header { + number: 41, + beneficiary: address!("0x00000000000000000000000000000000000000cc"), + gas_limit: 8_000_000, + timestamp: 1_699_999_999, + ..Default::default() + }; + let expected_uncle_hash = uncle_header.hash_slow(); + + let header = + Header { number: 42, withdrawals_root: Some(EMPTY_WITHDRAWALS), ..Default::default() }; + let body = BlockBody { + transactions: vec![], + ommers: vec![uncle_header], + withdrawals: Some(Withdrawals::new(vec![withdrawal])), + }; + let sealed = seal_block(header, body); + let data = to_block_data(&sealed); + + assert_eq!(data.withdrawals.len(), 1); + assert_eq!(data.withdrawals[0].index, 7); + assert_eq!(data.withdrawals[0].validator_index, 3); + assert_eq!( + data.withdrawals[0].address, + address!("0x00000000000000000000000000000000000000bb") + ); + assert_eq!(data.withdrawals[0].amount, 32_000_000_000); + + assert_eq!(data.uncles.len(), 1); + assert_eq!(data.uncles[0].hash, expected_uncle_hash); + assert_eq!(data.uncles[0].number, 41); + assert_eq!(data.uncles[0].coinbase, address!("0x00000000000000000000000000000000000000cc")); + assert_eq!(data.uncles[0].gas_limit, 8_000_000); + } + + #[test] + fn signed_tx_to_tx_event_legacy() { + let to = address!("0x00000000000000000000000000000000000000dd"); + let tx = TxLegacy { + chain_id: Some(1), + nonce: 5, + gas_price: 20_000_000_000, + gas_limit: 21_000, + to: TxKind::Call(to), + value: U256::from(100u64), + input: Bytes::new(), + }; + let envelope: EthereumTxEnvelope = + EthereumTxEnvelope::new_unhashed(EthereumTypedTransaction::Legacy(tx), test_sig()); + let (r, s, v) = envelope.signature_fields(); + let signer = address!("0x00000000000000000000000000000000000000ee"); + + let event = signed_tx_to_tx_event(&envelope, signer, 3, r, s, v.clone()); + + assert_eq!(event.tx_type as u8, 0); + assert_eq!(event.from, signer); + assert_eq!(event.to, Some(to)); + assert_eq!(event.value, U256::from(100u64)); + assert_eq!(event.gas, 21_000); + assert_eq!(event.gas_price, U256::from(20_000_000_000u64)); + assert_eq!(event.nonce, 5); + assert_eq!(event.index, 3); + assert_eq!(event.v, Some(v)); + assert!(event.max_fee_per_gas.is_none()); + assert!(event.max_priority_fee_per_gas.is_none()); + assert!(event.access_list.is_empty()); + assert!(event.blob_hashes.is_empty()); + } + + #[test] + fn signed_tx_to_tx_event_eip1559() { + let to = address!("0x00000000000000000000000000000000000000ff"); + let tx = TxEip1559 { + chain_id: 1, + nonce: 1, + gas_limit: 50_000, + max_fee_per_gas: 30_000_000_000, + max_priority_fee_per_gas: 2_000_000_000, + to: TxKind::Call(to), + value: U256::ZERO, + access_list: AccessList::default(), + input: bytes!("abcdef"), + }; + let envelope: EthereumTxEnvelope = + EthereumTxEnvelope::new_unhashed(EthereumTypedTransaction::Eip1559(tx), test_sig()); + let (r, s, v) = envelope.signature_fields(); + // Typed tx V is y_parity only (0 or 1) — trimmed to a single byte or empty for 0. + assert!(v.is_empty() || v.len() == 1); + + let event = signed_tx_to_tx_event( + &envelope, + address!("0x0000000000000000000000000000000000000011"), + 0, + r, + s, + v, + ); + + assert_eq!(event.tx_type as u8, 2); + assert_eq!(event.to, Some(to)); + assert_eq!(event.input, bytes!("abcdef")); + assert_eq!(event.max_fee_per_gas, Some(U256::from(30_000_000_000u64))); + assert_eq!(event.max_priority_fee_per_gas, Some(U256::from(2_000_000_000u64))); + // gas_price falls back to 0 for dynamic-fee txs when gas_price() is None + assert_eq!(event.gas_price, U256::ZERO); + } + + #[test] + fn signed_tx_to_tx_event_eip2930_access_list() { + let storage_key = + b256!("0x0101010101010101010101010101010101010101010101010101010101010101"); + let access_addr = address!("0x0000000000000000000000000000000000000022"); + let tx = TxEip2930 { + chain_id: 1, + nonce: 0, + gas_price: 10, + gas_limit: 100_000, + to: TxKind::Create, + value: U256::ZERO, + access_list: AccessList(vec![AccessListItem { + address: access_addr, + storage_keys: vec![storage_key], + }]), + input: Bytes::from(vec![0x60, 0x00]), + }; + let envelope: EthereumTxEnvelope = + EthereumTxEnvelope::new_unhashed(EthereumTypedTransaction::Eip2930(tx), test_sig()); + let (r, s, v) = envelope.signature_fields(); + + let event = signed_tx_to_tx_event( + &envelope, + address!("0x0000000000000000000000000000000000000033"), + 1, + r, + s, + v, + ); + + assert_eq!(event.tx_type as u8, 1); + assert_eq!(event.to, None, "CREATE has no recipient"); + assert_eq!(event.access_list.len(), 1); + assert_eq!(event.access_list[0].address, access_addr); + assert_eq!(event.access_list[0].storage_keys, vec![storage_key]); + } + + #[test] + fn receipt_data_from_parts_indexes_logs() { + let log = alloy_primitives::Log { + address: address!("0x0000000000000000000000000000000000000044"), + data: alloy_primitives::LogData::new_unchecked( + vec![B256::repeat_byte(0xab)], + bytes!("cafebabe"), + ), + }; + let receipt = receipt_data_from_parts(2, 21_000, 42_000, 1, std::slice::from_ref(&log), 5); + + assert_eq!(receipt.transaction_index, 2); + assert_eq!(receipt.gas_used, 21_000); + assert_eq!(receipt.cumulative_gas_used, 42_000); + assert_eq!(receipt.status, 1); + assert_eq!(receipt.logs.len(), 1); + assert_eq!(receipt.logs[0].block_index, 5); + assert_eq!(receipt.logs[0].address, address!("0x0000000000000000000000000000000000000044")); + assert_eq!(receipt.logs[0].data, bytes!("cafebabe")); + } + + #[test] + fn to_receipt_data_copies_bloom_and_blob_fields() { + let receipt = Receipt { status: true.into(), cumulative_gas_used: 50_000, logs: vec![] }; + let data = to_receipt_data(&receipt, 0, 25_000, 0, 131_072, Some(U256::from(1u64))); + + assert_eq!(data.transaction_index, 0); + assert_eq!(data.gas_used, 25_000); + assert_eq!(data.cumulative_gas_used, 50_000); + assert_eq!(data.status, 1); + assert_eq!(data.blob_gas_used, 131_072); + assert_eq!(data.blob_gas_price, Some(U256::from(1u64))); + assert_eq!(data.logs_bloom, *receipt.bloom().data()); + } + + #[test] + fn finalized_ref_from_num_hash_maps_some_and_none() { + let hash = B256::repeat_byte(0x55); + let some = Some(alloy_eips::BlockNumHash { number: 99, hash }); + let mapped = finalized_ref_from_num_hash(some).expect("Some"); + assert_eq!(mapped.number, 99); + assert_eq!(mapped.hash, Some(hash)); + + assert!(finalized_ref_from_num_hash(None).is_none()); + } + + #[test] + fn to_genesis_alloc_maps_accounts() { + let addr = address!("0x0000000000000000000000000000000000000066"); + let slot = B256::repeat_byte(0x01); + let val = B256::repeat_byte(0x02); + let mut storage = BTreeMap::new(); + storage.insert(slot, val); + + let mut genesis = Genesis::default(); + genesis.alloc.insert( + addr, + GenesisAccount { + balance: U256::from(1_000u64), + nonce: Some(3), + code: Some(bytes!("6000")), + storage: Some(storage), + ..Default::default() + }, + ); + + let alloc = to_genesis_alloc(&genesis); + let account = alloc.get(&addr).expect("alloc entry"); + assert_eq!(account.balance, Some(U256::from(1_000u64))); + assert_eq!(account.nonce, 3); + assert_eq!(account.code, Some(bytes!("6000"))); + assert_eq!(account.storage.get(&slot), Some(&val)); + } +}