From fd91980bc350f712354f7e1c145972ac3606bdc6 Mon Sep 17 00:00:00 2001 From: EvgenijM86 Date: Thu, 31 Jan 2019 14:48:07 +0200 Subject: [PATCH] fix crash when prev pindex not available --- src/rpc/blockchain.cpp | 2 +- src/validation.cpp | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 8e0f1fd4902..91a1ef0f2c3 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1091,7 +1091,7 @@ static UniValue SoftForkMajorityDesc(int version, CBlockIndex* pindex, const Con activated = pindex->nHeight >= consensusParams.BIP34Height; break; case 3: - activated = IsBTC16BIPsEnabled(pindex->nHeight); + activated = IsBTC16BIPsEnabled(pindex->nTime); break; case 4: activated = IsProtocolV06(pindex); diff --git a/src/validation.cpp b/src/validation.cpp index 6af61cf5b70..a4a00a8d2cc 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1692,7 +1692,7 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consens } // Start enforcing the DERSIG (BIP66) rule - if (IsBTC16BIPsEnabled(pindex->pprev->nTime)) { + if (pindex->pprev && IsBTC16BIPsEnabled(pindex->pprev->nTime)) { flags |= SCRIPT_VERIFY_DERSIG; } @@ -1702,7 +1702,7 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consens } // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) - if (IsBTC16BIPsEnabled(pindex->pprev->nTime)) { + if (pindex->pprev && IsBTC16BIPsEnabled(pindex->pprev->nTime)) { flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY; } @@ -1900,7 +1900,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) int nLockTimeFlags = 0; - if (IsBTC16BIPsEnabled(pindex->pprev->nTime)) { + if (pindex->pprev && IsBTC16BIPsEnabled(pindex->pprev->nTime)) { nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE; } @@ -3231,13 +3231,13 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, bool fProofOfS * in ConnectBlock(). * Note that -reindex-chainstate skips the validation that happens here! */ -static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) +static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const CBlockIndex* pindexPrev) { const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1; // Start enforcing BIP113 (Median Time Past) int nLockTimeFlags = 0; - if (IsBTC16BIPsEnabled(pindexPrev->nTime)) { + if (pindexPrev && IsBTC16BIPsEnabled(pindexPrev->nTime)) { nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST; } @@ -3271,7 +3271,7 @@ static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, c // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are // multiple, the last one is used. bool fHaveWitness = false; - if (IsBTC16BIPsEnabled(pindexPrev->nTime)) { + if (pindexPrev && IsBTC16BIPsEnabled(pindexPrev->nTime)) { int commitpos = GetWitnessCommitmentIndex(block); if (commitpos != -1) { bool malleated = false; @@ -3488,7 +3488,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr& pblock, CVali if (fNewBlock) *fNewBlock = true; if (!CheckBlock(block, state, chainparams.GetConsensus()) || - !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) { + !ContextualCheckBlock(block, state, pindex->pprev)) { if (state.IsInvalid() && !state.CorruptionPossible()) { pindex->nStatus |= BLOCK_FAILED_VALID; setDirtyBlockIndex.insert(pindex); @@ -3596,7 +3596,7 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state)); if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot)) return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state)); - if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev)) + if (!ContextualCheckBlock(block, state, pindexPrev)) return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state)); if (!g_chainstate.ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true)) return false;