Skip to content

Commit

Permalink
fix crash when prev pindex not available
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenijM86 committed Jan 31, 2019
1 parent 10a0c5f commit fd91980
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/rpc/blockchain.cpp
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/validation.cpp
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3488,7 +3488,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& 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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fd91980

Please sign in to comment.