Skip to content

Commit b621cfb

Browse files
OlegGirkoUdjinM6
authored andcommitted
Backport Bitcoin PR#8708: net: have CConnman handle message sending (#1553)
* serialization: teach serializers variadics Also add a variadic CDataStream ctor for ease-of-use. * connman is in charge of pushing messages The changes here are dense and subtle, but hopefully all is more explicit than before. - CConnman is now in charge of sending data rather than the nodes themselves. This is necessary because many decisions need to be made with all nodes in mind, and a model that requires the nodes calling up to their manager quickly turns to spaghetti. - The per-node-serializer (ssSend) has been replaced with a (quasi-)const send-version. Since the send version for serialization can only change once per connection, we now explicitly tag messages with INIT_PROTO_VERSION if they are sent before the handshake. With this done, there's no need to lock for access to nSendVersion. Also, a new stream is used for each message, so there's no need to lock during the serialization process. - This takes care of accounting for optimistic sends, so the nOptimisticBytesWritten hack can be removed. - -dropmessagestest and -fuzzmessagestest have not been preserved, as I suspect they haven't been used in years. * net: switch all callers to connman for pushing messages Drop all of the old stuff. * drop the optimistic write counter hack This is now handled properly in realtime. * net: remove now-unused ssSend and Fuzz * net: construct CNodeStates in place * net: handle version push in InitializeNode
1 parent 8b7dffb commit b621cfb

18 files changed

+363
-452
lines changed

src/alert.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ bool CAlert::AppliesToMe() const
125125
return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
126126
}
127127

128-
bool CAlert::RelayTo(CNode* pnode) const
128+
bool CAlert::RelayTo(CNode* pnode, CConnman& connman) const
129129
{
130130
if (!IsInEffect())
131131
return false;
@@ -139,7 +139,7 @@ bool CAlert::RelayTo(CNode* pnode) const
139139
AppliesToMe() ||
140140
GetAdjustedTime() < nRelayUntil)
141141
{
142-
pnode->PushMessage(NetMsgType::ALERT, *this);
142+
connman.PushMessage(pnode, NetMsgType::ALERT, *this);
143143
return true;
144144
}
145145
}

src/alert.h

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

1717
class CAlert;
1818
class CNode;
19+
class CConnman;
1920
class uint256;
2021

2122
extern std::map<uint256, CAlert> mapAlerts;
@@ -99,7 +100,7 @@ class CAlert : public CUnsignedAlert
99100
bool Cancels(const CAlert& alert) const;
100101
bool AppliesTo(int nVersion, const std::string& strSubVerIn) const;
101102
bool AppliesToMe() const;
102-
bool RelayTo(CNode* pnode) const;
103+
bool RelayTo(CNode* pnode, CConnman& connman) const;
103104
bool Sign();
104105
bool CheckSignature(const std::vector<unsigned char>& alertKey) const;
105106
bool ProcessAlert(const std::vector<unsigned char>& alertKey, bool fThread = true); // fThread means run -alertnotify in a free-running thread

src/governance.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,8 @@ void CGovernanceManager::Sync(CNode* pfrom, const uint256& nProp, const CBloomFi
834834
}
835835
}
836836

837-
pfrom->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_GOVOBJ, nObjCount);
838-
pfrom->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_GOVOBJ_VOTE, nVoteCount);
837+
g_connman->PushMessage(pfrom, NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_GOVOBJ, nObjCount);
838+
g_connman->PushMessage(pfrom, NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_GOVOBJ_VOTE, nVoteCount);
839839
LogPrintf("CGovernanceManager::Sync -- sent %d objects and %d votes to peer=%d\n", nObjCount, nVoteCount, pfrom->id);
840840
}
841841

@@ -1147,7 +1147,7 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
11471147
LogPrint("gobject", "CGovernanceObject::RequestGovernanceObject -- hash = %s (peer=%d)\n", nHash.ToString(), pfrom->GetId());
11481148

11491149
if(pfrom->nVersion < GOVERNANCE_FILTER_PROTO_VERSION) {
1150-
pfrom->PushMessage(NetMsgType::MNGOVERNANCESYNC, nHash);
1150+
g_connman->PushMessage(pfrom, NetMsgType::MNGOVERNANCESYNC, nHash);
11511151
return;
11521152
}
11531153

@@ -1170,7 +1170,7 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
11701170
}
11711171

