diff --git a/src/rpcncctrie.cpp b/src/rpcncctrie.cpp index eb6a54c7b..c62f971ce 100644 --- a/src/rpcncctrie.cpp +++ b/src/rpcncctrie.cpp @@ -3,6 +3,85 @@ #include "rpcserver.h" #include "univalue/univalue.h" +UniValue getclaimsintrie(const UniValue& params, bool fHelp) +{ + if (fHelp || params.size() > 0) + throw std::runtime_error( + "getclaimsintrie\n" + "Return all claims in the name trie.\n" + "Arguments:\n" + "None\n" + "Result: \n" + "[\n" + " {\n" + " \"name\" (string) the name claimed\n" + " \"claims\": [ (array of object) the claims for this name\n" + " {\n" + " \"txid\" (string) the txid of the claim\n" + " \"n\" (numeric) the vout value of the claim\n" + " \"amount\" (numeric) txout amount\n" + " \"height\" (numeric) the height of the block in which this transaction is located\n" + " \"value\" (string) the value of this claim\n" + " }\n" + " ]\n" + " }\n" + "]\n" + ); + + LOCK(cs_main); + UniValue ret(UniValue::VARR); + + CCoinsViewCache view(pcoinsTip); + std::vector nodes = pnccTrie->flattenTrie(); + + CNodeValue val; + + for (std::vector::iterator it = nodes.begin(); it != nodes.end(); ++it) + { + if (it->second.getBestValue(val)) + { + UniValue node(UniValue::VOBJ); + node.push_back(Pair("name", it->first)); + UniValue claims(UniValue::VARR); + for (std::vector::iterator itClaims = it->second.values.begin(); itClaims != it->second.values.end(); ++itClaims) + { + UniValue claim(UniValue::VOBJ); + claim.push_back(Pair("txid", itClaims->txhash.GetHex())); + claim.push_back(Pair("n", (int)itClaims->nOut)); + claim.push_back(Pair("amount", itClaims->nAmount)); + claim.push_back(Pair("height", itClaims->nHeight)); + const CCoins* coin = view.AccessCoins(val.txhash); + if (!coin) + { + LogPrintf("%s: %s does not exist in the coins view, despite being associated with a name\n", + __func__, val.txhash.GetHex()); + claim.push_back(Pair("error", "No value found for claim")); + } + else if (coin->vout.size() < val.nOut || coin->vout[val.nOut].IsNull()) + { + LogPrintf("%s: the specified txout of %s appears to have been spent\n", __func__, val.txhash.GetHex()); + claim.push_back(Pair("error", "Txout spent")); + } + else + { + int op; + std::vector > vvchParams; + if (!DecodeNCCScript(coin->vout[val.nOut].scriptPubKey, op, vvchParams)) + { + LogPrintf("%s: the specified txout of %s does not have an NCC command\n", __func__, val.txhash.GetHex()); + } + std::string sValue(vvchParams[1].begin(), vvchParams[1].end()); + claim.push_back(Pair("value", sValue)); + } + claims.push_back(claim); + } + node.push_back(Pair("claims", claims)); + ret.push_back(node); + } + } + return ret; +} + UniValue getnametrie(const UniValue& params, bool fHelp) { if (fHelp || params.size() > 0) diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 1a59d1579..3e0b46513 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -380,6 +380,7 @@ static const CRPCCommand vRPCCommands[] = #endif // ENABLE_WALLET /* NCC trie */ + { "nametrie", "getclaimsintrie", &getclaimsintrie, true }, { "nametrie", "getnametrie", &getnametrie, true }, { "nametrie", "getvalueforname", &getvalueforname, true }, { "nametrie", "gettotalclaimednames", &gettotalclaimednames, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index b5ee55413..c1f227a32 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -241,6 +241,7 @@ extern UniValue getchaintips(const UniValue& params, bool fHelp); extern UniValue invalidateblock(const UniValue& params, bool fHelp); extern UniValue reconsiderblock(const UniValue& params, bool fHelp); +extern UniValue getclaimsintrie(const UniValue& params, bool fHelp); // in rpcncctrie.cpp extern UniValue getnametrie(const UniValue& params, bool fHelp); // in rpcncctrie.cpp extern UniValue getvalueforname(const UniValue& params, bool fHelp); extern UniValue gettotalclaimednames(const UniValue& params, bool fHelp);