Skip to content

Commit

Permalink
simplify eip1898 usage (#1494)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Oct 25, 2024
1 parent d3d42d4 commit 3dcd01e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/providers/eth_provider/database/state.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -98,9 +98,9 @@ impl<P: EthereumProvider + Send + Sync> DatabaseRef for EthDatabase<P> {
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)
Expand Down
2 changes: 1 addition & 1 deletion src/providers/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ where
block_id: BlockId,
) -> EthApiResult<BlockHashOrNumber> {
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()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/eth_provider/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
}

async fn block_receipts(&self, block_id: Option<BlockId>) -> EthApiResult<Option<Vec<ExtendedTxReceipt>>> {
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? {
Expand Down
8 changes: 3 additions & 5 deletions src/tracing/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -159,14 +159,12 @@ impl<P: EthereumProvider + Send + Sync + Clone> TracerBuilder<P, Floating> {
}
.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)
Expand Down
24 changes: 6 additions & 18 deletions tests/tests/debug_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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![];
Expand Down
19 changes: 5 additions & 14 deletions tests/tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3dcd01e

Please sign in to comment.