Skip to content

Commit af20ec8

Browse files
committed
use new defined error other than string for block retrive methods
op error convert
1 parent da804a1 commit af20ec8

File tree

24 files changed

+149
-44
lines changed

24 files changed

+149
-44
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" }

crates/cfxcore/core/Cargo.toml

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

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

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

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

3231
use cfx_executor::spec::CommonParams;
3332

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

Lines changed: 6 additions & 8 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

@@ -9,9 +10,6 @@ use cfx_types::H256;
910
use primitives::{compute_block_number, EpochNumber};
1011
use std::cmp::min;
1112

12-
pub const EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE: &str =
13-
"Invalid params: expected a numbers with less than largest epoch number.";
14-
1513
impl ConsensusGraph {
1614
/// Returns the total number of blocks processed in consensus graph.
1715
///
@@ -26,7 +24,7 @@ impl ConsensusGraph {
2624
/// Convert EpochNumber to height based on the current ConsensusGraph
2725
pub fn get_height_from_epoch_number(
2826
&self, epoch_number: EpochNumber,
29-
) -> Result<u64, String> {
27+
) -> Result<u64, ProviderBlockError> {
3028
Ok(match epoch_number {
3129
EpochNumber::Earliest => 0,
3230
EpochNumber::LatestCheckpoint => {
@@ -43,7 +41,7 @@ impl ConsensusGraph {
4341
EpochNumber::Number(num) => {
4442
let epoch_num = num;
4543
if epoch_num > self.inner.read_recursive().best_epoch_number() {
46-
return Err(EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE.to_owned());
44+
return Err(ProviderBlockError::EpochNumberTooLarge);
4745
}
4846
epoch_num
4947
}
@@ -64,7 +62,7 @@ impl ConsensusGraph {
6462

6563
pub fn get_block_hashes_by_epoch(
6664
&self, epoch_number: EpochNumber,
67-
) -> Result<Vec<H256>, String> {
65+
) -> Result<Vec<H256>, ProviderBlockError> {
6866
self.get_height_from_epoch_number(epoch_number)
6967
.and_then(|height| {
7068
self.inner.read_recursive().block_hashes_by_epoch(height)
@@ -73,7 +71,7 @@ impl ConsensusGraph {
7371

7472
pub fn get_hash_from_epoch_number(
7573
&self, epoch_number: EpochNumber,
76-
) -> Result<H256, String> {
74+
) -> Result<H256, ProviderBlockError> {
7775
self.get_height_from_epoch_number(epoch_number)
7876
.and_then(|height| {
7977
self.inner.read().get_pivot_hash_from_epoch_number(height)
@@ -82,7 +80,7 @@ impl ConsensusGraph {
8280

8381
pub fn get_skipped_block_hashes_by_epoch(
8482
&self, epoch_number: EpochNumber,
85-
) -> Result<Vec<H256>, String> {
83+
) -> Result<Vec<H256>, ProviderBlockError> {
8684
self.get_height_from_epoch_number(epoch_number)
8785
.and_then(|height| {
8886
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/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, EPOCH_NUMBER_TOO_LARGE_ERR_MESSAGE,
25+
ConsensusGraph,
2626
};
2727
pub use statistics::ConsensusGraphStatistics;
2828

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

0 commit comments

Comments
 (0)