From 4cdebf77b2757d50768f188e886a55feaf4316fd Mon Sep 17 00:00:00 2001 From: Miguel Palhas Date: Thu, 8 Aug 2024 12:44:00 +0100 Subject: [PATCH] `eth_getBlockReceipts` should accept block hashes (#8623) * eth_getBlockReceipts using BlockID * fix block receipt * Update crates/anvil/src/eth/backend/mem/mod.rs Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com> * Update crates/anvil/src/eth/api.rs Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com> * code review --------- Co-authored-by: joaocosta9 Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com> --- crates/anvil/core/src/eth/mod.rs | 2 +- crates/anvil/src/eth/api.rs | 5 +---- crates/anvil/src/eth/backend/mem/mod.rs | 7 +++++-- crates/anvil/tests/it/fork.rs | 12 +++++++++--- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/anvil/core/src/eth/mod.rs b/crates/anvil/core/src/eth/mod.rs index 185e723f384c..7c86baad32ae 100644 --- a/crates/anvil/core/src/eth/mod.rs +++ b/crates/anvil/core/src/eth/mod.rs @@ -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), diff --git a/crates/anvil/src/eth/api.rs b/crates/anvil/src/eth/api.rs index 4f853cd0c761..822bc57f13f8 100644 --- a/crates/anvil/src/eth/api.rs +++ b/crates/anvil/src/eth/api.rs @@ -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>> { + pub async fn block_receipts(&self, number: BlockId) -> Result>> { node_info!("eth_getBlockReceipts"); self.backend.block_receipts(number).await } diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index c3285e25f97c..da0a22883217 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -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>, 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?; diff --git a/crates/anvil/tests/it/fork.rs b/crates/anvil/tests/it/fork.rs index 8a4542b488d8..884bc0605466 100644 --- a/crates/anvil/tests/it/fork.rs +++ b/crates/anvil/tests/it/fork.rs @@ -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, @@ -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")]