tidecoin 0.33.0-beta

General purpose library for using and interoperating with Tidecoin.
// SPDX-License-Identifier: CC0-1.0

//! Blockdata constants.
//!
//! This module provides various constants relating to the blockchain and
//! consensus code. In particular, it defines the genesis block and its
//! single transaction.

use crate::block::{self, Block, Checked};
use crate::internal_macros::impl_array_newtype_stringify;
use crate::locktime::absolute;
use crate::network::{Network, Params};
use crate::opcodes::all::*;
use crate::pow::CompactTarget;
use crate::transaction::{self, OutPoint, Transaction, TxIn, TxOut};
use crate::witness::Witness;
use crate::{script, Amount, BlockHash, BlockTime, Sequence};

/// How many seconds between blocks we expect on average.
pub const TARGET_BLOCK_SPACING: u32 = 60;
/// How many blocks between diffchanges.
pub const DIFFCHANGE_INTERVAL: u32 = 7200;
/// How much time on average should occur between diffchanges.
pub const DIFFCHANGE_TIMESPAN: u32 = 432_000;

/// The factor that non-witness serialization data is multiplied by during weight calculation.
pub const WITNESS_SCALE_FACTOR: usize = units::weight::WITNESS_SCALE_FACTOR;
/// The maximum allowed number of signature check operations in a block.
pub const MAX_BLOCK_SIGOPS_COST: i64 = 80_000;
/// Mainnet (tidecoin) pubkey address prefix.
pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 33; // 0x21
/// Mainnet alternate pubkey prefix used by Tidecoin node regtest/test decoding paths.
pub const PUBKEY_ADDRESS_PREFIX_REGTEST: u8 = 117; // 0x75
/// Mainnet (tidecoin) script address prefix.
pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 65; // 0x41
/// Mainnet alternate script address prefix accepted on decode.
pub const SCRIPT_ADDRESS_PREFIX_MAIN_ALT: u8 = 70; // 0x46
/// Test (testnet, regtest) pubkey address prefix.
pub const PUBKEY_ADDRESS_PREFIX_TEST: u8 = 92; // 0x5c
/// Test (testnet, regtest) script address prefix.
pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 127; // 0x7f
/// Testnet alternate script address prefix accepted on decode.
pub const SCRIPT_ADDRESS_PREFIX_TEST_ALT: u8 = 132; // 0x84
/// Regtest script address prefix used by Tidecoin node.
pub const SCRIPT_ADDRESS_PREFIX_REGTEST: u8 = 186; // 0xba
/// Regtest alternate script address prefix accepted on decode.
pub const SCRIPT_ADDRESS_PREFIX_REGTEST_ALT: u8 = 122; // 0x7a
/// The maximum allowed redeem script size for a P2SH output.
pub const MAX_REDEEM_SCRIPT_SIZE: usize = primitives::script::MAX_REDEEM_SCRIPT_SIZE; // 520
/// The maximum allowed redeem script size of the witness script.
pub const MAX_WITNESS_SCRIPT_SIZE: usize = 65_536;
/// The maximum allowed size of any single witness stack element.
pub const MAX_STACK_ELEMENT_SIZE: usize = 8_192;
/// Mainnet/testnet initial interval for the Tidecoin doubling-interval subsidy schedule.
pub const SUBSIDY_INITIAL_INTERVAL: u32 = 262_800;
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
pub const COINBASE_MATURITY: u32 = 100;
/// The maximum allowed size for a serialized block, in bytes (only for buffer size limits)
pub const MAX_BLOCK_SERIALIZED_SIZE: usize = 6_000_000;

