Merge rust-bitcoin/rust-bitcoin#1286: Make `ChainHash::using_genesis_block` constant

92ef41b663 Make `ChainHash::using_genesis_block` constant (Jeffrey Czyz)

Pull request description:

  ChainHash::using_genesis_block can't be `const` if it uses a `match` expression prior to Rust 1.46. Use an array mapping to work around this limitation.

  Follow-up suggested in [#1283](https://github.com/rust-bitcoin/rust-bitcoin/pull/1283#issuecomment-1249418809).

ACKs for top commit:
  apoelstra:
    ACK 92ef41b663
  Kixunil:
    ACK 92ef41b663

Tree-SHA512: 71f95877c8e5335012ad0339e1f8691e3b33344fa02ecc24c3d4d728232cb7b0de62aec20eb1855b23eeccfbc2eeab920b21ee2243d95c6c89fa8ad5bc846975
This commit is contained in:
Andrew Poelstra 2022-09-16 22:13:32 +00:00
commit aeacbe763d
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 11 additions and 7 deletions

View File

@ -188,13 +188,9 @@ impl ChainHash {
///
/// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash)
/// for specification.
pub fn using_genesis_block(network: Network) -> Self {
match network {
Network::Bitcoin => Self::BITCOIN,
Network::Testnet => Self::TESTNET,
Network::Signet => Self::SIGNET,
Network::Regtest => Self::REGTEST,
}
pub const fn using_genesis_block(network: Network) -> Self {
let hashes = [Self::BITCOIN, Self::TESTNET, Self::SIGNET, Self::REGTEST];
hashes[network as usize]
}
}
@ -281,6 +277,14 @@ mod test {
// Compare strings because the spec specifically states how the chain hash must encode to hex.
assert_eq!(got, want);
match network {
Network::Bitcoin => {},
Network::Testnet => {},
Network::Signet => {},
Network::Regtest => {},
// Update ChainHash::using_genesis_block and chain_hash_genesis_block with new variants.
}
}
macro_rules! chain_hash_genesis_block {