Code refactor
Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
parent
6423377fe1
commit
c83062bfa8
4 changed files with 574 additions and 586 deletions
File diff suppressed because it is too large
Load diff
|
@ -268,6 +268,15 @@ struct CNameOutPointType
|
|||
|
||||
struct CClaimIndexElement
|
||||
{
|
||||
CClaimIndexElement()
|
||||
{
|
||||
}
|
||||
|
||||
CClaimIndexElement(const std::string& name, const CClaimValue& claim)
|
||||
: name(name), claim(claim)
|
||||
{
|
||||
}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
|
@ -322,9 +331,8 @@ public:
|
|||
friend class CClaimTrieCacheNormalizationFork;
|
||||
};
|
||||
|
||||
class CClaimTrieProofNode
|
||||
struct CClaimTrieProofNode
|
||||
{
|
||||
public:
|
||||
CClaimTrieProofNode(std::vector<std::pair<unsigned char, uint256>> children, bool hasValue, const uint256& valHash)
|
||||
: children(std::move(children)), hasValue(hasValue), valHash(valHash)
|
||||
{
|
||||
|
@ -340,9 +348,8 @@ public:
|
|||
uint256 valHash;
|
||||
};
|
||||
|
||||
class CClaimTrieProof
|
||||
struct CClaimTrieProof
|
||||
{
|
||||
public:
|
||||
CClaimTrieProof()
|
||||
{
|
||||
}
|
||||
|
@ -363,12 +370,13 @@ public:
|
|||
int nHeightOfLastTakeover;
|
||||
};
|
||||
|
||||
typedef std::pair<std::string, CClaimValue> claimQueueEntryType;
|
||||
typedef std::vector<claimQueueEntryType> claimQueueRowType;
|
||||
template <typename T>
|
||||
using queueEntryType = std::pair<std::string, T>;
|
||||
|
||||
typedef std::vector<queueEntryType<CClaimValue>> claimQueueRowType;
|
||||
typedef std::map<int, claimQueueRowType> claimQueueType;
|
||||
|
||||
typedef std::pair<std::string, CSupportValue> supportQueueEntryType;
|
||||
typedef std::vector<supportQueueEntryType> supportQueueRowType;
|
||||
typedef std::vector<queueEntryType<CSupportValue>> supportQueueRowType;
|
||||
typedef std::map<int, supportQueueRowType> supportQueueType;
|
||||
|
||||
typedef std::vector<COutPointHeightType> queueNameRowType;
|
||||
|
@ -389,7 +397,7 @@ public:
|
|||
virtual ~CClaimTrieCacheBase() = default;
|
||||
|
||||
uint256 getMerkleHash();
|
||||
bool checkConsistency(int minimumHeight = 1) const;
|
||||
bool checkConsistency() const;
|
||||
|
||||
bool getClaimById(const uint160& claimId, std::string& name, CClaimValue& claim) const;
|
||||
|
||||
|
@ -461,29 +469,20 @@ protected:
|
|||
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 addClaimToQueues(const std::string& name, const CClaimValue& claim);
|
||||
|
||||
virtual bool addSupportToQueues(const std::string& name, const CSupportValue& support);
|
||||
virtual bool removeSupportFromQueue(const std::string& name, const COutPoint& outPoint, CSupportValue& support);
|
||||
|
||||
virtual std::string adjustNameForValidHeight(const std::string& name, int validHeight) const;
|
||||
|
||||
void addToExpirationQueue(int nExpirationHeight, CNameOutPointType& entry);
|
||||
void removeFromExpirationQueue(const std::string& name, const COutPoint& outPoint, int nHeight);
|
||||
|
||||
void addSupportToExpirationQueue(int nExpirationHeight, CNameOutPointType& entry);
|
||||
void removeSupportFromExpirationQueue(const std::string& name, const COutPoint& outPoint, int nHeight);
|
||||
|
||||
supportEntryType getSupportsForName(const std::string& name) const;
|
||||
|
||||
int getDelayForName(const std::string& name);
|
||||
virtual int getDelayForName(const std::string& name, const uint160& claimId);
|
||||
int getDelayForName(const std::string& name) const;
|
||||
virtual int getDelayForName(const std::string& name, const uint160& claimId) const;
|
||||
|
||||
CClaimTrie::iterator cacheData(const std::string& name, bool create = true);
|
||||
|
||||
bool getLastTakeoverForName(const std::string& name, uint160& claimId, int& takeoverHeight) const;
|
||||
|
||||
int getNumBlocksOfContinuousOwnership(const std::string& name);
|
||||
int getNumBlocksOfContinuousOwnership(const std::string& name) const;
|
||||
|
||||
expirationQueueType expirationQueueCache;
|
||||
expirationQueueType supportExpirationQueueCache;
|
||||
|
@ -491,6 +490,9 @@ protected:
|
|||
int nNextHeight; // Height of the block that is being worked on, which is
|
||||
// one greater than the height of the chain's tip
|
||||
|
||||
template <typename T>
|
||||
void reactivate(const expirationQueueRowType& row, int height, bool increment);
|
||||
|
||||
private:
|
||||
uint256 hashBlock;
|
||||
|
||||
|
@ -522,12 +524,47 @@ private:
|
|||
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);
|
||||
|
||||
typename claimQueueType::value_type* getQueueCacheRow(int nHeight, bool createIfNotExists = false);
|
||||
template <typename T>
|
||||
std::pair<const int, std::vector<queueEntryType<T>>>* getQueueCacheRow(int nHeight, bool createIfNotExists = false);
|
||||
|
||||
template <typename T>
|
||||
typename queueNameType::value_type* getQueueCacheNameRow(const std::string& name, bool createIfNotExists = false);
|
||||
|
||||
template <typename T>
|
||||
typename expirationQueueType::value_type* getExpirationQueueCacheRow(int nHeight, bool createIfNotExists = false);
|
||||
typename supportQueueType::value_type* getSupportQueueCacheRow(int nHeight, bool createIfNotExists = false);
|
||||
typename queueNameType::value_type* getSupportQueueCacheNameRow(const std::string& name, bool createIfNotExists = false);
|
||||
typename expirationQueueType::value_type* getSupportExpirationQueueCacheRow(int nHeight, bool createIfNotExists = false);
|
||||
|
||||
template <typename T>
|
||||
bool haveInQueue(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight);
|
||||
|
||||
template <typename T>
|
||||
T add(const std::string& name, const COutPoint& outPoint, const uint160& claimId, CAmount nAmount, int nHeight);
|
||||
|
||||
template <typename T>
|
||||
bool remove(T& value, const std::string& name, const COutPoint& outPoint, int nHeight, int& nValidAtHeight, bool fCheckTakeover = false);
|
||||
|
||||
template <typename T>
|
||||
bool addToQueue(const std::string& name, const T& value);
|
||||
|
||||
template <typename T>
|
||||
bool removeFromQueue(const std::string& name, const COutPoint& outPoint, T& value);
|
||||
|
||||
template <typename T>
|
||||
bool addToCache(const std::string& name, const T& value, bool fCheckTakeover = false);
|
||||
|
||||
template <typename T>
|
||||
bool removeFromCache(const std::string& name, const COutPoint& outPoint, T& value, bool fCheckTakeover = false);
|
||||
|
||||
template <typename T>
|
||||
bool undoSpend(const std::string& name, const T& value, int nValidAtHeight);
|
||||
|
||||
template <typename T>
|
||||
void undoIncrement(insertUndoType& insertUndo, std::vector<queueEntryType<T>>& expireUndo, std::set<T>* deleted = nullptr);
|
||||
|
||||
template <typename T>
|
||||
void undoDecrement(insertUndoType& insertUndo, std::vector<queueEntryType<T>>& expireUndo, std::vector<CClaimIndexElement>* added = nullptr, std::set<T>* deleted = nullptr);
|
||||
|
||||
template <typename T>
|
||||
void undoIncrement(const std::string& name, insertUndoType& insertUndo, std::vector<queueEntryType<T>>& expireUndo);
|
||||
|
||||
// for unit test
|
||||
friend struct ClaimTrieChainFixture;
|
||||
|
@ -558,8 +595,6 @@ public:
|
|||
private:
|
||||
int nExpirationTime;
|
||||
bool forkForExpirationChange(bool increment);
|
||||
void removeAndAddSupportToExpirationQueue(expirationQueueRowType& row, int height, bool increment);
|
||||
void removeAndAddToExpirationQueue(expirationQueueRowType& row, int height, bool increment);
|
||||
};
|
||||
|
||||
class CClaimTrieCacheNormalizationFork : public CClaimTrieCacheExpirationFork
|
||||
|
@ -598,10 +633,7 @@ protected:
|
|||
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;
|
||||
|
||||
int getDelayForName(const std::string& name, const uint160& claimId) override;
|
||||
|
||||
bool addClaimToQueues(const std::string& name, const CClaimValue& claim) override;
|
||||
bool addSupportToQueues(const std::string& name, const CSupportValue& support) override;
|
||||
int getDelayForName(const std::string& name, const uint160& claimId) const override;
|
||||
|
||||
std::string adjustNameForValidHeight(const std::string& name, int validHeight) const override;
|
||||
|
||||
|
|
|
@ -49,30 +49,6 @@ void CClaimTrieCacheExpirationFork::expirationForkActive(int nHeight, bool incre
|
|||
forkForExpirationChange(increment);
|
||||
}
|
||||
|
||||
void CClaimTrieCacheExpirationFork::removeAndAddToExpirationQueue(expirationQueueRowType& row, int height, bool increment)
|
||||
{
|
||||
for (auto e = row.begin(); e != row.end(); ++e) {
|
||||
// remove and insert with new expiration time
|
||||
removeFromExpirationQueue(e->name, e->outPoint, height);
|
||||
int extend_expiration = Params().GetConsensus().nExtendedClaimExpirationTime - Params().GetConsensus().nOriginalClaimExpirationTime;
|
||||
int new_expiration_height = increment ? height + extend_expiration : height - extend_expiration;
|
||||
CNameOutPointType entry(e->name, e->outPoint);
|
||||
addToExpirationQueue(new_expiration_height, entry);
|
||||
}
|
||||
}
|
||||
|
||||
void CClaimTrieCacheExpirationFork::removeAndAddSupportToExpirationQueue(expirationQueueRowType& row, int height, bool increment)
|
||||
{
|
||||
for (auto e = row.begin(); e != row.end(); ++e) {
|
||||
// remove and insert with new expiration time
|
||||
removeSupportFromExpirationQueue(e->name, e->outPoint, height);
|
||||
int extend_expiration = Params().GetConsensus().nExtendedClaimExpirationTime - Params().GetConsensus().nOriginalClaimExpirationTime;
|
||||
int new_expiration_height = increment ? height + extend_expiration : height - extend_expiration;
|
||||
CNameOutPointType entry(e->name, e->outPoint);
|
||||
addSupportToExpirationQueue(new_expiration_height, entry);
|
||||
}
|
||||
}
|
||||
|
||||
bool CClaimTrieCacheExpirationFork::forkForExpirationChange(bool increment)
|
||||
{
|
||||
/*
|
||||
|
@ -93,14 +69,14 @@ bool CClaimTrieCacheExpirationFork::forkForExpirationChange(bool increment)
|
|||
if (key.first == EXP_QUEUE_ROW) {
|
||||
expirationQueueRowType row;
|
||||
if (pcursor->GetValue(row)) {
|
||||
removeAndAddToExpirationQueue(row, height, increment);
|
||||
reactivate<CClaimValue>(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)) {
|
||||
removeAndAddSupportToExpirationQueue(row, height, increment);
|
||||
reactivate<CSupportValue>(row, height, increment);
|
||||
} else {
|
||||
return error("%s(): error reading support expiration queue rows from disk", __func__);
|
||||
}
|
||||
|
@ -195,9 +171,8 @@ bool CClaimTrieCacheNormalizationFork::normalizeAllNamesInTrieIfNecessary(insert
|
|||
if (support.nHeight + expirationTime() <= nNextHeight)
|
||||
continue;
|
||||
|
||||
CSupportValue removed;
|
||||
assert(removeSupportFromMap(it.key(), support.outPoint, removed, false));
|
||||
expireSupportUndo.emplace_back(it.key(), removed);
|
||||
assert(removeSupportFromMap(it.key(), support.outPoint, support, false));
|
||||
expireSupportUndo.emplace_back(it.key(), support);
|
||||
assert(insertSupportIntoMap(normalized, support, false));
|
||||
insertSupportUndo.emplace_back(it.key(), support.outPoint, -1);
|
||||
}
|
||||
|
@ -205,17 +180,16 @@ bool CClaimTrieCacheNormalizationFork::normalizeAllNamesInTrieIfNecessary(insert
|
|||
namesToCheckForTakeover.insert(normalized);
|
||||
|
||||
auto cached = cacheData(it.key(), false);
|
||||
if (!cached || cached->claims.empty())
|
||||
if (!cached || cached->empty())
|
||||
continue;
|
||||
|
||||
for (auto& claim : it->claims) {
|
||||
if (claim.nHeight + expirationTime() <= nNextHeight)
|
||||
continue;
|
||||
|
||||
CClaimValue removed;
|
||||
assert(removeClaimFromTrie(it.key(), claim.outPoint, removed, false));
|
||||
removeUndo.emplace_back(it.key(), removed);
|
||||
assert(insertClaimIntoTrie(normalized, claim, false));
|
||||
assert(removeClaimFromTrie(it.key(), claim.outPoint, claim, false));
|
||||
removeUndo.emplace_back(it.key(), claim);
|
||||
assert(insertClaimIntoTrie(normalized, claim, true));
|
||||
insertUndo.emplace_back(it.key(), claim.outPoint, -1);
|
||||
}
|
||||
|
||||
|
@ -255,21 +229,11 @@ CClaimsForNameType CClaimTrieCacheNormalizationFork::getClaimsForName(const std:
|
|||
return CClaimTrieCacheExpirationFork::getClaimsForName(normalizeClaimName(name));
|
||||
}
|
||||
|
||||
int CClaimTrieCacheNormalizationFork::getDelayForName(const std::string& name, const uint160& claimId)
|
||||
int CClaimTrieCacheNormalizationFork::getDelayForName(const std::string& name, const uint160& claimId) const
|
||||
{
|
||||
return CClaimTrieCacheExpirationFork::getDelayForName(normalizeClaimName(name), claimId);
|
||||
}
|
||||
|
||||
bool CClaimTrieCacheNormalizationFork::addClaimToQueues(const std::string& name, const CClaimValue& claim)
|
||||
{
|
||||
return CClaimTrieCacheExpirationFork::addClaimToQueues(normalizeClaimName(name, claim.nValidAtHeight > Params().GetConsensus().nNormalizedNameForkHeight), claim);
|
||||
}
|
||||
|
||||
bool CClaimTrieCacheNormalizationFork::addSupportToQueues(const std::string& name, const CSupportValue& support)
|
||||
{
|
||||
return CClaimTrieCacheExpirationFork::addSupportToQueues(normalizeClaimName(name, support.nValidAtHeight > Params().GetConsensus().nNormalizedNameForkHeight), support);
|
||||
}
|
||||
|
||||
std::string CClaimTrieCacheNormalizationFork::adjustNameForValidHeight(const std::string& name, int validHeight) const
|
||||
{
|
||||
return normalizeClaimName(name, validHeight > Params().GetConsensus().nNormalizedNameForkHeight);
|
||||
|
|
|
@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
|
|||
|
||||
BOOST_CHECK(!pclaimTrie->empty());
|
||||
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2);
|
||||
BOOST_CHECK(ntState.checkConsistency(0));
|
||||
BOOST_CHECK(ntState.checkConsistency());
|
||||
|
||||
CClaimTrieCacheTest ntState1(pclaimTrie);
|
||||
ntState1.removeClaimFromTrie(std::string("test"), tx1OutPoint, unused, true);
|
||||
|
@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
|
|||
|
||||
BOOST_CHECK(!pclaimTrie->empty());
|
||||
BOOST_CHECK_EQUAL(ntState2.getMerkleHash(), hash3);
|
||||
BOOST_CHECK(ntState2.checkConsistency(0));
|
||||
BOOST_CHECK(ntState2.checkConsistency());
|
||||
|
||||
CClaimTrieCacheTest ntState3(pclaimTrie);
|
||||
ntState3.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, hash160, 50, 100, 200), true);
|
||||
|
@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
|
|||
ntState3.flush();
|
||||
BOOST_CHECK(!pclaimTrie->empty());
|
||||
BOOST_CHECK_EQUAL(ntState3.getMerkleHash(), hash4);
|
||||
BOOST_CHECK(ntState3.checkConsistency(0));
|
||||
BOOST_CHECK(ntState3.checkConsistency());
|
||||
|
||||
CClaimTrieCacheTest ntState4(pclaimTrie);
|
||||
ntState4.removeClaimFromTrie(std::string("abab"), tx6OutPoint, unused, true);
|
||||
|
@ -160,7 +160,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
|
|||
ntState4.flush();
|
||||
BOOST_CHECK(!pclaimTrie->empty());
|
||||
BOOST_CHECK_EQUAL(ntState4.getMerkleHash(), hash2);
|
||||
BOOST_CHECK(ntState4.checkConsistency(0));
|
||||
BOOST_CHECK(ntState4.checkConsistency());
|
||||
|
||||
CClaimTrieCacheTest ntState5(pclaimTrie);
|
||||
ntState5.removeClaimFromTrie(std::string("test"), tx3OutPoint, unused, true);
|
||||
|
@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
|
|||
ntState5.flush();
|
||||
BOOST_CHECK(!pclaimTrie->empty());
|
||||
BOOST_CHECK_EQUAL(ntState5.getMerkleHash(), hash2);
|
||||
BOOST_CHECK(ntState5.checkConsistency(0));
|
||||
BOOST_CHECK(ntState5.checkConsistency());
|
||||
|
||||
CClaimTrieCacheTest ntState6(pclaimTrie);
|
||||
ntState6.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, hash160, 50, 101, 201), true);
|
||||
|
@ -178,7 +178,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
|
|||
ntState6.flush();
|
||||
BOOST_CHECK(!pclaimTrie->empty());
|
||||
BOOST_CHECK_EQUAL(ntState6.getMerkleHash(), hash2);
|
||||
BOOST_CHECK(ntState6.checkConsistency(0));
|
||||
BOOST_CHECK(ntState6.checkConsistency());
|
||||
|
||||
CClaimTrieCacheTest ntState7(pclaimTrie);
|
||||
ntState7.removeClaimFromTrie(std::string("test"), tx3OutPoint, unused, true);
|
||||
|
@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
|
|||
ntState7.flush();
|
||||
BOOST_CHECK(pclaimTrie->empty());
|
||||
BOOST_CHECK_EQUAL(ntState7.getMerkleHash(), hash0);
|
||||
BOOST_CHECK(ntState7.checkConsistency(0));
|
||||
BOOST_CHECK(ntState7.checkConsistency());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(basic_insertion_info_test)
|
||||
|
@ -337,13 +337,13 @@ BOOST_AUTO_TEST_CASE(trie_stays_consistent_test)
|
|||
BOOST_CHECK(cache.insertClaimIntoTrie(name, value, false));
|
||||
|
||||
cache.flush();
|
||||
BOOST_CHECK(cache.checkConsistency(0));
|
||||
BOOST_CHECK(cache.checkConsistency());
|
||||
|
||||
for (auto& name: names) {
|
||||
CClaimValue temp;
|
||||
BOOST_CHECK(cache.removeClaimFromTrie(name, COutPoint(), temp, false));
|
||||
cache.flush();
|
||||
BOOST_CHECK(cache.checkConsistency(0));
|
||||
BOOST_CHECK(cache.checkConsistency());
|
||||
}
|
||||
BOOST_CHECK_EQUAL(trie.height(), 0);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue