Skip to content

Commit

Permalink
Merge pull request #192 from rustaceanrob/cp-11-1
Browse files Browse the repository at this point in the history
Remove duplicate code `HeaderCheckpoint`
  • Loading branch information
rustaceanrob authored Nov 1, 2024
2 parents bed26e6 + 681f2de commit 0a3dfac
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions src/chain/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,35 +501,11 @@ impl HeaderCheckpoint {
/// 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::Bitcoin => Self::headers_from_const(MAINNET_HEADER_CP),
Network::Testnet => panic!("unimplemented network"),
Network::Testnet4 => TESTNET4_HEADER_CP
.iter()
.copied()
.map(|(height, hash)| {
HeaderCheckpoint::new(height, BlockHash::from_str(hash).unwrap())
})
.collect(),
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(),
Network::Testnet4 => Self::headers_from_const(TESTNET4_HEADER_CP),
Network::Signet => Self::headers_from_const(SIGNET_HEADER_CP),
Network::Regtest => Self::headers_from_const(REGTEST_HEADER_CP),
_ => unreachable!(),
};
let mut cp = *checkpoints.first().unwrap();
Expand All @@ -542,6 +518,15 @@ impl HeaderCheckpoint {
}
cp
}

fn headers_from_const(headers: &[(u32, &str)]) -> Vec<HeaderCheckpoint> {
headers
.into_iter()
.map(|(height, hash)| {
HeaderCheckpoint::new(*height, BlockHash::from_str(hash).unwrap())
})
.collect()
}
}

#[derive(Debug)]
Expand Down

0 comments on commit 0a3dfac

Please sign in to comment.