From e386039392fe2f72297c27bf68124f4f4a02544e Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Thu, 24 Oct 2019 10:08:35 +0300 Subject: [PATCH] Simplifications Signed-off-by: Anthony Fieroni --- src/claimtrie/hashes.h | 6 +++--- src/claimtrie/trie.cpp | 3 ++- src/claimtrie/trie.h | 1 + src/claimtrie/txoutpoint.cpp | 32 +++++++++++++++---------------- src/claimtrie/txoutpoint.h | 6 +++--- src/claimtrie/uints.cpp | 18 +++++++++++++++++ src/claimtrie/uints.h | 11 ++++++----- src/init.cpp | 1 + src/test/claimtriecache_tests.cpp | 2 +- src/test/test_bitcoin.cpp | 1 + src/validation.cpp | 22 +++++++++++++-------- 11 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/claimtrie/hashes.h b/src/claimtrie/hashes.h index f03db1487..39a5894f1 100644 --- a/src/claimtrie/hashes.h +++ b/src/claimtrie/hashes.h @@ -1,6 +1,6 @@ -#ifndef CLAIMTRIE_HASH_H -#define CLAIMTRIE_HASH_H +#ifndef CLAIMTRIE_HASHES_H +#define CLAIMTRIE_HASHES_H #include @@ -25,4 +25,4 @@ CUint256 Hash(TIterator begin, TIterator end, Args... args) return CalcHash(&sha, begin, end, args...); } -#endif // CLAIMTRIE_HASH_H +#endif // CLAIMTRIE_HASHES_H diff --git a/src/claimtrie/trie.cpp b/src/claimtrie/trie.cpp index 6f4fb3932..9045b7438 100644 --- a/src/claimtrie/trie.cpp +++ b/src/claimtrie/trie.cpp @@ -89,6 +89,7 @@ static const sqlite::sqlite_config sharedConfig { }; CClaimTrie::CClaimTrie(bool fWipe, int height, + const std::string& dataDir, int nNormalizedNameForkHeight, int64_t nOriginalClaimExpirationTime, int64_t nExtendedClaimExpirationTime, @@ -96,7 +97,7 @@ CClaimTrie::CClaimTrie(bool fWipe, int height, int64_t nAllClaimsInMerkleForkHeight, int proportionalDelayFactor) : nNextHeight(height), - db("claims.sqlite", sharedConfig), + db(dataDir + "/claims.sqlite", sharedConfig), nProportionalDelayFactor(proportionalDelayFactor), nNormalizedNameForkHeight(nNormalizedNameForkHeight), nOriginalClaimExpirationTime(nOriginalClaimExpirationTime), diff --git a/src/claimtrie/trie.h b/src/claimtrie/trie.h index 9c7dbaa9c..f4a9fe651 100644 --- a/src/claimtrie/trie.h +++ b/src/claimtrie/trie.h @@ -60,6 +60,7 @@ public: CClaimTrie(CClaimTrie&&) = delete; CClaimTrie(const CClaimTrie&) = delete; CClaimTrie(bool fWipe, int height, + const std::string& dataDir, int nNormalizedNameForkHeight, int64_t nOriginalClaimExpirationTime, int64_t nExtendedClaimExpirationTime, diff --git a/src/claimtrie/txoutpoint.cpp b/src/claimtrie/txoutpoint.cpp index ec4e3618f..2a514845c 100644 --- a/src/claimtrie/txoutpoint.cpp +++ b/src/claimtrie/txoutpoint.cpp @@ -18,25 +18,25 @@ bool CTxOutPoint::IsNull() const return hash.IsNull() && n == uint32_t(-1); } +bool CTxOutPoint::operator<(const CTxOutPoint& b) const +{ + int cmp = hash.Compare(b.hash); + return cmp < 0 || (cmp == 0 && n < b.n); +} + +bool CTxOutPoint::operator==(const CTxOutPoint& b) const +{ + return hash == b.hash && n == b.n; +} + +bool CTxOutPoint::operator!=(const CTxOutPoint& b) const +{ + return !(*this == b); +} + std::string CTxOutPoint::ToString() const { std::stringstream ss; ss << "CTxOutPoint(" << hash.ToString().substr(0, 10) << ", " << n << ')'; return ss.str(); } - -bool operator<(const CTxOutPoint& a, const CTxOutPoint& b) -{ - int cmp = a.hash.Compare(b.hash); - return cmp < 0 || (cmp == 0 && a.n < b.n); -} - -bool operator==(const CTxOutPoint& a, const CTxOutPoint& b) -{ - return (a.hash == b.hash && a.n == b.n); -} - -bool operator!=(const CTxOutPoint& a, const CTxOutPoint& b) -{ - return !(a == b); -} diff --git a/src/claimtrie/txoutpoint.h b/src/claimtrie/txoutpoint.h index 1ac118a86..3433f4575 100644 --- a/src/claimtrie/txoutpoint.h +++ b/src/claimtrie/txoutpoint.h @@ -27,9 +27,9 @@ public: void SetNull(); bool IsNull() const; - friend bool operator<(const CTxOutPoint& a, const CTxOutPoint& b); - friend bool operator==(const CTxOutPoint& a, const CTxOutPoint& b); - friend bool operator!=(const CTxOutPoint& a, const CTxOutPoint& b); + bool operator<(const CTxOutPoint& b) const; + bool operator==(const CTxOutPoint& b) const; + bool operator!=(const CTxOutPoint& b) const; std::string ToString() const; }; diff --git a/src/claimtrie/uints.cpp b/src/claimtrie/uints.cpp index 5ed4bb2a3..016b1cf2e 100644 --- a/src/claimtrie/uints.cpp +++ b/src/claimtrie/uints.cpp @@ -53,6 +53,24 @@ int CBaseBlob::Compare(const CBaseBlob& other) const return std::memcmp(begin(), other.begin(), size()); } +template +bool CBaseBlob::operator==(const CBaseBlob& b) const +{ + return Compare(b) == 0; +} + +template +bool CBaseBlob::operator!=(const CBaseBlob& b) const +{ + return Compare(b) != 0; +} + +template +bool CBaseBlob::operator<(const CBaseBlob& b) const +{ + return Compare(b) < 0; +} + template std::string CBaseBlob::GetHex() const { diff --git a/src/claimtrie/uints.h b/src/claimtrie/uints.h index fbc3e8fc5..525d8c8d5 100644 --- a/src/claimtrie/uints.h +++ b/src/claimtrie/uints.h @@ -3,6 +3,7 @@ #define CLAIMTRIE_UINTS_H #include +#include #include /** Template base class for fixed-sized opaque blobs. */ @@ -10,7 +11,7 @@ template class CBaseBlob { protected: - static constexpr int WIDTH = BITS / 8; + static constexpr uint32_t WIDTH = BITS / 8; std::unique_ptr data; public: CBaseBlob(); @@ -28,9 +29,9 @@ public: int Compare(const CBaseBlob& other) const; - friend inline bool operator==(const CBaseBlob& a, const CBaseBlob& b) { return a.Compare(b) == 0; } - friend inline bool operator!=(const CBaseBlob& a, const CBaseBlob& b) { return a.Compare(b) != 0; } - friend inline bool operator<(const CBaseBlob& a, const CBaseBlob& b) { return a.Compare(b) < 0; } + bool operator==(const CBaseBlob& b) const; + bool operator!=(const CBaseBlob& b) const; + bool operator<(const CBaseBlob& b) const; std::string GetHex() const; void SetHex(const char* psz); @@ -44,7 +45,7 @@ public: const uint8_t* begin() const; const uint8_t* end() const; - constexpr uint32_t size() const { return WIDTH; } + static constexpr uint32_t size() { return WIDTH; } }; typedef CBaseBlob<160> CUint160; diff --git a/src/init.cpp b/src/init.cpp index 75d6b95ad..583bd9f9c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1459,6 +1459,7 @@ bool AppInitMain() if (g_logger->Enabled() && LogAcceptCategory(BCLog::CLAIMS)) CLogPrint::global().setLogger(g_logger); pclaimTrie = new CClaimTrie(fReindex || fReindexChainState, 0, + (GetDataDir() / "claimtrie").string(), consensus.nNormalizedNameForkHeight, consensus.nOriginalClaimExpirationTime, consensus.nExtendedClaimExpirationTime, diff --git a/src/test/claimtriecache_tests.cpp b/src/test/claimtriecache_tests.cpp index 3c8e36c68..1632b1734 100644 --- a/src/test/claimtriecache_tests.cpp +++ b/src/test/claimtriecache_tests.cpp @@ -70,7 +70,7 @@ BOOST_FIXTURE_TEST_SUITE(claimtriecache_tests, RegTestingSetup) BOOST_AUTO_TEST_CASE(merkle_hash_single_test) { // check empty trie - auto one = uint256S("0000000000000000000000000000000000000000000000000000000000000001"); + auto one = CUint256S("0000000000000000000000000000000000000000000000000000000000000001"); CClaimTrieCacheTest cc(pclaimTrie); BOOST_CHECK_EQUAL(one, cc.getMerkleHash()); diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index 2e4819d73..7f65405b1 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -149,6 +149,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get())); auto& consensus = chainparams.GetConsensus(); pclaimTrie = new CClaimTrie(true, false, + (GetDataDir() / "claimtrie").string(), consensus.nNormalizedNameForkHeight, consensus.nOriginalClaimExpirationTime, consensus.nExtendedClaimExpirationTime, diff --git a/src/validation.cpp b/src/validation.cpp index c6e0bd5ad..83ecdd64e 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1475,12 +1475,20 @@ int ApplyTxInUndo(unsigned int index, CTxUndo& txUndo, CCoinsViewCache& view, CC return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN; } +template +inline bool equals(const T1& hash1, const T2& hash2) +{ + static_assert((std::is_same::value && std::is_same::value) || + (std::is_same::value && std::is_same::value), "Hash types are incompatible"); + return std::equal(hash1.begin(), hash1.end(), hash2.begin()); +} + /** Undo the effects of this block (with given index) on the UTXO set represented by coins. * When FAILED is returned, view is left in an indeterminate state. */ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view, CClaimTrieCache& trieCache) { assert(pindex->GetBlockHash() == view.GetBestBlock()); - if (pindex->hashClaimTrie != trieCache.getMerkleHash()) { + if (!equals(pindex->hashClaimTrie, trieCache.getMerkleHash())) { LogPrintf("%s: Indexed claim hash doesn't match current: %s vs %s\n", __func__, pindex->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString()); assert(false); @@ -1555,10 +1563,9 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI // move best block pointer to prevout block view.SetBestBlock(pindex->pprev->GetBlockHash()); assert(trieCache.finalizeDecrement(blockUndo.takeoverUndo)); - auto merkleHash = trieCache.getMerkleHash(); - if (merkleHash != pindex->pprev->hashClaimTrie) { + if (!equals(trieCache.getMerkleHash(), pindex->pprev->hashClaimTrie)) { LogPrintf("Hash comparison failure at block %d\n", pindex->nHeight); - assert(merkleHash == pindex->pprev->hashClaimTrie); + assert(false); } return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN; @@ -1769,7 +1776,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl assert(hashPrevBlock == view.GetBestBlock()); // also verify that the trie cache's current state corresponds to the previous block - if (pindex->pprev != nullptr && pindex->pprev->hashClaimTrie != trieCache.getMerkleHash()) { + if (pindex->pprev != nullptr && !equals(pindex->pprev->hashClaimTrie, trieCache.getMerkleHash())) { LogPrintf("%s: Previous block claim hash doesn't match current: %s vs %s\n", __func__, pindex->pprev->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString()); assert(false); @@ -2032,8 +2039,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl blockundo.insertSupportUndo, blockundo.expireSupportUndo, blockundo.takeoverUndo); assert(incremented); - if (trieCache.getMerkleHash() != block.hashClaimTrie) - { + if (!equals(trieCache.getMerkleHash(), block.hashClaimTrie)) { return state.DoS(100, error("ConnectBlock() : the merkle root of the claim trie does not match " "(actual=%s vs block=%s on height=%d)", trieCache.getMerkleHash().GetHex(), block.hashClaimTrie.GetHex(), pindex->nHeight), REJECT_INVALID, "bad-claim-merkle-hash"); @@ -2323,7 +2329,7 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha bool flushed = view.Flush(); assert(flushed); assert(trieCache.flush()); - assert(pindexDelete->pprev->hashClaimTrie == trieCache.getMerkleHash()); + assert(equals(pindexDelete->pprev->hashClaimTrie, trieCache.getMerkleHash())); } LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI); // Write the chain state to disk, if necessary.