Don't call lsn_reset at periodic time #360

Closed
bvbfan wants to merge 75 commits from lsn_reset into master
11 changed files with 66 additions and 37 deletions
Showing only changes of commit e386039392 - Show all commits

View file

@ -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

View file

@ -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),

View file

@ -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,

View file

@ -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);
}

View file

@ -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;
};

View file

@ -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
{

View file

@ -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;

View file

@ -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,

View file

@ -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());

View file

@ -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,

View file

@ -1475,12 +1475,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);
@ -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.