Serialize effective amount #231

Closed
bvbfan wants to merge 309 commits from serialize_effective_amount into master
8 changed files with 159 additions and 1 deletions
Showing only changes of commit 49c7c3ef25 - Show all commits

View file

@ -153,6 +153,68 @@ bool CNCCTrie::haveClaim(const std::string& name, const uint256& txhash, uint32_
return current->haveValue(txhash, nOut);
}
unsigned int CNCCTrie::getTotalNamesInTrie() const
{
if (empty())
return 0;
const CNCCTrieNode* current = &root;
return getTotalNamesRecursive(current);
}
unsigned int CNCCTrie::getTotalNamesRecursive(const CNCCTrieNode* current) const
{
unsigned int names_in_subtrie = 0;
if (!(current->values.empty()))
names_in_subtrie += 1;
for (nodeMapType::const_iterator it = current->children.begin(); it != current->children.end(); ++it)
{
names_in_subtrie += getTotalNamesRecursive(it->second);
}
return names_in_subtrie;
}
unsigned int CNCCTrie::getTotalClaimsInTrie() const
{
if (empty())
return 0;
const CNCCTrieNode* current = &root;
return getTotalClaimsRecursive(current);
}
unsigned int CNCCTrie::getTotalClaimsRecursive(const CNCCTrieNode* current) const
{
unsigned int claims_in_subtrie = current->values.size();
for (nodeMapType::const_iterator it = current->children.begin(); it != current->children.end(); ++it)
{
claims_in_subtrie += getTotalClaimsRecursive(it->second);
}
return claims_in_subtrie;
}
CAmount CNCCTrie::getTotalValueOfClaimsInTrie(bool fControllingOnly) const
{
if (empty())
return 0;
const CNCCTrieNode* current = &root;
return getTotalValueOfClaimsRecursive(current, fControllingOnly);
}
CAmount CNCCTrie::getTotalValueOfClaimsRecursive(const CNCCTrieNode* current, bool fControllingOnly) const
{
CAmount value_in_subtrie = 0;
for (std::vector<CNodeValue>::const_iterator itval = current->values.begin(); itval != current->values.end(); ++itval)
{
value_in_subtrie += itval->nAmount;
if (fControllingOnly)
break;
}
for (nodeMapType::const_iterator itchild = current->children.begin(); itchild != current->children.end(); ++itchild)
{
value_in_subtrie += getTotalValueOfClaimsRecursive(itchild->second, fControllingOnly);
}
return value_in_subtrie;
}
bool CNCCTrie::recursiveDumpToJSON(const std::string& name, const CNCCTrieNode* current, json_spirit::Array& ret) const
{
using namespace json_spirit;

View file

@ -160,6 +160,9 @@ public:
int nCurrentHeight;
bool queueEmpty() const;
bool haveClaim(const std::string& name, const uint256& txhash, uint32_t nOut) const;
unsigned int getTotalNamesInTrie() const;
unsigned int getTotalClaimsInTrie() const;
CAmount getTotalValueOfClaimsInTrie(bool fControllingOnly) const;
friend class CNCCTrieCache;
private:
void clear(CNCCTrieNode* current);
@ -169,6 +172,9 @@ private:
bool recursiveNullify(CNCCTrieNode* node, std::string& name);
bool recursiveCheckConsistency(CNCCTrieNode* node);
bool InsertFromDisk(const std::string& name, CNCCTrieNode* node);
unsigned int getTotalNamesRecursive(const CNCCTrieNode* current) const;
unsigned int getTotalClaimsRecursive(const CNCCTrieNode* current) const;
CAmount getTotalValueOfClaimsRecursive(const CNCCTrieNode* current, bool fControllingOnly) const;
bool recursiveDumpToJSON(const std::string& name, const CNCCTrieNode* current, json_spirit::Array& ret) const;
CNCCTrieNode root;
uint256 hashBlock;

View file

@ -95,6 +95,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "abandonname", 2},
{ "listnameclaims", 0},
{ "listnameclaims", 1},
{ "gettotalvalueofclaims", 0},
};
class CRPCConvertTable

View file

