Skip to content

Commit 690fea1

Browse files
authored
Merge pull request #3295 from Pana/rpc/eth_getBlockByNumber
op: eth_getBlockByNumber return null when parameter is bigger than latest block
2 parents 8d087cf + af20ec8 commit 690fea1

File tree

23 files changed

+156
-39
lines changed

23 files changed

+156
-39
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ members = [
111111
"crates/config",
112112
"crates/cfxcore/types",
113113
"crates/cfxcore/pow",
114+
"crates/cfxcore/errors",
114115
]
115116

116117
resolver = "2"
@@ -185,6 +186,7 @@ network = { path = "./crates/network" }
185186
cfxcore = { path = "./crates/cfxcore/core" }
186187
cfxcore-types = { path = "./crates/cfxcore/types" }
187188
cfxcore-pow = { path = "./crates/cfxcore/pow" }
189+
cfxcore-errors = { path = "./crates/cfxcore/errors" }
188190
cfx-parameters = { path = "./crates/parameters" }
189191
cfx-execute-helper = { path = "./crates/execution/execute-helper" }
190192
cfx-executor = { path = "./crates/execution/executor" }

changelogs/JSONRPC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Misc changes:
1818
4. These trace methods now support `SelfDestruct(Suicide)` trace, to access historical selfdestruct transaction data, a resync of the data is required.
1919
4. eSpace now support geth style `txpool` namespace methods, including: `txpool_status`, `txpool_inspect`, `txpool_content`, `txpool_contentFrom`
2020
5. `eth_call`, `eth_estimateGas` add support for `stateoverride` feature.
21+
6. `eth_getBlockByNumber` will return `null` when block number is bigger than latest block, other than return error message.
2122

2223
## v2.4.1
2324

crates/cfxcore/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ cfx-rpc-utils = { workspace = true }
116116
cfx-util-macros = { workspace = true }
117117
cfxcore-types = { workspace = true }
118118
cfxcore-pow = { workspace = true }
119+
cfxcore-errors = { workspace = true}
119120

120121
[dev-dependencies]
121122
cfx-storage = { workspace = true, features = ["testonly_code"] }

crates/cfxcore/core/src/consensus/consensus_graph/onchain_blocks_provider.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::ConsensusGraph;
22

33
use crate::errors::{invalid_params, Result as CoreResult};
4+
use cfxcore_errors::ProviderBlockError;
45

56
use cfx_parameters::consensus::*;
67

