diff --git a/src/claimtrie.cpp b/src/claimtrie.cpp index 0758b25b5..d6ad00a5b 100644 --- a/src/claimtrie.cpp +++ b/src/claimtrie.cpp @@ -41,60 +41,38 @@ uint256 getValueHash(const COutPoint& outPoint, int nHeightOfLastTakeover) return result; } +template +bool equals(const T& lhs, const T& rhs) +{ + return lhs == rhs; +} + template bool equals(const T& value, const COutPoint& outPoint) { return value.outPoint == outPoint; } -template -bool equals(const T& value, const std::string& name, const COutPoint& outPoint) -{ - return value.name == name && value.outPoint == outPoint; -} - template -bool equals(const std::pair& pair, const std::string& name, const COutPoint& outPoint) +bool equals(const std::pair& pair, const CNameOutPointType& point) { - return pair.first == name && pair.second.outPoint == outPoint; + return pair.first == point.name && pair.second.outPoint == point.outPoint; } -template -auto findOutPoint(T& cont, const COutPoint& outPoint) -> decltype(cont.begin()) +template +auto findOutPoint(T& cont, const C& point) -> decltype(cont.begin()) { using type = typename T::value_type; static_assert(std::is_same::type, std::vector>::value, "T should be a vector type"); - return std::find_if(cont.begin(), cont.end(), [&outPoint](const type& val) { - return equals(val, outPoint); + return std::find_if(cont.begin(), cont.end(), [&point](const type& val) { + return equals(val, point); }); } -template -auto findOutPoint(T& cont, const std::string& name, const COutPoint& outPoint) -> decltype(cont.begin()) +template +bool eraseOutPoint(std::vector& cont, const C& point, T* value = nullptr) { - using type = typename T::value_type; - static_assert(std::is_same::type, std::vector>::value, "T should be a vector type"); - return std::find_if(cont.begin(), cont.end(), [&name, &outPoint](const type& val) { - return equals(val, name, outPoint); - }); -} - -template -bool eraseOutPoint(std::vector& cont, const COutPoint& outPoint, T* value = nullptr) -{ - auto it = findOutPoint(cont, outPoint); - if (it == cont.end()) - return false; - if (value) - std::swap(*it, *value); - cont.erase(it); - return true; -} - -template -bool eraseOutPoint(std::vector& cont, const std::string& name, const COutPoint& outPoint, T* value = nullptr) -{ - auto it = findOutPoint(cont, name, outPoint); + auto it = findOutPoint(cont, point); if (it == cont.end()) return false; if (value) @@ -269,7 +247,7 @@ bool CClaimTrieCacheBase::haveInQueue(const std::string& name, const COutPoint& if (itNameRow != nameRow->second.end()) { nValidAtHeight = itNameRow->nHeight; if (auto row = getQueueCacheRow(nValidAtHeight)) { - auto iRow = findOutPoint(row->second, name, outPoint); + auto iRow = findOutPoint(row->second, CNameOutPointType{name, outPoint}); if (iRow != row->second.end()) { if (iRow->second.nValidAtHeight != nValidAtHeight) LogPrintf("%s: An inconsistency was found in the support queue. Please report this to the developers:\nDifferent nValidAtHeight between named queue and height queue\n: name: %s, txid: %s, nOut: %d, nValidAtHeight in named queue: %d, nValidAtHeight in height queue: %d current height: %d\n", __func__, name, outPoint.hash.GetHex(), outPoint.n, nValidAtHeight, iRow->second.nValidAtHeight, nNextHeight); @@ -856,7 +834,7 @@ bool CClaimTrieCacheBase::removeFromQueue(const std::string& name, const COutPoi auto itQueueName = findOutPoint(itQueueNameRow->second, outPoint); if (itQueueName != itQueueNameRow->second.end()) { if (auto itQueueRow = getQueueCacheRow(itQueueName->nHeight)) { - auto itQueue = findOutPoint(itQueueRow->second, name, outPoint); + auto itQueue = findOutPoint(itQueueRow->second, CNameOutPointType{name, outPoint}); if (itQueue != itQueueRow->second.end()) { std::swap(value, itQueue->second); itQueueNameRow->second.erase(itQueueName); @@ -933,7 +911,7 @@ bool CClaimTrieCacheBase::remove(T& value, const std::string& name, const COutPo if (removeFromQueue(adjusted, outPoint, value) || removeFromCache(name, outPoint, value, fCheckTakeover)) { int expirationHeight = value.nHeight + expirationTime(); if (auto itQueueRow = getExpirationQueueCacheRow(expirationHeight)) - eraseOutPoint(itQueueRow->second, adjusted, outPoint); + eraseOutPoint(itQueueRow->second, CNameOutPointType{adjusted, outPoint}); nValidAtHeight = value.nValidAtHeight; return true; } @@ -1285,7 +1263,7 @@ void CClaimTrieCacheBase::reactivate(const expirationQueueRowType& row, int heig for (auto e = row.begin(); e != row.end(); ++e) { // remove and insert with new expiration time if (auto itQueueRow = getExpirationQueueCacheRow(height)) - eraseOutPoint(itQueueRow->second, e->name, e->outPoint); + eraseOutPoint(itQueueRow->second, CNameOutPointType{e->name, e->outPoint}); int extend_expiration = Params().GetConsensus().nExtendedClaimExpirationTime - Params().GetConsensus().nOriginalClaimExpirationTime; int new_expiration_height = increment ? height + extend_expiration : height - extend_expiration; @@ -1294,8 +1272,15 @@ void CClaimTrieCacheBase::reactivate(const expirationQueueRowType& row, int heig } } -template void CClaimTrieCacheBase::reactivate(const expirationQueueRowType& row, int height, bool increment); -template void CClaimTrieCacheBase::reactivate(const expirationQueueRowType& row, int height, bool increment); +void CClaimTrieCacheBase::reactivateClaim(const expirationQueueRowType& row, int height, bool increment) +{ + reactivate(row, height, increment); +} + +void CClaimTrieCacheBase::reactivateSupport(const expirationQueueRowType& row, int height, bool increment) +{ + reactivate(row, height, increment); +} int CClaimTrieCacheBase::getNumBlocksOfContinuousOwnership(const std::string& name) const { diff --git a/src/claimtrie.h b/src/claimtrie.h index 17fc5e964..f268c670b 100644 --- a/src/claimtrie.h +++ b/src/claimtrie.h @@ -256,6 +256,11 @@ struct CNameOutPointType { } + bool operator==(const CNameOutPointType& other) const + { + return name == other.name && outPoint == other.outPoint; + } + ADD_SERIALIZE_METHODS; template @@ -484,15 +489,15 @@ protected: int getNumBlocksOfContinuousOwnership(const std::string& name) const; + void reactivateClaim(const expirationQueueRowType& row, int height, bool increment); + void reactivateSupport(const expirationQueueRowType& row, int height, bool increment); + expirationQueueType expirationQueueCache; expirationQueueType supportExpirationQueueCache; int nNextHeight; // Height of the block that is being worked on, which is // one greater than the height of the chain's tip - template - void reactivate(const expirationQueueRowType& row, int height, bool increment); - private: uint256 hashBlock; @@ -566,6 +571,9 @@ private: template void undoIncrement(const std::string& name, insertUndoType& insertUndo, std::vector>& expireUndo); + template + void reactivate(const expirationQueueRowType& row, int height, bool increment); + // for unit test friend struct ClaimTrieChainFixture; friend class CClaimTrieCacheTest; diff --git a/src/claimtrieforks.cpp b/src/claimtrieforks.cpp index a5fbf44ad..01c69a4fd 100644 --- a/src/claimtrieforks.cpp +++ b/src/claimtrieforks.cpp @@ -69,14 +69,14 @@ bool CClaimTrieCacheExpirationFork::forkForExpirationChange(bool increment) if (key.first == EXP_QUEUE_ROW) { expirationQueueRowType row; if (pcursor->GetValue(row)) { - reactivate(row, height, increment); + reactivateClaim(row, height, increment); } else { return error("%s(): error reading expiration queue rows from disk", __func__); } } else if (key.first == SUPPORT_EXP_QUEUE_ROW) { expirationQueueRowType row; if (pcursor->GetValue(row)) { - reactivate(row, height, increment); + reactivateSupport(row, height, increment); } else { return error("%s(): error reading support expiration queue rows from disk", __func__); }