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

Code before:
    CPubKey pubkey = key.GetPubKey();
    assert(key.VerifyPubKey(pubkey));
    CKeyID vchAddress = pubkey.GetID();
    {
        pwalletMain->MarkDirty();
        pwalletMain->SetAddressBook(vchAddress, strLabel, "receive");

        // Don't throw error in case a key is already there
        if (pwalletMain->HaveKey(vchAddress))
            return NullUniValue;

        pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;

        if (!pwalletMain->AddKeyPubKey(key, pubkey))
            throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");

        if (fRescan) {
            const uint32_t nHeight = getHeightParamFromRequest(request, 3);
            attemptRescanFromHeight(nHeight);
        }
    }

    return NullUniValue;
}

uint32_t getHeightParamFromRequest(const JSONRPCRequest& request, const uint32_t pos)
{
    if (request.params.size() <= pos)
        return 1;

    const int nHeight = request.params[pos].get_int();

    if (nHeight < 0 || nHeight > chainActive.Height())
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");

    return nHeight;
}

void attemptRescanFromHeight(const uint32_t nHeight)
{
    CBlockIndex* pblockindex = chainActive.Genesis();

    // we have no implicit first height for a key, so we need to scan the whole chain
    if (nHeight <= 1)
        pwalletMain->UpdateTimeFirstKey(1);
    else
        pblockindex = chainActive[nHeight];

    pwalletMain->ScanForWalletTransactions(pblockindex, true);
    pwalletMain->ReacceptWalletTransactions();
}

Review comment (suggestion):
I think that `pos` should be expressed as a `size_t` per this: https://github.com/dogecoin/dogecoin/blob/3a29ba6d497cd1d0a32ecb039da0d35ea43c9c85/src/univalue/include/univalue.h#L72

(also on line 38)

Code after:
    CPubKey pubkey = key.GetPubKey();
    assert(key.VerifyPubKey(pubkey));
    CKeyID vchAddress = pubkey.GetID();
    {
        pwalletMain->MarkDirty();
        pwalletMain->SetAddressBook(vchAddress, strLabel, "receive");

        // Don't throw error in case a key is already there
        if (pwalletMain->HaveKey(vchAddress))
            return NullUniValue;

        pwalletMain->mapKeyMetadata[vchAddress].nCreateTime = 1;

        if (!pwalletMain->AddKeyPubKey(key, pubkey))
            throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");

        if (fRescan) {
            const uint32_t nHeight = getHeightParamFromRequest(request, 3);
            attemptRescanFromHeight(nHeight);
        }
    }

    return NullUniValue;
}

uint32_t getHeightParamFromRequest(const JSONRPCRequest& request, const size_t pos)
{
    if (request.params.size() <= pos)
        return 1;

    const int nHeight = request.params[pos].get_int();

    if (nHeight < 0 || nHeight > chainActive.Height())
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");

    return nHeight;
}

void attemptRescanFromHeight(const uint32_t nHeight)
{
    CBlockIndex* pblockindex = chainActive.Genesis();

    // we have no implicit first height for a key, so we need to scan the whole chain
    if (nHeight <= 1)
        pwalletMain->UpdateTimeFirstKey(1);
    else
        pblockindex = chainActive[nHeight];

    pwalletMain->ScanForWalletTransactions(pblockindex, true);
    pwalletMain->ReacceptWalletTransactions();
}