Skip to content

Commit a66a7cc

Browse files
committed
net_processing: add thread safety annotations for Peer members accessed only via the msgproc thread
1 parent bf12abe commit a66a7cc

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/net_processing.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ struct Peer {
264264
/** The feerate in the most recent BIP133 `feefilter` message sent to the peer.
265265
* It is *not* a p2p protocol violation for the peer to send us
266266
* transactions with a lower fee rate than this. See BIP133. */
267-
CAmount m_fee_filter_sent{0};
267+
CAmount m_fee_filter_sent GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
268268
/** Timestamp after which we will send the next BIP133 `feefilter` message
269269
* to the peer. */
270-
std::chrono::microseconds m_next_send_feefilter{0};
270+
std::chrono::microseconds m_next_send_feefilter GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
271271

272272
struct TxRelay {
273273
mutable RecursiveMutex m_bloom_filter_mutex;
@@ -298,7 +298,7 @@ struct Peer {
298298
std::atomic<std::chrono::seconds> m_last_mempool_req{0s};
299299
/** The next time after which we will send an `inv` message containing
300300
* transaction announcements to this peer. */
301-
std::chrono::microseconds m_next_inv_send_time{0};
301+
std::chrono::microseconds m_next_inv_send_time GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
302302

303303
/** Minimum fee rate with which to filter transaction announcements to this node. See BIP133. */
304304
std::atomic<CAmount> m_fee_filter_received{0};
@@ -319,7 +319,7 @@ struct Peer {
319319
};
320320

321321
/** A vector of addresses to send to the peer, limited to MAX_ADDR_TO_SEND. */
322-
std::vector<CAddress> m_addrs_to_send;
322+
std::vector<CAddress> m_addrs_to_send GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
323323
/** Probabilistic filter to track recent addr messages relayed with this
324324
* peer. Used to avoid relaying redundant addresses to this peer.
325325
*
@@ -329,7 +329,7 @@ struct Peer {
329329
*
330330
* Presence of this filter must correlate with m_addr_relay_enabled.
331331
**/
332-
std::unique_ptr<CRollingBloomFilter> m_addr_known;
332+
std::unique_ptr<CRollingBloomFilter> m_addr_known GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
333333
/** Whether we are participating in address relay with this connection.
334334
*
335335
* We set this bool to true for outbound peers (other than
@@ -346,7 +346,7 @@ struct Peer {
346346
* initialized.*/
347347
std::atomic_bool m_addr_relay_enabled{false};
348348
/** Whether a getaddr request to this peer is outstanding. */
349-
bool m_getaddr_sent{false};
349+
bool m_getaddr_sent GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
350350
/** Guards address sending timers. */
351351
mutable Mutex m_addr_send_times_mutex;
352352
/** Time point to send the next ADDR message to this peer. */
@@ -357,12 +357,12 @@ struct Peer {
357357
* messages, indicating a preference to receive ADDRv2 instead of ADDR ones. */
358358
std::atomic_bool m_wants_addrv2{false};
359359
/** Whether this peer has already sent us a getaddr message. */
360-
bool m_getaddr_recvd{false};
360+
bool m_getaddr_recvd GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
361361
/** Number of addresses that can be processed from this peer. Start at 1 to
362362
* permit self-announcement. */
363-
double m_addr_token_bucket{1.0};
363+
double m_addr_token_bucket GUARDED_BY(NetEventsInterface::g_msgproc_mutex){1.0};
364364
/** When m_addr_token_bucket was last updated */
365-
std::chrono::microseconds m_addr_token_timestamp{GetTime<std::chrono::microseconds>()};
365+
std::chrono::microseconds m_addr_token_timestamp GUARDED_BY(NetEventsInterface::g_msgproc_mutex){GetTime<std::chrono::microseconds>()};
366366
/** Total number of addresses that were dropped due to rate limiting. */
367367
std::atomic<uint64_t> m_addr_rate_limited{0};
368368
/** Total number of addresses that were processed (excludes rate-limited ones). */
@@ -372,15 +372,15 @@ struct Peer {
372372
std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
373373

374374
/** Whether we've sent this peer a getheaders in response to an inv prior to initial-headers-sync completing */
375-
bool m_inv_triggered_getheaders_before_sync{false};
375+
bool m_inv_triggered_getheaders_before_sync GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
376376

377377
/** Protects m_getdata_requests **/
378378
Mutex m_getdata_requests_mutex;
379379
/** Work queue of items requested by this peer **/
380380
std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
381381

382382
/** Time of the last getheaders message to this peer */
383-
NodeClock::time_point m_last_getheaders_timestamp{};
383+
NodeClock::time_point m_last_getheaders_timestamp GUARDED_BY(NetEventsInterface::g_msgproc_mutex){};
384384

385385
/** Protects m_headers_sync **/
386386
Mutex m_headers_sync_mutex;
@@ -537,7 +537,7 @@ class PeerManagerImpl final : public PeerManager
537537

538538
private:
539539
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
540-
void ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
540+
void ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_msgproc_mutex);
541541

542542
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
543543
void EvictExtraOutboundPeers(std::chrono::seconds now) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@@ -601,7 +601,7 @@ class PeerManagerImpl final : public PeerManager
601601
void ProcessHeadersMessage(CNode& pfrom, Peer& peer,
602602
std::vector<CBlockHeader>&& headers,
603603
bool via_compact_block)
604-
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_headers_presync_mutex);
604+
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
605605
/** Various helpers for headers processing, invoked by ProcessHeadersMessage() */
606606
/** Return true if headers are continuous and have valid proof-of-work (DoS points assigned on failure) */
607607
bool CheckHeadersPoW(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams, Peer& peer);
@@ -610,7 +610,7 @@ class PeerManagerImpl final : public PeerManager
610610
/** Deal with state tracking and headers sync for peers that send the
611611
* occasional non-connecting header (this can happen due to BIP 130 headers
612612
* announcements for blocks interacting with the 2hr (MAX_FUTURE_BLOCK_TIME) rule). */
613-
void HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers);
613+
void HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
614614
/** Return true if the headers connect to each other, false otherwise */
615615
bool CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const;
616616
/** Try to continue a low-work headers sync that has already begun.
@@ -633,7 +633,7 @@ class PeerManagerImpl final : public PeerManager
633633
*/
634634
bool IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom,
635635
std::vector<CBlockHeader>& headers)
636-
EXCLUSIVE_LOCKS_REQUIRED(peer.m_headers_sync_mutex, !m_headers_presync_mutex);
636+
EXCLUSIVE_LOCKS_REQUIRED(peer.m_headers_sync_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
637637
/** Check work on a headers chain to be processed, and if insufficient,
638638
* initiate our anti-DoS headers sync mechanism.
639639
*
@@ -649,7 +649,7 @@ class PeerManagerImpl final : public PeerManager
649649
bool TryLowWorkHeadersSync(Peer& peer, CNode& pfrom,
650650
const CBlockIndex* chain_start_header,
651651
std::vector<CBlockHeader>& headers)
652-
EXCLUSIVE_LOCKS_REQUIRED(!peer.m_headers_sync_mutex, !m_peer_mutex, !m_headers_presync_mutex);
652+
EXCLUSIVE_LOCKS_REQUIRED(!peer.m_headers_sync_mutex, !m_peer_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
653653

654654
/** Return true if the given header is an ancestor of
655655
* m_chainman.m_best_header or our current tip */
@@ -659,7 +659,7 @@ class PeerManagerImpl final : public PeerManager
659659
* We don't issue a getheaders message if we have a recent one outstanding.
660660
* This returns true if a getheaders is actually sent, and false otherwise.
661661
*/
662-
bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer);
662+
bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
663663
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
664664
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast);
665665
/** Update peer state based on received headers message */
@@ -683,10 +683,10 @@ class PeerManagerImpl final : public PeerManager
683683
void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
684684

