Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_CONSENSUS_PARAMS_H
7 : : #define BITCOIN_CONSENSUS_PARAMS_H
8 : :
9 : : #include <script/verify_flags.h>
10 : : #include <uint256.h>
11 : :
12 : : #include <array>
13 : : #include <chrono>
14 : : #include <cstdint>
15 : : #include <limits>
16 : : #include <map>
17 : : #include <vector>
18 : :
19 : : namespace Consensus {
20 : :
21 : : /**
22 : : * A buried deployment is one where the height of the activation has been hardcoded into
23 : : * the client implementation long after the consensus change has activated. See BIP 90.
24 : : * Consensus changes for which the new rules are enforced from genesis are not listed here.
25 : : */
26 : : enum BuriedDeployment : int16_t {
27 : : // buried deployments get negative values to avoid overlap with DeploymentPos
28 : : DEPLOYMENT_HEIGHTINCB = std::numeric_limits<int16_t>::min(),
29 : : DEPLOYMENT_CLTV,
30 : : DEPLOYMENT_DERSIG,
31 : : DEPLOYMENT_CSV,
32 : : // SCRIPT_VERIFY_WITNESS is enforced from genesis, but the check for downloading
33 : : // missing witness data is not. BIP 147 also relies on hardcoded activation height.
34 : : DEPLOYMENT_SEGWIT,
35 : : };
36 : : constexpr bool ValidDeployment(BuriedDeployment dep) { return dep <= DEPLOYMENT_SEGWIT; }
37 : :
38 : : enum DeploymentPos : uint16_t {
39 : : DEPLOYMENT_TESTDUMMY,
40 : : // NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
41 : : // Removing an entry may require bumping MinBIP9WarningHeight.
42 : : MAX_VERSION_BITS_DEPLOYMENTS
43 : : };
44 : : constexpr bool ValidDeployment(DeploymentPos dep) { return dep < MAX_VERSION_BITS_DEPLOYMENTS; }
45 : :
46 : : /**
47 : : * Struct for each individual consensus rule change using BIP9.
48 : : */
49 : : struct BIP9Deployment {
50 : : /** Bit position to select the particular bit in nVersion. */
51 : : int bit{28};
52 : : /** Start MedianTime for version bits miner confirmation. Can be a date in the past */
53 : : int64_t nStartTime{NEVER_ACTIVE};
54 : : /** Timeout/expiry MedianTime for the deployment attempt. */
55 : : int64_t nTimeout{NEVER_ACTIVE};
56 : : /** If lock in occurs, delay activation until at least this block
57 : : * height. Note that activation will only occur on a retarget
58 : : * boundary.
59 : : */
60 : : int min_activation_height{0};
61 : : /** Period of blocks to check signalling in (usually retarget period, ie params.DifficultyAdjustmentInterval()) */
62 : : uint32_t period{2016};
63 : : /**
64 : : * Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
65 : : * which is also used for BIP9 deployments.
66 : : * Examples: 1916 for 95%, 1512 for testchains.
67 : : */
68 : : uint32_t threshold{1916};
69 : :
70 : : /** Constant for nTimeout very far in the future. */
71 : : static constexpr int64_t NO_TIMEOUT = std::numeric_limits<int64_t>::max();
72 : :
73 : : /** Special value for nStartTime indicating that the deployment is always active.
74 : : * This is useful for testing, as it means tests don't need to deal with the activation
75 : : * process (which takes at least 3 BIP9 intervals). Only tests that specifically test the
76 : : * behaviour during activation cannot use this. */
77 : : static constexpr int64_t ALWAYS_ACTIVE = -1;
78 : :
79 : : /** Special value for nStartTime indicating that the deployment is never active.
80 : : * This is useful for integrating the code changes for a new feature
81 : : * prior to deploying it on some or all networks. */
82 : : static constexpr int64_t NEVER_ACTIVE = -2;
83 : : };
84 : :
85 : : /**
86 : : * Parameters that influence chain consensus.
87 : : */
88 : : struct Params {
89 : : uint256 hashGenesisBlock;
90 : : int nSubsidyHalvingInterval;
91 : : /**
92 : : * Hashes of blocks that
93 : : * - are known to be consensus valid, and
94 : : * - buried in the chain, and
95 : : * - fail if the default script verify flags are applied.
96 : : */
97 : : std::map<uint256, script_verify_flags> script_flag_exceptions;
98 : : /** Block height and hash at which BIP34 becomes active */
99 : : int BIP34Height;
100 : : uint256 BIP34Hash;
101 : : /** Block height at which BIP65 becomes active */
102 : : int BIP65Height;
103 : : /** Block height at which BIP66 becomes active */
104 : : int BIP66Height;
105 : : /** Block height at which CSV (BIP68, BIP112 and BIP113) becomes active */
106 : : int CSVHeight;
107 : : /** Block height at which Segwit (BIP141, BIP143 and BIP147) becomes active.
108 : : * Note that segwit v0 script rules are enforced on all blocks except the
109 : : * BIP 16 exception blocks. */
110 : : int SegwitHeight;
111 : : /** Don't warn about unknown BIP 9 activations below this height.
112 : : * This prevents us from warning about the CSV, segwit and taproot activations. */
113 : : int MinBIP9WarningHeight;
114 : : std::array<BIP9Deployment,MAX_VERSION_BITS_DEPLOYMENTS> vDeployments;
115 : : /** Proof of work parameters */
116 : : uint256 powLimit;
117 : : bool fPowAllowMinDifficultyBlocks;
118 : : /**
119 : : * Enforce BIP94 timewarp attack mitigation. On testnet4 this also enforces
120 : : * the block storm mitigation.
121 : : */
122 : : bool enforce_BIP94;
123 : : bool fPowNoRetargeting;
124 : : int64_t nPowTargetSpacing;
125 : : int64_t nPowTargetTimespan;
126 : 10 : std::chrono::seconds PowTargetSpacing() const
127 : : {
128 [ # # ]: 10 : return std::chrono::seconds{nPowTargetSpacing};
129 : : }
130 [ - + ]: 78729 : int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
[ + - + + ]
131 : : /** The best chain should have at least this much work */
132 : : uint256 nMinimumChainWork;
133 : : /** By default assume that the signatures in ancestors of this block are valid */
134 : : uint256 defaultAssumeValid;
135 : :
136 : : /**
137 : : * If true, witness commitments contain a payload equal to a Bitcoin Script solution
138 : : * to the signet challenge. See BIP325.
139 : : */
140 : : bool signet_blocks{false};
141 : : std::vector<uint8_t> signet_challenge;
142 : :
143 : 163449 : int DeploymentHeight(BuriedDeployment dep) const
144 : : {
145 [ + + + + : 163449 : switch (dep) {
+ - ]
146 : 17004 : case DEPLOYMENT_HEIGHTINCB:
147 : 17004 : return BIP34Height;
148 : 17123 : case DEPLOYMENT_CLTV:
149 : 17123 : return BIP65Height;
150 : 17123 : case DEPLOYMENT_DERSIG:
151 : 17123 : return BIP66Height;
152 : 51131 : case DEPLOYMENT_CSV:
153 : 51131 : return CSVHeight;
154 : 61068 : case DEPLOYMENT_SEGWIT:
155 : 61068 : return SegwitHeight;
156 : : } // no default case, so the compiler can warn about missing cases
157 : : return std::numeric_limits<int>::max();
158 : : }
159 : : };
160 : :
161 : : } // namespace Consensus
162 : :
163 : : #endif // BITCOIN_CONSENSUS_PARAMS_H
|