From 681f2de63edaf5e9ec8fe50a8123f288e3e45fbb Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Fri, 1 Nov 2024 09:02:20 -1000 Subject: [PATCH] refact: remove duplicate code `HeaderCheckpoint` --- src/chain/checkpoints.rs | 41 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/chain/checkpoints.rs b/src/chain/checkpoints.rs index 55fbf10..882c72e 100644 --- a/src/chain/checkpoints.rs +++ b/src/chain/checkpoints.rs @@ -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 = 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(); @@ -542,6 +518,15 @@ impl HeaderCheckpoint { } cp } + + fn headers_from_const(headers: &[(u32, &str)]) -> Vec { + headers + .into_iter() + .map(|(height, hash)| { + HeaderCheckpoint::new(*height, BlockHash::from_str(hash).unwrap()) + }) + .collect() + } } #[derive(Debug)]