Skip to content

Commit d9e5cc7

Browse files
committed
refactor: move PeerManager out of CInstantSendManager ctor
1 parent 82d1aed commit d9e5cc7

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

src/dsnotificationinterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void CDSNotificationInterface::TransactionAddedToMempool(const CTransactionRef&
107107
{
108108
assert(m_cj_ctx && m_llmq_ctx);
109109

110-
m_llmq_ctx->isman->TransactionAddedToMempool(ptx);
110+
m_llmq_ctx->isman->TransactionAddedToMempool(m_peerman, ptx);
111111
m_llmq_ctx->clhandler->TransactionAddedToMempool(ptx, nAcceptTime);
112112
m_cj_ctx->dstxman->TransactionAddedToMempool(ptx);
113113
}

src/llmq/context.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ LLMQContext::LLMQContext(ChainstateManager& chainman, CConnman& connman, CDeterm
4747
llmq::quorumInstantSendManager = std::make_unique<llmq::CInstantSendManager>(*llmq::chainLocksHandler,
4848
chainman.ActiveChainstate(), *qman,
4949
*sigman, *shareman, sporkman,
50-
mempool, mn_sync, peerman,
51-
is_masternode, unit_tests, wipe);
50+
mempool, mn_sync, is_masternode,
51+
unit_tests, wipe);
5252
return llmq::quorumInstantSendManager.get();
5353
}()},
5454
ehfSignalsHandler{std::make_unique<llmq::CEHFSignalsHandler>(chainman, mnhfman, *sigman, *shareman, *qman)}
@@ -87,7 +87,7 @@ void LLMQContext::Start(CConnman& connman, PeerManager& peerman)
8787
sigman->StartWorkerThread(peerman);
8888

8989
llmq::chainLocksHandler->Start();
90-
llmq::quorumInstantSendManager->Start();
90+
llmq::quorumInstantSendManager->Start(peerman);
9191
}
9292

9393
void LLMQContext::Stop() {

src/llmq/instantsend.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,14 @@ std::vector<uint256> CInstantSendDb::RemoveChainedInstantSendLocks(const uint256
430430
////////////////
431431

432432

433-
void CInstantSendManager::Start()
433+
void CInstantSendManager::Start(PeerManager& peerman)
434434
{
435435
// can't start new thread if we have one running already
436436
if (workThread.joinable()) {
437437
assert(false);
438438
}
439439

440-
workThread = std::thread(&util::TraceThread, "isman", [this] { WorkThreadMain(); });
440+
workThread = std::thread(&util::TraceThread, "isman", [this, &peerman] { WorkThreadMain(peerman); });
441441

442442
sigman.RegisterRecoveredSigsListener(this);
443443
}
@@ -732,21 +732,23 @@ void CInstantSendManager::HandleNewInstantSendLockRecoveredSig(const llmq::CReco
732732
pendingInstantSendLocks.emplace(hash, std::make_pair(-1, islock));
733733
}
734734

735-
PeerMsgRet CInstantSendManager::ProcessMessage(const CNode& pfrom, std::string_view msg_type, CDataStream& vRecv)
735+
PeerMsgRet CInstantSendManager::ProcessMessage(const CNode& pfrom, PeerManager& peerman, std::string_view msg_type,
736+
CDataStream& vRecv)
736737
{
737738
if (IsInstantSendEnabled() && msg_type == NetMsgType::ISDLOCK) {
738739
const auto islock = std::make_shared<CInstantSendLock>();
739740
vRecv >> *islock;
740-
return ProcessMessageInstantSendLock(pfrom, islock);
741+
return ProcessMessageInstantSendLock(pfrom, peerman, islock);
741742
}
742743
return {};
743744
}
744745

745-
PeerMsgRet CInstantSendManager::ProcessMessageInstantSendLock(const CNode& pfrom, const llmq::CInstantSendLockPtr& islock)
746+
PeerMsgRet CInstantSendManager::ProcessMessageInstantSendLock(const CNode& pfrom, PeerManager& peerman,
747+
const llmq::CInstantSendLockPtr& islock)
746748
{
747749
auto hash = ::SerializeHash(*islock);
748750

749-
WITH_LOCK(::cs_main, Assert(m_peerman)->EraseObjectRequest(pfrom.GetId(), CInv(MSG_ISDLOCK, hash)));
751+
WITH_LOCK(::cs_main, peerman.EraseObjectRequest(pfrom.GetId(), CInv(MSG_ISDLOCK, hash)));
750752

751753
if (!islock->TriviallyValid()) {
752754
return tl::unexpected{100};
@@ -800,7 +802,7 @@ bool CInstantSendLock::TriviallyValid() const
800802
return true;
801803
}
802804

803-
bool CInstantSendManager::ProcessPendingInstantSendLocks()
805+
bool CInstantSendManager::ProcessPendingInstantSendLocks(PeerManager& peerman)
804806
{
805807
decltype(pendingInstantSendLocks) pend;
806808
bool fMoreWork{false};
@@ -845,7 +847,7 @@ bool CInstantSendManager::ProcessPendingInstantSendLocks()
845847
auto dkgInterval = llmq_params.dkgInterval;
846848

847849
// First check against the current active set and don't ban
848-
auto badISLocks = ProcessPendingInstantSendLocks(llmq_params, 0, pend, false);
850+
auto badISLocks = ProcessPendingInstantSendLocks(llmq_params, peerman, /*signOffset=*/0, pend, false);
849851
if (!badISLocks.empty()) {
850852
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- doing verification on old active set\n", __func__);
851853

@@ -858,13 +860,15 @@ bool CInstantSendManager::ProcessPendingInstantSendLocks()
858860
}
859861
}
860862
// Now check against the previous active set and perform banning if this fails
861-
ProcessPendingInstantSendLocks(llmq_params, dkgInterval, pend, true);
863+
ProcessPendingInstantSendLocks(llmq_params, peerman, dkgInterval, pend, true);
862864
}
863865

864866
return fMoreWork;
865867
}
866868

867-
std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPendingInstantSendLocks(const Consensus::LLMQParams& llmq_params, int signOffset, const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban)
869+
std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPendingInstantSendLocks(
870+
const Consensus::LLMQParams& llmq_params, PeerManager& peerman, int signOffset,
871+
const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban)
868872
{
869873
CBLSBatchVerifier<NodeId, uint256> batchVerifier(false, true, 8);
870874
std::unordered_map<uint256, CRecoveredSig, StaticSaltedHasher> recSigs;
@@ -936,7 +940,7 @@ std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPend
936940
for (const auto& nodeId : batchVerifier.badSources) {
937941
// Let's not be too harsh, as the peer might simply be unlucky and might have sent us an old lock which
938942
// does not validate anymore due to changed quorums
939-
Assert(m_peerman)->Misbehaving(nodeId, 20);
943+
peerman.Misbehaving(nodeId, 20);
940944
}
941945
}
942946
for (const auto& p : pend) {
@@ -951,7 +955,7 @@ std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPend
951955
continue;
952956
}
953957

954-
ProcessInstantSendLock(nodeId, hash, islock);
958+
ProcessInstantSendLock(nodeId, peerman, hash, islock);
955959

956960
// See comment further on top. We pass a reconstructed recovered sig to the signing manager to avoid
957961
// double-verification of the sig.
@@ -969,7 +973,8 @@ std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPend
969973
return badISLocks;
970974
}
971975

