From 50203a8f2a60d2dc6437ee0f8c8fe5d064174b66 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 20 Feb 2023 16:02:33 +0100 Subject: [PATCH] chore: phase out ethers rpc block types (#1463) --- crates/net/network/src/eth_requests.rs | 6 +-- crates/primitives/src/block.rs | 40 +++++++++++++++++-- crates/primitives/src/chain/info.rs | 16 ++++---- crates/rpc/rpc-api/src/debug.rs | 2 +- crates/rpc/rpc-api/src/eth.rs | 21 ++++++---- crates/rpc/rpc-api/src/trace.rs | 2 +- crates/rpc/rpc-builder/tests/it/http.rs | 10 ++--- crates/rpc/rpc-engine-api/src/engine_api.rs | 3 +- crates/rpc/rpc/src/debug.rs | 2 +- crates/rpc/rpc/src/eth/api/block.rs | 2 +- crates/rpc/rpc/src/eth/api/mod.rs | 13 +++--- crates/rpc/rpc/src/eth/api/server.rs | 24 +++++------ crates/rpc/rpc/src/eth/api/state.rs | 2 +- crates/rpc/rpc/src/eth/filter.rs | 6 +-- crates/rpc/rpc/src/trace.rs | 2 +- crates/storage/provider/src/providers/mod.rs | 2 +- .../storage/provider/src/test_utils/mock.rs | 12 +++--- .../storage/provider/src/test_utils/noop.rs | 4 +- crates/storage/provider/src/traits/block.rs | 14 +++---- .../storage/provider/src/traits/block_id.rs | 27 ++++++------- .../provider/src/traits/transactions.rs | 2 +- .../provider/src/traits/withdrawals.rs | 2 +- 22 files changed, 119 insertions(+), 95 deletions(-) diff --git a/crates/net/network/src/eth_requests.rs b/crates/net/network/src/eth_requests.rs index f1f38b330..324419be4 100644 --- a/crates/net/network/src/eth_requests.rs +++ b/crates/net/network/src/eth_requests.rs @@ -7,7 +7,7 @@ use reth_eth_wire::{ GetReceipts, NodeData, Receipts, }; use reth_interfaces::p2p::error::RequestResult; -use reth_primitives::{rpc, BlockHashOrNumber, Header, HeadersDirection, PeerId, U256}; +use reth_primitives::{BlockHashOrNumber, Header, HeadersDirection, PeerId, U256}; use reth_provider::{BlockProvider, HeaderProvider}; use std::{ borrow::Borrow, @@ -160,9 +160,7 @@ where let mut total_bytes = APPROX_BODY_SIZE; for hash in request.0 { - if let Some(block) = - self.client.block(rpc::BlockId::Hash(rpc::H256(hash.0))).unwrap_or_default() - { + if let Some(block) = self.client.block_by_hash(hash).unwrap_or_default() { let body = BlockBody { transactions: block.body, ommers: block.ommers, diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index a7e700f1c..b453a56bb 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -1,4 +1,5 @@ use crate::{Header, SealedHeader, TransactionSigned, Withdrawal, H256}; +use ethers_core::types::BlockNumber; use reth_codecs::derive_arbitrary; use reth_rlp::{Decodable, DecodeError, Encodable, RlpDecodable, RlpEncodable}; use serde::{ @@ -361,9 +362,22 @@ impl BlockNumberOrTag { } } -impl> From for BlockNumberOrTag { - fn from(num: T) -> Self { - BlockNumberOrTag::Number(num.into()) +impl From for BlockNumberOrTag { + fn from(num: u64) -> Self { + BlockNumberOrTag::Number(num) + } +} + +impl From for BlockNumberOrTag { + fn from(value: BlockNumber) -> Self { + match value { + BlockNumber::Latest => BlockNumberOrTag::Latest, + BlockNumber::Finalized => BlockNumberOrTag::Finalized, + BlockNumber::Safe => BlockNumberOrTag::Safe, + BlockNumber::Earliest => BlockNumberOrTag::Earliest, + BlockNumber::Pending => BlockNumberOrTag::Pending, + BlockNumber::Number(num) => BlockNumberOrTag::Number(num.as_u64()), + } } } @@ -439,11 +453,31 @@ pub struct BlockHash { /// Whether the block must be a canonical block pub require_canonical: Option, } + impl BlockHash { pub fn from_hash(block_hash: H256, require_canonical: Option) -> Self { BlockHash { block_hash, require_canonical } } } + +impl From for BlockHash { + fn from(value: H256) -> Self { + Self::from_hash(value, None) + } +} + +impl From for H256 { + fn from(value: BlockHash) -> Self { + value.block_hash + } +} + +impl AsRef for BlockHash { + fn as_ref(&self) -> &H256 { + &self.block_hash + } +} + #[cfg(test)] mod test { use super::{BlockId, BlockNumberOrTag::*, *}; diff --git a/crates/primitives/src/chain/info.rs b/crates/primitives/src/chain/info.rs index c9603eea1..1d84ce324 100644 --- a/crates/primitives/src/chain/info.rs +++ b/crates/primitives/src/chain/info.rs @@ -1,4 +1,4 @@ -use crate::{rpc::BlockNumber as RpcBlockNumber, BlockNumber, H256}; +use crate::{BlockNumber, BlockNumberOrTag, H256}; /// Current status of the blockchain's head. #[derive(Default, Debug, Eq, PartialEq)] @@ -15,14 +15,14 @@ pub struct ChainInfo { impl ChainInfo { /// Attempts to convert a [BlockNumber](crate::rpc::BlockNumber) enum to a numeric value - pub fn convert_block_number(&self, number: RpcBlockNumber) -> Option { + pub fn convert_block_number(&self, number: BlockNumberOrTag) -> Option { match number { - RpcBlockNumber::Finalized => self.last_finalized, - RpcBlockNumber::Safe => self.safe_finalized, - RpcBlockNumber::Earliest => Some(0), - RpcBlockNumber::Number(num) => Some(num.as_u64()), - RpcBlockNumber::Pending => None, - RpcBlockNumber::Latest => Some(self.best_number), + BlockNumberOrTag::Finalized => self.last_finalized, + BlockNumberOrTag::Safe => self.safe_finalized, + BlockNumberOrTag::Earliest => Some(0), + BlockNumberOrTag::Number(num) => Some(num), + BlockNumberOrTag::Pending => None, + BlockNumberOrTag::Latest => Some(self.best_number), } } } diff --git a/crates/rpc/rpc-api/src/debug.rs b/crates/rpc/rpc-api/src/debug.rs index 77c4a952e..4a78a635a 100644 --- a/crates/rpc/rpc-api/src/debug.rs +++ b/crates/rpc/rpc-api/src/debug.rs @@ -1,5 +1,5 @@ use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc}; -use reth_primitives::{rpc::BlockId, Bytes, H256}; +use reth_primitives::{BlockId, Bytes, H256}; use reth_rpc_types::RichBlock; /// Debug rpc interface. diff --git a/crates/rpc/rpc-api/src/eth.rs b/crates/rpc/rpc-api/src/eth.rs index c0305f890..2b08e40f5 100644 --- a/crates/rpc/rpc-api/src/eth.rs +++ b/crates/rpc/rpc-api/src/eth.rs @@ -1,7 +1,7 @@ use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc}; use reth_primitives::{ - rpc::{transaction::eip2930::AccessListWithGasUsed, BlockId, BlockNumber}, - Address, Bytes, H256, H64, U256, U64, + rpc::transaction::eip2930::AccessListWithGasUsed, Address, BlockId, BlockNumberOrTag, Bytes, + H256, H64, U256, U64, }; use reth_rpc_types::{ CallRequest, EIP1186AccountProofResponse, FeeHistory, Index, RichBlock, SyncStatus, @@ -43,7 +43,11 @@ pub trait EthApi { /// Returns information about a block by number. #[method(name = "eth_getBlockByNumber")] - async fn block_by_number(&self, number: BlockNumber, full: bool) -> Result>; + async fn block_by_number( + &self, + number: BlockNumberOrTag, + full: bool, + ) -> Result>; /// Returns the number of transactions in a block from a block matching the given block hash. #[method(name = "eth_getBlockTransactionCountByHash")] @@ -51,7 +55,10 @@ pub trait EthApi { /// Returns the number of transactions in a block matching the given block number. #[method(name = "eth_getBlockTransactionCountByNumber")] - async fn block_transaction_count_by_number(&self, number: BlockNumber) -> Result>; + async fn block_transaction_count_by_number( + &self, + number: BlockNumberOrTag, + ) -> Result>; /// Returns the number of uncles in a block from a block matching the given block hash. #[method(name = "eth_getUncleCountByBlockHash")] @@ -59,7 +66,7 @@ pub trait EthApi { /// Returns the number of uncles in a block with given block number. #[method(name = "eth_getUncleCountByBlockNumber")] - async fn block_uncles_count_by_number(&self, number: BlockNumber) -> Result; + async fn block_uncles_count_by_number(&self, number: BlockNumberOrTag) -> Result; /// Returns an uncle block of the given block and index. #[method(name = "eth_getUncleByBlockHashAndIndex")] @@ -73,7 +80,7 @@ pub trait EthApi { #[method(name = "eth_getUncleByBlockNumberAndIndex")] async fn uncle_by_block_number_and_index( &self, - number: BlockNumber, + number: BlockNumberOrTag, index: Index, ) -> Result>; @@ -93,7 +100,7 @@ pub trait EthApi { #[method(name = "eth_getTransactionByBlockNumberAndIndex")] async fn transaction_by_block_number_and_index( &self, - number: BlockNumber, + number: BlockNumberOrTag, index: Index, ) -> Result>; diff --git a/crates/rpc/rpc-api/src/trace.rs b/crates/rpc/rpc-api/src/trace.rs index 0f062e920..7ff742f23 100644 --- a/crates/rpc/rpc-api/src/trace.rs +++ b/crates/rpc/rpc-api/src/trace.rs @@ -1,5 +1,5 @@ use jsonrpsee::{core::RpcResult as Result, proc_macros::rpc}; -use reth_primitives::{rpc::BlockId, Bytes, H256}; +use reth_primitives::{BlockId, Bytes, H256}; use reth_rpc_types::{ trace::{filter::TraceFilter, parity::*}, CallRequest, Index, diff --git a/crates/rpc/rpc-builder/tests/it/http.rs b/crates/rpc/rpc-builder/tests/it/http.rs index fb4eb8008..4d7e27b44 100644 --- a/crates/rpc/rpc-builder/tests/it/http.rs +++ b/crates/rpc/rpc-builder/tests/it/http.rs @@ -9,9 +9,7 @@ use jsonrpsee::{ types::error::{CallError, ErrorCode}, }; use reth_primitives::{ - hex_literal::hex, - rpc::{BlockId, BlockNumber}, - Address, Bytes, NodeRecord, H256, H64, U256, + hex_literal::hex, Address, BlockId, BlockNumberOrTag, Bytes, NodeRecord, H256, H64, U256, }; use reth_rpc_api::{ clients::{AdminApiClient, EthApiClient}, @@ -52,7 +50,7 @@ where let address = Address::default(); let index = Index::default(); let hash = H256::default(); - let block_number = BlockNumber::default(); + let block_number = BlockNumberOrTag::default(); let call_request = CallRequest::default(); let transaction_request = TransactionRequest::default(); let bytes = Bytes::default(); @@ -159,7 +157,7 @@ async fn test_basic_debug_calls(client: &C) where C: ClientT + SubscriptionClientT + Sync, { - let block_id = BlockId::Number(BlockNumber::default()); + let block_id = BlockId::Number(BlockNumberOrTag::default()); assert!(is_unimplemented(DebugApiClient::raw_header(client, block_id).await.err().unwrap())); assert!(is_unimplemented(DebugApiClient::raw_block(client, block_id).await.err().unwrap())); @@ -183,7 +181,7 @@ async fn test_basic_trace_calls(client: &C) where C: ClientT + SubscriptionClientT + Sync, { - let block_id = BlockId::Number(BlockNumber::default()); + let block_id = BlockId::Number(BlockNumberOrTag::default()); let trace_filter = TraceFilter { from_block: None, to_block: None, diff --git a/crates/rpc/rpc-engine-api/src/engine_api.rs b/crates/rpc/rpc-engine-api/src/engine_api.rs index 4367c20da..0b8dd4e8f 100644 --- a/crates/rpc/rpc-engine-api/src/engine_api.rs +++ b/crates/rpc/rpc-engine-api/src/engine_api.rs @@ -7,7 +7,6 @@ use reth_executor::{ use reth_interfaces::consensus::ForkchoiceState; use reth_primitives::{ proofs::{self, EMPTY_LIST_HASH}, - rpc::{BlockId, H256 as EthersH256}, ChainSpec, Hardfork, Header, SealedBlock, TransactionSigned, H64, U256, }; use reth_provider::{BlockProvider, HeaderProvider, StateProvider}; @@ -158,7 +157,7 @@ impl EngineApi { return Ok(PayloadStatus::new(PayloadStatusEnum::Valid, block_hash)) } - let Some(parent) = self.client.block(BlockId::Hash(EthersH256(parent_hash.0)))? else { + let Some(parent) = self.client.block_by_hash(parent_hash)? else { // TODO: cache block for storing later return Ok(PayloadStatus::from_status(PayloadStatusEnum::Syncing)) }; diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 3f120d81a..de3d11398 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -1,7 +1,7 @@ use crate::{result::internal_rpc_err, EthApiSpec}; use async_trait::async_trait; use jsonrpsee::core::RpcResult as Result; -use reth_primitives::{rpc::BlockId, Bytes, H256}; +use reth_primitives::{BlockId, Bytes, H256}; use reth_rpc_api::DebugApiServer; use reth_rpc_types::RichBlock; diff --git a/crates/rpc/rpc/src/eth/api/block.rs b/crates/rpc/rpc/src/eth/api/block.rs index ad1dd245e..2710ea5a5 100644 --- a/crates/rpc/rpc/src/eth/api/block.rs +++ b/crates/rpc/rpc/src/eth/api/block.rs @@ -4,7 +4,7 @@ use crate::{ eth::error::{EthApiError, EthResult}, EthApi, }; -use reth_primitives::{rpc::BlockId, H256}; +use reth_primitives::{BlockId, H256}; use reth_provider::{BlockProvider, StateProviderFactory}; use reth_rpc_types::{Block, RichBlock}; diff --git a/crates/rpc/rpc/src/eth/api/mod.rs b/crates/rpc/rpc/src/eth/api/mod.rs index 994ebf2d2..176d40ce3 100644 --- a/crates/rpc/rpc/src/eth/api/mod.rs +++ b/crates/rpc/rpc/src/eth/api/mod.rs @@ -8,8 +8,7 @@ use async_trait::async_trait; use reth_interfaces::Result; use reth_network_api::NetworkInfo; use reth_primitives::{ - rpc::{BlockId, BlockNumber}, - Address, ChainInfo, TransactionSigned, H256, U64, + Address, BlockId, BlockNumberOrTag, ChainInfo, TransactionSigned, H256, U64, }; use reth_provider::{BlockProvider, StateProviderFactory}; use std::num::NonZeroUsize; @@ -97,7 +96,7 @@ impl EthApi where Client: BlockProvider + StateProviderFactory + 'static, { - fn convert_block_number(&self, num: BlockNumber) -> Result> { + fn convert_block_number(&self, num: BlockNumberOrTag) -> Result> { self.client().convert_block_number(num) } @@ -119,17 +118,17 @@ where block_id: BlockId, ) -> Result::HistorySP<'_>>> { match block_id { - BlockId::Hash(hash) => self.state_at_hash(H256(hash.0)).map(Some), + BlockId::Hash(hash) => self.state_at_hash(hash.into()).map(Some), BlockId::Number(num) => self.state_at_block_number(num), } } - /// Returns the state at the given [BlockNumber] enum + /// Returns the state at the given [BlockNumberOrTag] enum /// /// Returns `None` if no state available. pub(crate) fn state_at_block_number( &self, - num: BlockNumber, + num: BlockNumberOrTag, ) -> Result::HistorySP<'_>>> { if let Some(number) = self.convert_block_number(num)? { self.state_at_number(number).map(Some) @@ -158,7 +157,7 @@ where pub(crate) fn latest_state( &self, ) -> Result::HistorySP<'_>>> { - self.state_at_block_number(BlockNumber::Latest) + self.state_at_block_number(BlockNumberOrTag::Latest) } } diff --git a/crates/rpc/rpc/src/eth/api/server.rs b/crates/rpc/rpc/src/eth/api/server.rs index 0422a957e..0b35ddbe7 100644 --- a/crates/rpc/rpc/src/eth/api/server.rs +++ b/crates/rpc/rpc/src/eth/api/server.rs @@ -1,14 +1,15 @@ //! Implementation of the [`jsonrpsee`] generated [`reth_rpc_api::EthApiServer`] trait //! Handles RPC requests for the `eth_` namespace. +use super::EthApiSpec; use crate::{ eth::{api::EthApi, error::EthApiError}, result::{internal_rpc_err, ToRpcResult}, }; use jsonrpsee::core::RpcResult as Result; use reth_primitives::{ - rpc::{transaction::eip2930::AccessListWithGasUsed, BlockId, BlockNumber}, - Address, Bytes, Header, H256, H64, U256, U64, + rpc::transaction::eip2930::AccessListWithGasUsed, Address, BlockId, BlockNumberOrTag, Bytes, + Header, H256, H64, U256, U64, }; use reth_provider::{BlockProvider, HeaderProvider, StateProviderFactory}; use reth_rpc_api::EthApiServer; @@ -20,8 +21,6 @@ use reth_transaction_pool::TransactionPool; use serde_json::Value; use std::collections::BTreeMap; -use super::EthApiSpec; - #[async_trait::async_trait] impl EthApiServer for EthApi where @@ -62,7 +61,7 @@ where async fn block_by_number( &self, - _number: BlockNumber, + _number: BlockNumberOrTag, _full: bool, ) -> Result> { Err(internal_rpc_err("unimplemented")) @@ -74,7 +73,7 @@ where async fn block_transaction_count_by_number( &self, - _number: BlockNumber, + _number: BlockNumberOrTag, ) -> Result> { Err(internal_rpc_err("unimplemented")) } @@ -83,7 +82,7 @@ where Err(internal_rpc_err("unimplemented")) } - async fn block_uncles_count_by_number(&self, _number: BlockNumber) -> Result { + async fn block_uncles_count_by_number(&self, _number: BlockNumberOrTag) -> Result { Err(internal_rpc_err("unimplemented")) } @@ -97,7 +96,7 @@ where async fn uncle_by_block_number_and_index( &self, - _number: BlockNumber, + _number: BlockNumberOrTag, _index: Index, ) -> Result> { Err(internal_rpc_err("unimplemented")) @@ -120,7 +119,7 @@ where async fn transaction_by_block_number_and_index( &self, - _number: BlockNumber, + _number: BlockNumberOrTag, _index: Index, ) -> Result> { Err(internal_rpc_err("unimplemented")) @@ -212,8 +211,7 @@ where // Sorted map that's populated in two rounds: // 1. Cache entries until first non-cached block // 2. Database query from the first non-cached block - let mut fee_history_cache_items = - BTreeMap::::new(); + let mut fee_history_cache_items = BTreeMap::new(); let mut first_non_cached_block = None; let mut last_non_cached_block = None; @@ -347,7 +345,7 @@ mod tests { }; use rand::random; use reth_network_api::test_utils::NoopNetwork; - use reth_primitives::{rpc::BlockNumber, Block, Header, H256, U256}; + use reth_primitives::{Block, BlockNumberOrTag, Header, H256, U256}; use reth_provider::test_utils::{MockEthProvider, NoopProvider}; use reth_rpc_api::EthApiServer; use reth_transaction_pool::test_utils::testing_pool; @@ -358,7 +356,7 @@ mod tests { async fn test_fee_history() { let eth_api = EthApi::new(NoopProvider::default(), testing_pool(), NoopNetwork::default()); - let response = eth_api.fee_history(1.into(), BlockNumber::Latest.into(), None).await; + let response = eth_api.fee_history(1.into(), BlockNumberOrTag::Latest.into(), None).await; assert!(matches!(response, RpcResult::Err(RpcError::Call(CallError::Custom(_))))); let Err(RpcError::Call(CallError::Custom(error_object))) = response else { unreachable!() }; assert_eq!(error_object.code(), INVALID_PARAMS_CODE); diff --git a/crates/rpc/rpc/src/eth/api/state.rs b/crates/rpc/rpc/src/eth/api/state.rs index 39d14ef1e..9c398af87 100644 --- a/crates/rpc/rpc/src/eth/api/state.rs +++ b/crates/rpc/rpc/src/eth/api/state.rs @@ -4,7 +4,7 @@ use crate::{ eth::error::{EthApiError, EthResult}, EthApi, }; -use reth_primitives::{rpc::BlockId, Address, Bytes, H256, U256}; +use reth_primitives::{Address, BlockId, Bytes, H256, U256}; use reth_provider::{BlockProvider, StateProvider, StateProviderFactory}; impl EthApi diff --git a/crates/rpc/rpc/src/eth/filter.rs b/crates/rpc/rpc/src/eth/filter.rs index acc816fdf..d510d6299 100644 --- a/crates/rpc/rpc/src/eth/filter.rs +++ b/crates/rpc/rpc/src/eth/filter.rs @@ -108,14 +108,14 @@ where FilterBlockOption::Range { from_block, to_block } => { // from block is maximum of block from last poll or `from_block` of filter if let Some(filter_from_block) = - from_block.and_then(|num| info.convert_block_number(num)) + from_block.and_then(|num| info.convert_block_number(num.into())) { from_block_number = start_block.max(filter_from_block) } // to block is max the best number if let Some(filter_to_block) = - to_block.and_then(|num| info.convert_block_number(num)) + to_block.and_then(|num| info.convert_block_number(num.into())) { to_block_number = filter_to_block; if to_block_number > best_number { @@ -220,7 +220,7 @@ where // while block_number <= to_block { // let _block = self // .client - // .block_by_number(BlockNumber::Number(block_number.into())) + // .block_by_number(BlockNumberOrTag::Number(block_number.into())) // .to_rpc_result()? // .ok_or(EthApiError::UnknownBlockNumber)?; // diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index 7f567ac43..962129fbd 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -1,7 +1,7 @@ use crate::result::internal_rpc_err; use async_trait::async_trait; use jsonrpsee::core::RpcResult as Result; -use reth_primitives::{rpc::BlockId, Bytes, H256}; +use reth_primitives::{BlockId, Bytes, H256}; use reth_rpc_api::TraceApiServer; use reth_rpc_types::{ trace::{filter::TraceFilter, parity::*}, diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index 580502dd7..8b8b1b2b8 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -10,7 +10,7 @@ use reth_db::{ }; use reth_interfaces::Result; use reth_primitives::{ - rpc::BlockId, Block, BlockHash, BlockNumber, ChainInfo, ChainSpec, Hardfork, Header, + Block, BlockHash, BlockId, BlockNumber, ChainInfo, ChainSpec, Hardfork, Header, TransactionSigned, TxHash, TxNumber, Withdrawal, H256, U256, }; use std::{ops::RangeBounds, sync::Arc}; diff --git a/crates/storage/provider/src/test_utils/mock.rs b/crates/storage/provider/src/test_utils/mock.rs index 279411558..d609de6b3 100644 --- a/crates/storage/provider/src/test_utils/mock.rs +++ b/crates/storage/provider/src/test_utils/mock.rs @@ -5,10 +5,8 @@ use crate::{ use parking_lot::Mutex; use reth_interfaces::Result; use reth_primitives::{ - keccak256, - rpc::{BlockId, BlockNumber}, - Account, Address, Block, BlockHash, Bytes, ChainInfo, Header, StorageKey, StorageValue, - TransactionSigned, TxHash, H256, U256, + keccak256, Account, Address, Block, BlockHash, BlockId, BlockNumberOrTag, Bytes, ChainInfo, + Header, StorageKey, StorageValue, TransactionSigned, TxHash, H256, U256, }; use std::{collections::HashMap, ops::RangeBounds, sync::Arc}; @@ -191,9 +189,9 @@ impl BlockProvider for MockEthProvider { fn block(&self, id: BlockId) -> Result> { let lock = self.blocks.lock(); match id { - BlockId::Hash(hash) => Ok(lock.get(&H256(hash.0)).cloned()), - BlockId::Number(BlockNumber::Number(num)) => { - Ok(lock.values().find(|b| b.number == num.as_u64()).cloned()) + BlockId::Hash(hash) => Ok(lock.get(hash.as_ref()).cloned()), + BlockId::Number(BlockNumberOrTag::Number(num)) => { + Ok(lock.values().find(|b| b.number == num).cloned()) } _ => { unreachable!("unused in network tests") diff --git a/crates/storage/provider/src/test_utils/noop.rs b/crates/storage/provider/src/test_utils/noop.rs index eadd7e5a5..67900d202 100644 --- a/crates/storage/provider/src/test_utils/noop.rs +++ b/crates/storage/provider/src/test_utils/noop.rs @@ -4,8 +4,8 @@ use crate::{ }; use reth_interfaces::Result; use reth_primitives::{ - rpc::BlockId, Account, Address, Block, BlockHash, BlockNumber, Bytes, ChainInfo, Header, - StorageKey, StorageValue, TransactionSigned, TxHash, TxNumber, H256, U256, + Account, Address, Block, BlockHash, BlockId, BlockNumber, Bytes, ChainInfo, Header, StorageKey, + StorageValue, TransactionSigned, TxHash, TxNumber, H256, U256, }; use std::ops::RangeBounds; diff --git a/crates/storage/provider/src/traits/block.rs b/crates/storage/provider/src/traits/block.rs index c7267d429..7d289e2e9 100644 --- a/crates/storage/provider/src/traits/block.rs +++ b/crates/storage/provider/src/traits/block.rs @@ -1,9 +1,6 @@ use crate::{BlockIdProvider, HeaderProvider, TransactionsProvider}; use reth_interfaces::Result; -use reth_primitives::{ - rpc::{BlockId, BlockNumber}, - Block, H256, -}; +use reth_primitives::{Block, BlockId, BlockNumberOrTag, H256}; /// Api trait for fetching `Block` related data. #[auto_impl::auto_impl(&, Arc)] @@ -15,17 +12,16 @@ pub trait BlockProvider: /// Returns the block. Returns `None` if block is not found. fn block_by_hash(&self, hash: H256) -> Result> { - // TODO: replace with ruint - self.block(BlockId::Hash(reth_primitives::rpc::H256::from(hash.0))) + self.block(hash.into()) } /// Returns the block. Returns `None` if block is not found. - fn block_by_number_or_tag(&self, num: BlockNumber) -> Result> { - self.block(BlockId::Number(num)) + fn block_by_number_or_tag(&self, num: BlockNumberOrTag) -> Result> { + self.block(num.into()) } /// Returns the block. Returns `None` if block is not found. fn block_by_number(&self, num: u64) -> Result> { - self.block(BlockId::Number(BlockNumber::Number(num.into()))) + self.block(num.into()) } } diff --git a/crates/storage/provider/src/traits/block_id.rs b/crates/storage/provider/src/traits/block_id.rs index 5189662e2..fb393f35d 100644 --- a/crates/storage/provider/src/traits/block_id.rs +++ b/crates/storage/provider/src/traits/block_id.rs @@ -1,9 +1,6 @@ use super::BlockHashProvider; use reth_interfaces::Result; -use reth_primitives::{ - rpc::{BlockId, BlockNumber}, - ChainInfo, H256, U256, -}; +use reth_primitives::{BlockId, BlockNumberOrTag, ChainInfo, H256, U256}; /// Client trait for transforming [BlockId]. #[auto_impl::auto_impl(&, Arc)] @@ -11,18 +8,18 @@ pub trait BlockIdProvider: BlockHashProvider + Send + Sync { /// Returns the current info for the chain. fn chain_info(&self) -> Result; - /// Converts the `BlockNumber` variants. + /// Converts the `BlockNumberOrTag` variants. fn convert_block_number( &self, - num: BlockNumber, + num: BlockNumberOrTag, ) -> Result> { let num = match num { - BlockNumber::Latest => self.chain_info()?.best_number, - BlockNumber::Earliest => 0, - BlockNumber::Pending => return Ok(None), - BlockNumber::Number(num) => num.as_u64(), - BlockNumber::Finalized => return Ok(self.chain_info()?.last_finalized), - BlockNumber::Safe => return Ok(self.chain_info()?.safe_finalized), + BlockNumberOrTag::Latest => self.chain_info()?.best_number, + BlockNumberOrTag::Earliest => 0, + BlockNumberOrTag::Pending => return Ok(None), + BlockNumberOrTag::Number(num) => num, + BlockNumberOrTag::Finalized => return Ok(self.chain_info()?.last_finalized), + BlockNumberOrTag::Safe => return Ok(self.chain_info()?.safe_finalized), }; Ok(Some(num)) } @@ -30,9 +27,9 @@ pub trait BlockIdProvider: BlockHashProvider + Send + Sync { /// Get the hash of the block by matching the given id. fn block_hash_for_id(&self, block_id: BlockId) -> Result> { match block_id { - BlockId::Hash(hash) => Ok(Some(H256(hash.0))), + BlockId::Hash(hash) => Ok(Some(hash.into())), BlockId::Number(num) => { - if matches!(num, BlockNumber::Latest) { + if matches!(num, BlockNumberOrTag::Latest) { return Ok(Some(self.chain_info()?.best_hash)) } self.convert_block_number(num)? @@ -49,7 +46,7 @@ pub trait BlockIdProvider: BlockHashProvider + Send + Sync { block_id: BlockId, ) -> Result> { match block_id { - BlockId::Hash(hash) => self.block_number(H256(hash.0)), + BlockId::Hash(hash) => self.block_number(hash.into()), BlockId::Number(num) => self.convert_block_number(num), } } diff --git a/crates/storage/provider/src/traits/transactions.rs b/crates/storage/provider/src/traits/transactions.rs index 9a27b77a6..7919b9ca2 100644 --- a/crates/storage/provider/src/traits/transactions.rs +++ b/crates/storage/provider/src/traits/transactions.rs @@ -1,6 +1,6 @@ use crate::BlockIdProvider; use reth_interfaces::Result; -use reth_primitives::{rpc::BlockId, BlockNumber, TransactionSigned, TxHash, TxNumber}; +use reth_primitives::{BlockId, BlockNumber, TransactionSigned, TxHash, TxNumber}; use std::ops::RangeBounds; /// Client trait for fetching [TransactionSigned] related data. diff --git a/crates/storage/provider/src/traits/withdrawals.rs b/crates/storage/provider/src/traits/withdrawals.rs index f6c1f1ce1..2f37e3bc2 100644 --- a/crates/storage/provider/src/traits/withdrawals.rs +++ b/crates/storage/provider/src/traits/withdrawals.rs @@ -1,5 +1,5 @@ use reth_interfaces::Result; -use reth_primitives::{rpc::BlockId, Withdrawal}; +use reth_primitives::{BlockId, Withdrawal}; /// Client trait for fetching [Withdrawal] related data. pub trait WithdrawalsProvider: Send + Sync {