From 3dcd01e31e1895f6cc8ad10df47ab9df807ada99 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:40:59 +0200 Subject: [PATCH] simplify eip1898 usage (#1494) --- src/providers/eth_provider/database/state.rs | 6 ++--- src/providers/eth_provider/provider.rs | 2 +- src/providers/eth_provider/receipts.rs | 2 +- src/tracing/builder.rs | 8 +++---- tests/tests/debug_api.rs | 24 +++++--------------- tests/tests/eth_provider.rs | 19 ++++------------ 6 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/providers/eth_provider/database/state.rs b/src/providers/eth_provider/database/state.rs index 8c59b3334..b9a5517fe 100644 --- a/src/providers/eth_provider/database/state.rs +++ b/src/providers/eth_provider/database/state.rs @@ -1,6 +1,6 @@ use crate::providers::eth_provider::{error::EthApiError, provider::EthereumProvider}; use alloy_primitives::{Address, B256, U256}; -use alloy_rpc_types::{serde_helpers::JsonStorageKey, BlockHashOrNumber, BlockId, BlockNumberOrTag}; +use alloy_rpc_types::{serde_helpers::JsonStorageKey, BlockId}; use reth_revm::{ db::CacheDB, primitives::{AccountInfo, Bytecode}, @@ -98,9 +98,9 @@ impl DatabaseRef for EthDatabase

{ let hash = Handle::current().block_on(async { let hash = self .provider - .block_by_number(BlockNumberOrTag::Number(block_number), false) + .block_by_number(block_number.into(), false) .await? - .ok_or(EthApiError::UnknownBlock(BlockHashOrNumber::Number(block_number)))? + .ok_or(EthApiError::UnknownBlock(block_number.into()))? .header .hash; Result::<_, EthApiError>::Ok(hash) diff --git a/src/providers/eth_provider/provider.rs b/src/providers/eth_provider/provider.rs index adcc982c2..bfa88726c 100644 --- a/src/providers/eth_provider/provider.rs +++ b/src/providers/eth_provider/provider.rs @@ -280,7 +280,7 @@ where block_id: BlockId, ) -> EthApiResult { match block_id { - BlockId::Hash(hash) => Ok(BlockHashOrNumber::Hash(hash.into())), + BlockId::Hash(hash) => Ok(hash.into()), BlockId::Number(number_or_tag) => Ok(self.tag_into_block_number(number_or_tag).await?.into()), } } diff --git a/src/providers/eth_provider/receipts.rs b/src/providers/eth_provider/receipts.rs index 4a867bec7..854f284a9 100644 --- a/src/providers/eth_provider/receipts.rs +++ b/src/providers/eth_provider/receipts.rs @@ -34,7 +34,7 @@ where } async fn block_receipts(&self, block_id: Option) -> EthApiResult>> { - match block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)) { + match block_id.unwrap_or_else(|| BlockNumberOrTag::Latest.into()) { BlockId::Number(number_or_tag) => { let block_number = self.tag_into_block_number(number_or_tag).await?; if !self.database().block_exists(block_number.into()).await? { diff --git a/src/tracing/builder.rs b/src/tracing/builder.rs index 301449a6c..dc419f2b4 100644 --- a/src/tracing/builder.rs +++ b/src/tracing/builder.rs @@ -8,7 +8,7 @@ use crate::providers::eth_provider::{ provider::EthereumProvider, }; use alloy_primitives::{B256, U256}; -use alloy_rpc_types::{Block, BlockHashOrNumber, BlockId, BlockTransactions, Header}; +use alloy_rpc_types::{Block, BlockId, BlockTransactions, Header}; use alloy_rpc_types_trace::geth::{GethDebugTracingCallOptions, GethDebugTracingOptions}; use reth_revm::{ db::CacheDB, @@ -159,14 +159,12 @@ impl TracerBuilder { } .ok_or(match block_id { BlockId::Hash(hash) => EthApiError::UnknownBlock(hash.block_hash.into()), - BlockId::Number(number) => { - EthApiError::UnknownBlock(BlockHashOrNumber::Number(number.as_number().unwrap_or_default())) - } + BlockId::Number(number) => EthApiError::UnknownBlock(number.as_number().unwrap_or_default().into()), })?; // we can't trace a pending block if block.header.hash.is_zero() { - return Err(EthApiError::UnknownBlock(BlockHashOrNumber::Hash(B256::ZERO))); + return Err(EthApiError::UnknownBlock(B256::ZERO.into())); } Ok(block.inner) diff --git a/tests/tests/debug_api.rs b/tests/tests/debug_api.rs index 29dd67a50..c45f3282f 100644 --- a/tests/tests/debug_api.rs +++ b/tests/tests/debug_api.rs @@ -14,9 +14,7 @@ use kakarot_rpc::{ rpc::{start_kakarot_rpc_server, RawRpcParamsBuilder}, }, }; -use reth_primitives::{ - Block, BlockNumberOrTag, Log, Receipt, ReceiptWithBloom, TransactionSigned, TransactionSignedEcRecovered, -}; +use reth_primitives::{Block, Log, Receipt, ReceiptWithBloom, TransactionSigned, TransactionSignedEcRecovered}; use reth_rpc_types_compat::transaction::from_recovered_with_block_context; use rstest::*; use serde_json::Value; @@ -149,13 +147,8 @@ async fn test_raw_transactions(#[future] katana: Katana, _setup: ()) { // Get the Ethereum provider from the Katana instance. let eth_provider = katana.eth_provider(); - for (i, actual_tx) in eth_provider - .block_transactions(Some(alloy_rpc_types::BlockId::Number(BlockNumberOrTag::Number(block_number)))) - .await - .unwrap() - .unwrap() - .iter() - .enumerate() + for (i, actual_tx) in + eth_provider.block_transactions(Some(block_number.into())).await.unwrap().unwrap().iter().enumerate() { // Fetch the transaction for the current transaction hash. let tx = katana.eth_client.transaction_by_hash(actual_tx.hash).await.unwrap().unwrap(); @@ -240,13 +233,8 @@ async fn test_raw_receipts(#[future] katana: Katana, _setup: ()) { // Get eth provider let eth_provider = katana.eth_provider(); - for (i, receipt) in eth_provider - .block_receipts(Some(alloy_rpc_types::BlockId::Number(BlockNumberOrTag::Number(block_number)))) - .await - .unwrap() - .unwrap() - .iter() - .enumerate() + for (i, receipt) in + eth_provider.block_receipts(Some(block_number.into())).await.unwrap().unwrap().iter().enumerate() { // Fetch the transaction receipt for the current receipt hash. let tx_receipt = eth_provider.transaction_receipt(receipt.transaction_hash).await.unwrap().unwrap(); @@ -389,7 +377,7 @@ async fn test_raw_header(#[future] katana: Katana, _setup: ()) { let eth_provider = katana.eth_provider(); // Fetch the transaction receipt for the current receipt hash. - let block = eth_provider.block_by_number(BlockNumberOrTag::Number(block_number), true).await.unwrap().unwrap(); + let block = eth_provider.block_by_number(block_number.into(), true).await.unwrap().unwrap(); // Encode header into RLP bytes and assert equality with RLP bytes fetched by block number. let mut data = vec![]; diff --git a/tests/tests/eth_provider.rs b/tests/tests/eth_provider.rs index 516ea82b3..ec4934850 100644 --- a/tests/tests/eth_provider.rs +++ b/tests/tests/eth_provider.rs @@ -98,7 +98,7 @@ async fn test_block_by_number(#[future] katana: Katana, _setup: ()) { let block_number = katana.block_number(); // When: Retrieving block by specific block number - let block = eth_provider.block_by_number(BlockNumberOrTag::Number(block_number), false).await.unwrap().unwrap(); + let block = eth_provider.block_by_number(block_number.into(), false).await.unwrap().unwrap(); // Then: Ensure the retrieved block has the expected block number assert_eq!(block.header.number, block_number); @@ -168,16 +168,14 @@ async fn test_block_transaction_count_by_number(#[future] katana: Katana, _setup let header = katana.header_by_hash(first_tx.block_hash.unwrap()).unwrap(); // When: Retrieving transaction count for a specific block number - let count = - eth_provider.block_transaction_count_by_number(BlockNumberOrTag::Number(header.number)).await.unwrap().unwrap(); + let count = eth_provider.block_transaction_count_by_number(header.number.into()).await.unwrap().unwrap(); // Then: Ensure the retrieved transaction count matches the expected value assert_eq!(count, U256::from(1)); // When: Retrieving transaction count for the block of the most recent transaction let block_number = katana.most_recent_transaction().unwrap().block_number.unwrap(); - let count = - eth_provider.block_transaction_count_by_number(BlockNumberOrTag::Number(block_number)).await.unwrap().unwrap(); + let count = eth_provider.block_transaction_count_by_number(block_number.into()).await.unwrap().unwrap(); // Then: Ensure the retrieved transaction count matches the expected value assert_eq!(count, U256::from(1)); @@ -558,8 +556,7 @@ async fn test_fee_history(#[future] katana: Katana, _setup: ()) { let nbr_blocks = katana.headers.len(); // Call the fee_history method of the Ethereum provider. - let fee_history = - eth_provider.fee_history(U64::from(block_count), BlockNumberOrTag::Number(newest_block), None).await.unwrap(); + let fee_history = eth_provider.fee_history(U64::from(block_count), newest_block.into(), None).await.unwrap(); // Verify that the length of the base_fee_per_gas list in the fee history is equal // to the total number of blocks plus one. @@ -640,13 +637,7 @@ async fn test_block_receipts(#[future] katana: Katana, _setup: ()) { let transaction = katana.most_recent_transaction().unwrap(); // Then: Retrieve receipts by block number - let receipts = eth_provider - .block_receipts(Some(alloy_rpc_types::BlockId::Number(BlockNumberOrTag::Number( - transaction.block_number.unwrap(), - )))) - .await - .unwrap() - .unwrap(); + let receipts = eth_provider.block_receipts(Some(transaction.block_number.unwrap().into())).await.unwrap().unwrap(); assert_eq!(receipts.len(), 1); let receipt = receipts.first().unwrap(); assert_eq!(receipt.transaction_index, transaction.transaction_index);