Skip to content

Commit cfda740

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25174: net/net_processing: Add thread safety related annotations for CNode and Peer
9816dc9 net: note CNode members that are treated as const (Anthony Towns) ef26f2f net: mark CNode unique_ptr members as const (Anthony Towns) bbec32c net: mark TransportSerializer/m_serializer as const (Anthony Towns) 06ebdc8 net/net_processing: add missing thread safety annotations (Anthony Towns) Pull request description: Adds `GUARDED_BY` and `const` annotations to document how we currently ensure various members of `CNode` and `Peer` aren't subject to race conditions. ACKs for top commit: MarcoFalke: review ACK 9816dc9 📍 jonatack: utACK 9816dc9 hebasto: ACK 9816dc9, I have reviewed the code and it looks OK. In particular, I verified the usage of variables which got `GUARDED_BY` annotations. Tree-SHA512: fa95bca72435d79caadc736ee7687e505dbe8fbdb20690809e97666664a8d0dea39a7d17cf16f0437d7f5746b9ad98a466b26325d2913252c5d2b520b384b785
2 parents e191fac + 9816dc9 commit cfda740

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/net.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,8 @@ CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds
797797
return msg;
798798
}
799799

800-
void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) {
800+
void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const
801+
{
801802
// create dbl-sha256 checksum
802803
uint256 hash = Hash(msg.data);
803804

@@ -2722,7 +2723,9 @@ CNode::CNode(NodeId idIn,
27222723
ConnectionType conn_type_in,
27232724
bool inbound_onion,
27242725
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session)
2725-
: m_sock{sock},
2726+
: m_deserializer{std::make_unique<V1TransportDeserializer>(V1TransportDeserializer(Params(), idIn, SER_NETWORK, INIT_PROTO_VERSION))},
2727+
m_serializer{std::make_unique<V1TransportSerializer>(V1TransportSerializer())},
2728+
m_sock{sock},
27262729
m_connected{GetTime<std::chrono::seconds>()},
27272730
addr{addrIn},
27282731
addrBind{addrBindIn},
@@ -2745,9 +2748,6 @@ CNode::CNode(NodeId idIn,
27452748
} else {
27462749
LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
27472750
}
2748-
2749-
m_deserializer = std::make_unique<V1TransportDeserializer>(V1TransportDeserializer(Params(), id, SER_NETWORK, INIT_PROTO_VERSION));
2750-
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer());
27512751
}
27522752

27532753
bool CConnman::NodeFullyConnected(const CNode* pnode)

src/net.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,13 @@ class V1TransportDeserializer final : public TransportDeserializer
325325
class TransportSerializer {
326326
public:
327327
// prepare message for transport (header construction, error-correction computation, payload encryption, etc.)
328-
virtual void prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) = 0;
328+
virtual void prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const = 0;
329329
virtual ~TransportSerializer() {}
330330
};
331331

332-
class V1TransportSerializer : public TransportSerializer {
332+
class V1TransportSerializer : public TransportSerializer {
333333
public:
334-
void prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) override;
334+
void prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) const override;
335335
};
336336

337337
/** Information about a peer */
@@ -341,10 +341,10 @@ class CNode
341341
friend struct ConnmanTestMsg;
342342

343343
public:
344-
std::unique_ptr<TransportDeserializer> m_deserializer;
345-
std::unique_ptr<TransportSerializer> m_serializer;
344+
const std::unique_ptr<TransportDeserializer> m_deserializer; // Used only by SocketHandler thread
345+
const std::unique_ptr<const TransportSerializer> m_serializer;
346346

347-
NetPermissionFlags m_permissionFlags{NetPermissionFlags::None};
347+
NetPermissionFlags m_permissionFlags{NetPermissionFlags::None}; // treated as const outside of fuzz tester
348348

349349
/**
350350
* Socket used for communication with the node.
@@ -368,7 +368,7 @@ class CNode
368368

369369
RecursiveMutex cs_vProcessMsg;
370370
std::list<CNetMessage> vProcessMsg GUARDED_BY(cs_vProcessMsg);
371-
size_t nProcessQueueSize{0};
371+
size_t nProcessQueueSize GUARDED_BY(cs_vProcessMsg){0};
372372

373373
RecursiveMutex cs_sendProcessing;
374374

@@ -393,7 +393,7 @@ class CNode
393393
* from the wire. This cleaned string can safely be logged or displayed.
394394
*/
395395
std::string cleanSubVer GUARDED_BY(m_subver_mutex){};
396-
bool m_prefer_evict{false}; // This peer is preferred for eviction.
396+
bool m_prefer_evict{false}; // This peer is preferred for eviction. (treated as const)
397397
bool HasPermission(NetPermissionFlags permission) const {
398398
return NetPermissions::HasFlag(m_permissionFlags, permission);
399399
}

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ struct Peer {
289289
* non-wtxid-relay peers, wtxid for wtxid-relay peers). We use the
290290
* mempool to sort transactions in dependency order before relay, so
291291
* this does not have to be sorted. */
292-
std::set<uint256> m_tx_inventory_to_send;
292+
std::set<uint256> m_tx_inventory_to_send GUARDED_BY(m_tx_inventory_mutex);
293293
/** Whether the peer has requested us to send our complete mempool. Only
294294
* permitted if the peer has NetPermissionFlags::Mempool. See BIP35. */
295295
bool m_send_mempool GUARDED_BY(m_tx_inventory_mutex){false};
@@ -648,7 +648,7 @@ class PeerManagerImpl final : public PeerManager
648648
std::atomic<int> m_best_height{-1};
649649

650650
/** Next time to check for stale tip */
651-
std::chrono::seconds m_stale_tip_check_time{0s};
651+
std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
652652

653653
/** Whether this node is running in -blocksonly mode */
654654
const bool m_ignore_incoming_txs;
@@ -657,7 +657,7 @@ class PeerManagerImpl final : public PeerManager
657657

658658
/** Whether we've completed initial sync yet, for determining when to turn
659659
* on extra block-relay-only peers. */
660-
bool m_initial_sync_finished{false};
660+
bool m_initial_sync_finished GUARDED_BY(cs_main){false};
661661

662662
/** Protects m_peer_map. This mutex must not be locked while holding a lock
663663
* on any of the mutexes inside a Peer object. */

0 commit comments

Comments
 (0)