Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[anvil] correct log index for getTransactionReceipt #7483

Merged
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
10 changes: 10 additions & 0 deletions crates/anvil/core/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,16 @@ impl TypedReceipt {
TypedReceipt::Deposit(r) => &r.bloom,
}
}

pub fn logs(&self) -> &Vec<Log> {
match self {
TypedReceipt::Legacy(r) |
TypedReceipt::EIP1559(r) |
TypedReceipt::EIP2930(r) |
TypedReceipt::EIP4844(r) |
TypedReceipt::Deposit(r) => &r.receipt.logs,
}
}
}

impl From<TypedReceipt> for ReceiptWithBloom {
Expand Down
5 changes: 3 additions & 2 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>());
pre_receipts_log_index = Some(
cumulative_receipts.iter().map(|r| r.logs().len() as u32).sum::<u32>(),
);
}
logs.iter()
.enumerate()
Expand Down
26 changes: 26 additions & 0 deletions crates/anvil/tests/it/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<Vec<_>, _>>()
.unwrap()
.into_iter()
.flat_map(|receipt| receipt.unwrap().logs)
.collect::<Vec<_>>();
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")]
Expand Down
Loading