From 025f7401f9d74e66a5d86ce9c5b11992442afe2b Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Wed, 11 Sep 2024 09:37:39 -1000 Subject: [PATCH] refact(chain): use alias for u32 --- src/chain/checkpoints.rs | 11 ++++++----- src/chain/header_chain.rs | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/chain/checkpoints.rs b/src/chain/checkpoints.rs index b5e6a9f..8f881af 100644 --- a/src/chain/checkpoints.rs +++ b/src/chain/checkpoints.rs @@ -2,14 +2,15 @@ use std::{collections::VecDeque, str::FromStr}; use bitcoin::{BlockHash, Network}; +type Height = u32; /// Known block hashes for Regtest. Only the genesis hash. -pub const REGTEST_HEADER_CP: &[(u32, &str)] = &[( +pub const REGTEST_HEADER_CP: &[(Height, &str)] = &[( 0, "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", )]; /// Known block hashes for Signet. -pub const SIGNET_HEADER_CP: &[(u32, &str)] = &[ +pub const SIGNET_HEADER_CP: &[(Height, &str)] = &[ ( 0, "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6", @@ -101,7 +102,7 @@ pub const SIGNET_HEADER_CP: &[(u32, &str)] = &[ ]; /// Known block hashes on the Bitcoin blockchain. -pub const MAINNET_HEADER_CP: &[(u32, &str)] = &[ +pub const MAINNET_HEADER_CP: &[(Height, &str)] = &[ ( 0, "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f", @@ -452,14 +453,14 @@ pub const MAINNET_HEADER_CP: &[(u32, &str)] = &[ #[derive(Debug, Clone, Copy)] pub struct HeaderCheckpoint { /// The index of the block hash. - pub height: u32, + pub height: Height, /// The Bitcoin block hash expected at this height pub hash: BlockHash, } impl HeaderCheckpoint { /// Create a new checkpoint from a known checkpoint of significant work. - pub fn new(height: u32, hash: BlockHash) -> Self { + pub fn new(height: Height, hash: BlockHash) -> Self { HeaderCheckpoint { height, hash } } } diff --git a/src/chain/header_chain.rs b/src/chain/header_chain.rs index 46c8942..1ba24c3 100644 --- a/src/chain/header_chain.rs +++ b/src/chain/header_chain.rs @@ -6,7 +6,8 @@ use crate::{prelude::MEDIAN_TIME_PAST, DisconnectedHeader}; use super::checkpoints::HeaderCheckpoint; -pub(crate) type Headers = BTreeMap; +type Height = u32; +pub(crate) type Headers = BTreeMap; const LOCATOR_LOOKBACKS: &[usize] = &[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]; const MAX_LOOKBACK: usize = 1025; @@ -42,8 +43,8 @@ impl HeaderChain { } // The canoncial height of the chain, one less than the length - pub(crate) fn height(&self) -> u32 { - self.headers.len() as u32 + self.anchor_checkpoint.height + pub(crate) fn height(&self) -> Height { + self.headers.len() as Height + self.anchor_checkpoint.height } // The length of the chain we have interally @@ -71,7 +72,7 @@ impl HeaderChain { } // The height of the blockhash in the chain - pub(crate) async fn height_of_hash(&self, blockhash: BlockHash) -> Option { + pub(crate) async fn height_of_hash(&self, blockhash: BlockHash) -> Option { if blockhash.eq(&self.anchor_checkpoint.hash) { return Some(self.anchor_checkpoint.height); } @@ -84,7 +85,7 @@ impl HeaderChain { } // This header chain contains a block hash - pub(crate) fn header_at_height(&self, height: u32) -> Option<&Header> { + pub(crate) fn header_at_height(&self, height: Height) -> Option<&Header> { self.headers.get(&height) } @@ -111,7 +112,7 @@ impl HeaderChain { } // Calculate the chainwork after a fork height to evalutate the fork - pub(crate) fn chainwork_after_height(&self, height: u32) -> Work { + pub(crate) fn chainwork_after_height(&self, height: Height) -> Work { let work = self .headers .iter() @@ -167,7 +168,7 @@ impl HeaderChain { } // The last ten heights and headers in chronological order - pub(crate) fn last_ten(&self) -> BTreeMap { + pub(crate) fn last_ten(&self) -> BTreeMap { self.headers .iter() .rev() @@ -204,7 +205,7 @@ impl HeaderChain { let current_anchor = self.height(); for (index, header) in batch.iter().enumerate() { self.headers - .insert(current_anchor + 1 + index as u32, *header); + .insert(current_anchor + 1 + index as Height, *header); } } else { // Panic if we don't contain the hash. Something went wrong further up the call stack. @@ -235,13 +236,13 @@ impl HeaderChain { let current_anchor = self.height(); for (index, header) in batch.iter().enumerate() { self.headers - .insert(current_anchor + 1 + index as u32, *header); + .insert(current_anchor + 1 + index as Height, *header); } } reorged.into_iter().rev().collect() } - fn remove(&mut self, height: &u32) { + fn remove(&mut self, height: &Height) { self.headers.remove(height); }