Skip to content

Commit 64465b7

Browse files
committed
[Refactor] Consensus: Stake Modifier V2
1 parent 8083725 commit 64465b7

File tree

8 files changed

+22
-25
lines changed

8 files changed

+22
-25
lines changed

src/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ class CDiskBlockIndex : public CBlockIndex
554554
READWRITE(nFlags);
555555

556556
// v1/v2 modifier selection.
557-
if (!Params().IsStakeModifierV2(nHeight)) {
557+
if (!Params().GetConsensus().IsStakeModifierV2(nHeight)) {
558558
READWRITE(nStakeModifier);
559559
} else {
560560
READWRITE(nStakeModifierV2);

src/chainparams.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,6 @@ libzerocoin::ZerocoinParams* CChainParams::Zerocoin_Params(bool useModulusV1) co
152152
return &ZCParamsDec;
153153
}
154154

155-
bool CChainParams::HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime,
156-
const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const
157-
{
158-
// before stake modifier V2, the age required was 60 * 60 (1 hour).
159-
if (!IsStakeModifierV2(contextHeight))
160-
return (utxoFromBlockTime + consensus.nStakeMinAge <= contextTime);
161-
162-
// after stake modifier V2, we require the utxo to be nStakeMinDepth deep in the chain
163-
return (contextHeight - utxoFromBlockHeight >= consensus.nStakeMinDepth);
164-
}
165-
166155
class CMainParams : public CChainParams
167156
{
168157
public:
@@ -189,6 +178,7 @@ class CMainParams : public CChainParams
189178
// height based activations
190179
consensus.height_last_PoW = 259200;
191180
consensus.height_start_BIP65 = 1808634; // 82629b7a9978f5c7ea3f70a12db92633a7d2e436711500db28b97efd48b1e527
181+
consensus.height_start_StakeModifierV2 = 1967000;
192182
consensus.height_start_TimeProtoV2 = 2153200; // TimeProtocolV2, Blocks V7 and newMessageSignatures
193183

194184
/**
@@ -232,7 +222,6 @@ class CMainParams : public CChainParams
232222
nBlockDoubleAccumulated = 1050010;
233223
nEnforceNewSporkKey = 1566860400; //!> Sporks signed after Monday, August 26, 2019 11:00:00 PM GMT must use the new spork key
234224
nRejectOldSporkKey = 1569538800; //!> Fully reject old spork key after Thursday, September 26, 2019 11:00:00 PM GMT
235-
nBlockStakeModifierlV2 = 1967000;
236225

237226
// Public coin spend enforcement
238227
nPublicZCSpends = 1880000;
@@ -335,6 +324,7 @@ class CTestNetParams : public CMainParams
335324
// height based activations
336325
consensus.height_last_PoW = 200;
337326
consensus.height_start_BIP65 = 851019;
327+
consensus.height_start_StakeModifierV2 = 1214000;
338328
consensus.height_start_TimeProtoV2 = 1347000; // TimeProtocolV2, Blocks V7 and newMessageSignatures
339329

340330
/**
@@ -374,7 +364,6 @@ class CTestNetParams : public CMainParams
374364
nBlockZerocoinV2 = 444020; //!> The block that zerocoin v2 becomes active
375365
nEnforceNewSporkKey = 1566860400; //!> Sporks signed after Monday, August 26, 2019 11:00:00 PM GMT must use the new spork key
376366
nRejectOldSporkKey = 1569538800; //!> Reject old spork key after Thursday, September 26, 2019 11:00:00 PM GMT
377-
nBlockStakeModifierlV2 = 1214000;
378367

379368
// Public coin spend enforcement
380369
nPublicZCSpends = 1106100;
@@ -461,6 +450,7 @@ class CRegTestParams : public CTestNetParams
461450
// height based activations
462451
consensus.height_last_PoW = 250;
463452
consensus.height_start_BIP65 = 851019; // Not defined for regtest. Inherit TestNet value.
453+
consensus.height_start_StakeModifierV2 = consensus.height_last_PoW + 1; // start with modifier V2 on regtest
464454
consensus.height_start_TimeProtoV2 = 999999999;
465455

466456

@@ -494,7 +484,6 @@ class CRegTestParams : public CTestNetParams
494484
nBlockRecalculateAccumulators = 999999999; // Trigger a recalculation of accumulators
495485
nBlockFirstFraudulent = 999999999; // First block that bad serials emerged
496486
nBlockLastGoodCheckpoint = 999999999; // Last valid accumulator checkpoint
497-
nBlockStakeModifierlV2 = consensus.height_last_PoW + 1; // start with modifier V2 on regtest
498487

499488
nMintRequiredConfirmations = 10;
500489
nZerocoinRequiredStakeDepth = nMintRequiredConfirmations;

src/chainparams.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ class CChainParams
7676
bool SkipProofOfWorkCheck() const { return fSkipProofOfWorkCheck; }
7777
/** Make standard checks */
7878
bool RequireStandard() const { return fRequireStandard; }
79-
/** returns the coinstake maturity (min depth required) **/
80-
bool HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime, const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const;
8179

8280
/** The masternode count that we will allow the see-saw reward payments to be off by */
8381
int MasternodeCountDrift() const { return nMasternodeCountDrift; }
@@ -132,7 +130,6 @@ class CChainParams
132130
int Zerocoin_StartTime() const { return nZerocoinStartTime; }
133131
int Block_Enforce_Invalid() const { return nBlockEnforceInvalidUTXO; }
134132
int Zerocoin_Block_V2_Start() const { return nBlockZerocoinV2; }
135-
bool IsStakeModifierV2(const int nHeight) const { return nHeight >= nBlockStakeModifierlV2; }
136133
int NewSigsActive(const int nHeight) const { return nHeight >= nBlockEnforceNewMessageSignatures; }
137134
int Block_V7_StartHeight() const { return nBlockV7StartHeight; }
138135

@@ -208,7 +205,6 @@ class CChainParams
208205
int nBlockZerocoinV2;
209206
int nBlockDoubleAccumulated;
210207
int nPublicZCSpends;
211-
int nBlockStakeModifierlV2;
212208
int nBlockEnforceNewMessageSignatures;
213209
int nBlockV7StartHeight;
214210
int nBlockLastAccumulatorCheckpoint;

src/consensus/params.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ struct Params {
3636
// Height-based activations
3737
int height_last_PoW;
3838
int height_start_BIP65;
39+
int height_start_StakeModifierV2;
3940
int height_start_TimeProtoV2;
4041

4142
int64_t TargetTimespan(const bool fV2 = true) const { return fV2 ? nTargetTimespanV2 : nTargetTimespan; }
4243
uint256 ProofOfStakeLimit(const bool fV2) const { return fV2 ? posLimitV2 : posLimitV1; }
4344
bool MoneyRange(const CAmount& nValue) const { return (nValue >= 0 && nValue <= nMaxMoneyOut); }
4445
bool IsTimeProtocolV2(const int nHeight) const { return nHeight >= height_start_TimeProtoV2; }
46+
bool IsStakeModifierV2(const int nHeight) const { return nHeight >= height_start_StakeModifierV2; }
4547

4648
int FutureBlockTimeDrift(const int nHeight) const
4749
{
@@ -58,6 +60,16 @@ struct Params {
5860
// Time protocol v2 requires time in slots
5961
return (nTime % nTimeSlotLength) == 0;
6062
}
63+
64+
bool HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime,
65+
const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const
66+
{
67+
// before stake modifier V2, we require the utxo to be nStakeMinAge old
68+
if (!IsStakeModifierV2(contextHeight))
69+
return (utxoFromBlockTime + nStakeMinAge <= contextTime);
70+
// with stake modifier V2+, we require the utxo to be nStakeMinDepth deep in the chain
71+
return (contextHeight - utxoFromBlockHeight >= nStakeMinDepth);
72+
}
6173
};
6274
} // namespace Consensus
6375

src/kernel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ bool GetHashProofOfStake(const CBlockIndex* pindexPrev, CStakeInput* stake, cons
2929
CDataStream modifier_ss(SER_GETHASH, 0);
3030

3131
// Hash the modifier
32-
if (!Params().IsStakeModifierV2(pindexPrev->nHeight + 1)) {
32+
if (!Params().GetConsensus().IsStakeModifierV2(pindexPrev->nHeight + 1)) {
3333
// Modifier v1
3434
uint64_t nStakeModifier = 0;
3535
if (!GetOldStakeModifier(stake, nStakeModifier))
@@ -107,7 +107,7 @@ bool CheckProofOfStake(const CBlock& block, uint256& hashProofOfStake, std::uniq
107107

108108
if (!txin.IsZerocoinSpend() && nPreviousBlockHeight >= Params().Zerocoin_Block_Public_Spend_Enabled() - 1) {
109109
//check for maturity (min age/depth) requirements
110-
if (!Params().HasStakeMinAgeOrDepth(nPreviousBlockHeight+1, nTxTime, nBlockFromHeight, nBlockFromTime))
110+
if (!Params().GetConsensus().HasStakeMinAgeOrDepth(nPreviousBlockHeight+1, nTxTime, nBlockFromHeight, nBlockFromTime))
111111
return error("%s : min age violation - height=%d - nTimeTx=%d, nTimeBlockFrom=%d, nHeightBlockFrom=%d",
112112
__func__, nPreviousBlockHeight, nTxTime, nBlockFromTime, nBlockFromHeight);
113113
}
@@ -187,7 +187,7 @@ uint256 ComputeStakeModifier(const CBlockIndex* pindexPrev, const uint256& kerne
187187
ss << kernel;
188188

189189
// switch with old modifier on upgrade block
190-
if (!Params().IsStakeModifierV2(pindexPrev->nHeight + 1))
190+
if (!Params().GetConsensus().IsStakeModifierV2(pindexPrev->nHeight + 1))
191191
ss << pindexPrev->nStakeModifier;
192192
else
193193
ss << pindexPrev->nStakeModifierV2;

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3912,7 +3912,7 @@ CBlockIndex* AddToBlockIndex(const CBlock& block)
39123912
pindexNew->hashProofOfStake = mapProofOfStake[hash];
39133913
}
39143914

3915-
if (!Params().IsStakeModifierV2(pindexNew->nHeight)) {
3915+
if (!Params().GetConsensus().IsStakeModifierV2(pindexNew->nHeight)) {
39163916
uint64_t nStakeModifier = 0;
39173917
bool fGeneratedStakeModifier = false;
39183918
if (!ComputeNextStakeModifier(pindexNew->pprev, nStakeModifier, fGeneratedStakeModifier))

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
161161
if (!GetHashProofOfStake(blockindex->pprev, stake.get(), nTxTime, false, hashProofOfStakeRet))
162162
throw JSONRPCError(RPC_INTERNAL_ERROR, "Cannot get proof of stake hash");
163163

164-
std::string stakeModifier = (Params().IsStakeModifierV2(blockindex->nHeight) ?
164+
std::string stakeModifier = (Params().GetConsensus().IsStakeModifierV2(blockindex->nHeight) ?
165165
blockindex->nStakeModifierV2.GetHex() :
166166
strprintf("%016x", blockindex->nStakeModifier));
167167
result.push_back(Pair("stakeModifier", stakeModifier));

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
255255
pindexNew->nMint = diskindex.nMint;
256256
pindexNew->nMoneySupply = diskindex.nMoneySupply;
257257
pindexNew->nFlags = diskindex.nFlags;
258-
if (!Params().IsStakeModifierV2(pindexNew->nHeight)) {
258+
if (!Params().GetConsensus().IsStakeModifierV2(pindexNew->nHeight)) {
259259
pindexNew->nStakeModifier = diskindex.nStakeModifier;
260260
} else {
261261
pindexNew->nStakeModifierV2 = diskindex.nStakeModifierV2;

0 commit comments

Comments
 (0)