#[rustfmt::skip]
const GENESIS_OUTPUT_PK: [u8; 898] = [
    0x07, 0x09, 0x03, 0xb9, 0xfd, 0xab, 0xf8, 0x94, 0x36, 0x3d, 0xea, 0x70, 0x0a, 0x33, 0xac, 0xed,
    0xb3, 0x49, 0xb8, 0xd3, 0xd6, 0x05, 0xab, 0x6a, 0x8a, 0x99, 0x7b, 0xf8, 0x85, 0x58, 0x1a, 0x5c,
    0xb6, 0x56, 0x75, 0x8d, 0x18, 0xb3, 0xd1, 0x7b, 0x14, 0x4a, 0xeb, 0xa4, 0x42, 0xcc, 0xdd, 0x55,
    0x38, 0xc4, 0xd8, 0x8d, 0xd4, 0x5d, 0x28, 0xea, 0x8c, 0xf3, 0xb1, 0xe6, 0x0f, 0x73, 0xaf, 0x23,
    0xa6, 0xac, 0x86, 0x6d, 0x56, 0xf8, 0xc4, 0x9e, 0xab, 0xf8, 0x4a, 0xd3, 0xe7, 0xa9, 0x0c, 0x4b,
    0x19, 0x8f, 0x7a, 0x66, 0x1c, 0xae, 0x08, 0x86, 0x9f, 0xb0, 0x7b, 0xa0, 0xca, 0x2a, 0x2d, 0xcb,
    0xbb, 0xa5, 0x61, 0x9f, 0x43, 0xe2, 0x14, 0xc0, 0x88, 0xda, 0x98, 0x8e, 0xae, 0x9a, 0x39, 0xd6,
    0xa5, 0x99, 0x2c, 0x69, 0x7c, 0xf5, 0x6b, 0x46, 0xb6, 0xa8, 0x1d, 0x67, 0x10, 0xb2, 0xc8, 0x4d,
    0x71, 0x34, 0x54, 0x6b, 0x3e, 0xaf, 0xc4, 0x3d, 0xa6, 0x45, 0x46, 0x41, 0xa8, 0x5b, 0x8a, 0x4a,
    0x09, 0x74, 0x7f, 0x4e, 0x28, 0x4b, 0xe5, 0x3f, 0x03, 0xdf, 0xcf, 0xcb, 0xb1, 0xc0, 0xb9, 0x70,
    0x61, 0x00, 0xe7, 0x36, 0x44, 0xfb, 0xc8, 0xb3, 0x59, 0xbc, 0x23, 0x50, 0x8f, 0x1b, 0x43, 0x50,
    0xfe, 0x5e, 0x2d, 0xa0, 0x8a, 0x33, 0xcf, 0x0b, 0x51, 0x86, 0xce, 0x83, 0xdd, 0x1b, 0xcc, 0x4a,
    0x6d, 0x62, 0x40, 0x57, 0x5d, 0x4d, 0x08, 0xd1, 0xe1, 0x59, 0x22, 0x99, 0x4f, 0x12, 0xad, 0x44,
    0x74, 0x3e, 0x27, 0x1a, 0x76, 0x68, 0x3f, 0x66, 0xa9, 0x27, 0x34, 0x91, 0xda, 0x8a, 0x21, 0x7d,
    0xd2, 0x5a, 0x87, 0x0e, 0x37, 0xe2, 0x27, 0x57, 0x7c, 0xbd, 0x46, 0x2e, 0x97, 0xb4, 0x9c, 0x32,
    0xb2, 0x39, 0x9c, 0xc2, 0xc9, 0xdb, 0x2b, 0xa6, 0x95, 0x5f, 0x01, 0x43, 0xcb, 0xa6, 0x2c, 0xe9,
    0xe9, 0xb7, 0x9f, 0x84, 0x42, 0x20, 0xad, 0x3e, 0x38, 0x2c, 0xae, 0xbd, 0x08, 0x80, 0xd2, 0x73,
    0xb3, 0xd7, 0x59, 0x0a, 0x8c, 0xcd, 0xf7, 0xd2, 0x6c, 0x8a, 0x21, 0xc6, 0x4d, 0x63, 0x7d, 0x8d,
    0x5c, 0x0d, 0xae, 0x17, 0x85, 0x13, 0x23, 0x1f, 0x53, 0x2c, 0x0b, 0x26, 0x24, 0x4d, 0x25, 0x61,
    0x0c, 0xd8, 0x39, 0x31, 0xe4, 0x2c, 0x30, 0x39, 0xe3, 0xc1, 0x51, 0x01, 0x10, 0x4e, 0x40, 0xd5,
    0x3b, 0xf4, 0xc6, 0xf5, 0x99, 0x32, 0x2e, 0x29, 0x56, 0xe8, 0x02, 0x55, 0x18, 0x03, 0x75, 0x7f,
    0xde, 0x82, 0x59, 0x38, 0x92, 0x79, 0x8f, 0xea, 0xcc, 0xbd, 0xc5, 0x2b, 0x49, 0x51, 0x97, 0xf2,
    0x76, 0xc4, 0x85, 0x8a, 0xe1, 0x62, 0x53, 0xbb, 0x18, 0x68, 0x72, 0xc4, 0xc8, 0x76, 0x65, 0xba,
    0xe4, 0x1b, 0x14, 0x11, 0xb2, 0x6c, 0x6e, 0xc6, 0x36, 0xda, 0xd5, 0x36, 0x19, 0xd1, 0x83, 0xd4,
    0x92, 0xd6, 0x99, 0x6c, 0xc1, 0x8c, 0xb6, 0xfa, 0xd8, 0xcb, 0x7e, 0xf0, 0x08, 0x89, 0xa8, 0x8c,
    0xe5, 0x43, 0x86, 0x03, 0xd8, 0xb6, 0xe9, 0x0a, 0x1c, 0xca, 0x42, 0x03, 0xb4, 0xd4, 0x6b, 0x49,
    0x42, 0xb5, 0x94, 0x92, 0xaf, 0x79, 0x7c, 0x44, 0xf5, 0x82, 0x73, 0x08, 0x34, 0xbe, 0x34, 0x11,
    0x78, 0xd5, 0xef, 0x6e, 0x9f, 0x82, 0xe1, 0xca, 0xa4, 0x40, 0x68, 0x65, 0x25, 0xbf, 0x83, 0xc7,
    0x53, 0xe4, 0xa6, 0xbe, 0xb9, 0x25, 0x4d, 0x1d, 0xb6, 0x8c, 0x15, 0xfb, 0xb3, 0x49, 0x06, 0x87,
    0x10, 0x39, 0xf9, 0x06, 0x44, 0xda, 0x14, 0x68, 0x49, 0x3b, 0xf7, 0x29, 0x21, 0x1f, 0x30, 0x84,
    0x03, 0xa9, 0x11, 0x94, 0x3e, 0x06, 0x6a, 0x98, 0x08, 0x1d, 0x8d, 0xd6, 0x61, 0xbd, 0x97, 0xa6,
    0xb9, 0xe7, 0x74, 0x47, 0x9d, 0x68, 0x61, 0xaf, 0xcd, 0x04, 0x8c, 0x2a, 0x17, 0xd7, 0x0a, 0x48,
    0x1d, 0x5e, 0x38, 0xc0, 0x67, 0xb4, 0x26, 0x29, 0xbd, 0xa1, 0xac, 0xeb, 0xa8, 0x61, 0x3e, 0xbb,
    0x18, 0x27, 0x51, 0x15, 0x45, 0x46, 0x50, 0x25, 0x88, 0x6b, 0x65, 0x10, 0xb6, 0x22, 0x81, 0x6f,
    0x43, 0xf2, 0x4b, 0x1d, 0x03, 0x00, 0xdc, 0xc2, 0xdb, 0x24, 0xca, 0x7c, 0xb0, 0x2a, 0xa6, 0xe5,
    0x83, 0xe5, 0xa6, 0xc0, 0xe8, 0xa7, 0xbd, 0xe1, 0x11, 0x0f, 0xd5, 0x0e, 0x37, 0xd5, 0x55, 0x7e,
    0x86, 0x5d, 0x9a, 0x66, 0xad, 0x9c, 0x3d, 0x19, 0xd6, 0xf3, 0xa2, 0x77, 0xc7, 0x1a, 0xbf, 0xa5,
    0x72, 0xcb, 0x7a, 0x64, 0x79, 0x54, 0xf5, 0xc0, 0x29, 0x3b, 0xd2, 0x98, 0x98, 0x44, 0x83, 0xab,
    0x6e, 0xd6, 0x5b, 0x29, 0x15, 0x92, 0x1a, 0xad, 0xc0, 0xf9, 0x86, 0x00, 0x87, 0x0b, 0xea, 0x13,
    0x66, 0x8d, 0x21, 0xb1, 0xa3, 0x1d, 0x10, 0xec, 0x28, 0x62, 0x2b, 0xf9, 0xc2, 0x8e, 0x23, 0x50,
    0x18, 0x46, 0xc0, 0xfa, 0x02, 0xce, 0x71, 0x58, 0xaa, 0xc4, 0xb7, 0x97, 0x7d, 0x51, 0xde, 0xbf,
    0x11, 0x68, 0xcd, 0x03, 0xb7, 0xd4, 0x9a, 0x48, 0x0c, 0x1b, 0x38, 0x5c, 0x52, 0xd5, 0xa0, 0x79,
    0xa2, 0xbe, 0x88, 0x4e, 0x45, 0xb3, 0x32, 0x00, 0x3c, 0xa1, 0x98, 0x96, 0x37, 0x97, 0xa1, 0xc4,
    0x5e, 0xda, 0x0f, 0x9a, 0x0d, 0xce, 0x6f, 0x03, 0x86, 0x9a, 0xe7, 0x8b, 0x93, 0x7b, 0x81, 0x9d,
    0x38, 0xb7, 0xe1, 0x12, 0x0d, 0x7f, 0xa9, 0xa8, 0xa9, 0x6a, 0x9f, 0xd6, 0x91, 0xd6, 0xc6, 0xf4,
    0x48, 0xa1, 0xeb, 0xb7, 0x48, 0x62, 0xe9, 0x5d, 0x8b, 0x7f, 0x3c, 0xf4, 0x5d, 0x99, 0xf2, 0x98,
    0x1f, 0x4f, 0x56, 0x22, 0xea, 0x05, 0xd8, 0xfc, 0x09, 0x4e, 0xc9, 0xa0, 0x67, 0x0a, 0x26, 0x84,
    0x62, 0x2c, 0x55, 0x69, 0x64, 0xe5, 0x30, 0xc1, 0x37, 0x2b, 0x44, 0x29, 0x18, 0x85, 0x6a, 0xa7,
    0x94, 0x94, 0xc1, 0x99, 0xa6, 0x91, 0x2f, 0xfa, 0x2a, 0x89, 0x3c, 0x7e, 0x7e, 0x1c, 0x09, 0x08,
    0x92, 0x26, 0x4d, 0x6e, 0x4b, 0x58, 0x32, 0x81, 0xeb, 0x2d, 0xc2, 0x52, 0xc6, 0x1e, 0x49, 0x04,
    0x38, 0x2c, 0x56, 0xf2, 0xc3, 0x6f, 0x73, 0x6a, 0x75, 0x5b, 0x51, 0x5a, 0xa2, 0xb1, 0x08, 0x20,
    0x1b, 0xf1, 0x50, 0x2d, 0xe3, 0x22, 0x8c, 0xf2, 0x58, 0x70, 0x0d, 0x8a, 0x7b, 0xec, 0x14, 0xf1,
    0x13, 0xe8, 0xd6, 0xbe, 0x8d, 0xaf, 0x77, 0x96, 0x23, 0x7a, 0x5c, 0xd9, 0xad, 0xda, 0x37, 0xca,
    0x20, 0x1c, 0xd9, 0xc7, 0xf3, 0xde, 0xdc, 0x57, 0x4b, 0x59, 0x5f, 0x21, 0xc4, 0x86, 0x52, 0x44,
    0xe5, 0xdd, 0xe5, 0xc5, 0xc5, 0xf3, 0x30, 0xfa, 0x2e, 0x58, 0xa9, 0x88, 0xda, 0x0e, 0x22, 0x3f,
    0x0a, 0x42, 0xe4, 0x1f, 0x9f, 0x9d, 0xd9, 0x90, 0x8e, 0x82, 0xa8, 0x68, 0x19, 0xec, 0x69, 0x0c,
    0x57, 0x16,
];

