Complete cache trie by merging base one in #300

Closed
bvbfan wants to merge 1 commit from fix_cache_on_rollback into master
4 changed files with 27 additions and 7 deletions

View file

@ -579,6 +579,13 @@ bool CClaimTrieCacheBase::ReadFromDisk(const CBlockIndex* tip)
return false;
}
void CClaimTrieCacheBase::mergeTrieIntoCache()
{
for (auto it = base->cbegin(); it != base->cend(); ++it)
if (!nodesAlreadyCached.count(it.key()))
nodesToAddOrUpdate.copy(it);
}
CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrie* base) : base(base)
{
assert(base);
@ -721,11 +728,9 @@ bool CClaimTrieCacheBase::removeClaimFromTrie(const std::string& name, const COu
it->reorderClaims(supports);
} else {
// in case we pull a child into our spot; we will then need their kids for hash
bool hasChild = false;
for (auto& child: it.children()) {
hasChild = true;
bool hasChild = it.hasChildren();
for (auto& child: it.children())
cacheData(child.key(), false);
}
nodesToAddOrUpdate.erase(name);
nodesToDelete.insert(name);

View file

@ -445,6 +445,8 @@ public:
CClaimTrie::const_iterator end() const;
CClaimTrie::const_iterator find(const std::string& name) const;
void mergeTrieIntoCache();
void dumpToLog(CClaimTrie::const_iterator it, bool diffFromBase = true) const;
protected:

View file

@ -182,6 +182,7 @@ static UniValue getclaimsintrie(const JSONRPCRequest& request)
if (!request.params.empty()) {
CBlockIndex* blockIndex = BlockHashIndex(ParseHashV(request.params[0], "blockhash (optional parameter 1)"));
RollBackTo(blockIndex, coinsCache, trieCache);
trieCache.mergeTrieIntoCache();
}
UniValue ret(UniValue::VARR);
@ -266,6 +267,7 @@ static UniValue getnamesintrie(const JSONRPCRequest& request)
if (!request.params.empty()) {
CBlockIndex* blockIndex = BlockHashIndex(ParseHashV(request.params[0], "blockhash (optional parameter 1)"));
RollBackTo(blockIndex, coinsCache, trieCache);
trieCache.mergeTrieIntoCache();
}
UniValue ret(UniValue::VARR);

View file

@ -3908,11 +3908,17 @@ BOOST_AUTO_TEST_CASE(getnamesintrie_test)
{
ClaimTrieChainFixture fixture;
std::string sName1("test");
std::string sName2("test2");
std::string sName3("guest");
std::string sValue1("test");
fixture.MakeClaim(fixture.GetCoinbase(), sName1, sValue1, 42);
fixture.MakeClaim(fixture.GetCoinbase(), sName2, sValue1, 42);
fixture.IncrementBlocks(1);
uint256 blockHash = chainActive.Tip()->GetBlockHash();
fixture.MakeClaim(fixture.GetCoinbase(), sName1, sValue1, 42);
fixture.MakeClaim(fixture.GetCoinbase(), sName3, sValue1, 42);
fixture.IncrementBlocks(1);
rpcfn_type getnamesintrie = tableRPC["getnamesintrie"]->actor;
@ -3920,12 +3926,17 @@ BOOST_AUTO_TEST_CASE(getnamesintrie_test)
req.params = UniValue(UniValue::VARR);
UniValue results = getnamesintrie(req);
BOOST_CHECK_EQUAL(results.size(), 1U);
BOOST_CHECK_EQUAL(results.size(), 3U);
BOOST_CHECK_EQUAL(results[0].get_str(), sName3);
BOOST_CHECK_EQUAL(results[1].get_str(), sName1);
BOOST_CHECK_EQUAL(results[2].get_str(), sName2);
req.params.push_back(blockHash.GetHex());
results = getnamesintrie(req);
BOOST_CHECK_EQUAL(results.size(), 0U);
BOOST_CHECK_EQUAL(results.size(), 2U);
BOOST_CHECK_EQUAL(results[0].get_str(), sName1);
BOOST_CHECK_EQUAL(results[1].get_str(), sName2);
}
BOOST_AUTO_TEST_CASE(getvalueforname_test)