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

Code before:
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
{
    int64_t nTimeStart = GetTimeMicros();

    resetBlock();

    pblocktemplate.reset(new CBlockTemplate());

    if(!pblocktemplate.get())
        return nullptr;
    CBlock* const pblock = &pblocktemplate->block; // pointer for convenience

    // 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

Review comment (suggestion):
This works right now because regtest auxpow activation height is set to `0`. However, in [1.14](https://github.com/dogecoin/dogecoin/blob/31afd133119dd2e15862d46530cb99424cf564b0/src/chainparams.cpp#L428) it's set to `20`. If we would later change that to match, then the 0x00620004 blocks could cause problems. If we don't, the logic of height-aware auxpow is basically never tested... (also below on line 142).

I don't see any config trees in your current branding PR, is it safe to assume that height-aware parametrization is not coming back? If so, how do you propose to solve height-awareness? `if(height)-then-else`? I'm okay with leaving it for later, just need to note it down somewhere then.

Code after:
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
{
    int64_t nTimeStart = GetTimeMicros();

    resetBlock();

    pblocktemplate.reset(new CBlockTemplate());

    if(!pblocktemplate.get())
        return nullptr;
    CBlock* const pblock = &pblocktemplate->block; // pointer for convenience

    // 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