/// Constructs and returns the coinbase (and only) transaction of the Tidecoin genesis block.
fn tidecoin_genesis_tx() -> Transaction {
    let in_script = script::Builder::new()
        .push_int_unchecked(486604799)
        .push_int_non_minimal(4)
        .push_slice(<&script::PushBytes>::try_from(b"spectrum.ieee.org 09/Dec/2020 Photonic Quantum Computer Displays 'Supremacy' Over Supercomputers.".as_slice()).expect("genesis msg fits"))
        .into_script();

    let genesis_pk: &script::PushBytes =
        GENESIS_OUTPUT_PK.as_slice().try_into().expect("genesis pubkey fits in PushBytes");
    let out_script =
        script::Builder::new().push_slice(genesis_pk).push_opcode(OP_CHECKSIG).into_script();

    let mut ret = Transaction {
        version: transaction::Version::ONE,
        lock_time: absolute::LockTime::ZERO,
        inputs: vec![],
        outputs: vec![],
    };

    ret.inputs.push(TxIn {
        previous_output: OutPoint::COINBASE_PREVOUT,
        script_sig: in_script,
        sequence: Sequence::MAX,
        witness: Witness::default(),
    });

    ret.outputs.push(TxOut { amount: Amount::FIFTY_TDC, script_pubkey: out_script });

    ret
}

