11// Copyright (c) 2012-2020 The Peercoin developers
22// Copyright (c) 2015-2019 The PIVX developers
3- // Copyright (c) 2018-2021 John "ComputerCraftr" Studnicka
3+ // Copyright (c) 2018-2022 John "ComputerCraftr" Studnicka
44// Distributed under the MIT/X11 software license, see the accompanying
55// file COPYING or http://www.opensource.org/licenses/mit-license.php.
66
7+ #include < chain.h>
78#include < chainparams.h>
89#include < consensus/validation.h>
910#include < hash.h>
1718#include < util/system.h>
1819#include < validation.h>
1920
21+ #include < unordered_map>
22+
2023// Hard checkpoints of stake modifiers to ensure they are deterministic
21- static const std::map <int , unsigned int > mapStakeModifierCheckpoints = {
24+ static const std::unordered_map <int , unsigned int > mapStakeModifierCheckpoints = {
2225 { 0 , 0x0e00670bu },
2326 { 50000 , 0xcbe5a9b4u },
2427 { 100000 , 0xbd074053u },
@@ -31,7 +34,7 @@ static const std::map<int, unsigned int> mapStakeModifierCheckpoints = {
3134 { 450000 , 0xc85e93eau },
3235};
3336
34- static const std::map <int , unsigned int > mapStakeModifierTestnetCheckpoints = {
37+ static const std::unordered_map <int , unsigned int > mapStakeModifierTestnetCheckpoints = {
3538 { 0 , 0x0e00670bu },
3639 { 50000 , 0x4af2e306u },
3740 { 100000 , 0xd9e06043u },
@@ -573,15 +576,10 @@ bool CheckStakeKernelHash(const unsigned int& nBits, const CBlockIndex* pindexPr
573576 // nHashDrift should be <= MAX_FUTURE_BLOCK_TIME otherwise we risk creating a block which will be rejected due to nTimeTx being too far in the future
574577 bool fSuccess = false ;
575578 unsigned int nTryTime = 0 ;
576- const int nHeightStart = nHeightCurrent - 1 ;
577579 const int iteration = params.nStakeTimestampMask + 1 ; // 16 second time slots for 0xf masked time
578580 assert ((nHashDrift & params.nStakeTimestampMask ) == 0 );
579581 for (int i = nHashDrift; i >= 0 ; i -= iteration) // iterate the hashing
580582 {
581- // New block came in, move on
582- if (::ChainActive ().Height () != nHeightStart)
583- break ;
584-
585583 // Hash this iteration - start at nHashDrift and work backwards to nTimeTx
586584 nTryTime = nTimeTx + i; // nTimeTx + nHashDrift - i;
587585 hashProofOfStake = stakeHash (nTryTime, ss, prevout.n , prevout.hash , nTimeBlockFrom, true );
@@ -615,7 +613,7 @@ bool CheckStakeKernelHash(const unsigned int& nBits, const CBlockIndex* pindexPr
615613}
616614
617615// Check kernel hash target and coinstake signature
618- bool CheckProofOfStake (BlockValidationState& state, const CCoinsViewCache& view, const CBlockIndex* pindexPrev, const CTransactionRef& tx, const unsigned int & nBits, unsigned int nTimeTx, uint256& hashProofOfStake)
616+ bool CheckProofOfStake (BlockValidationState& state, const CCoinsViewCache& view, const CChain& active_chain, const CBlockIndex* pindexPrev, const CTransactionRef& tx, const unsigned int & nBits, unsigned int nTimeTx, uint256& hashProofOfStake)
619617{
620618 if (!tx->IsCoinStake ())
621619 return error (" CheckProofOfStake() : called on non-coinstake %s" , tx->GetHash ().ToString ());
@@ -639,15 +637,15 @@ bool CheckProofOfStake(BlockValidationState& state, const CCoinsViewCache& view,
639637
640638 // Read txPrev and header of its block
641639 // const CBlockIndex* pindexFrom = LookupBlockIndex(hashBlock);
642- const CBlockIndex* pindexFrom = :: ChainActive () [coin.nHeight ];
640+ const CBlockIndex* pindexFrom = active_chain [coin.nHeight ];
643641 if (!pindexFrom)
644642 return error (" CheckProofOfStake() : block index not found" );
645643
646644 // Verify signature
647645 {
648646 int nIn = 0 ;
649647 // const CTxOut& prevTxOut = txPrev->vout[tx->vin[nIn].prevout.n];
650- TransactionSignatureChecker checker (&(*tx), nIn, coin.out .nValue , &:: ChainActive () , PrecomputedTransactionData (*tx));
648+ TransactionSignatureChecker checker (&(*tx), nIn, coin.out .nValue , &active_chain , PrecomputedTransactionData (*tx));
651649 ScriptError serror = SCRIPT_ERR_OK;
652650
653651 if (!VerifyScript (tx->vin [nIn].scriptSig , coin.out .scriptPubKey , &(tx->vin [nIn].scriptWitness ), STANDARD_CONTEXTUAL_SCRIPT_VERIFY_FLAGS, checker, &serror))
0 commit comments