|
20 | 20 | #include <masternode/sync.h> |
21 | 21 |
|
22 | 22 | LLMQContext::LLMQContext(CEvoDB& evo_db, CTxMemPool& mempool, CConnman& connman, CSporkManager& sporkman, |
23 | | - const std::unique_ptr<PeerManager>& peerman, bool unit_tests, bool wipe) { |
24 | | - Create(evo_db, mempool, connman, sporkman, peerman, unit_tests, wipe); |
25 | | - |
26 | | - /* Context aliases to globals used by the LLMQ system */ |
27 | | - quorum_block_processor = llmq::quorumBlockProcessor.get(); |
28 | | - qman = llmq::quorumManager.get(); |
29 | | - clhandler = llmq::chainLocksHandler.get(); |
30 | | - isman = llmq::quorumInstantSendManager.get(); |
31 | | -} |
32 | | - |
33 | | -LLMQContext::~LLMQContext() { |
34 | | - isman = nullptr; |
35 | | - clhandler = nullptr; |
36 | | - qman = nullptr; |
37 | | - quorum_block_processor = nullptr; |
38 | | - |
39 | | - Destroy(); |
40 | | -} |
41 | | - |
42 | | -void LLMQContext::Create(CEvoDB& evo_db, CTxMemPool& mempool, CConnman& connman, CSporkManager& sporkman, |
43 | | - const std::unique_ptr<PeerManager>& peerman, bool unit_tests, bool wipe) { |
44 | | - bls_worker = std::make_shared<CBLSWorker>(); |
45 | | - |
46 | | - dkg_debugman = std::make_unique<llmq::CDKGDebugManager>(); |
47 | | - llmq::quorumBlockProcessor = std::make_unique<llmq::CQuorumBlockProcessor>(evo_db, connman, peerman); |
48 | | - qdkgsman = std::make_unique<llmq::CDKGSessionManager>(connman, *bls_worker, *dkg_debugman, *llmq::quorumBlockProcessor, sporkman, peerman, unit_tests, wipe); |
49 | | - llmq::quorumManager = std::make_unique<llmq::CQuorumManager>(evo_db, connman, *bls_worker, *llmq::quorumBlockProcessor, *qdkgsman, ::masternodeSync, peerman); |
50 | | - sigman = std::make_unique<llmq::CSigningManager>(connman, *llmq::quorumManager, peerman, unit_tests, wipe); |
51 | | - shareman = std::make_unique<llmq::CSigSharesManager>(connman, *llmq::quorumManager, *sigman, peerman); |
52 | | - llmq::chainLocksHandler = std::make_unique<llmq::CChainLocksHandler>(mempool, connman, sporkman, *sigman, *shareman, *llmq::quorumManager, *::masternodeSync, peerman); |
53 | | - llmq::quorumInstantSendManager = std::make_unique<llmq::CInstantSendManager>(mempool, connman, sporkman, *llmq::quorumManager, *sigman, *shareman, *llmq::chainLocksHandler, *::masternodeSync, peerman, unit_tests, wipe); |
54 | | - |
| 23 | + const std::unique_ptr<PeerManager>& peerman, bool unit_tests, bool wipe) : |
| 24 | + bls_worker{std::make_shared<CBLSWorker>()}, |
| 25 | + dkg_debugman{std::make_unique<llmq::CDKGDebugManager>()}, |
| 26 | + quorum_block_processor{[&]() -> llmq::CQuorumBlockProcessor* const { |
| 27 | + llmq::quorumBlockProcessor = std::make_unique<llmq::CQuorumBlockProcessor>(evo_db, connman, peerman); |
| 28 | + return llmq::quorumBlockProcessor.get(); |
| 29 | + }()}, |
| 30 | + qdkgsman{std::make_unique<llmq::CDKGSessionManager>(connman, *bls_worker, *dkg_debugman, *quorum_block_processor, sporkman, peerman, unit_tests, wipe)}, |
| 31 | + qman{[&]() -> llmq::CQuorumManager* const { |
| 32 | + llmq::quorumManager = std::make_unique<llmq::CQuorumManager>(evo_db, connman, *bls_worker, *quorum_block_processor, *qdkgsman, ::masternodeSync, peerman); |
| 33 | + return llmq::quorumManager.get(); |
| 34 | + }()}, |
| 35 | + sigman{std::make_unique<llmq::CSigningManager>(connman, *llmq::quorumManager, peerman, unit_tests, wipe)}, |
| 36 | + shareman{std::make_unique<llmq::CSigSharesManager>(connman, *llmq::quorumManager, *sigman, peerman)}, |
| 37 | + clhandler{[&]() -> llmq::CChainLocksHandler* const { |
| 38 | + llmq::chainLocksHandler = std::make_unique<llmq::CChainLocksHandler>(mempool, connman, sporkman, *sigman, *shareman, *llmq::quorumManager, *::masternodeSync, peerman); |
| 39 | + return llmq::chainLocksHandler.get(); |
| 40 | + }()}, |
| 41 | + isman{[&]() -> llmq::CInstantSendManager* const { |
| 42 | + llmq::quorumInstantSendManager = std::make_unique<llmq::CInstantSendManager>(mempool, connman, sporkman, *llmq::quorumManager, *sigman, *shareman, *llmq::chainLocksHandler, *::masternodeSync, peerman, unit_tests, wipe); |
| 43 | + return llmq::quorumInstantSendManager.get(); |
| 44 | + }()} |
| 45 | +{ |
55 | 46 | // NOTE: we use this only to wipe the old db, do NOT use it for anything else |
56 | 47 | // TODO: remove it in some future version |
57 | 48 | auto llmqDbTmp = std::make_unique<CDBWrapper>(unit_tests ? "" : (GetDataDir() / "llmq"), 1 << 20, unit_tests, true); |
58 | 49 | } |
59 | 50 |
|
60 | | -void LLMQContext::Destroy() { |
| 51 | +LLMQContext::~LLMQContext() { |
| 52 | + // LLMQContext doesn't own these objects, but still need to care of them for consistancy: |
61 | 53 | llmq::quorumInstantSendManager.reset(); |
62 | 54 | llmq::chainLocksHandler.reset(); |
63 | | - shareman.reset(); |
64 | | - sigman.reset(); |
65 | 55 | llmq::quorumManager.reset(); |
66 | | - qdkgsman.reset(); |
67 | 56 | llmq::quorumBlockProcessor.reset(); |
68 | | - dkg_debugman.reset(); |
69 | | - bls_worker.reset(); |
70 | 57 | { |
71 | 58 | LOCK(llmq::cs_llmq_vbc); |
72 | 59 | llmq::llmq_versionbitscache.Clear(); |
73 | 60 | } |
74 | 61 | } |
75 | 62 |
|
76 | 63 | void LLMQContext::Interrupt() { |
77 | | - if (shareman != nullptr) { |
78 | | - shareman->InterruptWorkerThread(); |
79 | | - } |
80 | | - if (llmq::quorumInstantSendManager != nullptr) { |
81 | | - llmq::quorumInstantSendManager->InterruptWorkerThread(); |
82 | | - } |
| 64 | + shareman->InterruptWorkerThread(); |
| 65 | + |
| 66 | + assert(isman == llmq::quorumInstantSendManager.get()); |
| 67 | + llmq::quorumInstantSendManager->InterruptWorkerThread(); |
83 | 68 | } |
84 | 69 |
|
85 | 70 | void LLMQContext::Start() { |
86 | | - if (bls_worker != nullptr) { |
87 | | - bls_worker->Start(); |
88 | | - } |
89 | | - if (qdkgsman != nullptr) { |
90 | | - qdkgsman->StartThreads(); |
91 | | - } |
92 | | - if (llmq::quorumManager != nullptr) { |
93 | | - llmq::quorumManager->Start(); |
94 | | - } |
95 | | - if (shareman != nullptr) { |
96 | | - shareman->RegisterAsRecoveredSigsListener(); |
97 | | - shareman->StartWorkerThread(); |
98 | | - } |
99 | | - if (llmq::chainLocksHandler != nullptr) { |
100 | | - llmq::chainLocksHandler->Start(); |
101 | | - } |
102 | | - if (llmq::quorumInstantSendManager != nullptr) { |
103 | | - llmq::quorumInstantSendManager->Start(); |
104 | | - } |
| 71 | + assert(quorum_block_processor == llmq::quorumBlockProcessor.get()); |
| 72 | + assert(qman == llmq::quorumManager.get()); |
| 73 | + assert(clhandler == llmq::chainLocksHandler.get()); |
| 74 | + assert(isman == llmq::quorumInstantSendManager.get()); |
| 75 | + |
| 76 | + bls_worker->Start(); |
| 77 | + qdkgsman->StartThreads(); |
| 78 | + qman->Start(); |
| 79 | + shareman->RegisterAsRecoveredSigsListener(); |
| 80 | + shareman->StartWorkerThread(); |
| 81 | + |
| 82 | + llmq::chainLocksHandler->Start(); |
| 83 | + llmq::quorumInstantSendManager->Start(); |
105 | 84 | } |
106 | 85 |
|
107 | 86 | void LLMQContext::Stop() { |
108 | | - if (llmq::quorumInstantSendManager != nullptr) { |
109 | | - llmq::quorumInstantSendManager->Stop(); |
110 | | - } |
111 | | - if (llmq::chainLocksHandler != nullptr) { |
112 | | - llmq::chainLocksHandler->Stop(); |
113 | | - } |
114 | | - if (shareman != nullptr) { |
115 | | - shareman->StopWorkerThread(); |
116 | | - shareman->UnregisterAsRecoveredSigsListener(); |
117 | | - } |
118 | | - if (llmq::quorumManager != nullptr) { |
119 | | - llmq::quorumManager->Stop(); |
120 | | - } |
121 | | - if (qdkgsman != nullptr) { |
122 | | - qdkgsman->StopThreads(); |
123 | | - } |
124 | | - if (bls_worker != nullptr) { |
125 | | - bls_worker->Stop(); |
126 | | - } |
| 87 | + assert(quorum_block_processor == llmq::quorumBlockProcessor.get()); |
| 88 | + assert(qman == llmq::quorumManager.get()); |
| 89 | + assert(clhandler == llmq::chainLocksHandler.get()); |
| 90 | + assert(isman == llmq::quorumInstantSendManager.get()); |
| 91 | + |
| 92 | + llmq::quorumInstantSendManager->Stop(); |
| 93 | + llmq::chainLocksHandler->Stop(); |
| 94 | + |
| 95 | + shareman->StopWorkerThread(); |
| 96 | + shareman->UnregisterAsRecoveredSigsListener(); |
| 97 | + qman->Stop(); |
| 98 | + qdkgsman->StopThreads(); |
| 99 | + bls_worker->Stop(); |
127 | 100 | } |
0 commit comments