sc_consensus_nakamoto/
chain_params.rs

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