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

feat: build checkpoint from height #170

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
72 changes: 72 additions & 0 deletions src/chain/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,46 @@ impl HeaderCheckpoint {
pub fn new(height: Height, hash: BlockHash) -> Self {
HeaderCheckpoint { height, hash }
}

/// Get the checkpoint that is closest to the specified height without exceeding that height.
/// This constructor is useful when recovering a wallet where one does not need to scan the
/// full chain, but must scan past a specified height to sufficiently recover the wallet.
pub fn closest_checkpoint_below_height(height: Height, network: Network) -> Self {
let checkpoints: Vec<HeaderCheckpoint> = match network {
Network::Bitcoin => MAINNET_HEADER_CP
.iter()
.copied()
.map(|(height, hash)| {
HeaderCheckpoint::new(height, BlockHash::from_str(hash).unwrap())
})
.collect(),
Network::Testnet => panic!("unimplemented network"),
Network::Signet => SIGNET_HEADER_CP
.iter()
.copied()
.map(|(height, hash)| {
HeaderCheckpoint::new(height, BlockHash::from_str(hash).unwrap())
})
.collect(),
Network::Regtest => REGTEST_HEADER_CP
.iter()
.copied()
.map(|(height, hash)| {
HeaderCheckpoint::new(height, BlockHash::from_str(hash).unwrap())
})
.collect(),
_ => unreachable!(),
};
let mut cp = *checkpoints.first().unwrap();
for checkpoint in checkpoints {
if height.ge(&checkpoint.height) {
cp = checkpoint;
} else {
break;
}
}
cp
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -545,3 +585,35 @@ impl TryFrom<(u32, &str)> for HeaderCheckpoint {
Ok(HeaderCheckpoint::new(value.0, hash))
}
}

#[cfg(test)]
mod tests {
use super::*;
use bitcoin::Network;

#[test]
fn test_correct_checkpoints_selected() {
let network = Network::Bitcoin;
let first_height = 840_000;
let checkpoint = HeaderCheckpoint::closest_checkpoint_below_height(first_height, network);
assert_eq!(
checkpoint.hash,
BlockHash::from_str("0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5")
.unwrap()
);
let second_height = 840_001;
let checkpoint = HeaderCheckpoint::closest_checkpoint_below_height(second_height, network);
assert_eq!(
checkpoint.hash,
BlockHash::from_str("0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5")
.unwrap()
);
let third_height = 839_999;
let checkpoint = HeaderCheckpoint::closest_checkpoint_below_height(third_height, network);
assert_eq!(
checkpoint.hash,
BlockHash::from_str("000000000000000000011d55599ed27d7efca05f5849b755319c89eb2cffbc1f")
.unwrap()
);
}
}
Loading