972-
void CInstantSendManager::ProcessInstantSendLock(NodeId from, const uint256& hash, const CInstantSendLockPtr& islock)
976+
void CInstantSendManager::ProcessInstantSendLock(NodeId from, PeerManager& peerman, const uint256& hash,
977+
const CInstantSendLockPtr& islock)
973978
{
974979
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- txid=%s, islock=%s: processing islock, peer=%d\n", __func__,
975980
islock->txid.ToString(), hash.ToString(), from);
@@ -1030,28 +1035,28 @@ void CInstantSendManager::ProcessInstantSendLock(NodeId from, const uint256& has
10301035

10311036
CInv inv(MSG_ISDLOCK, hash);
10321037
if (tx != nullptr) {
1033-
Assert(m_peerman)->RelayInvFiltered(inv, *tx, ISDLOCK_PROTO_VERSION);
1038+
peerman.RelayInvFiltered(inv, *tx, ISDLOCK_PROTO_VERSION);
10341039
} else {
10351040
// we don't have the TX yet, so we only filter based on txid. Later when that TX arrives, we will re-announce
10361041
// with the TX taken into account.
1037-
Assert(m_peerman)->RelayInvFiltered(inv, islock->txid, ISDLOCK_PROTO_VERSION);
1042+
peerman.RelayInvFiltered(inv, islock->txid, ISDLOCK_PROTO_VERSION);
10381043
}
10391044

10401045
ResolveBlockConflicts(hash, *islock);
10411046

10421047
if (tx != nullptr) {
1043-
RemoveMempoolConflictsForLock(hash, *islock);
1048+
RemoveMempoolConflictsForLock(peerman, hash, *islock);
10441049
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- notify about lock %s for tx %s\n", __func__,
10451050
hash.ToString(), tx->GetHash().ToString());
10461051
GetMainSignals().NotifyTransactionLock(tx, islock);
10471052
// bump mempool counter to make sure newly locked txes are picked up by getblocktemplate
10481053
mempool.AddTransactionsUpdated(1);
10491054
} else {
1050-
m_peerman->AskPeersForTransaction(islock->txid, m_is_masternode);
1055+
peerman.AskPeersForTransaction(islock->txid, m_is_masternode);
10511056
}
10521057
}
10531058

