1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 #include <limits>
11 
12 namespace Consensus {
13 
14 enum DeploymentPos
15 {
16     DEPLOYMENT_TESTDUMMY,
17     // NOTE: Also add new deployments to VersionBitsDeploymentInfo in versionbits.cpp
18     MAX_VERSION_BITS_DEPLOYMENTS
19 };
20 
21 /**
22  * Struct for each individual consensus rule change using BIP9.
23  */
24 struct BIP9Deployment {
25     /** Bit position to select the particular bit in nVersion. */
26     int bit;
27     /** Start MedianTime for version bits miner confirmation. Can be a date in the past */
28     int64_t nStartTime;
29     /** Timeout/expiry MedianTime for the deployment attempt. */
30     int64_t nTimeout;
31 
32     /** Constant for nTimeout very far in the future. */
33     static constexpr int64_t NO_TIMEOUT = std::numeric_limits<int64_t>::max();
34 
35     /** Special value for nStartTime indicating that the deployment is always active.
36      *  This is useful for testing, as it means tests don't need to deal with the activation
37      *  process (which takes at least 3 BIP9 intervals). Only tests that specifically test the
38      *  behaviour during activation cannot use this. */
39     static constexpr int64_t ALWAYS_ACTIVE = -1;
40 };
41 
42 /**
43  * Parameters that influence chain consensus.
44  */
45 struct Params {
46     uint256 hashGenesisBlock;
47     int nSubsidyHalvingInterval;
48     int nSubsidyHalvingIntervalV2;
49     /* Block hash that is excepted from BIP16 enforcement */
50     uint256 BIP16Exception;
51     /** Block height and hash at which BIP34 becomes active */
52     int BIP34Height;
53     uint256 BIP34Hash;
54     /** Block height at which BIP65 becomes active */
55     int BIP65Height;
56     /** Block height at which BIP66 becomes active */
57     int BIP66Height;
58     /** Block height at which CSV (BIP68, BIP112 and BIP113) becomes active */
59     int CSVHeight;
60     /** Block height at which Segwit (BIP141, BIP143 and BIP147) becomes active.
61      * Note that segwit v0 script rules are enforced on all blocks except the
62      * BIP 16 exception blocks. */
63     int SegwitHeight;
64     /** Don't warn about unknown BIP 9 activations below this height.
65      * This prevents us from warning about the CSV and segwit activations. */
66     int MinBIP9WarningHeight;
67     /** Block height at which QIP5 becomes active */
68     int QIP5Height;
69     /** Block height at which QIP6 becomes active */
70     int QIP6Height;
71     /** Block height at which QIP7 becomes active */
72     int QIP7Height;
73     /** Block height at which QIP9 becomes active */
74     int QIP9Height;
75     /** Block height at which Offline Staking becomes active */
76     int nOfflineStakeHeight;
77     /** Block height at which Reduce Block Time becomes active */
78     int nReduceBlocktimeHeight;
79     /** Block height at which EVM Muir Glacier fork becomes active */
80     int nMuirGlacierHeight;
81     /**
82      * Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
83      * (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
84      * Examples: 1916 for 95%, 1512 for testchains.
85      */
86     uint32_t nRuleChangeActivationThreshold;
87     uint32_t nMinerConfirmationWindow;
88     BIP9Deployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS];
89     /** Proof of work parameters */
90     uint256 powLimit;
91     uint256 posLimit;
92     uint256 QIP9PosLimit;
93     uint256 RBTPosLimit;
94     bool fPowAllowMinDifficultyBlocks;
95     bool fPowNoRetargeting;
96     bool fPoSNoRetargeting;
97     int64_t nPowTargetSpacing;
98     int64_t nRBTPowTargetSpacing;
99     int64_t nPowTargetTimespan;
100     int64_t nPowTargetTimespanV2;
101     int64_t nRBTPowTargetTimespan;
102     uint256 nMinimumChainWork;
103     uint256 defaultAssumeValid;
104     int nLastPOWBlock;
105     int nFirstMPoSBlock;
106     int nMPoSRewardRecipients;
107     int nFixUTXOCacheHFHeight;
108     int nEnableHeaderSignatureHeight;
109     /** Block sync-checkpoint span*/
110     int nCheckpointSpan;
111     int nRBTCheckpointSpan;
112     uint160 delegationsAddress;
113     int nLastMPoSBlock;
114     int nLastBigReward;
115     uint32_t nStakeTimestampMask;
116     uint32_t nRBTStakeTimestampMask;
117     int64_t nBlocktimeDownscaleFactor;
118     /** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
119     int nCoinbaseMaturity;
120     int nRBTCoinbaseMaturity;
DifficultyAdjustmentIntervalParams121     int64_t DifficultyAdjustmentInterval(int height) const
122     {
123         int64_t targetTimespan = TargetTimespan(height);
124         int64_t targetSpacing = TargetSpacing(height);
125         return targetTimespan / targetSpacing;
126     }
StakeTimestampMaskParams127     int64_t StakeTimestampMask(int height) const
128     {
129         return height < nReduceBlocktimeHeight ? nStakeTimestampMask : nRBTStakeTimestampMask;
130     }
SubsidyHalvingIntervalParams131     int SubsidyHalvingInterval(int height) const
132     {
133         return height < nReduceBlocktimeHeight ? nSubsidyHalvingInterval : nSubsidyHalvingIntervalV2;
134     }
BlocktimeDownscaleFactorParams135     int64_t BlocktimeDownscaleFactor(int height) const
136     {
137         return height < nReduceBlocktimeHeight ? 1 : nBlocktimeDownscaleFactor;
138     }
TargetSpacingParams139     int64_t TargetSpacing(int height) const
140     {
141         return height < nReduceBlocktimeHeight ? nPowTargetSpacing : nRBTPowTargetSpacing;
142     }
SubsidyHalvingWeightParams143     int SubsidyHalvingWeight(int height) const
144     {
145         if(height <= nLastBigReward)
146             return 0;
147 
148         int blocktimeDownscaleFactor = BlocktimeDownscaleFactor(height);
149         int blockCount = height - nLastBigReward;
150         int beforeDownscale = blocktimeDownscaleFactor == 1 ? 0 : nReduceBlocktimeHeight - nLastBigReward - 1;
151         int subsidyHalvingWeight = blockCount - beforeDownscale + beforeDownscale * blocktimeDownscaleFactor;
152         return subsidyHalvingWeight;
153     }
TimestampDownscaleFactorParams154     int64_t TimestampDownscaleFactor(int height) const
155     {
156         return height < nReduceBlocktimeHeight ? 1 : (nStakeTimestampMask + 1) / (nRBTStakeTimestampMask + 1);
157     }
TargetTimespanParams158     int64_t TargetTimespan(int height) const
159     {
160         return height < QIP9Height ? nPowTargetTimespan :
161             (height < nReduceBlocktimeHeight ? nPowTargetTimespanV2 : nRBTPowTargetTimespan);
162     }
CheckpointSpanParams163     int CheckpointSpan(int height) const
164     {
165         return height < nReduceBlocktimeHeight ? nCheckpointSpan : nRBTCheckpointSpan;
166     }
CoinbaseMaturityParams167     int CoinbaseMaturity(int height) const
168     {
169         return height < nReduceBlocktimeHeight ? nCoinbaseMaturity : nRBTCoinbaseMaturity;
170     }
MaxCheckpointSpanParams171     int MaxCheckpointSpan() const
172     {
173         return nCheckpointSpan <= nRBTCheckpointSpan ? nRBTCheckpointSpan : nCheckpointSpan;
174     }
175 };
176 } // namespace Consensus
177 
178 #endif // BITCOIN_CONSENSUS_PARAMS_H
179