Skip to content

Commit d254f94

Browse files
committed
Merge bitcoin/bitcoin#27324: net: #27257 follow-ups
cd0c8ee [net] Pass nRecvFloodSize to CNode (dergoegge) 860402e [net] Remove trivial GetConnectionType() getter (dergoegge) b5a85b3 [net] Delete CNetMessage copy constructor/assignment op (dergoegge) Pull request description: Follow-up PR for #27257 * Deletes the copy constructor/assignment operator of `CNetMessage` * Removes trivial getter for the connection type * Avoids passing `nRecvFloodSize` to CNode methods by passing it to `CNode` on creation ACKs for top commit: jnewbery: utACK cd0c8ee theStack: ACK cd0c8ee Tree-SHA512: 673a758668617f69fba77e61f0eaa1538da27a4849c82c98742436692baa2d7f001129af3e7a66b160e599d12109dac08137a146f10ff9b9ebdc5c2237311d41
2 parents 86e7410 + cd0c8ee commit d254f94

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

src/net.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,10 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
573573
pszDest ? pszDest : "",
574574
conn_type,
575575
/*inbound_onion=*/false,
576-
CNodeOptions{ .i2p_sam_session = std::move(i2p_transient_session) });
576+
CNodeOptions{
577+
.i2p_sam_session = std::move(i2p_transient_session),
578+
.recv_flood_size = nReceiveFloodSize,
579+
});
577580
pnode->AddRef();
578581

579582
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -917,7 +920,7 @@ bool CConnman::AttemptToEvictConnection()
917920
.m_is_local = node->addr.IsLocal(),
918921
.m_network = node->ConnectedThroughNetwork(),
919922
.m_noban = node->HasPermission(NetPermissionFlags::NoBan),
920-
.m_conn_type = node->GetConnectionType(),
923+
.m_conn_type = node->m_conn_type,
921924
};
922925
vEvictionCandidates.push_back(candidate);
923926
}
@@ -1051,8 +1054,9 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
10511054
ConnectionType::INBOUND,
10521055
inbound_onion,
10531056
CNodeOptions{
1054-
.permission_flags = permission_flags,
1055-
.prefer_evict = discouraged,
1057+
.permission_flags = permission_flags,
1058+
.prefer_evict = discouraged,
1059+
.recv_flood_size = nReceiveFloodSize,
10561060
});
10571061
pnode->AddRef();
10581062
m_msgproc->InitializeNode(*pnode, nodeServices);
@@ -1092,7 +1096,7 @@ bool CConnman::AddConnection(const std::string& address, ConnectionType conn_typ
10921096

10931097
// Count existing connections
10941098
int existing_connections = WITH_LOCK(m_nodes_mutex,
1095-
return std::count_if(m_nodes.begin(), m_nodes.end(), [conn_type](CNode* node) { return node->GetConnectionType() == conn_type; }););
1099+
return std::count_if(m_nodes.begin(), m_nodes.end(), [conn_type](CNode* node) { return node->m_conn_type == conn_type; }););
10961100

10971101
// Max connections of specified type already exist
10981102
if (max_connections != std::nullopt && existing_connections >= max_connections) return false;
@@ -1328,7 +1332,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
13281332
}
13291333
RecordBytesRecv(nBytes);
13301334
if (notify) {
1331-
pnode->MarkReceivedMsgsForProcessing(nReceiveFloodSize);
1335+
pnode->MarkReceivedMsgsForProcessing();
13321336
WakeMessageHandler();
13331337
}
13341338
}
@@ -1711,7 +1715,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17111715
if (pnode->IsBlockOnlyConn()) nOutboundBlockRelay++;
17121716

