add an rpc command for getting the value of a name

This commit is contained in:
Jimmy Kiselak 2015-02-10 18:44:14 -05:00
parent b68b97388e
commit 6f9808faff
5 changed files with 62 additions and 18 deletions

View file

@ -113,27 +113,17 @@ json_spirit::Array CNCCTrie::dumpToJSON() const
return ret; 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; const CNCCTrieNode* current = &root;
for (std::string::const_iterator itname = name.begin(); itname != name.end(); ++itname) for (std::string::const_iterator itname = name.begin(); itname != name.end(); ++itname)
{ {
nodeMapType::const_iterator itchildren = current->children.find(*itname); nodeMapType::const_iterator itchildren = current->children.find(*itname);
if (itchildren == current->children.end()) if (itchildren == current->children.end())
return ret; return false;
current = itchildren->second; current = itchildren->second;
} }
CNodeValue val; return current->getValue(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;
} }
bool CNCCTrie::checkConsistency() bool CNCCTrie::checkConsistency()

View file

@ -130,7 +130,7 @@ public:
bool checkConsistency(); bool checkConsistency();
bool ReadFromDisk(bool check = false); bool ReadFromDisk(bool check = false);
json_spirit::Array dumpToJSON() const; 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; friend class CNCCTrieCache;
private: private:
bool update(nodeCacheType& cache, hashMapType& hashes, const uint256& hashBlock); bool update(nodeCacheType& cache, hashMapType& hashes, const uint256& hashBlock);

View file

@ -1,4 +1,5 @@
#include "main.h" #include "main.h"
#include "ncc.h"
#include "json/json_spirit_value.h" #include "json/json_spirit_value.h"
@ -36,20 +37,71 @@ Value gettxinfoforname(const Array& params, bool fHelp)
{ {
if (fHelp || params.size() != 1) if (fHelp || params.size() != 1)
throw std::runtime_error( throw std::runtime_error(
"gettxinfoforname\n" "gettxinfoforname \"name\"\n"
"Return information about the transaction that has successfully claimed a name, if one exists\n" "Return information about the transaction that has successfully claimed a name, if one exists\n"
"Arguments:\n" "Arguments:\n"
"1. \"name\" (string) the name about which to return info\n" "1. \"name\" (string) the name about which to return info\n"
"Result: \n" "Result: \n"
"\"txid\" (string) the hash of the transaction which successfully claimed the name\n" "\"txid\" (string) the hash of the transaction which successfully claimed the name\n"
"\"n\" (numeric) vout value\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" "\"height\" (numeric) the height of the block in which this transaction is located\n"
); );
LOCK(cs_main); LOCK(cs_main);
std::string name = params[0].get_str(); 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; 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<std::vector<unsigned char> > 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;
}

View file

@ -382,8 +382,9 @@ static const CRPCCommand vRPCCommands[] =
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
/* NCC trie */ /* NCC trie */
{ "ncctrie", "getncctrie", &getncctrie, true, false}, { "ncctrie", "getncctrie", &getncctrie, true, false},
{ "ncctrie", "gettxinfoforname", &gettxinfoforname, true, false}, { "ncctrie", "gettxinfoforname", &gettxinfoforname, true, false},
{ "ncctrie", "getvalueforname", &getvalueforname, true, false},
}; };
CRPCTable::CRPCTable() CRPCTable::CRPCTable()

View file

@ -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 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 gettxinfoforname(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getvalueforname(const json_spirit::Array& params, bool fHelp);
// in rest.cpp // in rest.cpp
extern bool HTTPReq_REST(AcceptedConnection *conn, extern bool HTTPReq_REST(AcceptedConnection *conn,