Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2016 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 PIVX_CONSENSUS_PARAMS_H
7 : #define PIVX_CONSENSUS_PARAMS_H
8 :
9 : #include "amount.h"
10 : #include "libzerocoin/Params.h"
11 : #include "optional.h"
12 : #include "uint256.h"
13 : #include <map>
14 : #include <string>
15 :
16 : namespace Consensus {
17 :
18 : /**
19 : * Index into Params.vUpgrades and NetworkUpgradeInfo
20 : *
21 : * Being array indices, these MUST be numbered consecutively.
22 : *
23 : * The order of these indices MUST match the order of the upgrades on-chain, as
24 : * several functions depend on the enum being sorted.
25 : */
26 : enum UpgradeIndex : uint32_t {
27 : BASE_NETWORK,
28 : UPGRADE_POS,
29 : UPGRADE_POS_V2,
30 : UPGRADE_ZC,
31 : UPGRADE_ZC_V2,
32 : UPGRADE_BIP65,
33 : UPGRADE_ZC_PUBLIC,
34 : UPGRADE_V3_4,
35 : UPGRADE_V4_0,
36 : UPGRADE_V5_0,
37 : UPGRADE_V5_2,
38 : UPGRADE_V5_3,
39 : UPGRADE_V5_5,
40 : UPGRADE_V5_6,
41 : UPGRADE_V6_0,
42 : UPGRADE_TESTDUMMY,
43 : // NOTE: Also add new upgrades to NetworkUpgradeInfo in upgrades.cpp
44 : MAX_NETWORK_UPGRADES
45 : };
46 :
47 : struct NetworkUpgrade {
48 : /**
49 : * The first protocol version which will understand the new consensus rules
50 : */
51 : int nProtocolVersion;
52 :
53 : /**
54 : * Height of the first block for which the new consensus rules will be active
55 : */
56 : int nActivationHeight;
57 :
58 : /**
59 : * Special value for nActivationHeight indicating that the upgrade is always active.
60 : * This is useful for testing, as it means tests don't need to deal with the activation
61 : * process (namely, faking a chain of somewhat-arbitrary length).
62 : *
63 : * New blockchains that want to enable upgrade rules from the beginning can also use
64 : * this value. However, additional care must be taken to ensure the genesis block
65 : * satisfies the enabled rules.
66 : */
67 : static constexpr int ALWAYS_ACTIVE = 0;
68 :
69 : /**
70 : * Special value for nActivationHeight indicating that the upgrade will never activate.
71 : * This is useful when adding upgrade code that has a testnet activation height, but
72 : * should remain disabled on mainnet.
73 : */
74 : static constexpr int NO_ACTIVATION_HEIGHT = -1;
75 :
76 : /**
77 : * The hash of the block at height nActivationHeight, if known. This is set manually
78 : * after a network upgrade activates.
79 : *
80 : * We use this in IsInitialBlockDownload to detect whether we are potentially being
81 : * fed a fake alternate chain. We use NU activation blocks for this purpose instead of
82 : * the checkpoint blocks, because network upgrades (should) have significantly more
83 : * scrutiny than regular releases. nMinimumChainWork MUST be set to at least the chain
84 : * work of this block, otherwise this detection will have false positives.
85 : */
86 : Optional<uint256> hashActivationBlock;
87 : };
88 :
89 : enum LLMQType : uint8_t
90 : {
91 : LLMQ_NONE = 0xff,
92 :
93 : LLMQ_50_60 = 1, // 50 members, 30 (60%) threshold, one per hour
94 : LLMQ_400_60 = 2, // 400 members, 240 (60%) threshold, one every 12 hours
95 : LLMQ_400_85 = 3, // 400 members, 340 (85%) threshold, one every 24 hours
96 : LLMQ_20_60 = 4, // 20 members, 12 (60%) threshold, one per hour
97 :
98 : // for testing only
99 : LLMQ_TEST = 100, // 3 members, 2 (66%) threshold, one per hour. Params might differ when -llmqtestparams is used
100 : };
101 :
102 : // Configures a LLMQ and its DKG
103 : // See https://github.com/dashpay/dips/blob/master/dip-0006.md for more details
104 11927 : struct LLMQParams {
105 : LLMQType type;
106 :
107 : // not consensus critical, only used in logging, RPC and UI
108 : std::string name;
109 :
110 : // the size of the quorum, e.g. 50 or 400
111 : int size;
112 :
113 : // The minimum number of valid members after the DKK. If less members are determined valid, no commitment can be
114 : // created. Should be higher then the threshold to allow some room for failing nodes, otherwise quorum might end up
115 : // not being able to ever created a recovered signature if more nodes fail after the DKG
116 : int minSize;
117 :
118 : // The threshold required to recover a final signature. Should be at least 50%+1 of the quorum size. This value
119 : // also controls the size of the public key verification vector and has a large influence on the performance of
120 : // recovery. It also influences the amount of minimum messages that need to be exchanged for a single signing session.
121 : // This value has the most influence on the security of the quorum. The number of total malicious masternodes
122 : // required to negatively influence signing sessions highly correlates to the threshold percentage.
123 : int threshold;
124 :
125 : // The interval in number blocks for DKGs and the creation of LLMQs. If set to 60 for example, a DKG will start
126 : // every 60 blocks, which is approximately once every hour.
127 : int dkgInterval;
128 :
129 : // The number of blocks per phase in a DKG session. There are 6 phases plus the mining phase that need to be processed
130 : // per DKG. Set this value to a number of blocks so that each phase has enough time to propagate all required
131 : // messages to all members before the next phase starts. If blocks are produced too fast, whole DKG sessions will
132 : // fail.
133 : int dkgPhaseBlocks;
134 :
135 : // The starting block inside the DKG interval for when mining of commitments starts. The value is inclusive.
136 : // Starting from this block, the inclusion of (possibly null) commitments is enforced until the first non-null
137 : // commitment is mined. The chosen value should be at least 5 * dkgPhaseBlocks so that it starts right after the
138 : // finalization phase.
139 : int dkgMiningWindowStart;
140 :
141 : // The ending block inside the DKG interval for when mining of commitments ends. The value is inclusive.
142 : // Choose a value so that miners have enough time to receive the commitment and mine it. Also take into consideration
143 : // that miners might omit real commitments and revert to always including null commitments. The mining window should
144 : // be large enough so that other miners have a chance to produce a block containing a non-null commitment. The window
145 : // should at the same time not be too large so that not too much space is wasted with null commitments in case a DKG
146 : // session failed.
147 : int dkgMiningWindowEnd;
148 :
149 : // In the complaint phase, members will vote on other members being bad (missing valid contribution). If at least
150 : // dkgBadVotesThreshold have voted for another member to be bad, it will considered to be bad by all other members
151 : // as well. This serves as a protection against late-comers who send their contribution on the bring of
152 : // phase-transition, which would otherwise result in inconsistent views of the valid members set
153 : int dkgBadVotesThreshold;
154 :
155 : // Number of quorums to consider "active" for signing sessions
156 : int signingActiveQuorumCount;
157 :
158 : // Used for intra-quorum communication. This is the number of quorums for which we should keep old connections. This
159 : // should be at least one more then the active quorums set.
160 : int keepOldConnections;
161 :
162 : // How many members should we try to send all sigShares to before we give up.
163 : int recoveryMembers;
164 :
165 : // The limit of blocks up until where the dkg qfc will be accepted.
166 : int cacheDkgInterval;
167 : };
168 :
169 : /**
170 : * Parameters that influence chain consensus.
171 : */
172 : struct Params {
173 : uint256 hashGenesisBlock;
174 : bool fPowAllowMinDifficultyBlocks;
175 : bool fPowNoRetargeting;
176 : uint256 powLimit;
177 : uint256 posLimitV1;
178 : uint256 posLimitV2;
179 : int nBudgetCycleBlocks;
180 : int nBudgetFeeConfirmations;
181 : int nCoinbaseMaturity;
182 : int nFutureTimeDriftPoW;
183 : int nFutureTimeDriftPoS;
184 : CAmount nMaxMoneyOut;
185 : CAmount nMNCollateralAmt;
186 : int nMNCollateralMinConf;
187 : CAmount nMNBlockReward;
188 : CAmount nNewMNBlockReward;
189 : int64_t nProposalEstablishmentTime;
190 : int nStakeMinAge;
191 : int nStakeMinDepth;
192 : int64_t nTargetTimespan;
193 : int64_t nTargetTimespanV2;
194 : int64_t nTargetSpacing;
195 : int nTimeSlotLength;
196 : int nMaxProposalPayments;
197 :
198 : // spork keys
199 : std::string strSporkPubKey;
200 : std::string strSporkPubKeyOld;
201 : int64_t nTime_EnforceNewSporkKey;
202 : int64_t nTime_RejectOldSporkKey;
203 :
204 : // height-based activations
205 : int height_last_invalid_UTXO;
206 : int height_last_ZC_AccumCheckpoint;
207 : int height_last_ZC_WrappedSerials;
208 :
209 : // validation by-pass
210 : int64_t nPivxBadBlockTime;
211 : unsigned int nPivxBadBlockBits;
212 :
213 : // Map with network updates
214 : NetworkUpgrade vUpgrades[MAX_NETWORK_UPGRADES];
215 :
216 0 : int64_t TargetTimespan(const bool fV2 = true) const { return fV2 ? nTargetTimespanV2 : nTargetTimespan; }
217 0 : uint256 ProofOfStakeLimit(const bool fV2) const { return fV2 ? posLimitV2 : posLimitV1; }
218 12221153 : bool MoneyRange(const CAmount& nValue) const { return (nValue >= 0 && nValue <= nMaxMoneyOut); }
219 0 : bool IsTimeProtocolV2(const int nHeight) const { return NetworkUpgradeActive(nHeight, UPGRADE_V4_0); }
220 1086 : int MasternodeCollateralMinConf() const { return nMNCollateralMinConf; }
221 :
222 0 : int FutureBlockTimeDrift(const int nHeight) const
223 : {
224 : // PoS (TimeV2): 14 seconds
225 0 : if (IsTimeProtocolV2(nHeight)) return nTimeSlotLength - 1;
226 : // PoS (TimeV1): 3 minutes - PoW: 2 hours
227 0 : return (NetworkUpgradeActive(nHeight, UPGRADE_POS) ? nFutureTimeDriftPoS : nFutureTimeDriftPoW);
228 : }
229 :
230 0 : bool IsValidBlockTimeStamp(const int64_t nTime, const int nHeight) const
231 : {
232 : // Before time protocol V2, blocks can have arbitrary timestamps
233 0 : if (!IsTimeProtocolV2(nHeight)) return true;
234 : // Time protocol v2 requires time in slots
235 0 : return (nTime % nTimeSlotLength) == 0;
236 : }
237 :
238 2537 : bool HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime,
239 : const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const
240 : {
241 : // before stake modifier V2, we require the utxo to be nStakeMinAge old
242 2537 : if (!NetworkUpgradeActive(contextHeight, Consensus::UPGRADE_V3_4))
243 0 : return (utxoFromBlockTime + nStakeMinAge <= contextTime);
244 : // with stake modifier V2+, we require the utxo to be nStakeMinDepth deep in the chain
245 2537 : return (contextHeight - utxoFromBlockHeight >= nStakeMinDepth);
246 : }
247 :
248 :
249 : /*
250 : * (Legacy) Zerocoin consensus params
251 : */
252 : std::string ZC_Modulus; // parsed in Zerocoin_Params (either as hex or dec string)
253 : int ZC_MaxPublicSpendsPerTx;
254 : int ZC_MaxSpendsPerTx;
255 : int ZC_MinMintConfirmations;
256 : CAmount ZC_MinMintFee;
257 : int ZC_MinStakeDepth;
258 : int ZC_TimeStart;
259 : int ZC_HeightStart;
260 :
261 7278 : libzerocoin::ZerocoinParams* Zerocoin_Params(bool useModulusV1) const
262 : {
263 7278 : static CBigNum bnHexModulus = 0;
264 7278 : if (!bnHexModulus) bnHexModulus.SetHex(ZC_Modulus);
265 7278 : static libzerocoin::ZerocoinParams ZCParamsHex = libzerocoin::ZerocoinParams(bnHexModulus);
266 7278 : static CBigNum bnDecModulus = 0;
267 7278 : if (!bnDecModulus) bnDecModulus.SetDec(ZC_Modulus);
268 7278 : static libzerocoin::ZerocoinParams ZCParamsDec = libzerocoin::ZerocoinParams(bnDecModulus);
269 14556 : return (useModulusV1 ? &ZCParamsHex : &ZCParamsDec);
270 : }
271 :
272 : /**
273 : * Returns true if the given network upgrade is active as of the given block
274 : * height. Caller must check that the height is >= 0 (and handle unknown
275 : * heights).
276 : */
277 : bool NetworkUpgradeActive(int nHeight, Consensus::UpgradeIndex idx) const;
278 :
279 : // LLMQ
280 : std::map<LLMQType, LLMQParams> llmqs;
281 : Optional<LLMQParams> GetLLMQParams(uint8_t llmqtype) const;
282 : LLMQType llmqTypeChainLocks;
283 : };
284 : } // namespace Consensus
285 :
286 : #endif // PIVX_CONSENSUS_PARAMS_H
|