17131717
// Make sure our persistent outbound slots belong to different netgroups.
1714-
switch (pnode->GetConnectionType()) {
1718+
switch (pnode->m_conn_type) {
17151719
// We currently don't take inbound connections into account. Since they are
17161720
// free to make, an attacker could make them to prevent us from connecting to
17171721
// certain peers.
@@ -2754,8 +2758,6 @@ ServiceFlags CConnman::GetLocalServices() const
27542758
return nLocalServices;
27552759
}
27562760

2757-
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2758-
27592761
CNode::CNode(NodeId idIn,
27602762
std::shared_ptr<Sock> sock,
27612763
const CAddress& addrIn,
@@ -2777,9 +2779,10 @@ CNode::CNode(NodeId idIn,
27772779
m_inbound_onion{inbound_onion},
27782780
m_prefer_evict{node_opts.prefer_evict},
27792781
nKeyedNetGroup{nKeyedNetGroupIn},
2782+
m_conn_type{conn_type_in},
27802783
id{idIn},
27812784
nLocalHostNonce{nLocalHostNonceIn},
2782-
m_conn_type{conn_type_in},
2785+
m_recv_flood_size{node_opts.recv_flood_size},
27832786
m_i2p_sam_session{std::move(node_opts.i2p_sam_session)}
27842787
{
27852788
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
@@ -2795,7 +2798,7 @@ CNode::CNode(NodeId idIn,
27952798
}
27962799
}
27972800

2798-
void CNode::MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
2801+
void CNode::MarkReceivedMsgsForProcessing()
27992802
{
28002803
AssertLockNotHeld(m_msg_process_queue_mutex);
28012804

@@ -2809,10 +2812,10 @@ void CNode::MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
28092812
LOCK(m_msg_process_queue_mutex);
28102813
m_msg_process_queue.splice(m_msg_process_queue.end(), vRecvMsg);
28112814
m_msg_process_queue_size += nSizeAdded;
2812-
fPauseRecv = m_msg_process_queue_size > recv_flood_size;
2815+
fPauseRecv = m_msg_process_queue_size > m_recv_flood_size;
28132816
}
28142817

2815-
std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage(size_t recv_flood_size)
2818+
std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage()
28162819
{
28172820
LOCK(m_msg_process_queue_mutex);
28182821
if (m_msg_process_queue.empty()) return std::nullopt;
@@ -2821,7 +2824,7 @@ std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage(size_t recv_flood
28212824
// Just take one message
28222825
msgs.splice(msgs.begin(), m_msg_process_queue, m_msg_process_queue.begin());
28232826
m_msg_process_queue_size -= msgs.front().m_raw_message_size;
2824-
fPauseRecv = m_msg_process_queue_size > recv_flood_size;
2827+
fPauseRecv = m_msg_process_queue_size > m_recv_flood_size;
28252828

28262829
return std::make_pair(std::move(msgs.front()), !m_msg_process_queue.empty());
28272830
}

src/net.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ class CNetMessage {
236236
std::string m_type;
237237

238238
CNetMessage(CDataStream&& recv_in) : m_recv(std::move(recv_in)) {}
239+
// Only one CNetMessage object will exist for the same message on either
240+
// the receive or processing queue. For performance reasons we therefore
241+
// delete the copy constructor and assignment operator to avoid the
242+
// possibility of copying CNetMessage objects.
243+
CNetMessage(CNetMessage&&) = default;
244+
CNetMessage(const CNetMessage&) = delete;
245+
CNetMessage& operator=(CNetMessage&&) = default;
246+
CNetMessage& operator=(const CNetMessage&) = delete;
239247

240248
void SetVersion(int nVersionIn)
241249
{
@@ -342,6 +350,7 @@ struct CNodeOptions
342350
NetPermissionFlags permission_flags = NetPermissionFlags::None;
343351
std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
344352
bool prefer_evict = false;
353+
size_t recv_flood_size{DEFAULT_MAXRECEIVEBUFFER * 1000};
345354
};
346355

347356
/** Information about a peer */
@@ -410,21 +419,18 @@ class CNode
410419
std::atomic_bool fPauseRecv{false};
411420
std::atomic_bool fPauseSend{false};
412421

413-
const ConnectionType& GetConnectionType() const
414-
{
415-
return m_conn_type;
416-
}
422+
const ConnectionType m_conn_type;
417423

418424
/** Move all messages from the received queue to the processing queue. */
419-
void MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
425+
void MarkReceivedMsgsForProcessing()
420426
EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex);
421427

422428
/** Poll the next message from the processing queue of this connection.
423429
*
424430
* Returns std::nullopt if the processing queue is empty, or a pair
425431
* consisting of the message and a bool that indicates if the processing
426432
* queue has more entries. */
427-
std::optional<std::pair<CNetMessage, bool>> PollMessage(size_t recv_flood_size)
433+
std::optional<std::pair<CNetMessage, bool>> PollMessage()
428434
EXCLUSIVE_LOCKS_REQUIRED(!m_msg_process_queue_mutex);
429435

430436
/** Account for the total size of a sent message in the per msg type connection stats. */
@@ -614,9 +620,9 @@ class CNode
614620
private:
615621
const NodeId id;
616622
const uint64_t nLocalHostNonce;
617-
const ConnectionType m_conn_type;
618623
std::atomic<int> m_greatest_common_version{INIT_PROTO_VERSION};
619624

625+
const size_t m_recv_flood_size;
620626
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread
621627

622628
Mutex m_msg_process_queue_mutex;
@@ -879,8 +885,6 @@ class CConnman
879885
/** Get a unique deterministic randomizer. */
880886
CSipHasher GetDeterministicRandomizer(uint64_t id) const;
881887

882-
unsigned int GetReceiveFloodSize() const;
883-
884888
void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
885889

886890
/** Return true if we should disconnect the peer for failing an inactivity check. */

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4887,7 +4887,7 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
48874887
// Don't bother if send buffer is too full to respond anyway
48884888
if (pfrom->fPauseSend) return false;
48894889

4890-
auto poll_result{pfrom->PollMessage(m_connman.GetReceiveFloodSize())};
4890+
auto poll_result{pfrom->PollMessage()};
48914891
if (!poll_result) {
48924892
// No message to process
48934893
return false;

src/test/fuzz/connman.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
125125
std::vector<CNodeStats> stats;
126126
connman.GetNodeStats(stats);
127127
(void)connman.GetOutboundTargetBytesLeft();
128-
(void)connman.GetReceiveFloodSize();
129128
(void)connman.GetTotalBytesRecv();
130129
(void)connman.GetTotalBytesSent();
131130
(void)connman.GetTryNewOutboundPeer();

src/test/util/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_by
6666
{
6767
assert(node.ReceiveMsgBytes(msg_bytes, complete));
6868
if (complete) {
69-
node.MarkReceivedMsgsForProcessing(nReceiveFloodSize);
69+
node.MarkReceivedMsgsForProcessing();
7070
}
7171
}
7272

0 commit comments

Comments
 (0)