renamed some of the cache fields
This commit is contained in:
parent
e5c8b6b8ff
commit
6259378466
5 changed files with 85 additions and 109 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -119,3 +119,5 @@ contrib/devtools/split-debug.sh
|
||||||
|
|
||||||
.idea
|
.idea
|
||||||
cmake-build-*/
|
cmake-build-*/
|
||||||
|
|
||||||
|
compile_commands\.json
|
||||||
|
|
|
@ -228,8 +228,8 @@ bool CClaimTrieCacheBase::haveSupport(const std::string& name, const COutPoint&
|
||||||
|
|
||||||
supportEntryType CClaimTrieCacheBase::getSupportsForName(const std::string& name) const
|
supportEntryType CClaimTrieCacheBase::getSupportsForName(const std::string& name) const
|
||||||
{
|
{
|
||||||
auto sit = cacheSupports.find(name);
|
auto sit = supportCache.find(name);
|
||||||
if (sit != cacheSupports.end())
|
if (sit != supportCache.end())
|
||||||
return sit->second;
|
return sit->second;
|
||||||
|
|
||||||
supportEntryType supports;
|
supportEntryType supports;
|
||||||
|
@ -381,7 +381,7 @@ uint256 recursiveMerkleHash(TIterator& it, const iCbType<TIterator>& process, co
|
||||||
|
|
||||||
bool recursiveCheckConsistency(CClaimTrie::const_iterator& it, std::string& failed)
|
bool recursiveCheckConsistency(CClaimTrie::const_iterator& it, std::string& failed)
|
||||||
{
|
{
|
||||||
struct CRecursiveBreak{};
|
struct CRecursiveBreak : public std::exception {};
|
||||||
|
|
||||||
using iterator = CClaimTrie::const_iterator;
|
using iterator = CClaimTrie::const_iterator;
|
||||||
iCbType<iterator> verify = [&failed](iterator& it) {
|
iCbType<iterator> verify = [&failed](iterator& it) {
|
||||||
|
@ -420,7 +420,7 @@ bool CClaimTrieCacheBase::checkConsistency() const
|
||||||
auto basePath = base->nodes(failed);
|
auto basePath = base->nodes(failed);
|
||||||
if (basePath.size() > 1) basePath.pop_back();
|
if (basePath.size() > 1) basePath.pop_back();
|
||||||
dumpToLog(basePath.back(), false);
|
dumpToLog(basePath.back(), false);
|
||||||
auto cachePath = cache.nodes(failed);
|
auto cachePath = nodesToAddOrUpdate.nodes(failed);
|
||||||
if (!cachePath.empty()) {
|
if (!cachePath.empty()) {
|
||||||
LogPrintf("\nPrinting %s's parent from cache:\n", failed);
|
LogPrintf("\nPrinting %s's parent from cache:\n", failed);
|
||||||
if (cachePath.size() > 1) cachePath.pop_back();
|
if (cachePath.size() > 1) cachePath.pop_back();
|
||||||
|
@ -470,23 +470,23 @@ bool CClaimTrieCacheBase::flush()
|
||||||
{
|
{
|
||||||
CDBBatch batch(*(base->db));
|
CDBBatch batch(*(base->db));
|
||||||
|
|
||||||
for (const auto& claim : claimsToDelete) {
|
for (const auto& claim : claimsToDeleteFromByIdIndex) {
|
||||||
auto it = std::find_if(claimsToAdd.begin(), claimsToAdd.end(),
|
auto it = std::find_if(claimsToAddToByIdIndex.begin(), claimsToAddToByIdIndex.end(),
|
||||||
[&claim](const CClaimIndexElement& e) {
|
[&claim](const CClaimIndexElement& e) {
|
||||||
return e.claim.claimId == claim.claimId;
|
return e.claim.claimId == claim.claimId;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (it == claimsToAdd.end())
|
if (it == claimsToAddToByIdIndex.end())
|
||||||
batch.Erase(std::make_pair(CLAIM_BY_ID, claim.claimId));
|
batch.Erase(std::make_pair(CLAIM_BY_ID, claim.claimId));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& e : claimsToAdd)
|
for (const auto& e : claimsToAddToByIdIndex)
|
||||||
batch.Write(std::make_pair(CLAIM_BY_ID, e.claim.claimId), e);
|
batch.Write(std::make_pair(CLAIM_BY_ID, e.claim.claimId), e);
|
||||||
|
|
||||||
getMerkleHash();
|
getMerkleHash();
|
||||||
|
|
||||||
for (const auto& nodeName : nodesToDelete) {
|
for (const auto& nodeName : nodesToDelete) {
|
||||||
if (cache.contains(nodeName))
|
if (nodesToAddOrUpdate.contains(nodeName))
|
||||||
continue;
|
continue;
|
||||||
auto nodes = base->nodes(nodeName);
|
auto nodes = base->nodes(nodeName);
|
||||||
base->erase(nodeName);
|
base->erase(nodeName);
|
||||||
|
@ -495,7 +495,7 @@ bool CClaimTrieCacheBase::flush()
|
||||||
batch.Erase(std::make_pair(TRIE_NODE, node.key()));
|
batch.Erase(std::make_pair(TRIE_NODE, node.key()));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = cache.begin(); it != cache.end(); ++it) {
|
for (auto it = nodesToAddOrUpdate.begin(); it != nodesToAddOrUpdate.end(); ++it) {
|
||||||
auto old = base->find(it.key());
|
auto old = base->find(it.key());
|
||||||
if (!old || old.data() != it.data()) {
|
if (!old || old.data() != it.data()) {
|
||||||
base->copy(it);
|
base->copy(it);
|
||||||
|
@ -503,7 +503,7 @@ bool CClaimTrieCacheBase::flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BatchWriteQueue(batch, SUPPORT, cacheSupports);
|
BatchWriteQueue(batch, SUPPORT, supportCache);
|
||||||
BatchWriteQueue(batch, CLAIM_QUEUE_ROW, claimQueueCache);
|
BatchWriteQueue(batch, CLAIM_QUEUE_ROW, claimQueueCache);
|
||||||
BatchWriteQueue(batch, CLAIM_QUEUE_NAME_ROW, claimQueueNameCache);
|
BatchWriteQueue(batch, CLAIM_QUEUE_NAME_ROW, claimQueueNameCache);
|
||||||
BatchWriteQueue(batch, EXP_QUEUE_ROW, expirationQueueCache);
|
BatchWriteQueue(batch, EXP_QUEUE_ROW, expirationQueueCache);
|
||||||
|
@ -512,8 +512,8 @@ bool CClaimTrieCacheBase::flush()
|
||||||
BatchWriteQueue(batch, SUPPORT_EXP_QUEUE_ROW, supportExpirationQueueCache);
|
BatchWriteQueue(batch, SUPPORT_EXP_QUEUE_ROW, supportExpirationQueueCache);
|
||||||
|
|
||||||
base->nNextHeight = nNextHeight;
|
base->nNextHeight = nNextHeight;
|
||||||
if (!cache.empty())
|
if (!nodesToAddOrUpdate.empty())
|
||||||
LogPrintf("Cache size: %zu from base size: %zu on block %d\n", cache.height(), base->height(), nNextHeight);
|
LogPrintf("Cache size: %zu from base size: %zu on block %d\n", nodesToAddOrUpdate.height(), base->height(), nNextHeight);
|
||||||
auto ret = base->db->WriteBatch(batch);
|
auto ret = base->db->WriteBatch(batch);
|
||||||
clear();
|
clear();
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -602,32 +602,32 @@ uint256 CClaimTrieCacheBase::recursiveComputeMerkleHash(CClaimTrie::iterator& it
|
||||||
|
|
||||||
uint256 CClaimTrieCacheBase::getMerkleHash()
|
uint256 CClaimTrieCacheBase::getMerkleHash()
|
||||||
{
|
{
|
||||||
auto it = cache.begin();
|
auto it = nodesToAddOrUpdate.begin();
|
||||||
if (cache.empty() && nodesToDelete.empty())
|
if (nodesToAddOrUpdate.empty() && nodesToDelete.empty())
|
||||||
it = base->begin();
|
it = base->begin();
|
||||||
return !it ? one : recursiveComputeMerkleHash(it);
|
return !it ? one : recursiveComputeMerkleHash(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
CClaimTrie::const_iterator CClaimTrieCacheBase::begin() const
|
CClaimTrie::const_iterator CClaimTrieCacheBase::begin() const
|
||||||
{
|
{
|
||||||
return cache.empty() && nodesToDelete.empty() ? base->cbegin() : cache.begin();
|
return nodesToAddOrUpdate.empty() && nodesToDelete.empty() ? base->cbegin() : nodesToAddOrUpdate.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
CClaimTrie::const_iterator CClaimTrieCacheBase::end() const
|
CClaimTrie::const_iterator CClaimTrieCacheBase::end() const
|
||||||
{
|
{
|
||||||
return cache.empty() && nodesToDelete.empty() ? base->cend() : cache.end();
|
return nodesToAddOrUpdate.empty() && nodesToDelete.empty() ? base->cend() : nodesToAddOrUpdate.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
CClaimTrie::const_iterator CClaimTrieCacheBase::find(const std::string& name) const
|
CClaimTrie::const_iterator CClaimTrieCacheBase::find(const std::string& name) const
|
||||||
{
|
{
|
||||||
if (auto it = cache.find(name))
|
if (auto it = nodesToAddOrUpdate.find(name))
|
||||||
return it;
|
return it;
|
||||||
return base->find(name);
|
return base->find(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::empty() const
|
bool CClaimTrieCacheBase::empty() const
|
||||||
{
|
{
|
||||||
return base->empty() && cache.empty();
|
return base->empty() && nodesToAddOrUpdate.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
CClaimTrie::iterator CClaimTrieCacheBase::cacheData(const std::string& name, bool create)
|
CClaimTrie::iterator CClaimTrieCacheBase::cacheData(const std::string& name, bool create)
|
||||||
|
@ -636,10 +636,10 @@ CClaimTrie::iterator CClaimTrieCacheBase::cacheData(const std::string& name, boo
|
||||||
const auto insert = [this](CClaimTrie::iterator& it) {
|
const auto insert = [this](CClaimTrie::iterator& it) {
|
||||||
auto& key = it.key();
|
auto& key = it.key();
|
||||||
// we only ever cache nodes once per cache instance
|
// we only ever cache nodes once per cache instance
|
||||||
if (!alreadyCachedNodes.count(key)) {
|
if (!nodesAlreadyCached.count(key)) {
|
||||||
// do not insert nodes that are already present
|
// do not insert nodes that are already present
|
||||||
alreadyCachedNodes.insert(key);
|
nodesAlreadyCached.insert(key);
|
||||||
cache.insert(key, it.data());
|
nodesToAddOrUpdate.insert(key, it.data());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -648,14 +648,14 @@ CClaimTrie::iterator CClaimTrieCacheBase::cacheData(const std::string& name, boo
|
||||||
auto nodes = base->nodes(name);
|
auto nodes = base->nodes(name);
|
||||||
for (auto& node: nodes) {
|
for (auto& node: nodes) {
|
||||||
for (auto& child : node.children())
|
for (auto& child : node.children())
|
||||||
if (!alreadyCachedNodes.count(child.key()))
|
if (!nodesAlreadyCached.count(child.key()))
|
||||||
cache.copy(child);
|
nodesToAddOrUpdate.copy(child);
|
||||||
insert(node);
|
insert(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto it = cache.find(name);
|
auto it = nodesToAddOrUpdate.find(name);
|
||||||
if (!it && create) {
|
if (!it && create) {
|
||||||
it = cache.insert(name, CClaimTrieData{});
|
it = nodesToAddOrUpdate.insert(name, CClaimTrieData{});
|
||||||
confirmTakeoverWorkaroundNeeded(name);
|
confirmTakeoverWorkaroundNeeded(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -689,7 +689,7 @@ bool CClaimTrieCacheBase::getLastTakeoverForName(const std::string& name, uint16
|
||||||
|
|
||||||
void CClaimTrieCacheBase::markAsDirty(const std::string& name, bool fCheckTakeover)
|
void CClaimTrieCacheBase::markAsDirty(const std::string& name, bool fCheckTakeover)
|
||||||
{
|
{
|
||||||
for (auto& node : cache.nodes(name))
|
for (auto& node : nodesToAddOrUpdate.nodes(name))
|
||||||
node->hash.SetNull();
|
node->hash.SetNull();
|
||||||
|
|
||||||
if (fCheckTakeover)
|
if (fCheckTakeover)
|
||||||
|
@ -726,7 +726,7 @@ bool CClaimTrieCacheBase::removeClaimFromTrie(const std::string& name, const COu
|
||||||
cacheData(child.key(), false);
|
cacheData(child.key(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.erase(name);
|
nodesToAddOrUpdate.erase(name);
|
||||||
nodesToDelete.insert(name);
|
nodesToDelete.insert(name);
|
||||||
|
|
||||||
// NOTE: old code had a bug in it where nodes with no claims but with children would get left in the cache.
|
// NOTE: old code had a bug in it where nodes with no claims but with children would get left in the cache.
|
||||||
|
@ -754,7 +754,7 @@ T CClaimTrieCacheBase::add(const std::string& name, const COutPoint& outPoint, c
|
||||||
bool CClaimTrieCacheBase::addClaim(const std::string& name, const COutPoint& outPoint, const uint160& claimId, CAmount nAmount, int nHeight)
|
bool CClaimTrieCacheBase::addClaim(const std::string& name, const COutPoint& outPoint, const uint160& claimId, CAmount nAmount, int nHeight)
|
||||||
{
|
{
|
||||||
auto claim = add<CClaimValue>(name, outPoint, claimId, nAmount, nHeight);
|
auto claim = add<CClaimValue>(name, outPoint, claimId, nAmount, nHeight);
|
||||||
claimsToAdd.emplace_back(name, claim);
|
claimsToAddToByIdIndex.emplace_back(name, claim);
|
||||||
LogPrintf("%s: name: %s, txhash: %s, nOut: %d, claimId: %s, nAmount: %d, nHeight: %d, nValidHeight: %d\n", __func__, name, outPoint.hash.GetHex(), outPoint.n, claimId.GetHex(), nAmount, nHeight, claim.nValidAtHeight);
|
LogPrintf("%s: name: %s, txhash: %s, nOut: %d, claimId: %s, nAmount: %d, nHeight: %d, nValidHeight: %d\n", __func__, name, outPoint.hash.GetHex(), outPoint.n, claimId.GetHex(), nAmount, nHeight, claim.nValidAtHeight);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -815,7 +815,7 @@ bool CClaimTrieCacheBase::undoSpendClaim(const std::string& name, const COutPoin
|
||||||
{
|
{
|
||||||
LogPrintf("%s: name: %s, txhash: %s, nOut: %d, claimId: %s, nAmount: %d, nHeight: %d, nValidAtHeight: %d, nNextHeight: %d\n", __func__, name, outPoint.hash.GetHex(), outPoint.n, claimId.GetHex(), nAmount, nHeight, nValidAtHeight, nNextHeight);
|
LogPrintf("%s: name: %s, txhash: %s, nOut: %d, claimId: %s, nAmount: %d, nHeight: %d, nValidAtHeight: %d, nNextHeight: %d\n", __func__, name, outPoint.hash.GetHex(), outPoint.n, claimId.GetHex(), nAmount, nHeight, nValidAtHeight, nNextHeight);
|
||||||
CClaimValue claim(outPoint, claimId, nAmount, nHeight, nValidAtHeight);
|
CClaimValue claim(outPoint, claimId, nAmount, nHeight, nValidAtHeight);
|
||||||
claimsToAdd.emplace_back(name, claim);
|
claimsToAddToByIdIndex.emplace_back(name, claim);
|
||||||
return undoSpend(name, claim, nValidAtHeight);
|
return undoSpend(name, claim, nValidAtHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -848,16 +848,6 @@ bool CClaimTrieCacheBase::removeFromQueue(const std::string& name, const COutPoi
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::removeClaimFromQueue(const std::string& name, const COutPoint& outPoint, CClaimValue& claim)
|
|
||||||
{
|
|
||||||
return removeFromQueue(name, outPoint, claim);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::removeSupportFromQueue(const std::string& name, const COutPoint& outPoint, CSupportValue& support)
|
|
||||||
{
|
|
||||||
return removeFromQueue(name, outPoint, support);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::undoAddClaim(const std::string& name, const COutPoint& outPoint, int nHeight)
|
bool CClaimTrieCacheBase::undoAddClaim(const std::string& name, const COutPoint& outPoint, int nHeight)
|
||||||
{
|
{
|
||||||
int throwaway;
|
int throwaway;
|
||||||
|
@ -924,7 +914,7 @@ bool CClaimTrieCacheBase::removeClaim(const std::string& name, const COutPoint&
|
||||||
|
|
||||||
CClaimValue claim;
|
CClaimValue claim;
|
||||||
if (remove(claim, name, outPoint, nHeight, nValidAtHeight, fCheckTakeover)) {
|
if (remove(claim, name, outPoint, nHeight, nValidAtHeight, fCheckTakeover)) {
|
||||||
claimsToDelete.insert(claim);
|
claimsToDeleteFromByIdIndex.insert(claim);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -938,9 +928,9 @@ bool CClaimTrieCacheBase::removeSupport(const std::string& name, const COutPoint
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::insertSupportIntoMap(const std::string& name, const CSupportValue& support, bool fCheckTakeover)
|
bool CClaimTrieCacheBase::insertSupportIntoMap(const std::string& name, const CSupportValue& support, bool fCheckTakeover)
|
||||||
{
|
{
|
||||||
auto sit = cacheSupports.find(name);
|
auto sit = supportCache.find(name);
|
||||||
if (sit == cacheSupports.end())
|
if (sit == supportCache.end())
|
||||||
sit = cacheSupports.emplace(name, getSupportsForName(name)).first;
|
sit = supportCache.emplace(name, getSupportsForName(name)).first;
|
||||||
|
|
||||||
sit->second.push_back(support);
|
sit->second.push_back(support);
|
||||||
addTakeoverWorkaroundPotential(name);
|
addTakeoverWorkaroundPotential(name);
|
||||||
|
@ -955,9 +945,9 @@ bool CClaimTrieCacheBase::insertSupportIntoMap(const std::string& name, const CS
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::removeSupportFromMap(const std::string& name, const COutPoint& outPoint, CSupportValue& support, bool fCheckTakeover)
|
bool CClaimTrieCacheBase::removeSupportFromMap(const std::string& name, const COutPoint& outPoint, CSupportValue& support, bool fCheckTakeover)
|
||||||
{
|
{
|
||||||
auto sit = cacheSupports.find(name);
|
auto sit = supportCache.find(name);
|
||||||
if (sit == cacheSupports.end())
|
if (sit == supportCache.end())
|
||||||
sit = cacheSupports.emplace(name, getSupportsForName(name)).first;
|
sit = supportCache.emplace(name, getSupportsForName(name)).first;
|
||||||
|
|
||||||
if (eraseOutPoint(sit->second, outPoint, &support)) {
|
if (eraseOutPoint(sit->second, outPoint, &support)) {
|
||||||
addTakeoverWorkaroundPotential(name);
|
addTakeoverWorkaroundPotential(name);
|
||||||
|
@ -982,7 +972,7 @@ void CClaimTrieCacheBase::dumpToLog(CClaimTrie::const_iterator it, bool diffFrom
|
||||||
|
|
||||||
std::string indent(it.depth(), ' ');
|
std::string indent(it.depth(), ' ');
|
||||||
auto children = it.children();
|
auto children = it.children();
|
||||||
auto empty = children.size() == 0 && it->claims.size() == 0;
|
auto empty = children.empty() && it->claims.empty();
|
||||||
LogPrintf("%s%s, %s, %zu = %s,%s take: %d, kids: %zu\n", indent, it.key(), HexStr(it.key().begin(), it.key().end()),
|
LogPrintf("%s%s, %s, %zu = %s,%s take: %d, kids: %zu\n", indent, it.key(), HexStr(it.key().begin(), it.key().end()),
|
||||||
empty ? " empty," : "", it.depth(), it->hash.ToString(), it->nHeightOfLastTakeover, children.size());
|
empty ? " empty," : "", it.depth(), it->hash.ToString(), it->nHeightOfLastTakeover, children.size());
|
||||||
for (auto& claim: it->claims)
|
for (auto& claim: it->claims)
|
||||||
|
@ -1008,7 +998,7 @@ void CClaimTrieCacheBase::addTakeoverWorkaroundPotential(const std::string& key)
|
||||||
// (and the shortcut would later lead to a miscalculation of the takeover height)
|
// (and the shortcut would later lead to a miscalculation of the takeover height)
|
||||||
if (nNextHeight > Params().GetConsensus().nMinTakeoverWorkaroundHeight
|
if (nNextHeight > Params().GetConsensus().nMinTakeoverWorkaroundHeight
|
||||||
&& nNextHeight < Params().GetConsensus().nMaxTakeoverWorkaroundHeight
|
&& nNextHeight < Params().GetConsensus().nMaxTakeoverWorkaroundHeight
|
||||||
&& !cache.contains(key) && base->contains(key))
|
&& !nodesToAddOrUpdate.contains(key) && base->contains(key))
|
||||||
takeoverWorkaround.emplace(key, false);
|
takeoverWorkaround.emplace(key, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1053,8 +1043,8 @@ void CClaimTrieCacheBase::undoIncrement(insertUndoType& insertUndo, std::vector<
|
||||||
} else {
|
} else {
|
||||||
LogPrintf("%s: An inconsistency was found in the queue. Please report this to the developers:\nFound in height queue but not in named queue: name: %s, txid: %s, nOut: %d, nValidAtHeight: %d, current height: %d\n", __func__, itEntry.first, itEntry.second.outPoint.hash.GetHex(), itEntry.second.outPoint.n, itEntry.second.nValidAtHeight, nNextHeight);
|
LogPrintf("%s: An inconsistency was found in the queue. Please report this to the developers:\nFound in height queue but not in named queue: name: %s, txid: %s, nOut: %d, nValidAtHeight: %d, current height: %d\n", __func__, itEntry.first, itEntry.second.outPoint.hash.GetHex(), itEntry.second.outPoint.n, itEntry.second.nValidAtHeight, nNextHeight);
|
||||||
LogPrintf("Elements found for that name:\n");
|
LogPrintf("Elements found for that name:\n");
|
||||||
for (const auto& itQueueName : itQueueNameRow->second)
|
for (const auto& itQueueNameInner : itQueueNameRow->second)
|
||||||
LogPrintf("\ttxid: %s, nOut: %d, nValidAtHeight: %d\n", itQueueName.outPoint.hash.GetHex(), itQueueName.outPoint.n, itQueueName.nHeight);
|
LogPrintf("\ttxid: %s, nOut: %d, nValidAtHeight: %d\n", itQueueNameInner.outPoint.hash.GetHex(), itQueueNameInner.outPoint.n, itQueueNameInner.nHeight);
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -1113,7 +1103,7 @@ void CClaimTrieCacheBase::undoIncrement(const std::string& name, insertUndoType&
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueRowType& expireUndo, insertUndoType& insertSupportUndo, supportQueueRowType& expireSupportUndo, std::vector<std::pair<std::string, int>>& takeoverHeightUndo)
|
bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueRowType& expireUndo, insertUndoType& insertSupportUndo, supportQueueRowType& expireSupportUndo, std::vector<std::pair<std::string, int>>& takeoverHeightUndo)
|
||||||
{
|
{
|
||||||
undoIncrement(insertUndo, expireUndo, &claimsToDelete);
|
undoIncrement(insertUndo, expireUndo, &claimsToDeleteFromByIdIndex);
|
||||||
undoIncrement(insertSupportUndo, expireSupportUndo);
|
undoIncrement(insertSupportUndo, expireSupportUndo);
|
||||||
|
|
||||||
// check each potentially taken over name to see if a takeover occurred.
|
// check each potentially taken over name to see if a takeover occurred.
|
||||||
|
@ -1125,7 +1115,7 @@ bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueR
|
||||||
// Run through all names that have been taken over
|
// Run through all names that have been taken over
|
||||||
for (const auto& itNamesToCheck : namesToCheckForTakeover) {
|
for (const auto& itNamesToCheck : namesToCheckForTakeover) {
|
||||||
// Check if a takeover has occurred (only going to hit each name once)
|
// Check if a takeover has occurred (only going to hit each name once)
|
||||||
auto itCachedNode = cache.find(itNamesToCheck);
|
auto itCachedNode = nodesToAddOrUpdate.find(itNamesToCheck);
|
||||||
// many possibilities
|
// many possibilities
|
||||||
// if this node is new, don't put it into the undo -- there will be nothing to restore, after all
|
// if this node is new, don't put it into the undo -- there will be nothing to restore, after all
|
||||||
// if all of this node's claims were deleted, it should be put into the undo -- there could be
|
// if all of this node's claims were deleted, it should be put into the undo -- there could be
|
||||||
|
@ -1170,7 +1160,7 @@ bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueR
|
||||||
// if there was no takeover, we set it to old height if we have one
|
// if there was no takeover, we set it to old height if we have one
|
||||||
// else set it to new height
|
// else set it to new height
|
||||||
|
|
||||||
if ((itCachedNode = cache.find(itNamesToCheck))) {
|
if ((itCachedNode = nodesToAddOrUpdate.find(itNamesToCheck))) {
|
||||||
if (takeoverHappened) {
|
if (takeoverHappened) {
|
||||||
itCachedNode->nHeightOfLastTakeover = nNextHeight;
|
itCachedNode->nHeightOfLastTakeover = nNextHeight;
|
||||||
CClaimValue winner;
|
CClaimValue winner;
|
||||||
|
@ -1234,7 +1224,7 @@ bool CClaimTrieCacheBase::decrementBlock(insertUndoType& insertUndo, claimQueueR
|
||||||
nNextHeight--;
|
nNextHeight--;
|
||||||
|
|
||||||
undoDecrement(insertSupportUndo, expireSupportUndo);
|
undoDecrement(insertSupportUndo, expireSupportUndo);
|
||||||
undoDecrement(insertUndo, expireUndo, &claimsToAdd, &claimsToDelete);
|
undoDecrement(insertUndo, expireUndo, &claimsToAddToByIdIndex, &claimsToDeleteFromByIdIndex);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1260,15 +1250,15 @@ template <typename T>
|
||||||
void CClaimTrieCacheBase::reactivate(const expirationQueueRowType& row, int height, bool increment)
|
void CClaimTrieCacheBase::reactivate(const expirationQueueRowType& row, int height, bool increment)
|
||||||
{
|
{
|
||||||
supportedType<T>();
|
supportedType<T>();
|
||||||
for (auto e = row.begin(); e != row.end(); ++e) {
|
for (auto& e: row) {
|
||||||
// remove and insert with new expiration time
|
// remove and insert with new expiration time
|
||||||
if (auto itQueueRow = getExpirationQueueCacheRow<T>(height))
|
if (auto itQueueRow = getExpirationQueueCacheRow<T>(height))
|
||||||
eraseOutPoint(itQueueRow->second, CNameOutPointType{e->name, e->outPoint});
|
eraseOutPoint(itQueueRow->second, CNameOutPointType{e.name, e.outPoint});
|
||||||
|
|
||||||
int extend_expiration = Params().GetConsensus().nExtendedClaimExpirationTime - Params().GetConsensus().nOriginalClaimExpirationTime;
|
int extend_expiration = Params().GetConsensus().nExtendedClaimExpirationTime - Params().GetConsensus().nOriginalClaimExpirationTime;
|
||||||
int new_expiration_height = increment ? height + extend_expiration : height - extend_expiration;
|
int new_expiration_height = increment ? height + extend_expiration : height - extend_expiration;
|
||||||
auto itQueueExpiration = getExpirationQueueCacheRow<T>(new_expiration_height, true);
|
auto itQueueExpiration = getExpirationQueueCacheRow<T>(new_expiration_height, true);
|
||||||
itQueueExpiration->second.emplace_back(e->name, e->outPoint);
|
itQueueExpiration->second.emplace_back(e.name, e.outPoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1320,15 +1310,15 @@ std::string CClaimTrieCacheBase::adjustNameForValidHeight(const std::string& nam
|
||||||
|
|
||||||
bool CClaimTrieCacheBase::clear()
|
bool CClaimTrieCacheBase::clear()
|
||||||
{
|
{
|
||||||
cache.clear();
|
nodesToAddOrUpdate.clear();
|
||||||
claimsToAdd.clear();
|
claimsToAddToByIdIndex.clear();
|
||||||
cacheSupports.clear();
|
supportCache.clear();
|
||||||
nodesToDelete.clear();
|
nodesToDelete.clear();
|
||||||
claimsToDelete.clear();
|
claimsToDeleteFromByIdIndex.clear();
|
||||||
takeoverCache.clear();
|
takeoverCache.clear();
|
||||||
claimQueueCache.clear();
|
claimQueueCache.clear();
|
||||||
supportQueueCache.clear();
|
supportQueueCache.clear();
|
||||||
alreadyCachedNodes.clear();
|
nodesAlreadyCached.clear();
|
||||||
takeoverWorkaround.clear();
|
takeoverWorkaround.clear();
|
||||||
removalWorkaround.clear();
|
removalWorkaround.clear();
|
||||||
claimQueueNameCache.clear();
|
claimQueueNameCache.clear();
|
||||||
|
@ -1348,7 +1338,7 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, CClaimTriePro
|
||||||
bool fNameHasValue = false;
|
bool fNameHasValue = false;
|
||||||
int nHeightOfLastTakeover = 0;
|
int nHeightOfLastTakeover = 0;
|
||||||
std::vector<CClaimTrieProofNode> nodes;
|
std::vector<CClaimTrieProofNode> nodes;
|
||||||
for (const auto& it : cache.nodes(name)) {
|
for (const auto& it : nodesToAddOrUpdate.nodes(name)) {
|
||||||
CClaimValue claim;
|
CClaimValue claim;
|
||||||
const auto& key = it.key();
|
const auto& key = it.key();
|
||||||
bool fNodeHasValue = it->getBestClaim(claim);
|
bool fNodeHasValue = it->getBestClaim(claim);
|
||||||
|
@ -1364,6 +1354,7 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, CClaimTriePro
|
||||||
for (auto i = pos; i + 1 < childKey.size(); ++i) {
|
for (auto i = pos; i + 1 < childKey.size(); ++i) {
|
||||||
children.emplace_back(childKey[i], uint256{});
|
children.emplace_back(childKey[i], uint256{});
|
||||||
nodes.emplace_back(std::move(children), fNodeHasValue, valueHash);
|
nodes.emplace_back(std::move(children), fNodeHasValue, valueHash);
|
||||||
|
children.clear(); // move promises to leave it in a valid state only
|
||||||
valueHash.SetNull();
|
valueHash.SetNull();
|
||||||
fNodeHasValue = false;
|
fNodeHasValue = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,7 @@ struct CClaimValue
|
||||||
int nHeight;
|
int nHeight;
|
||||||
int nValidAtHeight;
|
int nValidAtHeight;
|
||||||
|
|
||||||
CClaimValue()
|
CClaimValue() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CClaimValue(const COutPoint& outPoint, const uint160& claimId, CAmount nAmount, int nHeight, int nValidAtHeight)
|
CClaimValue(const COutPoint& outPoint, const uint160& claimId, CAmount nAmount, int nHeight, int nValidAtHeight)
|
||||||
: outPoint(outPoint), claimId(claimId), nAmount(nAmount), nEffectiveAmount(nAmount), nHeight(nHeight), nValidAtHeight(nValidAtHeight)
|
: outPoint(outPoint), claimId(claimId), nAmount(nAmount), nEffectiveAmount(nAmount), nHeight(nHeight), nValidAtHeight(nValidAtHeight)
|
||||||
|
@ -97,9 +95,7 @@ struct CSupportValue
|
||||||
int nHeight;
|
int nHeight;
|
||||||
int nValidAtHeight;
|
int nValidAtHeight;
|
||||||
|
|
||||||
CSupportValue()
|
CSupportValue() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CSupportValue(const COutPoint& outPoint, const uint160& supportedClaimId, CAmount nAmount, int nHeight, int nValidAtHeight)
|
CSupportValue(const COutPoint& outPoint, const uint160& supportedClaimId, CAmount nAmount, int nHeight, int nValidAtHeight)
|
||||||
: outPoint(outPoint), supportedClaimId(supportedClaimId), nAmount(nAmount), nHeight(nHeight), nValidAtHeight(nValidAtHeight)
|
: outPoint(outPoint), supportedClaimId(supportedClaimId), nAmount(nAmount), nHeight(nHeight), nValidAtHeight(nValidAtHeight)
|
||||||
|
@ -197,9 +193,7 @@ struct COutPointHeightType
|
||||||
COutPoint outPoint;
|
COutPoint outPoint;
|
||||||
int nHeight;
|
int nHeight;
|
||||||
|
|
||||||
COutPointHeightType()
|
COutPointHeightType() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
COutPointHeightType(const COutPoint& outPoint, int nHeight)
|
COutPointHeightType(const COutPoint& outPoint, int nHeight)
|
||||||
: outPoint(outPoint), nHeight(nHeight)
|
: outPoint(outPoint), nHeight(nHeight)
|
||||||
|
@ -222,9 +216,7 @@ struct CNameOutPointHeightType
|
||||||
COutPoint outPoint;
|
COutPoint outPoint;
|
||||||
int nHeight;
|
int nHeight;
|
||||||
|
|
||||||
CNameOutPointHeightType()
|
CNameOutPointHeightType() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CNameOutPointHeightType(std::string name, const COutPoint& outPoint, int nHeight)
|
CNameOutPointHeightType(std::string name, const COutPoint& outPoint, int nHeight)
|
||||||
: name(std::move(name)), outPoint(outPoint), nHeight(nHeight)
|
: name(std::move(name)), outPoint(outPoint), nHeight(nHeight)
|
||||||
|
@ -247,9 +239,7 @@ struct CNameOutPointType
|
||||||
std::string name;
|
std::string name;
|
||||||
COutPoint outPoint;
|
COutPoint outPoint;
|
||||||
|
|
||||||
CNameOutPointType()
|
CNameOutPointType() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CNameOutPointType(std::string name, const COutPoint& outPoint)
|
CNameOutPointType(std::string name, const COutPoint& outPoint)
|
||||||
: name(std::move(name)), outPoint(outPoint)
|
: name(std::move(name)), outPoint(outPoint)
|
||||||
|
@ -273,12 +263,10 @@ struct CNameOutPointType
|
||||||
|
|
||||||
struct CClaimIndexElement
|
struct CClaimIndexElement
|
||||||
{
|
{
|
||||||
CClaimIndexElement()
|
CClaimIndexElement() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CClaimIndexElement(const std::string& name, const CClaimValue& claim)
|
CClaimIndexElement(std::string name, CClaimValue claim)
|
||||||
: name(name), claim(claim)
|
: name(std::move(name)), claim(std::move(claim))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,8 +290,8 @@ struct CClaimsForNameType
|
||||||
int nLastTakeoverHeight;
|
int nLastTakeoverHeight;
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
||||||
CClaimsForNameType(claimEntryType claims, supportEntryType supports, int nLastTakeoverHeight, const std::string& name)
|
CClaimsForNameType(claimEntryType claims, supportEntryType supports, int nLastTakeoverHeight, std::string name)
|
||||||
: claims(std::move(claims)), supports(std::move(supports)), nLastTakeoverHeight(nLastTakeoverHeight), name(name)
|
: claims(std::move(claims)), supports(std::move(supports)), nLastTakeoverHeight(nLastTakeoverHeight), name(std::move(name))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,9 +343,7 @@ struct CClaimTrieProofNode
|
||||||
|
|
||||||
struct CClaimTrieProof
|
struct CClaimTrieProof
|
||||||
{
|
{
|
||||||
CClaimTrieProof()
|
CClaimTrieProof() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CClaimTrieProof(std::vector<CClaimTrieProofNode> nodes, bool hasValue, const COutPoint& outPoint, int nHeightOfLastTakeover)
|
CClaimTrieProof(std::vector<CClaimTrieProofNode> nodes, bool hasValue, const COutPoint& outPoint, int nHeightOfLastTakeover)
|
||||||
: nodes(std::move(nodes)), hasValue(hasValue), outPoint(outPoint), nHeightOfLastTakeover(nHeightOfLastTakeover)
|
: nodes(std::move(nodes)), hasValue(hasValue), outPoint(outPoint), nHeightOfLastTakeover(nHeightOfLastTakeover)
|
||||||
|
@ -463,8 +449,8 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
CClaimTrie* base;
|
CClaimTrie* base;
|
||||||
CClaimTrie cache;
|
CClaimTrie nodesToAddOrUpdate; // nodes pulled in from base (and possibly modified thereafter), written to base on flush
|
||||||
std::unordered_set<std::string> namesToCheckForTakeover;
|
std::unordered_set<std::string> namesToCheckForTakeover; // takeover numbers are updated on increment
|
||||||
|
|
||||||
uint256 recursiveComputeMerkleHash(CClaimTrie::iterator& it);
|
uint256 recursiveComputeMerkleHash(CClaimTrie::iterator& it);
|
||||||
|
|
||||||
|
@ -474,8 +460,6 @@ protected:
|
||||||
virtual bool insertSupportIntoMap(const std::string& name, const CSupportValue& support, bool fCheckTakeover);
|
virtual bool insertSupportIntoMap(const std::string& name, const CSupportValue& support, bool fCheckTakeover);
|
||||||
virtual bool removeSupportFromMap(const std::string& name, const COutPoint& outPoint, CSupportValue& support, bool fCheckTakeover);
|
virtual bool removeSupportFromMap(const std::string& name, const COutPoint& outPoint, CSupportValue& support, bool fCheckTakeover);
|
||||||
|
|
||||||
virtual bool removeSupportFromQueue(const std::string& name, const COutPoint& outPoint, CSupportValue& support);
|
|
||||||
|
|
||||||
virtual std::string adjustNameForValidHeight(const std::string& name, int validHeight) const;
|
virtual std::string adjustNameForValidHeight(const std::string& name, int validHeight) const;
|
||||||
|
|
||||||
supportEntryType getSupportsForName(const std::string& name) const;
|
supportEntryType getSupportsForName(const std::string& name) const;
|
||||||
|
@ -505,16 +489,16 @@ private:
|
||||||
|
|
||||||
bool fRequireTakeoverHeights;
|
bool fRequireTakeoverHeights;
|
||||||
|
|
||||||
claimQueueType claimQueueCache;
|
claimQueueType claimQueueCache; // claims not active yet: to be written to disk on flush
|
||||||
queueNameType claimQueueNameCache;
|
queueNameType claimQueueNameCache;
|
||||||
supportQueueType supportQueueCache;
|
supportQueueType supportQueueCache; // supports not active yet: to be written to disk on flush
|
||||||
queueNameType supportQueueNameCache;
|
queueNameType supportQueueNameCache;
|
||||||
claimIndexElementListType claimsToAdd;
|
claimIndexElementListType claimsToAddToByIdIndex; // written to index on flush
|
||||||
claimIndexClaimListType claimsToDelete;
|
claimIndexClaimListType claimsToDeleteFromByIdIndex;
|
||||||
|
|
||||||
std::unordered_map<std::string, supportEntryType> cacheSupports;
|
std::unordered_map<std::string, supportEntryType> supportCache; // to be added/updated to base (and disk) on flush
|
||||||
std::unordered_set<std::string> nodesToDelete;
|
std::unordered_set<std::string> nodesToDelete; // to be removed from base (and disk) on flush
|
||||||
std::unordered_set<std::string> alreadyCachedNodes;
|
std::unordered_set<std::string> nodesAlreadyCached; // set of nodes already pulled into cache from base
|
||||||
std::unordered_map<std::string, bool> takeoverWorkaround;
|
std::unordered_map<std::string, bool> takeoverWorkaround;
|
||||||
std::unordered_set<std::string> removalWorkaround;
|
std::unordered_set<std::string> removalWorkaround;
|
||||||
|
|
||||||
|
@ -527,7 +511,6 @@ private:
|
||||||
void markAsDirty(const std::string& name, bool fCheckTakeover);
|
void markAsDirty(const std::string& name, bool fCheckTakeover);
|
||||||
bool removeSupport(const std::string& name, const COutPoint& outPoint, int nHeight, int& nValidAtHeight, bool fCheckTakeover);
|
bool removeSupport(const std::string& name, const COutPoint& outPoint, int nHeight, int& nValidAtHeight, bool fCheckTakeover);
|
||||||
bool removeClaim(const std::string& name, const COutPoint& outPoint, int nHeight, int& nValidAtHeight, bool fCheckTakeover);
|
bool removeClaim(const std::string& name, const COutPoint& outPoint, int nHeight, int& nValidAtHeight, bool fCheckTakeover);
|
||||||
bool removeClaimFromQueue(const std::string& name, const COutPoint& outPoint, CClaimValue& claim);
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::pair<const int, std::vector<queueEntryType<T>>>* getQueueCacheRow(int nHeight, bool createIfNotExists = false);
|
std::pair<const int, std::vector<queueEntryType<T>>>* getQueueCacheRow(int nHeight, bool createIfNotExists = false);
|
||||||
|
@ -635,8 +618,8 @@ public:
|
||||||
CClaimsForNameType getClaimsForName(const std::string& name) const override;
|
CClaimsForNameType getClaimsForName(const std::string& name) const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool insertClaimIntoTrie(const std::string& name, const CClaimValue& claim, bool fCheckTakeover = false) override;
|
bool insertClaimIntoTrie(const std::string& name, const CClaimValue& claim, bool fCheckTakeover) override;
|
||||||
bool removeClaimFromTrie(const std::string& name, const COutPoint& outPoint, CClaimValue& claim, bool fCheckTakeover = false) override;
|
bool removeClaimFromTrie(const std::string& name, const COutPoint& outPoint, CClaimValue& claim, bool fCheckTakeover) override;
|
||||||
|
|
||||||
bool insertSupportIntoMap(const std::string& name, const CSupportValue& support, bool fCheckTakeover) override;
|
bool insertSupportIntoMap(const std::string& name, const CSupportValue& support, bool fCheckTakeover) override;
|
||||||
bool removeSupportFromMap(const std::string& name, const COutPoint& outPoint, CSupportValue& support, bool fCheckTakeover) override;
|
bool removeSupportFromMap(const std::string& name, const COutPoint& outPoint, CSupportValue& support, bool fCheckTakeover) override;
|
||||||
|
|
|
@ -356,11 +356,11 @@ struct ClaimTrieChainFixture: public CClaimTrieCacheExpirationFork
|
||||||
|
|
||||||
bool supportEmpty()
|
bool supportEmpty()
|
||||||
{
|
{
|
||||||
for (const auto& entry: cacheSupports) {
|
for (const auto& entry: supportCache) {
|
||||||
if (!entry.second.empty())
|
if (!entry.second.empty())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return cacheSupports.empty() && keyTypeEmpty<std::string>(SUPPORT);
|
return supportCache.empty() && keyTypeEmpty<std::string>(SUPPORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool supportQueueEmpty()
|
bool supportQueueEmpty()
|
||||||
|
|
|
@ -23,22 +23,22 @@ public:
|
||||||
|
|
||||||
void insert(const std::string& key, CClaimTrieData&& data)
|
void insert(const std::string& key, CClaimTrieData&& data)
|
||||||
{
|
{
|
||||||
cache.insert(key, std::move(data));
|
nodesToAddOrUpdate.insert(key, std::move(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool erase(const std::string& key)
|
bool erase(const std::string& key)
|
||||||
{
|
{
|
||||||
return cache.erase(key);
|
return nodesToAddOrUpdate.erase(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cacheSize()
|
int cacheSize()
|
||||||
{
|
{
|
||||||
return cache.height();
|
return nodesToAddOrUpdate.height();
|
||||||
}
|
}
|
||||||
|
|
||||||
CClaimTrie::iterator getCache(const std::string& key)
|
CClaimTrie::iterator getCache(const std::string& key)
|
||||||
{
|
{
|
||||||
return cache.find(key);
|
return nodesToAddOrUpdate.find(key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue