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 :
97 : // for testing only
98 : LLMQ_TEST = 100, // 3 members, 2 (66%) threshold, one per hour. Params might differ when -llmqtestparams is used
99 : };
100 :
101 : // Configures a LLMQ and its DKG
102 : // See https://github.com/dashpay/dips/blob/master/dip-0006.md for more details
103 11522 : struct LLMQParams {
104 : LLMQType type;
105 :
106 : // not consensus critical, only used in logging, RPC and UI
107 : std::string name;
108 :
109 : // the size of the quorum, e.g. 50 or 400
110 : int size;
111 :
112 : // The minimum number of valid members after the DKK. If less members are determined valid, no commitment can be
113 : // created. Should be higher then the threshold to allow some room for failing nodes, otherwise quorum might end up
114 : // not being able to ever created a recovered signature if more nodes fail after the DKG
115 : int minSize;
116 :
117 : // The threshold required to recover a final signature. Should be at least 50%+1 of the quorum size. This value
118 : // also controls the size of the public key verification vector and has a large influence on the performance of
119 : // recovery. It also influences the amount of minimum messages that need to be exchanged for a single signing session.
120 : // This value has the most influence on the security of the quorum. The number of total malicious masternodes
121 : // required to negatively influence signing sessions highly correlates to the threshold percentage.
122 : int threshold;
123 :
124 : // The interval in number blocks for DKGs and the creation of LLMQs. If set to 60 for example, a DKG will start
125 : // every 60 blocks, which is approximately once every hour.
126 : int dkgInterval;
127 :
128 : // The number of blocks per phase in a DKG session. There are 6 phases plus the mining phase that need to be processed
129 : // per DKG. Set this value to a number of blocks so that each phase has enough time to propagate all required
130 : // messages to all members before the next phase starts. If blocks are produced too fast, whole DKG sessions will
131 : // fail.
132 : int dkgPhaseBlocks;
133 :
134 : // The starting block inside the DKG interval for when mining of commitments starts. The value is inclusive.
135 : // Starting from this block, the inclusion of (possibly null) commitments is enforced until the first non-null
136 : // commitment is mined. The chosen value should be at least 5 * dkgPhaseBlocks so that it starts right after the
137 : // finalization phase.
138 : int dkgMiningWindowStart;
139 :
140 : // The ending block inside the DKG interval for when mining of commitments ends. The value is inclusive.
141 : // Choose a value so that miners have enough time to receive the commitment and mine it. Also take into consideration
142 : // that miners might omit real commitments and revert to always including null commitments. The mining window should
143 : // be large enough so that other miners have a chance to produce a block containing a non-null commitment. The window
144 : // should at the same time not be too large so that not too much space is wasted with null commitments in case a DKG
145 : // session failed.
146 : int dkgMiningWindowEnd;
147 :
148 : // In the complaint phase, members will vote on other members being bad (missing valid contribution). If at least
149 : // dkgBadVotesThreshold have voted for another member to be bad, it will considered to be bad by all other members
150 : // as well. This serves as a protection against late-comers who send their contribution on the bring of
151 : // phase-transition, which would otherwise result in inconsistent views of the valid members set
152 : int dkgBadVotesThreshold;
153 :
154 : // Number of quorums to consider "active" for signing sessions
155 : int signingActiveQuorumCount;
156 :
157 : // Used for intra-quorum communication. This is the number of quorums for which we should keep old connections. This
158 : // should be at least one more then the active quorums set.
159 : int keepOldConnections;
160 :
161 : // How many members should we try to send all sigShares to before we give up.
162 : int recoveryMembers;
163 :
164 : // The limit of blocks up until where the dkg qfc will be accepted.
165 : int cacheDkgInterval;
166 : };
167 :
168 : /**
169 : * Parameters that influence chain consensus.
170 : */
171 : struct Params {
172 : uint256 hashGenesisBlock;
173 : bool fPowAllowMinDifficultyBlocks;
174 : bool fPowNoRetargeting;
175 : uint256 powLimit;
176 : uint256 posLimitV1;
177 : uint256 posLimitV2;
178 : int nBudgetCycleBlocks;
179 : int nBudgetFeeConfirmations;
180 : int nCoinbaseMaturity;
181 : int nFutureTimeDriftPoW;
182 : int nFutureTimeDriftPoS;
183 : CAmount nMaxMoneyOut;
184 : CAmount nMNCollateralAmt;
185 : int nMNCollateralMinConf;
186 : CAmount nMNBlockReward;
187 : CAmount nNewMNBlockReward;
188 : int64_t nProposalEstablishmentTime;
189 : int nStakeMinAge;
190 : int nStakeMinDepth;
191 : int64_t nTargetTimespan;
192 : int64_t nTargetTimespanV2;
193 : int64_t nTargetSpacing;
194 : int nTimeSlotLength;
195 : int nMaxProposalPayments;
196 :
197 : // spork keys
198 : std::string strSporkPubKey;
199 : std::string strSporkPubKeyOld;
200 : int64_t nTime_EnforceNewSporkKey;
201 : int64_t nTime_RejectOldSporkKey;
202 :
203 : // height-based activations
204 : int height_last_invalid_UTXO;
205 : int height_last_ZC_AccumCheckpoint;
206 : int height_last_ZC_WrappedSerials;
207 :
208 : // validation by-pass
209 : int64_t nPivxBadBlockTime;
210 : unsigned int nPivxBadBlockBits;
211 :
212 : // Map with network updates
213 : NetworkUpgrade vUpgrades[MAX_NETWORK_UPGRADES];
214 :
215 0 : int64_t TargetTimespan(const bool fV2 = true) const { return fV2 ? nTargetTimespanV2 : nTargetTimespan; }
216 0 : uint256 ProofOfStakeLimit(const bool fV2) const { return fV2 ? posLimitV2 : posLimitV1; }
217 13994299 : bool MoneyRange(const CAmount& nValue) const { return (nValue >= 0 && nValue <= nMaxMoneyOut); }
218 0 : bool IsTimeProtocolV2(const int nHeight) const { return NetworkUpgradeActive(nHeight, UPGRADE_V4_0); }
219 1106 : int MasternodeCollateralMinConf() const { return nMNCollateralMinConf; }
220 :
221 0 : int FutureBlockTimeDrift(const int nHeight) const
222 : {
223 : // PoS (TimeV2): 14 seconds
224 0 : if (IsTimeProtocolV2(nHeight)) return nTimeSlotLength - 1;
225 : // PoS (TimeV1): 3 minutes - PoW: 2 hours
226 0 : return (NetworkUpgradeActive(nHeight, UPGRADE_POS) ? nFutureTimeDriftPoS : nFutureTimeDriftPoW);
227 : }
228 :
229 0 : bool IsValidBlockTimeStamp(const int64_t nTime, const int nHeight) const
230 : {
231 : // Before time protocol V2, blocks can have arbitrary timestamps
232 0 : if (!IsTimeProtocolV2(nHeight)) return true;
233 : // Time protocol v2 requires time in slots
234 0 : return (nTime % nTimeSlotLength) == 0;
235 : }
236 :
237 2537 : bool HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime,
238 : const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const
239 : {
240 : // before stake modifier V2, we require the utxo to be nStakeMinAge old
241 2537 : if (!NetworkUpgradeActive(contextHeight, Consensus::UPGRADE_V3_4))
242 0 : return (utxoFromBlockTime + nStakeMinAge <= contextTime);
243 : // with stake modifier V2+, we require the utxo to be nStakeMinDepth deep in the chain
244 2537 : return (contextHeight - utxoFromBlockHeight >= nStakeMinDepth);
245 : }
246 :
247 :
248 : /*
249 : * (Legacy) Zerocoin consensus params
250 : */
251 : std::string ZC_Modulus; // parsed in Zerocoin_Params (either as hex or dec string)
252 : int ZC_MaxPublicSpendsPerTx;
253 : int ZC_MaxSpendsPerTx;
254 : int ZC_MinMintConfirmations;
255 : CAmount ZC_MinMintFee;
256 : int ZC_MinStakeDepth;
257 : int ZC_TimeStart;
258 : int ZC_HeightStart;
259 :
260 6718 : libzerocoin::ZerocoinParams* Zerocoin_Params(bool useModulusV1) const
261 : {
262 6718 : static CBigNum bnHexModulus = 0;
263 6718 : if (!bnHexModulus) bnHexModulus.SetHex(ZC_Modulus);
264 6718 : static libzerocoin::ZerocoinParams ZCParamsHex = libzerocoin::ZerocoinParams(bnHexModulus);
265 6718 : static CBigNum bnDecModulus = 0;
266 6718 : if (!bnDecModulus) bnDecModulus.SetDec(ZC_Modulus);
267 6718 : static libzerocoin::ZerocoinParams ZCParamsDec = libzerocoin::ZerocoinParams(bnDecModulus);
268 13436 : return (useModulusV1 ? &ZCParamsHex : &ZCParamsDec);
269 : }
270 :
271 : /**
272 : * Returns true if the given network upgrade is active as of the given block
273 : * height. Caller must check that the height is >= 0 (and handle unknown
274 : * heights).
275 : */
276 : bool NetworkUpgradeActive(int nHeight, Consensus::UpgradeIndex idx) const;
277 :
278 : // LLMQ
279 : std::map<LLMQType, LLMQParams> llmqs;
280 : Optional<LLMQParams> GetLLMQParams(uint8_t llmqtype) const;
281 : LLMQType llmqTypeChainLocks;
282 : };
283 : } // namespace Consensus
284 :
285 : #endif // PIVX_CONSENSUS_PARAMS_H
|