From 0bffa25e6504fe8f7d37bc1ed432e9de3c14334b Mon Sep 17 00:00:00 2001 From: Gerson <71728860+Gerson2102@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:41:26 -0600 Subject: [PATCH] dev: simplify WithOtherFields usage (#1486) * Simplifying the usage of types * Refactoring the code by simplifying usages of withotherfield type * Addressing PR comments on improving docs for new type aliases * Adding type alias for TxReceipt --- src/client/mod.rs | 14 +++--- src/eth_rpc/api/eth_api.rs | 10 ++--- src/eth_rpc/api/txpool_api.rs | 7 ++- src/eth_rpc/servers/eth_rpc.rs | 45 +++++++------------ src/eth_rpc/servers/txpool_rpc.rs | 11 ++--- src/providers/eth_provider/blocks.rs | 43 +++++++----------- .../eth_provider/database/ethereum.rs | 27 ++++++----- .../eth_provider/database/types/header.rs | 7 ++- .../eth_provider/database/types/receipt.rs | 3 ++ .../database/types/transaction.rs | 2 + src/providers/eth_provider/receipts.rs | 17 +++---- src/providers/eth_provider/transactions.rs | 20 +++++---- src/providers/eth_provider/tx_pool.rs | 7 ++- src/providers/pool_provider.rs | 16 +++---- src/test_utils/katana/mod.rs | 18 ++++---- src/test_utils/mock_provider.rs | 26 +++++------ src/test_utils/mongo/mod.rs | 10 +++-- src/tracing/builder.rs | 12 ++--- tests/tests/debug_api.rs | 6 +-- tests/tests/txpool_api.rs | 15 ++++--- 20 files changed, 144 insertions(+), 172 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 260927d69..635beae4a 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -6,7 +6,7 @@ use crate::{ }, providers::{ eth_provider::{ - database::Database, + database::{types::transaction::ExtendedTransaction, Database}, error::SignatureError, provider::{EthApiResult, EthDataProvider}, TransactionProvider, TxPoolProvider, @@ -17,9 +17,7 @@ use crate::{ use alloy_eips::eip2718::Encodable2718; use alloy_primitives::{Address, Bytes, B256}; use alloy_rlp::Decodable; -use alloy_rpc_types::Transaction; use alloy_rpc_types_txpool::TxpoolContent; -use alloy_serde::WithOtherFields; use async_trait::async_trait; use reth_chainspec::ChainSpec; use reth_primitives::{TransactionSigned, TransactionSignedEcRecovered}; @@ -40,7 +38,7 @@ pub trait KakarotTransactions { #[async_trait] pub trait TransactionHashProvider { /// Returns the transaction by hash. - async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>>; + async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>; } /// Provides a wrapper structure around the Ethereum Provider @@ -133,11 +131,11 @@ impl TxPoolProvider for EthClient where SP: starknet::providers::Provider + Send + Sync, { - fn content(&self) -> TxpoolContent> { + fn content(&self) -> TxpoolContent { #[inline] fn insert>( tx: &T, - content: &mut BTreeMap>>, + content: &mut BTreeMap>, ) { content.entry(tx.sender()).or_default().insert( tx.nonce().to_string(), @@ -160,7 +158,7 @@ where content } - async fn txpool_content(&self) -> EthApiResult>> { + async fn txpool_content(&self) -> EthApiResult> { Ok(self.content()) } } @@ -170,7 +168,7 @@ impl TransactionHashProvider for EthClient where SP: starknet::providers::Provider + Send + Sync, { - async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>> { + async fn transaction_by_hash(&self, hash: B256) -> EthApiResult> { Ok(self .pool .get(&hash) diff --git a/src/eth_rpc/api/eth_api.rs b/src/eth_rpc/api/eth_api.rs index 32b2832b9..737f8fff1 100644 --- a/src/eth_rpc/api/eth_api.rs +++ b/src/eth_rpc/api/eth_api.rs @@ -1,8 +1,9 @@ +use crate::providers::eth_provider::database::types::receipt::ExtendedTxReceipt; use alloy_primitives::{Address, Bytes, B256, B64, U256, U64}; use alloy_rpc_types::{ serde_helpers::JsonStorageKey, state::StateOverride, AccessListResult, Block, BlockOverrides, EIP1186AccountProofResponse, FeeHistory, Filter, FilterChanges, Index, SyncStatus, Transaction as EthTransaction, - TransactionReceipt, TransactionRequest, Work, + TransactionRequest, Work, }; use alloy_serde::WithOtherFields; use jsonrpsee::{core::RpcResult, proc_macros::rpc}; @@ -103,7 +104,7 @@ pub trait EthApi { /// Returns the receipt of a transaction by transaction hash. #[method(name = "getTransactionReceipt")] - async fn transaction_receipt(&self, hash: B256) -> RpcResult>>; + async fn transaction_receipt(&self, hash: B256) -> RpcResult>; /// Returns the balance of the account of given address. #[method(name = "getBalance")] @@ -268,8 +269,5 @@ pub trait EthApi { /// Returns all transaction receipts for a given block. #[method(name = "getBlockReceipts")] - async fn block_receipts( - &self, - block_id: Option, - ) -> RpcResult>>>; + async fn block_receipts(&self, block_id: Option) -> RpcResult>>; } diff --git a/src/eth_rpc/api/txpool_api.rs b/src/eth_rpc/api/txpool_api.rs index 1d69a0dcd..397b02789 100644 --- a/src/eth_rpc/api/txpool_api.rs +++ b/src/eth_rpc/api/txpool_api.rs @@ -1,7 +1,6 @@ +use crate::providers::eth_provider::database::types::transaction::ExtendedTransaction; use alloy_primitives::Address; -use alloy_rpc_types::Transaction; use alloy_rpc_types_txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, TxpoolStatus}; -use alloy_serde::WithOtherFields; use jsonrpsee::{core::RpcResult, proc_macros::rpc}; /// Txpool API @@ -27,12 +26,12 @@ pub trait TxPoolApi { /// /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_contentFrom) for more details #[method(name = "contentFrom")] - async fn txpool_content_from(&self, from: Address) -> RpcResult>>; + async fn txpool_content_from(&self, from: Address) -> RpcResult>; /// Returns the details of all transactions currently pending for inclusion in the next /// block(s), grouped by nonce. /// /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_content) for more details #[method(name = "content")] - async fn txpool_content(&self) -> RpcResult>>; + async fn txpool_content(&self) -> RpcResult>; } diff --git a/src/eth_rpc/servers/eth_rpc.rs b/src/eth_rpc/servers/eth_rpc.rs index 452ca7e87..500fb7a59 100644 --- a/src/eth_rpc/servers/eth_rpc.rs +++ b/src/eth_rpc/servers/eth_rpc.rs @@ -2,17 +2,17 @@ use crate::{ client::{EthClient, KakarotTransactions, TransactionHashProvider}, eth_rpc::api::eth_api::EthApiServer, providers::eth_provider::{ - constant::MAX_PRIORITY_FEE_PER_GAS, error::EthApiError, BlockProvider, ChainProvider, GasProvider, LogProvider, - ReceiptProvider, StateProvider, TransactionProvider, + constant::MAX_PRIORITY_FEE_PER_GAS, + database::types::{header::ExtendedBlock, receipt::ExtendedTxReceipt, transaction::ExtendedTransaction}, + error::EthApiError, + BlockProvider, ChainProvider, GasProvider, LogProvider, ReceiptProvider, StateProvider, TransactionProvider, }, }; use alloy_primitives::{Address, Bytes, B256, B64, U256, U64}; use alloy_rpc_types::{ - serde_helpers::JsonStorageKey, state::StateOverride, AccessListResult, Block, BlockOverrides, - EIP1186AccountProofResponse, FeeHistory, Filter, FilterChanges, Index, SyncStatus, Transaction, TransactionReceipt, - TransactionRequest, Work, + serde_helpers::JsonStorageKey, state::StateOverride, AccessListResult, BlockOverrides, EIP1186AccountProofResponse, + FeeHistory, Filter, FilterChanges, Index, SyncStatus, TransactionRequest, Work, }; -use alloy_serde::WithOtherFields; use jsonrpsee::core::{async_trait, RpcResult}; use reth_primitives::{BlockId, BlockNumberOrTag}; use serde_json::Value; @@ -67,20 +67,12 @@ where } #[tracing::instrument(skip(self), ret, err)] - async fn block_by_hash( - &self, - hash: B256, - full: bool, - ) -> RpcResult>>>> { + async fn block_by_hash(&self, hash: B256, full: bool) -> RpcResult> { Ok(self.eth_client.eth_provider().block_by_hash(hash, full).await?) } #[tracing::instrument(skip(self), err)] - async fn block_by_number( - &self, - number: BlockNumberOrTag, - full: bool, - ) -> RpcResult>>>> { + async fn block_by_number(&self, number: BlockNumberOrTag, full: bool) -> RpcResult> { Ok(self.eth_client.eth_provider().block_by_number(number, full).await?) } @@ -104,11 +96,7 @@ where Ok(U256::ZERO) } - async fn uncle_by_block_hash_and_index( - &self, - _hash: B256, - _index: Index, - ) -> RpcResult>>>> { + async fn uncle_by_block_hash_and_index(&self, _hash: B256, _index: Index) -> RpcResult> { tracing::warn!("Kakarot chain does not produce uncles"); Ok(None) } @@ -117,13 +105,13 @@ where &self, _number: BlockNumberOrTag, _index: Index, - ) -> RpcResult>>>> { + ) -> RpcResult> { tracing::warn!("Kakarot chain does not produce uncles"); Ok(None) } #[tracing::instrument(skip(self), ret, err)] - async fn transaction_by_hash(&self, hash: B256) -> RpcResult>> { + async fn transaction_by_hash(&self, hash: B256) -> RpcResult> { Ok(self.eth_client.transaction_by_hash(hash).await?) } @@ -132,7 +120,7 @@ where &self, hash: B256, index: Index, - ) -> RpcResult>> { + ) -> RpcResult> { Ok(self.eth_client.eth_provider().transaction_by_block_hash_and_index(hash, index).await?) } @@ -141,12 +129,12 @@ where &self, number: BlockNumberOrTag, index: Index, - ) -> RpcResult>> { + ) -> RpcResult> { Ok(self.eth_client.eth_provider().transaction_by_block_number_and_index(number, index).await?) } #[tracing::instrument(skip(self), ret, err)] - async fn transaction_receipt(&self, hash: B256) -> RpcResult>> { + async fn transaction_receipt(&self, hash: B256) -> RpcResult> { Ok(self.eth_client.eth_provider().transaction_receipt(hash).await?) } @@ -303,10 +291,7 @@ where Err(EthApiError::Unsupported("eth_getFilterLogs").into()) } - async fn block_receipts( - &self, - block_id: Option, - ) -> RpcResult>>> { + async fn block_receipts(&self, block_id: Option) -> RpcResult>> { Ok(self.eth_client.eth_provider().block_receipts(block_id).await?) } } diff --git a/src/eth_rpc/servers/txpool_rpc.rs b/src/eth_rpc/servers/txpool_rpc.rs index b67d7a7e5..c4e61f62e 100644 --- a/src/eth_rpc/servers/txpool_rpc.rs +++ b/src/eth_rpc/servers/txpool_rpc.rs @@ -1,8 +1,9 @@ -use crate::{eth_rpc::api::txpool_api::TxPoolApiServer, providers::pool_provider::PoolProvider}; +use crate::{ + eth_rpc::api::txpool_api::TxPoolApiServer, + providers::{eth_provider::database::types::transaction::ExtendedTransaction, pool_provider::PoolProvider}, +}; use alloy_primitives::Address; -use alloy_rpc_types::Transaction; use alloy_rpc_types_txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, TxpoolStatus}; -use alloy_serde::WithOtherFields; use jsonrpsee::core::{async_trait, RpcResult}; use tracing::instrument; @@ -53,7 +54,7 @@ where /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_contentFrom) for more details /// Handler for `txpool_contentFrom` #[instrument(skip(self))] - async fn txpool_content_from(&self, from: Address) -> RpcResult>> { + async fn txpool_content_from(&self, from: Address) -> RpcResult> { self.pool_provider.txpool_content_from(from).await.map_err(Into::into) } @@ -63,7 +64,7 @@ where /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_content) for more details /// Handler for `txpool_content` #[instrument(skip(self))] - async fn txpool_content(&self) -> RpcResult>> { + async fn txpool_content(&self) -> RpcResult> { self.pool_provider.txpool_content().await.map_err(Into::into) } } diff --git a/src/providers/eth_provider/blocks.rs b/src/providers/eth_provider/blocks.rs index 9b7493daf..d96aaf9a9 100644 --- a/src/providers/eth_provider/blocks.rs +++ b/src/providers/eth_provider/blocks.rs @@ -1,17 +1,21 @@ -use super::{database::ethereum::EthereumBlockStore, error::KakarotError}; +use super::{ + database::{ + ethereum::EthereumBlockStore, + types::{header::ExtendedBlock, transaction::ExtendedTransaction}, + }, + error::KakarotError, +}; use crate::providers::eth_provider::{ database::ethereum::EthereumTransactionStore, provider::{EthApiResult, EthDataProvider}, }; use alloy_primitives::{B256, U256, U64}; -use alloy_rpc_types::{Block, Header, Transaction}; -use alloy_serde::WithOtherFields; +use alloy_rpc_types::Header; use async_trait::async_trait; use auto_impl::auto_impl; use mongodb::bson::doc; use reth_primitives::{BlockId, BlockNumberOrTag}; use tracing::Instrument; - /// Ethereum block provider trait. #[async_trait] #[auto_impl(Arc, &)] @@ -23,18 +27,11 @@ pub trait BlockProvider { async fn block_number(&self) -> EthApiResult; /// Returns a block by hash. Block can be full or just the hashes of the transactions. - async fn block_by_hash( - &self, - hash: B256, - full: bool, - ) -> EthApiResult>>>>; + async fn block_by_hash(&self, hash: B256, full: bool) -> EthApiResult>; /// Returns a block by number. Block can be full or just the hashes of the transactions. - async fn block_by_number( - &self, - number_or_tag: BlockNumberOrTag, - full: bool, - ) -> EthApiResult>>>>; + async fn block_by_number(&self, number_or_tag: BlockNumberOrTag, full: bool) + -> EthApiResult>; /// Returns the transaction count for a block by hash. async fn block_transaction_count_by_hash(&self, hash: B256) -> EthApiResult>; @@ -43,10 +40,7 @@ pub trait BlockProvider { async fn block_transaction_count_by_number(&self, number_or_tag: BlockNumberOrTag) -> EthApiResult>; /// Returns the transactions for a block. - async fn block_transactions( - &self, - block_id: Option, - ) -> EthApiResult>>>; + async fn block_transactions(&self, block_id: Option) -> EthApiResult>>; } #[async_trait] @@ -76,11 +70,7 @@ where Ok(block_number) } - async fn block_by_hash( - &self, - hash: B256, - full: bool, - ) -> EthApiResult>>>> { + async fn block_by_hash(&self, hash: B256, full: bool) -> EthApiResult> { Ok(self.database().block(hash.into(), full).await?) } @@ -88,7 +78,7 @@ where &self, number_or_tag: BlockNumberOrTag, full: bool, - ) -> EthApiResult>>>> { + ) -> EthApiResult> { let block_number = self.tag_into_block_number(number_or_tag).await?; Ok(self.database().block(block_number.into(), full).await?) } @@ -102,10 +92,7 @@ where self.database().transaction_count(block_number.into()).await } - async fn block_transactions( - &self, - block_id: Option, - ) -> EthApiResult>>> { + async fn block_transactions(&self, block_id: Option) -> EthApiResult>> { let block_hash_or_number = self .block_id_into_block_number_or_hash(block_id.unwrap_or_else(|| BlockNumberOrTag::Latest.into())) .await?; diff --git a/src/providers/eth_provider/database/ethereum.rs b/src/providers/eth_provider/database/ethereum.rs index c6515e842..6d7620999 100644 --- a/src/providers/eth_provider/database/ethereum.rs +++ b/src/providers/eth_provider/database/ethereum.rs @@ -1,13 +1,16 @@ use super::{ filter, filter::EthDatabaseFilterBuilder, - types::{header::StoredHeader, transaction::StoredTransaction}, + types::{ + header::{ExtendedBlock, StoredHeader}, + transaction::{ExtendedTransaction, StoredTransaction}, + }, Database, }; use crate::providers::eth_provider::error::EthApiError; use alloy_primitives::{B256, U256}; use alloy_rlp::Encodable; -use alloy_rpc_types::{Block, BlockHashOrNumber, BlockTransactions, Header, Transaction}; +use alloy_rpc_types::{Block, BlockHashOrNumber, BlockTransactions, Header}; use alloy_serde::WithOtherFields; use async_trait::async_trait; use mongodb::bson::doc; @@ -20,20 +23,20 @@ use tracing::instrument; pub trait EthereumTransactionStore { /// Returns the transaction with the given hash. Returns None if the /// transaction is not found. - async fn transaction(&self, hash: &B256) -> Result>, EthApiError>; + async fn transaction(&self, hash: &B256) -> Result, EthApiError>; /// Returns all transactions for the given block hash or number. async fn transactions( &self, block_hash_or_number: BlockHashOrNumber, - ) -> Result>, EthApiError>; + ) -> Result, EthApiError>; /// Upserts the given transaction. - async fn upsert_transaction(&self, transaction: WithOtherFields) -> Result<(), EthApiError>; + async fn upsert_transaction(&self, transaction: ExtendedTransaction) -> Result<(), EthApiError>; } #[async_trait] impl EthereumTransactionStore for Database { #[instrument(skip_all, name = "db::transaction", err)] - async fn transaction(&self, hash: &B256) -> Result>, EthApiError> { + async fn transaction(&self, hash: &B256) -> Result, EthApiError> { let filter = EthDatabaseFilterBuilder::::default().with_tx_hash(hash).build(); Ok(self.get_one::(filter, None).await?.map(Into::into)) } @@ -42,7 +45,7 @@ impl EthereumTransactionStore for Database { async fn transactions( &self, block_hash_or_number: BlockHashOrNumber, - ) -> Result>, EthApiError> { + ) -> Result, EthApiError> { let filter = EthDatabaseFilterBuilder::::default() .with_block_hash_or_number(block_hash_or_number) .build(); @@ -51,7 +54,7 @@ impl EthereumTransactionStore for Database { } #[instrument(skip_all, name = "db::upsert_transaction", err)] - async fn upsert_transaction(&self, transaction: WithOtherFields) -> Result<(), EthApiError> { + async fn upsert_transaction(&self, transaction: ExtendedTransaction) -> Result<(), EthApiError> { let filter = EthDatabaseFilterBuilder::::default().with_tx_hash(&transaction.hash).build(); Ok(self.update_one(StoredTransaction::from(transaction), filter, true).await?) } @@ -72,7 +75,7 @@ pub trait EthereumBlockStore { &self, block_hash_or_number: BlockHashOrNumber, full: bool, - ) -> Result>>>, EthApiError>; + ) -> Result, EthApiError>; /// Returns true if the block with the given hash or number exists. #[instrument(skip(self), name = "db::block_exists", err)] async fn block_exists(&self, block_hash_or_number: BlockHashOrNumber) -> Result { @@ -110,7 +113,7 @@ impl EthereumBlockStore for Database { &self, block_hash_or_number: BlockHashOrNumber, full: bool, - ) -> Result>>>, EthApiError> { + ) -> Result, EthApiError> { let maybe_header = self.header(block_hash_or_number).await?; if maybe_header.is_none() { return Ok(None); @@ -303,8 +306,8 @@ mod tests { let block_hash = header.hash; - let block: WithOtherFields>> = { - let transactions: Vec> = mongo_fuzzer + let block: ExtendedBlock = { + let transactions: Vec = mongo_fuzzer .transactions .iter() .filter_map(|stored_transaction| { diff --git a/src/providers/eth_provider/database/types/header.rs b/src/providers/eth_provider/database/types/header.rs index bcb35a985..631198047 100644 --- a/src/providers/eth_provider/database/types/header.rs +++ b/src/providers/eth_provider/database/types/header.rs @@ -1,4 +1,6 @@ -use alloy_rpc_types::Header; +use super::transaction::ExtendedTransaction; +use alloy_rpc_types::{Block, Header}; +use alloy_serde::WithOtherFields; use serde::{Deserialize, Serialize}; use std::ops::Deref; #[cfg(any(test, feature = "arbitrary", feature = "testing"))] @@ -8,6 +10,9 @@ use { reth_primitives::constants::EMPTY_ROOT_HASH, }; +/// Type alias for a block that contains extended transactions and additional fields. +pub type ExtendedBlock = WithOtherFields>; + /// A header as stored in the database #[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq, Eq)] pub struct StoredHeader { diff --git a/src/providers/eth_provider/database/types/receipt.rs b/src/providers/eth_provider/database/types/receipt.rs index e998bd68c..958ba1eb4 100644 --- a/src/providers/eth_provider/database/types/receipt.rs +++ b/src/providers/eth_provider/database/types/receipt.rs @@ -4,6 +4,9 @@ use alloy_serde::WithOtherFields; use reth_primitives::Receipt; use serde::{Deserialize, Serialize}; +/// Type alias for a transaction receipt with additional fields. +pub type ExtendedTxReceipt = WithOtherFields; + /// A transaction receipt as stored in the database #[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)] pub struct StoredTransactionReceipt { diff --git a/src/providers/eth_provider/database/types/transaction.rs b/src/providers/eth_provider/database/types/transaction.rs index 8d8dcff94..66d8442d6 100644 --- a/src/providers/eth_provider/database/types/transaction.rs +++ b/src/providers/eth_provider/database/types/transaction.rs @@ -11,6 +11,8 @@ use { reth_primitives::transaction::legacy_parity, reth_testing_utils::generators::{self}, }; +/// Type alias for a transaction with additional fields. +pub type ExtendedTransaction = WithOtherFields; /// A full transaction as stored in the database #[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)] diff --git a/src/providers/eth_provider/receipts.rs b/src/providers/eth_provider/receipts.rs index 9d4a24714..4a867bec7 100644 --- a/src/providers/eth_provider/receipts.rs +++ b/src/providers/eth_provider/receipts.rs @@ -3,12 +3,11 @@ use crate::providers::eth_provider::{ database::{ ethereum::EthereumBlockStore, filter::{self}, + types::receipt::ExtendedTxReceipt, }, provider::{EthApiResult, EthDataProvider}, }; use alloy_primitives::B256; -use alloy_rpc_types::TransactionReceipt; -use alloy_serde::WithOtherFields; use async_trait::async_trait; use auto_impl::auto_impl; use mongodb::bson::doc; @@ -18,13 +17,10 @@ use reth_primitives::{BlockId, BlockNumberOrTag}; #[auto_impl(Arc, &)] pub trait ReceiptProvider { /// Returns the transaction receipt by hash of the transaction. - async fn transaction_receipt(&self, hash: B256) -> EthApiResult>>; + async fn transaction_receipt(&self, hash: B256) -> EthApiResult>; /// Returns the block receipts for a block. - async fn block_receipts( - &self, - block_id: Option, - ) -> EthApiResult>>>; + async fn block_receipts(&self, block_id: Option) -> EthApiResult>>; } #[async_trait] @@ -32,15 +28,12 @@ impl ReceiptProvider for EthDataProvider where SP: starknet::providers::Provider + Send + Sync, { - async fn transaction_receipt(&self, hash: B256) -> EthApiResult>> { + async fn transaction_receipt(&self, hash: B256) -> EthApiResult> { let filter = EthDatabaseFilterBuilder::::default().with_tx_hash(&hash).build(); Ok(self.database().get_one::(filter, None).await?.map(Into::into)) } - async fn block_receipts( - &self, - block_id: Option, - ) -> EthApiResult>>> { + async fn block_receipts(&self, block_id: Option) -> EthApiResult>> { match block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)) { BlockId::Number(number_or_tag) => { let block_number = self.tag_into_block_number(number_or_tag).await?; diff --git a/src/providers/eth_provider/transactions.rs b/src/providers/eth_provider/transactions.rs index d90cc30fa..4906c49c9 100644 --- a/src/providers/eth_provider/transactions.rs +++ b/src/providers/eth_provider/transactions.rs @@ -1,5 +1,8 @@ use super::{ - database::{filter::EthDatabaseFilterBuilder, types::transaction::StoredTransaction}, + database::{ + filter::EthDatabaseFilterBuilder, + types::transaction::{ExtendedTransaction, StoredTransaction}, + }, error::ExecutionError, starknet::kakarot_core::{account_contract::AccountContractReader, starknet_address}, utils::{contract_not_found, entrypoint_not_found}, @@ -13,8 +16,7 @@ use crate::{ }, }; use alloy_primitives::{Address, B256, U256}; -use alloy_rpc_types::{Index, Transaction}; -use alloy_serde::WithOtherFields; +use alloy_rpc_types::Index; use async_trait::async_trait; use auto_impl::auto_impl; use mongodb::bson::doc; @@ -25,21 +27,21 @@ use tracing::Instrument; #[auto_impl(Arc, &)] pub trait TransactionProvider: ChainProvider { /// Returns the transaction by hash. - async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>>; + async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>; /// Returns the transaction by block hash and index. async fn transaction_by_block_hash_and_index( &self, hash: B256, index: Index, - ) -> EthApiResult>>; + ) -> EthApiResult>; /// Returns the transaction by block number and index. async fn transaction_by_block_number_and_index( &self, number_or_tag: BlockNumberOrTag, index: Index, - ) -> EthApiResult>>; + ) -> EthApiResult>; /// Returns the nonce for the address at the given block. async fn transaction_count(&self, address: Address, block_id: Option) -> EthApiResult; @@ -50,7 +52,7 @@ impl TransactionProvider for EthDataProvider where SP: starknet::providers::Provider + Send + Sync, { - async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>> { + async fn transaction_by_hash(&self, hash: B256) -> EthApiResult> { let filter = EthDatabaseFilterBuilder::::default().with_tx_hash(&hash).build(); Ok(self.database().get_one::(filter, None).await?.map(Into::into)) } @@ -59,7 +61,7 @@ where &self, hash: B256, index: Index, - ) -> EthApiResult>> { + ) -> EthApiResult> { let filter = EthDatabaseFilterBuilder::::default() .with_block_hash(&hash) .with_tx_index(&index) @@ -71,7 +73,7 @@ where &self, number_or_tag: BlockNumberOrTag, index: Index, - ) -> EthApiResult>> { + ) -> EthApiResult> { let block_number = self.tag_into_block_number(number_or_tag).await?; let filter = EthDatabaseFilterBuilder::::default() .with_block_number(block_number) diff --git a/src/providers/eth_provider/tx_pool.rs b/src/providers/eth_provider/tx_pool.rs index cf734dba4..8e5d985b1 100644 --- a/src/providers/eth_provider/tx_pool.rs +++ b/src/providers/eth_provider/tx_pool.rs @@ -1,7 +1,6 @@ +use super::database::types::transaction::ExtendedTransaction; use crate::providers::eth_provider::provider::EthApiResult; -use alloy_rpc_types::Transaction; use alloy_rpc_types_txpool::TxpoolContent; -use alloy_serde::WithOtherFields; use async_trait::async_trait; use auto_impl::auto_impl; use mongodb::bson::doc; @@ -11,8 +10,8 @@ use mongodb::bson::doc; #[auto_impl(Arc, &)] pub trait TxPoolProvider { /// Returns a vec of pending pool transactions. - fn content(&self) -> TxpoolContent>; + fn content(&self) -> TxpoolContent; /// Returns the content of the pending pool. - async fn txpool_content(&self) -> EthApiResult>>; + async fn txpool_content(&self) -> EthApiResult>; } diff --git a/src/providers/pool_provider.rs b/src/providers/pool_provider.rs index 845dd8881..e73451271 100644 --- a/src/providers/pool_provider.rs +++ b/src/providers/pool_provider.rs @@ -1,9 +1,7 @@ use super::eth_provider::TxPoolProvider; -use crate::providers::eth_provider::provider::EthApiResult; +use crate::providers::eth_provider::{database::types::transaction::ExtendedTransaction, provider::EthApiResult}; use alloy_primitives::Address; -use alloy_rpc_types::Transaction; use alloy_rpc_types_txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, TxpoolInspectSummary, TxpoolStatus}; -use alloy_serde::WithOtherFields; use async_trait::async_trait; use auto_impl::auto_impl; @@ -12,9 +10,8 @@ use auto_impl::auto_impl; pub trait PoolProvider { async fn txpool_status(&self) -> EthApiResult; async fn txpool_inspect(&self) -> EthApiResult; - async fn txpool_content_from(&self, from: Address) - -> EthApiResult>>; - async fn txpool_content(&self) -> EthApiResult>>; + async fn txpool_content_from(&self, from: Address) -> EthApiResult>; + async fn txpool_content(&self) -> EthApiResult>; } #[derive(Debug, Clone)] @@ -73,14 +70,11 @@ impl PoolProvider for PoolDataProvide Ok(inspect) } - async fn txpool_content_from( - &self, - from: Address, - ) -> EthApiResult>> { + async fn txpool_content_from(&self, from: Address) -> EthApiResult> { Ok(self.eth_provider.txpool_content().await?.remove_from(&from)) } - async fn txpool_content(&self) -> EthApiResult>> { + async fn txpool_content(&self) -> EthApiResult> { Ok(self.eth_provider.txpool_content().await?) } } diff --git a/src/test_utils/katana/mod.rs b/src/test_utils/katana/mod.rs index 1d15f06c7..e91e38cf0 100644 --- a/src/test_utils/katana/mod.rs +++ b/src/test_utils/katana/mod.rs @@ -9,7 +9,10 @@ use crate::{ ethereum::EthereumTransactionStore, filter::{self, format_hex, EthDatabaseFilterBuilder}, types::{ - header::StoredHeader, log::StoredLog, receipt::StoredTransactionReceipt, transaction::StoredTransaction, + header::StoredHeader, + log::StoredLog, + receipt::{ExtendedTxReceipt, StoredTransactionReceipt}, + transaction::{ExtendedTransaction, StoredTransaction}, }, CollectionName, }, @@ -37,7 +40,6 @@ use { super::mongo::MongoFuzzer, alloy_primitives::B256, alloy_rpc_types::Header, - alloy_rpc_types::{Transaction, TransactionReceipt}, alloy_serde::WithOtherFields, katana_node::config::{ rpc::{ApiKind, RpcConfig}, @@ -281,11 +283,7 @@ impl<'a> Katana { } /// Adds transactions to the database along with a corresponding header. - pub async fn add_transactions_with_header_to_database( - &self, - txs: Vec>, - header: Header, - ) { + pub async fn add_transactions_with_header_to_database(&self, txs: Vec, header: Header) { let provider = self.eth_provider(); let database = provider.database(); let Header { number, .. } = header; @@ -329,7 +327,7 @@ impl<'a> Katana { } /// Retrieves the first stored transaction - pub fn first_transaction(&self) -> Option> { + pub fn first_transaction(&self) -> Option { self.transactions.first().map(Into::into) } @@ -339,14 +337,14 @@ impl<'a> Katana { } /// Retrieves the most recent stored transaction based on block number - pub fn most_recent_transaction(&self) -> Option> { + pub fn most_recent_transaction(&self) -> Option { self.transactions .iter() .max_by_key(|stored_transaction| stored_transaction.block_number.unwrap_or_default()) .map(Into::into) } - pub fn most_recent_run_out_of_resources_receipt(&self) -> Option> { + pub fn most_recent_run_out_of_resources_receipt(&self) -> Option { self.receipts .iter() .filter_map(|stored_receipt| { diff --git a/src/test_utils/mock_provider.rs b/src/test_utils/mock_provider.rs index b4b8993e5..b9e3b53b6 100644 --- a/src/test_utils/mock_provider.rs +++ b/src/test_utils/mock_provider.rs @@ -1,12 +1,10 @@ use crate::providers::eth_provider::{ - provider::EthApiResult, BlockProvider, ChainProvider, GasProvider, LogProvider, ReceiptProvider, StateProvider, - TransactionProvider, + database::types::{header::ExtendedBlock, receipt::ExtendedTxReceipt, transaction::ExtendedTransaction}, + provider::EthApiResult, + BlockProvider, ChainProvider, GasProvider, LogProvider, ReceiptProvider, StateProvider, TransactionProvider, }; use alloy_primitives::{Address, Bytes, B256, U256, U64}; -use alloy_rpc_types::{ - Block, Filter, FilterChanges, Header, SyncStatus, Transaction, TransactionReceipt, TransactionRequest, -}; -use alloy_serde::WithOtherFields; +use alloy_rpc_types::{Filter, FilterChanges, Header, SyncStatus, TransactionRequest}; use async_trait::async_trait; use mockall::mock; use reth_primitives::{BlockId, BlockNumberOrTag}; @@ -26,19 +24,19 @@ mock! { &self, hash: B256, full: bool, - ) -> EthApiResult>>>>; + ) -> EthApiResult>; async fn block_by_number( &self, number: BlockNumberOrTag, full: bool, - ) -> EthApiResult>>>>; + ) -> EthApiResult>; async fn block_transaction_count_by_hash(&self, hash: B256) -> EthApiResult>; async fn block_transaction_count_by_number(&self, number_or_tag: BlockNumberOrTag) -> EthApiResult>; - async fn block_transactions(&self, block_id: Option) -> EthApiResult>>>; + async fn block_transactions(&self, block_id: Option) -> EthApiResult>>; } #[async_trait] @@ -64,9 +62,9 @@ mock! { #[async_trait] impl ReceiptProvider for EthereumProviderStruct { - async fn transaction_receipt(&self, hash: B256) -> EthApiResult>>; + async fn transaction_receipt(&self, hash: B256) -> EthApiResult>; - async fn block_receipts(&self, block_id: Option) -> EthApiResult>>>; + async fn block_receipts(&self, block_id: Option) -> EthApiResult>>; } #[async_trait] @@ -82,11 +80,11 @@ mock! { #[async_trait] impl TransactionProvider for EthereumProviderStruct { - async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>>; + async fn transaction_by_hash(&self, hash: B256) -> EthApiResult>; - async fn transaction_by_block_hash_and_index(&self, hash: B256, index: alloy_rpc_types::Index) -> EthApiResult>>; + async fn transaction_by_block_hash_and_index(&self, hash: B256, index: alloy_rpc_types::Index) -> EthApiResult>; - async fn transaction_by_block_number_and_index(&self, number_or_tag: BlockNumberOrTag, index: alloy_rpc_types::Index) -> EthApiResult>>; + async fn transaction_by_block_number_and_index(&self, number_or_tag: BlockNumberOrTag, index: alloy_rpc_types::Index) -> EthApiResult>; async fn transaction_count(&self, address: Address, block_id: Option) -> EthApiResult; } diff --git a/src/test_utils/mongo/mod.rs b/src/test_utils/mongo/mod.rs index 6e0a98780..8179124e5 100644 --- a/src/test_utils/mongo/mod.rs +++ b/src/test_utils/mongo/mod.rs @@ -2,14 +2,16 @@ use crate::providers::eth_provider::{ constant::U64_HEX_STRING_LEN, database::{ types::{ - header::StoredHeader, log::StoredLog, receipt::StoredTransactionReceipt, transaction::StoredTransaction, + header::StoredHeader, + log::StoredLog, + receipt::{ExtendedTxReceipt, StoredTransactionReceipt}, + transaction::StoredTransaction, }, CollectionName, Database, }, }; use alloy_primitives::{B256, U256}; -use alloy_rpc_types::{Transaction, TransactionReceipt}; -use alloy_serde::WithOtherFields; +use alloy_rpc_types::Transaction; use arbitrary::Arbitrary; use mongodb::{ bson::{self, doc, Document}, @@ -213,7 +215,7 @@ impl MongoFuzzer { // Generate a receipt for the transaction. let receipt = self.generate_transaction_receipt(&transaction.tx); // add an isRunOutOfRessources field to the receipt - let mut receipt_with_other_fields: WithOtherFields = receipt.into(); + let mut receipt_with_other_fields: ExtendedTxReceipt = receipt.into(); receipt_with_other_fields.other.insert("isRunOutOfRessources".to_string(), serde_json::Value::Bool(true)); let stored_receipt = StoredTransactionReceipt { receipt: receipt_with_other_fields }; diff --git a/src/tracing/builder.rs b/src/tracing/builder.rs index 7868d58c6..301449a6c 100644 --- a/src/tracing/builder.rs +++ b/src/tracing/builder.rs @@ -1,13 +1,15 @@ use super::{Tracer, TracerResult}; use crate::providers::eth_provider::{ - database::state::{EthCacheDatabase, EthDatabase}, + database::{ + state::{EthCacheDatabase, EthDatabase}, + types::transaction::ExtendedTransaction, + }, error::{EthApiError, TransactionError}, provider::EthereumProvider, }; use alloy_primitives::{B256, U256}; -use alloy_rpc_types::{Block, BlockHashOrNumber, BlockId, BlockTransactions, Header, Transaction}; +use alloy_rpc_types::{Block, BlockHashOrNumber, BlockId, BlockTransactions, Header}; use alloy_rpc_types_trace::geth::{GethDebugTracingCallOptions, GethDebugTracingOptions}; -use alloy_serde::WithOtherFields; use reth_revm::{ db::CacheDB, primitives::{BlockEnv, CfgEnv, Env, EnvWithHandlerCfg, HandlerCfg, SpecId}, @@ -90,7 +92,7 @@ impl From for TracingOptions { pub struct TracerBuilder { eth_provider: P, env: Env, - block: Block>, + block: Block, tracing_options: TracingOptions, _phantom: std::marker::PhantomData, } @@ -150,7 +152,7 @@ impl TracerBuilder { /// # Returns /// /// Returns the block if it exists, otherwise returns None - async fn block(&self, block_id: BlockId) -> TracerResult>> { + async fn block(&self, block_id: BlockId) -> TracerResult> { let block = match block_id { BlockId::Hash(hash) => self.eth_provider.block_by_hash(hash.block_hash, true).await?, BlockId::Number(number) => self.eth_provider.block_by_number(number, true).await?, diff --git a/tests/tests/debug_api.rs b/tests/tests/debug_api.rs index 140371730..29dd67a50 100644 --- a/tests/tests/debug_api.rs +++ b/tests/tests/debug_api.rs @@ -3,11 +3,11 @@ use alloy_eips::eip2718::{Decodable2718, Encodable2718}; use alloy_primitives::Bytes; use alloy_rlp::Encodable; -use alloy_rpc_types::{Transaction, TransactionInfo}; +use alloy_rpc_types::TransactionInfo; use alloy_serde::WithOtherFields; use kakarot_rpc::{ client::TransactionHashProvider, - providers::eth_provider::{BlockProvider, ReceiptProvider}, + providers::eth_provider::{database::types::transaction::ExtendedTransaction, BlockProvider, ReceiptProvider}, test_utils::{ fixtures::{katana, setup}, katana::Katana, @@ -320,7 +320,7 @@ async fn test_raw_block(#[future] katana: Katana, _setup: ()) { .expect("Failed to call Debug RPC"); let response = res.text().await.expect("Failed to get response body"); let response: Value = serde_json::from_str(&response).expect("Failed to deserialize response body"); - let rpc_block: WithOtherFields>> = + let rpc_block: WithOtherFields> = serde_json::from_value(response["result"].clone()).expect("Failed to deserialize result"); let primitive_block = Block::try_from(rpc_block.inner).unwrap(); diff --git a/tests/tests/txpool_api.rs b/tests/tests/txpool_api.rs index 9d87374b0..2566a6b15 100644 --- a/tests/tests/txpool_api.rs +++ b/tests/tests/txpool_api.rs @@ -5,10 +5,13 @@ use alloy_rpc_types::Transaction; use alloy_rpc_types_txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, TxpoolInspectSummary, TxpoolStatus}; use alloy_serde::WithOtherFields; use jsonrpsee::server::ServerHandle; -use kakarot_rpc::test_utils::{ - fixtures::{katana_empty, setup}, - katana::Katana, - rpc::{start_kakarot_rpc_server, RawRpcParamsBuilder}, +use kakarot_rpc::{ + providers::eth_provider::database::types::transaction::ExtendedTransaction, + test_utils::{ + fixtures::{katana_empty, setup}, + katana::Katana, + rpc::{start_kakarot_rpc_server, RawRpcParamsBuilder}, + }, }; use reth_transaction_pool::{TransactionOrigin, TransactionPool}; use rstest::*; @@ -74,7 +77,7 @@ async fn test_txpool_content(#[future] katana_empty: Katana, _setup: ()) { .expect("Failed to insert transaction into the mempool"); // Fetch the transaction pool content - let tx_pool_content: TxpoolContent> = + let tx_pool_content: TxpoolContent = request("txpool_content", server_addr.port(), Vec::::new()).await; // Get updated mempool size @@ -131,7 +134,7 @@ async fn test_txpool_content_from(#[future] katana_empty: Katana, _setup: ()) { let transaction_signer = transaction_signed.recover_signer().unwrap(); // Fetch the transaction pool content from the sender - let tx_pool_content: TxpoolContentFrom> = + let tx_pool_content: TxpoolContentFrom = request("txpool_contentFrom", server_addr.port(), vec![transaction_signer.to_string()]).await; // Assert that we recovered a single pending transaction