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.
24const MAX_BITS_BITCOIN: Uint256 = Uint256([
25    0xffffffffffffffffu64,
26    0xffffffffffffffffu64,
27    0xffffffffffffffffu64,
28    0x00000000ffffffffu64,
29]);
30/// Lowest possible difficulty for Testnet.
31const MAX_BITS_TESTNET: Uint256 = Uint256([
32    0xffffffffffffffffu64,
33    0xffffffffffffffffu64,
34    0xffffffffffffffffu64,
35    0x00000000ffffffffu64,
36]);
37/// Lowest possible difficulty for Regtest.
38const MAX_BITS_REGTEST: Uint256 = Uint256([
39    0xffffffffffffffffu64,
40    0xffffffffffffffffu64,
41    0xffffffffffffffffu64,
42    0x7fffffffffffffffu64,
43]);
44
45#[derive(Debug, Clone)]
46/// Parameters that influence chain consensus.
47pub struct Params {
48    /// Network for which parameters are valid.
49    pub network: Network,
50    /// Time when BIP16 becomes active.
51    pub bip16_time: u32,
52    /// Block height at which BIP34 becomes active.
53    pub bip34_height: u32,
54    /// Block height at which BIP65 becomes active.
55    pub bip65_height: u32,
56    /// Block height at which BIP66 becomes active.
57    pub bip66_height: u32,
58    /// Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
59    /// (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
60    /// Examples: 1916 for 95%, 1512 for testchains.
61    pub rule_change_activation_threshold: u32,
62    /// Number of blocks with the same set of rules.
63    pub miner_confirmation_window: u32,
64    /// Proof of work limit value. It contains the lowest possible difficulty.
65    pub pow_limit: Uint256,
66    /// Expected amount of time to mine one block.
67    pub pow_target_spacing: u64,
68    /// Difficulty recalculation interval.
69    pub pow_target_timespan: u64,
70    /// Determines whether minimal difficulty may be used for blocks or not.
71    pub allow_min_difficulty_blocks: bool,
72    /// Determines whether retargeting is disabled for this network or not.
73    pub no_pow_retargeting: bool,
74}
75
76impl Params {
77    /// Creates parameters set for the given network.
78    pub fn new(network: Network) -> Self {
79        match network {
80            Network::Bitcoin => Params {
81                network: Network::Bitcoin,
82                bip16_time: 1333238400,                 // Apr 1 2012
83                bip34_height: 227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
84                bip65_height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
85                bip66_height: 363725, // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
86                rule_change_activation_threshold: 1916, // 95%
87                miner_confirmation_window: 2016,
88                pow_limit: MAX_BITS_BITCOIN.clone(),
89                pow_target_spacing: 10 * 60,            // 10 minutes.
90                pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
91                allow_min_difficulty_blocks: false,
92                no_pow_retargeting: false,
93            },
94            Network::Testnet => Params {
95                network: Network::Testnet,
96                bip16_time: 1333238400,                 // Apr 1 2012
97                bip34_height: 21111, // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
98                bip65_height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
99                bip66_height: 330776, // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
100                rule_change_activation_threshold: 1512, // 75%
101                miner_confirmation_window: 2016,
102                pow_limit: MAX_BITS_TESTNET.clone(),
103                pow_target_spacing: 10 * 60,            // 10 minutes.
104                pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
105                allow_min_difficulty_blocks: true,
106                no_pow_retargeting: false,
107            },
108            Network::Regtest => Params {
109                network: Network::Regtest,
110                bip16_time: 1333238400,  // Apr 1 2012
111                bip34_height: 100000000, // not activated on regtest
112                bip65_height: 1351,
113                bip66_height: 1251,                    // used only in rpc tests
114                rule_change_activation_threshold: 108, // 75%
115                miner_confirmation_window: 144,
116                pow_limit: MAX_BITS_REGTEST.clone(),
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: true,
121            },
122        }
123    }
124
125    /// Calculates the number of blocks between difficulty adjustments.
126    pub fn difficulty_adjustment_interval(&self) -> u64 {
127        self.pow_target_timespan / self.pow_target_spacing
128    }
129}