Merge rust-bitcoin/rust-bitcoin#1283: Add constants to `ChainHash` for each `Network`

b1d85160ba Add constants to `ChainHash` for each `Network` (Jeffrey Czyz)

Pull request description:

  `ChainHash::using_genesis_block` can't be made `const` because it uses a `match` expression, which is only valid in Rust 1.46. Add individual constants as a workaround so that `ChainHash` can be used in `const` contexts.

ACKs for top commit:
  apoelstra:
    ACK b1d85160ba
  tcharding:
    ACK b1d85160ba

Tree-SHA512: 808628e633be7db8fd3676edebefc998bba55c2434028b46873cff82fa5440b4f940904a2cb4cced23e1cabf7680234a434f823e89a13c73e52c81fd50ec312f
This commit is contained in:
Andrew Poelstra 2022-09-16 13:13:26 +00:00
commit ba642daf54
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 14 additions and 10 deletions

View File

@ -167,12 +167,6 @@ pub fn genesis_block(network: Network) -> Block {
} }
} }
// Mainnet value can be verified at https://github.com/lightning/bolts/blob/master/00-introduction.md
const GENESIS_BLOCK_HASH_BITCOIN: [u8; 32] = [111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0];
const GENESIS_BLOCK_HASH_TESTNET: [u8; 32] = [67, 73, 127, 215, 248, 38, 149, 113, 8, 244, 163, 15, 217, 206, 195, 174, 186, 121, 151, 32, 132, 233, 14, 173, 1, 234, 51, 9, 0, 0, 0, 0];
const GENESIS_BLOCK_HASH_SIGNET: [u8; 32] = [246, 30, 238, 59, 99, 163, 128, 164, 119, 160, 99, 175, 50, 178, 187, 201, 124, 159, 249, 240, 31, 44, 66, 37, 233, 115, 152, 129, 8, 0, 0, 0];
const GENESIS_BLOCK_HASH_REGTEST: [u8; 32] = [6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15];
/// The uniquely identifying hash of the target blockchain. /// The uniquely identifying hash of the target blockchain.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChainHash([u8; 32]); pub struct ChainHash([u8; 32]);
@ -180,16 +174,26 @@ impl_array_newtype!(ChainHash, u8, 32);
impl_bytes_newtype!(ChainHash, 32); impl_bytes_newtype!(ChainHash, 32);
impl ChainHash { impl ChainHash {
// Mainnet value can be verified at https://github.com/lightning/bolts/blob/master/00-introduction.md
/// `ChainHash` for mainnet bitcoin.
pub const BITCOIN: Self = Self([111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0]);
/// `ChainHash` for testnet bitcoin.
pub const TESTNET: Self = Self([67, 73, 127, 215, 248, 38, 149, 113, 8, 244, 163, 15, 217, 206, 195, 174, 186, 121, 151, 32, 132, 233, 14, 173, 1, 234, 51, 9, 0, 0, 0, 0]);
/// `ChainHash` for signet bitcoin.
pub const SIGNET: Self = Self([246, 30, 238, 59, 99, 163, 128, 164, 119, 160, 99, 175, 50, 178, 187, 201, 124, 159, 249, 240, 31, 44, 66, 37, 233, 115, 152, 129, 8, 0, 0, 0]);
/// `ChainHash` for regtest bitcoin.
pub const REGTEST: Self = Self([6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15]);
/// Returns the hash of the `network` genesis block for use as a chain hash. /// Returns the hash of the `network` genesis block for use as a chain hash.
/// ///
/// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash) /// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash)
/// for specification. /// for specification.
pub fn using_genesis_block(network: Network) -> Self { pub fn using_genesis_block(network: Network) -> Self {
match network { match network {
Network::Bitcoin => ChainHash(GENESIS_BLOCK_HASH_BITCOIN), Network::Bitcoin => Self::BITCOIN,
Network::Testnet => ChainHash(GENESIS_BLOCK_HASH_TESTNET), Network::Testnet => Self::TESTNET,
Network::Signet => ChainHash(GENESIS_BLOCK_HASH_SIGNET), Network::Signet => Self::SIGNET,
Network::Regtest => ChainHash(GENESIS_BLOCK_HASH_REGTEST), Network::Regtest => Self::REGTEST,
} }
} }
} }