Skip to content

Commit 362e310

Browse files
committed
merge bitcoin#21943: Dedup and RAII-fy the creation of a copy of CConnman::vNodes
1 parent bf98ad6 commit 362e310

File tree

6 files changed

+225
-115
lines changed

6 files changed

+225
-115
lines changed

src/governance/governance.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,11 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
12231223

12241224
int CGovernanceManager::RequestGovernanceObjectVotes(CNode& peer, CConnman& connman) const
12251225
{
1226-
std::array<CNode*, 1> nodeCopy{&peer};
1227-
return RequestGovernanceObjectVotes(nodeCopy, connman);
1226+
const std::vector<CNode*> vNodeCopy{&peer};
1227+
return RequestGovernanceObjectVotes(vNodeCopy, connman);
12281228
}
12291229

1230-
int CGovernanceManager::RequestGovernanceObjectVotes(Span<CNode*> vNodesCopy, CConnman& connman) const
1230+
int CGovernanceManager::RequestGovernanceObjectVotes(const std::vector<CNode*>& vNodesCopy, CConnman& connman) const
12311231
{
12321232
static std::map<uint256, std::map<CService, int64_t> > mapAskedRecently;
12331233

@@ -1501,7 +1501,7 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& co
15011501

15021502
void CGovernanceManager::RequestOrphanObjects(CConnman& connman)
15031503
{
1504-
std::vector<CNode*> vNodesCopy = connman.CopyNodeVector(CConnman::FullyConnectedOnly);
1504+
const CConnman::NodesSnapshot snap{connman, /* filter = */ CConnman::FullyConnectedOnly};
15051505

15061506
std::vector<uint256> vecHashesFiltered;
15071507
{
@@ -1517,15 +1517,13 @@ void CGovernanceManager::RequestOrphanObjects(CConnman& connman)
15171517

15181518
LogPrint(BCLog::GOBJECT, "CGovernanceObject::RequestOrphanObjects -- number objects = %d\n", vecHashesFiltered.size());
15191519
for (const uint256& nHash : vecHashesFiltered) {
1520-
for (CNode* pnode : vNodesCopy) {
1520+
for (CNode* pnode : snap.Nodes()) {
15211521
if (!pnode->CanRelay()) {
15221522
continue;
15231523
}
15241524
RequestGovernanceObject(pnode, nHash, connman);
15251525
}
15261526
}
1527-
1528-
connman.ReleaseNodeVector(vNodesCopy);
15291527
}
15301528

15311529
void CGovernanceManager::CleanOrphanObjects()

src/governance/governance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class CGovernanceManager : public GovernanceStore
358358
void InitOnLoad();
359359

360360
int RequestGovernanceObjectVotes(CNode& peer, CConnman& connman) const;
361-
int RequestGovernanceObjectVotes(Span<CNode*> vNodesCopy, CConnman& connman) const;
361+
int RequestGovernanceObjectVotes(const std::vector<CNode*>& vNodesCopy, CConnman& connman) const;
362362

363363
/*
364364
* Trigger Management (formerly CGovernanceTriggerManager)

src/llmq/signing_shares.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,14 +1092,13 @@ bool CSigSharesManager::SendMessages()
10921092
return session->sendSessionId;
10931093
};
10941094

1095-
std::vector<CNode*> vNodesCopy = connman.CopyNodeVector(CConnman::FullyConnectedOnly);
1096-
1095+
const CConnman::NodesSnapshot snap{connman, /* filter = */ CConnman::FullyConnectedOnly};
10971096
{
10981097
LOCK(cs);
10991098
CollectSigSharesToRequest(sigSharesToRequest);
11001099
CollectSigSharesToSend(sigShareBatchesToSend);
11011100
CollectSigSharesToAnnounce(sigSharesToAnnounce);
1102-
CollectSigSharesToSendConcentrated(sigSharesToSend, vNodesCopy);
1101+
CollectSigSharesToSendConcentrated(sigSharesToSend, snap.Nodes());
11031102

11041103
for (auto& [nodeId, sigShareMap] : sigSharesToRequest) {
11051104
for (auto& [hash, sigShareInv] : sigShareMap) {
@@ -1120,7 +1119,7 @@ bool CSigSharesManager::SendMessages()
11201119

11211120
bool didSend = false;
11221121

1123-
for (auto& pnode : vNodesCopy) {
1122+
for (auto& pnode : snap.Nodes()) {
11241123
CNetMsgMaker msgMaker(pnode->GetCommonVersion());
11251124

11261125
if (const auto it1 = sigSessionAnnouncements.find(pnode->GetId()); it1 != sigSessionAnnouncements.end()) {
@@ -1222,9 +1221,6 @@ bool CSigSharesManager::SendMessages()
12221221
}
12231222
}
12241223

1225-
// looped through all nodes, release them
1226-
connman.ReleaseNodeVector(vNodesCopy);
1227-
12281224
return didSend;
12291225
}
12301226

src/masternode/sync.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,11 @@ void CMasternodeSync::ProcessTick()
140140
}
141141

142142
nTimeLastProcess = GetTime();
143-
std::vector<CNode*> vNodesCopy = connman.CopyNodeVector(CConnman::FullyConnectedOnly);
143+
const CConnman::NodesSnapshot snap{connman, /* filter = */ CConnman::FullyConnectedOnly};
144144

145145
// gradually request the rest of the votes after sync finished
146146
if(IsSynced()) {
147-
m_govman.RequestGovernanceObjectVotes(vNodesCopy, connman);
148-
connman.ReleaseNodeVector(vNodesCopy);
147+
m_govman.RequestGovernanceObjectVotes(snap.Nodes(), connman);
149148
return;
150149
}
151150

@@ -154,7 +153,7 @@ void CMasternodeSync::ProcessTick()
154153
LogPrint(BCLog::MNSYNC, "CMasternodeSync::ProcessTick -- nTick %d nCurrentAsset %d nTriedPeerCount %d nSyncProgress %f\n", nTick, nCurrentAsset, nTriedPeerCount, nSyncProgress);
155154
uiInterface.NotifyAdditionalDataSyncProgressChanged(nSyncProgress);
156155

157-
for (auto& pnode : vNodesCopy)
156+
for (auto& pnode : snap.Nodes())
158157
{
159158
CNetMsgMaker msgMaker(pnode->GetCommonVersion());
160159

@@ -189,7 +188,7 @@ void CMasternodeSync::ProcessTick()
189188
}
190189

191190
if (nCurrentAsset == MASTERNODE_SYNC_BLOCKCHAIN) {
192-
int64_t nTimeSyncTimeout = vNodesCopy.size() > 3 ? MASTERNODE_SYNC_TICK_SECONDS : MASTERNODE_SYNC_TIMEOUT_SECONDS;
191+
int64_t nTimeSyncTimeout = snap.Nodes().size() > 3 ? MASTERNODE_SYNC_TICK_SECONDS : MASTERNODE_SYNC_TIMEOUT_SECONDS;
193192
if (fReachedBestHeader && (GetTime() - nTimeLastBumped > nTimeSyncTimeout)) {
194193
// At this point we know that:
195194
// a) there are peers (because we are looping on at least one of them);
@@ -205,7 +204,7 @@ void CMasternodeSync::ProcessTick()
205204

206205
if (gArgs.GetBoolArg("-syncmempool", DEFAULT_SYNC_MEMPOOL)) {
207206
// Now that the blockchain is synced request the mempool from the connected outbound nodes if possible
208-
for (auto pNodeTmp : vNodesCopy) {
207+
for (auto pNodeTmp : snap.Nodes()) {
209208
bool fRequestedEarlier = m_netfulfilledman.HasFulfilledRequest(pNodeTmp->addr, "mempool-sync");
210209
if (pNodeTmp->nVersion >= 70216 && !pNodeTmp->IsInboundConn() && !fRequestedEarlier && !pNodeTmp->IsBlockRelayOnly()) {
211210
m_netfulfilledman.AddFulfilledRequest(pNodeTmp->addr, "mempool-sync");
@@ -222,7 +221,6 @@ void CMasternodeSync::ProcessTick()
222221
if(nCurrentAsset == MASTERNODE_SYNC_GOVERNANCE) {
223222
if (!m_govman.IsValid()) {
224223
SwitchToNextAsset();
225-
connman.ReleaseNodeVector(vNodesCopy);
226224
return;
227225
}
228226
LogPrint(BCLog::GOBJECT, "CMasternodeSync::ProcessTick -- nTick %d nCurrentAsset %d nTimeLastBumped %lld GetTime() %lld diff %lld\n", nTick, nCurrentAsset, nTimeLastBumped, GetTime(), GetTime() - nTimeLastBumped);
@@ -235,7 +233,6 @@ void CMasternodeSync::ProcessTick()
235233
// it's kind of ok to skip this for now, hopefully we'll catch up later?
236234
}
237235
SwitchToNextAsset();
238-
connman.ReleaseNodeVector(vNodesCopy);
239236
return;
240237
}
241238

@@ -259,12 +256,11 @@ void CMasternodeSync::ProcessTick()
259256

260257
if (nCurrentAsset != MASTERNODE_SYNC_GOVERNANCE) {
261258
// looped through all nodes and not syncing governance yet/already, release them
262-
connman.ReleaseNodeVector(vNodesCopy);
263259
return;
264260
}
265261

266262
// request votes on per-obj basis from each node
267-
for (const auto& pnode : vNodesCopy) {
263+
for (const auto& pnode : snap.Nodes()) {
268264
if(!m_netfulfilledman.HasFulfilledRequest(pnode->addr, "governance-sync")) {
269265
continue; // to early for this node
270266
}
@@ -291,16 +287,12 @@ void CMasternodeSync::ProcessTick()
291287
// reset nTimeNoObjectsLeft to be able to use the same condition on resync
292288
nTimeNoObjectsLeft = 0;
293289
SwitchToNextAsset();
294-
connman.ReleaseNodeVector(vNodesCopy);
295290
return;
296291
}
297292
nLastTick = nTick;
298293
nLastVotes = m_govman.GetVoteCount();
299294
}
300295
}
301-
302-
// looped through all nodes, release them
303-
connman.ReleaseNodeVector(vNodesCopy);
304296
}
305297

306298
void CMasternodeSync::SendGovernanceSyncRequest(CNode* pnode) const

0 commit comments

Comments
 (0)