Skip to content

Commit

Permalink
consensus: tweak the checkpoint verifier
Browse files Browse the repository at this point in the history
Use HashMap, without an Option<> wrapper.
Tidy up the matches.

Part of #429.
  • Loading branch information
teor2345 committed Jun 19, 2020
1 parent 76a788d commit 7876e82
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions zebra-consensus/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct CheckpointVerifier<S> {
/// which only happen in the last few hundred blocks in the chain.
/// (zcashd allows chain reorganizations up to 99 blocks, and prunes
/// orphaned side-chains after 288 blocks.)
checkpoint_list: Option<HashMap<BlockHeight, BlockHeaderHash>>,
checkpoint_list: HashMap<BlockHeight, BlockHeaderHash>,
}

/// The error type for the CheckpointVerifier Service.
Expand Down Expand Up @@ -64,9 +64,8 @@ where

// These checks are cheap, so we can do them in the call()

let checkpoints = match &self.checkpoint_list {
Some(checkpoints) => checkpoints,
None => return async { Err("the checkpoint list is empty".into()) }.boxed(),
if self.checkpoint_list.is_empty() {
return async { Err("the checkpoint list is empty".into()) }.boxed();
};

let block_height = match block.coinbase_height() {
Expand All @@ -80,17 +79,14 @@ where
// - implement chaining from checkpoints to their ancestors
// - if chaining is expensive, move this check to the Future
// - should the state contain a mapping from previous_block_hash to block?
let checkpoint_hash_ref = match checkpoints.get(&block_height) {
Some(hash) => hash,
let checkpoint_hash = match self.checkpoint_list.get(&block_height) {
Some(&hash) => hash,
None => {
return async { Err("the block's height is not a checkpoint height".into()) }
.boxed()
}
};

// Avoid moving a reference into the future.
let checkpoint_hash = *checkpoint_hash_ref;

// `state_service.call` is OK here because we already called
// `state_service.poll_ready` in our `poll_ready`.
let add_block = self.state_service.call(zebra_state::Request::AddBlock {
Expand Down Expand Up @@ -131,7 +127,7 @@ where
/// backed by the same state layer.
pub fn init<S>(
state_service: S,
checkpoint_list: Option<HashMap<BlockHeight, BlockHeaderHash>>,
checkpoint_list: HashMap<BlockHeight, BlockHeaderHash>,
) -> impl Service<
Arc<Block>,
Response = BlockHeaderHash,
Expand Down

0 comments on commit 7876e82

Please sign in to comment.