chore: phase out ethers rpc block types (#1463)

This commit is contained in:
Matthias Seitz
2023-02-20 16:02:33 +01:00
committed by GitHub
parent 2a9472e9c3
commit 50203a8f2a
22 changed files with 119 additions and 95 deletions

View File

@ -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.

View File

@ -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<Option<RichBlock>>;
async fn block_by_number(
&self,
number: BlockNumberOrTag,
full: bool,
) -> Result<Option<RichBlock>>;
/// 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<Option<U256>>;
async fn block_transaction_count_by_number(
&self,
number: BlockNumberOrTag,
) -> Result<Option<U256>>;
/// 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<U256>;
async fn block_uncles_count_by_number(&self, number: BlockNumberOrTag) -> Result<U256>;
/// 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<Option<RichBlock>>;
@ -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<Option<Transaction>>;

View File

@ -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,

View File

@ -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<C>(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<C>(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,

View File

@ -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<Client: HeaderProvider + BlockProvider + StateProvider> EngineApi<Client> {
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))
};

View File

@ -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;

View File

@ -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};

View File

@ -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<Client, Pool, Network> EthApi<Client, Pool, Network>
where
Client: BlockProvider + StateProviderFactory + 'static,
{
fn convert_block_number(&self, num: BlockNumber) -> Result<Option<u64>> {
fn convert_block_number(&self, num: BlockNumberOrTag) -> Result<Option<u64>> {
self.client().convert_block_number(num)
}
@ -119,17 +118,17 @@ where
block_id: BlockId,
) -> Result<Option<<Client as StateProviderFactory>::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<Option<<Client as StateProviderFactory>::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<Option<<Client as StateProviderFactory>::HistorySP<'_>>> {
self.state_at_block_number(BlockNumber::Latest)
self.state_at_block_number(BlockNumberOrTag::Latest)
}
}

View File

@ -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<Client, Pool, Network> EthApiServer for EthApi<Client, Pool, Network>
where
@ -62,7 +61,7 @@ where
async fn block_by_number(
&self,
_number: BlockNumber,
_number: BlockNumberOrTag,
_full: bool,
) -> Result<Option<RichBlock>> {
Err(internal_rpc_err("unimplemented"))
@ -74,7 +73,7 @@ where
async fn block_transaction_count_by_number(
&self,
_number: BlockNumber,
_number: BlockNumberOrTag,
) -> Result<Option<U256>> {
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<U256> {
async fn block_uncles_count_by_number(&self, _number: BlockNumberOrTag) -> Result<U256> {
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<Option<RichBlock>> {
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<Option<reth_rpc_types::Transaction>> {
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::<reth_primitives::BlockNumber, FeeHistoryCacheItem>::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);

View File

@ -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<Client, Pool, Network> EthApi<Client, Pool, Network>

View File

@ -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)?;
//

View File

@ -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::*},