1054-
void CInstantSendManager::TransactionAddedToMempool(const CTransactionRef& tx)
1059+
void CInstantSendManager::TransactionAddedToMempool(PeerManager& peerman, const CTransactionRef& tx)
10551060
{
10561061
if (!IsInstantSendEnabled() || !m_mn_sync.IsBlockchainSynced() || tx->vin.empty()) {
10571062
return;
@@ -1080,7 +1085,7 @@ void CInstantSendManager::TransactionAddedToMempool(const CTransactionRef& tx)
10801085
// TX is not locked, so make sure it is tracked
10811086
AddNonLockedTx(tx, nullptr);
10821087
} else {
1083-
RemoveMempoolConflictsForLock(::SerializeHash(*islock), *islock);
1088+
RemoveMempoolConflictsForLock(peerman, ::SerializeHash(*islock), *islock);
10841089
}
10851090
}
10861091

@@ -1292,7 +1297,8 @@ void CInstantSendManager::HandleFullyConfirmedBlock(const CBlockIndex* pindex)
12921297
}
12931298
}
12941299

1295-
void CInstantSendManager::RemoveMempoolConflictsForLock(const uint256& hash, const CInstantSendLock& islock)
1300+
void CInstantSendManager::RemoveMempoolConflictsForLock(PeerManager& peerman, const uint256& hash,
1301+
const CInstantSendLock& islock)
12961302
{
12971303
std::unordered_map<uint256, CTransactionRef, StaticSaltedHasher> toDelete;
12981304

@@ -1321,7 +1327,7 @@ void CInstantSendManager::RemoveMempoolConflictsForLock(const uint256& hash, con
13211327
for (const auto& p : toDelete) {
13221328
RemoveConflictedTx(*p.second);
13231329
}
1324-
m_peerman->AskPeersForTransaction(islock.txid, m_is_masternode);
1330+
peerman.AskPeersForTransaction(islock.txid, m_is_masternode);
13251331
}
13261332
}
13271333

@@ -1583,10 +1589,10 @@ size_t CInstantSendManager::GetInstantSendLockCount() const
15831589
return db.GetInstantSendLockCount();
15841590
}
15851591