685685
/** Send `addr` messages on a regular schedule. */
686-
void MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time);
686+
void MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
687687

688688
/** Send a single `sendheaders` message, after we have completed headers sync with a peer. */
689-
void MaybeSendSendHeaders(CNode& node, Peer& peer);
689+
void MaybeSendSendHeaders(CNode& node, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
690690

691691
/** Relay (gossip) an address to a few randomly chosen nodes.
692692
*
@@ -695,10 +695,10 @@ class PeerManagerImpl final : public PeerManager
695695
* @param[in] fReachable Whether the address' network is reachable. We relay unreachable
696696
* addresses less.
697697
*/
698-
void RelayAddress(NodeId originator, const CAddress& addr, bool fReachable) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
698+
void RelayAddress(NodeId originator, const CAddress& addr, bool fReachable) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
699699

700700
/** Send `feefilter` message. */
701-
void MaybeSendFeefilter(CNode& node, Peer& peer, std::chrono::microseconds current_time);
701+
void MaybeSendFeefilter(CNode& node, Peer& peer, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
702702

703703
const CChainParams& m_chainparams;
704704
CConnman& m_connman;
@@ -1010,7 +1010,10 @@ class PeerManagerImpl final : public PeerManager
10101010
* @return True if address relay is enabled with peer
10111011
* False if address relay is disallowed
10121012
*/
1013-
bool SetupAddressRelay(const CNode& node, Peer& peer);
1013+
bool SetupAddressRelay(const CNode& node, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1014+
1015+
void AddAddressKnown(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1016+
void PushAddress(Peer& peer, const CAddress& addr, FastRandomContext& insecure_rand) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
10141017
};
10151018

10161019
const CNodeState* PeerManagerImpl::State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -1036,13 +1039,13 @@ static bool IsAddrCompatible(const Peer& peer, const CAddress& addr)
10361039
return peer.m_wants_addrv2 || addr.IsAddrV1Compatible();
10371040
}
10381041

1039-
static void AddAddressKnown(Peer& peer, const CAddress& addr)
1042+
void PeerManagerImpl::AddAddressKnown(Peer& peer, const CAddress& addr)
10401043
{
10411044
assert(peer.m_addr_known);
10421045
peer.m_addr_known->insert(addr.GetKey());
10431046
}
10441047

1045-
static void PushAddress(Peer& peer, const CAddress& addr, FastRandomContext& insecure_rand)
1048+
void PeerManagerImpl::PushAddress(Peer& peer, const CAddress& addr, FastRandomContext& insecure_rand)
10461049
{
10471050
// Known checking here is only to save space from duplicates.
10481051
// Before sending, we'll filter it again for known addresses that were
@@ -5103,7 +5106,7 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros
51035106

51045107
// Remove addr records that the peer already knows about, and add new
51055108
// addrs to the m_addr_known filter on the same pass.
5106-
auto addr_already_known = [&peer](const CAddress& addr) {
5109+
auto addr_already_known = [&peer](const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) {
51075110
bool ret = peer.m_addr_known->contains(addr.GetKey());
51085111
if (!ret) peer.m_addr_known->insert(addr.GetKey());
51095112
return ret;

0 commit comments

Comments
 (0)