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