diff --git a/src/validation.cpp b/src/validation.cpp index 90d273c24c08a7..4d2485e13eef11 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3074,7 +3074,7 @@ bool ResetBlockFailureFlags(CBlockIndex *pindex) { return true; } -CBlockIndex* AddToBlockIndex(const CBlockHeader& block) +CBlockIndex* AddToBlockIndex(const CBlockHeader& block, bool fValid = true) { // Check for duplicate uint256 hash = block.GetHash(); @@ -3100,9 +3100,13 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) } pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime); pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew); - pindexNew->RaiseValidity(BLOCK_VALID_TREE); - if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork) - pindexBestHeader = pindexNew; + if (fValid) { + pindexNew->RaiseValidity(BLOCK_VALID_TREE); + if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork) + pindexBestHeader = pindexNew; + } else { + pindexNew->nStatus |= BLOCK_FAILED_VALID; + } setDirtyBlockIndex.insert(pindexNew); @@ -3479,6 +3483,10 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state)); if (llmq::chainLocksHandler->HasConflictingChainLock(pindexPrev->nHeight + 1, hash)) { + if (pindex == NULL) { + // add it to the block index but also mark it as invalid immediately + AddToBlockIndex(block, false); + } return state.DoS(10, error("%s: header %s conflicts with chainlock", __func__, hash.ToString()), REJECT_INVALID, "bad-chainlock"); } }