Repository: dogecoin/dogecoin
File: src/validation.cpp (C++)

Code before:
            }
        }
        // When we reach this point, we switched to a new tip (stored in pindexNewTip).

        // Notifications/callbacks that can run without cs_main

        // Notify external listeners about the new tip.
        GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);

        // Always notify the UI if a new block tip was connected
        if (pindexFork != pindexNewTip) {
            uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
        }
    } while (pindexNewTip != pindexMostWork);
    if (pindexNewTip != NULL) {
        CheckBlockIndex(chainparams.GetConsensus(pindexNewTip->nHeight));
    } else {
        CheckBlockIndex(chainparams.GetConsensus(0));
    }

    // Write changes periodically to disk, after relay.
    if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
        return false;
    }

    return true;
}


bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
{
    {
        LOCK(cs_main);
        if (pindex->nChainWork < chainActive.Tip()->nChainWork) {
            // Nothing to do, this block is not at the tip.
            return true;
        }
        if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) {
            // The chain has been extended since the last call, reset the counter.
            nBlockReverseSequenceId = -1;
        }
        nLastPreciousChainwork = chainActive.Tip()->nChainWork;
        setBlockIndexCandidates.erase(pindex);
        pindex->nSequenceId = nBlockReverseSequenceId;
        if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
            // We can't keep reducing the counter if somebody really wants to
            // call preciousblock 2**31-1 times on the same set of tips...
            nBlockReverseSequenceId--;
        }
        if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) {
            setBlockIndexCandidates.insert(pindex);

Review comment (suggestion):
Right now, this is okay, because CheckBlockIndex exclusively uses the chainParams input [to check the genesis block in case the previous block (`pprev`) is nil](https://github.com/dogecoin/dogecoin/blob/a7442cbfb032bf2fb69badc46f3dd7307c50bd2f/src/validation.cpp#L4101), so that works regardless of height.

Note though that doing this has the side-effect that we can never do height-aware checks in CheckBlockIndex. This is currently acceptable, but means we just need to be sure in the future too - I think we have to document that.

Code after:
        }
        // When we reach this point, we switched to a new tip (stored in pindexNewTip).

        // Notifications/callbacks that can run without cs_main

        // Notify external listeners about the new tip.
        GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);

        // Always notify the UI if a new block tip was connected
        if (pindexFork != pindexNewTip) {
            uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
        }

        // Perform the shutdown detection to the end of the loop to prevent
        // pindexNewTip being nil
        boost::this_thread::interruption_point();
        if (ShutdownRequested()) {
            LogPrintf("ActivateBestChain: shutdown requested, breaking loop\n");
            break;
        }

    } while (pindexNewTip != pindexMostWork);
    if (pindexNewTip != NULL) {
        CheckBlockIndex(chainparams.GetConsensus(pindexNewTip->nHeight));
    } else {
        CheckBlockIndex(chainparams.GetConsensus(0));
    }

    // Write changes periodically to disk, after relay.
    if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
        return false;
    }

    return true;
}


bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
{
    {
        LOCK(cs_main);
        if (pindex->nChainWork < chainActive.Tip()->nChainWork) {
            // Nothing to do, this block is not at the tip.
            return true;
        }
        if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) {
            // The chain has been extended since the last call, reset the counter.
            nBlockReverseSequenceId = -1;
        }
        nLastPreciousChainwork = chainActive.Tip()->nChainWork;
        setBlockIndexCandidates.erase(pindex);