11721172
LogPrint("gobject", "CGovernanceManager::RequestGovernanceObject -- nHash %s nVoteCount %d peer=%d\n", nHash.ToString(), nVoteCount, pfrom->id);
1173-
pfrom->PushMessage(NetMsgType::MNGOVERNANCESYNC, nHash, filter);
1173+
g_connman->PushMessage(pfrom, NetMsgType::MNGOVERNANCESYNC, nHash, filter);
11741174
}
11751175

11761176
int CGovernanceManager::RequestGovernanceObjectVotes(CNode* pnode)

src/main.cpp

Lines changed: 85 additions & 59 deletions
Large diffs are not rendered by default.

src/masternode-payments.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ void CMasternodePayments::Sync(CNode* pnode)
842842
}
843843

844844
LogPrintf("CMasternodePayments::Sync -- Sent %d votes to peer %d\n", nInvCount, pnode->id);
845-
pnode->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_MNW, nInvCount);
845+
g_connman->PushMessage(pnode, NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_MNW, nInvCount);
846846
}
847847

848848
// Request low data/unknown payment blocks in batches directly from some node instead of/after preliminary Sync.
@@ -864,7 +864,7 @@ void CMasternodePayments::RequestLowDataPaymentBlocks(CNode* pnode)
864864
// We should not violate GETDATA rules
865865
if(vToFetch.size() == MAX_INV_SZ) {
866866
LogPrintf("CMasternodePayments::SyncLowDataPaymentBlocks -- asking peer %d for %d blocks\n", pnode->id, MAX_INV_SZ);
867-
pnode->PushMessage(NetMsgType::GETDATA, vToFetch);
867+
g_connman->PushMessage(pnode, NetMsgType::GETDATA, vToFetch);
868868
// Start filling new batch
869869
vToFetch.clear();
870870
}
@@ -912,7 +912,7 @@ void CMasternodePayments::RequestLowDataPaymentBlocks(CNode* pnode)
912912
// We should not violate GETDATA rules
913913
if(vToFetch.size() == MAX_INV_SZ) {
914914
LogPrintf("CMasternodePayments::SyncLowDataPaymentBlocks -- asking peer %d for %d payment blocks\n", pnode->id, MAX_INV_SZ);
915-
pnode->PushMessage(NetMsgType::GETDATA, vToFetch);
915+
g_connman->PushMessage(pnode, NetMsgType::GETDATA, vToFetch);
916916
// Start filling new batch
917917
vToFetch.clear();
918918
}
@@ -921,7 +921,7 @@ void CMasternodePayments::RequestLowDataPaymentBlocks(CNode* pnode)
921921
// Ask for the rest of it
922922
if(!vToFetch.empty()) {
923923
LogPrintf("CMasternodePayments::SyncLowDataPaymentBlocks -- asking peer %d for %d payment blocks\n", pnode->id, vToFetch.size());
924-
pnode->PushMessage(NetMsgType::GETDATA, vToFetch);
924+
g_connman->PushMessage(pnode, NetMsgType::GETDATA, vToFetch);
925925
}
926926
}
927927