/// Constructs and returns the genesis block.
pub fn genesis_block(params: impl AsRef<Params>) -> Block<Checked> {
    let params = params.as_ref();
    let transactions = vec![tidecoin_genesis_tx()];
    let merkle_root = block::compute_merkle_root(&transactions).expect("transactions is not empty");
    let witness_root = block::compute_witness_root(&transactions);

    match params.network {
        Network::Tidecoin | Network::Testnet => Block::new_unchecked(
            block::Header {
                version: block::Version::ONE,
                prev_blockhash: BlockHash::GENESIS_PREVIOUS_BLOCK_HASH,
                merkle_root,
                time: BlockTime::from_u32(1609074580),
                bits: CompactTarget::from_consensus(0x2001ffff),
                nonce: 11033477,
                auxpow: None,
            },
            transactions,
        )
        .assume_checked(witness_root),
        Network::Regtest => Block::new_unchecked(
            block::Header {
                version: block::Version::ONE,
                prev_blockhash: BlockHash::GENESIS_PREVIOUS_BLOCK_HASH,
                merkle_root,
                time: BlockTime::from_u32(1609074580),
                bits: CompactTarget::from_consensus(0x200f0f0f),
                nonce: 0,
                auxpow: None,
            },
            transactions,
        )
        .assume_checked(witness_root),
    }
}

