sc_consensus_nakamoto/
chain_params.rs

1use bitcoin::consensus::Params;
2use bitcoin::{BlockHash, Network};
3use std::collections::HashMap;
4
5/// Extended [`Params`].
6#[derive(Debug, Clone)]
7pub struct ChainParams {
8    /// Chain params defined in rust-bitcoin.
9    pub params: Params,
10    /// Block height at which CSV becomes active.
11    pub csv_height: u32,
12    /// Block height at which Segwit becomes active.
13    pub segwit_height: u32,
14    /// A map of block hashes to script verification flag exceptions.
15    ///
16    /// This allows for certain blocks to have specific script verification flags, overriding
17    /// the default rules. For example, exceptions may be made for blocks that activated
18    /// BIP16 (P2SH) or Taproot under special conditions.
19    pub script_flag_exceptions: HashMap<BlockHash, u32>,
20}
21
22impl ChainParams {
23    /// Constructs a new instance of [`ChainParams`].
24    // https://github.com/bitcoin/bitcoin/blob/6f9db1ebcab4064065ccd787161bf2b87e03cc1f/src/kernel/chainparams.cpp#L71
25    pub fn new(network: Network) -> Self {
26        let params = Params::new(network);
27        match network {
28            Network::Bitcoin => Self {
29                params,
30                csv_height: 419328, // 000000000000000004a1b34462cb8aeebd5799177f7a29cf28f2d1961716b5b5
31                segwit_height: 481824, // 0000000000000000001c8018d9cb3b742ef25114f27563e3fc4a1902167f9893
32                script_flag_exceptions: [
33                    // BIP16 exception
34                    (
35                        "00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22",
36                        bitcoinconsensus::VERIFY_NONE,
37                    ),
38                    // Taproot exception
39                    (
40                        "0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad",
41                        bitcoinconsensus::VERIFY_P2SH | bitcoinconsensus::VERIFY_WITNESS,
42                    ),
43                ]
44                .into_iter()
45                .map(|(block_hash, flag)| {
46                    (block_hash.parse().expect("Hash must be valid; qed"), flag)
47                })
48                .collect(),
49            },
50            Network::Testnet => Self {
51                params,
52                csv_height: 770112, // 00000000025e930139bac5c6c31a403776da130831ab85be56578f3fa75369bb
53                segwit_height: 834624, // 00000000002b980fcd729daaa248fd9316a5200e9b367f4ff2c42453e84201ca
54                script_flag_exceptions: HashMap::from_iter([
55                    // BIP16 exception
56                    (
57                        "00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105"
58                            .parse()
59                            .expect("Hash must be valid; qed"),
60                        bitcoinconsensus::VERIFY_NONE,
61                    ),
62                ]),
63            },
64            Network::Signet => Self {
65                params,
66                csv_height: 1,
67                segwit_height: 1,
68                script_flag_exceptions: Default::default(),
69            },
70            Network::Regtest => Self {
71                params,
72                csv_height: 1,    // Always active unless overridden
73                segwit_height: 0, // Always active unless overridden
74                script_flag_exceptions: Default::default(),
75            },
76            _ => unreachable!("Unknown Bitcoin Network"),
77        }
78    }
79}