Adding effective amount function to CClaimTrie

This commit is contained in:
Kay Kurokawa 2017-01-04 13:57:28 -05:00
parent 6542b53b36
commit 86592934cb
4 changed files with 50 additions and 14 deletions

View file

@ -525,6 +525,31 @@ claimsForNameType CClaimTrie::getClaimsForName(const std::string& name) const
return allClaims;
}
//return effective amount form claim, retuns 0 if claim is not found
CAmount CClaimTrie::getEffectiveAmountForClaim(const std::string& name, uint160 claimId) const
{
claimsForNameType claims = getClaimsForName(name);
CAmount effectiveAmount = 0;
bool claim_found = false;
for (std::vector<CClaimValue>::iterator it=claims.claims.begin(); it!=claims.claims.end(); ++it)
{
if (it->claimId == claimId && it->nValidAtHeight < nCurrentHeight)
effectiveAmount += it->nAmount;
claim_found = true;
break;
}
if (!claim_found)
return effectiveAmount;
for (std::vector<CSupportValue>::iterator it=claims.supports.begin(); it!=claims.supports.end(); ++it)
{
if (it->supportedClaimId == claimId && it->nValidAtHeight < nCurrentHeight)
effectiveAmount += it->nAmount;
}
return effectiveAmount;
}
bool CClaimTrie::checkConsistency() const
{
if (empty())

View file

@ -302,6 +302,7 @@ public:
bool getLastTakeoverForName(const std::string& name, int& lastTakeoverHeight) const;
claimsForNameType getClaimsForName(const std::string& name) const;
CAmount getEffectiveAmountForClaim(const std::string& name, uint160 claimId) const;
bool queueEmpty() const;
bool supportEmpty() const;

View file

@ -187,7 +187,7 @@ UniValue getvalueforname(const UniValue& params, bool fHelp)
ret.push_back(Pair("txid", claim.outPoint.hash.GetHex()));
ret.push_back(Pair("n", (int)claim.outPoint.n));
ret.push_back(Pair("amount", claim.nAmount));
ret.push_back(Pair("effective amount", claim.nEffectiveAmount));
ret.push_back(Pair("effective amount", pclaimTrie->getEffectiveAmountForClaim(name, claim.claimId)));
ret.push_back(Pair("height", claim.nHeight));
return ret;
}

View file

@ -71,16 +71,20 @@ best_claim_effective_amount_equals(std::string name, CAmount amount)
res.message()<<"No claim found";
return res;
}
else if (val.nEffectiveAmount != amount)
else
{
CAmount effective_amount = pclaimTrie->getEffectiveAmountForClaim(name, val.claimId);
if (val.nEffectiveAmount != amount)
{
boost::test_tools::predicate_result res(false);
res.message()<<amount<<" != "<<val.nEffectiveAmount;
res.message()<<amount<<" != "<<effective_amount;
return res;
}
else
{
return true;
}
}
}
@ -442,6 +446,7 @@ BOOST_AUTO_TEST_CASE(claimtriebranching_support)
CMutableTransaction s2 = fixture.MakeSupport(fixture.GetCoinbase(),tx2,"test",10);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx2));
BOOST_CHECK(best_claim_effective_amount_equals("test",11));
fixture.DecrementBlocks(1);
// check support delay
@ -449,16 +454,21 @@ BOOST_AUTO_TEST_CASE(claimtriebranching_support)
CMutableTransaction tx4 = fixture.MakeClaim(fixture.GetCoinbase(),"test","two",2);
fixture.IncrementBlocks(10);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
CMutableTransaction s4 = fixture.MakeSupport(fixture.GetCoinbase(),tx3,"test",10); //10 delay
fixture.IncrementBlocks(10);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx3));
BOOST_CHECK(best_claim_effective_amount_equals("test",11));
fixture.DecrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
fixture.DecrementBlocks(10);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
fixture.DecrementBlocks(10);
}
/*