Skip to content

Commit 8be75fd

Browse files
w0xlthebasto
andcommitted
p2p: add assertions and negative TS annotations for m_total_bytes_sent_mutex
Co-authored-by: Hennadii Stepanov <[email protected]>
1 parent a237a06 commit 8be75fd

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

src/net.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,8 @@ void CConnman::SocketEvents(const std::vector<CNode*>& nodes,
15631563

15641564
void CConnman::SocketHandler()
15651565
{
1566+
AssertLockNotHeld(m_total_bytes_sent_mutex);
1567+
15661568
std::set<SOCKET> recv_set;
15671569
std::set<SOCKET> send_set;
15681570
std::set<SOCKET> error_set;
@@ -1589,6 +1591,8 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
15891591
const std::set<SOCKET>& send_set,
15901592
const std::set<SOCKET>& error_set)
15911593
{
1594+
AssertLockNotHeld(m_total_bytes_sent_mutex);
1595+
15921596
for (CNode* pnode : nodes) {
15931597
if (interruptNet)
15941598
return;
@@ -1690,6 +1694,8 @@ void CConnman::SocketHandlerListening(const std::set<SOCKET>& recv_set)
16901694

16911695
void CConnman::ThreadSocketHandler()
16921696
{
1697+
AssertLockNotHeld(m_total_bytes_sent_mutex);
1698+
16931699
SetSyscallSandboxPolicy(SyscallSandboxPolicy::NET);
16941700
while (!interruptNet)
16951701
{
@@ -2561,6 +2567,7 @@ bool CConnman::InitBinds(const Options& options)
25612567

25622568
bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
25632569
{
2570+
AssertLockNotHeld(m_total_bytes_sent_mutex);
25642571
Init(connOptions);
25652572

25662573
if (fListen && !InitBinds(connOptions)) {
@@ -2913,7 +2920,9 @@ void CConnman::RecordBytesRecv(uint64_t bytes)
29132920

29142921
void CConnman::RecordBytesSent(uint64_t bytes)
29152922
{
2923+
AssertLockNotHeld(m_total_bytes_sent_mutex);
29162924
LOCK(m_total_bytes_sent_mutex);
2925+
29172926
nTotalBytesSent += bytes;
29182927

29192928
const auto now = GetTime<std::chrono::seconds>();
@@ -2929,6 +2938,7 @@ void CConnman::RecordBytesSent(uint64_t bytes)
29292938

29302939
uint64_t CConnman::GetMaxOutboundTarget() const
29312940
{
2941+
AssertLockNotHeld(m_total_bytes_sent_mutex);
29322942
LOCK(m_total_bytes_sent_mutex);
29332943
return nMaxOutboundLimit;
29342944
}
@@ -2940,7 +2950,15 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeframe() const
29402950

29412951
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
29422952
{
2953+
AssertLockNotHeld(m_total_bytes_sent_mutex);
29432954
LOCK(m_total_bytes_sent_mutex);
2955+
return GetMaxOutboundTimeLeftInCycle_();
2956+
}
2957+
2958+
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle_() const
2959+
{
2960+
AssertLockHeld(m_total_bytes_sent_mutex);
2961+
29442962
if (nMaxOutboundLimit == 0)
29452963
return 0s;
29462964

@@ -2954,14 +2972,15 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() const
29542972

29552973
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
29562974
{
2975+
AssertLockNotHeld(m_total_bytes_sent_mutex);
29572976
LOCK(m_total_bytes_sent_mutex);
29582977
if (nMaxOutboundLimit == 0)
29592978
return false;
29602979

29612980
if (historicalBlockServingLimit)
29622981
{
29632982
// keep a large enough buffer to at least relay each block once
2964-
const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2983+
const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle_();
29652984
const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MAX_BLOCK_SERIALIZED_SIZE;
29662985
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
29672986
return true;
@@ -2974,6 +2993,7 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
29742993

29752994
uint64_t CConnman::GetOutboundTargetBytesLeft() const
29762995
{
2996+
AssertLockNotHeld(m_total_bytes_sent_mutex);
29772997
LOCK(m_total_bytes_sent_mutex);
29782998
if (nMaxOutboundLimit == 0)
29792999
return 0;
@@ -2988,6 +3008,7 @@ uint64_t CConnman::GetTotalBytesRecv() const
29883008

29893009
uint64_t CConnman::GetTotalBytesSent() const
29903010
{
3011+
AssertLockNotHeld(m_total_bytes_sent_mutex);
29913012
LOCK(m_total_bytes_sent_mutex);
29923013
return nTotalBytesSent;
29933014
}
@@ -3035,6 +3056,7 @@ bool CConnman::NodeFullyConnected(const CNode* pnode)
30353056

30363057
void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
30373058
{
3059+
AssertLockNotHeld(m_total_bytes_sent_mutex);
30383060
size_t nMessageSize = msg.data.size();
30393061
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", msg.m_type, nMessageSize, pnode->GetId());
30403062
if (gArgs.GetBoolArg("-capturemessages", false)) {

src/net.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,10 @@ class CConnman
760760
bool m_i2p_accept_incoming;
761761
};
762762

763-
void Init(const Options& connOptions) {
763+
void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex)
764+
{
765+
AssertLockNotHeld(m_total_bytes_sent_mutex);
766+
764767
nLocalServices = connOptions.nLocalServices;
765768
nMaxConnections = connOptions.nMaxConnections;
766769
m_max_outbound_full_relay = std::min(connOptions.m_max_outbound_full_relay, connOptions.nMaxConnections);
@@ -789,7 +792,7 @@ class CConnman
789792

790793
CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, bool network_active = true);
791794
~CConnman();
792-
bool Start(CScheduler& scheduler, const Options& options);
795+
bool Start(CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
793796

794797
void StopThreads();
795798
void StopNodes();
@@ -808,7 +811,7 @@ class CConnman
808811

809812
bool ForNode(NodeId id, std::function<bool(CNode* pnode)> func);
810813

811-
void PushMessage(CNode* pnode, CSerializedNetMsg&& msg);
814+
void PushMessage(CNode* pnode, CSerializedNetMsg&& msg) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
812815

813816
using NodeFn = std::function<void(CNode*)>;
814817
void ForEachNode(const NodeFn& func)
@@ -899,24 +902,22 @@ class CConnman
899902
//! that peer during `net_processing.cpp:PushNodeVersion()`.
900903
ServiceFlags GetLocalServices() const;
901904

902-
uint64_t GetMaxOutboundTarget() const;
905+
uint64_t GetMaxOutboundTarget() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
903906
std::chrono::seconds GetMaxOutboundTimeframe() const;
904907

905908
//! check if the outbound target is reached
906909
//! if param historicalBlockServingLimit is set true, the function will
907910
//! response true if the limit for serving historical blocks has been reached
908-
bool OutboundTargetReached(bool historicalBlockServingLimit) const;
911+
bool OutboundTargetReached(bool historicalBlockServingLimit) const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
909912

910913
//! response the bytes left in the current max outbound cycle
911914
//! in case of no limit, it will always response 0
912-
uint64_t GetOutboundTargetBytesLeft() const;
915+
uint64_t GetOutboundTargetBytesLeft() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
913916

914-
//! returns the time left in the current max outbound cycle
915-
//! in case of no limit, it will always return 0
916-
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const;
917+
std::chrono::seconds GetMaxOutboundTimeLeftInCycle() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
917918

918919
uint64_t GetTotalBytesRecv() const;
919-
uint64_t GetTotalBytesSent() const;
920+
uint64_t GetTotalBytesSent() const EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
920921

921922
/** Get a unique deterministic randomizer. */
922923
CSipHasher GetDeterministicRandomizer(uint64_t id) const;
@@ -942,6 +943,10 @@ class CConnman
942943
NetPermissionFlags m_permissions;
943944
};
944945

946+
//! returns the time left in the current max outbound cycle
947+
//! in case of no limit, it will always return 0
948+
std::chrono::seconds GetMaxOutboundTimeLeftInCycle_() const EXCLUSIVE_LOCKS_REQUIRED(m_total_bytes_sent_mutex);
949+
945950
bool BindListenPort(const CService& bindAddr, bilingual_str& strError, NetPermissionFlags permissions);
946951
bool Bind(const CService& addr, unsigned int flags, NetPermissionFlags permissions);
947952
bool InitBinds(const Options& options);
@@ -1001,7 +1006,7 @@ class CConnman
10011006
/**
10021007
* Check connected and listening sockets for IO readiness and process them accordingly.
10031008
*/
1004-
void SocketHandler();
1009+
void SocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
10051010

10061011
/**
10071012
* Do the read/write for connected sockets that are ready for IO.
@@ -1014,15 +1019,16 @@ class CConnman
10141019
void SocketHandlerConnected(const std::vector<CNode*>& nodes,
10151020
const std::set<SOCKET>& recv_set,
10161021
const std::set<SOCKET>& send_set,
1017-
const std::set<SOCKET>& error_set);
1022+
const std::set<SOCKET>& error_set)
1023+
EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
10181024

10191025
/**
10201026
* Accept incoming connections, one from each read-ready listening socket.
10211027
* @param[in] recv_set Sockets that are ready for read.
10221028
*/
10231029
void SocketHandlerListening(const std::set<SOCKET>& recv_set);
10241030

1025-
void ThreadSocketHandler();
1031+
void ThreadSocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
10261032
void ThreadDNSAddressSeed();
10271033

10281034
uint64_t CalculateKeyedNetGroup(const CAddress& ad) const;
@@ -1051,7 +1057,7 @@ class CConnman
10511057

10521058
// Network stats
10531059
void RecordBytesRecv(uint64_t bytes);
1054-
void RecordBytesSent(uint64_t bytes);
1060+
void RecordBytesSent(uint64_t bytes) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
10551061

10561062
/**
10571063
* Return vector of current BLOCK_RELAY peers.

0 commit comments

Comments
 (0)