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

feat(anvil): support eth_getBlockReceipts method #6771

Merged
merged 2 commits into from
Jan 12, 2024
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
66 changes: 33 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ pub enum EthRequest {
#[cfg_attr(feature = "serde", serde(rename = "eth_getTransactionReceipt", with = "sequence"))]
EthGetTransactionReceipt(B256),

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

#[cfg_attr(feature = "serde", serde(rename = "eth_getUncleByBlockHashAndIndex"))]
EthGetUncleByBlockHashAndIndex(B256, Index),

Expand Down
14 changes: 14 additions & 0 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ impl EthApi {
EthRequest::EthGetTransactionReceipt(tx) => {
self.transaction_receipt(tx).await.to_rpc_result()
}
EthRequest::EthGetBlockReceipts(number) => {
self.block_receipts(number).await.to_rpc_result()
}
EthRequest::EthGetUncleByBlockHashAndIndex(hash, index) => {
self.uncle_by_block_hash_and_index(hash, index).await.to_rpc_result()
}
Expand Down Expand Up @@ -1159,6 +1162,17 @@ impl EthApi {
self.backend.transaction_receipt(hash).await
}

/// Returns block receipts by block number.
///
/// Handler for ETH RPC call: `eth_getBlockReceipts`
pub async fn block_receipts(
&self,
number: BlockNumber,
) -> Result<Option<Vec<TransactionReceipt>>> {
node_info!("eth_getBlockReceipts");
self.backend.block_receipts(number).await
}

/// Returns an uncles at given block and index.
///
/// Handler for ETH RPC call: `eth_getUncleByBlockHashAndIndex`
Expand Down
24 changes: 24 additions & 0 deletions crates/anvil/src/eth/backend/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,29 @@ impl ClientFork {
Ok(None)
}

pub async fn block_receipts(
&self,
number: u64,
) -> Result<Option<Vec<TransactionReceipt>>, TransportError> {
if let receipts @ Some(_) = self.storage_read().block_receipts.get(&number).cloned() {
return Ok(receipts);
}

// TODO Needs to be removed.
// Since alloy doesn't indicate in the result whether the block exists,
// this is being temporarily implemented in anvil.
if self.predates_fork_inclusive(number) {
let receipts = self.provider().get_block_receipts(BlockNumber::Number(number)).await?;

let mut storage = self.storage_write();
storage.block_receipts.insert(number, receipts.clone());

return Ok(Some(receipts));
}

Ok(None)
}

pub async fn block_by_hash(&self, hash: B256) -> Result<Option<Block>, TransportError> {
if let Some(mut block) = self.storage_read().blocks.get(&hash).cloned() {
block.transactions.convert_to_hashes();
Expand Down Expand Up @@ -647,6 +670,7 @@ pub struct ForkedStorage {
pub logs: HashMap<Filter, Vec<Log>>,
pub geth_transaction_traces: HashMap<B256, GethTrace>,
pub block_traces: HashMap<u64, Vec<Trace>>,
pub block_receipts: HashMap<u64, Vec<TransactionReceipt>>,
pub eth_gas_estimations: HashMap<(Arc<CallRequest>, u64), U256>,
pub eth_call: HashMap<(Arc<CallRequest>, u64), Bytes>,
pub code_at: HashMap<(Address, u64), Bytes>,
Expand Down
38 changes: 38 additions & 0 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,19 @@ impl Backend {
Some(receipts)
}

/// Returns all transaction receipts of the block
pub fn mined_block_receipts(&self, id: impl Into<BlockId>) -> Option<Vec<TransactionReceipt>> {
let mut receipts = Vec::new();
let block = self.get_block(id)?;

for transaction in block.transactions {
let receipt = self.mined_transaction_receipt(transaction.hash().to_alloy())?;
receipts.push(receipt.inner);
}

Some(receipts)
}

/// Returns the transaction receipt for the given hash
pub(crate) fn mined_transaction_receipt(&self, hash: B256) -> Option<MinedTransactionReceipt> {
let MinedTransaction { info, receipt, block_hash, .. } =
Expand Down Expand Up @@ -2073,6 +2086,31 @@ impl Backend {
Some(MinedTransactionReceipt { inner, out: info.out.map(|o| o.0.into()) })
}

/// Returns the blocks receipts for the given number
pub async fn block_receipts(
&self,
number: BlockNumber,
) -> Result<Option<Vec<TransactionReceipt>>, 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));

if fork.predates_fork_inclusive(number) {
let receipts = fork
.block_receipts(number)
.await
.map_err(BlockchainError::AlloyForkProvider)?;

return Ok(receipts);
}
}

Ok(None)
}

pub async fn transaction_by_block_number_and_index(
&self,
number: BlockNumber,
Expand Down
Loading
Loading