Skip to content

Commit c44bc8c

Browse files
Merge #6841: refactor: move CSigSharesManager to ActiveContext, drop CActiveMasternodeManager from CSigSharesManager
af9a15f lint: apply review suggestions (Kittywhiskers Van Gogh) 6c89f0e refactor: move `RelayRecoveredSig()` call out of `CSigSharesManager` (Kittywhiskers Van Gogh) 0052fca refactor: move `AsyncSignIfMember()` to `CSigSharesManager` (Kittywhiskers Van Gogh) 489cf6e refactor: adjust `CSigSharesManager` ctor arguments (Kittywhiskers Van Gogh) f6b02b7 refactor: move `CDKGSessionManager` thread management to `ActiveContext` (Kittywhiskers Van Gogh) 74afa28 refactor: move `CSigSharesManager` to `ActiveContext` (Kittywhiskers Van Gogh) 6563ade refactor: remove unused `CSigningManager` from `CChainLocksHandler` ctor (Kittywhiskers Van Gogh) Pull request description: ## Additional Information * Depends on #6828 * Depends on #6839 * This pull request does _not_ address `CQuorumManager` or `CDKGSessionManager` as both complicate signer separation as they accommodate watch-only mode ([source](https://github.com/dashpay/dash/blob/e5ce1f0bef825d63e81a77dfbe65804da5ae04de/src/llmq/dkgsessionmgr.cpp#L46-L49), [source](https://github.com/dashpay/dash/blob/e5ce1f0bef825d63e81a77dfbe65804da5ae04de/src/llmq/quorums.cpp#L804-L806)) and allow for LLMQ data recovery enablement ([source](https://github.com/dashpay/dash/blob/e5ce1f0bef825d63e81a77dfbe65804da5ae04de/src/llmq/quorums.cpp#L251-L253)), which makes them better suited for a separate PR. In contrast, `CSigSharesManager` can be more-or-less wholesale moved to `ActiveContext`, the objective of this PR. * As part of isolating masternode-mode logic `CSigningManager::AsyncSignIfMember()`, which accepts `CSigSharesManager` as an argument, was moved to `CSigSharesManager` accepting `CSigningManager` as argument. This required exposing the recovered signatures DB to `CSigningManager`, which has been done with `GetDb()`. * As a result of this change and the move to `ActiveContext`, the `quorum sign` RPC will only work if masternode-mode is enabled * To allow dropping `CActiveMasternodeManager` from `CSigningManager`, we needed to drop a `RelayRecoveredSig()` call that is only done in masternode-mode ([source](https://github.com/dashpay/dash/blob/e5ce1f0bef825d63e81a77dfbe65804da5ae04de/src/llmq/signing.cpp#L619)). To do that, we rely on the `NotifyRecoveredSig()` signal that is invoked at the tail of `CSigningManager::ProcessRecoveredSig()` ([source](https://github.com/dashpay/dash/blob/e5ce1f0bef825d63e81a77dfbe65804da5ae04de/src/llmq/signing.cpp#L619)) by having `ActiveNotificationInterface` slot in, acting in _response_ to the notification rather than before (i.e. existing behavior). ## Breaking Changes The `quorum sign` RPC will only work if masternode-mode is enabled. ## Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added or updated relevant unit/integration/functional/e2e tests - [x] I have made corresponding changes to the documentation (note: N/A) - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ ACKs for top commit: UdjinM6: utACK af9a15f Tree-SHA512: c769a88b5d311f7562d2fcb9f2953746de6edcb13227034ed2db50c059d31838b925b13de00d4d91bd4fa3ea5d98c13ac3ddfba4cf0c4b17187d7004f7446b1b
2 parents 9a5644a + af9a15f commit c44bc8c

19 files changed

+308
-261
lines changed

src/chainlock/chainlock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ bool AreChainLocksEnabled(const CSporkManager& sporkman)
4343
return sporkman.IsSporkActive(SPORK_19_CHAINLOCKS_ENABLED);
4444
}
4545

46-
CChainLocksHandler::CChainLocksHandler(CChainState& chainstate, CQuorumManager& _qman, CSigningManager& _sigman,
47-
CSporkManager& sporkman, CTxMemPool& _mempool, const CMasternodeSync& mn_sync) :
46+
CChainLocksHandler::CChainLocksHandler(CChainState& chainstate, CQuorumManager& _qman, CSporkManager& sporkman,
47+
CTxMemPool& _mempool, const CMasternodeSync& mn_sync) :
4848
m_chainstate{chainstate},
4949
qman{_qman},
5050
spork_manager{sporkman},

