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