repaired getchangesinblock

This commit is contained in:
Brannon King 2019-12-12 17:04:36 -07:00
parent ce9950b6f1
commit 8a5cc01111
5 changed files with 70 additions and 10 deletions

View file

@ -888,3 +888,40 @@ void CClaimTrieCacheBase::getNamesInTrie(std::function<void(const std::string&)>
callback(name); callback(name);
}; };
} }
std::vector<CUint160> CClaimTrieCacheBase::getActivatedClaims(int height) {
std::vector<CUint160> ret;
auto query = db << "SELECT DISTINCT claimID FROM claim WHERE activationHeight = ?1 AND blockHeight < ?1" << height;
for (auto&& row: query) {
ret.emplace_back();
row >> ret.back();
}
return ret;
}
std::vector<CUint160> CClaimTrieCacheBase::getClaimsWithActivatedSupports(int height) {
std::vector<CUint160> ret;
auto query = db << "SELECT DISTINCT supportedClaimID FROM support WHERE activationHeight = ?1 AND blockHeight < ?1" << height;
for (auto&& row: query) {
ret.emplace_back();
row >> ret.back();
}
return ret;
}
std::vector<CUint160> CClaimTrieCacheBase::getExpiredClaims(int height) {
std::vector<CUint160> ret;
auto query = db << "SELECT DISTINCT claimID FROM claim WHERE expirationHeight = ?1 AND blockHeight < ?1" << height;
for (auto&& row: query) {
ret.emplace_back();
row >> ret.back();
}
return ret;
}
std::vector<CUint160> CClaimTrieCacheBase::getClaimsWithExpiredSupports(int height) {
std::vector<CUint160> ret;
auto query = db << "SELECT DISTINCT supportedClaimID FROM support WHERE expirationHeight = ?1 AND blockHeight < ?1" << height;
for (auto&& row: query) {
ret.emplace_back();
row >> ret.back();
}
return ret;
}

View file

@ -102,6 +102,11 @@ public:
bool getLastTakeoverForName(const std::string& name, CUint160& claimId, int& takeoverHeight) const; bool getLastTakeoverForName(const std::string& name, CUint160& claimId, int& takeoverHeight) const;
bool findNameForClaim(std::vector<unsigned char> claim, CClaimValue& value, std::string& name) const; bool findNameForClaim(std::vector<unsigned char> claim, CClaimValue& value, std::string& name) const;
std::vector<CUint160> getActivatedClaims(int height);
std::vector<CUint160> getClaimsWithActivatedSupports(int height);
std::vector<CUint160> getExpiredClaims(int height);
std::vector<CUint160> getClaimsWithExpiredSupports(int height);
protected: protected:
int nNextHeight; // Height of the block that is being worked on, which is int nNextHeight; // Height of the block that is being worked on, which is
CClaimTrie* base; CClaimTrie* base;

View file

