diff --git a/crates/anvil/core/src/eth/transaction/mod.rs b/crates/anvil/core/src/eth/transaction/mod.rs index 72a88c54712c..10de5f80e7f0 100644 --- a/crates/anvil/core/src/eth/transaction/mod.rs +++ b/crates/anvil/core/src/eth/transaction/mod.rs @@ -1011,6 +1011,16 @@ impl TypedReceipt { TypedReceipt::Deposit(r) => &r.bloom, } } + + pub fn logs(&self) -> &Vec { + match self { + TypedReceipt::Legacy(r) | + TypedReceipt::EIP1559(r) | + TypedReceipt::EIP2930(r) | + TypedReceipt::EIP4844(r) | + TypedReceipt::Deposit(r) => &r.receipt.logs, + } + } } impl From for ReceiptWithBloom { diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index 013c303e3285..a60ba56408fa 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -2009,8 +2009,9 @@ impl Backend { let mut pre_receipts_log_index = None; if !cumulative_receipts.is_empty() { cumulative_receipts.truncate(cumulative_receipts.len() - 1); - pre_receipts_log_index = - Some(cumulative_receipts.iter().map(|_r| logs.len() as u32).sum::()); + pre_receipts_log_index = Some( + cumulative_receipts.iter().map(|r| r.logs().len() as u32).sum::(), + ); } logs.iter() .enumerate() diff --git a/crates/anvil/tests/it/logs.rs b/crates/anvil/tests/it/logs.rs index 5fc9cf9f22f1..2f9290f3e754 100644 --- a/crates/anvil/tests/it/logs.rs +++ b/crates/anvil/tests/it/logs.rs @@ -86,6 +86,32 @@ async fn get_all_events() { let num_logs = num_tx + pre_logs.len(); assert_eq!(logs.len(), num_logs); + + // test that logs returned from get_logs and get_transaction_receipt have + // the same log_index, block_number, and transaction_hash + let mut tasks = vec![]; + let mut seen_tx_hashes = std::collections::HashSet::new(); + for log in &logs { + if seen_tx_hashes.contains(&log.transaction_hash.unwrap()) { + continue; + } + tasks.push(client.get_transaction_receipt(log.transaction_hash.unwrap())); + seen_tx_hashes.insert(log.transaction_hash.unwrap()); + } + let receipt_logs = futures::future::join_all(tasks) + .await + .into_iter() + .collect::, _>>() + .unwrap() + .into_iter() + .flat_map(|receipt| receipt.unwrap().logs) + .collect::>(); + assert_eq!(receipt_logs.len(), logs.len()); + for (receipt_log, log) in receipt_logs.iter().zip(logs.iter()) { + assert_eq!(receipt_log.transaction_hash, log.transaction_hash); + assert_eq!(receipt_log.block_number, log.block_number); + assert_eq!(receipt_log.log_index, log.log_index); + } } #[tokio::test(flavor = "multi_thread")]