1586-
void CInstantSendManager::WorkThreadMain()
1592+
void CInstantSendManager::WorkThreadMain(PeerManager& peerman)
15871593
{
15881594
while (!workInterrupt) {
1589-
bool fMoreWork = ProcessPendingInstantSendLocks();
1595+
bool fMoreWork = ProcessPendingInstantSendLocks(peerman);
15901596
ProcessPendingRetryLockTxs();
15911597

15921598
if (!fMoreWork && !workInterrupt.sleep_for(std::chrono::milliseconds(100))) {

src/llmq/instantsend.h

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ class CInstantSendManager : public CRecoveredSigsListener
206206
CSporkManager& spork_manager;
207207
CTxMemPool& mempool;
208208
const CMasternodeSync& m_mn_sync;
209-
const std::unique_ptr<PeerManager>& m_peerman;
210209

211210
const bool m_is_masternode;
212211

@@ -256,9 +255,8 @@ class CInstantSendManager : public CRecoveredSigsListener
256255
public:
257256
explicit CInstantSendManager(CChainLocksHandler& _clhandler, CChainState& chainstate, CQuorumManager& _qman,
258257
CSigningManager& _sigman, CSigSharesManager& _shareman, CSporkManager& sporkman,
259-
CTxMemPool& _mempool, const CMasternodeSync& mn_sync,
260-
const std::unique_ptr<PeerManager>& peerman, bool is_masternode, bool unitTests,
261-
bool fWipe) :
258+
CTxMemPool& _mempool, const CMasternodeSync& mn_sync, bool is_masternode,
259+
bool unitTests, bool fWipe) :
262260
db(unitTests, fWipe),
263261
clhandler(_clhandler),
264262
m_chainstate(chainstate),
@@ -268,14 +266,13 @@ class CInstantSendManager : public CRecoveredSigsListener
268266
spork_manager(sporkman),
269267
mempool(_mempool),
270268
m_mn_sync(mn_sync),
271-
m_peerman(peerman),
272269
m_is_masternode{is_masternode}
273270
{
274271
workInterrupt.reset();
275272
}
276273
~CInstantSendManager() = default;
277274

278-
void Start();
275+
void Start(PeerManager& peerman);
279276
void Stop();
280277
void InterruptWorkerThread() { workInterrupt(); };
281278

@@ -295,18 +292,15 @@ class CInstantSendManager : public CRecoveredSigsListener
295292
const Consensus::Params& params) EXCLUSIVE_LOCKS_REQUIRED(!cs_inputReqests);
296293
void TrySignInstantSendLock(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(!cs_creating);
297294

298-
PeerMsgRet ProcessMessageInstantSendLock(const CNode& pfrom, const CInstantSendLockPtr& islock);
299-
bool ProcessPendingInstantSendLocks()
295+
PeerMsgRet ProcessMessageInstantSendLock(const CNode& pfrom, PeerManager& peerman, const CInstantSendLockPtr& islock);
296+
bool ProcessPendingInstantSendLocks(PeerManager& peerman)
300297
EXCLUSIVE_LOCKS_REQUIRED(!cs_creating, !cs_inputReqests, !cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
301298

302-
std::unordered_set<uint256, StaticSaltedHasher> ProcessPendingInstantSendLocks(const Consensus::LLMQParams& llmq_params,
303-
int signOffset,
304-
const std::unordered_map<uint256,
305-
std::pair<NodeId, CInstantSendLockPtr>,
306-
StaticSaltedHasher>& pend,
307-
bool ban)
299+
std::unordered_set<uint256, StaticSaltedHasher> ProcessPendingInstantSendLocks(
300+
const Consensus::LLMQParams& llmq_params, PeerManager& peerman, int signOffset,
301+
const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban)
308302
EXCLUSIVE_LOCKS_REQUIRED(!cs_creating, !cs_inputReqests, !cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
309-
void ProcessInstantSendLock(NodeId from, const uint256& hash, const CInstantSendLockPtr& islock)
303+
void ProcessInstantSendLock(NodeId from, PeerManager& peerman, const uint256& hash, const CInstantSendLockPtr& islock)
310304
EXCLUSIVE_LOCKS_REQUIRED(!cs_creating, !cs_inputReqests, !cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
311305

312306
void AddNonLockedTx(const CTransactionRef& tx, const CBlockIndex* pindexMined)
@@ -318,14 +312,14 @@ class CInstantSendManager : public CRecoveredSigsListener
318312
void TruncateRecoveredSigsForInputs(const CInstantSendLock& islock)
319313
EXCLUSIVE_LOCKS_REQUIRED(!cs_inputReqests);
320314

321-
void RemoveMempoolConflictsForLock(const uint256& hash, const CInstantSendLock& islock)
315+
void RemoveMempoolConflictsForLock(PeerManager& peerman, const uint256& hash, const CInstantSendLock& islock)
322316
EXCLUSIVE_LOCKS_REQUIRED(!cs_inputReqests, !cs_nonLocked, !cs_pendingRetry);
323317
void ResolveBlockConflicts(const uint256& islockHash, const CInstantSendLock& islock)
324318
EXCLUSIVE_LOCKS_REQUIRED(!cs_inputReqests, !cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
325319
void ProcessPendingRetryLockTxs()
326320
EXCLUSIVE_LOCKS_REQUIRED(!cs_creating, !cs_inputReqests, !cs_nonLocked, !cs_pendingRetry);
327321

328-
void WorkThreadMain()
322+
void WorkThreadMain(PeerManager& peerman)
329323
EXCLUSIVE_LOCKS_REQUIRED(!cs_creating, !cs_inputReqests, !cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
330324

331325
void HandleFullyConfirmedBlock(const CBlockIndex* pindex)
@@ -339,9 +333,9 @@ class CInstantSendManager : public CRecoveredSigsListener
339333
[[nodiscard]] MessageProcessingResult HandleNewRecoveredSig(const CRecoveredSig& recoveredSig) override
340334
EXCLUSIVE_LOCKS_REQUIRED(!cs_creating, !cs_inputReqests, !cs_pendingLocks);
341335

342-
PeerMsgRet ProcessMessage(const CNode& pfrom, std::string_view msg_type, CDataStream& vRecv);
336+
PeerMsgRet ProcessMessage(const CNode& pfrom, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv);
343337

344-
void TransactionAddedToMempool(const CTransactionRef& tx)
338+
void TransactionAddedToMempool(PeerManager& peerman, const CTransactionRef& tx)
345339
EXCLUSIVE_LOCKS_REQUIRED(!cs_creating, !cs_inputReqests, !cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
346340
void TransactionRemovedFromMempool(const CTransactionRef& tx);
347341
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5278,7 +5278,7 @@ void PeerManagerImpl::ProcessMessage(
52785278
return; // CLSIG
52795279
}
52805280

5281-
ProcessPeerMsgRet(m_llmq_ctx->isman->ProcessMessage(pfrom, msg_type, vRecv), pfrom);
5281+
ProcessPeerMsgRet(m_llmq_ctx->isman->ProcessMessage(pfrom, *this, msg_type, vRecv), pfrom);
52825282
return;
52835283
}
52845284

0 commit comments

Comments
 (0)