Fix claims sorting, refactor getClaimsForName, add InsertNode, fix memleaks #249
5 changed files with 193 additions and 235 deletions
|
@ -437,97 +437,6 @@ bool CClaimTrie::getLastTakeoverForName(const std::string& name, int& lastTakeov
|
|||
return false;
|
||||
}
|
||||
|
||||
claimsForNameType CClaimTrie::getClaimsForName(const std::string& name) const
|
||||
{
|
||||
std::vector<CClaimValue> claims;
|
||||
std::vector<CSupportValue> supports;
|
||||
int nLastTakeoverHeight = 0;
|
||||
const CClaimTrieNode* current = getNodeForName(name);
|
||||
if (current)
|
||||
{
|
||||
if (!current->claims.empty())
|
||||
{
|
||||
nLastTakeoverHeight = current->nHeightOfLastTakeover;
|
||||
}
|
||||
for (std::vector<CClaimValue>::const_iterator itClaims = current->claims.begin(); itClaims != current->claims.end(); ++itClaims)
|
||||
{
|
||||
claims.push_back(*itClaims);
|
||||
}
|
||||
}
|
||||
supportMapEntryType supportNode;
|
||||
if (getSupportNode(name, supportNode))
|
||||
{
|
||||
for (std::vector<CSupportValue>::const_iterator itSupports = supportNode.begin(); itSupports != supportNode.end(); ++itSupports)
|
||||
{
|
||||
supports.push_back(*itSupports);
|
||||
}
|
||||
}
|
||||
queueNameRowType namedClaimRow;
|
||||
if (getQueueNameRow(name, namedClaimRow))
|
||||
{
|
||||
for (queueNameRowType::const_iterator itClaimsForName = namedClaimRow.begin(); itClaimsForName != namedClaimRow.end(); ++itClaimsForName)
|
||||
{
|
||||
claimQueueRowType claimRow;
|
||||
if (getQueueRow(itClaimsForName->nHeight, claimRow))
|
||||
{
|
||||
for (claimQueueRowType::const_iterator itClaimRow = claimRow.begin(); itClaimRow != claimRow.end(); ++itClaimRow)
|
||||
{
|
||||
if (itClaimRow->first == name && itClaimRow->second.outPoint == itClaimsForName->outPoint)
|
||||
{
|
||||
claims.push_back(itClaimRow->second);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
queueNameRowType namedSupportRow;
|
||||
if (getSupportQueueNameRow(name, namedSupportRow))
|
||||
{
|
||||
for (queueNameRowType::const_iterator itSupportsForName = namedSupportRow.begin(); itSupportsForName != namedSupportRow.end(); ++itSupportsForName)
|
||||
{
|
||||
supportQueueRowType supportRow;
|
||||
if (getSupportQueueRow(itSupportsForName->nHeight, supportRow))
|
||||
{
|
||||
for (supportQueueRowType::const_iterator itSupportRow = supportRow.begin(); itSupportRow != supportRow.end(); ++itSupportRow)
|
||||
{
|
||||
if (itSupportRow->first == name && itSupportRow->second.outPoint == itSupportsForName->outPoint)
|
||||
{
|
||||
supports.push_back(itSupportRow->second);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
claimsForNameType allClaims(claims, supports, nLastTakeoverHeight);
|
||||
return allClaims;
|
||||
}
|
||||
|
||||
//return effective amount from claim, retuns 0 if claim is not found
|
||||
CAmount CClaimTrie::getEffectiveAmountForClaim(const std::string& name, const uint160& claimId, std::vector<CSupportValue>* supports) const
|
||||
{
|
||||
return getEffectiveAmountForClaim(getClaimsForName(name), claimId, supports);
|
||||
}
|
||||
|
||||
CAmount CClaimTrie::getEffectiveAmountForClaim(const claimsForNameType& claims, const uint160& claimId, std::vector<CSupportValue>* supports) const
|
||||
{
|
||||
CAmount effectiveAmount = 0;
|
||||
for (std::vector<CClaimValue>::const_iterator it = claims.claims.begin(); it != claims.claims.end(); ++it) {
|
||||
if (it->claimId == claimId && it->nValidAtHeight < nCurrentHeight) {
|
||||
effectiveAmount += it->nAmount;
|
||||
for (std::vector<CSupportValue>::const_iterator it = claims.supports.begin(); it != claims.supports.end(); ++it) {
|
||||
if (it->supportedClaimId == claimId && it->nValidAtHeight < nCurrentHeight) {
|
||||
effectiveAmount += it->nAmount;
|
||||
if (supports) supports->push_back(*it);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return effectiveAmount;
|
||||
}
|
||||
|
||||
bool CClaimTrie::checkConsistency() const
|
||||
{
|
||||
if (empty())
|
||||
|
@ -2554,7 +2463,7 @@ bool CClaimTrieCache::iterateTrie(CNodeCallback& callback) const
|
|||
return true;
|
||||
}
|
||||
|
||||
claimsForNameType CClaimTrieCache::getClaimsForName(const std::string& name) const
|
||||
CClaimSupportToName CClaimTrieCache::getClaimsForName(const std::string& name) const
|
||||
{
|
||||
int nLastTakeoverHeight = 0;
|
||||
std::vector<CClaimValue> claims;
|
||||
|
@ -2596,30 +2505,32 @@ claimsForNameType CClaimTrieCache::getClaimsForName(const std::string& name) con
|
|||
}
|
||||
}
|
||||
}
|
||||
return claimsForNameType(claims, supports, nLastTakeoverHeight);
|
||||
}
|
||||
|
||||
CAmount CClaimTrieCache::getEffectiveAmountForClaim(const std::string& name, const uint160& claimId, std::vector<CSupportValue>* supports) const
|
||||
{
|
||||
return getEffectiveAmountForClaim(getClaimsForName(name), claimId, supports);
|
||||
}
|
||||
|
||||
CAmount CClaimTrieCache::getEffectiveAmountForClaim(const claimsForNameType& claims, const uint160& claimId, std::vector<CSupportValue>* supports) const
|
||||
{
|
||||
CAmount effectiveAmount = 0;
|
||||
for (std::vector<CClaimValue>::const_iterator it = claims.claims.begin(); it != claims.claims.end(); ++it) {
|
||||
if (it->claimId == claimId && it->nValidAtHeight < nCurrentHeight) {
|
||||
effectiveAmount += it->nAmount;
|
||||
for (std::vector<CSupportValue>::const_iterator it = claims.supports.begin(); it != claims.supports.end(); ++it) {
|
||||
if (it->supportedClaimId == claimId && it->nValidAtHeight < nCurrentHeight) {
|
||||
effectiveAmount += it->nAmount;
|
||||
if (supports) supports->push_back(*it);
|
||||
}
|
||||
}
|
||||
break;
|
||||
// match support to claim
|
||||
std::vector<CClaimSupport> claimSupport;
|
||||
for (std::vector<CClaimValue>::const_iterator it = claims.begin(); it != claims.end(); ++it) {
|
||||
CAmount nAmount = it->nValidAtHeight < nCurrentHeight ? it->nAmount : 0;
|
||||
std::vector<CClaimSupport>::iterator itClaimSupport = claimSupport.insert(claimSupport.end(), CClaimSupport(it->claimId, *it, nAmount, std::vector<CSupportValue>()));
|
||||
for (std::vector<CSupportValue>::const_iterator itSupport = supports.begin(), itSupportEnd = supports.end();
|
||||
(itSupport = std::find_if(itSupport, itSupportEnd, CClaimIdMatcher(it->claimId))) != itSupportEnd; ++itSupport) {
|
||||
itClaimSupport->support.push_back(*itSupport);
|
||||
if (itSupport->nValidAtHeight < nCurrentHeight)
|
||||
itClaimSupport->effectiveAmount += itSupport->nAmount;
|
||||
}
|
||||
}
|
||||
return effectiveAmount;
|
||||
// unmatched support
|
||||
for (std::vector<CSupportValue>::const_iterator it = supports.begin(); it != supports.end(); ++it) {
|
||||
std::vector<CClaimValue>::const_iterator itClaims = std::find_if(claims.begin(), claims.end(), CClaimIdMatcher(it->supportedClaimId));
|
||||
if (itClaims != claims.end()) continue;
|
||||
std::vector<CClaimSupport>::iterator itSupport = std::find_if(claimSupport.begin(), claimSupport.end(), CClaimIdMatcher(it->supportedClaimId));
|
||||
if (itSupport == claimSupport.end()) {
|
||||
itSupport = claimSupport.insert(itSupport, CClaimSupport());
|
||||
itSupport->claimId = it->supportedClaimId;
|
||||
}
|
||||
if (it->nValidAtHeight < nCurrentHeight)
|
||||
itSupport->effectiveAmount += it->nAmount;
|
||||
itSupport->support.push_back(*it);
|
||||
}
|
||||
return CClaimSupportToName(nLastTakeoverHeight, claimSupport);
|
||||
}
|
||||
|
||||
bool CClaimTrieCache::getInfoForName(const std::string& name, CClaimValue& claim) const
|
||||
|
|
|
@ -9,9 +9,10 @@
|
|||
#include "chainparams.h"
|
||||
#include "primitives/transaction.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
// leveldb keys
|
||||
#define HASH_BLOCK 'h'
|
||||
|
@ -283,14 +284,77 @@ typedef std::map<std::string, uint256> hashMapType;
|
|||
typedef std::set<CClaimValue> claimIndexClaimListType;
|
||||
typedef std::vector<CClaimIndexElement> claimIndexElementListType;
|
||||
|
||||
struct claimsForNameType
|
||||
class CClaimSupport
|
||||
{
|
||||
std::vector<CClaimValue> claims;
|
||||
std::vector<CSupportValue> supports;
|
||||
int nLastTakeoverHeight;
|
||||
public:
|
||||
CClaimSupport()
|
||||
{
|
||||
effectiveAmount = 0;
|
||||
}
|
||||
|
||||
claimsForNameType(std::vector<CClaimValue> claims, std::vector<CSupportValue> supports, int nLastTakeoverHeight)
|
||||
: claims(claims), supports(supports), nLastTakeoverHeight(nLastTakeoverHeight) {}
|
||||
CClaimSupport(const uint160& claimId, const CClaimValue& claim, CAmount effectiveAmount, const std::vector<CSupportValue>& support)
|
||||
: claimId(claimId), claim(claim), effectiveAmount(effectiveAmount), support(support)
|
||||
{
|
||||
}
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return !claimId.IsNull();
|
||||
}
|
||||
|
||||
uint160 claimId;
|
||||
CClaimValue claim;
|
||||
CAmount effectiveAmount;
|
||||
std::vector<CSupportValue> support;
|
||||
};
|
||||
|
||||
class CClaimIdMatcher
|
||||
{
|
||||
public:
|
||||
CClaimIdMatcher(const uint160& claimId) : claimId(claimId)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator()(const CClaimValue& value) const
|
||||
{
|
||||
return claimId == value.claimId;
|
||||
}
|
||||
|
||||
bool operator()(const CClaimSupport& value) const
|
||||
{
|
||||
return claimId == value.claimId;
|
||||
}
|
||||
|
||||
bool operator()(const CSupportValue& value) const
|
||||
{
|
||||
return claimId == value.supportedClaimId;
|
||||
}
|
||||
|
||||
private:
|
||||
const uint160 claimId;
|
||||
};
|
||||
|
||||
class CClaimSupportToName
|
||||
{
|
||||
public:
|
||||
CClaimSupportToName(int nLastTakeoverHeight, const std::vector<CClaimSupport>& claims)
|
||||
: nLastTakeoverHeight(nLastTakeoverHeight), claims(claims)
|
||||
{
|
||||
}
|
||||
|
||||
const CClaimSupport& find(const uint160& claimId) const
|
||||
{
|
||||
static const CClaimSupport invalidClaim;
|
||||
std::vector<CClaimSupport>::const_iterator it = std::find_if(claims.begin(), claims.end(), CClaimIdMatcher(claimId));
|
||||
if (it != claims.end()) {
|
||||
return *it;
|
||||
} else {
|
||||
return invalidClaim;
|
||||
}
|
||||
}
|
||||
|
||||
const int nLastTakeoverHeight;
|
||||
const std::vector<CClaimSupport> claims;
|
||||
};
|
||||
|
||||
class CClaimTrieCache;
|
||||
|
@ -318,11 +382,6 @@ public:
|
|||
bool getInfoForName(const std::string& name, CClaimValue& claim) const;
|
||||
bool getLastTakeoverForName(const std::string& name, int& lastTakeoverHeight) const;
|
||||
|
||||
claimsForNameType getClaimsForName(const std::string& name) const;
|
||||
|
||||
CAmount getEffectiveAmountForClaim(const std::string& name, const uint160& claimId, std::vector<CSupportValue>* supports = NULL) const;
|
||||
CAmount getEffectiveAmountForClaim(const claimsForNameType& claims, const uint160& claimId, std::vector<CSupportValue>* supports = NULL) const;
|
||||
|
||||
bool queueEmpty() const;
|
||||
bool supportEmpty() const;
|
||||
bool supportQueueEmpty() const;
|
||||
|
@ -550,10 +609,7 @@ public:
|
|||
|
||||
bool iterateTrie(CNodeCallback& callback) const;
|
||||
|
||||
claimsForNameType getClaimsForName(const std::string& name) const;
|
||||
|
||||
CAmount getEffectiveAmountForClaim(const std::string& name, const uint160& claimId, std::vector<CSupportValue>* supports = NULL) const;
|
||||
CAmount getEffectiveAmountForClaim(const claimsForNameType& claims, const uint160& claimId, std::vector<CSupportValue>* supports = NULL) const;
|
||||
CClaimSupportToName getClaimsForName(const std::string& name) const;
|
||||
|
||||
protected:
|
||||
CClaimTrie* base;
|
||||
|
|
|
@ -325,7 +325,7 @@ UniValue getvalueforname(const UniValue& params, bool fHelp)
|
|||
if (!getValueForClaim(coinsCache, claim.outPoint, sValue))
|
||||
return ret;
|
||||
|
||||
CAmount nEffectiveAmount = trieCache.getEffectiveAmountForClaim(name, claim.claimId);
|
||||
CAmount nEffectiveAmount = trieCache.getClaimsForName(name).find(claim.claimId).effectiveAmount;
|
||||
|
||||
ret.push_back(Pair("value", sValue));
|
||||
ret.push_back(Pair("claimId", claim.claimId.GetHex()));
|
||||
|
@ -337,9 +337,6 @@ UniValue getvalueforname(const UniValue& params, bool fHelp)
|
|||
return ret;
|
||||
}
|
||||
|
||||
typedef std::pair<CClaimValue, std::vector<CSupportValue> > claimAndSupportsType;
|
||||
typedef std::map<uint160, claimAndSupportsType> claimSupportMapType;
|
||||
|
||||
UniValue supportToJSON(const CSupportValue& support)
|
||||
{
|
||||
UniValue ret(UniValue::VOBJ);
|
||||
|
@ -351,16 +348,16 @@ UniValue supportToJSON(const CSupportValue& support)
|
|||
return ret;
|
||||
}
|
||||
|
||||
UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, CAmount nEffectiveAmount, claimSupportMapType::const_iterator itClaimsAndSupports)
|
||||
UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, const CClaimSupport& claimSupport)
|
||||
{
|
||||
UniValue ret(UniValue::VOBJ);
|
||||
const CClaimValue& claim = itClaimsAndSupports->second.first;
|
||||
const std::vector<CSupportValue>& supports = itClaimsAndSupports->second.second;
|
||||
UniValue supportObjs(UniValue::VARR);
|
||||
UniValue supportObjs(UniValue::VOBJ);
|
||||
const CClaimValue& claim = claimSupport.claim;
|
||||
const std::vector<CSupportValue>& supports = claimSupport.support;
|
||||
for (std::vector<CSupportValue>::const_iterator itSupports = supports.begin(); itSupports != supports.end(); ++itSupports) {
|
||||
supportObjs.push_back(supportToJSON(*itSupports));
|
||||
}
|
||||
ret.push_back(Pair("claimId", itClaimsAndSupports->first.GetHex()));
|
||||
ret.push_back(Pair("claimId", claimSupport.claimId.GetHex()));
|
||||
ret.push_back(Pair("txid", claim.outPoint.hash.GetHex()));
|
||||
ret.push_back(Pair("n", (int)claim.outPoint.n));
|
||||
ret.push_back(Pair("nHeight", claim.nHeight));
|
||||
|
@ -369,7 +366,7 @@ UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, CAmount nEffe
|
|||
std::string sValue;
|
||||
if (getValueForClaim(coinsCache, claim.outPoint, sValue))
|
||||
ret.push_back(Pair("value", sValue));
|
||||
ret.push_back(Pair("nEffectiveAmount", nEffectiveAmount));
|
||||
ret.push_back(Pair("nEffectiveAmount", claimSupport.effectiveAmount));
|
||||
ret.push_back(Pair("supports", supportObjs));
|
||||
return ret;
|
||||
}
|
||||
|
@ -432,36 +429,26 @@ UniValue getclaimsforname(const UniValue& params, bool fHelp)
|
|||
}
|
||||
|
||||
std::string name = params[0].get_str();
|
||||
claimsForNameType claimsForName = trieCache.getClaimsForName(name);
|
||||
CClaimSupportToName claimsForName = trieCache.getClaimsForName(name);
|
||||
|
||||
UniValue claimObjs(UniValue::VARR);
|
||||
claimSupportMapType claimSupportMap;
|
||||
UniValue claimsObj(UniValue::VARR);
|
||||
UniValue unmatchedSupports(UniValue::VARR);
|
||||
|
||||
for (std::vector<CClaimValue>::const_iterator itClaims = claimsForName.claims.begin(); itClaims != claimsForName.claims.end(); ++itClaims) {
|
||||
claimAndSupportsType claimAndSupports = std::make_pair(*itClaims, std::vector<CSupportValue>());
|
||||
claimSupportMap.insert(std::pair<uint160, claimAndSupportsType>(itClaims->claimId, claimAndSupports));
|
||||
}
|
||||
|
||||
for (std::vector<CSupportValue>::const_iterator itSupports = claimsForName.supports.begin(); itSupports != claimsForName.supports.end(); ++itSupports) {
|
||||
claimSupportMapType::iterator itClaimAndSupports = claimSupportMap.find(itSupports->supportedClaimId);
|
||||
if (itClaimAndSupports == claimSupportMap.end()) {
|
||||
unmatchedSupports.push_back(supportToJSON(*itSupports));
|
||||
const std::vector<CClaimSupport>& claims = claimsForName.claims;
|
||||
for (std::vector<CClaimSupport>::const_iterator it = claims.begin(); it != claims.end(); ++it) {
|
||||
if (!it->claim.claimId.IsNull()) {
|
||||
claimsObj.push_back(claimAndSupportsToJSON(coinsCache, *it));
|
||||
} else {
|
||||
itClaimAndSupports->second.second.push_back(*itSupports);
|
||||
const std::vector<CSupportValue>& support = it->support;
|
||||
for (std::vector<CSupportValue>::const_iterator it = support.begin(); it != support.end(); ++it) {
|
||||
unmatchedSupports.push_back(supportToJSON(*it));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UniValue ret(UniValue::VOBJ);
|
||||
ret.push_back(Pair("nLastTakeoverHeight", claimsForName.nLastTakeoverHeight));
|
||||
|
||||
for (claimSupportMapType::const_iterator itClaimsAndSupports = claimSupportMap.begin(); itClaimsAndSupports != claimSupportMap.end(); ++itClaimsAndSupports) {
|
||||
CAmount nEffectiveAmount = trieCache.getEffectiveAmountForClaim(claimsForName, itClaimsAndSupports->first);
|
||||
UniValue claimObj = claimAndSupportsToJSON(coinsCache, nEffectiveAmount, itClaimsAndSupports);
|
||||
claimObjs.push_back(claimObj);
|
||||
}
|
||||
|
||||
ret.push_back(Pair("claims", claimObjs));
|
||||
ret.push_back(Pair("claims", claimsObj));
|
||||
ret.push_back(Pair("supports without claims", unmatchedSupports));
|
||||
return ret;
|
||||
}
|
||||
|
@ -500,36 +487,37 @@ UniValue getclaimbyid(const UniValue& params, bool fHelp)
|
|||
UniValue claim(UniValue::VOBJ);
|
||||
std::string name;
|
||||
CClaimValue claimValue;
|
||||
pclaimTrie->getClaimById(claimId, name, claimValue);
|
||||
if (claimValue.claimId == claimId)
|
||||
{
|
||||
std::vector<CSupportValue> supports;
|
||||
CAmount effectiveAmount = pclaimTrie->getEffectiveAmountForClaim(name, claimValue.claimId, &supports);
|
||||
if (!pclaimTrie->getClaimById(claimId, name, claimValue))
|
||||
return claim;
|
||||
|
||||
std::string sValue;
|
||||
CCoinsViewCache coins(pcoinsTip);
|
||||
getValueForClaim(coins, claimValue.outPoint, sValue);
|
||||
claim.push_back(Pair("name", name));
|
||||
claim.push_back(Pair("value", sValue));
|
||||
claim.push_back(Pair("claimId", claimValue.claimId.GetHex()));
|
||||
claim.push_back(Pair("txid", claimValue.outPoint.hash.GetHex()));
|
||||
claim.push_back(Pair("n", (int) claimValue.outPoint.n));
|
||||
claim.push_back(Pair("amount", claimValue.nAmount));
|
||||
claim.push_back(Pair("effective amount", effectiveAmount));
|
||||
UniValue supportList(UniValue::VARR);
|
||||
BOOST_FOREACH(const CSupportValue& support, supports) {
|
||||
UniValue supportEntry(UniValue::VOBJ);
|
||||
supportEntry.push_back(Pair("txid", support.outPoint.hash.GetHex()));
|
||||
supportEntry.push_back(Pair("n", (int)support.outPoint.n));
|
||||
supportEntry.push_back(Pair("height", support.nHeight));
|
||||
supportEntry.push_back(Pair("valid at height", support.nValidAtHeight));
|
||||
supportEntry.push_back(Pair("amount", support.nAmount));
|
||||
supportList.push_back(supportEntry);
|
||||
}
|
||||
claim.push_back(Pair("supports", supportList));
|
||||
claim.push_back(Pair("height", claimValue.nHeight));
|
||||
claim.push_back(Pair("valid at height", claimValue.nValidAtHeight));
|
||||
CClaimTrieCache trieCache(pclaimTrie);
|
||||
const CClaimSupport claimSupport = trieCache.getClaimsForName(name).find(claimId);
|
||||
if (!claimSupport)
|
||||
return claim;
|
||||
|
||||
std::string sValue;
|
||||
CCoinsViewCache coins(pcoinsTip);
|
||||
getValueForClaim(coins, claimValue.outPoint, sValue);
|
||||
claim.push_back(Pair("name", name));
|
||||
claim.push_back(Pair("value", sValue));
|
||||
claim.push_back(Pair("claimId", claimValue.claimId.GetHex()));
|
||||
claim.push_back(Pair("txid", claimValue.outPoint.hash.GetHex()));
|
||||
claim.push_back(Pair("n", (int) claimValue.outPoint.n));
|
||||
claim.push_back(Pair("amount", claimValue.nAmount));
|
||||
claim.push_back(Pair("effective amount", claimSupport.effectiveAmount));
|
||||
UniValue supportList(UniValue::VARR);
|
||||
BOOST_FOREACH(const CSupportValue& support, claimSupport.support) {
|
||||
UniValue supportEntry(UniValue::VOBJ);
|
||||
supportEntry.push_back(Pair("txid", support.outPoint.hash.GetHex()));
|
||||
supportEntry.push_back(Pair("n", (int)support.outPoint.n));
|
||||
supportEntry.push_back(Pair("height", support.nHeight));
|
||||
supportEntry.push_back(Pair("valid at height", support.nValidAtHeight));
|
||||
supportEntry.push_back(Pair("amount", support.nAmount));
|
||||
supportList.push_back(supportEntry);
|
||||
}
|
||||
claim.push_back(Pair("supports", supportList));
|
||||
claim.push_back(Pair("height", claimValue.nHeight));
|
||||
claim.push_back(Pair("valid at height", claimValue.nValidAtHeight));
|
||||
return claim;
|
||||
}
|
||||
|
||||
|
|
|
@ -62,28 +62,25 @@ is_best_claim(std::string name, const CTransaction &tx)
|
|||
boost::test_tools::predicate_result
|
||||
best_claim_effective_amount_equals(std::string name, CAmount amount)
|
||||
{
|
||||
CClaimValue val;
|
||||
bool have_info = pclaimTrie->getInfoForName(name, val);
|
||||
if (!have_info)
|
||||
{
|
||||
CClaimTrieCache trieCache(pclaimTrie);
|
||||
std::vector<CClaimSupport> claims = trieCache.getClaimsForName(name).claims;
|
||||
if (claims.empty()) {
|
||||
boost::test_tools::predicate_result res(false);
|
||||
res.message()<<"No claim found";
|
||||
res.message() << "No claim found";
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
CAmount effective_amount = pclaimTrie->getEffectiveAmountForClaim(name, val.claimId);
|
||||
if (effective_amount != amount)
|
||||
{
|
||||
boost::test_tools::predicate_result res(false);
|
||||
res.message()<<amount<<" != "<<effective_amount;
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (claims[0].effectiveAmount != amount) {
|
||||
boost::test_tools::predicate_result res(false);
|
||||
res.message() << amount << " != " << claims[0].effectiveAmount;
|
||||
return res;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
CAmount getEffectiveAmountForClaim(std::string name, const uint160& claimId)
|
||||
{
|
||||
CClaimTrieCache trieCache(pclaimTrie);
|
||||
return trieCache.getClaimsForName(name).find(claimId).effectiveAmount;
|
||||
}
|
||||
|
||||
CMutableTransaction BuildTransaction(const CMutableTransaction& prev, uint32_t prevout=0, unsigned int numOutputs=1)
|
||||
|
@ -357,12 +354,13 @@ BOOST_AUTO_TEST_CASE(claim_test)
|
|||
CMutableTransaction tx3 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2);
|
||||
fixture.IncrementBlocks(1);
|
||||
BOOST_CHECK(is_best_claim("test",tx3));
|
||||
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
|
||||
CClaimTrieCache trieCache(pclaimTrie);
|
||||
BOOST_CHECK_EQUAL(2U, trieCache.getClaimsForName("test").claims.size());
|
||||
|
||||
fixture.DecrementBlocks(1);
|
||||
BOOST_CHECK(!is_best_claim("test",tx2));
|
||||
BOOST_CHECK(!is_best_claim("test",tx3));
|
||||
BOOST_CHECK_EQUAL(0U, pclaimTrie->getClaimsForName("test").claims.size());
|
||||
BOOST_CHECK_EQUAL(0U, trieCache.getClaimsForName("test").claims.size());
|
||||
|
||||
// make two claims , one older
|
||||
CMutableTransaction tx4 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",1);
|
||||
|
@ -374,7 +372,7 @@ BOOST_AUTO_TEST_CASE(claim_test)
|
|||
BOOST_CHECK(is_best_claim("test", tx4));
|
||||
fixture.IncrementBlocks(1);
|
||||
BOOST_CHECK(is_best_claim("test",tx4));
|
||||
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
|
||||
BOOST_CHECK_EQUAL(2U, trieCache.getClaimsForName("test").claims.size());
|
||||
|
||||
fixture.DecrementBlocks(1);
|
||||
BOOST_CHECK(is_best_claim("test", tx4));
|
||||
|
@ -394,7 +392,7 @@ BOOST_AUTO_TEST_CASE(claim_test)
|
|||
BOOST_CHECK(is_best_claim("test", tx6));
|
||||
fixture.IncrementBlocks(10);
|
||||
BOOST_CHECK(is_best_claim("test",tx7));
|
||||
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
|
||||
BOOST_CHECK_EQUAL(2U, trieCache.getClaimsForName("test").claims.size());
|
||||
|
||||
fixture.DecrementBlocks(10);
|
||||
BOOST_CHECK(is_claim_in_queue("test",tx7));
|
||||
|
@ -596,7 +594,8 @@ BOOST_AUTO_TEST_CASE(support_spend_test)
|
|||
CMutableTransaction s2 = fixture.MakeSupport(fixture.GetCoinbase(),tx5,"test",2);
|
||||
fixture.IncrementBlocks(1);
|
||||
BOOST_CHECK(is_best_claim("test",tx5));
|
||||
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
|
||||
CClaimTrieCache trieCache(pclaimTrie);
|
||||
BOOST_CHECK_EQUAL(2U, trieCache.getClaimsForName("test").claims.size());
|
||||
|
||||
// build the spend where s2 is sppent on txin[1] and tx3 is spent on txin[0]
|
||||
uint32_t prevout = 0;
|
||||
|
@ -661,7 +660,8 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test)
|
|||
CMutableTransaction u3 = fixture.MakeUpdate(tx3, "test", "one", ClaimIdHash(tx3.GetHash(), 0), 2);
|
||||
fixture.IncrementBlocks(1);
|
||||
BOOST_CHECK(is_best_claim("test",u3));
|
||||
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
|
||||
CClaimTrieCache trieCache(pclaimTrie);
|
||||
BOOST_CHECK_EQUAL(2U, trieCache.getClaimsForName("test").claims.size());
|
||||
fixture.DecrementBlocks(11);
|
||||
|
||||
// losing update on winning claim happens without delay
|
||||
|
@ -669,7 +669,7 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test)
|
|||
CMutableTransaction tx6 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2);
|
||||
fixture.IncrementBlocks(10);
|
||||
BOOST_CHECK(is_best_claim("test", tx5));
|
||||
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
|
||||
BOOST_CHECK_EQUAL(2U, trieCache.getClaimsForName("test").claims.size());
|
||||
CMutableTransaction u4 = fixture.MakeUpdate(tx5, "test", "one", ClaimIdHash(tx5.GetHash(), 0), 1);
|
||||
fixture.IncrementBlocks(1);
|
||||
BOOST_CHECK(is_best_claim("test",tx6));
|
||||
|
@ -778,7 +778,7 @@ BOOST_AUTO_TEST_CASE(claimtrie_expire_test)
|
|||
}
|
||||
|
||||
/*
|
||||
* tests for CClaimTrie::getEffectiveAmountForClaim
|
||||
* tests effectiveAmount
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(claimtriebranching_get_effective_amount_for_claim)
|
||||
{
|
||||
|
@ -788,38 +788,38 @@ BOOST_AUTO_TEST_CASE(claimtriebranching_get_effective_amount_for_claim)
|
|||
uint160 claimId = ClaimIdHash(claimtx.GetHash(), 0);
|
||||
fixture.IncrementBlocks(1);
|
||||
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 2);
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId) == 0); //not found returns 0
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId) == 2);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("inexistent", claimId) == 0); //not found returns 0
|
||||
|
||||
// one claim, one support
|
||||
fixture.MakeSupport(fixture.GetCoinbase(), claimtx, "test", 40);
|
||||
fixture.IncrementBlocks(1);
|
||||
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
|
||||
// Two claims, first one with supports
|
||||
CMutableTransaction claimtx2 = fixture.MakeClaim(fixture.GetCoinbase(), "test", "two", 1);
|
||||
uint160 claimId2 = ClaimIdHash(claimtx2.GetHash(), 0);
|
||||
fixture.IncrementBlocks(10);
|
||||
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 1);
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId) == 0);
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId2) == 0);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId2) == 1);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("inexistent", claimId) == 0);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("inexistent", claimId2) == 0);
|
||||
|
||||
// Two claims, both with supports, second claim effective amount being less than first claim
|
||||
fixture.MakeSupport(fixture.GetCoinbase(), claimtx2, "test", 6);
|
||||
fixture.IncrementBlocks(13); //delay
|
||||
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 7);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId2) == 7);
|
||||
|
||||
// Two claims, both with supports, second one taking over
|
||||
fixture.MakeSupport(fixture.GetCoinbase(), claimtx2, "test", 1330);
|
||||
fixture.IncrementBlocks(26); //delay
|
||||
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 1337);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId) == 42);
|
||||
BOOST_CHECK(getEffectiveAmountForClaim("test", claimId2) == 1337);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -3361,8 +3361,8 @@ BOOST_AUTO_TEST_CASE(getclaimsforname_test)
|
|||
UniValue claims = results["claims"];
|
||||
BOOST_CHECK(claims.size() == 2U);
|
||||
BOOST_CHECK(results["nLastTakeoverHeight"].get_int() == height + 1);
|
||||
BOOST_CHECK(claims[0]["nEffectiveAmount"].get_int() == 0);
|
||||
BOOST_CHECK(claims[1]["nEffectiveAmount"].get_int() == 2);
|
||||
BOOST_CHECK(claims[0]["nEffectiveAmount"].get_int() == 2); // best claim
|
||||
BOOST_CHECK(claims[1]["nEffectiveAmount"].get_int() == 0);
|
||||
BOOST_CHECK(claims[0]["supports"].size() == 0U);
|
||||
BOOST_CHECK(claims[1]["supports"].size() == 0U);
|
||||
|
||||
|
@ -3372,7 +3372,7 @@ BOOST_AUTO_TEST_CASE(getclaimsforname_test)
|
|||
claims = results["claims"];
|
||||
BOOST_CHECK(claims.size() == 2U);
|
||||
BOOST_CHECK(results["nLastTakeoverHeight"].get_int() == height + 3);
|
||||
BOOST_CHECK(claims[0]["nEffectiveAmount"].get_int() == 3);
|
||||
BOOST_CHECK(claims[0]["nEffectiveAmount"].get_int() == 3); // best claim
|
||||
BOOST_CHECK(claims[1]["nEffectiveAmount"].get_int() == 2);
|
||||
BOOST_CHECK(claims[0]["supports"].size() == 0U);
|
||||
BOOST_CHECK(claims[1]["supports"].size() == 0U);
|
||||
|
|
|
@ -212,12 +212,13 @@ BOOST_AUTO_TEST_CASE(basic_insertion_info_test)
|
|||
CClaimValue claimVal(claimOutPoint, claimId, amount, height, validHeight);
|
||||
ctc.insertClaimIntoTrie("test", claimVal);
|
||||
|
||||
// try getClaimsForName, getEffectiveAmountForClaim, getInfoForName
|
||||
claimsForNameType res = ctc.getClaimsForName("test");
|
||||
// try getClaimsForName, getInfoForName
|
||||
CClaimSupportToName res = ctc.getClaimsForName("test");
|
||||
BOOST_CHECK(res.claims.size() == 1);
|
||||
BOOST_CHECK(res.claims[0] == claimVal);
|
||||
BOOST_CHECK(res.claims[0].claim == claimVal);
|
||||
BOOST_CHECK(res.claims[0].claimId == claimId);
|
||||
|
||||
BOOST_CHECK_EQUAL(10, ctc.getEffectiveAmountForClaim("test", claimId));
|
||||
BOOST_CHECK_EQUAL(10, res.claims[0].effectiveAmount);
|
||||
|
||||
CClaimValue claim;
|
||||
BOOST_CHECK(ctc.getInfoForName("test", claim));
|
||||
|
@ -232,8 +233,10 @@ BOOST_AUTO_TEST_CASE(basic_insertion_info_test)
|
|||
CSupportValue support(supportOutPoint, claimId, supportAmount, height, validHeight);
|
||||
ctc.insertSupportIntoMap("test", support, false);
|
||||
|
||||
// try getEffectiveAmount
|
||||
BOOST_CHECK_EQUAL(20, ctc.getEffectiveAmountForClaim("test", claimId));
|
||||
// try effectiveAmount again
|
||||
CClaimSupportToName res1 = ctc.getClaimsForName("test");
|
||||
BOOST_CHECK(res1.claims.size() == 1);
|
||||
BOOST_CHECK_EQUAL(20, res1.claims[0].effectiveAmount);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(recursive_prune_test)
|
||||
|
|
Loading…
Add table
Reference in a new issue