src/masternode-sync.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,12 @@ void CMasternodeSync::ProcessTick()
324324
if(Params().NetworkIDString() == CBaseChainParams::REGTEST)
325325
{
326326
if(nRequestedMasternodeAttempt <= 2) {
327-
pnode->PushMessage(NetMsgType::GETSPORKS); //get current network sporks
327+
g_connman->PushMessageWithVersion(pnode, INIT_PROTO_VERSION, NetMsgType::GETSPORKS); //get current network sporks
328328
} else if(nRequestedMasternodeAttempt < 4) {
329329
mnodeman.DsegUpdate(pnode);
330330
} else if(nRequestedMasternodeAttempt < 6) {
331331
int nMnCount = mnodeman.CountMasternodes();
332-
pnode->PushMessage(NetMsgType::MASTERNODEPAYMENTSYNC, nMnCount); //sync payment votes
332+
g_connman->PushMessage(pnode, NetMsgType::MASTERNODEPAYMENTSYNC, nMnCount); //sync payment votes
333333
SendGovernanceSyncRequest(pnode);
334334
} else {
335335
nRequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED;
@@ -355,7 +355,7 @@ void CMasternodeSync::ProcessTick()
355355
// only request once from each peer
356356
netfulfilledman.AddFulfilledRequest(pnode->addr, "spork-sync");
357357
// get current network sporks
358-
pnode->PushMessage(NetMsgType::GETSPORKS);
358+
g_connman->PushMessageWithVersion(pnode, INIT_PROTO_VERSION, NetMsgType::GETSPORKS);
359359
LogPrintf("CMasternodeSync::ProcessTick -- nTick %d nRequestedMasternodeAssets %d -- requesting sporks from peer %d\n", nTick, nRequestedMasternodeAssets, pnode->id);
360360
continue; // always get sporks first, switch to the next node without waiting for the next tick
361361
}
@@ -431,7 +431,7 @@ void CMasternodeSync::ProcessTick()
431431
nRequestedMasternodeAttempt++;
432432

433433
// ask node for all payment votes it has (new nodes will only return votes for future payments)
434-
pnode->PushMessage(NetMsgType::MASTERNODEPAYMENTSYNC, mnpayments.GetStorageLimit());
434+
g_connman->PushMessage(pnode, NetMsgType::MASTERNODEPAYMENTSYNC, mnpayments.GetStorageLimit());
435435
// ask node for missing pieces only (old nodes will not be asked)
436436
mnpayments.RequestLowDataPaymentBlocks(pnode);
437437

@@ -511,10 +511,10 @@ void CMasternodeSync::SendGovernanceSyncRequest(CNode* pnode)
511511
CBloomFilter filter;
512512
filter.clear();
513513

514-
pnode->PushMessage(NetMsgType::MNGOVERNANCESYNC, uint256(), filter);
514+
g_connman->PushMessage(pnode, NetMsgType::MNGOVERNANCESYNC, uint256(), filter);
515515
}
516516
else {
517-
pnode->PushMessage(NetMsgType::MNGOVERNANCESYNC, uint256());
517+
g_connman->PushMessage(pnode, NetMsgType::MNGOVERNANCESYNC, uint256());
518518
}
519519
}
520520

src/masternodeman.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void CMasternodeMan::AskForMN(CNode* pnode, const CTxIn &vin)
162162
}
163163
mWeAskedForMasternodeListEntry[vin.prevout][pnode->addr] = GetTime() + DSEG_UPDATE_SECONDS;
164164

165-
pnode->PushMessage(NetMsgType::DSEG, vin);
165+
g_connman->PushMessage(pnode, NetMsgType::DSEG, vin);
166166
}
167167

168168
void CMasternodeMan::Check()
@@ -438,7 +438,7 @@ void CMasternodeMan::DsegUpdate(CNode* pnode)
438438
}
439439
}
440440

441-
pnode->PushMessage(NetMsgType::DSEG, CTxIn());
441+
g_connman->PushMessage(pnode, NetMsgType::DSEG, CTxIn());
442442
int64_t askAgain = GetTime() + DSEG_UPDATE_SECONDS;
443443
mWeAskedForMasternodeList[pnode->addr] = askAgain;
444444

@@ -926,7 +926,7 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
926926
}
927927

928928
if(vin == CTxIn()) {
929-
pfrom->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_LIST, nInvCount);
929+
g_connman->PushMessage(pfrom, NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_LIST, nInvCount);
930930
LogPrintf("DSEG -- Sent %d Masternode invs to peer %d\n", nInvCount, pfrom->id);
931931
return;
932932
}
@@ -1109,7 +1109,7 @@ bool CMasternodeMan::SendVerifyRequest(const CAddress& addr, const std::vector<C
11091109
CMasternodeVerification mnv(addr, GetRandInt(999999), pCurrentBlockIndex->nHeight - 1);
11101110
mWeAskedForVerification[addr] = mnv;
11111111
LogPrintf("CMasternodeMan::SendVerifyRequest -- verifying node using nonce %d addr=%s\n", mnv.nonce, addr.ToString());
1112-
pnode->PushMessage(NetMsgType::MNVERIFY, mnv);
1112+
g_connman->PushMessage(pnode, NetMsgType::MNVERIFY, mnv);
11131113

11141114
return true;
11151115
}
@@ -1150,7 +1150,7 @@ void CMasternodeMan::SendVerifyReply(CNode* pnode, CMasternodeVerification& mnv)
11501150
return;
11511151
}
11521152

1153-
pnode->PushMessage(NetMsgType::MNVERIFY, mnv);
1153+
g_connman->PushMessage(pnode, NetMsgType::MNVERIFY, mnv);
11541154
netfulfilledman.AddFulfilledRequest(pnode->addr, strprintf("%s", NetMsgType::MNVERIFY)+"-reply");
11551155
}
11561156

0 commit comments

Comments
 (0)