diff --git a/src/claimscriptop.cpp b/src/claimscriptop.cpp index 37d94a5ad..3109db36d 100644 --- a/src/claimscriptop.cpp +++ b/src/claimscriptop.cpp @@ -14,25 +14,25 @@ CClaimScriptAddOp::CClaimScriptAddOp(const COutPoint& point, CAmount nValue, int bool CClaimScriptAddOp::claimName(CClaimTrieCache& trieCache, const std::string& name, const std::vector& metadata) { - return addClaim(trieCache, name, ClaimIdHash(point.hash, point.n), metadata); + return addClaim(trieCache, name, ClaimIdHash(point.hash, point.n), -1, metadata); } bool CClaimScriptAddOp::updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, const std::vector& metadata) { - return addClaim(trieCache, name, claimId, metadata); + return addClaim(trieCache, name, claimId, -1, metadata); } bool CClaimScriptAddOp::addClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, - const std::vector& metadata) + int takeoverHeight, const std::vector& metadata) { - return trieCache.addClaim(name, point, claimId, nValue, nHeight, metadata); + return trieCache.addClaim(name, point, claimId, nValue, nHeight, takeoverHeight, metadata); } bool CClaimScriptAddOp::supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, const std::vector& metadata) { - return trieCache.addSupport(name, point, nValue, claimId, nHeight, metadata); + return trieCache.addSupport(name, point, nValue, claimId, nHeight, -1, metadata); } CClaimScriptUndoAddOp::CClaimScriptUndoAddOp(const COutPoint& point, int nHeight) : point(point), nHeight(nHeight) @@ -43,23 +43,22 @@ bool CClaimScriptUndoAddOp::claimName(CClaimTrieCache& trieCache, const std::str const std::vector& metadata) { auto claimId = ClaimIdHash(point.hash, point.n); - LogPrint(BCLog::CLAIMS, "--- [%lu]: OP_CLAIM_NAME \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + LogPrint(BCLog::CLAIMS, "--- [%lu]: Undoing OP_CLAIM_NAME \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); return undoAddClaim(trieCache, name, claimId); } bool CClaimScriptUndoAddOp::updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, const std::vector& metadata) { - LogPrint(BCLog::CLAIMS, "--- [%lu]: OP_UPDATE_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + LogPrint(BCLog::CLAIMS, "--- [%lu]: Undoing OP_UPDATE_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); return undoAddClaim(trieCache, name, claimId); } bool CClaimScriptUndoAddOp::undoAddClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) { - LogPrint(BCLog::CLAIMS, "%s: (txid: %s, nOut: %d) Removing %s, claimId: %s, from the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); std::string nodeName; int validHeight; - bool res = trieCache.removeClaim(claimId, nodeName, validHeight); + bool res = trieCache.removeClaim(claimId, point, nodeName, validHeight); if (!res) LogPrint(BCLog::CLAIMS, "%s: Removing claim fails\n", __func__); return res; @@ -69,11 +68,8 @@ bool CClaimScriptUndoAddOp::supportClaim(CClaimTrieCache& trieCache, const std:: const std::vector& metadata) { if (LogAcceptCategory(BCLog::CLAIMS)) { - LogPrintf("--- [%lu]: OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, + LogPrintf("--- [%lu]: Undoing OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); - LogPrintf( - "%s: (txid: %s, nOut: %d) Removing support for %s, claimId: %s, from the claim trie due to block disconnect\n", - __func__, point.hash.ToString(), point.n, name, claimId.ToString()); } std::string nodeName; int validHeight; @@ -92,22 +88,21 @@ bool CClaimScriptSpendOp::claimName(CClaimTrieCache& trieCache, const std::strin const std::vector& metadata) { auto claimId = ClaimIdHash(point.hash, point.n); - LogPrint(BCLog::CLAIMS, "+++ [%lu]: OP_CLAIM_NAME \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + LogPrint(BCLog::CLAIMS, "+++ [%lu]: Spending OP_CLAIM_NAME \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); return spendClaim(trieCache, name, claimId); } bool CClaimScriptSpendOp::updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, const std::vector& metadata) { - LogPrint(BCLog::CLAIMS, "+++ [%lu]: OP_UPDATE_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + LogPrint(BCLog::CLAIMS, "+++ [%lu]: Spending OP_UPDATE_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); return spendClaim(trieCache, name, claimId); } bool CClaimScriptSpendOp::spendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) { - LogPrint(BCLog::CLAIMS, "%s: (txid: %s, nOut: %d) Spending %s, claimId: %s, from the claim trie\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); std::string nodeName; - bool res = trieCache.removeClaim(claimId, nodeName, nValidHeight); + bool res = trieCache.removeClaim(claimId, point, nodeName, nValidHeight); if (!res) LogPrint(BCLog::CLAIMS, "%s: Removing fails\n", __func__); return res; @@ -117,10 +112,8 @@ bool CClaimScriptSpendOp::supportClaim(CClaimTrieCache& trieCache, const std::st const std::vector& metadata) { if (LogAcceptCategory(BCLog::CLAIMS)) { - LogPrintf("+++ [%lu]: OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, + LogPrintf("+++ [%lu]: Spending OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); - LogPrintf("%s: (txid: %s, nOut: %d) Restoring support for %s, claimId: %s, to the claim trie\n", __func__, - point.hash.ToString(), point.n, name, claimId.ToString()); } std::string nodeName; bool res = trieCache.removeSupport(point, nodeName, nValidHeight); @@ -149,15 +142,15 @@ bool CClaimScriptUndoSpendOp::updateClaim(CClaimTrieCache& trieCache, const std: bool CClaimScriptUndoSpendOp::undoSpendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, const std::vector& metadata) { - LogPrint(BCLog::CLAIMS, "%s: (txid: %s, nOut: %d) Restoring %s, claimId: %s, to the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); - return trieCache.addClaim(name, point, claimId, nValue, nHeight, metadata); + LogPrint(BCLog::CLAIMS, "%s: (txid: %s, nOut: %d) Undoing spend of %s, claimId: %s, to the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); + return trieCache.addClaim(name, point, claimId, nValue, nHeight, nValidHeight, metadata); } bool CClaimScriptUndoSpendOp::supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, const std::vector& metadata) { LogPrint(BCLog::CLAIMS, "%s: (txid: %s, nOut: %d) Restoring support for %s, claimId: %s, to the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); - return trieCache.addSupport(name, point, nValue, claimId, nHeight, metadata); + return trieCache.addSupport(name, point, nValue, claimId, nHeight, nValidHeight, metadata); } static std::string vchToString(const std::vector& name) @@ -195,12 +188,12 @@ void UpdateCache(const CTransaction& tx, CClaimTrieCache& trieCache, const CCoin bool spendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override { if (CClaimScriptSpendOp::spendClaim(trieCache, name, claimId)) { - callback(name, claimId); + callback(name, claimId, nValidHeight); return true; } return false; } - std::function callback; + std::function callback; }; spentClaimsType spentClaims; @@ -223,8 +216,8 @@ void UpdateCache(const CTransaction& tx, CClaimTrieCache& trieCache, const CCoin int nValidAtHeight; CSpendClaimHistory spendClaim(COutPoint(txin.prevout.hash, txin.prevout.n), scriptHeight, nValidAtHeight); - spendClaim.callback = [&spentClaims](const std::string& name, const uint160& claimId) { - spentClaims.emplace_back(name, claimId); + spendClaim.callback = [&spentClaims](const std::string& name, const uint160& claimId, const int takeoverHeight) { + spentClaims.emplace_back(name, claimId, takeoverHeight); }; if (ProcessClaim(spendClaim, trieCache, scriptPubKey) && callbacks.claimUndoHeights) callbacks.claimUndoHeights(j, nValidAtHeight); @@ -236,13 +229,14 @@ void UpdateCache(const CTransaction& tx, CClaimTrieCache& trieCache, const CCoin using CClaimScriptAddOp::CClaimScriptAddOp; bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, - const std::vector& metadata) override + const std::vector& metadata) override { - if (callback(name, claimId)) - return CClaimScriptAddOp::updateClaim(trieCache, name, claimId, metadata); + int takeoverHeight = -1; + if (callback(name, claimId, takeoverHeight)) + return CClaimScriptAddOp::addClaim(trieCache, name, claimId, takeoverHeight, metadata); return false; } - std::function callback; + std::function callback; }; for (std::size_t j = 0; j < tx.vout.size(); j++) { @@ -252,9 +246,10 @@ void UpdateCache(const CTransaction& tx, CClaimTrieCache& trieCache, const CCoin continue; CAddSpendClaim addClaim(COutPoint(tx.GetHash(), j), txout.nValue, nHeight); - addClaim.callback = [&trieCache, &spentClaims](const std::string& name, const uint160& claimId) -> bool { + addClaim.callback = [&trieCache, &spentClaims](const std::string& name, const uint160& claimId, int& takeoverHeight) -> bool { for (auto itSpent = spentClaims.begin(); itSpent != spentClaims.end(); ++itSpent) { - if (itSpent->second == claimId && trieCache.normalizeClaimName(name) == trieCache.normalizeClaimName(itSpent->first)) { + if (std::get<1>(*itSpent) == claimId && trieCache.normalizeClaimName(name) == trieCache.normalizeClaimName(std::get<0>(*itSpent))) { + takeoverHeight = std::get<2>(*itSpent); spentClaims.erase(itSpent); return true; } diff --git a/src/claimscriptop.h b/src/claimscriptop.h index 9289a2645..583db10bf 100644 --- a/src/claimscriptop.h +++ b/src/claimscriptop.h @@ -88,7 +88,7 @@ protected: * @param[in] claimId id of the claim */ virtual bool addClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId, - const std::vector& metadata); + int takeoverHeight, const std::vector& metadata); const COutPoint point; const CAmount nValue; const int nHeight; @@ -238,7 +238,7 @@ protected: */ bool ProcessClaim(CClaimScriptOp& claimOp, CClaimTrieCache& trieCache, const CScript& scriptPubKey); -typedef std::pair spentClaimType; +typedef std::tuple spentClaimType; typedef std::vector spentClaimsType; diff --git a/src/claimtrie.cpp b/src/claimtrie.cpp index e1118d007..80ac2c0d3 100644 --- a/src/claimtrie.cpp +++ b/src/claimtrie.cpp @@ -523,51 +523,61 @@ bool CClaimTrieCacheBase::getLastTakeoverForName(const std::string& name, uint16 } bool CClaimTrieCacheBase::addClaim(const std::string& name, const COutPoint& outPoint, const uint160& claimId, - CAmount nAmount, int nHeight, const std::vector& metadata) + CAmount nAmount, int nHeight, int nValidHeight, const std::vector& metadata) { if (!transacting) { transacting = true; db << "begin"; } - auto delay = getDelayForName(name, claimId); - auto nodeName = adjustNameForValidHeight(name, nHeight + delay); + // in the update scenario the previous one should be removed already + + if (nValidHeight <= 0) { + auto delay = getDelayForName(name, claimId); + nValidHeight = nHeight + delay; + } + auto nodeName = adjustNameForValidHeight(name, nValidHeight); auto expires = expirationTime() + nHeight; - auto validHeight = nHeight + delay; 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; + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" << claimId << name << nodeName + << outPoint.hash << outPoint.n << nAmount << nHeight << nValidHeight << expires << metadata; db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT(name) DO UPDATE SET hash = NULL" << nodeName; return true; } bool CClaimTrieCacheBase::addSupport(const std::string& name, const COutPoint& outPoint, CAmount nAmount, - const uint160& supportedClaimId, int nHeight, const std::vector& metadata) + const uint160& supportedClaimId, int nHeight, int nValidHeight, const std::vector& metadata) { if (!transacting) { transacting = true; db << "begin"; } - auto delay = getDelayForName(name, supportedClaimId); - auto nodeName = adjustNameForValidHeight(name, nHeight + delay); + if (nValidHeight <= 0) { + auto delay = getDelayForName(name, supportedClaimId); + nValidHeight = nHeight + delay; + } + auto nodeName = adjustNameForValidHeight(name, nValidHeight); auto expires = expirationTime() + nHeight; - auto validHeight = nHeight + delay; 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; + << outPoint.hash << outPoint.n << nAmount << nHeight << nValidHeight << expires << metadata; 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) +bool CClaimTrieCacheBase::removeClaim(const uint160& claimId, const COutPoint& outPoint, std::string& nodeName, int& validHeight) { if (!transacting) { transacting = true; db << "begin"; } - auto query = db << "SELECT nodeName, validHeight FROM claims WHERE claimID = ?" - << claimId; + // this gets tricky in that we may be removing an update + // when going forward we spend a claim (aka, call removeClaim) before updating it (aka, call addClaim) + // when going backwards we first remove the update by calling removeClaim + // we then undo the spend of the previous one by calling addClaim with the original data + // in order to maintain the proper takeover height the udpater will need to use our height returned here + + auto query = db << "SELECT nodeName, validHeight FROM claims WHERE claimID = ? and txID = ? and txN = ?" + << claimId << outPoint.hash << outPoint.n; auto it = query.begin(); if (it == query.end()) return false; *it >> nodeName >> validHeight; - db << "DELETE FROM claims WHERE claimID = ?" << claimId; + db << "DELETE FROM claims WHERE claimID = ? AND txID = ? and txN = ?" << claimId << outPoint.hash << outPoint.n; db << "UPDATE nodes SET hash = NULL WHERE name = ?" << nodeName; return true; } @@ -882,12 +892,10 @@ bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueR // for all dirty nodes look for new takeovers if (!transacting) { transacting = true; db << "begin"; } - { - db << "UPDATE nodes SET hash = NULL WHERE name IN " - "(SELECT nodeName FROM claims WHERE validHeight = ?)" << nNextHeight; - db << "UPDATE nodes SET hash = NULL WHERE name IN " - "(SELECT nodeName FROM supports WHERE validHeight = ?)" << nNextHeight; - } + db << "UPDATE nodes SET hash = NULL WHERE name IN " + "(SELECT nodeName FROM claims WHERE validHeight = ?)" << nNextHeight; + db << "UPDATE nodes SET hash = NULL WHERE name IN " + "(SELECT nodeName FROM supports WHERE validHeight = ?)" << nNextHeight; assert(expireUndo.empty()); { @@ -934,7 +942,7 @@ bool CClaimTrieCacheBase::incrementBlock(insertUndoType& insertUndo, claimQueueR // if somebody activates on this block and they are the new best, then everybody activates on this block CClaimValue value; - if (getInfoForName(nameWithTakeover, value) && value.nValidAtHeight == nNextHeight - 1) + if (!getInfoForName(nameWithTakeover, value) || value.nValidAtHeight == nNextHeight) activateAllFor(insertUndo, insertSupportUndo, nameWithTakeover); } @@ -946,7 +954,7 @@ void CClaimTrieCacheBase::activateAllFor(insertUndoType& insertUndo, insertUndoT const std::string& name) { // now that we know a takeover is happening, we bring everybody in: { - auto query = db << "SELECT txID, txN, validHeight FROM claims WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" + auto query = db << "SELECT txID, txN, validHeight FROM claims WHERE nodeName = ? AND validHeight > ? AND expirationHeight > ?" << name << nNextHeight << nNextHeight; for (auto &&row: query) { uint256 hash; @@ -957,12 +965,12 @@ void CClaimTrieCacheBase::activateAllFor(insertUndoType& insertUndo, insertUndoT } } // and then update them all to activate now: - 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 = db << "SELECT txID, txN, validHeight FROM supports WHERE nodeName = ? AND validHeight > ? AND expirationHeight >= ?" + auto query = db << "SELECT txID, txN, validHeight FROM supports WHERE nodeName = ? AND validHeight > ? AND expirationHeight > ?" << name << nNextHeight << nNextHeight; for (auto &&row: query) { uint256 hash; @@ -973,7 +981,7 @@ void CClaimTrieCacheBase::activateAllFor(insertUndoType& insertUndo, insertUndoT } } // and then update them all to activate now: - 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; } @@ -984,29 +992,36 @@ bool CClaimTrieCacheBase::decrementBlock(insertUndoType& insertUndo, claimQueueR nNextHeight--; for (auto it = expireSupportUndo.crbegin(); it != expireSupportUndo.crend(); ++it) { - 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 supports SET validHeight = ? WHERE txID = ? AND txN = ?" + << it->second.nValidAtHeight << 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) { - db << "UPDATE claims SET validHeight = ?, active = ? WHERE claimID = ?" - << it->second.nValidAtHeight << (it->second.nValidAtHeight <= nNextHeight) << it->second.claimId; + db << "UPDATE claims SET validHeight = ? WHERE claimID = ?" + << it->second.nValidAtHeight << it->second.claimId; db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->first; } for (auto it = insertSupportUndo.crbegin(); it != insertSupportUndo.crend(); ++it) { + LogPrint(BCLog::CLAIMS, "Resetting support valid height to %d for %s\n", it->nValidHeight, it->name); db << "UPDATE supports SET validHeight = ? WHERE txID = ? AND txN = ?" - << it->outPoint.hash << it->outPoint.n; + << it->nValidHeight << 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) { + LogPrint(BCLog::CLAIMS, "Resetting valid height to %d for %s\n", it->nValidHeight, it->name); db << "UPDATE claims SET validHeight = ? WHERE nodeName = ? AND txID = ? AND txN = ?" - << it->name << it->outPoint.hash << it->outPoint.n; + << it->nValidHeight << it->name << it->outPoint.hash << it->outPoint.n; db << "UPDATE nodes SET hash = NULL WHERE name = ?" << it->name; } + db << "UPDATE nodes SET hash = NULL WHERE name IN " + "(SELECT nodeName FROM claims WHERE validHeight = ?)" << nNextHeight; + db << "UPDATE nodes SET hash = NULL WHERE name IN " + "(SELECT nodeName FROM supports WHERE validHeight = ?)" << nNextHeight; + return true; } @@ -1263,9 +1278,9 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, const uint160 } bool CClaimTrieCacheBase::findNameForClaim(const std::vector& claim, CClaimValue& value, std::string& name) { - auto query = db << "SELECT nodeName, claimId, txID, txN, amount, validHeight, block_height " + auto query = db << "SELECT nodeName, claimId, txID, txN, amount, validHeight, blockHeight " "FROM claims WHERE SUBSTR(claimID, 1, ?) = ? AND validHeight < ? AND expirationHeight >= ?" - << claim.size() + 1 << claim << nNextHeight << nNextHeight; + << claim.size() << claim << nNextHeight << nNextHeight; auto hit = false; for (auto&& row: query) { if (hit) return false; @@ -1273,7 +1288,7 @@ bool CClaimTrieCacheBase::findNameForClaim(const std::vector& cla >> value.nAmount >> value.nValidAtHeight >> value.nHeight; hit = true; } - return true; + return hit; } void CClaimTrieCacheBase::getNamesInTrie(std::function callback) diff --git a/src/claimtrie.h b/src/claimtrie.h index 8bd28937f..7540d2b27 100644 --- a/src/claimtrie.h +++ b/src/claimtrie.h @@ -179,12 +179,12 @@ struct CNameOutPointHeightType { std::string name; COutPoint outPoint; - int nHeight = 0; + int nValidHeight = 0; CNameOutPointHeightType() = default; - CNameOutPointHeightType(std::string name, const COutPoint& outPoint, int nHeight) - : name(std::move(name)), outPoint(outPoint), nHeight(nHeight) + CNameOutPointHeightType(std::string name, const COutPoint& outPoint, int nValidHeight) + : name(std::move(name)), outPoint(outPoint), nValidHeight(nValidHeight) { } @@ -195,7 +195,7 @@ struct CNameOutPointHeightType { READWRITE(name); READWRITE(outPoint); - READWRITE(nHeight); + READWRITE(nValidHeight); } }; @@ -370,12 +370,12 @@ public: bool haveSupportInQueue(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight) const; bool addClaim(const std::string& name, const COutPoint& outPoint, const uint160& claimId, CAmount nAmount, - int nHeight, const std::vector& metadata); + int nHeight, int nValidHeight, const std::vector& metadata); bool addSupport(const std::string& name, const COutPoint& outPoint, CAmount nAmount, - const uint160& supportedClaimId, int nHeight, const std::vector& metadata); + const uint160& supportedClaimId, int nHeight, int nValidHeight, const std::vector& metadata); bool removeSupport(const COutPoint& outPoint, std::string& nodeName, int& validHeight); - bool removeClaim(const uint160& claimId, std::string& nodeName, int& validHeight); + bool removeClaim(const uint160& claimId, const COutPoint& outPoint, std::string& nodeName, int& validHeight); virtual bool incrementBlock(insertUndoType& insertUndo, claimQueueRowType& expireUndo, diff --git a/src/claimtrieforks.cpp b/src/claimtrieforks.cpp index 04a6c945e..0baccb86a 100644 --- a/src/claimtrieforks.cpp +++ b/src/claimtrieforks.cpp @@ -138,7 +138,7 @@ bool CClaimTrieCacheNormalizationFork::normalizeAllNamesInTrieIfNecessary(bool f db.define("NORMALIZED", [this](const std::string& str) { return normalizeClaimName(str, true); }); - auto query = 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 WHERE nodeName != nn"; for(auto&& row: query) { std::string newName, oldName; uint160 claimID; @@ -146,7 +146,7 @@ bool CClaimTrieCacheNormalizationFork::normalizeAllNamesInTrieIfNecessary(bool f if (!forward) std::swap(newName, oldName); 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; + db << "INSERT INTO nodes(name) VALUES(?) ON CONFLICT(name) DO UPDATE SET hash = NULL" << newName; } return true; diff --git a/src/test/claimtriebranching_tests.cpp b/src/test/claimtriebranching_tests.cpp index ce287c41c..7bef002e4 100644 --- a/src/test/claimtriebranching_tests.cpp +++ b/src/test/claimtriebranching_tests.cpp @@ -539,7 +539,7 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test) CMutableTransaction tx2 = fixture.MakeClaim(fixture.GetCoinbase(), name, "two", 2); uint160 claimId2 = ClaimIdHash(tx2.GetHash(), 0); - fixture.IncrementBlocks(1); + fixture.IncrementBlocks(5); BOOST_CHECK(fixture.getClaimById(claimId2, claimName, claimValue)); BOOST_CHECK_EQUAL(claimName, name); @@ -547,7 +547,7 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test) CMutableTransaction u1 = fixture.MakeUpdate(tx1, name, "updated one", claimId, 1); - fixture.IncrementBlocks(1); + fixture.IncrementBlocks(2); BOOST_CHECK(fixture.getClaimById(claimId, claimName, claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId); @@ -558,7 +558,7 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test) fixture.IncrementBlocks(1); BOOST_CHECK(!fixture.getClaimById(claimId, claimName, claimValue)); - fixture.DecrementBlocks(3); + fixture.DecrementBlocks(8); CClaimValue claimValue2; claimName = ""; diff --git a/src/test/claimtriecache_tests.cpp b/src/test/claimtriecache_tests.cpp index b31f928c7..57cb4d6ec 100644 --- a/src/test/claimtriecache_tests.cpp +++ b/src/test/claimtriecache_tests.cpp @@ -26,7 +26,7 @@ public: if (c.IsNull()) c = ClaimIdHash(p.hash, p.n); - return addClaim(key, p, c, value.nAmount, value.nHeight, {}); + return addClaim(key, p, c, value.nAmount, value.nHeight, -1, {}); } bool removeClaimFromTrie(const std::string& key, const COutPoint& outPoint) { @@ -37,7 +37,7 @@ public: if (p.hash.IsNull()) p.hash = Hash(key.begin(), key.end()); - auto ret = removeClaim(ClaimIdHash(p.hash, p.n), nodeName, validHeight); + auto ret = removeClaim(ClaimIdHash(p.hash, p.n), p, nodeName, validHeight); assert(!ret || nodeName == key); return ret; } @@ -47,7 +47,7 @@ public: if (p.hash.IsNull()) p.hash = Hash(key.begin(), key.end()); - return addSupport(key, p, value.nAmount, value.supportedClaimId, value.nHeight, {}); + return addSupport(key, p, value.nAmount, value.supportedClaimId, value.nHeight, -1, {}); } bool removeSupportFromMap(const std::string& key, const COutPoint& outPoint) { @@ -117,7 +117,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test) BOOST_CHECK_EQUAL(ntState.getTotalClaimsInTrie(), 2U); BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash1); - ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 1, 0)); + ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 0, 0)); BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash1); ntState.insertClaimIntoTrie(std::string("tes"), CClaimValue(tx4OutPoint, {}, 50, 0, 0)); BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2); @@ -142,7 +142,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test) } { CClaimTrieCacheTest ntState2(pclaimTrie); - ntState2.insertClaimIntoTrie(std::string("abab"), CClaimValue(tx6OutPoint, {}, 50, 0, 0)); + ntState2.insertClaimIntoTrie(std::string("abab"), CClaimValue(tx6OutPoint, {}, 50, 0, 200)); ntState2.removeClaimFromTrie(std::string("test"), tx1OutPoint); BOOST_CHECK_EQUAL(ntState2.getMerkleHash(), hash3); diff --git a/src/test/claimtrienormalization_tests.cpp b/src/test/claimtrienormalization_tests.cpp index 1a93570be..533b56429 100644 --- a/src/test/claimtrienormalization_tests.cpp +++ b/src/test/claimtrienormalization_tests.cpp @@ -217,11 +217,11 @@ BOOST_AUTO_TEST_CASE(claimtriecache_normalization) BOOST_CHECK(ReadBlockFromDisk(block, pindex, Params().GetConsensus())); BOOST_CHECK(g_chainstate.DisconnectBlock(block, pindex, coins, trieCache) == DisconnectResult::DISCONNECT_OK); BOOST_CHECK(!trieCache.shouldNormalize()); - BOOST_CHECK(!trieCache.removeClaim(ClaimIdHash(tx2.GetHash(), 0), name_normd, amelieValidHeight)); - BOOST_CHECK(trieCache.removeClaim(ClaimIdHash(tx2.GetHash(), 0), name_upper, amelieValidHeight)); + BOOST_CHECK(!trieCache.removeClaim(ClaimIdHash(tx2.GetHash(), 0), COutPoint(tx2.GetHash(), 0), name_normd, amelieValidHeight)); + BOOST_CHECK(trieCache.removeClaim(ClaimIdHash(tx2.GetHash(), 0), COutPoint(tx2.GetHash(), 0), name_upper, amelieValidHeight)); BOOST_CHECK(trieCache.getInfoForName(name, nval1)); - BOOST_CHECK(trieCache.addClaim(name, COutPoint(tx1.GetHash(), 0), ClaimIdHash(tx1.GetHash(), 0), CAmount(2), currentHeight + 1, {})); + BOOST_CHECK(trieCache.addClaim(name, COutPoint(tx1.GetHash(), 0), ClaimIdHash(tx1.GetHash(), 0), CAmount(2), currentHeight + 1, -1, {})); BOOST_CHECK(trieCache.getInfoForName(name, nval1)); insertUndoType insertUndo; claimQueueRowType expireUndo; @@ -304,11 +304,11 @@ BOOST_AUTO_TEST_CASE(normalization_removal_test) CClaimTrieCache cache(pclaimTrie); int height = chainActive.Height() + 1; - cache.addClaim("AB", COutPoint(tx1.GetHash(), 0), ClaimIdHash(tx1.GetHash(), 0), 1, height, {}); - cache.addClaim("Ab", COutPoint(tx2.GetHash(), 0), ClaimIdHash(tx2.GetHash(), 0), 2, height, {}); - cache.addClaim("aB", COutPoint(tx3.GetHash(), 0), ClaimIdHash(tx3.GetHash(), 0), 3, height, {}); - cache.addSupport("AB", COutPoint(sx1.GetHash(), 0), 1, ClaimIdHash(tx1.GetHash(), 0), height, {}); - cache.addSupport("Ab", COutPoint(sx2.GetHash(), 0), 1, ClaimIdHash(tx2.GetHash(), 0), height, {}); + cache.addClaim("AB", COutPoint(tx1.GetHash(), 0), ClaimIdHash(tx1.GetHash(), 0), 1, height, -1, {}); + cache.addClaim("Ab", COutPoint(tx2.GetHash(), 0), ClaimIdHash(tx2.GetHash(), 0), 2, height, -1, {}); + cache.addClaim("aB", COutPoint(tx3.GetHash(), 0), ClaimIdHash(tx3.GetHash(), 0), 3, height, -1, {}); + cache.addSupport("AB", COutPoint(sx1.GetHash(), 0), 1, ClaimIdHash(tx1.GetHash(), 0), height, -1, {}); + cache.addSupport("Ab", COutPoint(sx2.GetHash(), 0), 1, ClaimIdHash(tx2.GetHash(), 0), height, -1, {}); insertUndoType insertUndo; claimQueueRowType expireUndo; insertUndoType insertSupportUndo; @@ -323,9 +323,9 @@ BOOST_AUTO_TEST_CASE(normalization_removal_test) std::string unused; BOOST_CHECK(cache.removeSupport(COutPoint(sx1.GetHash(), 0), unused, height)); BOOST_CHECK(cache.removeSupport(COutPoint(sx2.GetHash(), 0), unused, height)); - BOOST_CHECK(cache.removeClaim(ClaimIdHash(tx1.GetHash(), 0), unused, height)); - BOOST_CHECK(cache.removeClaim(ClaimIdHash(tx2.GetHash(), 0), unused, height)); - BOOST_CHECK(cache.removeClaim(ClaimIdHash(tx3.GetHash(), 0), unused, height)); + BOOST_CHECK(cache.removeClaim(ClaimIdHash(tx1.GetHash(), 0), COutPoint(tx1.GetHash(), 0), unused, height)); + BOOST_CHECK(cache.removeClaim(ClaimIdHash(tx2.GetHash(), 0), COutPoint(tx2.GetHash(), 0), unused, height)); + BOOST_CHECK(cache.removeClaim(ClaimIdHash(tx3.GetHash(), 0), COutPoint(tx3.GetHash(), 0), unused, height)); BOOST_CHECK(cache.getClaimsForName("ab").claimsNsupports.size() == 0U); } diff --git a/src/validation.cpp b/src/validation.cpp index 3a0a53602..63232bc05 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1461,13 +1461,8 @@ int ApplyTxInUndo(unsigned int index, CTxUndo& txUndo, CCoinsViewCache& view, CC // restore claim if applicable if (undo.fIsClaim && !undo.txout.scriptPubKey.empty()) { auto nValidHeight = undo.nClaimValidHeight; - if (nValidHeight > 0 && nValidHeight >= undo.nHeight) { - CClaimScriptUndoSpendOp undoSpend(COutPoint(out.hash, out.n), undo.txout.nValue, undo.nHeight, nValidHeight); - ProcessClaim(undoSpend, trieCache, undo.txout.scriptPubKey); - } else { - LogPrintf("%s: (txid: %s, nOut: %d) Not restoring claim/support to the claim trie because it expired before it was spent\n", __func__, out.hash.ToString(), out.n); - LogPrintf("%s: nValidHeight = %d, undo.nHeight = %d, nCurrentHeight = %d\n", __func__, nValidHeight, undo.nHeight, chainActive.Height()); - } + CClaimScriptUndoSpendOp undoSpend(COutPoint(out.hash, out.n), undo.txout.nValue, undo.nHeight, nValidHeight); + ProcessClaim(undoSpend, trieCache, undo.txout.scriptPubKey); } // The potential_overwrite parameter to AddCoin is only allowed to be false if we know for