Skip to content

Commit 2621b7f

Browse files
committed
[Refactoring] Move ContextualCheckZerocoinStake() to kernel.cpp
And call it in CheckProofOfStake() which is called by AcceptBlock() instead of calling it directly from AcceptBlock().
1 parent 988b33d commit 2621b7f

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

src/kernel.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,33 @@ bool Stake(CStakeInput* stakeInput, unsigned int nBits, unsigned int nTimeBlockF
350350
return fSuccess;
351351
}
352352

353+
bool ContextualCheckZerocoinStake(int nPreviousBlockHeight, CStakeInput* stake)
354+
{
355+
if (nPreviousBlockHeight < Params().Zerocoin_Block_V2_Start())
356+
return error("%s: zPIV stake block is less than allowed start height", __func__);
357+
358+
if (CZPivStake* zPIV = dynamic_cast<CZPivStake*>(stake)) {
359+
CBlockIndex* pindexFrom = zPIV->GetIndexFrom();
360+
if (!pindexFrom)
361+
return error("%s: failed to get index associated with zPIV stake checksum", __func__);
362+
363+
if (chainActive.Height() - pindexFrom->nHeight < Params().Zerocoin_RequiredStakeDepth())
364+
return error("%s: zPIV stake does not have required confirmation depth. Current height %d, stakeInput height %d.", __func__, chainActive.Height(), pindexFrom->nHeight);
365+
366+
//The checksum needs to be the exact checksum from 200 blocks ago
367+
uint256 nCheckpoint200 = chainActive[nPreviousBlockHeight - Params().Zerocoin_RequiredStakeDepth()]->nAccumulatorCheckpoint;
368+
uint32_t nChecksum200 = ParseChecksum(nCheckpoint200, libzerocoin::AmountToZerocoinDenomination(zPIV->GetValue()));
369+
if (nChecksum200 != zPIV->GetChecksum())
370+
return error("%s: accumulator checksum is different than the block 200 blocks previous. stake=%d block200=%d", __func__, zPIV->GetChecksum(), nChecksum200);
371+
} else {
372+
return error("%s: dynamic_cast of stake ptr failed", __func__);
373+
}
374+
375+
return true;
376+
}
377+
353378
// Check kernel hash target and coinstake signature
354-
bool CheckProofOfStake(const CBlock block, uint256& hashProofOfStake, std::unique_ptr<CStakeInput>& stake)
379+
bool CheckProofOfStake(const CBlock block, uint256& hashProofOfStake, std::unique_ptr<CStakeInput>& stake, int nPreviousBlockHeight)
355380
{
356381
const CTransaction tx = block.vtx[1];
357382
if (!tx.IsCoinStake())
@@ -367,6 +392,9 @@ bool CheckProofOfStake(const CBlock block, uint256& hashProofOfStake, std::uniqu
367392
return error("%s: spend is using the wrong SpendType (%d)", __func__, (int)spend.getSpendType());
368393

369394
stake = std::unique_ptr<CStakeInput>(new CZPivStake(spend));
395+
396+
if (!ContextualCheckZerocoinStake(nPreviousBlockHeight, stake.get()))
397+
return error("%s: staked zPIV fails context checks", __func__);
370398
} else {
371399
// First try finding the previous transaction in database
372400
uint256 hashBlock;

src/kernel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ bool CheckStakeModifierCheckpoints(int nHeight, unsigned int nStakeModifierCheck
4343
// Get time weight using supplied timestamps
4444
int64_t GetWeight(int64_t nIntervalBeginning, int64_t nIntervalEnd);
4545

46+
bool ContextualCheckZerocoinStake(int nPreviousBlockHeight, CStakeInput* stake);
47+
4648
#endif // BITCOIN_KERNEL_H

src/main.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4691,31 +4691,6 @@ bool AcceptBlockHeader(const CBlock& block, CValidationState& state, CBlockIndex
46914691
return true;
46924692
}
46934693

4694-
bool ContextualCheckZerocoinStake(int nHeight, CStakeInput* stake)
4695-
{
4696-
if (nHeight < Params().Zerocoin_Block_V2_Start())
4697-
return error("%s: zPIV stake block is less than allowed start height", __func__);
4698-
4699-
if (CZPivStake* zPIV = dynamic_cast<CZPivStake*>(stake)) {
4700-
CBlockIndex* pindexFrom = zPIV->GetIndexFrom();
4701-
if (!pindexFrom)
4702-
return error("%s: failed to get index associated with zPIV stake checksum", __func__);
4703-
4704-
if (chainActive.Height() - pindexFrom->nHeight < Params().Zerocoin_RequiredStakeDepth())
4705-
return error("%s: zPIV stake does not have required confirmation depth. Current height %d, stakeInput height %d.", __func__, chainActive.Height(), pindexFrom->nHeight);
4706-
4707-
//The checksum needs to be the exact checksum from 200 blocks ago
4708-
uint256 nCheckpoint200 = chainActive[nHeight - Params().Zerocoin_RequiredStakeDepth()]->nAccumulatorCheckpoint;
4709-
uint32_t nChecksum200 = ParseChecksum(nCheckpoint200, libzerocoin::AmountToZerocoinDenomination(zPIV->GetValue()));
4710-
if (nChecksum200 != zPIV->GetChecksum())
4711-
return error("%s: accumulator checksum is different than the block 200 blocks previous. stake=%d block200=%d", __func__, zPIV->GetChecksum(), nChecksum200);
4712-
} else {
4713-
return error("%s: dynamic_cast of stake ptr failed", __func__);
4714-
}
4715-
4716-
return true;
4717-
}
4718-
47194694
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, CDiskBlockPos* dbp, bool fAlreadyCheckedBlock)
47204695
{
47214696
AssertLockHeld(cs_main);
@@ -4754,15 +4729,12 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
47544729
uint256 hashProofOfStake = 0;
47554730
unique_ptr<CStakeInput> stake;
47564731

4757-
if (!CheckProofOfStake(block, hashProofOfStake, stake))
4732+
if (!CheckProofOfStake(block, hashProofOfStake, stake, pindexPrev->nHeight))
47584733
return state.DoS(100, error("%s: proof of stake check failed", __func__));
47594734

47604735
if (!stake)
47614736
return error("%s: null stake ptr", __func__);
47624737

4763-
if (stake->IsZPIV() && !ContextualCheckZerocoinStake(pindexPrev->nHeight, stake.get()))
4764-
return state.DoS(100, error("%s: staked zPIV fails context checks", __func__));
4765-
47664738
uint256 hash = block.GetHash();
47674739
if(!mapProofOfStake.count(hash)) // add to mapProofOfStake
47684740
mapProofOfStake.insert(make_pair(hash, hashProofOfStake));

0 commit comments

Comments
 (0)