From 3b7df9aea0a1a2aaf5f6b2b2f8d53220e7d9966d Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Sat, 16 Nov 2024 16:11:31 +0700 Subject: [PATCH 1/2] feat: update IS database instantly, no more dependency on DIP0020 This fork is activated long time ago, no more possible conflicts --- src/llmq/instantsend.cpp | 12 +++--------- src/llmq/instantsend.h | 4 ++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/llmq/instantsend.cpp b/src/llmq/instantsend.cpp index bf76757fb2eb7..ad47e118b09db 100644 --- a/src/llmq/instantsend.cpp +++ b/src/llmq/instantsend.cpp @@ -61,10 +61,9 @@ CInstantSendDb::CInstantSendDb(bool unitTests, bool fWipe) : CInstantSendDb::~CInstantSendDb() = default; -void CInstantSendDb::Upgrade(const CTxMemPool& mempool) +void CInstantSendDb::Upgrade() { - LOCK2(cs_main, mempool.cs); - LOCK(cs_db); + LOCK2(cs_main, cs_db); int v{0}; if (!db->Read(DB_VERSION, v) || v < CInstantSendDb::CURRENT_VERSION) { CDBBatch batch(*db); @@ -1103,7 +1102,7 @@ void CInstantSendManager::TransactionAddedToMempool(const CTransactionRef& tx) void CInstantSendManager::TransactionRemovedFromMempool(const CTransactionRef& tx) { - if (tx->vin.empty() || !fUpgradedDB) { + if (tx->vin.empty()) { return; } @@ -1252,11 +1251,6 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock) void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew) { - if (!fUpgradedDB && pindexNew->nHeight + 1 >= Params().GetConsensus().DIP0020Height) { - db.Upgrade(mempool); - fUpgradedDB = true; - } - bool fDIP0008Active = pindexNew->pprev && pindexNew->pprev->nHeight >= Params().GetConsensus().DIP0008Height; if (AreChainLocksEnabled(spork_manager) && fDIP0008Active) { diff --git a/src/llmq/instantsend.h b/src/llmq/instantsend.h index defafa5cf2754..4b22a12d51e89 100644 --- a/src/llmq/instantsend.h +++ b/src/llmq/instantsend.h @@ -116,7 +116,7 @@ class CInstantSendDb explicit CInstantSendDb(bool unitTests, bool fWipe); ~CInstantSendDb(); - void Upgrade(const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(!cs_db); + void Upgrade() EXCLUSIVE_LOCKS_REQUIRED(!cs_db); /** * This method is called when an InstantSend Lock is processed and adds the lock to the database @@ -209,7 +209,6 @@ class CInstantSendManager : public CRecoveredSigsListener const std::unique_ptr& m_peerman; const bool m_is_masternode; - std::atomic fUpgradedDB{false}; std::thread workThread; CThreadInterrupt workInterrupt; @@ -264,6 +263,7 @@ class CInstantSendManager : public CRecoveredSigsListener shareman(_shareman), spork_manager(sporkman), mempool(_mempool), m_mn_sync(mn_sync), m_peerman(peerman), m_is_masternode{is_masternode} { + db.Upgrade(); // Upgrade DB if need to do it workInterrupt.reset(); } ~CInstantSendManager() = default; From 89528bc4ddc7fbcbf0f127e9b04eba318f25dcc6 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sat, 16 Nov 2024 15:44:04 +0300 Subject: [PATCH 2/2] feat: overwrite outdated IS db with a new empty one --- src/llmq/instantsend.cpp | 36 ++++++++++-------------------------- src/llmq/instantsend.h | 5 ++--- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/src/llmq/instantsend.cpp b/src/llmq/instantsend.cpp index ad47e118b09db..c50fa939e0f0c 100644 --- a/src/llmq/instantsend.cpp +++ b/src/llmq/instantsend.cpp @@ -57,41 +57,25 @@ uint256 CInstantSendLock::GetRequestId() const CInstantSendDb::CInstantSendDb(bool unitTests, bool fWipe) : db(std::make_unique(unitTests ? "" : (gArgs.GetDataDirNet() / "llmq/isdb"), 32 << 20, unitTests, fWipe)) { + Upgrade(unitTests); } CInstantSendDb::~CInstantSendDb() = default; -void CInstantSendDb::Upgrade() +void CInstantSendDb::Upgrade(bool unitTests) { - LOCK2(cs_main, cs_db); + LOCK(cs_db); int v{0}; if (!db->Read(DB_VERSION, v) || v < CInstantSendDb::CURRENT_VERSION) { + // Wipe db + db.reset(); + db = std::make_unique(unitTests ? "" : (gArgs.GetDataDirNet() / "llmq/isdb"), 32 << 20, unitTests, + /*fWipe=*/true); CDBBatch batch(*db); - CInstantSendLock islock; - - auto it = std::unique_ptr(db->NewIterator()); - auto firstKey = std::make_tuple(std::string{DB_ISLOCK_BY_HASH}, uint256()); - it->Seek(firstKey); - decltype(firstKey) curKey; - - while (it->Valid()) { - if (!it->GetKey(curKey) || std::get<0>(curKey) != DB_ISLOCK_BY_HASH) { - break; - } - uint256 hashBlock; - CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, islock.txid, Params().GetConsensus(), hashBlock); - if (it->GetValue(islock) && !tx) { - // Drop locks for unknown txes - batch.Erase(std::make_tuple(DB_HASH_BY_TXID, islock.txid)); - for (auto& in : islock.inputs) { - batch.Erase(std::make_tuple(DB_HASH_BY_OUTPOINT, in)); - } - batch.Erase(curKey); - } - it->Next(); - } batch.Write(DB_VERSION, CInstantSendDb::CURRENT_VERSION); - db->WriteBatch(batch); + // Sync DB changes to disk + db->WriteBatch(batch, /*fSync=*/true); + batch.Clear(); } } diff --git a/src/llmq/instantsend.h b/src/llmq/instantsend.h index 4b22a12d51e89..5f8f9e18bd680 100644 --- a/src/llmq/instantsend.h +++ b/src/llmq/instantsend.h @@ -112,12 +112,12 @@ class CInstantSendDb uint256 GetInstantSendLockHashByTxidInternal(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_db); + void Upgrade(bool unitTests) EXCLUSIVE_LOCKS_REQUIRED(!cs_db); + public: explicit CInstantSendDb(bool unitTests, bool fWipe); ~CInstantSendDb(); - void Upgrade() EXCLUSIVE_LOCKS_REQUIRED(!cs_db); - /** * This method is called when an InstantSend Lock is processed and adds the lock to the database * @param hash The hash of the InstantSend Lock @@ -263,7 +263,6 @@ class CInstantSendManager : public CRecoveredSigsListener shareman(_shareman), spork_manager(sporkman), mempool(_mempool), m_mn_sync(mn_sync), m_peerman(peerman), m_is_masternode{is_masternode} { - db.Upgrade(); // Upgrade DB if need to do it workInterrupt.reset(); } ~CInstantSendManager() = default;