/// The uniquely identifying hash of the target blockchain.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChainHash([u8; 32]);
internals::impl_array_newtype!(ChainHash, u8, 32);
impl_array_newtype_stringify!(ChainHash, 32);

impl ChainHash {
    /// `ChainHash` for mainnet tidecoin.
    pub const TIDECOIN: Self = Self([
        0x75, 0x80, 0x01, 0xd3, 0x7f, 0x6a, 0x48, 0x80, 0x9e, 0xef, 0xae, 0x15, 0x62, 0xda, 0x1d,
        0x39, 0x1c, 0x38, 0x66, 0xed, 0x77, 0x33, 0x48, 0x32, 0x9f, 0x98, 0xd8, 0x02, 0x76, 0xcc,
        0x0e, 0x48,
    ]);
    /// `ChainHash` for testnet tidecoin (same genesis as mainnet).
    pub const TESTNET: Self = Self([
        0x75, 0x80, 0x01, 0xd3, 0x7f, 0x6a, 0x48, 0x80, 0x9e, 0xef, 0xae, 0x15, 0x62, 0xda, 0x1d,
        0x39, 0x1c, 0x38, 0x66, 0xed, 0x77, 0x33, 0x48, 0x32, 0x9f, 0x98, 0xd8, 0x02, 0x76, 0xcc,
        0x0e, 0x48,
    ]);
    /// `ChainHash` for regtest tidecoin.
    pub const REGTEST: Self = Self([
        0x8b, 0x2e, 0x2a, 0xbf, 0x47, 0x18, 0x2a, 0xf4, 0x87, 0x65, 0x89, 0xae, 0x41, 0xc6, 0x17,
        0xb9, 0x6f, 0xa0, 0xc6, 0x31, 0x30, 0xe2, 0x5e, 0x7c, 0x5d, 0xa5, 0x1e, 0x9b, 0x55, 0x1b,
        0x9a, 0xc1,
    ]);

