lbryio / lbrycrd

The blockchain that provides the digital content namespace for the LBRY protocol
https://lbry.com
MIT License
2.57k stars 178 forks source link

add RPC method to return changed/removed claims in a block #267

Closed BrannonKing closed 5 years ago

BrannonKing commented 5 years ago

I was thinking something like this:

extern bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex *pindex);

UniValue getchangesinblock(const JSONRPCRequest& request) {
    if (request.fHelp || request.params.size() > 1)
        throw std::runtime_error(
                "getchangesinblock\n"
                "Return the list of claims added, updated, and removed in a block\n"
                "or doesn't.\n"
                "Arguments:\n"
                "1. \"blockhash\"      (string, optional) the hash of the block in question\n"
                "Result: \n"
                "{\n"
                "  \"claims_added_or_updated\":   (array of string) claimIDs added or updated in the trie\n"
                "  \"claims_removed\":            (array of string) claimIDs that were removed from the trie.\n"
                "  \"supports_added_or_updated\": (array of string) IDs of supports added or updated\n"
                "  \"supports_removed\":          (array of string) IDs that were removed from the trie.\n"
                "}\n");

    CBlockUndo undo;
    {
        LOCK(cs_main);
        auto index = chainActive.Tip();
        if (request.params.size() > 0) {
            index = BlockHashIndex(ParseHashV(request.params[0], "blockhash (optional parameter)"));
        }

        if (!UndoReadFromDisk(undo, index))
            throw JSONRPCError(RPC_INTERNAL_ERROR,
                               "Unable to read the undo block for height " + std::to_string(index->nHeight));
    }
    UniValue added(UniValue::VARR);
    for (auto& u: undo.insertUndo)
        added.push_back(ClaimIdHash(u.outPoint.hash, u.outPoint.n).ToString());

    UniValue removed(UniValue::VARR);
    for (auto& u: undo.expireUndo)
        added.push_back(u.second.claimId.ToString());

//    UniValue sremoved(UniValue::VARR);
//    for (auto& u: undo.expireSupportUndo)
//        added.push_back(u.second..ToString());

    UniValue result(UniValue::VOBJ);
    result.pushKV("claims_added_or_updated", added);
    return result;
}

Also, make sure that the other RPC methods return the takeover height.

BrannonKing commented 5 years ago

Merged via https://github.com/lbryio/lbrycrd/pull/303. It doesn't report all changes, but it does report the ones that were queued for that block.