@ -751,23 +751,35 @@ UniValue getchangesinblock(const JSONRPCRequest& request)
LOCK(cs_main); LOCK(cs_main);
bool allowSupportMetadata; bool allowSupportMetadata;
CCoinsViewCache coinsCache(pcoinsTip.get()); CCoinsViewCache coinsCache(pcoinsTip.get());
{
auto index = chainActive.Tip();
if (request.params.size() > 0)
index = BlockHashIndex(ParseHashV(request.params[0], T_BLOCKHASH " (optional parameter)"));
if (!ReadBlockFromDisk(block, index, Params().GetConsensus()))
throw JSONRPCError(RPC_INTERNAL_ERROR,
"Unable to read the undo block for height " + std::to_string(index->nHeight));
allowSupportMetadata = index->nHeight >= Params().GetConsensus().nAllClaimsInMerkleForkHeight;
}
UniValue claimsAddUp(UniValue::VARR), UniValue claimsAddUp(UniValue::VARR),
claimsRm(UniValue::VARR), claimsRm(UniValue::VARR),
supportsAddUp(UniValue::VARR), supportsAddUp(UniValue::VARR),
supportsRm(UniValue::VARR); supportsRm(UniValue::VARR);
{
auto index = chainActive.Tip();
if (request.params.size() > 0)
index = BlockHashIndex(ParseHashV(request.params[0], T_BLOCKHASH " (optional parameter)"));
CClaimTrieCache trieCache(pclaimTrie);
RollBackTo(index, coinsCache, trieCache);
if (!ReadBlockFromDisk(block, index, Params().GetConsensus()))
throw JSONRPCError(RPC_INTERNAL_ERROR,
"Unable to read the undo block for height " + std::to_string(index->nHeight));
allowSupportMetadata = index->nHeight >= Params().GetConsensus().nAllClaimsInMerkleForkHeight;
for (auto& claim: trieCache.getActivatedClaims(index->nHeight))
claimsAddUp.push_back(claim.ToString());
for (auto& claim: trieCache.getClaimsWithActivatedSupports(index->nHeight))
supportsAddUp.push_back(claim.ToString());
for (auto& claim: trieCache.getExpiredClaims(index->nHeight))
claimsRm.push_back(claim.ToString());
for (auto& claim: trieCache.getClaimsWithExpiredSupports(index->nHeight))
supportsRm.push_back(claim.ToString());
}
int op; int op;
std::vector<std::vector<unsigned char> > vvchParams; std::vector<std::vector<unsigned char> > vvchParams;
@ -802,6 +814,7 @@ UniValue getchangesinblock(const JSONRPCRequest& request)
break; break;
case OP_SUPPORT_CLAIM: case OP_SUPPORT_CLAIM:
supportsRm.push_back(uint160(vvchParams[1]).ToString()); supportsRm.push_back(uint160(vvchParams[1]).ToString());
break;
} }
} }
@ -815,11 +828,14 @@ UniValue getchangesinblock(const JSONRPCRequest& request)
switch (op) { switch (op) {
case OP_CLAIM_NAME: case OP_CLAIM_NAME:
case OP_UPDATE_CLAIM:
claimsAddUp.push_back(ClaimIdHash(tx->GetHash(), i).ToString()); claimsAddUp.push_back(ClaimIdHash(tx->GetHash(), i).ToString());
break; break;
case OP_UPDATE_CLAIM:
claimsAddUp.push_back(uint160(vvchParams[1]).ToString());
break;
case OP_SUPPORT_CLAIM: case OP_SUPPORT_CLAIM:
supportsAddUp.push_back(ClaimIdHash(tx->GetHash(), i).ToString()); supportsAddUp.push_back(uint160(vvchParams[1]).ToString());
break;
} }
} }
} }

View file

@ -360,8 +360,10 @@ BOOST_AUTO_TEST_CASE(hash_bid_seq_claim_changes_test)
req.params.push_back(UniValue(blockhash.GetHex())); req.params.push_back(UniValue(blockhash.GetHex()));
result = getchangesinblock(req); result = getchangesinblock(req);
BOOST_CHECK_EQUAL(result[T_CLAIMSADDEDORUPDATED].size(), 1); BOOST_CHECK_EQUAL(result[T_CLAIMSADDEDORUPDATED].size(), 3);
BOOST_CHECK_EQUAL(result[T_CLAIMSADDEDORUPDATED][0].get_str(), claimId4.GetHex()); BOOST_CHECK_EQUAL(result[T_CLAIMSADDEDORUPDATED][0].get_str(), claimId2.GetHex());
BOOST_CHECK_EQUAL(result[T_CLAIMSADDEDORUPDATED][1].get_str(), claimId3.GetHex());
BOOST_CHECK_EQUAL(result[T_CLAIMSADDEDORUPDATED][2].get_str(), claimId4.GetHex());
BOOST_CHECK_EQUAL(result[T_CLAIMSREMOVED].size(), 0); BOOST_CHECK_EQUAL(result[T_CLAIMSREMOVED].size(), 0);
BOOST_CHECK_EQUAL(result[T_SUPPORTSADDEDORUPDATED].size(), 0); BOOST_CHECK_EQUAL(result[T_SUPPORTSADDEDORUPDATED].size(), 0);
BOOST_CHECK_EQUAL(result[T_SUPPORTSREMOVED].size(), 0); BOOST_CHECK_EQUAL(result[T_SUPPORTSREMOVED].size(), 0);