Skip to content

Commit

Permalink
eth_getBlockReceipts should accept block hashes (foundry-rs#8623)
Browse files Browse the repository at this point in the history
* eth_getBlockReceipts using BlockID

* fix block receipt

* Update crates/anvil/src/eth/backend/mem/mod.rs

Co-authored-by: zerosnacks <[email protected]>

* Update crates/anvil/src/eth/api.rs

Co-authored-by: zerosnacks <[email protected]>

* code review

---------

Co-authored-by: joaocosta9 <[email protected]>
Co-authored-by: zerosnacks <[email protected]>
  • Loading branch information
3 people authored Aug 8, 2024
1 parent 960878a commit 4cdebf7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub enum EthRequest {
EthGetTransactionReceipt(B256),

#[cfg_attr(feature = "serde", serde(rename = "eth_getBlockReceipts", with = "sequence"))]
EthGetBlockReceipts(BlockNumber),
EthGetBlockReceipts(BlockId),

#[cfg_attr(feature = "serde", serde(rename = "eth_getUncleByBlockHashAndIndex"))]
EthGetUncleByBlockHashAndIndex(B256, Index),
Expand Down
5 changes: 1 addition & 4 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,10 +1217,7 @@ impl EthApi {
/// Returns block receipts by block number.
///
/// Handler for ETH RPC call: `eth_getBlockReceipts`
pub async fn block_receipts(
&self,
number: BlockNumber,
) -> Result<Option<Vec<ReceiptResponse>>> {
pub async fn block_receipts(&self, number: BlockId) -> Result<Option<Vec<ReceiptResponse>>> {
node_info!("eth_getBlockReceipts");
self.backend.block_receipts(number).await
}
Expand Down
7 changes: 5 additions & 2 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2236,14 +2236,17 @@ impl Backend {
/// Returns the blocks receipts for the given number
pub async fn block_receipts(
&self,
number: BlockNumber,
number: BlockId,
) -> Result<Option<Vec<ReceiptResponse>>, BlockchainError> {
if let Some(receipts) = self.mined_block_receipts(number) {
return Ok(Some(receipts));
}

if let Some(fork) = self.get_fork() {
let number = self.convert_block_number(Some(number));
let number = match self.ensure_block_number(Some(number)).await {
Err(_) => return Ok(None),
Ok(n) => n,
};

if fork.predates_fork_inclusive(number) {
let receipts = fork.block_receipts(number).await?;
Expand Down
12 changes: 9 additions & 3 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
utils::{http_provider, http_provider_with_signer},
};
use alloy_network::{EthereumWallet, ReceiptResponse, TransactionBuilder};
use alloy_primitives::{address, bytes, Address, Bytes, TxHash, TxKind, U256};
use alloy_primitives::{address, b256, bytes, Address, Bytes, TxHash, TxKind, U256};
use alloy_provider::Provider;
use alloy_rpc_types::{
anvil::Forking,
Expand Down Expand Up @@ -1015,12 +1015,18 @@ async fn test_block_receipts() {
let (api, _) = spawn(fork_config()).await;

// Receipts from the forked block (14608400)
let receipts = api.block_receipts(BlockNumberOrTag::Number(BLOCK_NUMBER)).await.unwrap();
let receipts = api.block_receipts(BlockNumberOrTag::Number(BLOCK_NUMBER).into()).await.unwrap();
assert!(receipts.is_some());

// Receipts from a block in the future (14608401)
let receipts = api.block_receipts(BlockNumberOrTag::Number(BLOCK_NUMBER + 1)).await.unwrap();
let receipts =
api.block_receipts(BlockNumberOrTag::Number(BLOCK_NUMBER + 1).into()).await.unwrap();
assert!(receipts.is_none());

// Receipts from a block hash (14608400)
let hash = b256!("4c1c76f89cfe4eb503b09a0993346dd82865cac9d76034efc37d878c66453f0a");
let receipts = api.block_receipts(BlockId::Hash(hash.into())).await.unwrap();
assert!(receipts.is_some());
}

#[tokio::test(flavor = "multi_thread")]
Expand Down

0 comments on commit 4cdebf7

Please sign in to comment.