@ -105,3 +105,78 @@ Value getvalueforname(const Array& params, bool fHelp)
return ret;
}
Value gettotalclaimednames(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw std::runtime_error(
"gettotalclaimednames\n"
"Return the total number of names that have been\n"
"successfully claimed, and therefore exist in the trie\n"
"Arguments:\n"
"Result:\n"
"\"total names\" (numeric) the total number of\n"
" names in the trie\n"
);
LOCK(cs_main);
Object ret;
if (!pnccTrie)
{
ret.push_back(Pair("total names", -1));
return ret;
}
unsigned int num_names = pnccTrie->getTotalNamesInTrie();
ret.push_back(Pair("total names", (int)num_names));
return ret;
}
Value gettotalclaims(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw std::runtime_error(
"gettotalclaims\n"
"Return the total number of active claims in the trie\n"
"Arguments:\n"
"Result:\n"
"\"total claims\" (numeric) the total number\n"
" of active claims\n"
);
LOCK(cs_main);
Object ret;
if (!pnccTrie)
{
ret.push_back(Pair("total claims", -1));
return ret;
}
unsigned int num_claims = pnccTrie->getTotalClaimsInTrie();
ret.push_back(Pair("total claims", (int)num_claims));
return ret;
}
Value gettotalvalueofclaims(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw std::runtime_error(
"gettotalvalueofclaims\n"
"Return the total value of the claims in the trie\n"
"Arguments:\n"
"1. \"controlling_only\" (boolean) only include the value\n"
" of controlling claims\n"
"Result:\n"
"\"total value\" (numeric) the total value of the\n"
" claims in the trie\n"
);
LOCK(cs_main);
Object ret;
if (!pnccTrie)
{
ret.push_back(Pair("total value", -1));
return ret;
}
bool controlling_only = false;
if (params.size() == 1)
controlling_only = params[0].get_bool();
CAmount total_amount = pnccTrie->getTotalValueOfClaimsInTrie(controlling_only);
ret.push_back(Pair("total value", total_amount));
return ret;
}

View file

@ -388,6 +388,9 @@ static const CRPCCommand vRPCCommands[] =
{ "ncctrie", "getncctrie", &getncctrie, true, false},
{ "ncctrie", "gettxinfoforname", &gettxinfoforname, true, false},
{ "ncctrie", "getvalueforname", &getvalueforname, true, false},
{ "ncctrie", "gettotalclaimednames", &gettotalclaimednames, true, false},
{ "ncctrie", "gettotalclaims", &gettotalclaims, true, false},
{ "ncctrie", "gettotalvalueofclaims", &gettotalvalueofclaims, true, false},
};
CRPCTable::CRPCTable()

View file

@ -241,6 +241,9 @@ 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);
extern json_spirit::Value gettotalclaimednames(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value gettotalclaims(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value gettotalvalueofclaims(const json_spirit::Array& params, bool fHelp);
// in rest.cpp
extern bool HTTPReq_REST(AcceptedConnection *conn,

View file

@ -28,7 +28,7 @@ public:
unsigned int nNCCValidHeight; // If the outpoint was an NCC claim, the height at which the claim should be inserted into the trie
CTxInUndo() : txout(), fLastUnspent(false), fCoinBase(false), nHeight(0), nVersion(0), nNCCValidHeight(0) {}
CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0, unsigned int nNCCValidHeight = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn), nNCCValidHeight(nNCCValidHeight) { }
CTxInUndo(const CTxOut &txoutIn, bool fLastUnspent = false, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0, unsigned int nNCCValidHeight = 0) : txout(txoutIn), fLastUnspent(fLastUnspent), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn), nNCCValidHeight(nNCCValidHeight) { }
unsigned int GetSerializeSize(int nType, int nVersion) const {
return ::GetSerializeSize(VARINT(nHeight*4+(fCoinBase ? 2 : 0)+(fLastUnspent ? 1: 0)), nType, nVersion) +

View file

@ -630,6 +630,13 @@ void ListNameClaims(const CWalletTx& wtx, const string& strAccount, int nMinDept
entry.push_back(Pair("amount", ValueFromAmount(s.amount)));
entry.push_back(Pair("vout", s.vout));
entry.push_back(Pair("fee", ValueFromAmount(nFee)));
BlockMap::iterator it = mapBlockIndex.find(wtx.hashBlock);
if (it != mapBlockIndex.end())
{
CBlockIndex* pindex = it->second;
if (pindex)
entry.push_back(Pair("height", pindex->nHeight));
}
entry.push_back(Pair("confirmations", wtx.GetDepthInMainChain()));
entry.push_back(Pair("is spent", pwalletMain->IsSpent(wtx.GetHash(), s.vout)));
entry.push_back(Pair("is in ncc trie", pnccTrie->haveClaim(sName, wtx.GetHash(), s.vout)));
@ -662,6 +669,7 @@ Value listnameclaims(const Array& params, bool fHelp)
" \"amount\": x.xxx, (numeric) The amount in btc.\n"
" \"vout\": n, (numeric) The vout value\n"
" \"fee\": x.xxx, (numeric) The amount of the fee in btc.\n"
" \"height\": n (numeric) The height of the block in which this transaction was included.\n"
" \"confirmations\": n, (numeric) The number of confirmations for the transaction\n"
" \"blockhash\": \"hashvalue\", (string) The block hash containing the transaction.\n"
" \"blockindex\": n, (numeric) The block index containing the transaction.\n"