Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(chain): use alias for u32 #130

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/chain/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 }
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/chain/header_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{prelude::MEDIAN_TIME_PAST, DisconnectedHeader};

use super::checkpoints::HeaderCheckpoint;

pub(crate) type Headers = BTreeMap<u32, Header>;
type Height = u32;
pub(crate) type Headers = BTreeMap<Height, Header>;

const LOCATOR_LOOKBACKS: &[usize] = &[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024];
const MAX_LOOKBACK: usize = 1025;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<u32> {
pub(crate) async fn height_of_hash(&self, blockhash: BlockHash) -> Option<Height> {
if blockhash.eq(&self.anchor_checkpoint.hash) {
return Some(self.anchor_checkpoint.height);
}
Expand All @@ -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)
}

Expand All @@ -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()
Expand Down Expand Up @@ -167,7 +168,7 @@ impl HeaderChain {
}

// The last ten heights and headers in chronological order
pub(crate) fn last_ten(&self) -> BTreeMap<u32, Header> {
pub(crate) fn last_ten(&self) -> BTreeMap<Height, Header> {
self.headers
.iter()
.rev()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down
Loading