Skip to content

Commit

Permalink
Add EpochNumber::LatestCheckpoint, and use it as default for from_epo…
Browse files Browse the repository at this point in the history
…ch (#1518)

* Add EpochNumber::LatestCheckpoint, and use it as default for from_epoch
in cfx_getLogs.

* Update CHANGELOG.

* fmt

* Fix unit test.

* Remove old comments.
  • Loading branch information
peilun-conflux authored and Peilun Li committed Jun 9, 2020
1 parent 5264264 commit f5ddfd2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions client/src/rpc/types/epoch_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
}
}
}
Expand All @@ -64,6 +67,9 @@ impl EpochNumber {
EpochNumber::LatestMined => PrimitiveEpochNumber::LatestMined,
EpochNumber::LatestState => PrimitiveEpochNumber::LatestState,
EpochNumber::Num(num) => PrimitiveEpochNumber::Number(num),
EpochNumber::LatestCheckpoint => {
PrimitiveEpochNumber::LatestCheckpoint
}
}
}
}
Expand All @@ -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)),
Expand All @@ -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'"
)
}

Expand Down Expand Up @@ -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:<BLOCK_HASH>'"
"an epoch number or 'latest_mined', 'latest_state', 'latest_checkpoint',\
or 'earliest', or 'hash:<BLOCK_HASH>'"
)
}

Expand Down
7 changes: 5 additions & 2 deletions client/src/rpc/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions core/src/consensus/consensus_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions core/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ impl ConsensusGraph {
) -> Result<u64, String> {
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) => {
Expand Down Expand Up @@ -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()
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/light_protocol/query_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions primitives/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EpochNumber> for u64 {
Expand Down

0 comments on commit f5ddfd2

Please sign in to comment.