diff --git a/CHANGELOG.md b/CHANGELOG.md index cc95ba43e5..d6083e99c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ - Improve the performance of the consensus layer for unstable TreeGraph scenarios. +- Change the default value of `from_epoch` in RPC `cfx_getLogs` from "earliest" to "latest_checkpoint". +Now if no `from_epoch` is specified, it will only return logs after the latest checkpoint. + # 0.5.0 ## Improvements diff --git a/client/src/rpc/types/epoch_number.rs b/client/src/rpc/types/epoch_number.rs index dc0c84e644..a94dcb2a5a 100644 --- a/client/src/rpc/types/epoch_number.rs +++ b/client/src/rpc/types/epoch_number.rs @@ -18,12 +18,14 @@ use std::{fmt, str::FromStr}; pub enum EpochNumber { /// Number Num(u64), - /// Latest mined block. - LatestMined, + /// Earliest epoch (true genesis) + Earliest, + /// The latest checkpoint (cur_era_genesis) + LatestCheckpoint, /// Latest block with state. LatestState, - /// Earliest epoch (genesis) - Earliest, + /// Latest mined block. + LatestMined, } //impl Default for EpochNumber { @@ -51,8 +53,9 @@ impl Serialize for EpochNumber { serializer.serialize_str("latest_state") } EpochNumber::Earliest => serializer.serialize_str("earliest"), - /* EpochNumber::Pending => - * serializer.serialize_str("pending"), */ + EpochNumber::LatestCheckpoint => { + serializer.serialize_str("latest_checkpoint") + } } } } @@ -64,6 +67,9 @@ impl EpochNumber { EpochNumber::LatestMined => PrimitiveEpochNumber::LatestMined, EpochNumber::LatestState => PrimitiveEpochNumber::LatestState, EpochNumber::Num(num) => PrimitiveEpochNumber::Number(num), + EpochNumber::LatestCheckpoint => { + PrimitiveEpochNumber::LatestCheckpoint + } } } } @@ -76,6 +82,7 @@ impl FromStr for EpochNumber { "latest_mined" => Ok(EpochNumber::LatestMined), "latest_state" => Ok(EpochNumber::LatestState), "earliest" => Ok(EpochNumber::Earliest), + "latest_checkpoint" => Ok(EpochNumber::LatestCheckpoint), _ if s.starts_with("0x") => u64::from_str_radix(&s[2..], 16) .map(EpochNumber::Num) .map_err(|e| format!("Invalid epoch number: {}", e)), @@ -100,7 +107,7 @@ impl<'a> Visitor<'a> for EpochNumberVisitor { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!( formatter, - "an epoch number or 'latest_mined', 'latest_state', or 'earliest'" + "an epoch number or 'latest_mined', 'latest_state', 'latest_checkpoint', or 'earliest'" ) } @@ -171,7 +178,8 @@ impl<'a> Visitor<'a> for BlockHashOrEpochNumberVisitor { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!( formatter, - "an epoch number or 'latest_mined', 'latest_state', or 'earliest', or 'hash:'" + "an epoch number or 'latest_mined', 'latest_state', 'latest_checkpoint',\ + or 'earliest', or 'hash:'" ) } diff --git a/client/src/rpc/types/filter.rs b/client/src/rpc/types/filter.rs index d92574b345..7186dad31b 100644 --- a/client/src/rpc/types/filter.rs +++ b/client/src/rpc/types/filter.rs @@ -130,7 +130,10 @@ impl Filter { }; PrimitiveFilter { - from_epoch: self.from_epoch.unwrap_or(EpochNumber::Earliest).into(), + from_epoch: self + .from_epoch + .unwrap_or(EpochNumber::LatestCheckpoint) + .into(), to_epoch: self.to_epoch.unwrap_or(EpochNumber::LatestMined).into(), block_hashes: maybe_vec_into(&self.block_hashes), address: maybe_vec_into(&address), @@ -343,7 +346,7 @@ mod tests { }; let primitive_filter = PrimitiveFilter { - from_epoch: PrimitiveEpochNumber::Earliest, + from_epoch: PrimitiveEpochNumber::LatestCheckpoint, to_epoch: PrimitiveEpochNumber::LatestMined, block_hashes: Some(vec![ H256::from_str("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap(), diff --git a/core/src/consensus/consensus_trait.rs b/core/src/consensus/consensus_trait.rs index 574f48f2c8..c2aa033f7f 100644 --- a/core/src/consensus/consensus_trait.rs +++ b/core/src/consensus/consensus_trait.rs @@ -34,6 +34,8 @@ pub trait ConsensusGraphTrait: Send + Sync { fn best_epoch_number(&self) -> u64; + fn latest_checkpoint_epoch_number(&self) -> u64; + fn best_chain_id(&self) -> u64; fn best_block_hash(&self) -> H256; diff --git a/core/src/consensus/mod.rs b/core/src/consensus/mod.rs index 88c7eff305..76ad0ad7e2 100644 --- a/core/src/consensus/mod.rs +++ b/core/src/consensus/mod.rs @@ -319,6 +319,9 @@ impl ConsensusGraph { ) -> Result { Ok(match epoch_number { EpochNumber::Earliest => 0, + EpochNumber::LatestCheckpoint => { + self.latest_checkpoint_epoch_number() + } EpochNumber::LatestMined => self.best_epoch_number(), EpochNumber::LatestState => self.best_executed_state_epoch_number(), EpochNumber::Number(num) => { @@ -1094,6 +1097,14 @@ impl ConsensusGraphTrait for ConsensusGraph { self.best_info.read_recursive().best_epoch_number } + fn latest_checkpoint_epoch_number(&self) -> u64 { + self.data_man + .block_height_by_hash( + &self.data_man.get_cur_consensus_era_genesis_hash(), + ) + .expect("header for cur_era_genesis should exist") + } + fn best_chain_id(&self) -> u64 { self.best_info.read_recursive().best_chain_id() } diff --git a/core/src/light_protocol/query_service.rs b/core/src/light_protocol/query_service.rs index d0eb6e7fc7..758aafe218 100644 --- a/core/src/light_protocol/query_service.rs +++ b/core/src/light_protocol/query_service.rs @@ -503,6 +503,9 @@ impl QueryService { match epoch { EpochNumber::Earliest => Ok(0), + EpochNumber::LatestCheckpoint => { + Ok(self.consensus.latest_checkpoint_epoch_number()) + } EpochNumber::LatestMined => Ok(latest_verifiable), EpochNumber::LatestState => Ok(latest_verifiable), EpochNumber::Number(n) if n <= latest_verifiable => Ok(n), diff --git a/primitives/src/epoch.rs b/primitives/src/epoch.rs index 7f970393ba..f4e16545b5 100644 --- a/primitives/src/epoch.rs +++ b/primitives/src/epoch.rs @@ -15,10 +15,12 @@ pub enum EpochNumber { Number(u64), /// Earliest block (checkpoint). Earliest, - /// Latest mined block. - LatestMined, + /// The latest checkpoint (cur_era_genesis) + LatestCheckpoint, /// Latest block with state. LatestState, + /// Latest mined block. + LatestMined, } impl Into for u64 {