    /// Returns the hash of the `network` genesis block for use as a chain hash.
    pub fn using_genesis_block(params: impl AsRef<Params>) -> Self {
        match params.as_ref().network {
            Network::Tidecoin => Self::TIDECOIN,
            Network::Testnet => Self::TESTNET,
            Network::Regtest => Self::REGTEST,
        }
    }

    /// Returns the hash of the `network` genesis block for use as a chain hash.
    pub const fn using_genesis_block_const(network: Network) -> Self {
        match network {
            Network::Tidecoin => Self::TIDECOIN,
            Network::Testnet => Self::TESTNET,
            Network::Regtest => Self::REGTEST,
        }
    }

    /// Converts genesis block hash into `ChainHash`.
    pub fn from_genesis_block_hash(block_hash: crate::BlockHash) -> Self {
        Self(block_hash.to_byte_array())
    }
}

#[cfg(test)]
mod test {
    use alloc::string::ToString;

    use super::*;
    use crate::network::params;
    use crate::Txid;

    #[test]
    fn tidecoin_genesis_first_transaction() {
        let gen = tidecoin_genesis_tx();

        assert_eq!(gen.version, transaction::Version::ONE);
        assert_eq!(gen.inputs.len(), 1);
        assert_eq!(gen.inputs[0].previous_output.txid, Txid::COINBASE_PREVOUT);
        assert_eq!(gen.inputs[0].previous_output.vout, 0xFFFFFFFF);
        assert_eq!(gen.inputs[0].sequence, Sequence::MAX);
        assert_eq!(gen.outputs.len(), 1);
        assert_eq!(gen.outputs[0].amount, "50 TDC".parse::<Amount>().unwrap());
        assert_eq!(gen.lock_time, absolute::LockTime::ZERO);
    }

    #[test]
    fn tidecoin_genesis_block_calling_convention() {
        let _ = genesis_block(&params::MAINNET);
        let _ = genesis_block(Network::Tidecoin);
        let _ = genesis_block(Params::MAINNET);
        let _ = genesis_block(&Params::MAINNET);
    }

