Skip to content

Commit 50957d0

Browse files
authored
Merge pull request #3328 from Pana/op/blockNumberDeserialize
RPC type BlockNumber optimize
2 parents 01bd77f + 6a34a4c commit 50957d0

File tree

15 files changed

+116
-110
lines changed

15 files changed

+116
-110
lines changed

crates/rpc/rpc-eth-api/src/debug.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use alloy_rpc_types_trace::geth::{
22
GethDebugTracingCallOptions, GethDebugTracingOptions, GethTrace,
33
TraceResult,
44
};
5-
use cfx_rpc_eth_types::{BlockNumber, TransactionRequest};
5+
use cfx_rpc_eth_types::{BlockId, TransactionRequest};
66
use cfx_types::H256;
77
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
88

@@ -28,12 +28,12 @@ pub trait DebugApi {
2828

2929
#[method(name = "traceBlockByNumber")]
3030
async fn debug_trace_block_by_number(
31-
&self, block: BlockNumber, opts: Option<GethDebugTracingOptions>,
31+
&self, block: BlockId, opts: Option<GethDebugTracingOptions>,
3232
) -> RpcResult<Vec<TraceResult>>;
3333

3434
#[method(name = "traceCall")]
3535
async fn debug_trace_call(
36-
&self, request: TransactionRequest, block_number: Option<BlockNumber>,
36+
&self, request: TransactionRequest, block_number: Option<BlockId>,
3737
opts: Option<GethDebugTracingCallOptions>,
3838
) -> RpcResult<GethTrace>;
3939
}

crates/rpc/rpc-eth-api/src/eth.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use cfx_rpc_eth_types::{
2-
AccessListResult, AccountPendingTransactions, Block,
3-
BlockNumber as BlockId, BlockOverrides, Bundle, EthCallResponse,
4-
EthRpcLogFilter as Filter, FeeHistory, Header, Log, Receipt,
5-
RpcStateOverride, SimulatePayload, SimulatedBlock, StateContext,
6-
SyncStatus, Transaction, TransactionRequest,
2+
AccessListResult, AccountPendingTransactions, Block, BlockId,
3+
BlockOverrides, Bundle, EthCallResponse, EthRpcLogFilter as Filter,
4+
FeeHistory, Header, Log, Receipt, RpcStateOverride, SimulatePayload,
5+
SimulatedBlock, StateContext, SyncStatus, Transaction, TransactionRequest,
76
};
87
use cfx_rpc_primitives::{Bytes, Index};
98
use cfx_types::{Address, H256, H64, U256, U64};

crates/rpc/rpc-eth-api/src/parity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cfx_rpc_eth_types::{BlockNumber as BlockId, Receipt};
1+
use cfx_rpc_eth_types::{BlockId, Receipt};
22
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
33

44
#[rpc(server, namespace = "parity")]

