bitcoin/consensus/
params.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Bitcoin consensus parameters.
4//!
5//! This module provides a predefined set of parameters for different Bitcoin
6//! chains (such as mainnet, testnet, testnet4).
7//!
8
9use crate::network::Network;
10#[cfg(doc)]
11use crate::pow::CompactTarget;
12use crate::pow::Target;
13
14/// Parameters that influence chain consensus.
15#[non_exhaustive]
16#[derive(Debug, Clone)]
17pub struct Params {
18    /// Network for which parameters are valid.
19    pub network: Network,
20    /// Time when BIP16 becomes active.
21    pub bip16_time: u32,
22    /// Block height at which BIP34 becomes active.
23    pub bip34_height: u32,
24    /// Block height at which BIP65 becomes active.
25    pub bip65_height: u32,
26    /// Block height at which BIP66 becomes active.
27    pub bip66_height: u32,
28    /// Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
29    /// (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
30    /// Examples: 1916 for 95%, 1512 for testchains.
31    pub rule_change_activation_threshold: u32,
32    /// Number of blocks with the same set of rules.
33    pub miner_confirmation_window: u32,
34    /// Proof of work limit value. It contains the lowest possible difficulty.
35    #[deprecated(since = "0.32.0", note = "field renamed to max_attainable_target")]
36    pub pow_limit: Target,
37    /// The maximum **attainable** target value for these params.
38    ///
39    /// Not all target values are attainable because consensus code uses the compact format to
40    /// represent targets (see [`CompactTarget`]).
41    ///
42    /// Note that this value differs from Bitcoin Core's powLimit field in that this value is
43    /// attainable, but Bitcoin Core's is not. Specifically, because targets in Bitcoin are always
44    /// rounded to the nearest float expressible in "compact form", not all targets are attainable.
45    /// Still, this should not affect consensus as the only place where the non-compact form of
46    /// this is used in Bitcoin Core's consensus algorithm is in comparison and there are no
47    /// compact-expressible values between Bitcoin Core's and the limit expressed here.
48    pub max_attainable_target: Target,
49    /// Expected amount of time to mine one block.
50    pub pow_target_spacing: u64,
51    /// Difficulty recalculation interval.
52    pub pow_target_timespan: u64,
53    /// Determines whether minimal difficulty may be used for blocks or not.
54    pub allow_min_difficulty_blocks: bool,
55    /// Determines whether retargeting is disabled for this network or not.
56    pub no_pow_retargeting: bool,
57}
58
59/// The mainnet parameters.
60///
61/// Use this for a static reference e.g., `&params::MAINNET`.
62///
63/// For more on static vs const see The Rust Reference [using-statics-or-consts] section.
64///
65/// [using-statics-or-consts]: <https://doc.rust-lang.org/reference/items/static-items.html#using-statics-or-consts>
66pub static MAINNET: Params = Params::MAINNET;
67/// The testnet3 parameters.
68#[deprecated(since = "0.32.4", note = "Use TESTNET3 instead")]
69pub static TESTNET: Params = Params::TESTNET3;
70/// The testnet3 parameters.
71pub static TESTNET3: Params = Params::TESTNET3;
72/// The testnet4 parameters.
73pub static TESTNET4: Params = Params::TESTNET4;
74/// The signet parameters.
75pub static SIGNET: Params = Params::SIGNET;
76/// The regtest parameters.
77pub static REGTEST: Params = Params::REGTEST;
78
79#[allow(deprecated)]            // For `pow_limit`.
80impl Params {
81    /// The mainnet parameters (alias for `Params::MAINNET`).
82    pub const BITCOIN: Params = Params::MAINNET;
83
84    /// The mainnet parameters.
85    pub const MAINNET: Params = Params {
86        network: Network::Bitcoin,
87        bip16_time: 1333238400,                 // Apr 1 2012
88        bip34_height: 227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
89        bip65_height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
90        bip66_height: 363725, // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
91        rule_change_activation_threshold: 1916, // 95%
92        miner_confirmation_window: 2016,
93        pow_limit: Target::MAX_ATTAINABLE_MAINNET,
94        max_attainable_target: Target::MAX_ATTAINABLE_MAINNET,
95        pow_target_spacing: 10 * 60,            // 10 minutes.
96        pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
97        allow_min_difficulty_blocks: false,
98        no_pow_retargeting: false,
99    };
100
101    /// The testnet3 parameters.
102    #[deprecated(since = "0.32.4", note = "Use TESTNET3 instead")]
103    pub const TESTNET: Params = Params {
104        network: Network::Testnet,
105        bip16_time: 1333238400,                 // Apr 1 2012
106        bip34_height: 21111, // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
107        bip65_height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
108        bip66_height: 330776, // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
109        rule_change_activation_threshold: 1512, // 75%
110        miner_confirmation_window: 2016,
111        pow_limit: Target::MAX_ATTAINABLE_TESTNET,
112        max_attainable_target: Target::MAX_ATTAINABLE_TESTNET,
113        pow_target_spacing: 10 * 60,            // 10 minutes.
114        pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
115        allow_min_difficulty_blocks: true,
116        no_pow_retargeting: false,
117    };
118
119    /// The testnet3 parameters.
120    pub const TESTNET3: Params = Params {
121        network: Network::Testnet,
122        bip16_time: 1333238400,                 // Apr 1 2012
123        bip34_height: 21111, // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
124        bip65_height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
125        bip66_height: 330776, // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
126        rule_change_activation_threshold: 1512, // 75%
127        miner_confirmation_window: 2016,
128        pow_limit: Target::MAX_ATTAINABLE_TESTNET,
129        max_attainable_target: Target::MAX_ATTAINABLE_TESTNET,
130        pow_target_spacing: 10 * 60,            // 10 minutes.
131        pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
132        allow_min_difficulty_blocks: true,
133        no_pow_retargeting: false,
134    };
135
136    /// The testnet4 parameters.
137    pub const TESTNET4: Params = Params {
138        network: Network::Testnet4,
139        bip16_time: 1333238400, // Apr 1 2012
140        bip34_height: 1,
141        bip65_height: 1,
142        bip66_height: 1,
143        rule_change_activation_threshold: 1512, // 75%
144        miner_confirmation_window: 2016,
145        pow_limit: Target::MAX_ATTAINABLE_TESTNET,
146        max_attainable_target: Target::MAX_ATTAINABLE_TESTNET,
147        pow_target_spacing: 10 * 60,            // 10 minutes.
148        pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
149        allow_min_difficulty_blocks: true,
150        no_pow_retargeting: false,
151    };
152
153    /// The signet parameters.
154    pub const SIGNET: Params = Params {
155        network: Network::Signet,
156        bip16_time: 1333238400, // Apr 1 2012
157        bip34_height: 1,
158        bip65_height: 1,
159        bip66_height: 1,
160        rule_change_activation_threshold: 1916, // 95%
161        miner_confirmation_window: 2016,
162        pow_limit: Target::MAX_ATTAINABLE_SIGNET,
163        max_attainable_target: Target::MAX_ATTAINABLE_SIGNET,
164        pow_target_spacing: 10 * 60,            // 10 minutes.
165        pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
166        allow_min_difficulty_blocks: false,
167        no_pow_retargeting: false,
168    };
169
170    /// The regtest parameters.
171    pub const REGTEST: Params = Params {
172        network: Network::Regtest,
173        bip16_time: 1333238400,  // Apr 1 2012
174        bip34_height: 100000000, // not activated on regtest
175        bip65_height: 1351,
176        bip66_height: 1251,                    // used only in rpc tests
177        rule_change_activation_threshold: 108, // 75%
178        miner_confirmation_window: 144,
179        pow_limit: Target::MAX_ATTAINABLE_REGTEST,
180        max_attainable_target: Target::MAX_ATTAINABLE_REGTEST,
181        pow_target_spacing: 10 * 60,            // 10 minutes.
182        pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
183        allow_min_difficulty_blocks: true,
184        no_pow_retargeting: true,
185    };
186
187    /// Creates parameters set for the given network.
188    pub const fn new(network: Network) -> Self {
189        match network {
190            Network::Bitcoin => Params::MAINNET,
191            Network::Testnet => Params::TESTNET3,
192            Network::Testnet4 => Params::TESTNET4,
193            Network::Signet => Params::SIGNET,
194            Network::Regtest => Params::REGTEST,
195        }
196    }
197
198    /// Calculates the number of blocks between difficulty adjustments.
199    pub fn difficulty_adjustment_interval(&self) -> u64 {
200        self.pow_target_timespan / self.pow_target_spacing
201    }
202}
203
204impl From<Network> for Params {
205    fn from(value: Network) -> Self { Self::new(value) }
206}
207
208impl From<&Network> for Params {
209    fn from(value: &Network) -> Self { Self::new(*value) }
210}
211
212impl From<Network> for &'static Params {
213    fn from(value: Network) -> Self { value.params() }
214}
215
216impl From<&Network> for &'static Params {
217    fn from(value: &Network) -> Self { value.params() }
218}
219
220impl AsRef<Params> for Params {
221    fn as_ref(&self) -> &Params { self }
222}
223
224impl AsRef<Params> for Network {
225    fn as_ref(&self) -> &Params { Self::params(*self) }
226}