Simplifications
Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
parent
c6334a5ef2
commit
e386039392
11 changed files with 66 additions and 37 deletions
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
#ifndef CLAIMTRIE_HASH_H
|
#ifndef CLAIMTRIE_HASHES_H
|
||||||
#define CLAIMTRIE_HASH_H
|
#define CLAIMTRIE_HASHES_H
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
|
@ -25,4 +25,4 @@ CUint256 Hash(TIterator begin, TIterator end, Args... args)
|
||||||
return CalcHash(&sha, begin, end, 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,
|
CClaimTrie::CClaimTrie(bool fWipe, int height,
|
||||||
|
const std::string& dataDir,
|
||||||
int nNormalizedNameForkHeight,
|
int nNormalizedNameForkHeight,
|
||||||
int64_t nOriginalClaimExpirationTime,
|
int64_t nOriginalClaimExpirationTime,
|
||||||
int64_t nExtendedClaimExpirationTime,
|
int64_t nExtendedClaimExpirationTime,
|
||||||
|
@ -96,7 +97,7 @@ CClaimTrie::CClaimTrie(bool fWipe, int height,
|
||||||
int64_t nAllClaimsInMerkleForkHeight,
|
int64_t nAllClaimsInMerkleForkHeight,
|
||||||
int proportionalDelayFactor) :
|
int proportionalDelayFactor) :
|
||||||
nNextHeight(height),
|
nNextHeight(height),
|
||||||
db("claims.sqlite", sharedConfig),
|
db(dataDir + "/claims.sqlite", sharedConfig),
|
||||||
nProportionalDelayFactor(proportionalDelayFactor),
|
nProportionalDelayFactor(proportionalDelayFactor),
|
||||||
nNormalizedNameForkHeight(nNormalizedNameForkHeight),
|
nNormalizedNameForkHeight(nNormalizedNameForkHeight),
|
||||||
nOriginalClaimExpirationTime(nOriginalClaimExpirationTime),
|
nOriginalClaimExpirationTime(nOriginalClaimExpirationTime),
|
||||||
|
|
|
@ -60,6 +60,7 @@ public:
|
||||||
CClaimTrie(CClaimTrie&&) = delete;
|
CClaimTrie(CClaimTrie&&) = delete;
|
||||||
CClaimTrie(const CClaimTrie&) = delete;
|
CClaimTrie(const CClaimTrie&) = delete;
|
||||||
CClaimTrie(bool fWipe, int height,
|
CClaimTrie(bool fWipe, int height,
|
||||||
|
const std::string& dataDir,
|
||||||
int nNormalizedNameForkHeight,
|
int nNormalizedNameForkHeight,
|
||||||
int64_t nOriginalClaimExpirationTime,
|
int64_t nOriginalClaimExpirationTime,
|
||||||
int64_t nExtendedClaimExpirationTime,
|
int64_t nExtendedClaimExpirationTime,
|
||||||
|
|
|
@ -18,25 +18,25 @@ bool CTxOutPoint::IsNull() const
|
||||||
return hash.IsNull() && n == uint32_t(-1);
|
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::string CTxOutPoint::ToString() const
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "CTxOutPoint(" << hash.ToString().substr(0, 10) << ", " << n << ')';
|
ss << "CTxOutPoint(" << hash.ToString().substr(0, 10) << ", " << n << ')';
|
||||||
return ss.str();
|
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();
|
void SetNull();
|
||||||
bool IsNull() const;
|
bool IsNull() const;
|
||||||
|
|
||||||
friend bool operator<(const CTxOutPoint& a, const CTxOutPoint& b);
|
bool operator<(const CTxOutPoint& b) const;
|
||||||
friend bool operator==(const CTxOutPoint& a, const CTxOutPoint& b);
|
bool operator==(const CTxOutPoint& b) const;
|
||||||
friend bool operator!=(const CTxOutPoint& a, const CTxOutPoint& b);
|
bool operator!=(const CTxOutPoint& b) const;
|
||||||
|
|
||||||
std::string ToString() const;
|
std::string ToString() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -53,6 +53,24 @@ int CBaseBlob<BITS>::Compare(const CBaseBlob& other) const
|
||||||
return std::memcmp(begin(), other.begin(), size());
|
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>
|
template<uint32_t BITS>
|
||||||
std::string CBaseBlob<BITS>::GetHex() const
|
std::string CBaseBlob<BITS>::GetHex() const
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#define CLAIMTRIE_UINTS_H
|
#define CLAIMTRIE_UINTS_H
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/** Template base class for fixed-sized opaque blobs. */
|
/** Template base class for fixed-sized opaque blobs. */
|
||||||
|
@ -10,7 +11,7 @@ template<uint32_t BITS>
|
||||||
class CBaseBlob
|
class CBaseBlob
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
static constexpr int WIDTH = BITS / 8;
|
static constexpr uint32_t WIDTH = BITS / 8;
|
||||||
std::unique_ptr<uint8_t[]> data;
|
std::unique_ptr<uint8_t[]> data;
|
||||||
public:
|
public:
|
||||||
CBaseBlob();
|
CBaseBlob();
|
||||||
|
@ -28,9 +29,9 @@ public:
|
||||||
|
|
||||||
int Compare(const CBaseBlob& other) const;
|
int Compare(const CBaseBlob& other) const;
|
||||||
|
|
||||||
friend inline bool operator==(const CBaseBlob& a, const CBaseBlob& b) { return a.Compare(b) == 0; }
|
bool operator==(const CBaseBlob& b) const;
|
||||||
friend inline bool operator!=(const CBaseBlob& a, const CBaseBlob& b) { return a.Compare(b) != 0; }
|
bool operator!=(const CBaseBlob& b) const;
|
||||||
friend inline bool operator<(const CBaseBlob& a, const CBaseBlob& b) { return a.Compare(b) < 0; }
|
bool operator<(const CBaseBlob& b) const;
|
||||||
|
|
||||||
std::string GetHex() const;
|
std::string GetHex() const;
|
||||||
void SetHex(const char* psz);
|
void SetHex(const char* psz);
|
||||||
|
@ -44,7 +45,7 @@ public:
|
||||||
const uint8_t* begin() const;
|
const uint8_t* begin() const;
|
||||||
const uint8_t* end() const;
|
const uint8_t* end() const;
|
||||||
|
|
||||||
constexpr uint32_t size() const { return WIDTH; }
|
static constexpr uint32_t size() { return WIDTH; }
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef CBaseBlob<160> CUint160;
|
typedef CBaseBlob<160> CUint160;
|
||||||
|
|
|
@ -1459,6 +1459,7 @@ bool AppInitMain()
|
||||||
if (g_logger->Enabled() && LogAcceptCategory(BCLog::CLAIMS))
|
if (g_logger->Enabled() && LogAcceptCategory(BCLog::CLAIMS))
|
||||||
CLogPrint::global().setLogger(g_logger);
|
CLogPrint::global().setLogger(g_logger);
|
||||||
pclaimTrie = new CClaimTrie(fReindex || fReindexChainState, 0,
|
pclaimTrie = new CClaimTrie(fReindex || fReindexChainState, 0,
|
||||||
|
(GetDataDir() / "claimtrie").string(),
|
||||||
consensus.nNormalizedNameForkHeight,
|
consensus.nNormalizedNameForkHeight,
|
||||||
consensus.nOriginalClaimExpirationTime,
|
consensus.nOriginalClaimExpirationTime,
|
||||||
consensus.nExtendedClaimExpirationTime,
|
consensus.nExtendedClaimExpirationTime,
|
||||||
|
|
|
@ -70,7 +70,7 @@ BOOST_FIXTURE_TEST_SUITE(claimtriecache_tests, RegTestingSetup)
|
||||||
BOOST_AUTO_TEST_CASE(merkle_hash_single_test)
|
BOOST_AUTO_TEST_CASE(merkle_hash_single_test)
|
||||||
{
|
{
|
||||||
// check empty trie
|
// check empty trie
|
||||||
auto one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
|
auto one = CUint256S("0000000000000000000000000000000000000000000000000000000000000001");
|
||||||
CClaimTrieCacheTest cc(pclaimTrie);
|
CClaimTrieCacheTest cc(pclaimTrie);
|
||||||
BOOST_CHECK_EQUAL(one, cc.getMerkleHash());
|
BOOST_CHECK_EQUAL(one, cc.getMerkleHash());
|
||||||
|
|
||||||
|
|
|
@ -149,6 +149,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
|
||||||
pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
|
pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
|
||||||
auto& consensus = chainparams.GetConsensus();
|
auto& consensus = chainparams.GetConsensus();
|
||||||
pclaimTrie = new CClaimTrie(true, false,
|
pclaimTrie = new CClaimTrie(true, false,
|
||||||
|
(GetDataDir() / "claimtrie").string(),
|
||||||
consensus.nNormalizedNameForkHeight,
|
consensus.nNormalizedNameForkHeight,
|
||||||
consensus.nOriginalClaimExpirationTime,
|
consensus.nOriginalClaimExpirationTime,
|
||||||
consensus.nExtendedClaimExpirationTime,
|
consensus.nExtendedClaimExpirationTime,
|
||||||
|
|
|
@ -1475,12 +1475,20 @@ int ApplyTxInUndo(unsigned int index, CTxUndo& txUndo, CCoinsViewCache& view, CC
|
||||||
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
|
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.
|
/** 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. */
|
* When FAILED is returned, view is left in an indeterminate state. */
|
||||||
DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view, CClaimTrieCache& trieCache)
|
DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view, CClaimTrieCache& trieCache)
|
||||||
{
|
{
|
||||||
assert(pindex->GetBlockHash() == view.GetBestBlock());
|
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",
|
LogPrintf("%s: Indexed claim hash doesn't match current: %s vs %s\n",
|
||||||
__func__, pindex->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
|
__func__, pindex->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
|
||||||
assert(false);
|
assert(false);
|
||||||
|
@ -1555,10 +1563,9 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
|
||||||
// move best block pointer to prevout block
|
// move best block pointer to prevout block
|
||||||
view.SetBestBlock(pindex->pprev->GetBlockHash());
|
view.SetBestBlock(pindex->pprev->GetBlockHash());
|
||||||
assert(trieCache.finalizeDecrement(blockUndo.takeoverUndo));
|
assert(trieCache.finalizeDecrement(blockUndo.takeoverUndo));
|
||||||
auto merkleHash = trieCache.getMerkleHash();
|
if (!equals(trieCache.getMerkleHash(), pindex->pprev->hashClaimTrie)) {
|
||||||
if (merkleHash != pindex->pprev->hashClaimTrie) {
|
|
||||||
LogPrintf("Hash comparison failure at block %d\n", pindex->nHeight);
|
LogPrintf("Hash comparison failure at block %d\n", pindex->nHeight);
|
||||||
assert(merkleHash == pindex->pprev->hashClaimTrie);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
|
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
|
||||||
|
@ -1769,7 +1776,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
|
||||||
assert(hashPrevBlock == view.GetBestBlock());
|
assert(hashPrevBlock == view.GetBestBlock());
|
||||||
|
|
||||||
// also verify that the trie cache's current state corresponds to the previous block
|
// 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",
|
LogPrintf("%s: Previous block claim hash doesn't match current: %s vs %s\n",
|
||||||
__func__, pindex->pprev->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
|
__func__, pindex->pprev->hashClaimTrie.ToString(), trieCache.getMerkleHash().ToString());
|
||||||
assert(false);
|
assert(false);
|
||||||
|
@ -2032,8 +2039,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
|
||||||
blockundo.insertSupportUndo, blockundo.expireSupportUndo, blockundo.takeoverUndo);
|
blockundo.insertSupportUndo, blockundo.expireSupportUndo, blockundo.takeoverUndo);
|
||||||
assert(incremented);
|
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 "
|
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(),
|
"(actual=%s vs block=%s on height=%d)", trieCache.getMerkleHash().GetHex(),
|
||||||
block.hashClaimTrie.GetHex(), pindex->nHeight), REJECT_INVALID, "bad-claim-merkle-hash");
|
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();
|
bool flushed = view.Flush();
|
||||||
assert(flushed);
|
assert(flushed);
|
||||||
assert(trieCache.flush());
|
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);
|
LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI);
|
||||||
// Write the chain state to disk, if necessary.
|
// Write the chain state to disk, if necessary.
|
||||||
|
|
Loading…
Reference in a new issue