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

Code before:
    // Add dummy coinbase tx as first transaction
    pblock->vtx.emplace_back();
    pblocktemplate->vTxFees.push_back(-1); // updated at end
    pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end

    LOCK2(cs_main, m_mempool.cs);
    CBlockIndex* pindexPrev = ::ChainActive().Tip();
    assert(pindexPrev != nullptr);
    nHeight = pindexPrev->nHeight + 1;

    const int32_t nChainId = chainparams.GetConsensus().nAuxpowChainId;
    // Dogecoin: Force version 4 to disable BIP 9 version bits
    const int32_t nVersion = 4;
    pblock->SetBaseVersion(nVersion, nChainId);
    // -regtest only: allow overriding block.nVersion with
    // -blockversion=N to test forking scenarios
    if (chainparams.MineBlocksOnDemand()) {
        const int32_t nOverrideVersion = gArgs.GetArg("-blockversion", pblock->GetBaseVersion());
        if (nOverrideVersion >= CPureBlockHeader::VERSION_AUXPOW) {
            throw std::runtime_error(strprintf("%s: block version too high: %d", __func__, nOverrideVersion));
        }
        pblock->SetBaseVersion(nOverrideVersion, nChainId);
    }

    pblock->nTime = GetAdjustedTime();
    const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();

    nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
                       ? nMedianTimePast
                       : pblock->GetBlockTime();

    // Decide whether to include witness transactions
    // This is only needed in case the witness softfork activation is reverted
    // (which would require a very deep reorganization).
    // Note that the mempool would accept transactions with witness data before
    // IsWitnessEnabled, but we would only ever mine blocks after IsWitnessEnabled
    // unless there is a massive block reorganization with the witness softfork
    // not activated.
    // TODO: replace this with a call to main to assess validity of a mempool
    // transaction (which in most cases can be a no-op).
    fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());

    int nPackagesSelected = 0;
    int nDescendantsUpdated = 0;
    addPackageTxs(nPackagesSelected, nDescendantsUpdated);

    int64_t nTime1 = GetTimeMicros();

    m_last_block_num_txs = nBlockTx;
    m_last_block_weight = nBlockWeight;


Review comment (refactor):
Can we move this to initialization time (`init.cpp`) with all the other parameters that have checks applied to them, and throw the error as early as possible, rather than waiting for this to pop up when creating a block?

Code after:
    // Add dummy coinbase tx as first transaction
    pblock->vtx.emplace_back();
    pblocktemplate->vTxFees.push_back(-1); // updated at end
    pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end

    LOCK2(cs_main, m_mempool.cs);
    CBlockIndex* pindexPrev = ::ChainActive().Tip();
    assert(pindexPrev != nullptr);
    nHeight = pindexPrev->nHeight + 1;

    const int32_t nChainId = chainparams.GetConsensus().nAuxpowChainId;
    // Dogecoin: Force version 4 to disable BIP 9 version bits
    const int32_t nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
    pblock->SetBaseVersion(nVersion, nChainId);
    // -regtest only: allow overriding block.nVersion with
    // -blockversion=N to test forking scenarios
    if (chainparams.MineBlocksOnDemand()) {
        // Copy the constant to a local constant as otherwise LogPrintf grumbles
        const int32_t nMaxVersion = CPureBlockHeader::VERSION_AUXPOW;
        const int32_t nOverrideVersion = gArgs.GetArg("-blockversion", pblock->GetBaseVersion());
        if (nOverrideVersion >= 1 && nOverrideVersion < nMaxVersion) {
            pblock->SetBaseVersion(nOverrideVersion, nChainId);
        } else {
            LogPrintf("DefaultOptions(): Specified -blockversion is either below 1, or above AuxPoW version %d.\n", nMaxVersion);
            pblock->SetBaseVersion(pblock->GetBaseVersion(), nChainId);
        }
    }

    pblock->nTime = GetAdjustedTime();
    const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();

    nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
                       ? nMedianTimePast
                       : pblock->GetBlockTime();

    // Decide whether to include witness transactions
    // This is only needed in case the witness softfork activation is reverted
    // (which would require a very deep reorganization).
    // Note that the mempool would accept transactions with witness data before
    // IsWitnessEnabled, but we would only ever mine blocks after IsWitnessEnabled
    // unless there is a massive block reorganization with the witness softfork
    // not activated.
    // TODO: replace this with a call to main to assess validity of a mempool
    // transaction (which in most cases can be a no-op).
    fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());

    int nPackagesSelected = 0;
    int nDescendantsUpdated = 0;
    addPackageTxs(nPackagesSelected, nDescendantsUpdated);

    int64_t nTime1 = GetTimeMicros();