Skip to content

Commit 1504bf9

Browse files
committed
[Tests] Use V2 stake modifiers on regtest (issue #1309)
1 parent 3ad11a7 commit 1504bf9

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

src/chainparams.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ libzerocoin::ZerocoinParams* CChainParams::Zerocoin_Params(bool useModulusV1) co
123123
bool CChainParams::HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime,
124124
const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const
125125
{
126-
// before stake modifier V2, the age required was 60 * 60 (1 hour). Not required for regtest
126+
// before stake modifier V2, the age required was 60 * 60 (1 hour).
127127
if (!IsStakeModifierV2(contextHeight))
128-
return NetworkID() == CBaseChainParams::REGTEST || (utxoFromBlockTime + nStakeMinAge <= contextTime);
128+
return (utxoFromBlockTime + nStakeMinAge <= contextTime);
129129

130130
// after stake modifier V2, we require the utxo to be nStakeMinDepth deep in the chain
131131
return (contextHeight - utxoFromBlockHeight >= nStakeMinDepth);
@@ -457,7 +457,7 @@ class CRegTestParams : public CTestNetParams
457457
nBlockRecalculateAccumulators = 999999999; // Trigger a recalculation of accumulators
458458
nBlockFirstFraudulent = 999999999; // First block that bad serials emerged
459459
nBlockLastGoodCheckpoint = 999999999; // Last valid accumulator checkpoint
460-
nBlockStakeModifierlV2 = nLastPOWBlock + 1; // start with modifier V2 on testnet
460+
nBlockStakeModifierlV2 = nLastPOWBlock + 1; // start with modifier V2 on regtest
461461
nBlockTimeProtocolV2 = 999999999;
462462

463463
nMintRequiredConfirmations = 10;

src/kernel.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ bool initStakeInput(const CBlock& block, std::unique_ptr<CStakeInput>& stake, in
181181
uint256 ComputeStakeModifier(const CBlockIndex* pindexPrev, const uint256& kernel)
182182
{
183183
// genesis block's modifier is 0
184-
// all block's modifiers are 0 on regtest
185-
if (!pindexPrev || Params().IsRegTestNet())
186-
return uint256();
184+
if (!pindexPrev) return uint256();
187185

188186
CHashWriter ss(SER_GETHASH, 0);
189187
ss << kernel;
@@ -372,11 +370,6 @@ bool GetOldModifier(const uint256& hashBlockFrom, uint64_t& nStakeModifier)
372370
return error("%s : block not indexed", __func__);
373371
const CBlockIndex* pindexFrom = mapBlockIndex[hashBlockFrom];
374372
int64_t nStakeModifierTime = pindexFrom->GetBlockTime();
375-
// Fixed stake modifier only for regtest
376-
if (Params().IsRegTestNet()) {
377-
nStakeModifier = pindexFrom->nStakeModifier;
378-
return true;
379-
}
380373
const CBlockIndex* pindex = pindexFrom;
381374
CBlockIndex* pindexNext = chainActive[pindex->nHeight + 1];
382375

@@ -413,10 +406,6 @@ bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeMod
413406
nStakeModifier = 0;
414407
fGeneratedStakeModifier = false;
415408

416-
// modifier 0 on RegTest
417-
if (Params().IsRegTestNet()) {
418-
return true;
419-
}
420409
if (!pindexPrev) {
421410
fGeneratedStakeModifier = true;
422411
return true; // genesis block's modifier is 0
@@ -507,3 +496,4 @@ bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeMod
507496
fGeneratedStakeModifier = true;
508497
return true;
509498
}
499+

test/functional/mining_pos_fakestake.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,21 @@ def fake_stake(self,
172172
fDoubleSpend: (bool) if true, stake input is double spent in block.vtx
173173
:return:
174174
"""
175-
# Get block number, block time and prevBlock hash
175+
def get_prev_modifier(prevBlockHash):
176+
prevBlock = self.nodes[1].getblock(prevBlockHash)
177+
if prevBlock['height'] > 250:
178+
return prevBlock['stakeModifier']
179+
return "0"
180+
181+
# Get block number, block time and prevBlock hash and modifier
176182
currHeight = self.nodes[1].getblockcount()
177183
isMainChain = (nHeight == -1)
178184
chainName = "main" if isMainChain else "forked"
179185
nTime = self.mocktime
180186
if isMainChain:
181187
nHeight = currHeight + 1
182188
prevBlockHash = self.nodes[1].getblockhash(nHeight - 1)
189+
prevModifier = get_prev_modifier(prevBlockHash)
183190
nTime += (nHeight - currHeight) * 60
184191

185192
# New block hash, coinstake input and list of txes
@@ -199,6 +206,7 @@ def fake_stake(self,
199206
nHeight += 1
200207
nTime += 60
201208
prevBlockHash = bHash
209+
prevModifier = get_prev_modifier(prevBlockHash)
202210

203211
stakeInputs = self.get_prevouts(1, staking_utxo_list, False, nHeight - 1)
204212
# Update stake inputs for second block sent on forked chain (must stake the same input)
@@ -211,7 +219,7 @@ def fake_stake(self,
211219
block_txes = self.make_txes(1, spending_prevouts, self.DUMMY_KEY.get_pubkey())
212220

213221
# Stake the spam block
214-
block = self.stake_block(1, nHeight, prevBlockHash, stakeInputs,
222+
block = self.stake_block(1, nHeight, prevBlockHash, prevModifier, stakeInputs,
215223
nTime, "", block_txes, fDoubleSpend)
216224
# Log stake input
217225
prevout = COutPoint()

test/functional/test_framework/messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def rehash(self):
516516
return self.sha256
517517

518518
# PIVX
519-
def solve_stake(self, stakeInputs):
519+
def solve_stake(self, stakeInputs, prevModifier):
520520
target0 = uint256_from_compact(self.nBits)
521521
loop = True
522522
while loop:
@@ -525,9 +525,9 @@ def solve_stake(self, stakeInputs):
525525
target = int(target0 * nvalue / 100) % 2**256
526526
data = b""
527527
# always modifier V2 (256 bits) on regtest
528-
data += ser_uint256(0)
528+
data += ser_uint256(prevModifier)
529529
data += struct.pack("<I", prevTime)
530-
# prevout is CStake uniquenessfor zPoS is provided as stakeMap key (instead of it being an COutPoint)
530+
# prevout is CStake uniqueness
531531
data += uniqueness
532532
data += struct.pack("<I", self.nTime)
533533
posHash = uint256_from_str(hash256(data))

test/functional/test_framework/test_framework.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ def make_txes(self, node_id, spendingPrevOuts, to_pubKey):
806806
def stake_block(self, node_id,
807807
nHeight,
808808
prevHhash,
809+
prevModifier,
809810
stakeableUtxos,
810811
startTime=None,
811812
privKeyWIF=None,
@@ -815,6 +816,7 @@ def stake_block(self, node_id,
815816
:param node_id: (int) index of the CTestNode used as rpc connection. Must own stakeableUtxos.
816817
nHeight: (int) height of the block being produced
817818
prevHash: (string) hex string of the previous block hash
819+
prevModifier (string) hex string of the previous block stake modifier
818820
stakeableUtxos: ({bytes --> (int, bytes, int)} dictionary)
819821
maps CStake "uniqueness" (i.e. serialized COutPoint -or hash stake, for zpiv-)
820822
to (amount, prevScript, timeBlockFrom).
@@ -840,11 +842,10 @@ def stake_block(self, node_id,
840842
block = create_block(int(prevHhash, 16), coinbaseTx, nTime)
841843

842844
# Find valid kernel hash - iterates stakeableUtxos, then block.nTime
843-
block.solve_stake(stakeableUtxos)
845+
block.solve_stake(stakeableUtxos, int(prevModifier, 16))
844846

845847
# Check if this is a zPoS block or regular/cold stake - sign stake tx
846848
block_sig_key = CECKey()
847-
prevout = None
848849
isZPoS = is_zerocoin(block.prevoutStake)
849850
if isZPoS:
850851
# !TODO: remove me
@@ -915,7 +916,16 @@ def stake_next_block(self, node_id,
915916
assert_greater_than(len(self.nodes), node_id)
916917
nHeight = self.nodes[node_id].getblockcount()
917918
prevHhash = self.nodes[node_id].getblockhash(nHeight)
918-
return self.stake_block(node_id, nHeight+1, prevHhash, stakeableUtxos, btime, privKeyWIF, vtx, fDoubleSpend)
919+
prevModifier = self.nodes[node_id].getblock(prevHhash)['stakeModifier']
920+
return self.stake_block(node_id,
921+
nHeight+1,
922+
prevHhash,
923+
prevModifier,
924+
stakeableUtxos,
925+
btime,
926+
privKeyWIF,
927+
vtx,
928+
fDoubleSpend)
919929

920930

921931
def check_tx_in_chain(self, node_id, txid):

0 commit comments

Comments
 (0)