Skip to content

Commit

Permalink
dev: simplify WithOtherFields<T> usage (#1486)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Gerson2102 authored Oct 23, 2024
1 parent c5a2667 commit 0bffa25
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 172 deletions.
14 changes: 6 additions & 8 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
},
providers::{
eth_provider::{
database::Database,
database::{types::transaction::ExtendedTransaction, Database},
error::SignatureError,
provider::{EthApiResult, EthDataProvider},
TransactionProvider, TxPoolProvider,
Expand All @@ -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};
Expand All @@ -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<Option<WithOtherFields<Transaction>>>;
async fn transaction_by_hash(&self, hash: B256) -> EthApiResult<Option<ExtendedTransaction>>;
}

/// Provides a wrapper structure around the Ethereum Provider
Expand Down Expand Up @@ -133,11 +131,11 @@ impl<SP> TxPoolProvider for EthClient<SP>
where
SP: starknet::providers::Provider + Send + Sync,
{
fn content(&self) -> TxpoolContent<WithOtherFields<Transaction>> {
fn content(&self) -> TxpoolContent<ExtendedTransaction> {
#[inline]
fn insert<T: PoolTransaction<Consensus = TransactionSignedEcRecovered>>(
tx: &T,
content: &mut BTreeMap<Address, BTreeMap<String, WithOtherFields<Transaction>>>,
content: &mut BTreeMap<Address, BTreeMap<String, ExtendedTransaction>>,
) {
content.entry(tx.sender()).or_default().insert(
tx.nonce().to_string(),
Expand All @@ -160,7 +158,7 @@ where
content
}

async fn txpool_content(&self) -> EthApiResult<TxpoolContent<WithOtherFields<Transaction>>> {
async fn txpool_content(&self) -> EthApiResult<TxpoolContent<ExtendedTransaction>> {
Ok(self.content())
}
}
Expand All @@ -170,7 +168,7 @@ impl<SP> TransactionHashProvider for EthClient<SP>
where
SP: starknet::providers::Provider + Send + Sync,
{
async fn transaction_by_hash(&self, hash: B256) -> EthApiResult<Option<WithOtherFields<Transaction>>> {
async fn transaction_by_hash(&self, hash: B256) -> EthApiResult<Option<ExtendedTransaction>> {
Ok(self
.pool
.get(&hash)
Expand Down
10 changes: 4 additions & 6 deletions src/eth_rpc/api/eth_api.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<Option<WithOtherFields<TransactionReceipt>>>;
async fn transaction_receipt(&self, hash: B256) -> RpcResult<Option<ExtendedTxReceipt>>;

/// Returns the balance of the account of given address.
#[method(name = "getBalance")]
Expand Down Expand Up @@ -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<BlockId>,
) -> RpcResult<Option<Vec<WithOtherFields<TransactionReceipt>>>>;
async fn block_receipts(&self, block_id: Option<BlockId>) -> RpcResult<Option<Vec<ExtendedTxReceipt>>>;
}
7 changes: 3 additions & 4 deletions src/eth_rpc/api/txpool_api.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<TxpoolContentFrom<WithOtherFields<Transaction>>>;
async fn txpool_content_from(&self, from: Address) -> RpcResult<TxpoolContentFrom<ExtendedTransaction>>;

/// 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<TxpoolContent<WithOtherFields<Transaction>>>;
async fn txpool_content(&self) -> RpcResult<TxpoolContent<ExtendedTransaction>>;
}
45 changes: 15 additions & 30 deletions src/eth_rpc/servers/eth_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,20 +67,12 @@ where
}

#[tracing::instrument(skip(self), ret, err)]
async fn block_by_hash(
&self,
hash: B256,
full: bool,
) -> RpcResult<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>> {
async fn block_by_hash(&self, hash: B256, full: bool) -> RpcResult<Option<ExtendedBlock>> {
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<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>> {
async fn block_by_number(&self, number: BlockNumberOrTag, full: bool) -> RpcResult<Option<ExtendedBlock>> {
Ok(self.eth_client.eth_provider().block_by_number(number, full).await?)
}

Expand All @@ -104,11 +96,7 @@ where
Ok(U256::ZERO)
}

async fn uncle_by_block_hash_and_index(
&self,
_hash: B256,
_index: Index,
) -> RpcResult<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>> {
async fn uncle_by_block_hash_and_index(&self, _hash: B256, _index: Index) -> RpcResult<Option<ExtendedBlock>> {
tracing::warn!("Kakarot chain does not produce uncles");
Ok(None)
}
Expand All @@ -117,13 +105,13 @@ where
&self,
_number: BlockNumberOrTag,
_index: Index,
) -> RpcResult<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>> {
) -> RpcResult<Option<ExtendedBlock>> {
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<Option<WithOtherFields<Transaction>>> {
async fn transaction_by_hash(&self, hash: B256) -> RpcResult<Option<ExtendedTransaction>> {
Ok(self.eth_client.transaction_by_hash(hash).await?)
}

Expand All @@ -132,7 +120,7 @@ where
&self,
hash: B256,
index: Index,
) -> RpcResult<Option<WithOtherFields<Transaction>>> {
) -> RpcResult<Option<ExtendedTransaction>> {
Ok(self.eth_client.eth_provider().transaction_by_block_hash_and_index(hash, index).await?)
}

Expand All @@ -141,12 +129,12 @@ where
&self,
number: BlockNumberOrTag,
index: Index,
) -> RpcResult<Option<WithOtherFields<Transaction>>> {
) -> RpcResult<Option<ExtendedTransaction>> {
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<Option<WithOtherFields<TransactionReceipt>>> {
async fn transaction_receipt(&self, hash: B256) -> RpcResult<Option<ExtendedTxReceipt>> {
Ok(self.eth_client.eth_provider().transaction_receipt(hash).await?)
}

Expand Down Expand Up @@ -303,10 +291,7 @@ where
Err(EthApiError::Unsupported("eth_getFilterLogs").into())
}

async fn block_receipts(
&self,
block_id: Option<BlockId>,
) -> RpcResult<Option<Vec<WithOtherFields<TransactionReceipt>>>> {
async fn block_receipts(&self, block_id: Option<BlockId>) -> RpcResult<Option<Vec<ExtendedTxReceipt>>> {
Ok(self.eth_client.eth_provider().block_receipts(block_id).await?)
}
}
11 changes: 6 additions & 5 deletions src/eth_rpc/servers/txpool_rpc.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<TxpoolContentFrom<WithOtherFields<Transaction>>> {
async fn txpool_content_from(&self, from: Address) -> RpcResult<TxpoolContentFrom<ExtendedTransaction>> {
self.pool_provider.txpool_content_from(from).await.map_err(Into::into)
}

Expand All @@ -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<TxpoolContent<WithOtherFields<Transaction>>> {
async fn txpool_content(&self) -> RpcResult<TxpoolContent<ExtendedTransaction>> {
self.pool_provider.txpool_content().await.map_err(Into::into)
}
}
43 changes: 15 additions & 28 deletions src/providers/eth_provider/blocks.rs
Original file line number Diff line number Diff line change
@@ -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, &)]
Expand All @@ -23,18 +27,11 @@ pub trait BlockProvider {
async fn block_number(&self) -> EthApiResult<U64>;

/// 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<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>>;
async fn block_by_hash(&self, hash: B256, full: bool) -> EthApiResult<Option<ExtendedBlock>>;

/// 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<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>>;
async fn block_by_number(&self, number_or_tag: BlockNumberOrTag, full: bool)
-> EthApiResult<Option<ExtendedBlock>>;

/// Returns the transaction count for a block by hash.
async fn block_transaction_count_by_hash(&self, hash: B256) -> EthApiResult<Option<U256>>;
Expand All @@ -43,10 +40,7 @@ pub trait BlockProvider {
async fn block_transaction_count_by_number(&self, number_or_tag: BlockNumberOrTag) -> EthApiResult<Option<U256>>;

/// Returns the transactions for a block.
async fn block_transactions(
&self,
block_id: Option<BlockId>,
) -> EthApiResult<Option<Vec<WithOtherFields<Transaction>>>>;
async fn block_transactions(&self, block_id: Option<BlockId>) -> EthApiResult<Option<Vec<ExtendedTransaction>>>;
}

#[async_trait]
Expand Down Expand Up @@ -76,19 +70,15 @@ where
Ok(block_number)
}

async fn block_by_hash(
&self,
hash: B256,
full: bool,
) -> EthApiResult<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>> {
async fn block_by_hash(&self, hash: B256, full: bool) -> EthApiResult<Option<ExtendedBlock>> {
Ok(self.database().block(hash.into(), full).await?)
}

async fn block_by_number(
&self,
number_or_tag: BlockNumberOrTag,
full: bool,
) -> EthApiResult<Option<WithOtherFields<Block<WithOtherFields<Transaction>>>>> {
) -> EthApiResult<Option<ExtendedBlock>> {
let block_number = self.tag_into_block_number(number_or_tag).await?;
Ok(self.database().block(block_number.into(), full).await?)
}
Expand All @@ -102,10 +92,7 @@ where
self.database().transaction_count(block_number.into()).await
}

async fn block_transactions(
&self,
block_id: Option<BlockId>,
) -> EthApiResult<Option<Vec<WithOtherFields<Transaction>>>> {
async fn block_transactions(&self, block_id: Option<BlockId>) -> EthApiResult<Option<Vec<ExtendedTransaction>>> {
let block_hash_or_number = self
.block_id_into_block_number_or_hash(block_id.unwrap_or_else(|| BlockNumberOrTag::Latest.into()))
.await?;
Expand Down
Loading

0 comments on commit 0bffa25

Please sign in to comment.