diff --git a/src/ncctrie.cpp b/src/ncctrie.cpp index 83094b17f..0b3a4802a 100644 --- a/src/ncctrie.cpp +++ b/src/ncctrie.cpp @@ -113,27 +113,17 @@ json_spirit::Array CNCCTrie::dumpToJSON() const return ret; } -json_spirit::Object CNCCTrie::getInfoForName(const std::string& name) const +bool CNCCTrie::getInfoForName(const std::string& name, CNodeValue& val) const { - using namespace json_spirit; - Object ret; const CNCCTrieNode* current = &root; for (std::string::const_iterator itname = name.begin(); itname != name.end(); ++itname) { nodeMapType::const_iterator itchildren = current->children.find(*itname); if (itchildren == current->children.end()) - return ret; + return false; current = itchildren->second; } - CNodeValue val; - if (current->getValue(val)) - { - ret.push_back(Pair("txid", val.txhash.GetHex())); - ret.push_back(Pair("n", (int)val.nOut)); - ret.push_back(Pair("value", val.nAmount)); - ret.push_back(Pair("height", val.nHeight)); - } - return ret; + return current->getValue(val); } bool CNCCTrie::checkConsistency() diff --git a/src/ncctrie.h b/src/ncctrie.h index 646d5bfa6..4c5a60657 100644 --- a/src/ncctrie.h +++ b/src/ncctrie.h @@ -130,7 +130,7 @@ public: bool checkConsistency(); bool ReadFromDisk(bool check = false); json_spirit::Array dumpToJSON() const; - json_spirit::Object getInfoForName(const std::string& name) const; + bool getInfoForName(const std::string& name, CNodeValue& val) const; friend class CNCCTrieCache; private: bool update(nodeCacheType& cache, hashMapType& hashes, const uint256& hashBlock); diff --git a/src/rpcncctrie.cpp b/src/rpcncctrie.cpp index b4840624b..a8fa1083f 100644 --- a/src/rpcncctrie.cpp +++ b/src/rpcncctrie.cpp @@ -1,4 +1,5 @@ #include "main.h" +#include "ncc.h" #include "json/json_spirit_value.h" @@ -36,20 +37,71 @@ Value gettxinfoforname(const Array& params, bool fHelp) { if (fHelp || params.size() != 1) throw std::runtime_error( - "gettxinfoforname\n" + "gettxinfoforname \"name\"\n" "Return information about the transaction that has successfully claimed a name, if one exists\n" "Arguments:\n" "1. \"name\" (string) the name about which to return info\n" "Result: \n" "\"txid\" (string) the hash of the transaction which successfully claimed the name\n" "\"n\" (numeric) vout value\n" - "\"value\" (numeric) txout value\n" + "\"amount\" (numeric) txout amount\n" "\"height\" (numeric) the height of the block in which this transaction is located\n" ); LOCK(cs_main); std::string name = params[0].get_str(); - Object ret = pnccTrie->getInfoForName(name); + Object ret;// = pnccTrie->getInfoForName(name); + CNodeValue val; + if (pnccTrie->getInfoForName(name, val)) + { + ret.push_back(Pair("txid", val.txhash.GetHex())); + ret.push_back(Pair("n", (int)val.nOut)); + ret.push_back(Pair("amount", val.nAmount)); + ret.push_back(Pair("height", val.nHeight)); + } return ret; } + +Value getvalueforname(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 1) + throw std::runtime_error( + "getvalueforname \"name\"\n" + "Return the value associated with a name, if one exists\n" + "Arguments:\n" + "1. \"name\" (string) the name to look up\n" + "Result: \n" + "\"value\" (string) the value of the name, if it exists\n" + ); + LOCK(cs_main); + std::string name = params[0].get_str(); + CNodeValue val; + Object ret; + if (!pnccTrie->getInfoForName(name, val)) + return ret; + CCoinsViewCache view(pcoinsTip); + 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()); + return ret; + } + 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()); + return ret; + } + + 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()); + ret.push_back(Pair("value", sValue)); + return ret; +} + diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 2325f7ad4..035e3fe90 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -382,8 +382,9 @@ static const CRPCCommand vRPCCommands[] = #endif // ENABLE_WALLET /* NCC trie */ - { "ncctrie", "getncctrie", &getncctrie, true, false}, + { "ncctrie", "getncctrie", &getncctrie, true, false}, { "ncctrie", "gettxinfoforname", &gettxinfoforname, true, false}, + { "ncctrie", "getvalueforname", &getvalueforname, true, false}, }; CRPCTable::CRPCTable() diff --git a/src/rpcserver.h b/src/rpcserver.h index b36b2be36..f928647f6 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -239,6 +239,7 @@ extern json_spirit::Value reconsiderblock(const json_spirit::Array& params, bool extern json_spirit::Value getncctrie(const json_spirit::Array& params, bool fHelp); // in rpcncctrie.cpp extern json_spirit::Value gettxinfoforname(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value getvalueforname(const json_spirit::Array& params, bool fHelp); // in rest.cpp extern bool HTTPReq_REST(AcceptedConnection *conn,