litecoinlib/consensus/params.rs
1// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
2// SPDX-License-Identifier: CC0-1.0
3
4//! Bitcoin consensus parameters.
5//!
6//! This module provides a predefined set of parameters for different Bitcoin
7//! chains (such as mainnet, testnet).
8//!
9
10use crate::network::constants::Network;
11use crate::pow::Work;
12
13/// Parameters that influence chain consensus.
14#[non_exhaustive]
15#[derive(Debug, Clone)]
16pub struct Params {
17 /// Network for which parameters are valid.
18 pub network: Network,
19 /// Time when BIP16 becomes active.
20 pub bip16_time: u32,
21 /// Block height at which BIP34 becomes active.
22 pub bip34_height: u32,
23 /// Block height at which BIP65 becomes active.
24 pub bip65_height: u32,
25 /// Block height at which BIP66 becomes active.
26 pub bip66_height: u32,
27 /// Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
28 /// (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
29 /// Examples: 1916 for 95%, 1512 for testchains.
30 pub rule_change_activation_threshold: u32,
31 /// Number of blocks with the same set of rules.
32 pub miner_confirmation_window: u32,
33 /// Proof of work limit value. It contains the lowest possible difficulty.
34 ///
35 /// Note that this value differs from Bitcoin Core's powLimit field in that this value is
36 /// attainable, but Bitcoin Core's is not. Specifically, because targets in Bitcoin are always
37 /// rounded to the nearest float expressible in "compact form", not all targets are attainable.
38 /// Still, this should not affect consensus as the only place where the non-compact form of
39 /// this is used in Bitcoin Core's consensus algorithm is in comparison and there are no
40 /// compact-expressible values between Bitcoin Core's and the limit expressed here.
41 pub pow_limit: Work,
42 /// Expected amount of time to mine one block.
43 pub pow_target_spacing: u64,
44 /// Difficulty recalculation interval.
45 pub pow_target_timespan: u64,
46 /// Determines whether minimal difficulty may be used for blocks or not.
47 pub allow_min_difficulty_blocks: bool,
48 /// Determines whether retargeting is disabled for this network or not.
49 pub no_pow_retargeting: bool,
50}
51
52impl Params {
53 /// Creates parameters set for the given network.
54 pub fn new(network: Network) -> Self {
55 match network {
56 Network::Bitcoin => Params {
57 network: Network::Bitcoin,
58 bip16_time: 1333238400, // Apr 1 2012
59 bip34_height: 227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
60 bip65_height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
61 bip66_height: 363725, // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
62 rule_change_activation_threshold: 1916, // 95%
63 miner_confirmation_window: 2016,
64 pow_limit: Work::MAINNET_MIN,
65 pow_target_spacing: 10 * 60, // 10 minutes.
66 pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
67 allow_min_difficulty_blocks: false,
68 no_pow_retargeting: false,
69 },
70 Network::Testnet => Params {
71 network: Network::Testnet,
72 bip16_time: 1333238400, // Apr 1 2012
73 bip34_height: 21111, // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
74 bip65_height: 581885, // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
75 bip66_height: 330776, // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
76 rule_change_activation_threshold: 1512, // 75%
77 miner_confirmation_window: 2016,
78 pow_limit: Work::TESTNET_MIN,
79 pow_target_spacing: 10 * 60, // 10 minutes.
80 pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
81 allow_min_difficulty_blocks: true,
82 no_pow_retargeting: false,
83 },
84 Network::Signet => Params {
85 network: Network::Signet,
86 bip16_time: 1333238400, // Apr 1 2012
87 bip34_height: 1,
88 bip65_height: 1,
89 bip66_height: 1,
90 rule_change_activation_threshold: 1916, // 95%
91 miner_confirmation_window: 2016,
92 pow_limit: Work::SIGNET_MIN,
93 pow_target_spacing: 10 * 60, // 10 minutes.
94 pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
95 allow_min_difficulty_blocks: false,
96 no_pow_retargeting: false,
97 },
98 Network::Regtest => Params {
99 network: Network::Regtest,
100 bip16_time: 1333238400, // Apr 1 2012
101 bip34_height: 100000000, // not activated on regtest
102 bip65_height: 1351,
103 bip66_height: 1251, // used only in rpc tests
104 rule_change_activation_threshold: 108, // 75%
105 miner_confirmation_window: 144,
106 pow_limit: Work::REGTEST_MIN,
107 pow_target_spacing: 10 * 60, // 10 minutes.
108 pow_target_timespan: 14 * 24 * 60 * 60, // 2 weeks.
109 allow_min_difficulty_blocks: true,
110 no_pow_retargeting: true,
111 },
112 }
113 }
114
115 /// Calculates the number of blocks between difficulty adjustments.
116 pub fn difficulty_adjustment_interval(&self) -> u64 {
117 self.pow_target_timespan / self.pow_target_spacing
118 }
119}