From 86ae72abb2a67e94ea5728fb97ab32e642b2bcad Mon Sep 17 00:00:00 2001 From: Brannon King Date: Thu, 24 Oct 2019 15:21:36 -0600 Subject: [PATCH] made another test pass, cleaned up cruft --- src/claimtrie.cpp | 253 +++++++++++++++--------------- src/claimtrie.h | 20 ++- src/claimtrieforks.cpp | 34 ++-- src/init.cpp | 2 +- src/rpc/claimtrie.cpp | 2 +- src/test/claimtriecache_tests.cpp | 13 +- src/test/claimtriefixture.cpp | 18 +-- src/test/claimtriefixture.h | 10 +- src/test/test_bitcoin.cpp | 4 +- 9 files changed, 182 insertions(+), 174 deletions(-) diff --git a/src/claimtrie.cpp b/src/claimtrie.cpp index ab1c3d720..04531562f 100644 --- a/src/claimtrie.cpp +++ b/src/claimtrie.cpp @@ -41,91 +41,94 @@ uint256 getValueHash(const COutPoint& outPoint, int nHeightOfLastTakeover) return result; } -CClaimTrie::CClaimTrie(bool fMemory, bool fWipe, int height, int proportionalDelayFactor, std::size_t cacheMB) - : _db(fMemory ? ":memory:" : (GetDataDir() / "claims.sqlite").string()) +static const sqlite::sqlite_config sharedConfig{ + sqlite::OpenFlags::READWRITE | sqlite::OpenFlags::CREATE, // TODO: test with this: | sqlite::OpenFlags::SHAREDCACHE, + nullptr, sqlite::Encoding::UTF8 +}; + +CClaimTrie::CClaimTrie(bool fWipe, int height, int proportionalDelayFactor, std::size_t cacheMB) + : dbPath((GetDataDir() / "claims.sqlite").string()), db(dbPath, sharedConfig), + nNextHeight(height), nProportionalDelayFactor(proportionalDelayFactor) { - nNextHeight = height; - nProportionalDelayFactor = proportionalDelayFactor; + db.define("merkle_root", [](std::vector& hashes, const std::vector& blob) { hashes.emplace_back(uint256(blob)); }, + [](const std::vector& hashes) { return ComputeMerkleRoot(hashes); }); - _db.define("merkle_root", [](std::vector& hashes, const std::vector& blob) { hashes.emplace_back(uint256(blob)); }, - [](const std::vector& hashes) { return ComputeMerkleRoot(hashes); }); + db.define("merkle_pair", [](const std::vector& blob1, const std::vector& blob2) { return Hash(blob1.begin(), blob1.end(), blob2.begin(), blob2.end()); }); + db.define("merkle", [](const std::vector& blob1) { return Hash(blob1.begin(), blob1.end()); }); - _db.define("merkle_pair", [](const std::vector& blob1, const std::vector& blob2) { return Hash(blob1.begin(), blob1.end(), blob2.begin(), blob2.end()); }); - _db.define("merkle", [](const std::vector& blob1) { return Hash(blob1.begin(), blob1.end()); }); + db << "CREATE TABLE IF NOT EXISTS nodes (name TEXT NOT NULL PRIMARY KEY, parent TEXT, hash BLOB)"; + db << "CREATE INDEX IF NOT EXISTS nodes_hash ON nodes (hash)"; + db << "CREATE INDEX IF NOT EXISTS nodes_parent ON nodes (parent)"; - _db << "CREATE TABLE IF NOT EXISTS nodes (name TEXT NOT NULL PRIMARY KEY, parent TEXT, hash BLOB)"; - _db << "CREATE INDEX nodes_hash ON nodes (hash)"; - _db << "CREATE INDEX nodes_parent ON nodes (parent)"; - - _db << "CREATE TABLE IF NOT EXISTS claims (claimID BLOB NOT NULL PRIMARY KEY, name TEXT NOT NULL, " + db << "CREATE TABLE IF NOT EXISTS claims (claimID BLOB NOT NULL PRIMARY KEY, name TEXT NOT NULL, " "nodeName TEXT NOT NULL REFERENCES nodes(name) DEFERRABLE INITIALLY DEFERRED, " "txID BLOB NOT NULL, txN INTEGER NOT NULL, blockHeight INTEGER NOT NULL, " "validHeight INTEGER NOT NULL, expirationHeight INTEGER NOT NULL, " "amount INTEGER NOT NULL, metadata BLOB);"; - _db << "CREATE INDEX claims_validHeight ON claims (validHeight)"; - _db << "CREATE INDEX claims_expirationHeight ON claims (expirationHeight)"; - _db << "CREATE INDEX claims_nodeName ON claims (nodeName)"; + db << "CREATE INDEX IF NOT EXISTS claims_validHeight ON claims (validHeight)"; + db << "CREATE INDEX IF NOT EXISTS claims_expirationHeight ON claims (expirationHeight)"; + db << "CREATE INDEX IF NOT EXISTS claims_nodeName ON claims (nodeName)"; - _db << "CREATE TABLE IF NOT EXISTS supports (txID BLOB NOT NULL, txN INTEGER NOT NULL, " + db << "CREATE TABLE IF NOT EXISTS supports (txID BLOB NOT NULL, txN INTEGER NOT NULL, " "supportedClaimID BLOB NOT NULL, name TEXT NOT NULL, nodeName TEXT NOT NULL, " "blockHeight INTEGER NOT NULL, validHeight INTEGER NOT NULL, expirationHeight INTEGER NOT NULL, " "amount INTEGER NOT NULL, metadata BLOB, PRIMARY KEY(txID, txN));"; - _db << "CREATE INDEX supports_supportedClaimID ON supports (supportedClaimID)"; - _db << "CREATE INDEX supports_validHeight ON supports (validHeight)"; - _db << "CREATE INDEX supports_expirationHeight ON supports (expirationHeight)"; - _db << "CREATE INDEX supports_nodeName ON supports (nodeName)"; + db << "CREATE INDEX IF NOT EXISTS supports_supportedClaimID ON supports (supportedClaimID)"; + db << "CREATE INDEX IF NOT EXISTS supports_validHeight ON supports (validHeight)"; + db << "CREATE INDEX IF NOT EXISTS supports_expirationHeight ON supports (expirationHeight)"; + db << "CREATE INDEX IF NOT EXISTS supports_nodeName ON supports (nodeName)"; - _db << "PRAGMA cache_size=-" + std::to_string(cacheMB * 1024); // in -KB - _db << "PRAGMA synchronous=NORMAL"; // don't disk sync after transaction commit - _db << "PRAGMA journal_mode=WAL"; // PRAGMA wal_autocheckpoint=10000; - _db << "PRAGMA case_sensitive_like=true"; + db << "PRAGMA cache_size=-" + std::to_string(cacheMB * 1024); // in -KB + db << "PRAGMA synchronous=NORMAL"; // don't disk sync after transaction commit + db << "PRAGMA journal_mode=WAL"; // PRAGMA wal_autocheckpoint=10000; + db << "PRAGMA case_sensitive_like=true"; if (fWipe) { - _db << "DELETE FROM nodes"; - _db << "DELETE FROM claims"; - _db << "DELETE FROM supports"; + db << "DELETE FROM nodes"; + db << "DELETE FROM claims"; + db << "DELETE FROM supports"; } - _db << "INSERT OR IGNORE INTO nodes(name, hash) VALUES('', ?)" << uint256(); // ensure that we always have our root node + db << "INSERT OR IGNORE INTO nodes(name, hash) VALUES('', ?)" << one; // ensure that we always have our root node } CClaimTrieCacheBase::~CClaimTrieCacheBase() { - base->_db << "rollback"; + db << "rollback"; } bool CClaimTrie::SyncToDisk() { // alternatively, switch to full sync after we are caught up on the chain - auto rc = sqlite3_wal_checkpoint_v2(_db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); + auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); return rc == SQLITE_OK; } bool CClaimTrie::empty() { int64_t count; - _db << "SELECT COUNT(*) FROM claims" >> count; + db << "SELECT COUNT(*) FROM claims" >> count; return count == 0; } bool CClaimTrieCacheBase::haveClaim(const std::string& name, const COutPoint& outPoint) const { - auto query = base->_db << "SELECT 1 FROM claims WHERE nodeName = ? AND txID = ? AND txN = ? " + auto query = db << "SELECT 1 FROM claims WHERE nodeName = ? AND txID = ? AND txN = ? " "AND validHeight < ? AND expirationHeight >= ? LIMIT 1" - << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; + << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; return query.begin() != query.end(); } bool CClaimTrieCacheBase::haveSupport(const std::string& name, const COutPoint& outPoint) const { - auto query = base->_db << "SELECT 1 FROM supports WHERE nodeName = ? AND txID = ? AND txN = ? " + auto query = db << "SELECT 1 FROM supports WHERE nodeName = ? AND txID = ? AND txN = ? " "AND validHeight < ? AND expirationHeight >= ? LIMIT 1" - << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; + << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; return query.begin() != query.end(); } supportEntryType CClaimTrieCacheBase::getSupportsForName(const std::string& name) const { // includes values that are not yet valid - auto query = base->_db << "SELECT supportedClaimID, txID, txN, blockHeight, validHeight, amount " + auto query = db << "SELECT supportedClaimID, txID, txN, blockHeight, validHeight, amount " "FROM supports WHERE nodeName = ? AND expirationHeight >= ?" << name << nNextHeight; supportEntryType ret; for (auto&& row: query) { @@ -139,9 +142,9 @@ supportEntryType CClaimTrieCacheBase::getSupportsForName(const std::string& name bool CClaimTrieCacheBase::haveClaimInQueue(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight) const { - auto query = base->_db << "SELECT validHeight FROM claims WHERE nodeName = ? AND txID = ? AND txN = ? " + auto query = db << "SELECT validHeight FROM claims WHERE nodeName = ? AND txID = ? AND txN = ? " "AND validHeight >= ? AND expirationHeight >= ? LIMIT 1" - << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; + << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; for (auto&& row: query) { row >> nValidAtHeight; return true; @@ -151,9 +154,9 @@ bool CClaimTrieCacheBase::haveClaimInQueue(const std::string& name, const COutPo bool CClaimTrieCacheBase::haveSupportInQueue(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight) const { - auto query = base->_db << "SELECT validHeight FROM supports WHERE nodeName = ? AND txID = ? AND txN = ? " + auto query = db << "SELECT validHeight FROM supports WHERE nodeName = ? AND txID = ? AND txN = ? " "AND validHeight >= ? AND expirationHeight >= ? LIMIT 1" - << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; + << name << outPoint.hash << outPoint.n << nNextHeight << nNextHeight; for (auto&& row: query) { row >> nValidAtHeight; return true; @@ -174,49 +177,48 @@ bool CClaimTrieCacheBase::deleteNodeIfPossible(const std::string& name, std::str if (name.empty()) return false; // to remove a node it must have one or less children and no claims vector_builder claimsBuilder; - base->_db << "SELECT name FROM claims WHERE name = ? AND validHeight < ? AND expirationHeight >= ? " + db << "SELECT name FROM claims WHERE name = ? AND validHeight < ? AND expirationHeight >= ? " << name << nNextHeight << nNextHeight >> claimsBuilder; claims = std::move(claimsBuilder); if (!claims.empty()) return false; // still has claims // we now know it has no claims, but we need to check its children int64_t count; std::string childName; - base->_db << "SELECT COUNT(*),MAX(name) FROM nodes WHERE parent = ?" << name >> std::tie(count, childName); + db << "SELECT COUNT(*),MAX(name) FROM nodes WHERE parent = ?" << name >> std::tie(count, childName); if (count > 1) return false; // still has multiple children // okay. it's going away - auto query = base->_db << "SELECT parent FROM nodes WHERE name = ?" << name; + auto query = db << "SELECT parent FROM nodes WHERE name = ?" << name; auto it = query.begin(); if (it == query.end()) return true; // we'll assume that whoever deleted this node previously cleaned things up correctly *it >> parent; - base->_db << "DELETE FROM nodes WHERE name = ?" << name; - auto ret = base->_db.rows_modified() > 0; + db << "DELETE FROM nodes WHERE name = ?" << name; + auto ret = db.rows_modified() > 0; if (ret && count == 1) // make the child skip us and point to its grandparent: - base->_db << "UPDATE nodes SET parent = ? WHERE name = ?" << parent << childName; + db << "UPDATE nodes SET parent = ? WHERE name = ?" << parent << childName; if (ret) - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << parent; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << parent; return ret; } void CClaimTrieCacheBase::ensureTreeStructureIsUpToDate() { - if (!dirtyNodes) return; - dirtyNodes = false; - // your children are your nodes that match your key, go at least one longer, // and have nothing in common with the other nodes in that set -- a hard query w/o parent // the plan: update all the claim hashes first vector_builder names; - base->_db << "SELECT name FROM nodes WHERE hash IS NULL ORDER BY LENGTH(name) DESC, name DESC" >> names; + db << "SELECT name FROM nodes WHERE hash IS NULL ORDER BY LENGTH(name) DESC, name DESC" >> names; + + if (names.empty()) return; // could track this with a dirty flag to avoid the above query (but it would be some maintenance) // there's an assumption that all nodes with claims are here; we do that as claims are inserted // should we do the same to remove nodes? no; we need their last takeover height if they come back //float time = 0; // assume parents are not set correctly here: - auto parentQuery = base->_db << "SELECT name FROM nodes WHERE parent IS NOT NULL AND " + auto parentQuery = db << "SELECT name FROM nodes WHERE parent IS NOT NULL AND " "name IN (WITH RECURSIVE prefix(p) AS (VALUES(?) UNION ALL " - "SELECT SUBSTR(p, 1, LENGTH(p)) FROM prefix WHERE p != '') SELECT p FROM prefix) " + "SELECT SUBSTR(p, 1, LENGTH(p)-1) FROM prefix WHERE p != '') SELECT p FROM prefix) " "ORDER BY LENGTH(name) DESC LIMIT 1"; for (auto& name: names) { @@ -240,7 +242,7 @@ void CClaimTrieCacheBase::ensureTreeStructureIsUpToDate() { // if it already exists: if (nameOrParent == name) { // TODO: we could actually set the hash here - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << name; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << name; continue; } @@ -249,7 +251,7 @@ void CClaimTrieCacheBase::ensureTreeStructureIsUpToDate() { // we know now that we need to insert it, // but we may need to insert a parent node for it first (also called a split) vector_builder siblings; - base->_db << "SELECT name FROM nodes WHERE parent = ?" << parent >> siblings; + db << "SELECT name FROM nodes WHERE parent = ?" << parent >> siblings; std::size_t splitPos = 0; for (auto& sibling: siblings) { if (sibling[parent.size()] == name[parent.size()]) { @@ -258,15 +260,15 @@ void CClaimTrieCacheBase::ensureTreeStructureIsUpToDate() { ++splitPos; auto newNodeName = name.substr(0, splitPos); // notify new node's parent: - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << parent; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << parent; // and his sibling: - base->_db << "UPDATE nodes SET parent = ? WHERE name = ?" << newNodeName << sibling; + db << "UPDATE nodes SET parent = ? WHERE name = ?" << newNodeName << sibling; if (splitPos == name.size()) { // our new node is the same as the one we wanted to insert break; } // insert the split node: - base->_db << "INSERT INTO nodes(name, parent, hash) VALUES(?, ?, NULL) " + db << "INSERT INTO nodes(name, parent, hash) VALUES(?, ?, NULL) " "ON CONFLICT(name) DO UPDATE SET parent = excluded.parent, hash = NULL" << newNodeName << parent; parent = std::move(newNodeName); @@ -274,25 +276,25 @@ void CClaimTrieCacheBase::ensureTreeStructureIsUpToDate() { } } - base->_db << "INSERT INTO nodes(name, parent, hash) VALUES(?, ?, NULL) " + db << "INSERT INTO nodes(name, parent, hash) VALUES(?, ?, NULL) " "ON CONFLICT(name) DO UPDATE SET parent = excluded.parent, hash = NULL" << name << parent; if (splitPos == 0) - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << parent; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << parent; } // now we need to percolate the nulls up the tree // parents should all be set right - base->_db << "UPDATE nodes SET hash = NULL WHERE name IN (WITH RECURSIVE prefix(p) AS (" - "SELECT parent WHERE hash IS NULL ORDER BY name DESC UNION " - "SELECT parent FROM prefix,nodes WHERE name = p AND p IS NOT NULL)"; + db << "UPDATE nodes SET hash = NULL WHERE name IN (WITH RECURSIVE prefix(p) AS " + "(SELECT parent FROM nodes WHERE hash IS NULL UNION SELECT parent FROM prefix, nodes " + "WHERE name = prefix.p AND prefix.p IS NOT NULL ORDER BY parent DESC) SELECT p FROM prefix)"; } std::size_t CClaimTrieCacheBase::getTotalNamesInTrie() const { // you could do this select from the nodes table, but you would have to ensure it is not dirty first std::size_t ret; - base->_db << "SELECT COUNT(DISTINCT nodeName) FROM claims WHERE validHeight < ? AND expirationHeight >= ?" + db << "SELECT COUNT(DISTINCT nodeName) FROM claims WHERE validHeight < ? AND expirationHeight >= ?" << nNextHeight << nNextHeight >> ret; return ret; } @@ -300,7 +302,7 @@ std::size_t CClaimTrieCacheBase::getTotalNamesInTrie() const std::size_t CClaimTrieCacheBase::getTotalClaimsInTrie() const { std::size_t ret; - base->_db << "SELECT COUNT(*) FROM claims WHERE validHeight < ? AND expirationHeight >= ?" + db << "SELECT COUNT(*) FROM claims WHERE validHeight < ? AND expirationHeight >= ?" << nNextHeight << nNextHeight >> ret; return ret; } @@ -313,17 +315,17 @@ CAmount CClaimTrieCacheBase::getTotalValueOfClaimsInTrie(bool fControllingOnly) "FROM claims c WHERE c.validHeight < ? AND s.expirationHeight >= ?"); if (fControllingOnly) throw std::runtime_error("not implemented yet"); // TODO: finish this - base->_db << query << nNextHeight << nNextHeight << nNextHeight << nNextHeight >> ret; + db << query << nNextHeight << nNextHeight << nNextHeight << nNextHeight >> ret; return ret; } bool CClaimTrieCacheBase::getInfoForName(const std::string& name, CClaimValue& claim) const { - auto query = base->_db << "SELECT c.claimID, c.txID, c.txN, c.blockHeight, c.validHeight, c.amount, " + auto query = db << "SELECT c.claimID, c.txID, c.txN, c.blockHeight, c.validHeight, c.amount, " "(SELECT TOTAL(s.amount)+c.amount FROM supports s WHERE s.supportedClaimID = c.claimID AND s.validHeight < ? AND s.expirationHeight >= ?) as effectiveAmount " "FROM claims c WHERE c.nodeName = ? AND c.validHeight < ? AND c.expirationHeight >= ? " "ORDER BY effectiveAmount DESC, c.blockHeight, c.txID, c.txN LIMIT 1" - << nNextHeight << nNextHeight << name << nNextHeight << nNextHeight; + << nNextHeight << nNextHeight << name << nNextHeight << nNextHeight; for (auto&& row: query) { row >> claim.claimId >> claim.outPoint.hash >> claim.outPoint.n >> claim.nHeight >> claim.nValidAtHeight >> claim.nAmount >> claim.nEffectiveAmount; @@ -338,9 +340,9 @@ CClaimSupportToName CClaimTrieCacheBase::getClaimsForName(const std::string& nam int nLastTakeoverHeight = 0; auto supports = getSupportsForName(name); - auto query = base->_db << "SELECT claimID, txID, txN, blockHeight, validHeight, amount " + auto query = db << "SELECT claimID, txID, txN, blockHeight, validHeight, amount " "FROM claims WHERE nodeName = ? AND expirationHeight >= ?" - << name << nNextHeight; + << name << nNextHeight; for (auto&& row: query) { CClaimValue claim; row >> claim.claimId >> claim.outPoint.hash >> claim.outPoint.n @@ -387,7 +389,7 @@ uint256 CClaimTrieCacheBase::recursiveComputeMerkleHash(const std::string& name, { std::vector vchToHash; const auto pos = name.size(); - auto query = base->_db << "SELECT name, hash FROM nodes WHERE parent = ? ORDER BY name" << name; + auto query = db << "SELECT name, hash FROM nodes WHERE parent = ? ORDER BY name" << name; for (auto&& row : query) { std::string key; std::unique_ptr hash; @@ -409,7 +411,7 @@ uint256 CClaimTrieCacheBase::recursiveComputeMerkleHash(const std::string& name, auto computedHash = vchToHash.empty() ? one : Hash(vchToHash.begin(), vchToHash.end()); if (!checkOnly) - base->_db << "UPDATE nodes SET hash = ? WHERE name = ?" << computedHash << name; + db << "UPDATE nodes SET hash = ? WHERE name = ?" << computedHash << name; return computedHash; } @@ -417,7 +419,7 @@ bool CClaimTrieCacheBase::checkConsistency() { // verify that all claims hash to the values on the nodes - auto query = base->_db << "SELECT name, hash FROM nodes"; + auto query = db << "SELECT name, hash FROM nodes"; for (auto&& row: query) { std::string name; uint256 hash; @@ -433,8 +435,8 @@ bool CClaimTrieCacheBase::flush() { getMerkleHash(); try { - base->_db << "commit"; - base->_db << "begin"; + db << "commit"; + db << "begin"; } catch (const std::exception& e) { LogPrintf("ERROR in ClaimTrieCache flush: %s\n", e.what()); @@ -456,7 +458,7 @@ bool CClaimTrieCacheBase::ValidateTipMatches(const CBlockIndex* tip) // eh, we better blow it away; it's their job to back up the folder first // well, only do it if we're empty on the sqlite side -- aka, we haven't trie to sync first std::size_t count; - base->_db << "SELECT COUNT(*) FROM nodes" >> count; + db << "SELECT COUNT(*) FROM nodes" >> count; if (!count) { auto oldDataPath = GetDataDir() / "claimtrie"; boost::system::error_code ec; @@ -472,11 +474,12 @@ bool CClaimTrieCacheBase::ValidateTipMatches(const CBlockIndex* tip) return false; } -CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrie* base) : base(base), dirtyNodes(false) +CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrie* base) + : base(base), db(base->dbPath, sharedConfig) { assert(base); nNextHeight = base->nNextHeight; - base->_db << "begin"; + db << "begin"; } int CClaimTrieCacheBase::expirationTime() const @@ -488,7 +491,7 @@ uint256 CClaimTrieCacheBase::getMerkleHash() { ensureTreeStructureIsUpToDate(); std::unique_ptr hash; - base->_db << "SELECT hash FROM nodes WHERE name = ''" >> hash; + db << "SELECT hash FROM nodes WHERE name = ''" >> hash; if (hash == nullptr || hash->IsNull()) return recursiveComputeMerkleHash("", false); return *hash; @@ -511,13 +514,13 @@ bool CClaimTrieCacheBase::addClaim(const std::string& name, const COutPoint& out auto nodeName = adjustNameForValidHeight(name, nHeight + delay); auto expires = expirationTime() + nHeight; auto validHeight = nHeight + delay; - base->_db << "INSERT INTO claims(claimID, name, nodeName, txID, txN, amount, blockHeight, validHeight, expirationHeight, metadata) " + db << "INSERT INTO claims(claimID, name, nodeName, txID, txN, amount, blockHeight, validHeight, expirationHeight, metadata) " "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(claimID) DO UPDATE SET name = excluded.name, " "nodeName = excluded.nodeName, txID = excluded.txID, txN = excluded.txN, amount = excluded.amount, " "expirationHeight = excluded.expirationHeight, metadata = excluded.metadata" << claimId << name << nodeName << outPoint.hash << outPoint.n << nAmount << nHeight << validHeight << expires << metadata; - base->_db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT(name) DO UPDATE SET hash = NULL" << nodeName; + db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT(name) DO UPDATE SET hash = NULL" << nodeName; return true; } @@ -528,37 +531,35 @@ bool CClaimTrieCacheBase::addSupport(const std::string& name, const COutPoint& o auto nodeName = adjustNameForValidHeight(name, nHeight + delay); auto expires = expirationTime() + nHeight; auto validHeight = nHeight + delay; - base->_db << "INSERT INTO supports(supportedClaimID, name, nodeName, txID, txN, amount, blockHeight, validHeight, expirationHeight, metadata) " + db << "INSERT INTO supports(supportedClaimID, name, nodeName, txID, txN, amount, blockHeight, validHeight, expirationHeight, metadata) " "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" << supportedClaimId << name << nodeName << outPoint.hash << outPoint.n << nAmount << nHeight << validHeight << expires << metadata; - base->_db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT(name) DO UPDATE SET hash = NULL" << nodeName; + db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT(name) DO UPDATE SET hash = NULL" << nodeName; return true; } bool CClaimTrieCacheBase::removeClaim(const uint160& claimId, std::string& nodeName, int& validHeight) { - auto query = base->_db << "SELECT nodeName, validHeight FROM claims WHERE claimID = ?" - << claimId; + auto query = db << "SELECT nodeName, validHeight FROM claims WHERE claimID = ?" + << claimId; auto it = query.begin(); if (it == query.end()) return false; *it >> nodeName >> validHeight; - base->_db << "DELETE FROM claims WHERE claimID = ?" << claimId; - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << nodeName; - dirtyNodes = base->_db.rows_modified() > 0; + db << "DELETE FROM claims WHERE claimID = ?" << claimId; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << nodeName; return true; } bool CClaimTrieCacheBase::removeSupport(const COutPoint& outPoint, std::string& nodeName, int& validHeight) { - auto query = base->_db << "SELECT nodeName, validHeight FROM supports WHERE txID = ? AND txN = ?" - << outPoint.hash << outPoint.n; + auto query = db << "SELECT nodeName, validHeight FROM supports WHERE txID = ? AND txN = ?" + << outPoint.hash << outPoint.n; auto it = query.begin(); if (it == query.end()) return false; *it >> nodeName >> validHeight; - base->_db << "DELETE FROM supports WHERE txID = ? AND txN = ?" << outPoint.hash << outPoint.n; - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << nodeName; - dirtyNodes = base->_db.rows_modified() > 0; + db << "DELETE FROM supports WHERE txID = ? AND txN = ?" << outPoint.hash << outPoint.n; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << nodeName; return true; } @@ -858,15 +859,15 @@ bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueR // for all dirty nodes look for new takeovers { - base->_db << "UPDATE nodes SET hash = NULL WHERE name IN " + db << "UPDATE nodes SET hash = NULL WHERE name IN " "(SELECT nodeName FROM claims WHERE validHeight = ?)" << nNextHeight; - base->_db << "UPDATE nodes SET hash = NULL WHERE name IN " + db << "UPDATE nodes SET hash = NULL WHERE name IN " "(SELECT nodeName FROM supports WHERE validHeight = ?)" << nNextHeight; } assert(expireUndo.empty()); { - auto becomingExpired = base->_db << "SELECT txID, txN, nodeName, claimID, validHeight, blockHeight, amount " + auto becomingExpired = db << "SELECT txID, txN, nodeName, claimID, validHeight, blockHeight, amount " "FROM claims WHERE expirationHeight = ?" << nNextHeight; for (auto &&row: becomingExpired) { CClaimValue value; @@ -876,12 +877,12 @@ bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueR expireUndo.emplace_back(name, value); } } - base->_db << "UPDATE nodes SET hash = NULL WHERE name IN (SELECT nodeName FROM claims WHERE expirationHeight = ?)" + db << "UPDATE nodes SET hash = NULL WHERE name IN (SELECT nodeName FROM claims WHERE expirationHeight = ?)" << nNextHeight; assert(expireSupportUndo.empty()); { - auto becomingExpired = base->_db << "SELECT txID, txN, nodeName, supportedClaimID, validHeight, blockHeight, amount " + auto becomingExpired = db << "SELECT txID, txN, nodeName, supportedClaimID, validHeight, blockHeight, amount " "FROM supports WHERE expirationHeight = ?" << nNextHeight; for (auto &&row: becomingExpired) { CSupportValue value; @@ -891,12 +892,12 @@ bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueR expireSupportUndo.emplace_back(name, value); } } - base->_db << "UPDATE nodes SET hash = NULL WHERE name IN (SELECT nodeName FROM supports WHERE expirationHeight = ?)" + db << "UPDATE nodes SET hash = NULL WHERE name IN (SELECT nodeName FROM supports WHERE expirationHeight = ?)" << nNextHeight; // takeover handling: vector_builder takeovers; - base->_db << "SELECT name FROM nodes WHERE hash IS NULL" >> takeovers; + db << "SELECT name FROM nodes WHERE hash IS NULL" >> takeovers; for (const auto& nameWithTakeover : takeovers) { if (nNextHeight >= 496856 && nNextHeight <= 653524) { @@ -921,8 +922,8 @@ void CClaimTrieCacheBase::activateAllFor(insertUndoType& insertUndo, insertUndoT const std::string& name) { // now that we know a takeover is happening, we bring everybody in: { - auto query = base->_db << "SELECT txID, txN, validHeight FROM claims WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" - << name << nNextHeight << nNextHeight; + auto query = db << "SELECT txID, txN, validHeight FROM claims WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" + << name << nNextHeight << nNextHeight; for (auto &&row: query) { uint256 hash; uint32_t n; @@ -932,13 +933,13 @@ void CClaimTrieCacheBase::activateAllFor(insertUndoType& insertUndo, insertUndoT } } // and then update them all to activate now: - base->_db << "UPDATE claims SET validHeight = ? WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" + db << "UPDATE claims SET validHeight = ? WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" << nNextHeight << name << nNextHeight << nNextHeight; // then do the same for supports: { - auto query = base->_db << "SELECT txID, txN, validHeight FROM supports WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" - << name << nNextHeight << nNextHeight; + auto query = db << "SELECT txID, txN, validHeight FROM supports WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" + << name << nNextHeight << nNextHeight; for (auto &&row: query) { uint256 hash; uint32_t n; @@ -948,7 +949,7 @@ void CClaimTrieCacheBase::activateAllFor(insertUndoType& insertUndo, insertUndoT } } // and then update them all to activate now: - base->_db << "UPDATE supports SET validHeight = ? WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" + db << "UPDATE supports SET validHeight = ? WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" << nNextHeight << name << nNextHeight << nNextHeight; } @@ -957,27 +958,27 @@ bool CClaimTrieCacheBase::decrementBlock(insertUndoType& insertUndo, claimQueueR nNextHeight--; for (auto it = expireSupportUndo.crbegin(); it != expireSupportUndo.crend(); ++it) { - base->_db << "UPDATE supports SET validHeight = ?, active = ? WHERE txID = ? AND txN = ?" - << it->second.nValidAtHeight << (it->second.nValidAtHeight <= nNextHeight) << it->second.outPoint.hash << it->second.outPoint.n; - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->first; + db << "UPDATE supports SET validHeight = ?, active = ? WHERE txID = ? AND txN = ?" + << it->second.nValidAtHeight << (it->second.nValidAtHeight <= nNextHeight) << it->second.outPoint.hash << it->second.outPoint.n; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->first; } for (auto it = expireUndo.crbegin(); it != expireUndo.crend(); ++it) { - base->_db << "UPDATE claims SET validHeight = ?, active = ? WHERE claimID = ?" - << it->second.nValidAtHeight << (it->second.nValidAtHeight <= nNextHeight) << it->second.claimId; - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->first; + db << "UPDATE claims SET validHeight = ?, active = ? WHERE claimID = ?" + << it->second.nValidAtHeight << (it->second.nValidAtHeight <= nNextHeight) << it->second.claimId; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->first; } for (auto it = insertSupportUndo.crbegin(); it != insertSupportUndo.crend(); ++it) { - base->_db << "UPDATE supports SET validHeight = ? WHERE txID = ? AND txN = ?" - << it->outPoint.hash << it->outPoint.n; - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->name; + db << "UPDATE supports SET validHeight = ? WHERE txID = ? AND txN = ?" + << it->outPoint.hash << it->outPoint.n; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->name; } for (auto it = insertUndo.crbegin(); it != insertUndo.crend(); ++it) { - base->_db << "UPDATE claims SET validHeight = ? WHERE nodeName = ? AND txID = ? AND txN = ?" - << it->name << it->outPoint.hash << it->outPoint.n; - base->_db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->name; + db << "UPDATE claims SET validHeight = ? WHERE nodeName = ? AND txID = ? AND txN = ?" + << it->name << it->outPoint.hash << it->outPoint.n; + db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->name; } return true; @@ -1188,9 +1189,9 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, const uint160 // cache the parent nodes getMerkleHash(); proof = CClaimTrieProof(); - auto nodeQuery = base->_db << "SELECT name FROM nodes WHERE " + auto nodeQuery = db << "SELECT name FROM nodes WHERE " "name IN (WITH RECURSIVE prefix(p) AS (VALUES(?) UNION ALL " - "SELECT SUBSTR(p, 1, LENGTH(p)) FROM prefix WHERE p != '') SELECT p FROM prefix) " + "SELECT SUBSTR(p, 1, LENGTH(p)-1) FROM prefix WHERE p != '') SELECT p FROM prefix) " "ORDER BY LENGTH(name)" << name; for (auto&& row: nodeQuery) { CClaimValue claim; @@ -1203,7 +1204,7 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, const uint160 const auto pos = key.size(); std::vector> children; - auto childQuery = base->_db << "SELECT name, hash FROM nodes WHERE parent = ?" << key; + auto childQuery = db << "SELECT name, hash FROM nodes WHERE parent = ?" << key; for (auto&& child : childQuery) { std::string childKey; uint256 hash; @@ -1236,9 +1237,9 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, const uint160 } bool CClaimTrieCacheBase::findNameForClaim(const std::vector& claim, CClaimValue& value, std::string& name) { - auto query = base->_db << "SELECT nodeName, claimId, txID, txN, amount, validHeight, block_height " + auto query = db << "SELECT nodeName, claimId, txID, txN, amount, validHeight, block_height " "FROM claims WHERE SUBSTR(claimID, 1, ?) = ? AND validHeight < ? AND expirationHeight >= ?" - << claim.size() + 1 << claim << nNextHeight << nNextHeight; + << claim.size() + 1 << claim << nNextHeight << nNextHeight; auto hit = false; for (auto&& row: query) { if (hit) return false; @@ -1251,8 +1252,8 @@ bool CClaimTrieCacheBase::findNameForClaim(const std::vector& cla void CClaimTrieCacheBase::getNamesInTrie(std::function callback) { - auto query = base->_db << "SELECT DISTINCT nodeName FROM claims WHERE validHeight < ? AND expirationHeight >= ?" - << nNextHeight << nNextHeight; + auto query = db << "SELECT DISTINCT nodeName FROM claims WHERE validHeight < ? AND expirationHeight >= ?" + << nNextHeight << nNextHeight; for (auto&& row: query) { std::string name; row >> name; diff --git a/src/claimtrie.h b/src/claimtrie.h index 7e0f4355c..cc68f7ab4 100644 --- a/src/claimtrie.h +++ b/src/claimtrie.h @@ -289,22 +289,27 @@ struct CClaimSupportToName const std::vector unmatchedSupports; }; +class CClaimTrieCacheBase; class CClaimTrie { + friend CClaimTrieCacheBase; + std::string dbPath; + int nNextHeight; + sqlite::database db; // keep below dbPath + public: + const int nProportionalDelayFactor; + CClaimTrie() = delete; CClaimTrie(CClaimTrie&&) = delete; CClaimTrie(const CClaimTrie&) = delete; - CClaimTrie(bool fMemory, bool fWipe, int height, int proportionalDelayFactor = 32, std::size_t cacheMB=50); + CClaimTrie(bool fWipe, int height, int proportionalDelayFactor = 32, std::size_t cacheMB=50); CClaimTrie& operator=(CClaimTrie&&) = delete; CClaimTrie& operator=(const CClaimTrie&) = delete; bool SyncToDisk(); bool empty(); // for tests - sqlite::database _db; - int nNextHeight = 0; - int nProportionalDelayFactor = 0; }; struct CClaimTrieProofNode @@ -402,10 +407,11 @@ public: protected: CClaimTrie* base; - bool dirtyNodes; + mutable sqlite::database db; + int nNextHeight; // Height of the block that is being worked on, which is + // one greater than the height of the chain's tip virtual uint256 recursiveComputeMerkleHash(const std::string& name, bool checkOnly); - supportEntryType getSupportsForName(const std::string& name) const; int getDelayForName(const std::string& name) const; @@ -415,8 +421,6 @@ protected: bool deleteNodeIfPossible(const std::string& name, std::string& parent, std::vector& claims); void ensureTreeStructureIsUpToDate(); - int nNextHeight; // Height of the block that is being worked on, which is - // one greater than the height of the chain's tip private: // for unit test diff --git a/src/claimtrieforks.cpp b/src/claimtrieforks.cpp index 99dccfcbc..1316be860 100644 --- a/src/claimtrieforks.cpp +++ b/src/claimtrieforks.cpp @@ -72,10 +72,9 @@ bool CClaimTrieCacheExpirationFork::forkForExpirationChange(bool increment) auto extension = Params().GetConsensus().nExtendedClaimExpirationTime - Params().GetConsensus().nOriginalClaimExpirationTime; if (!increment) extension = -extension; - base->_db << "UPDATE claims SET expirationHeight = expirationHeight + ? WHERE expirationHeight >= ?" << extension << nNextHeight; - base->_db << "UPDATE supports SET expirationHeight = expirationHeight + ? WHERE expirationHeight >= ?" << extension << nNextHeight; - base->_db << "UPDATE nodes SET hash = NULL"; // recompute all hashes (as there aren't that many at this point) - dirtyNodes = true; + db << "UPDATE claims SET expirationHeight = expirationHeight + ? WHERE expirationHeight >= ?" << extension << nNextHeight; + db << "UPDATE supports SET expirationHeight = expirationHeight + ? WHERE expirationHeight >= ?" << extension << nNextHeight; + db << "UPDATE nodes SET hash = NULL"; // recompute all hashes (as there aren't that many at this point) return true; } @@ -133,19 +132,18 @@ bool CClaimTrieCacheNormalizationFork::normalizeAllNamesInTrieIfNecessary(bool f // run the one-time upgrade of all names that need to change // it modifies the (cache) trie as it goes, so we need to grab everything to be modified first - base->_db.define("NORMALIZED", [this](const std::string& str) { return normalizeClaimName(str, true); }); + db.define("NORMALIZED", [this](const std::string& str) { return normalizeClaimName(str, true); }); - auto query = base->_db << "SELECT NORMALIZED(name) as nn, name, claimID FROM claims HAVING nodeName != nn"; + auto query = db << "SELECT NORMALIZED(name) as nn, name, claimID FROM claims HAVING nodeName != nn"; for(auto&& row: query) { std::string newName, oldName; uint160 claimID; row >> newName >> oldName >> claimID; if (!forward) std::swap(newName, oldName); - base->_db << "UPDATE claims SET nodeName = ? WHERE claimID = ?" << newName << claimID; - base->_db << "DELETE FROM nodes WHERE name = ?" << oldName; - base->_db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT DO UPDATE hash = NULL" << newName; + db << "UPDATE claims SET nodeName = ? WHERE claimID = ?" << newName << claimID; + db << "DELETE FROM nodes WHERE name = ?" << oldName; + db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT DO UPDATE hash = NULL" << newName; } - dirtyNodes = true; return true; } @@ -201,7 +199,7 @@ uint256 CClaimTrieCacheHashFork::recursiveComputeMerkleHash(const std::string& n if (nNextHeight < Params().GetConsensus().nAllClaimsInMerkleForkHeight) return CClaimTrieCacheNormalizationFork::recursiveComputeMerkleHash(name, checkOnly); - auto childQuery = base->_db << "SELECT name, hash FROM nodes WHERE parent = ? ORDER BY name" << name; + auto childQuery = db << "SELECT name, hash FROM nodes WHERE parent = ? ORDER BY name" << name; std::vector childHashes; for (auto&& row: childQuery) { @@ -215,7 +213,7 @@ uint256 CClaimTrieCacheHashFork::recursiveComputeMerkleHash(const std::string& n childHashes.push_back(*hash); } - auto claimQuery = base->_db << "SELECT c.txID, c.txN, c.validHeight, c.amount + " + auto claimQuery = db << "SELECT c.txID, c.txN, c.validHeight, c.amount + " "SUM(SELECT s.amount FROM supports s WHERE s.supportedClaimID = c.claimID " "AND s.validHeight < ? AND s.expirationHeight >= ?) as effectiveAmount" "FROM claims c WHERE c.nodeName = ? AND c.validHeight < ? AND c.expirationHeight >= ? " @@ -235,7 +233,7 @@ uint256 CClaimTrieCacheHashFork::recursiveComputeMerkleHash(const std::string& n auto computedHash = Hash(left.begin(), left.end(), right.begin(), right.end()); if (!checkOnly) - base->_db << "UPDATE nodes SET hash = ? WHERE name = ?" << computedHash << name; + db << "UPDATE nodes SET hash = ? WHERE name = ?" << computedHash << name; return computedHash; } @@ -308,16 +306,16 @@ bool CClaimTrieCacheHashFork::getProofForName(const std::string& name, const uin // cache the parent nodes getMerkleHash(); proof = CClaimTrieProof(); - auto nodeQuery = base->_db << "SELECT name FROM nodes WHERE " + auto nodeQuery = db << "SELECT name FROM nodes WHERE " "name IN (WITH RECURSIVE prefix(p) AS (VALUES(?) UNION ALL " - "SELECT SUBSTR(p, 1, LENGTH(p)) FROM prefix WHERE p != '') SELECT p FROM prefix) " + "SELECT SUBSTR(p, 1, LENGTH(p)-1) FROM prefix WHERE p != '') SELECT p FROM prefix) " "ORDER BY LENGTH(name)" << name; for (auto&& row: nodeQuery) { std::string key;; row >> key; std::vector childHashes; uint32_t nextCurrentIdx = 0; - auto childQuery = base->_db << "SELECT name, hash FROM nodes WHERE parent = ?" << key; + auto childQuery = db << "SELECT name, hash FROM nodes WHERE parent = ?" << key; for (auto&& child : childQuery) { std::string childKey; uint256 childHash; @@ -367,14 +365,14 @@ void CClaimTrieCacheHashFork::initializeIncrement() CClaimTrieCacheNormalizationFork::initializeIncrement(); // we could do this in the constructor, but that would not allow for multiple increments in a row (as done in unit tests) if (nNextHeight == Params().GetConsensus().nAllClaimsInMerkleForkHeight - 1) - base->_db << "UPDATE nodes SET hash = NULL"; + db << "UPDATE nodes SET hash = NULL"; } bool CClaimTrieCacheHashFork::finalizeDecrement() { auto ret = CClaimTrieCacheNormalizationFork::finalizeDecrement(); if (ret && nNextHeight == Params().GetConsensus().nAllClaimsInMerkleForkHeight - 1) - base->_db << "UPDATE nodes SET hash = NULL"; + db << "UPDATE nodes SET hash = NULL"; return ret; } diff --git a/src/init.cpp b/src/init.cpp index b13aed1ba..4bcaa5b2d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1463,7 +1463,7 @@ bool AppInitMain() int64_t trieCacheMB = gArgs.GetArg("-claimtriecache", nDefaultDbCache); trieCacheMB = std::min(trieCacheMB, nMaxDbCache); trieCacheMB = std::max(trieCacheMB, nMinDbCache); - pclaimTrie = new CClaimTrie(false, fReindex || fReindexChainState, 32, trieCacheMB); + pclaimTrie = new CClaimTrie(fReindex || fReindexChainState, 32, trieCacheMB); if (fReset) { pblocktree->WriteReindexing(true); diff --git a/src/rpc/claimtrie.cpp b/src/rpc/claimtrie.cpp index 5543dcd47..179df294c 100644 --- a/src/rpc/claimtrie.cpp +++ b/src/rpc/claimtrie.cpp @@ -653,7 +653,7 @@ UniValue getnameproof(const JSONRPCRequest& request) CCoinsViewCache coinsCache(pcoinsTip.get()); CClaimTrieCache trieCache(pclaimTrie); - int validHeight = pclaimTrie->nNextHeight - 1; + int validHeight = chainActive.Tip()->nHeight; if (request.params.size() > 1) { CBlockIndex* pblockIndex = BlockHashIndex(ParseHashV(request.params[1], T_BLOCKHASH " (optional parameter 2)")); RollBackTo(pblockIndex, coinsCache, trieCache); diff --git a/src/test/claimtriecache_tests.cpp b/src/test/claimtriecache_tests.cpp index e534e10c6..d6ab91c50 100644 --- a/src/test/claimtriecache_tests.cpp +++ b/src/test/claimtriecache_tests.cpp @@ -273,18 +273,23 @@ BOOST_AUTO_TEST_CASE(trie_stays_consistent_test) "goodness", "goodnight", "goodnatured", "goods", "go", "goody", "goo" }; - CClaimTrie trie(true, false, 1); + CClaimTrie trie(false, 1); CClaimTrieCacheTest cache(&trie); CClaimValue value; - for (auto& name: names) + for (auto& name: names) { + value.outPoint.hash = Hash(name.begin(), name.end()); + value.outPoint.n = 0; + value.claimId = ClaimIdHash(value.outPoint.hash, 0); BOOST_CHECK(cache.insertClaimIntoTrie(name, value)); + } cache.flush(); BOOST_CHECK(cache.checkConsistency()); for (auto& name: names) { - BOOST_CHECK(cache.removeClaimFromTrie(name, COutPoint())); + auto hash = Hash(name.begin(), name.end()); + BOOST_CHECK(cache.removeClaimFromTrie(name, COutPoint(hash, 0))); cache.flush(); BOOST_CHECK(cache.checkConsistency()); } @@ -299,7 +304,7 @@ BOOST_AUTO_TEST_CASE(takeover_workaround_triggers) BOOST_SCOPE_EXIT(&consensus, currentMax) { consensus.nMaxTakeoverWorkaroundHeight = currentMax; } BOOST_SCOPE_EXIT_END - CClaimTrie trie(true, false, 1); + CClaimTrie trie(false, 1); CClaimTrieCacheTest cache(&trie); insertUndoType icu, isu; claimQueueRowType ecu; supportQueueRowType esu; diff --git a/src/test/claimtriefixture.cpp b/src/test/claimtriefixture.cpp index 7eba8b582..a6d3b85fe 100644 --- a/src/test/claimtriefixture.cpp +++ b/src/test/claimtriefixture.cpp @@ -307,35 +307,35 @@ void ClaimTrieChainFixture::DecrementBlocks() DecrementBlocks(chainActive.Height() - mark); } -bool ClaimTrieChainFixture::queueEmpty() +bool ClaimTrieChainFixture::queueEmpty() const { int64_t count; - base->_db << "SELECT COUNT(*) FROM claims WHERE validHeight >= ?" << nNextHeight >> count; + db << "SELECT COUNT(*) FROM claims WHERE validHeight >= ?" << nNextHeight >> count; return count == 0; } -bool ClaimTrieChainFixture::expirationQueueEmpty() +bool ClaimTrieChainFixture::expirationQueueEmpty() const { int64_t count; - base->_db << "SELECT COUNT(*) FROM claims WHERE expirationHeight >= ?" << nNextHeight >> count; + db << "SELECT COUNT(*) FROM claims WHERE expirationHeight >= ?" << nNextHeight >> count; return count == 0; } -bool ClaimTrieChainFixture::supportEmpty() +bool ClaimTrieChainFixture::supportEmpty() const { int64_t count; - base->_db << "SELECT COUNT(*) FROM supports" >> count; + db << "SELECT COUNT(*) FROM supports" >> count; return count == 0; } -bool ClaimTrieChainFixture::supportQueueEmpty() +bool ClaimTrieChainFixture::supportQueueEmpty() const { int64_t count; - base->_db << "SELECT COUNT(*) FROM supports WHERE validHeight >= ?" << nNextHeight >> count; + db << "SELECT COUNT(*) FROM supports WHERE validHeight >= ?" << nNextHeight >> count; return count == 0; } -int ClaimTrieChainFixture::proportionalDelayFactor() +int ClaimTrieChainFixture::proportionalDelayFactor() const { return base->nProportionalDelayFactor; } diff --git a/src/test/claimtriefixture.h b/src/test/claimtriefixture.h index 1a5ccc70b..d6fd06268 100644 --- a/src/test/claimtriefixture.h +++ b/src/test/claimtriefixture.h @@ -93,15 +93,15 @@ struct ClaimTrieChainFixture: public CClaimTrieCache // decrement back to last mark void DecrementBlocks(); - bool queueEmpty(); + bool queueEmpty() const; - bool expirationQueueEmpty(); + bool expirationQueueEmpty() const; - bool supportEmpty(); + bool supportEmpty() const; - bool supportQueueEmpty(); + bool supportQueueEmpty() const; - int proportionalDelayFactor(); + int proportionalDelayFactor() const; bool getClaimById(const uint160& claimId, std::string& name, CClaimValue& value); diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index 40691b14d..0bd40f3b4 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -83,7 +83,7 @@ std::ostream& operator<<(std::ostream& os, const CSupportValue& support) } BasicTestingSetup::BasicTestingSetup(const std::string& chainName) - : m_path_root(fs::temp_directory_path() / "test_bitcoin" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30)))) + : m_path_root(fs::temp_directory_path() / "test_lbrycrd" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30)))) { // for debugging: if (boost::unit_test::runtime_config::get(boost::unit_test::runtime_config::btrt_log_level) @@ -138,7 +138,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha pblocktree.reset(new CBlockTreeDB(1 << 20, true)); pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true)); pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get())); - pclaimTrie = new CClaimTrie(true, false, 1); + pclaimTrie = new CClaimTrie(true, 1); if (!LoadGenesisBlock(chainparams)) { throw std::runtime_error("LoadGenesisBlock failed."); }