diff --git a/src/init.cpp b/src/init.cpp index d96fae79c..e15e5f5cd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2592,6 +2592,7 @@ bool AppInitMain(const util::Ref &context, NodeContext &node, interfaces::BlockA if (fSmartnodeMode) { node.scheduler->scheduleEvery( std::bind(&CCoinJoinServer::DoMaintenance, std::ref(coinJoinServer), std::ref(*node.connman)), 1000); + node.scheduler->scheduleEvery(&llmq::DoMaintenance, 3600000); // run every 1 hours #ifdef ENABLE_WALLET } else if(CCoinJoinClientOptions::IsEnabled()) { node.scheduler->scheduleEvery(std::bind(&DoCoinJoinMaintenance, std::ref(*node.connman)), 1000); diff --git a/src/llmq/quorums_dkgsessionmgr.cpp b/src/llmq/quorums_dkgsessionmgr.cpp index 80f2e36ea..40ff1ee66 100644 --- a/src/llmq/quorums_dkgsessionmgr.cpp +++ b/src/llmq/quorums_dkgsessionmgr.cpp @@ -424,4 +424,48 @@ namespace llmq { return sporkManager.IsSporkActive(SPORK_17_QUORUM_DKG_ENABLED); } + void CDKGSessionManager::CleanupOldContributions() const + { + if (db->IsEmpty()) { + return; + } + + const auto prefixes = {DB_VVEC, DB_SKCONTRIB, DB_ENC_CONTRIB, DB_NODE_VOTE}; + + for (const auto& p : Params().GetConsensus().llmqs) { + auto& params = p.second; + LogPrint(BCLog::LLMQ, "CDKGSessionManager::%s -- looking for old entries for llmq type %d\n", __func__, params.type); + + CDBBatch batch(*db); + size_t cnt_old{0}, cnt_all{0}; + for (const auto& prefix : prefixes) { + std::unique_ptr pcursor(db->NewIterator()); + auto start = std::make_tuple(prefix, params.type, uint256(), uint256()); + decltype(start) k; + + pcursor->Seek(start); + LOCK(cs_main); + while (pcursor->Valid() || std::get<0>(k) != prefix || std::get<1>(k) != params.type) { + if (!pcursor->GetKey(k)) { + break; + } + cnt_all++; + const CBlockIndex* pindexQuorum = LookupBlockIndex(std::get<2>(k)); + if (pindexQuorum == nullptr || ::ChainActive().Tip()->nHeight - pindexQuorum->nHeight > params.max_store_depth()) { + // not found or too old + batch.Erase(k); + cnt_old++; + } + pcursor->Next(); + } + pcursor.reset(); + } + LogPrint(BCLog::LLMQ, "CDKGSessionManager::%s -- found %lld entries for llmq type %d\n", __func__, cnt_all, uint8_t(params.type)); + if (cnt_old > 0) { + db->WriteBatch(batch); + LogPrint(BCLog::LLMQ, "CDKGSessionManager::%s -- removed %lld old entries for llmq type %d\n", __func__, cnt_old, uint8_t(params.type)); + } + } + } + } // namespace llmq diff --git a/src/llmq/quorums_dkgsessionmgr.h b/src/llmq/quorums_dkgsessionmgr.h index 9f867a5fe..c885c2ab9 100644 --- a/src/llmq/quorums_dkgsessionmgr.h +++ b/src/llmq/quorums_dkgsessionmgr.h @@ -101,6 +101,8 @@ namespace llmq { const CBlockIndex *pQuorumBaseBlockIndex, const uint256 &proTxHash, const uint32_t& updateVote); + + void CleanupOldContributions() const; private: void MigrateDKG(); diff --git a/src/llmq/quorums_init.cpp b/src/llmq/quorums_init.cpp index 3d0153a49..d66e0c46d 100644 --- a/src/llmq/quorums_init.cpp +++ b/src/llmq/quorums_init.cpp @@ -113,4 +113,10 @@ namespace llmq { } } + void DoMaintenance() { + if (quorumDKGSessionManager != nullptr) { + quorumDKGSessionManager->CleanupOldContributions(); + } + } + } // namespace llmq diff --git a/src/llmq/quorums_init.h b/src/llmq/quorums_init.h index 1295f8829..26921fede 100644 --- a/src/llmq/quorums_init.h +++ b/src/llmq/quorums_init.h @@ -27,6 +27,8 @@ namespace llmq { void StopLLMQSystem(); void InterruptLLMQSystem(); + + void DoMaintenance(); } // namespace llmq #endif //RAPTOREUM_QUORUMS_INIT_H diff --git a/src/llmq/quorums_parameters.h b/src/llmq/quorums_parameters.h index 46cb8948d..53326e68c 100644 --- a/src/llmq/quorums_parameters.h +++ b/src/llmq/quorums_parameters.h @@ -91,8 +91,21 @@ namespace Consensus { // should be at least one more then the active quorums set. int keepOldConnections; + // The number of quorums for which we should keep keys. Usually it's equal to signingActiveQuorumCount * 2. + // Unlike for other quorum types we want to keep data (secret key shares and vvec) + // for Platform quorums for much longer because Platform can be restarted and + // it must be able to re-sign stuff. + + int keepOldKeys; + // How many members should we try to send all sigShares to before we give up. int recoveryMembers; + public: + // For how many blocks recent DKG info should be kept + [[ nodiscard ]] constexpr int max_store_depth() const + { + return keepOldKeys * dkgInterval; + } }; static constexpr LLMQParams @@ -112,6 +125,7 @@ namespace Consensus { .signingActiveQuorumCount = 2, // just a few ones to allow easier testing .keepOldConnections = 3, + .keepOldKeys = 4, .recoveryMembers = 3, }; @@ -132,6 +146,7 @@ namespace Consensus { .signingActiveQuorumCount = 2, // just a few ones to allow easier testing .keepOldConnections = 3, + .keepOldKeys = 4, .recoveryMembers = 3, }; @@ -152,6 +167,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // two days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 5, }; @@ -172,6 +188,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // four days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 5, }; @@ -192,6 +209,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // two days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 5, }; @@ -212,6 +230,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // four days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 5, }; @@ -232,6 +251,7 @@ namespace Consensus { .signingActiveQuorumCount = 6, // just a few ones to allow easier testing .keepOldConnections = 7, + .keepOldKeys = 12, .recoveryMembers = 7, }; @@ -252,6 +272,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // two days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 5, }; @@ -272,6 +293,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // four days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 5, }; @@ -292,6 +314,7 @@ namespace Consensus { .signingActiveQuorumCount = 24, // a full day worth of LLMQs .keepOldConnections = 25, + .keepOldKeys = 48, .recoveryMembers = 25, }; @@ -312,6 +335,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // two days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 100, }; @@ -333,6 +357,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // four days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 100, }; @@ -353,6 +378,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // two days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 50, }; @@ -375,6 +401,7 @@ namespace Consensus { .signingActiveQuorumCount = 4, // four days worth of LLMQs .keepOldConnections = 5, + .keepOldKeys = 8, .recoveryMembers = 50, }; @@ -396,6 +423,7 @@ namespace Consensus { .signingActiveQuorumCount = 2, // just a few ones to allow easier testing .keepOldConnections = 3, + .keepOldKeys = 4, .recoveryMembers = 3, }; @@ -416,6 +444,7 @@ namespace Consensus { .signingActiveQuorumCount = 24, // a full day worth of LLMQs .keepOldConnections = 25, + .keepOldKeys = 48, .recoveryMembers = 50, }; @@ -437,6 +466,7 @@ namespace Consensus { .signingActiveQuorumCount = 24, // a full day worth of LLMQs .keepOldConnections = 25, + .keepOldKeys = 48, .recoveryMembers = 50, };