    #[test]
    fn tidecoin_genesis_full_block() {
        let gen = genesis_block(&params::MAINNET);

        assert_eq!(gen.header().version, block::Version::ONE);
        assert_eq!(gen.header().prev_blockhash, BlockHash::GENESIS_PREVIOUS_BLOCK_HASH);
        assert_eq!(
            gen.header().merkle_root.to_string(),
            "50a03c5c0b982dccfd03bebc0f6142fa298354743dce82be936e69335d614ff8"
        );
        assert_eq!(gen.header().time, BlockTime::from_u32(1609074580));
        assert_eq!(gen.header().bits, CompactTarget::from_consensus(0x2001ffff));
        assert_eq!(gen.header().nonce, 11033477);
        assert_eq!(
            gen.header().block_hash().to_string(),
            "480ecc7602d8989f32483377ed66381c391dda6215aeef9e80486a7fd3018075"
        );
    }

    #[test]
    fn testnet_genesis_full_block() {
        let gen = genesis_block(&params::TESTNET);
        assert_eq!(gen.header().version, block::Version::ONE);
        assert_eq!(gen.header().prev_blockhash, BlockHash::GENESIS_PREVIOUS_BLOCK_HASH);
        assert_eq!(
            gen.header().merkle_root.to_string(),
            "50a03c5c0b982dccfd03bebc0f6142fa298354743dce82be936e69335d614ff8"
        );
        assert_eq!(gen.header().time, BlockTime::from_u32(1609074580));
        assert_eq!(gen.header().bits, CompactTarget::from_consensus(0x2001ffff));
        assert_eq!(gen.header().nonce, 11033477);
        assert_eq!(
            gen.header().block_hash().to_string(),
            "480ecc7602d8989f32483377ed66381c391dda6215aeef9e80486a7fd3018075"
        );
    }

    #[test]
    fn regtest_genesis_full_block() {
        let gen = genesis_block(&params::REGTEST);
        assert_eq!(gen.header().version, block::Version::ONE);
        assert_eq!(gen.header().prev_blockhash, BlockHash::GENESIS_PREVIOUS_BLOCK_HASH);
        assert_eq!(
            gen.header().merkle_root.to_string(),
            "50a03c5c0b982dccfd03bebc0f6142fa298354743dce82be936e69335d614ff8"
        );
        assert_eq!(gen.header().time, BlockTime::from_u32(1609074580));
        assert_eq!(gen.header().bits, CompactTarget::from_consensus(0x200f0f0f));
        assert_eq!(gen.header().nonce, 0);
        assert_eq!(
            gen.header().block_hash().to_string(),
            "c19a1b559b1ea55d7c5ee23031c6a06fb917c641ae896587f42a1847bf2a2e8b"
        );
    }

    fn chain_hash_and_genesis_block(network: Network) {
        use hashes::sha256;

        let genesis_hash = genesis_block(network).block_hash();
        let hash = sha256::Hash::from_byte_array(genesis_hash.to_byte_array());
        let want = format!("{:02x}", hash);

        let chain_hash = ChainHash::using_genesis_block_const(network);
        let got = format!("{:02x}", chain_hash);

        assert_eq!(got, want);

        #[allow(unreachable_patterns)]
        match network {
            Network::Tidecoin => {},
            Network::Testnet => {},
            Network::Regtest => {},
            _ => panic!("update ChainHash::using_genesis_block and chain_hash_and_genesis_block with new variants"),
        }
    }

    macro_rules! chain_hash_genesis_block {
        ($($test_name:ident, $network:expr);* $(;)*) => {
            $(
                #[test]
                fn $test_name() {
                    chain_hash_and_genesis_block($network);
                }
            )*
        }
    }

    chain_hash_genesis_block! {
        mainnet_chain_hash_genesis_block, Network::Tidecoin;
        testnet_chain_hash_genesis_block, Network::Testnet;
        regtest_chain_hash_genesis_block, Network::Regtest;
    }

    #[test]
    fn mainnet_chain_hash_test_vector() {
        let got = ChainHash::using_genesis_block_const(Network::Tidecoin).to_string();
        let want = "758001d37f6a48809eefae1562da1d391c3866ed773348329f98d80276cc0e48";
        assert_eq!(got, want);
    }
}