src/chainlock/chainlock.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#include <gsl/pointers.h>
1919

2020
#include <atomic>
21+
#include <cassert>
22+
#include <chrono>
2123
#include <map>
24+
#include <memory>
25+
#include <thread>
2226
#include <unordered_map>
2327

2428
class CBlock;
@@ -66,8 +70,8 @@ class CChainLocksHandler final : public chainlock::ChainLockSignerParent
6670
std::atomic<std::chrono::seconds> lastCleanupTime{0s};
6771

6872
public:
69-
explicit CChainLocksHandler(CChainState& chainstate, CQuorumManager& _qman, CSigningManager& _sigman,
70-
CSporkManager& sporkman, CTxMemPool& _mempool, const CMasternodeSync& mn_sync);
73+
explicit CChainLocksHandler(CChainState& chainstate, CQuorumManager& _qman, CSporkManager& sporkman,
74+
CTxMemPool& _mempool, const CMasternodeSync& mn_sync);
7175
~CChainLocksHandler();
7276

7377
void ConnectSigner(gsl::not_null<chainlock::ChainLockSigner*> signer)

src/chainlock/signing.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <chainlock/clsig.h>
1111
#include <instantsend/instantsend.h>
12+
#include <llmq/signing_shares.h>
1213
#include <masternode/sync.h>
1314
#include <spork.h>
1415

@@ -140,7 +141,7 @@ void ChainLockSigner::TrySignChainTip(const llmq::CInstantSendManager& isman)
140141
lastSignedMsgHash = msgHash;
141142
}
142143

143-
m_sigman.AsyncSignIfMember(Params().GetConsensus().llmqTypeChainLocks, m_shareman, requestId, msgHash);
144+
m_shareman.AsyncSignIfMember(Params().GetConsensus().llmqTypeChainLocks, m_sigman, requestId, msgHash);
144145
}
145146

146147
void ChainLockSigner::EraseFromBlockHashTxidMap(const uint256& hash)

src/init.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ void Interrupt(NodeContext& node)
231231
InterruptRPC();
232232
InterruptREST();
233233
InterruptTorControl();
234+
if (node.active_ctx) {
235+
node.active_ctx->Interrupt();
236+
}
234237
if (node.llmq_ctx) {
235238
node.llmq_ctx->Interrupt();
236239
}
@@ -266,6 +269,7 @@ void PrepareShutdown(NodeContext& node)
266269
StopREST();
267270
StopRPC();
268271
StopHTTPServer();
272+
if (node.active_ctx) node.active_ctx->Stop();
269273
if (node.llmq_ctx) node.llmq_ctx->Stop();
270274

271275
for (const auto& client : node.chain_clients) {
@@ -2263,7 +2267,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
22632267

22642268
// ********************************************************* Step 10a: schedule Dash-specific tasks
22652269

2266-
node.llmq_ctx->Start(*node.connman, *node.peerman);
2270+
node.llmq_ctx->Start(*node.peerman);
2271+
if (node.active_ctx) node.active_ctx->Start(*node.connman, *node.peerman);
22672272

22682273
node.scheduler->scheduleEvery(std::bind(&CNetFulfilledRequestManager::DoMaintenance, std::ref(*node.netfulfilledman)), std::chrono::minutes{1});
22692274
node.scheduler->scheduleEvery(std::bind(&CMasternodeSync::DoMaintenance, std::ref(*node.mn_sync), std::cref(*node.peerman), std::cref(*node.govman)), std::chrono::seconds{1});

src/instantsend/signing.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <chainlock/chainlock.h>
1515
#include <llmq/quorums.h>
16+
#include <llmq/signing_shares.h>
1617
#include <masternode/sync.h>
1718
#include <spork.h>
1819

@@ -330,7 +331,7 @@ bool InstantSendSigner::TrySignInputLocks(const CTransaction& tx, bool fRetroact
330331
WITH_LOCK(cs_input_requests, inputRequestIds.emplace(id));
331332
LogPrint(BCLog::INSTANTSEND, "%s -- txid=%s: trying to vote on input %s with id %s. fRetroactive=%d\n",
332333
__func__, tx.GetHash().ToString(), in.prevout.ToStringShort(), id.ToString(), fRetroactive);
333-
if (m_sigman.AsyncSignIfMember(llmqType, m_shareman, id, tx.GetHash(), {}, fRetroactive)) {
334+
if (m_shareman.AsyncSignIfMember(llmqType, m_sigman, id, tx.GetHash(), {}, fRetroactive)) {
334335
LogPrint(BCLog::INSTANTSEND, "%s -- txid=%s: voted on input %s with id %s\n", __func__,
335336
tx.GetHash().ToString(), in.prevout.ToStringShort(), id.ToString());
336337
}
@@ -388,6 +389,6 @@ void InstantSendSigner::TrySignInstantSendLock(const CTransaction& tx)
388389
txToCreatingInstantSendLocks.emplace(tx.GetHash(), &e.first->second);
389390
}
390391

391-
m_sigman.AsyncSignIfMember(llmqType, m_shareman, id, tx.GetHash(), quorum->m_quorum_base_block_index->GetBlockHash());
392+
m_shareman.AsyncSignIfMember(llmqType, m_sigman, id, tx.GetHash(), quorum->m_quorum_base_block_index->GetBlockHash());
392393
}
393394
} // namespace instantsend

