Skip to content

Commit da804a1

Browse files
committed
eth_getBlockByNumber return null when parameter is bigger than latest block
1 parent cc5b1e7 commit da804a1

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

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/src/consensus/consensus_graph/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::{
2727
verification::VerificationConfig,
2828
NodeType, Notifications,
2929
};
30+
pub use onchain_blocks_provider::EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE;
3031

3132
use cfx_executor::spec::CommonParams;
3233

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use cfx_types::H256;
99
use primitives::{compute_block_number, EpochNumber};
1010
use std::cmp::min;
1111

12+
pub const EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE: &str =
13+
"Invalid params: expected a numbers with less than largest epoch number.";
14+
1215
impl ConsensusGraph {
1316
/// Returns the total number of blocks processed in consensus graph.
1417
///
@@ -40,7 +43,7 @@ impl ConsensusGraph {
4043
EpochNumber::Number(num) => {
4144
let epoch_num = num;
4245
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());
46+
return Err(EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE.to_owned());
4447
}
4548
epoch_num
4649
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use consensus_graph::{
2222
rpc_api::transaction_provider::{
2323
MaybeExecutedTxExtraInfo, TransactionInfo,
2424
},
25-
ConsensusGraph,
25+
ConsensusGraph, EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE,
2626
};
2727
pub use statistics::ConsensusGraphStatistics;
2828

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use cfx_types::{
3232
use cfx_util_macros::bail;
3333
use cfx_vm_types::Error as VmError;
3434
use cfxcore::{
35+
consensus::EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE,
3536
errors::{Error as CoreError, Result as CoreResult},
3637
ConsensusGraph, SharedConsensusGraph, SharedSynchronizationService,
3738
SharedTransactionPool,
@@ -606,13 +607,19 @@ impl EthApi {
606607
.map_err(RpcError::invalid_params)?
607608
}
608609
_ => {
609-
self.consensus_graph()
610-
.get_phantom_block_by_number(
611-
block_num.try_into()?,
612-
None,
613-
false, /* include_traces */
614-
)
615-
.map_err(RpcError::invalid_params)?
610+
match self.consensus_graph().get_phantom_block_by_number(
611+
block_num.try_into()?,
612+
None,
613+
false, /* include_traces */
614+
) {
615+
Ok(pb) => pb,
616+
Err(e) if e == EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE => {
617+
None
618+
}
619+
Err(e) => {
620+
return Err(RpcError::invalid_params(e).into());
621+
}
622+
}
616623
}
617624
}
618625
};

0 commit comments

Comments
 (0)