Simplify the claim index by reducing the memory cache in exchange for

slightly higher latency calls to leveldb

Add a simple claim by id test
This commit is contained in:
lbrynaut 2018-05-07 09:44:01 -05:00 committed by Umpei Kay Kurokawa
parent a0221c3bf7
commit d5df3ff10d
3 changed files with 14 additions and 78 deletions

View file

@ -593,36 +593,19 @@ bool CClaimTrie::recursiveCheckConsistency(const CClaimTrieNode* node) const
void CClaimTrie::addToClaimIndex(const std::string& name, const CClaimValue& claim)
{
// sanity checks only
claimIndexType::const_iterator itClaim = claimIndex.find(claim.claimId);
if (itClaim != claimIndex.end() && itClaim->second.claim != claim)
LogPrintf("%s: WARNING: ClaimIndex Conflict on claim id %s\n", __func__, claim.claimId.GetHex());
CClaimIndexElement element = { name, claim };
claimIndex[claim.claimId] = element;
LogPrintf("%s: ClaimIndex[%s] updated %s\n", __func__, claim.claimId.GetHex(), name);
db.Write(std::make_pair(CLAIM_BY_ID, claim.claimId), element);
}
void CClaimTrie::removeFromClaimIndex(const CClaimValue& claim)
{
claimIndexType::iterator itClaim = claimIndex.find(claim.claimId);
if (itClaim != claimIndex.end())
{
LogPrintf("%s: ClaimIndex[%s] removing %s\n", __func__, claim.claimId.GetHex(), itClaim->second.name);
claimIndex.erase(itClaim);
}
db.Erase(std::make_pair(CLAIM_BY_ID, claim.claimId));
}
bool CClaimTrie::getClaimById(const uint160 claimId, std::string& name, CClaimValue& claim) const
{
claimIndexType::const_iterator itClaim = claimIndex.find(claimId);
if (itClaim != claimIndex.end())
{
name = itClaim->second.name;
claim = itClaim->second.claim;
return true;
}
CClaimIndexElement element;
if (db.Read(std::make_pair(CLAIM_BY_ID, claimId), element))
{
@ -969,16 +952,6 @@ bool CClaimTrie::updateTakeoverHeight(const std::string& name, int nTakeoverHeig
return true;
}
void CClaimTrie::BatchWriteClaimIndex(CDBBatch& batch) const
{
for(claimIndexType::const_iterator itClaim = claimIndex.begin();
itClaim != claimIndex.end(); ++itClaim)
{
batch.Write(std::make_pair(CLAIM_BY_ID, itClaim->second.claim.claimId), itClaim->second);
LogPrintf("%s: Writing %s to disk\n", __func__, itClaim->second.claim.claimId.GetHex());
}
}
void CClaimTrie::BatchWriteNode(CDBBatch& batch, const std::string& name, const CClaimTrieNode* pNode) const
{
uint32_t num_claims = 0;
@ -1116,8 +1089,6 @@ bool CClaimTrie::WriteToDisk()
dirtySupportQueueNameRows.clear();
BatchWriteSupportExpirationQueueRows(batch);
dirtySupportExpirationQueueRows.clear();
BatchWriteClaimIndex(batch);
claimIndex.clear();
batch.Write(HASH_BLOCK, hashBlock);
batch.Write(CURRENT_HEIGHT, nCurrentHeight);
return db.WriteBatch(batch);
@ -1139,11 +1110,6 @@ bool CClaimTrie::InsertFromDisk(const std::string& name, CClaimTrieNode* node)
current = itchild->second;
}
current->children[name[name.size()-1]] = node;
for(size_t i = 0; i < current->claims.size(); ++i)
{
addToClaimIndex(name.substr(0, name.size()-1), current->claims[i]);
}
return true;
}
@ -1156,32 +1122,6 @@ bool CClaimTrie::ReadFromDisk(bool check)
boost::scoped_ptr<CDBIterator> pcursor(const_cast<CDBWrapper*>(&db)->NewIterator());
pcursor->SeekToFirst();
while (pcursor->Valid())
{
std::pair<char, std::string> key;
if (pcursor->GetKey(key) && (key.first == CLAIM_BY_ID))
{
CClaimIndexElement element;
if (pcursor->GetValue(element))
{
claimIndex[element.claim.claimId] = element;
LogPrintf("%s: ClaimIndex Read %s from disk\n", __func__, element.claim.claimId.GetHex());
}
else
{
return error("%s(): error reading claim index entry from disk", __func__);
}
}
pcursor->Next();
}
if (claimIndex.empty())
LogPrintf("Building ClaimIndex from persistent ClaimTrie...\n");
else
LogPrintf("Restored ClaimIndex with %lu elements...\n", claimIndex.size());
pcursor->SeekToFirst();
while (pcursor->Valid())
{
std::pair<char, std::string> key;

View file

@ -282,8 +282,6 @@ typedef std::map<std::string, CClaimTrieNode*, nodenamecompare> nodeCacheType;
typedef std::map<std::string, uint256> hashMapType;
typedef std::map<uint160, CClaimIndexElement> claimIndexType;
struct claimsForNameType
{
std::vector<CClaimValue> claims;
@ -430,8 +428,6 @@ private:
nodeCacheType dirtyNodes;
supportMapType dirtySupportNodes;
mutable claimIndexType claimIndex;
};
class CClaimTrieProofNode

View file

@ -762,38 +762,38 @@ BOOST_AUTO_TEST_CASE(claimtriebranching_get_effective_amount_for_claim)
uint160 claimId = ClaimIdHash(claimtx.GetHash(), 0);
fixture.IncrementBlocks(1);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 2);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId) == 0); //not found returns 0
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 2);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId) == 0); //not found returns 0
// one claim, one support
fixture.MakeSupport(fixture.GetCoinbase(), claimtx, "test", 40);
fixture.IncrementBlocks(1);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
// Two claims, first one with supports
CMutableTransaction claimtx2 = fixture.MakeClaim(fixture.GetCoinbase(), "test", "two", 1);
uint160 claimId2 = ClaimIdHash(claimtx2.GetHash(), 0);
fixture.IncrementBlocks(10);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 1);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId) == 0);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId2) == 0);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 1);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId) == 0);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("inexistent", claimId2) == 0);
// Two claims, both with supports, second claim effective amount being less than first claim
fixture.MakeSupport(fixture.GetCoinbase(), claimtx2, "test", 6);
fixture.IncrementBlocks(13); //delay
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 7);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 7);
// Two claims, both with supports, second one taking over
fixture.MakeSupport(fixture.GetCoinbase(), claimtx2, "test", 1330);
fixture.IncrementBlocks(26); //delay
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
BOOST_TEST(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 1337);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId) == 42);
BOOST_CHECK(pclaimTrie->getEffectiveAmountForClaim("test", claimId2) == 1337);
}
BOOST_AUTO_TEST_SUITE_END()