@@ -23,7 +24,7 @@ impl ConsensusGraph {
2324
/// Convert EpochNumber to height based on the current ConsensusGraph
2425
pub fn get_height_from_epoch_number(
2526
&self, epoch_number: EpochNumber,
26-
) -> Result<u64, String> {
27+
) -> Result<u64, ProviderBlockError> {
2728
Ok(match epoch_number {
2829
EpochNumber::Earliest => 0,
2930
EpochNumber::LatestCheckpoint => {
@@ -40,7 +41,7 @@ impl ConsensusGraph {
4041
EpochNumber::Number(num) => {
4142
let epoch_num = num;
4243
if epoch_num > self.inner.read_recursive().best_epoch_number() {
43-
return Err("Invalid params: expected a numbers with less than largest epoch number.".to_owned());
44+
return Err(ProviderBlockError::EpochNumberTooLarge);
4445
}
4546
epoch_num
4647
}
@@ -61,7 +62,7 @@ impl ConsensusGraph {
6162

6263
pub fn get_block_hashes_by_epoch(
6364
&self, epoch_number: EpochNumber,
64-
) -> Result<Vec<H256>, String> {
65+
) -> Result<Vec<H256>, ProviderBlockError> {
6566
self.get_height_from_epoch_number(epoch_number)
6667
.and_then(|height| {
6768
self.inner.read_recursive().block_hashes_by_epoch(height)
@@ -70,7 +71,7 @@ impl ConsensusGraph {
7071

7172
pub fn get_hash_from_epoch_number(
7273
&self, epoch_number: EpochNumber,
73-
) -> Result<H256, String> {
74+
) -> Result<H256, ProviderBlockError> {
7475
self.get_height_from_epoch_number(epoch_number)
7576
.and_then(|height| {
7677
self.inner.read().get_pivot_hash_from_epoch_number(height)
@@ -79,7 +80,7 @@ impl ConsensusGraph {
7980

8081
pub fn get_skipped_block_hashes_by_epoch(
8182
&self, epoch_number: EpochNumber,
82-
) -> Result<Vec<H256>, String> {
83+
) -> Result<Vec<H256>, ProviderBlockError> {
8384
self.get_height_from_epoch_number(epoch_number)
8485
.and_then(|height| {
8586
self.inner

crates/cfxcore/core/src/consensus/consensus_graph/rpc_api/phantom_block_provider.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use cfx_execute_helper::{
44
};
55
use cfx_rpc_cfx_types::PhantomBlock;
66
use cfx_types::{Bloom, Space, H256, U256};
7+
use cfxcore_errors::ProviderBlockError;
78
use primitives::{receipt::Receipt, EpochNumber, TransactionStatus};
89
use std::sync::Arc;
910

@@ -12,7 +13,7 @@ use super::super::ConsensusGraph;
1213
impl ConsensusGraph {
1314
pub fn get_phantom_block_bloom_filter(
1415
&self, block_num: EpochNumber, pivot_assumption: H256,
15-
) -> Result<Option<Bloom>, String> {
16+
) -> Result<Option<Bloom>, ProviderBlockError> {
1617
let hashes = self.get_block_hashes_by_epoch(block_num)?;
1718

1819
// sanity check: epoch is not empty
@@ -66,7 +67,7 @@ impl ConsensusGraph {
6667
pub fn get_phantom_block_pivot_by_number(
6768
&self, block_num: EpochNumber, pivot_assumption: Option<H256>,
6869
include_traces: bool,
69-
) -> Result<Option<PhantomBlock>, String> {
70+
) -> Result<Option<PhantomBlock>, ProviderBlockError> {
7071
self.get_phantom_block_by_number_inner(
7172
block_num,
7273
pivot_assumption,
@@ -78,7 +79,7 @@ impl ConsensusGraph {
7879
pub fn get_phantom_block_by_number(
7980
&self, block_num: EpochNumber, pivot_assumption: Option<H256>,
8081
include_traces: bool,
81-
) -> Result<Option<PhantomBlock>, String> {
82+
) -> Result<Option<PhantomBlock>, ProviderBlockError> {
8283
self.get_phantom_block_by_number_inner(
8384
block_num,
8485
pivot_assumption,
@@ -90,7 +91,7 @@ impl ConsensusGraph {
9091
fn get_phantom_block_by_number_inner(
9192
&self, block_num: EpochNumber, pivot_assumption: Option<H256>,
9293
include_traces: bool, only_pivot: bool,
93-
) -> Result<Option<PhantomBlock>, String> {
94+
) -> Result<Option<PhantomBlock>, ProviderBlockError> {
9495
let hashes = self.get_block_hashes_by_epoch(block_num)?;
9596

9697
// special handling for genesis block
@@ -297,7 +298,7 @@ impl ConsensusGraph {
297298

298299
pub fn get_phantom_block_by_hash(
299300
&self, hash: &H256, include_traces: bool,
300-
) -> Result<Option<PhantomBlock>, String> {
301+
) -> Result<Option<PhantomBlock>, ProviderBlockError> {
301302
let epoch_num = match self.get_block_epoch_number(hash) {
302303
None => return Ok(None),
303304
Some(n) => n,

crates/cfxcore/core/src/consensus/consensus_inner/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod blame_verifier;
66
pub mod confirmation_meter;
77
pub mod consensus_executor;
88
pub mod consensus_new_block_handler;
9+
use cfxcore_errors::ProviderBlockError;
910
use cfxcore_pow as pow;
1011

1112
use pow::{PowComputer, ProofOfWorkConfig};
@@ -2131,7 +2132,7 @@ impl ConsensusGraphInner {
21312132
/// out of the current era.
21322133
pub fn get_pivot_hash_from_epoch_number(
21332134
&self, epoch_number: u64,
2134-
) -> Result<EpochId, String> {
2135+
) -> Result<EpochId, ProviderBlockError> {
21352136
let height = epoch_number;
21362137
if height >= self.cur_era_genesis_height {
21372138
let pivot_index = (height - self.cur_era_genesis_height) as usize;
@@ -2161,7 +2162,7 @@ impl ConsensusGraphInner {
21612162

21622163
pub fn block_hashes_by_epoch(
21632164
&self, epoch_number: u64,
2164-
) -> Result<Vec<H256>, String> {
2165+
) -> Result<Vec<H256>, ProviderBlockError> {
21652166
debug!(
21662167
"block_hashes_by_epoch epoch_number={:?} pivot_chain.len={:?}",
21672168
epoch_number,
@@ -2199,7 +2200,7 @@ impl ConsensusGraphInner {
21992200

22002201
pub fn skipped_block_hashes_by_epoch(
22012202
&self, epoch_number: u64,
2202-
) -> Result<Vec<H256>, String> {
2203+
) -> Result<Vec<H256>, ProviderBlockError> {
22032204
debug!(
22042205
"skipped_block_hashes_by_epoch epoch_number={:?} pivot_chain.len={:?}",
22052206
epoch_number,
@@ -2351,11 +2352,11 @@ impl ConsensusGraphInner {
23512352

23522353
pub fn check_block_pivot_assumption(
23532354
&self, pivot_hash: &H256, epoch: u64,
2354-
) -> Result<(), String> {
2355+
) -> Result<(), ProviderBlockError> {
23552356
let last_number = self.best_epoch_number();
23562357
let hash = self.get_pivot_hash_from_epoch_number(epoch)?;
23572358
if epoch > last_number || hash != *pivot_hash {
2358-
return Err("Error: pivot chain assumption failed".to_owned());
2359+
return Err("Error: pivot chain assumption failed".into());
23592360
}
23602361
Ok(())
23612362
}
@@ -4143,6 +4144,7 @@ impl StateMaintenanceTrait for ConsensusGraphInner {
41434144
self,
41444145
epoch_number,
41454146
)
4147+
.map_err(|e| e.to_string())
41464148
}
41474149

41484150
fn get_epoch_execution_commitment_with_db(

crates/cfxcore/core/src/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use cfx_rpc_eth_types::Error as EthRpcError;
66
pub use cfx_rpc_utils::error::error_codes::EXCEPTION_ERROR;
77
use cfx_statedb::Error as StateDbError;
88
use cfx_storage::Error as StorageError;
9+
use cfxcore_errors::ProviderBlockError;
910
use jsonrpc_core::{futures::future, Error as JsonRpcError, ErrorCode};
1011
use jsonrpsee::types::ErrorObjectOwned;
1112
use primitives::{account::AccountError, filter::FilterError};
@@ -90,6 +91,10 @@ impl From<String> for Error {
9091
fn from(s: String) -> Error { Error::Msg(s) }
9192
}
9293

94+
impl From<ProviderBlockError> for Error {
95+
fn from(e: ProviderBlockError) -> Error { Error::from(e.to_string()) }
96+
}
97+
9398
impl From<EthRpcError> for Error {
9499
fn from(e: EthRpcError) -> Error {
95100
let e: JsonRpcError = e.into();

crates/cfxcore/core/src/light_protocol/common/ledger_info.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ impl LedgerInfo {
6464
#[inline]
6565
fn pivot_hash_of(&self, height: u64) -> Result<H256, Error> {
6666
let epoch = EpochNumber::Number(height);
67-
Ok(self.consensus.get_hash_from_epoch_number(epoch)?)
67+
Ok(self
68+
.consensus
69+
.get_hash_from_epoch_number(epoch)
70+
.map_err(|e| e.to_string())?)
6871
}
6972

7073
/// Get header at `height` on the pivot chain, if it exists.
@@ -79,7 +82,10 @@ impl LedgerInfo {
7982
#[inline]
8083
pub fn block_hashes_in(&self, height: u64) -> Result<Vec<H256>, Error> {
8184
let epoch = EpochNumber::Number(height);
82-
Ok(self.consensus.get_block_hashes_by_epoch(epoch)?)
85+
Ok(self
86+
.consensus
87+
.get_block_hashes_by_epoch(epoch)
88+
.map_err(|e| e.to_string())?)
8389
}
8490

8591
/// Get the correct deferred state root of the block at `height` on the

crates/cfxcore/core/src/light_protocol/query_service.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,10 @@ impl QueryService {
279279
break;
280280
}
281281

282-
let mut epoch_hashes = inner.read().block_hashes_by_epoch(epoch)?;
282+
let mut epoch_hashes = inner
283+
.read()
284+
.block_hashes_by_epoch(epoch)
285+
.map_err(|e| e.to_string())?;
283286
epoch_hashes.reverse();
284287

285288
let missing = GAS_PRICE_BLOCK_SAMPLE_SIZE - hashes.len();

0 commit comments

Comments
 (0)