From 24f04710c0d94bb61db65f90d166ef1c8d39a5d6 Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Sat, 31 Aug 2024 00:58:59 +0800 Subject: [PATCH] feat: use alloy_network::Network::ReceiptResponse instead of AnyTransactionReceipt in RPC (#10621) --- crates/rpc/rpc-builder/src/lib.rs | 8 +- crates/rpc/rpc-builder/tests/it/http.rs | 114 ++++++++++++------ crates/rpc/rpc-builder/tests/it/middleware.rs | 4 +- crates/rpc/rpc-eth-api/src/core.rs | 43 ++++--- crates/rpc/rpc-eth-api/src/lib.rs | 2 +- crates/rpc/rpc-eth-api/src/types.rs | 9 +- crates/rpc/rpc-testing-util/src/debug.rs | 4 +- crates/rpc/rpc-testing-util/tests/it/trace.rs | 3 +- crates/rpc/rpc/src/engine.rs | 9 +- crates/rpc/rpc/src/eth/core.rs | 8 +- crates/rpc/rpc/src/otterscan.rs | 9 +- 11 files changed, 139 insertions(+), 74 deletions(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 6c454bff6b57..58f2d9b8fe13 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -791,7 +791,11 @@ where /// If called outside of the tokio runtime. See also [`Self::eth_api`] pub fn register_eth(&mut self) -> &mut Self where - EthApi: EthApiServer, + EthApi: EthApiServer< + reth_rpc_types::Transaction, + reth_rpc_types::Block, + reth_rpc_types::AnyTransactionReceipt, + >, { let eth_api = self.eth_api().clone(); self.modules.insert(RethRpcModule::Eth, eth_api.into_rpc().into()); @@ -808,6 +812,7 @@ where EthApi: EthApiServer< WithOtherFields, reth_rpc_types::Block>, + reth_rpc_types::AnyTransactionReceipt, > + TraceExt, { let otterscan_api = self.otterscan_api(); @@ -911,6 +916,7 @@ where EthApi: EthApiServer< WithOtherFields, reth_rpc_types::Block>, + reth_rpc_types::AnyTransactionReceipt, > + TraceExt, { let eth_api = self.eth_api().clone(); diff --git a/crates/rpc/rpc-builder/tests/it/http.rs b/crates/rpc/rpc-builder/tests/it/http.rs index 8e9c7ce60835..659f9dc70ac5 100644 --- a/crates/rpc/rpc-builder/tests/it/http.rs +++ b/crates/rpc/rpc-builder/tests/it/http.rs @@ -13,7 +13,8 @@ use jsonrpsee::{ }; use reth_network_peers::NodeRecord; use reth_primitives::{ - hex_literal::hex, Address, BlockId, BlockNumberOrTag, Bytes, TxHash, B256, B64, U256, U64, + hex_literal::hex, Address, BlockId, BlockNumberOrTag, Bytes, Receipt, TxHash, B256, B64, U256, + U64, }; use reth_rpc_api::{ clients::{AdminApiClient, EthApiClient}, @@ -173,68 +174,93 @@ where .unwrap(); // Implemented - EthApiClient::::protocol_version(client).await.unwrap(); - EthApiClient::::chain_id(client).await.unwrap(); - EthApiClient::::accounts(client).await.unwrap(); - EthApiClient::::get_account(client, address, block_number.into()) + EthApiClient::::protocol_version(client).await.unwrap(); + EthApiClient::::chain_id(client).await.unwrap(); + EthApiClient::::accounts(client).await.unwrap(); + EthApiClient::::get_account(client, address, block_number.into()) .await .unwrap(); - EthApiClient::::block_number(client).await.unwrap(); - EthApiClient::::get_code(client, address, None).await.unwrap(); - EthApiClient::::send_raw_transaction(client, tx).await.unwrap(); - EthApiClient::::fee_history(client, U64::from(0), block_number, None) + EthApiClient::::block_number(client).await.unwrap(); + EthApiClient::::get_code(client, address, None).await.unwrap(); + EthApiClient::::send_raw_transaction(client, tx).await.unwrap(); + EthApiClient::::fee_history( + client, + U64::from(0), + block_number, + None, + ) + .await + .unwrap(); + EthApiClient::::balance(client, address, None).await.unwrap(); + EthApiClient::::transaction_count(client, address, None) .await .unwrap(); - EthApiClient::::balance(client, address, None).await.unwrap(); - EthApiClient::::transaction_count(client, address, None).await.unwrap(); - EthApiClient::::storage_at(client, address, U256::default().into(), None) + EthApiClient::::storage_at( + client, + address, + U256::default().into(), + None, + ) + .await + .unwrap(); + EthApiClient::::block_by_hash(client, hash, false).await.unwrap(); + EthApiClient::::block_by_number(client, block_number, false) .await .unwrap(); - EthApiClient::::block_by_hash(client, hash, false).await.unwrap(); - EthApiClient::::block_by_number(client, block_number, false).await.unwrap(); - EthApiClient::::block_transaction_count_by_number(client, block_number) + EthApiClient::::block_transaction_count_by_number( + client, + block_number, + ) + .await + .unwrap(); + EthApiClient::::block_transaction_count_by_hash(client, hash) .await .unwrap(); - EthApiClient::::block_transaction_count_by_hash(client, hash) + EthApiClient::::block_uncles_count_by_hash(client, hash) .await .unwrap(); - EthApiClient::::block_uncles_count_by_hash(client, hash).await.unwrap(); - EthApiClient::::block_uncles_count_by_number(client, block_number) + EthApiClient::::block_uncles_count_by_number(client, block_number) .await .unwrap(); - EthApiClient::::uncle_by_block_hash_and_index(client, hash, index) + EthApiClient::::uncle_by_block_hash_and_index(client, hash, index) .await .unwrap(); - EthApiClient::::uncle_by_block_number_and_index( + EthApiClient::::uncle_by_block_number_and_index( client, block_number, index, ) .await .unwrap(); - EthApiClient::::sign(client, address, bytes.clone()).await.unwrap_err(); - EthApiClient::::sign_typed_data(client, address, typed_data) + EthApiClient::::sign(client, address, bytes.clone()) + .await + .unwrap_err(); + EthApiClient::::sign_typed_data(client, address, typed_data) .await .unwrap_err(); - EthApiClient::::transaction_by_hash(client, tx_hash).await.unwrap(); - EthApiClient::::transaction_by_block_hash_and_index(client, hash, index) + EthApiClient::::transaction_by_hash(client, tx_hash) .await .unwrap(); - EthApiClient::::transaction_by_block_number_and_index( + EthApiClient::::transaction_by_block_hash_and_index( + client, hash, index, + ) + .await + .unwrap(); + EthApiClient::::transaction_by_block_number_and_index( client, block_number, index, ) .await .unwrap(); - EthApiClient::::create_access_list( + EthApiClient::::create_access_list( client, call_request.clone(), Some(block_number.into()), ) .await .unwrap(); - EthApiClient::::estimate_gas( + EthApiClient::::estimate_gas( client, call_request.clone(), Some(block_number.into()), @@ -242,7 +268,7 @@ where ) .await .unwrap(); - EthApiClient::::call( + EthApiClient::::call( client, call_request.clone(), Some(block_number.into()), @@ -251,30 +277,38 @@ where ) .await .unwrap(); - EthApiClient::::syncing(client).await.unwrap(); - EthApiClient::::send_transaction(client, transaction_request) + EthApiClient::::syncing(client).await.unwrap(); + EthApiClient::::send_transaction(client, transaction_request) + .await + .unwrap_err(); + EthApiClient::::hashrate(client).await.unwrap(); + EthApiClient::::submit_hashrate( + client, + U256::default(), + B256::default(), + ) + .await + .unwrap(); + EthApiClient::::gas_price(client).await.unwrap_err(); + EthApiClient::::max_priority_fee_per_gas(client) .await .unwrap_err(); - EthApiClient::::hashrate(client).await.unwrap(); - EthApiClient::::submit_hashrate(client, U256::default(), B256::default()) + EthApiClient::::get_proof(client, address, vec![], None) .await .unwrap(); - EthApiClient::::gas_price(client).await.unwrap_err(); - EthApiClient::::max_priority_fee_per_gas(client).await.unwrap_err(); - EthApiClient::::get_proof(client, address, vec![], None).await.unwrap(); // Unimplemented assert!(is_unimplemented( - EthApiClient::::author(client).await.err().unwrap() + EthApiClient::::author(client).await.err().unwrap() )); assert!(is_unimplemented( - EthApiClient::::is_mining(client).await.err().unwrap() + EthApiClient::::is_mining(client).await.err().unwrap() )); assert!(is_unimplemented( - EthApiClient::::get_work(client).await.err().unwrap() + EthApiClient::::get_work(client).await.err().unwrap() )); assert!(is_unimplemented( - EthApiClient::::submit_work( + EthApiClient::::submit_work( client, B64::default(), B256::default(), @@ -285,7 +319,7 @@ where .unwrap() )); assert!(is_unimplemented( - EthApiClient::::sign_transaction(client, call_request.clone()) + EthApiClient::::sign_transaction(client, call_request.clone()) .await .err() .unwrap() diff --git a/crates/rpc/rpc-builder/tests/it/middleware.rs b/crates/rpc/rpc-builder/tests/it/middleware.rs index 6ef6c7f67797..e95ee07642c7 100644 --- a/crates/rpc/rpc-builder/tests/it/middleware.rs +++ b/crates/rpc/rpc-builder/tests/it/middleware.rs @@ -8,7 +8,7 @@ use reth_rpc::EthApi; use reth_rpc_builder::{RpcServerConfig, TransportRpcModuleConfig}; use reth_rpc_eth_api::EthApiClient; use reth_rpc_server_types::RpcModuleSelection; -use reth_rpc_types::{Block, Transaction}; +use reth_rpc_types::{Block, Receipt, Transaction}; use std::{ future::Future, pin::Pin, @@ -75,7 +75,7 @@ async fn test_rpc_middleware() { .unwrap(); let client = handle.http_client().unwrap(); - EthApiClient::::protocol_version(&client).await.unwrap(); + EthApiClient::::protocol_version(&client).await.unwrap(); let count = mylayer.count.load(Ordering::Relaxed); assert_eq!(count, 1); } diff --git a/crates/rpc/rpc-eth-api/src/core.rs b/crates/rpc/rpc-eth-api/src/core.rs index 409dc0076791..9a19307ce990 100644 --- a/crates/rpc/rpc-eth-api/src/core.rs +++ b/crates/rpc/rpc-eth-api/src/core.rs @@ -13,8 +13,8 @@ use reth_rpc_types::{ serde_helpers::JsonStorageKey, simulate::{SimBlock, SimulatedBlock}, state::{EvmOverrides, StateOverride}, - AnyTransactionReceipt, BlockOverrides, BlockTransactions, Bundle, EIP1186AccountProofResponse, - EthCallResponse, FeeHistory, Header, Index, StateContext, SyncStatus, TransactionRequest, Work, + BlockOverrides, BlockTransactions, Bundle, EIP1186AccountProofResponse, EthCallResponse, + FeeHistory, Header, Index, StateContext, SyncStatus, TransactionRequest, Work, }; use reth_transaction_pool::{PoolTransaction, TransactionPool}; use tracing::trace; @@ -23,19 +23,27 @@ use crate::{ helpers::{ EthApiSpec, EthBlocks, EthCall, EthFees, EthState, EthTransactions, FullEthApi, LoadState, }, - RpcBlock, RpcTransaction, + RpcBlock, RpcReceipt, RpcTransaction, }; /// Helper trait, unifies functionality that must be supported to implement all RPC methods for /// server. pub trait FullEthApiServer: - EthApiServer, RpcBlock> + FullEthApi + Clone + EthApiServer< + RpcTransaction, + RpcBlock, + RpcReceipt, + > + FullEthApi + + Clone { } impl FullEthApiServer for T where - T: EthApiServer, RpcBlock> - + FullEthApi + T: EthApiServer< + RpcTransaction, + RpcBlock, + RpcReceipt, + > + FullEthApi + Clone { } @@ -43,7 +51,7 @@ impl FullEthApiServer for T where /// Eth rpc interface: #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] -pub trait EthApi { +pub trait EthApi { /// Returns the protocol version encoded as a string. #[method(name = "protocolVersion")] async fn protocol_version(&self) -> RpcResult; @@ -100,10 +108,7 @@ pub trait EthApi { /// Returns all transaction receipts for a given block. #[method(name = "getBlockReceipts")] - async fn block_receipts( - &self, - block_id: BlockId, - ) -> RpcResult>>; + async fn block_receipts(&self, block_id: BlockId) -> RpcResult>>; /// Returns an uncle block of the given block and index. #[method(name = "getUncleByBlockHashAndIndex")] @@ -171,7 +176,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")] @@ -361,7 +366,12 @@ pub trait EthApi { } #[async_trait::async_trait] -impl EthApiServer, RpcBlock> for T +impl + EthApiServer< + RpcTransaction, + RpcBlock, + RpcReceipt, + > for T where T: FullEthApi, jsonrpsee_types::error::ErrorObject<'static>: From, @@ -457,7 +467,7 @@ where async fn block_receipts( &self, block_id: BlockId, - ) -> RpcResult>> { + ) -> RpcResult>>> { trace!(target: "rpc::eth", ?block_id, "Serving eth_getBlockReceipts"); Ok(EthBlocks::block_receipts(self, block_id).await?) } @@ -604,7 +614,10 @@ where } /// Handler for: `eth_getTransactionReceipt` - async fn transaction_receipt(&self, hash: B256) -> RpcResult> { + async fn transaction_receipt( + &self, + hash: B256, + ) -> RpcResult>> { trace!(target: "rpc::eth", ?hash, "Serving eth_getTransactionReceipt"); Ok(EthTransactions::transaction_receipt(self, hash).await?) } diff --git a/crates/rpc/rpc-eth-api/src/lib.rs b/crates/rpc/rpc-eth-api/src/lib.rs index 30623d6b8e2f..ec6490917c33 100644 --- a/crates/rpc/rpc-eth-api/src/lib.rs +++ b/crates/rpc/rpc-eth-api/src/lib.rs @@ -24,7 +24,7 @@ pub use core::{EthApiServer, FullEthApiServer}; pub use filter::EthFilterApiServer; pub use helpers::error::{AsEthApiError, FromEthApiError, FromEvmError, IntoEthApiError}; pub use pubsub::EthPubSubApiServer; -pub use types::{EthApiTypes, RpcBlock, RpcTransaction}; +pub use types::{EthApiTypes, RpcBlock, RpcReceipt, RpcTransaction}; #[cfg(feature = "client")] pub use bundle::{EthBundleApiClient, EthCallBundleApiClient}; diff --git a/crates/rpc/rpc-eth-api/src/types.rs b/crates/rpc/rpc-eth-api/src/types.rs index 55f137300761..e1d83095adaa 100644 --- a/crates/rpc/rpc-eth-api/src/types.rs +++ b/crates/rpc/rpc-eth-api/src/types.rs @@ -4,7 +4,7 @@ use std::error::Error; use alloy_network::{AnyNetwork, Network}; use reth_rpc_eth_types::EthApiError; -use reth_rpc_types::{Block, Transaction, WithOtherFields}; +use reth_rpc_types::{AnyTransactionReceipt, Block, Transaction, WithOtherFields}; use crate::{AsEthApiError, FromEthApiError, FromEvmError}; @@ -19,10 +19,12 @@ pub trait EthApiTypes: Send + Sync + Clone { + Send + Sync; /// Blockchain primitive types, specific to network, e.g. block and transaction. - // todo: remove restriction `reth_rpc_types::Transaction` + // todo: remove restriction [`reth_rpc_types::Transaction`] + // todo: remove restriction [`reth_rpc_types::AnyTransactionReceipt`] type NetworkTypes: Network< TransactionResponse = WithOtherFields, HeaderResponse = reth_rpc_types::Header, + ReceiptResponse = AnyTransactionReceipt, >; } @@ -36,3 +38,6 @@ pub type RpcTransaction = ::TransactionResponse; /// Adapter for network specific block type. pub type RpcBlock = Block, ::HeaderResponse>; + +/// Adapter for network specific receipt type. +pub type RpcReceipt = ::ReceiptResponse; diff --git a/crates/rpc/rpc-testing-util/src/debug.rs b/crates/rpc/rpc-testing-util/src/debug.rs index 5bb003dac8e7..9bbccaa2a43d 100644 --- a/crates/rpc/rpc-testing-util/src/debug.rs +++ b/crates/rpc/rpc-testing-util/src/debug.rs @@ -8,7 +8,7 @@ use std::{ use futures::{Stream, StreamExt}; use jsonrpsee::core::client::Error as RpcError; -use reth_primitives::{BlockId, TxHash, B256}; +use reth_primitives::{BlockId, Receipt, TxHash, B256}; use reth_rpc_api::{clients::DebugApiClient, EthApiClient}; use reth_rpc_types::{ trace::{ @@ -77,7 +77,7 @@ pub trait DebugApiExt { impl DebugApiExt for T where - T: EthApiClient + DebugApiClient + Sync, + T: EthApiClient + DebugApiClient + Sync, { type Provider = T; diff --git a/crates/rpc/rpc-testing-util/tests/it/trace.rs b/crates/rpc/rpc-testing-util/tests/it/trace.rs index 619c1ed43374..46d17e40b2ee 100644 --- a/crates/rpc/rpc-testing-util/tests/it/trace.rs +++ b/crates/rpc/rpc-testing-util/tests/it/trace.rs @@ -3,6 +3,7 @@ use std::{collections::HashSet, time::Instant}; use futures::StreamExt; use jsonrpsee::http_client::HttpClientBuilder; use jsonrpsee_http_client::HttpClient; +use reth_primitives::Receipt; use reth_rpc_api_testing_util::{debug::DebugApiExt, trace::TraceApiExt, utils::parse_env_url}; use reth_rpc_eth_api::EthApiClient; use reth_rpc_types::{ @@ -110,7 +111,7 @@ async fn debug_trace_block_entire_chain() { let client = HttpClientBuilder::default().build(url).unwrap(); let current_block: u64 = - >::block_number(&client) + >::block_number(&client) .await .unwrap() .try_into() diff --git a/crates/rpc/rpc/src/engine.rs b/crates/rpc/rpc/src/engine.rs index d21834213716..50bd33545b68 100644 --- a/crates/rpc/rpc/src/engine.rs +++ b/crates/rpc/rpc/src/engine.rs @@ -4,7 +4,7 @@ use reth_primitives::{Address, BlockId, BlockNumberOrTag, Bytes, B256, U256, U64 use reth_rpc_api::{EngineEthApiServer, EthApiServer, EthFilterApiServer}; /// Re-export for convenience pub use reth_rpc_engine_api::EngineApi; -use reth_rpc_eth_api::{EthApiTypes, RpcBlock, RpcTransaction}; +use reth_rpc_eth_api::{EthApiTypes, RpcBlock, RpcReceipt, RpcTransaction}; use reth_rpc_types::{ state::StateOverride, BlockOverrides, EIP1186AccountProofResponse, Filter, JsonStorageKey, Log, SyncStatus, TransactionRequest, WithOtherFields, @@ -36,8 +36,11 @@ impl EngineEthApi { impl EngineEthApiServer> for EngineEthApi where - Eth: EthApiServer, RpcBlock> - + EthApiTypes< + Eth: EthApiServer< + RpcTransaction, + RpcBlock, + RpcReceipt, + > + EthApiTypes< NetworkTypes: Network< TransactionResponse = WithOtherFields, >, diff --git a/crates/rpc/rpc/src/eth/core.rs b/crates/rpc/rpc/src/eth/core.rs index c79007fe3d4d..06e1be121374 100644 --- a/crates/rpc/rpc/src/eth/core.rs +++ b/crates/rpc/rpc/src/eth/core.rs @@ -490,7 +490,7 @@ mod tests { /// Invalid block range #[tokio::test] async fn test_fee_history_empty() { - let response = as EthApiServer<_, _>>::fee_history( + let response = as EthApiServer<_, _, _>>::fee_history( &build_test_eth_api(NoopProvider::default()), U64::from(1), BlockNumberOrTag::Latest, @@ -512,7 +512,7 @@ mod tests { let (eth_api, _, _) = prepare_eth_api(newest_block, oldest_block, block_count, MockEthProvider::default()); - let response = as EthApiServer<_, _>>::fee_history( + let response = as EthApiServer<_, _, _>>::fee_history( ð_api, U64::from(newest_block + 1), newest_block.into(), @@ -535,7 +535,7 @@ mod tests { let (eth_api, _, _) = prepare_eth_api(newest_block, oldest_block, block_count, MockEthProvider::default()); - let response = as EthApiServer<_, _>>::fee_history( + let response = as EthApiServer<_, _, _>>::fee_history( ð_api, U64::from(1), (newest_block + 1000).into(), @@ -558,7 +558,7 @@ mod tests { let (eth_api, _, _) = prepare_eth_api(newest_block, oldest_block, block_count, MockEthProvider::default()); - let response = as EthApiServer<_, _>>::fee_history( + let response = as EthApiServer<_, _, _>>::fee_history( ð_api, U64::from(0), newest_block.into(), diff --git a/crates/rpc/rpc/src/otterscan.rs b/crates/rpc/rpc/src/otterscan.rs index 06b95c2e1498..dd9a539563bf 100644 --- a/crates/rpc/rpc/src/otterscan.rs +++ b/crates/rpc/rpc/src/otterscan.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use jsonrpsee::{core::RpcResult, types::ErrorObjectOwned}; use reth_primitives::{Address, BlockNumberOrTag, TxHash, B256, U256}; use reth_rpc_api::{EthApiServer, OtterscanServer}; -use reth_rpc_eth_api::{helpers::TraceExt, EthApiTypes, RpcBlock, RpcTransaction}; +use reth_rpc_eth_api::{helpers::TraceExt, EthApiTypes, RpcBlock, RpcReceipt, RpcTransaction}; use reth_rpc_eth_types::{utils::binary_search, EthApiError}; use reth_rpc_server_types::result::internal_rpc_err; use reth_rpc_types::{ @@ -66,8 +66,11 @@ where #[async_trait] impl OtterscanServer for OtterscanApi where - Eth: EthApiServer, RpcBlock> - + EthApiTypes< + Eth: EthApiServer< + RpcTransaction, + RpcBlock, + RpcReceipt, + > + EthApiTypes< NetworkTypes: Network< TransactionResponse = WithOtherFields, >,