diff --git a/src/claimtrie.cpp b/src/claimtrie.cpp index d075dbc06..d6b6fdcdc 100644 --- a/src/claimtrie.cpp +++ b/src/claimtrie.cpp @@ -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); diff --git a/src/claimtrie.h b/src/claimtrie.h index 46c4e8088..2ede31e03 100644 --- a/src/claimtrie.h +++ b/src/claimtrie.h @@ -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: diff --git a/src/rpc/claimtrie.cpp b/src/rpc/claimtrie.cpp index 9a4e13c5d..1c56cfb23 100644 --- a/src/rpc/claimtrie.cpp +++ b/src/rpc/claimtrie.cpp @@ -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); diff --git a/src/test/claimtriebranching_tests.cpp b/src/test/claimtriebranching_tests.cpp index d58601bbe..45b5c8fd5 100644 --- a/src/test/claimtriebranching_tests.cpp +++ b/src/test/claimtriebranching_tests.cpp @@ -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)