Introduce pending amount, the value when claim and its supports got valid

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2019-09-02 16:39:01 +03:00 committed by Brannon King
parent d2b78a7fc3
commit f71d6a4263
5 changed files with 78 additions and 6 deletions

View file

@ -391,27 +391,44 @@ bool CClaimTrieCacheBase::getInfoForName(const std::string& name, CClaimValue& c
return base->find(name, claims) && claims.getBestClaim(claim); return base->find(name, claims) && claims.getBestClaim(claim);
} }
template <typename T>
void CClaimTrieCacheBase::insertRowsFromQueue(std::vector<T>& result, const std::string& name) const
{
supportedType<T>();
if (auto nameRows = getQueueCacheNameRow<T>(name))
for (auto& nameRow : *nameRows)
if (auto rows = getQueueCacheRow<T>(nameRow.nHeight))
for (auto& row : *rows)
if (row.first == name)
result.push_back(row.second);
}
CClaimSupportToName CClaimTrieCacheBase::getClaimsForName(const std::string& name) const CClaimSupportToName CClaimTrieCacheBase::getClaimsForName(const std::string& name) const
{ {
claimEntryType claims; claimEntryType claims;
int nLastTakeoverHeight = 0; int nLastTakeoverHeight = 0;
auto supports = getSupportsForName(name); auto supports = getSupportsForName(name);
insertRowsFromQueue(supports, name);
CClaimTrieData data;
if (auto it = nodesToAddOrUpdate.find(name)) { if (auto it = nodesToAddOrUpdate.find(name)) {
claims = it->claims; claims = it->claims;
nLastTakeoverHeight = it->nHeightOfLastTakeover; nLastTakeoverHeight = it->nHeightOfLastTakeover;
} else if (!nodesToDelete.count(name)) {
CClaimTrieData data;
if (base->find(name, data)) {
claims = data.claims;
nLastTakeoverHeight = data.nHeightOfLastTakeover;
}
} }
else if (!nodesToDelete.count(name) && base->find(name, data)) { insertRowsFromQueue(claims, name);
claims = data.claims;
nLastTakeoverHeight = data.nHeightOfLastTakeover;
}
auto find = [&supports](decltype(supports)::iterator& it, const CClaimValue& claim) { auto find = [&supports](decltype(supports)::iterator& it, const CClaimValue& claim) {
it = std::find_if(it, supports.end(), [&claim](const CSupportValue& support) { it = std::find_if(it, supports.end(), [&claim](const CSupportValue& support) {
return claim.claimId == support.supportedClaimId; return claim.claimId == support.supportedClaimId;
}); });
return it != supports.end(); return it != supports.end();
}; };
// match support to claim // match support to claim
std::vector<CClaimNsupports> claimsNsupports; std::vector<CClaimNsupports> claimsNsupports;
for (const auto& claim : claims) { for (const auto& claim : claims) {

View file

@ -628,6 +628,9 @@ private:
bool validateTrieConsistency(const CBlockIndex* tip); bool validateTrieConsistency(const CBlockIndex* tip);
template <typename T>
void insertRowsFromQueue(std::vector<T>& result, const std::string& name) const;
template <typename T> template <typename T>
std::vector<queueEntryType<T>>* getQueueCacheRow(int nHeight, bool createIfNotExists); std::vector<queueEntryType<T>>* getQueueCacheRow(int nHeight, bool createIfNotExists);

View file

@ -45,6 +45,7 @@
#define T_CLAIMSREMOVED "claimsRemoved" #define T_CLAIMSREMOVED "claimsRemoved"
#define T_SUPPORTSREMOVED "supportsRemoved" #define T_SUPPORTSREMOVED "supportsRemoved"
#define T_ADDRESS "address" #define T_ADDRESS "address"
#define T_PENDINGAMOUNT "pendingAmount"
enum { enum {
GETCLAIMSINTRIE = 0, GETCLAIMSINTRIE = 0,
@ -89,6 +90,7 @@ S3(" ", T_HEIGHT, " (numeric) the height of the block in whic
S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") \ S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") \
S3(" ", T_AMOUNT, " (numeric) the amount of the claim") \ S3(" ", T_AMOUNT, " (numeric) the amount of the claim") \
S3(" ", T_EFFECTIVEAMOUNT, " (numeric) the amount plus amount from all supports associated with the claim") \ S3(" ", T_EFFECTIVEAMOUNT, " (numeric) the amount plus amount from all supports associated with the claim") \
S3(" ", T_PENDINGAMOUNT, " (numeric) expected amount when claim and its support got valid") \
S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim") \ S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim") \
S3(" ", T_VALUE, " (string) the metadata of the support if any") \ S3(" ", T_VALUE, " (string) the metadata of the support if any") \
S3(" ", T_ADDRESS, " (string) the destination address of the support") \ S3(" ", T_ADDRESS, " (string) the destination address of the support") \
@ -184,6 +186,7 @@ S3(" ", T_HEIGHT, " (numeric) the height of the block in whic
S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the claim became/becomes valid") S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the claim became/becomes valid")
S3(" ", T_AMOUNT, " (numeric) the amount of the claim") S3(" ", T_AMOUNT, " (numeric) the amount of the claim")
S3(" ", T_EFFECTIVEAMOUNT, " (numeric) the amount plus amount from all supports associated with the claim") S3(" ", T_EFFECTIVEAMOUNT, " (numeric) the amount plus amount from all supports associated with the claim")
S3(" ", T_PENDINGAMOUNT, " (numeric) expected amount when claim and its support got valid")
S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim") S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim")
S3(" ", T_VALUE, " (string) the metadata of the support if any") S3(" ", T_VALUE, " (string) the metadata of the support if any")
S3(" ", T_ADDRESS, " (string) the destination address of the support") S3(" ", T_ADDRESS, " (string) the destination address of the support")

View file

@ -245,6 +245,14 @@ UniValue supportToJSON(const CCoinsViewCache& coinsCache, const CSupportValue& s
return ret; return ret;
} }
CAmount amountToClaim(const CClaimNsupports& claimNsupports)
{
auto fullAmount = claimNsupports.claim.nAmount;
for (auto& support : claimNsupports.supports)
fullAmount += support.nAmount;
return fullAmount;
}
UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, const CClaimNsupports& claimNsupports) UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, const CClaimNsupports& claimNsupports)
{ {
auto& claim = claimNsupports.claim; auto& claim = claimNsupports.claim;
@ -253,6 +261,10 @@ UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, const CClaimN
auto result = claimToJSON(coinsCache, claim); auto result = claimToJSON(coinsCache, claim);
result.pushKV(T_EFFECTIVEAMOUNT, claimNsupports.effectiveAmount); result.pushKV(T_EFFECTIVEAMOUNT, claimNsupports.effectiveAmount);
auto fullAmount = amountToClaim(claimNsupports);
if (fullAmount > claimNsupports.effectiveAmount)
result.pushKV(T_PENDINGAMOUNT, fullAmount);
UniValue supportObjs(UniValue::VARR); UniValue supportObjs(UniValue::VARR);
for (auto& support : supports) for (auto& support : supports)
supportObjs.push_back(supportToJSON(coinsCache, support)); supportObjs.push_back(supportToJSON(coinsCache, support));

View file

@ -4007,7 +4007,7 @@ BOOST_AUTO_TEST_CASE(getclaimsforname_test)
UniValue results = getclaimsforname(req); UniValue results = getclaimsforname(req);
UniValue claims = results[T_CLAIMS]; UniValue claims = results[T_CLAIMS];
BOOST_CHECK_EQUAL(claims.size(), 1U); BOOST_CHECK_EQUAL(claims.size(), 2U);
BOOST_CHECK_EQUAL(results[T_LASTTAKEOVERHEIGHT].get_int(), height + 1); BOOST_CHECK_EQUAL(results[T_LASTTAKEOVERHEIGHT].get_int(), height + 1);
BOOST_CHECK_EQUAL(claims[0][T_EFFECTIVEAMOUNT].get_int(), 2); BOOST_CHECK_EQUAL(claims[0][T_EFFECTIVEAMOUNT].get_int(), 2);
BOOST_CHECK_EQUAL(claims[0][T_SUPPORTS].size(), 0U); BOOST_CHECK_EQUAL(claims[0][T_SUPPORTS].size(), 0U);
@ -4460,4 +4460,41 @@ BOOST_AUTO_TEST_CASE(hash_bid_seq_claim_changes_test)
BOOST_CHECK_EQUAL(result[T_SUPPORTSREMOVED].size(), 0); BOOST_CHECK_EQUAL(result[T_SUPPORTSREMOVED].size(), 0);
} }
BOOST_AUTO_TEST_CASE(claim_rpc_pending_amount_test)
{
ClaimTrieChainFixture fixture;
std::string sName1("test");
std::string sValue1("test1");
std::string sValue2("test2");
rpcfn_type getclaimsforname = tableRPC["getclaimsforname"]->actor;
JSONRPCRequest req;
req.params = UniValue(UniValue::VARR);
req.params.push_back(UniValue(sName1));
fixture.MakeClaim(fixture.GetCoinbase(), sName1, sValue1, 1);
fixture.IncrementBlocks(1);
fixture.MakeClaim(fixture.GetCoinbase(), sName1, sValue2, 2);
fixture.IncrementBlocks(1);
auto claims = getclaimsforname(req)[T_CLAIMS];
BOOST_CHECK_EQUAL(claims.size(), 2U);
BOOST_CHECK_EQUAL(claims[0][T_EFFECTIVEAMOUNT].get_int(), 1);
BOOST_CHECK(!claims[0].exists(T_PENDINGAMOUNT));
BOOST_CHECK_EQUAL(claims[1][T_EFFECTIVEAMOUNT].get_int(), 0);
BOOST_CHECK(claims[1].exists(T_PENDINGAMOUNT));
BOOST_CHECK_EQUAL(claims[1][T_PENDINGAMOUNT].get_int(), 2);
fixture.IncrementBlocks(1);
claims = getclaimsforname(req)[T_CLAIMS];
BOOST_CHECK_EQUAL(claims.size(), 2U);
BOOST_CHECK_EQUAL(claims[0][T_EFFECTIVEAMOUNT].get_int(), 2);
BOOST_CHECK(!claims[0].exists(T_PENDINGAMOUNT));
BOOST_CHECK_EQUAL(claims[1][T_EFFECTIVEAMOUNT].get_int(), 1);
BOOST_CHECK(!claims[1].exists(T_PENDINGAMOUNT));
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()