bitcoin/consensus/
params.rs

1// Rust Bitcoin Library
2// Written in 2014 by
3//   Andrew Poelstra <apoelstra@wpsoftware.net>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the CC0 Public Domain Dedication
11// along with this software.
12// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13//
14
15//! Consensus parameters
16//!
17//! This module provides predefined set of parameters for different chains.
18//!
19
20use network::constants::Network;
21use util::uint::Uint256;
22
23/// Lowest possible difficulty for Mainnet. See comment on Params::pow_limit for more info.
24const MAX_BITS_BITCOIN: Uint256 = Uint256([
25    0x0000000000000000u64,
26    0x0000000000000000u64,
27    0x0000000000000000u64,
28    0x00000000ffff0000u64,
29]);
30/// Lowest possible difficulty for Testnet. See comment on Params::pow_limit for more info.
31const MAX_BITS_TESTNET: Uint256 = Uint256([
32    0x0000000000000000u64,
33    0x0000000000000000u64,
34    0x0000000000000000u64,
35    0x00000000ffff0000u64,
36]);
37/// Lowest possible difficulty for Signet. See comment on Params::pow_limit for more info.
38const MAX_BITS_SIGNET: Uint256 = Uint256([
39    0x0000000000000000u64,
40    0x0000000000000000u64,
41    0x0000000000000000u64,
42    0x00000377ae000000u64,
43]);
44/// Lowest possible difficulty for Regtest. See comment on Params::pow_limit for more info.
45const MAX_BITS_REGTEST: Uint256 = Uint256([
46    0x0000000000000000u64,
47    0x0000000000000000u64,
48    0x0000000000000000u64,
49    0x7fffff0000000000u64,
50]);
51
52#[derive(Debug, Clone)]
53/// Parameters that influence chain consensus.
54pub struct Params {
55    /// Network for which parameters are valid.
56    pub network: Network,
57    /// Time when BIP16 becomes active.
58    pub bip16_time: u32,
59    /// Block height at which BIP34 becomes active.
60    pub bip34_height: u32,
61    /// Block height at which BIP65 becomes active.
62    pub bip65_height: u32,
63    /// Block height at which BIP66 becomes active.
64    pub bip66_height: u32,
65    /// Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
66    /// (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
67    /// Examples: 1916 for 95%, 1512 for testchains.
68    pub rule_change_activation_threshold: u32,
69    /// Number of blocks with the same set of rules.
70    pub miner_confirmation_window: u32,
71    /// Proof of work limit value. It contains the lowest possible difficulty.
72    ///
73    /// Note that this value differs from Bitcoin Core's powLimit field in that this value is
74    /// attainable, but Bitcoin Core's is not. Specifically, because targets in Bitcoin are always
75    /// rounded to the nearest float expressible in "compact form", not all targets are attainable.
76    /// Still, this should not affect consensus as the only place where the non-compact form of
77    /// this is used in Bitcoin Core's consensus algorithm is in comparison and there are no
78    /// compact-expressible values between Bitcoin Core's and the limit expressed here.
79    pub pow_limit: Uint256,
80    /// Expected amount of time to mine one block.
81    pub pow_target_spacing: u64,
82    /// Difficulty recalculation interval.
83    pub pow_target_timespan: u64,
84    /// Determines whether minimal difficulty may be used for blocks or not.
85    pub allow_min_difficulty_blocks: bool,
86    /// Determines whether retargeting is disabled for this network or not.
87    pub no_pow_retargeting: bool,
88}
89
90impl Params {
91    /// Creates parameters set for the given network.
92    pub fn new(network: Network) -> Self {
93        match network {
94            Network::Bitcoin => Params {
95                network: Network::Bitcoin,
96                bip16_time: 1333238400,                 // Apr 1 2012
97                bip34_height: 227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
98                bip65_height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
99                bip66_height: 363725, // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
100                rule_change_activation_threshold: 1916, // 95%
101                miner_confirmation_window: 2016,
102                pow_limit: MAX_BITS_BITCOIN,
103                pow_target_spacing: 10 * 60,            // 10 minutes.
104                pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
105                allow_min_difficulty_blocks: false,
106                no_pow_retargeting: false,
107            },
108            Network::Testnet => Params {
109                network: Network::Testnet,
110                bip16_time: 1333238400,                 // Apr 1 2012
111                bip34_height: 21111, // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
112                bip65_height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
113                bip66_height: 330776, // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
114                rule_change_activation_threshold: 1512, // 75%
115                miner_confirmation_window: 2016,
116                pow_limit: MAX_BITS_TESTNET,
117                pow_target_spacing: 10 * 60,            // 10 minutes.
118                pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
119                allow_min_difficulty_blocks: true,
120                no_pow_retargeting: false,
121            },
122            Network::Signet => Params {
123                network: Network::Signet,
124                bip16_time: 1333238400,                 // Apr 1 2012
125                bip34_height: 1,
126                bip65_height: 1,
127                bip66_height: 1,
128                rule_change_activation_threshold: 1916, // 95%
129                miner_confirmation_window: 2016,
130                pow_limit: MAX_BITS_SIGNET,
131                pow_target_spacing: 10 * 60,            // 10 minutes.
132                pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
133                allow_min_difficulty_blocks: false,
134                no_pow_retargeting: false,
135            },
136            Network::Regtest => Params {
137                network: Network::Regtest,
138                bip16_time: 1333238400,  // Apr 1 2012
139                bip34_height: 100000000, // not activated on regtest
140                bip65_height: 1351,
141                bip66_height: 1251,                    // used only in rpc tests
142                rule_change_activation_threshold: 108, // 75%
143                miner_confirmation_window: 144,
144                pow_limit: MAX_BITS_REGTEST,
145                pow_target_spacing: 10 * 60,            // 10 minutes.
146                pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
147                allow_min_difficulty_blocks: true,
148                no_pow_retargeting: true,
149            },
150        }
151    }
152
153    /// Calculates the number of blocks between difficulty adjustments.
154    pub fn difficulty_adjustment_interval(&self) -> u64 {
155        self.pow_target_timespan / self.pow_target_spacing
156    }
157}