crates/rpc/rpc-eth-api/src/trace.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cfx_rpc_eth_types::{
2-
BlockNumber, Index, LocalizedSetAuthTrace, LocalizedTrace, TraceFilter,
2+
BlockId, Index, LocalizedSetAuthTrace, LocalizedTrace, TraceFilter,
33
};
44
use cfx_types::H256;
55
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
@@ -9,13 +9,13 @@ pub trait TraceApi {
99
/// Returns all traces produced at the given block.
1010
#[method(name = "block")]
1111
async fn block_traces(
12-
&self, block_number: BlockNumber,
12+
&self, block_number: BlockId,
1313
) -> RpcResult<Option<Vec<LocalizedTrace>>>;
1414

1515
/// Returns all set auth traces produced at the given block.
1616
#[method(name = "blockSetAuth")]
1717
async fn block_set_auth_traces(
18-
&self, block_number: BlockNumber,
18+
&self, block_number: BlockId,
1919
) -> RpcResult<Option<Vec<LocalizedSetAuthTrace>>>;
2020

2121
/// Returns all traces matching the provided filter.

crates/rpc/rpc-eth-impl/src/debug.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use alloy_rpc_types_trace::geth::{
66
};
77
use async_trait::async_trait;
88
use cfx_rpc_eth_api::DebugApiServer;
9-
use cfx_rpc_eth_types::{BlockNumber, TransactionRequest};
9+
use cfx_rpc_eth_types::{BlockId, TransactionRequest};
1010
use cfx_rpc_utils::error::jsonrpsee_error_helpers::invalid_params_msg;
1111
use cfx_types::{AddressSpaceUtil, Space, H256, U256};
1212
use cfxcore::{
@@ -36,19 +36,15 @@ impl DebugApi {
3636

3737
pub fn consensus_graph(&self) -> &ConsensusGraph { &self.consensus }
3838

39-
pub fn get_block_epoch_num(
40-
&self, block: BlockNumber,
41-
) -> Result<u64, String> {
39+
pub fn get_block_epoch_num(&self, block: BlockId) -> Result<u64, String> {
4240
let num = match block {
43-
BlockNumber::Num(block_number) => block_number,
44-
BlockNumber::Latest
45-
| BlockNumber::Safe
46-
| BlockNumber::Finalized => {
41+
BlockId::Num(block_number) => block_number,
42+
BlockId::Latest | BlockId::Safe | BlockId::Finalized => {
4743
let epoch_num = block.try_into().expect("should success");
4844
self.consensus_graph()
4945
.get_height_from_epoch_number(epoch_num)?
5046
}
51-
BlockNumber::Hash {
47+
BlockId::Hash {
5248
hash,
5349
require_canonical,
5450
} => self
@@ -64,8 +60,7 @@ impl DebugApi {
6460
}
6561

6662
pub fn trace_call(
67-
&self, mut request: TransactionRequest,
68-
block_number: Option<BlockNumber>,
63+
&self, mut request: TransactionRequest, block_number: Option<BlockId>,
6964
opts: Option<GethDebugTracingCallOptions>,
7065
) -> Result<GethTrace, CoreError> {
7166
if request.from.is_none() {
@@ -265,7 +260,7 @@ impl DebugApiServer for DebugApi {
265260
}
266261

267262
async fn debug_trace_block_by_number(
268-
&self, block: BlockNumber, opts: Option<GethDebugTracingOptions>,
263+
&self, block: BlockId, opts: Option<GethDebugTracingOptions>,
269264
) -> RpcResult<Vec<TraceResult>> {
270265
let num = self
271266
.get_block_epoch_num(block)
@@ -275,7 +270,7 @@ impl DebugApiServer for DebugApi {
275270
}
276271

277272
async fn debug_trace_call(
278-
&self, request: TransactionRequest, block_number: Option<BlockNumber>,
273+
&self, request: TransactionRequest, block_number: Option<BlockId>,
279274
opts: Option<GethDebugTracingCallOptions>,
280275
) -> RpcResult<GethTrace> {
281276
self.trace_call(request, block_number, opts)

crates/rpc/rpc-eth-impl/src/eth.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use cfx_rpc_cfx_types::{
1111
use cfx_rpc_eth_api::EthApiServer;
1212
use cfx_rpc_eth_types::{
1313
AccessListResult, AccountOverride, AccountPendingTransactions, Block,
14-
BlockNumber as BlockId, BlockOverrides, Bundle, Error, EthCallResponse,
15-
EthRpcLogFilter, EthRpcLogFilter as Filter, EvmOverrides, FeeHistory,
16-
Header, Log, Receipt, RpcStateOverride, SimulatePayload, SimulatedBlock,
17-
StateContext, SyncInfo, SyncStatus, Transaction, TransactionRequest,
14+
BlockId, BlockOverrides, Bundle, Error, EthCallResponse, EthRpcLogFilter,
15+
EthRpcLogFilter as Filter, EvmOverrides, FeeHistory, Header, Log, Receipt,
16+
RpcStateOverride, SimulatePayload, SimulatedBlock, StateContext, SyncInfo,
17+
SyncStatus, Transaction, TransactionRequest,
1818
};
1919
use cfx_rpc_primitives::{Bytes, Index, U64 as HexU64};
2020
use cfx_rpc_utils::{

crates/rpc/rpc-eth-impl/src/filter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
};
1010
use cfx_rpc_eth_api::EthFilterApiServer;
1111
use cfx_rpc_eth_types::{
12-
BlockNumber, EthRpcLogFilter as Filter, FilterChanges, Log,
12+
BlockId, EthRpcLogFilter as Filter, FilterChanges, Log,
1313
};
1414
use cfx_rpc_utils::error::jsonrpsee_error_helpers::{
1515
invalid_request_msg, jsonrpc_error_to_error_object_owned,
@@ -52,7 +52,7 @@ impl EthFilterApiServer for EthFilterApi {
5252
let mut polls = self.inner.polls().lock();
5353
let epoch_number = self.inner.best_executed_epoch_number();
5454

55-
if filter.to_block == Some(BlockNumber::Pending) {
55+
if filter.to_block == Some(BlockId::Pending) {
5656
bail!(invalid_request_msg(
5757
"Filter logs from pending blocks is not supported"
5858
))

crates/rpc/rpc-eth-impl/src/parity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::EthApi;
22
use async_trait::async_trait;
33
use cfx_rpc_eth_api::ParityApiServer;
4-
use cfx_rpc_eth_types::{BlockNumber as BlockId, Receipt};
4+
use cfx_rpc_eth_types::{BlockId, Receipt};
55
use jsonrpsee::core::RpcResult;
66

77
pub struct ParityApi {

crates/rpc/rpc-eth-impl/src/trace.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use cfx_rpc_common_impl::trace::{
1010
use cfx_rpc_eth_api::TraceApiServer;
1111
use cfx_rpc_eth_types::{
1212
trace::{LocalizedSetAuthTrace, LocalizedTrace as EthLocalizedTrace},
13-
BlockNumber, Index, LocalizedTrace, TraceFilter,
13+
BlockId, Index, LocalizedTrace, TraceFilter,
1414
};
1515
use cfx_types::H256;
1616
use cfx_util_macros::unwrap_option_or_return_result_none as unwrap_or_return;
@@ -30,10 +30,10 @@ impl TraceApi {
3030
}
3131

3232
pub fn get_block(
33-
&self, block_number: BlockNumber,
33+
&self, block_number: BlockId,
3434
) -> CoreResult<Option<PhantomBlock>> {
3535
let phantom_block = match block_number {
36-
BlockNumber::Hash { hash, .. } => self
36+
BlockId::Hash { hash, .. } => self
3737
.trace_handler
3838
.consensus_graph()
3939
.get_phantom_block_by_hash(
@@ -56,7 +56,7 @@ impl TraceApi {
5656
}
5757

5858
pub fn block_traces(
59-
&self, block_number: BlockNumber,
59+
&self, block_number: BlockId,
6060
) -> CoreResult<Option<Vec<LocalizedTrace>>> {
6161
let phantom_block = self.get_block(block_number)?;
6262

@@ -86,7 +86,7 @@ impl TraceApi {
8686
}
8787

8888
pub fn block_set_auth_traces(
89-
&self, block_number: BlockNumber,
89+
&self, block_number: BlockId,
9090
) -> CoreResult<Option<Vec<LocalizedSetAuthTrace>>> {
9191
let phantom_block = self.get_block(block_number)?;
9292

@@ -202,7 +202,7 @@ impl TraceApi {
202202
#[async_trait::async_trait]
203203
impl TraceApiServer for TraceApi {
204204
async fn block_traces(
205-
&self, block_number: BlockNumber,
205+
&self, block_number: BlockId,
206206
) -> RpcResult<Option<Vec<LocalizedTrace>>> {
207207
self.block_traces(block_number).map_err(|err| err.into())
208208
}
@@ -220,7 +220,7 @@ impl TraceApiServer for TraceApi {
220220
}
221221

222222
async fn block_set_auth_traces(
223-
&self, block_number: BlockNumber,
223+
&self, block_number: BlockId,
224224
) -> RpcResult<Option<Vec<LocalizedSetAuthTrace>>> {
225225
self.block_set_auth_traces(block_number)
226226
.map_err(|err| err.into())

0 commit comments

Comments
 (0)