Simplifications
Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
parent
c0a0263058
commit
792ba0b032
11 changed files with 66 additions and 37 deletions
|
@ -1,6 +1,6 @@
|
|||
|
||||
#ifndef CLAIMTRIE_HASH_H
|
||||
#define CLAIMTRIE_HASH_H
|
||||
#ifndef CLAIMTRIE_HASHES_H
|
||||
#define CLAIMTRIE_HASHES_H
|
||||
|
||||
#include <openssl/sha.h>
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -53,6 +53,24 @@ int CBaseBlob<BITS>::Compare(const CBaseBlob& other) const
|
|||
return std::memcmp(begin(), other.begin(), size());
|
||||
}
|
||||
|
||||
template<uint32_t BITS>
|
||||
bool CBaseBlob<BITS>::operator==(const CBaseBlob& b) const
|
||||
{
|
||||
return Compare(b) == 0;
|
||||
}
|
||||
|
||||
template<uint32_t BITS>
|
||||
bool CBaseBlob<BITS>::operator!=(const CBaseBlob& b) const
|
||||
{
|
||||
return Compare(b) != 0;
|
||||
}
|
||||
|
||||
template<uint32_t BITS>
|
||||
bool CBaseBlob<BITS>::operator<(const CBaseBlob& b) const
|
||||
{
|
||||
return Compare(b) < 0;
|
||||
}
|
||||
|
||||
template<uint32_t BITS>
|
||||
std::string CBaseBlob<BITS>::GetHex() const
|
||||
{
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#define CLAIMTRIE_UINTS_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/** Template base class for fixed-sized opaque blobs. */
|
||||
|
@ -10,7 +11,7 @@ template<uint32_t BITS>
|
|||
class CBaseBlob
|
||||
{
|
||||
protected:
|
||||
static constexpr int WIDTH = BITS / 8;
|
||||
static constexpr uint32_t WIDTH = BITS / 8;
|
||||
std::unique_ptr<uint8_t[]> 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;
|
||||
|
|
|
@ -1488,6 +1488,7 @@ bool AppInitMain(InitInterfaces& interfaces)
|
|||
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,
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -144,6 +144,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
|
|||
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
|
||||
auto& consensus = chainparams.GetConsensus();
|
||||
pclaimTrie = new CClaimTrie(true, false,
|
||||
(GetDataDir() / "claimtrie").string(),
|
||||
consensus.nNormalizedNameForkHeight,
|
||||
consensus.nOriginalClaimExpirationTime,
|
||||
consensus.nExtendedClaimExpirationTime,
|
||||
|
|
|
@ -1740,12 +1740,20 @@ int ApplyTxInUndo(unsigned int index, CTxUndo& txUndo, CCoinsViewCache& view, CC
|
|||
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline bool equals(const T1& hash1, const T2& hash2)
|
||||
{
|
||||
static_assert((std::is_same<T1, uint256>::value && std::is_same<T2, CUint256>::value) ||
|
||||
(std::is_same<T2, uint256>::value && std::is_same<T1, CUint256>::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);
|
||||
|
@ -1820,10 +1828,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;
|
||||
|
@ -2021,7 +2028,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);
|
||||
|
@ -2308,8 +2315,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");
|
||||
|
@ -2595,7 +2601,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.
|
||||
|
|
Loading…
Reference in a new issue