src/llmq/context.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
#include <llmq/dkgsessionmgr.h>
1313
#include <llmq/quorums.h>
1414
#include <llmq/signing.h>
15-
#include <llmq/signing_shares.h>
1615
#include <llmq/snapshot.h>
1716
#include <validation.h>
1817

1918
LLMQContext::LLMQContext(ChainstateManager& chainman, CDeterministicMNManager& dmnman, CEvoDB& evo_db,
2019
CMasternodeMetaMan& mn_metaman, CMNHFManager& mnhfman, CSporkManager& sporkman,
2120
CTxMemPool& mempool, const CActiveMasternodeManager* const mn_activeman,
2221
const CMasternodeSync& mn_sync, bool unit_tests, bool wipe) :
23-
is_masternode{mn_activeman != nullptr},
2422
bls_worker{std::make_shared<CBLSWorker>()},
2523
dkg_debugman{std::make_unique<llmq::CDKGDebugManager>()},
2624
qsnapman{std::make_unique<llmq::CQuorumSnapshotManager>(evo_db)},
@@ -32,10 +30,8 @@ LLMQContext::LLMQContext(ChainstateManager& chainman, CDeterministicMNManager& d
3230
qman{std::make_unique<llmq::CQuorumManager>(*bls_worker, chainman.ActiveChainstate(), dmnman, *qdkgsman, evo_db,
3331
*quorum_block_processor, *qsnapman, mn_activeman, mn_sync, sporkman,
3432
unit_tests, wipe)},
35-
sigman{std::make_unique<llmq::CSigningManager>(mn_activeman, chainman.ActiveChainstate(), *qman, unit_tests, wipe)},
36-
shareman{std::make_unique<llmq::CSigSharesManager>(*sigman, mn_activeman, *qman, sporkman)},
37-
clhandler{std::make_unique<llmq::CChainLocksHandler>(chainman.ActiveChainstate(), *qman, *sigman, sporkman, mempool,
38-
mn_sync)},
33+
sigman{std::make_unique<llmq::CSigningManager>(chainman.ActiveChainstate(), *qman, unit_tests, wipe)},
34+
clhandler{std::make_unique<llmq::CChainLocksHandler>(chainman.ActiveChainstate(), *qman, sporkman, mempool, mn_sync)},
3935
isman{std::make_unique<llmq::CInstantSendManager>(*clhandler, chainman.ActiveChainstate(), *qman, *sigman, sporkman,
4036
mempool, mn_sync, unit_tests, wipe)}
4137
{
@@ -49,31 +45,21 @@ LLMQContext::~LLMQContext() {
4945

5046
void LLMQContext::Interrupt() {
5147
isman->InterruptWorkerThread();
52-
shareman->InterruptWorkerThread();
5348
sigman->InterruptWorkerThread();
5449
}
5550

56-
void LLMQContext::Start(CConnman& connman, PeerManager& peerman)
51+
void LLMQContext::Start(PeerManager& peerman)
5752
{
58-
if (is_masternode) {
59-
qdkgsman->StartThreads(connman, peerman);
60-
}
6153
qman->Start();
6254
sigman->StartWorkerThread(peerman);
63-
shareman->RegisterAsRecoveredSigsListener();
64-
shareman->StartWorkerThread(connman, peerman);
6555
clhandler->Start(*isman);
6656
isman->Start(peerman);
6757
}
6858

69-
void LLMQContext::Stop() {
59+
void LLMQContext::Stop()
60+
{
7061
isman->Stop();
7162
clhandler->Stop();
72-
shareman->StopWorkerThread();
73-
shareman->UnregisterAsRecoveredSigsListener();
7463
sigman->StopWorkerThread();
7564
qman->Stop();
76-
if (is_masternode) {
77-
qdkgsman->StopThreads();
78-
}
7965
}

src/llmq/context.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
class CActiveMasternodeManager;
1111
class CBLSWorker;
12-
class CConnman;
1312
class ChainstateManager;
1413
class CDeterministicMNManager;
1514
class CEvoDB;
@@ -28,14 +27,10 @@ class CInstantSendManager;
2827
class CQuorumBlockProcessor;
2928
class CQuorumManager;
3029
class CQuorumSnapshotManager;
31-
class CSigSharesManager;
3230
class CSigningManager;
3331
}
3432

3533
struct LLMQContext {
36-
private:
37-
const bool is_masternode;
38-
3934
public:
4035
LLMQContext() = delete;
4136
LLMQContext(const LLMQContext&) = delete;
@@ -46,7 +41,7 @@ struct LLMQContext {
4641
~LLMQContext();
4742

4843
void Interrupt();
49-
void Start(CConnman& connman, PeerManager& peerman);
44+
void Start(PeerManager& peerman);
5045
void Stop();
5146

5247
/** Guaranteed if LLMQContext is initialized then all members are valid too
@@ -66,7 +61,6 @@ struct LLMQContext {
6661
const std::unique_ptr<llmq::CDKGSessionManager> qdkgsman;
6762
const std::unique_ptr<llmq::CQuorumManager> qman;
6863
const std::unique_ptr<llmq::CSigningManager> sigman;
69-
const std::unique_ptr<llmq::CSigSharesManager> shareman;
7064
const std::unique_ptr<llmq::CChainLocksHandler> clhandler;
7165
const std::unique_ptr<llmq::CInstantSendManager> isman;
7266
};

src/llmq/ehf_signals.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <llmq/ehf_signals.h>
6-
#include <llmq/quorums.h>
7-
#include <llmq/commitment.h>
86

97
#include <chainparams.h>
108
#include <consensus/validation.h>
119
#include <deploymentstatus.h>
12-
#include <evo/mnhftx.h>
1310
#include <index/txindex.h> // g_txindex
1411
#include <primitives/transaction.h>
1512
#include <validation.h>
1613

14+
#include <evo/mnhftx.h>
15+
#include <llmq/commitment.h>
16+
#include <llmq/quorums.h>
17+
#include <llmq/signing_shares.h>
18+
1719
namespace llmq {
1820
CEHFSignalsHandler::CEHFSignalsHandler(ChainstateManager& chainman, CMNHFManager& mnhfman, CSigningManager& sigman,
1921
CSigSharesManager& shareman, const CQuorumManager& qman) :
@@ -76,7 +78,7 @@ void CEHFSignalsHandler::trySignEHFSignal(int bit, const CBlockIndex* const pind
7678
const uint256 msgHash = mnhfPayload.PrepareTx().GetHash();
7779

7880
WITH_LOCK(cs, ids.insert(requestId));
79-
sigman.AsyncSignIfMember(llmqType, shareman, requestId, msgHash, quorum->qc->quorumHash, false, true);
81+
shareman.AsyncSignIfMember(llmqType, sigman, requestId, msgHash, quorum->qc->quorumHash, false, true);
8082
}
8183

8284
MessageProcessingResult CEHFSignalsHandler::HandleNewRecoveredSig(const CRecoveredSig& recoveredSig)

src/llmq/signing.cpp

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,8 @@ void CRecoveredSigsDb::CleanupOldVotes(int64_t maxAge)
332332

333333
//////////////////
334334

335-
CSigningManager::CSigningManager(const CActiveMasternodeManager* const mn_activeman, const CChainState& chainstate,
336-
const CQuorumManager& _qman, bool fMemory, bool fWipe) :
335+
CSigningManager::CSigningManager(const CChainState& chainstate, const CQuorumManager& _qman, bool fMemory, bool fWipe) :
337336
db(fMemory, fWipe),
338-
m_mn_activeman(mn_activeman),
339337
m_chainstate(chainstate),
340338
qman(_qman)
341339
{
@@ -615,10 +613,6 @@ void CSigningManager::ProcessRecoveredSig(const std::shared_ptr<const CRecovered
615613
db.WriteRecoveredSig(*recoveredSig);
616614
WITH_LOCK(cs_pending, pendingReconstructedRecoveredSigs.erase(recoveredSig->GetHash()));
617615

618-
if (m_mn_activeman != nullptr) {
619-
peerman.RelayRecoveredSig(recoveredSig->GetHash());
620-
}
621-
622616
auto listeners = WITH_LOCK(cs_listeners, return recoveredSigsListeners);
623617
for (auto& l : listeners) {
624618
peerman.PostProcessMessage(l->HandleNewRecoveredSig(*recoveredSig));
@@ -666,80 +660,6 @@ void CSigningManager::UnregisterRecoveredSigsListener(CRecoveredSigsListener* l)
666660
recoveredSigsListeners.erase(itRem, recoveredSigsListeners.end());
667661
}
668662

669-
bool CSigningManager::AsyncSignIfMember(Consensus::LLMQType llmqType, CSigSharesManager& shareman, const uint256& id,
670-
const uint256& msgHash, const uint256& quorumHash, bool allowReSign,
671-
bool allowDiffMsgHashSigning)
672-
{
673-
if (m_mn_activeman == nullptr) return false;
674-
if (m_mn_activeman->GetProTxHash().IsNull()) return false;
675-
676-
const auto quorum = [&]() {
677-
if (quorumHash.IsNull()) {
678-
// This might end up giving different results on different members
679-
// This might happen when we are on the brink of confirming a new quorum
680-
// This gives a slight risk of not getting enough shares to recover a signature
681-
// But at least it shouldn't be possible to get conflicting recovered signatures
682-
// TODO fix this by re-signing when the next block arrives, but only when that block results in a change of the quorum list and no recovered signature has been created in the mean time
683-
const auto &llmq_params_opt = Params().GetLLMQ(llmqType);
684-
assert(llmq_params_opt.has_value());
685-
return SelectQuorumForSigning(llmq_params_opt.value(), m_chainstate.m_chain, qman, id);
686-
} else {
687-
return qman.GetQuorum(llmqType, quorumHash);
688-
}
689-
}();
690-
691-
if (!quorum) {
692-
LogPrint(BCLog::LLMQ, "CSigningManager::%s -- failed to select quorum. id=%s, msgHash=%s\n", __func__, id.ToString(), msgHash.ToString());
693-
return false;
694-
}
695-
696-
if (!quorum->IsValidMember(m_mn_activeman->GetProTxHash())) {
697-
return false;
698-
}
699-
700-
{
701-
bool hasVoted = db.HasVotedOnId(llmqType, id);
702-
if (hasVoted) {
703-
uint256 prevMsgHash;
704-
db.GetVoteForId(llmqType, id, prevMsgHash);
705-
if (msgHash != prevMsgHash) {
706-
if (allowDiffMsgHashSigning) {
707-
LogPrintf("CSigningManager::%s -- already voted for id=%s and msgHash=%s. Signing for different msgHash=%s\n",
708-
__func__, id.ToString(), prevMsgHash.ToString(), msgHash.ToString());
709-
hasVoted = false;
710-
} else {
711-
LogPrintf("CSigningManager::%s -- already voted for id=%s and msgHash=%s. Not voting on conflicting msgHash=%s\n",
712-
__func__, id.ToString(), prevMsgHash.ToString(), msgHash.ToString());
713-
return false;
714-
}
715-
} else if (allowReSign) {
716-
LogPrint(BCLog::LLMQ, "CSigningManager::%s -- already voted for id=%s and msgHash=%s. Resigning!\n", __func__,
717-
id.ToString(), prevMsgHash.ToString());
718-
} else {
719-
LogPrint(BCLog::LLMQ, "CSigningManager::%s -- already voted for id=%s and msgHash=%s. Not voting again.\n", __func__,
720-
id.ToString(), prevMsgHash.ToString());
721-
return false;
722-
}
723-
}
724-
725-
if (db.HasRecoveredSigForId(llmqType, id)) {
726-
// no need to sign it if we already have a recovered sig
727-
return true;
728-
}
729-
if (!hasVoted) {
730-
db.WriteVoteForId(llmqType, id, msgHash);
731-
}
732-
}
733-
734-
if (allowReSign) {
735-
// make us re-announce all known shares (other nodes might have run into a timeout)
736-
shareman.ForceReAnnouncement(quorum, llmqType, id, msgHash);
737-
}
738-
shareman.AsyncSign(quorum, id, msgHash);
739-
740-
return true;
741-
}
742-
743663
bool CSigningManager::HasRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash) const
744664
{
745665
return db.HasRecoveredSig(llmqType, id, msgHash);
@@ -791,7 +711,7 @@ void CSigningManager::StartWorkerThread(PeerManager& peerman)
791711
assert(false);
792712
}
793713

794-
workThread = std::thread(&util::TraceThread, "sigshares", [this, &peerman] { WorkThreadMain(peerman); });
714+
workThread = std::thread(&util::TraceThread, "recsigs", [this, &peerman] { WorkThreadMain(peerman); });
795715
}
796716

797717
void CSigningManager::StopWorkerThread()

0 commit comments

Comments
 (0)