From e8a3a288695bcec46f0f52940534fd3613ce8153 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Thu, 5 Nov 2020 20:00:10 -0800 Subject: [PATCH] swap best_chain_len and related constants to u32 for consistency (#1257) --- zebra-state/src/constants.rs | 7 ++----- zebra-state/src/service/memory_state.rs | 12 +++++------- zebra-state/src/tests.rs | 4 ++-- zebra-state/src/util.rs | 2 +- zebra-utils/src/bin/zebra-checkpoints/main.rs | 2 +- 5 files changed, 11 insertions(+), 16 deletions(-) diff --git a/zebra-state/src/constants.rs b/zebra-state/src/constants.rs index 612a7b03..960db673 100644 --- a/zebra-state/src/constants.rs +++ b/zebra-state/src/constants.rs @@ -1,17 +1,14 @@ -use zebra_chain::block; - /// The maturity threshold for transparent coinbase outputs. /// /// A transaction MUST NOT spend a transparent output of a coinbase transaction /// from a block less than 100 blocks prior to the spend. Note that transparent /// outputs of coinbase transactions include Founders' Reward outputs. -pub const MIN_TRASPARENT_COINBASE_MATURITY: block::Height = block::Height(100); +pub const MIN_TRASPARENT_COINBASE_MATURITY: u32 = 100; /// The maximum chain reorganisation height. /// /// Allowing reorganisations past this height could allow double-spends of /// coinbase transactions. -pub const MAX_BLOCK_REORG_HEIGHT: block::Height = - block::Height(MIN_TRASPARENT_COINBASE_MATURITY.0 - 1); +pub const MAX_BLOCK_REORG_HEIGHT: u32 = MIN_TRASPARENT_COINBASE_MATURITY - 1; pub const SLED_FORMAT_VERSION: u32 = 1; diff --git a/zebra-state/src/service/memory_state.rs b/zebra-state/src/service/memory_state.rs index d22de653..eff2f8e5 100644 --- a/zebra-state/src/service/memory_state.rs +++ b/zebra-state/src/service/memory_state.rs @@ -493,13 +493,11 @@ impl NonFinalizedState { } /// Returns the length of the non-finalized portion of the current best chain. - pub fn best_chain_len(&self) -> block::Height { - block::Height( - self.best_chain() - .expect("only called after inserting a block") - .blocks - .len() as u32, - ) + pub fn best_chain_len(&self) -> u32 { + self.best_chain() + .expect("only called after inserting a block") + .blocks + .len() as u32 } /// Returns `true` if `hash` is contained in the non-finalized portion of any diff --git a/zebra-state/src/tests.rs b/zebra-state/src/tests.rs index 26aeb1e4..26af6df2 100644 --- a/zebra-state/src/tests.rs +++ b/zebra-state/src/tests.rs @@ -48,10 +48,10 @@ fn test_block_locator_heights() { block::Height(min_height), "locators must end with the specified final height" ); - assert!(height - final_height.0 <= constants::MAX_BLOCK_REORG_HEIGHT.0, + assert!(height - final_height.0 <= constants::MAX_BLOCK_REORG_HEIGHT, format!("locator for {} must not be more than the maximum reorg height {} below the tip, but {} is {} blocks below the tip", height, - constants::MAX_BLOCK_REORG_HEIGHT.0, + constants::MAX_BLOCK_REORG_HEIGHT, final_height.0, height - final_height.0)); } diff --git a/zebra-state/src/util.rs b/zebra-state/src/util.rs index bd8d8574..bd721ed5 100644 --- a/zebra-state/src/util.rs +++ b/zebra-state/src/util.rs @@ -8,7 +8,7 @@ pub fn block_locator_heights(tip_height: block::Height) -> Vec { // Stop at the reorg limit, or the genesis block. let min_locator_height = tip_height .0 - .saturating_sub(constants::MAX_BLOCK_REORG_HEIGHT.0); + .saturating_sub(constants::MAX_BLOCK_REORG_HEIGHT); let locators = iter::successors(Some(1u32), |h| h.checked_mul(2)) .flat_map(move |step| tip_height.0.checked_sub(step)); let locators = iter::once(tip_height.0) diff --git a/zebra-utils/src/bin/zebra-checkpoints/main.rs b/zebra-utils/src/bin/zebra-checkpoints/main.rs index 406a3619..e30187fc 100644 --- a/zebra-utils/src/bin/zebra-checkpoints/main.rs +++ b/zebra-utils/src/bin/zebra-checkpoints/main.rs @@ -89,7 +89,7 @@ fn main() -> Result<()> { // Zcash reorg limit. let height_limit = height_limit .0 - .checked_sub(zebra_state::MAX_BLOCK_REORG_HEIGHT.0) + .checked_sub(zebra_state::MAX_BLOCK_REORG_HEIGHT) .map(block::Height) .expect("zcashd has some mature blocks: wait for zcashd to sync more blocks");