diff --git a/src/init.cpp b/src/init.cpp index bf68900aaf93..678a5a29e56c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1832,7 +1832,7 @@ bool AppInitMain() deterministicMNManager.reset(); deterministicMNManager.reset(new CDeterministicMNManager(*evoDb)); - llmq::InitLLMQSystem(*evoDb, &scheduler, false, fReset || fReindexChainState); + llmq::InitLLMQSystem(*evoDb, false, fReset || fReindexChainState); if (fReset) { pblocktree->WriteReindexing(true); diff --git a/src/llmq/quorums_chainlocks.cpp b/src/llmq/quorums_chainlocks.cpp index 926036926f3d..a0f5be06d385 100644 --- a/src/llmq/quorums_chainlocks.cpp +++ b/src/llmq/quorums_chainlocks.cpp @@ -33,13 +33,17 @@ std::string CChainLockSig::ToString() const return strprintf("CChainLockSig(nHeight=%d, blockHash=%s)", nHeight, blockHash.ToString()); } -CChainLocksHandler::CChainLocksHandler(CScheduler* _scheduler) : - scheduler(_scheduler) +CChainLocksHandler::CChainLocksHandler() { + scheduler = new CScheduler(); + CScheduler::Function serviceLoop = boost::bind(&CScheduler::serviceQueue, scheduler); + scheduler_thread = new boost::thread(boost::bind(&TraceThread, "cl-scheduler", serviceLoop)); } CChainLocksHandler::~CChainLocksHandler() { + delete scheduler_thread; + delete scheduler; } void CChainLocksHandler::Start() @@ -56,6 +60,8 @@ void CChainLocksHandler::Start() void CChainLocksHandler::Stop() { quorumSigningManager->UnregisterRecoveredSigsListener(this); + scheduler_thread->interrupt(); + scheduler_thread->join(); } bool CChainLocksHandler::AlreadyHave(const CInv& inv) @@ -518,7 +524,7 @@ void CChainLocksHandler::EnforceBestChainLock() } LogPrintf("CChainLocksHandler::%s -- CLSIG (%s) invalidates block %s\n", __func__, clsig.ToString(), jt->second->GetBlockHash().ToString()); - DoInvalidateBlock(jt->second, false); + DoInvalidateBlock(jt->second); } pindex = pindex->pprev; @@ -581,27 +587,18 @@ void CChainLocksHandler::HandleNewRecoveredSig(const llmq::CRecoveredSig& recove } // WARNING, do not hold cs while calling this method as we'll otherwise run into a deadlock -void CChainLocksHandler::DoInvalidateBlock(const CBlockIndex* pindex, bool activateBestChain) +void CChainLocksHandler::DoInvalidateBlock(const CBlockIndex* pindex) { - auto& params = Params(); - - { - LOCK(cs_main); + LOCK(cs_main); - // get the non-const pointer - CBlockIndex* pindex2 = mapBlockIndex[pindex->GetBlockHash()]; + auto& params = Params(); - CValidationState state; - if (!InvalidateBlock(state, params, pindex2)) { - LogPrintf("CChainLocksHandler::%s -- InvalidateBlock failed: %s\n", __func__, FormatStateMessage(state)); - // This should not have happened and we are in a state were it's not safe to continue anymore - assert(false); - } - } + // get the non-const pointer + CBlockIndex* pindex2 = mapBlockIndex[pindex->GetBlockHash()]; CValidationState state; - if (activateBestChain && !ActivateBestChain(state, params)) { - LogPrintf("CChainLocksHandler::%s -- ActivateBestChain failed: %s\n", __func__, FormatStateMessage(state)); + if (!InvalidateBlock(state, params, pindex2)) { + LogPrintf("CChainLocksHandler::%s -- InvalidateBlock failed: %s\n", __func__, FormatStateMessage(state)); // This should not have happened and we are in a state were it's not safe to continue anymore assert(false); } diff --git a/src/llmq/quorums_chainlocks.h b/src/llmq/quorums_chainlocks.h index b2296b94146f..033f8041f74e 100644 --- a/src/llmq/quorums_chainlocks.h +++ b/src/llmq/quorums_chainlocks.h @@ -14,6 +14,8 @@ #include #include +#include + class CBlockIndex; class CScheduler; @@ -52,6 +54,7 @@ class CChainLocksHandler : public CRecoveredSigsListener private: CScheduler* scheduler; + boost::thread* scheduler_thread; CCriticalSection cs; bool tryLockChainTipScheduled{false}; bool isSporkActive{false}; @@ -78,7 +81,7 @@ class CChainLocksHandler : public CRecoveredSigsListener int64_t lastCleanupTime{0}; public: - explicit CChainLocksHandler(CScheduler* _scheduler); + explicit CChainLocksHandler(); ~CChainLocksHandler(); void Start(); @@ -110,7 +113,7 @@ class CChainLocksHandler : public CRecoveredSigsListener bool InternalHasChainLock(int nHeight, const uint256& blockHash); bool InternalHasConflictingChainLock(int nHeight, const uint256& blockHash); - void DoInvalidateBlock(const CBlockIndex* pindex, bool activateBestChain); + void DoInvalidateBlock(const CBlockIndex* pindex); BlockTxs::mapped_type GetBlockTxs(const uint256& blockHash); diff --git a/src/llmq/quorums_init.cpp b/src/llmq/quorums_init.cpp index 4d2bb4e80e39..23b8b5ebdc9d 100644 --- a/src/llmq/quorums_init.cpp +++ b/src/llmq/quorums_init.cpp @@ -15,7 +15,6 @@ #include #include -#include namespace llmq { @@ -24,7 +23,7 @@ CBLSWorker* blsWorker; CDBWrapper* llmqDb; -void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests, bool fWipe) +void InitLLMQSystem(CEvoDB& evoDb, bool unitTests, bool fWipe) { llmqDb = new CDBWrapper(unitTests ? "" : (GetDataDir() / "llmq"), 1 << 20, unitTests, fWipe); blsWorker = new CBLSWorker(); @@ -35,7 +34,7 @@ void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests, bool f quorumManager = new CQuorumManager(evoDb, *blsWorker, *quorumDKGSessionManager); quorumSigSharesManager = new CSigSharesManager(); quorumSigningManager = new CSigningManager(*llmqDb, unitTests); - chainLocksHandler = new CChainLocksHandler(scheduler); + chainLocksHandler = new CChainLocksHandler(); quorumInstantSendManager = new CInstantSendManager(*llmqDb); } diff --git a/src/llmq/quorums_init.h b/src/llmq/quorums_init.h index 38309b0a13bc..d6e16222635c 100644 --- a/src/llmq/quorums_init.h +++ b/src/llmq/quorums_init.h @@ -7,7 +7,6 @@ class CDBWrapper; class CEvoDB; -class CScheduler; namespace llmq { @@ -16,7 +15,7 @@ namespace llmq static const bool DEFAULT_WATCH_QUORUMS = false; // Init/destroy LLMQ globals -void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests, bool fWipe = false); +void InitLLMQSystem(CEvoDB& evoDb, bool unitTests, bool fWipe = false); void DestroyLLMQSystem(); // Manage scheduled tasks, threads, listeners etc. diff --git a/src/test/test_dash.cpp b/src/test/test_dash.cpp index 55254afaad6d..434e86a2cfc8 100644 --- a/src/test/test_dash.cpp +++ b/src/test/test_dash.cpp @@ -97,7 +97,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha connman = g_connman.get(); pblocktree.reset(new CBlockTreeDB(1 << 20, true)); pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true)); - llmq::InitLLMQSystem(*evoDb, nullptr, true); + llmq::InitLLMQSystem(*evoDb, true); pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get())); if (!LoadGenesisBlock(chainparams)) { throw std::runtime_error("LoadGenesisBlock failed."); @@ -117,6 +117,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha TestingSetup::~TestingSetup() { llmq::InterruptLLMQSystem(); + llmq::StopLLMQSystem(); threadGroup.interrupt_all(); threadGroup.join_all(); GetMainSignals().FlushBackgroundCallbacks();