Skip to content

Commit d930c7f

Browse files
committed
p2p, rpc, test: address rate-limiting follow-ups
1 parent 2b06af1 commit d930c7f

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/net_processing.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static constexpr size_t MAX_ADDR_TO_SEND{1000};
161161
static constexpr double MAX_ADDR_RATE_PER_SECOND{0.1};
162162
/** The soft limit of the address processing token bucket (the regular MAX_ADDR_RATE_PER_SECOND
163163
* based increments won't go above this, but the MAX_ADDR_TO_SEND increment following GETADDR
164-
* is exempt from this limit. */
164+
* is exempt from this limit). */
165165
static constexpr size_t MAX_ADDR_PROCESSING_TOKEN_BUCKET{MAX_ADDR_TO_SEND};
166166

167167
// Internal stuff
@@ -263,14 +263,14 @@ struct Peer {
263263
std::atomic_bool m_wants_addrv2{false};
264264
/** Whether this peer has already sent us a getaddr message. */
265265
bool m_getaddr_recvd{false};
266-
/** Number of addr messages that can be processed from this peer. Start at 1 to
266+
/** Number of addresses that can be processed from this peer. Start at 1 to
267267
* permit self-announcement. */
268268
double m_addr_token_bucket{1.0};
269269
/** When m_addr_token_bucket was last updated */
270270
std::chrono::microseconds m_addr_token_timestamp{GetTime<std::chrono::microseconds>()};
271271
/** Total number of addresses that were dropped due to rate limiting. */
272272
std::atomic<uint64_t> m_addr_rate_limited{0};
273-
/** Total number of addresses that were processed (excludes rate limited ones). */
273+
/** Total number of addresses that were processed (excludes rate-limited ones). */
274274
std::atomic<uint64_t> m_addr_processed{0};
275275

276276
/** Set of txids to reconsider once their parent transactions have been accepted **/
@@ -2848,11 +2848,12 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
28482848
return;
28492849

28502850
// Apply rate limiting.
2851-
if (rate_limited) {
2852-
if (peer->m_addr_token_bucket < 1.0) {
2851+
if (peer->m_addr_token_bucket < 1.0) {
2852+
if (rate_limited) {
28532853
++num_rate_limit;
28542854
continue;
28552855
}
2856+
} else {
28562857
peer->m_addr_token_bucket -= 1.0;
28572858
}
28582859
// We only bother storing full nodes, though this may include
@@ -2880,12 +2881,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
28802881
}
28812882
peer->m_addr_processed += num_proc;
28822883
peer->m_addr_rate_limited += num_rate_limit;
2883-
LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d%s\n",
2884-
vAddr.size(),
2885-
num_proc,
2886-
num_rate_limit,
2887-
pfrom.GetId(),
2888-
fLogIPs ? ", peeraddr=" + pfrom.addr.ToString() : "");
2884+
LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d\n",
2885+
vAddr.size(), num_proc, num_rate_limit, pfrom.GetId());
28892886

28902887
m_addrman.Add(vAddrOk, pfrom.addr, 2 * 60 * 60);
28912888
if (vAddr.size() < 1000) peer->m_getaddr_sent = false;

src/rpc/net.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ static RPCHelpMan getpeerinfo()
151151
{RPCResult::Type::NUM, "n", "The heights of blocks we're currently asking from this peer"},
152152
}},
153153
{RPCResult::Type::BOOL, "addr_relay_enabled", "Whether we participate in address relay with this peer"},
154+
{RPCResult::Type::NUM, "addr_processed", "The total number of addresses processed, excluding those dropped due to rate limiting"},
155+
{RPCResult::Type::NUM, "addr_rate_limited", "The total number of addresses dropped due to rate limiting"},
154156
{RPCResult::Type::ARR, "permissions", "Any special permissions that have been granted to this peer",
155157
{
156158
{RPCResult::Type::STR, "permission_type", Join(NET_PERMISSIONS_DOC, ",\n") + ".\n"},

test/functional/p2p_addr_relay.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def blocksonly_mode_tests(self):
311311

312312
self.nodes[0].disconnect_p2ps()
313313

314-
def send_addrs_and_test_rate_limiting(self, peer, no_relay, new_addrs, total_addrs):
314+
def send_addrs_and_test_rate_limiting(self, peer, no_relay, *, new_addrs, total_addrs):
315315
"""Send an addr message and check that the number of addresses processed and rate-limited is as expected"""
316316

317317
peer.send_and_ping(self.setup_rand_addr_msg(new_addrs))
@@ -329,45 +329,45 @@ def send_addrs_and_test_rate_limiting(self, peer, no_relay, new_addrs, total_add
329329
assert_equal(addrs_rate_limited, max(0, total_addrs - peer.tokens))
330330

331331
def rate_limit_tests(self):
332-
333332
self.mocktime = int(time.time())
334333
self.restart_node(0, [])
335334
self.nodes[0].setmocktime(self.mocktime)
336335

337-
for contype, no_relay in [("outbound-full-relay", False), ("block-relay-only", True), ("inbound", False)]:
338-
self.log.info(f'Test rate limiting of addr processing for {contype} peers')
339-
if contype == "inbound":
336+
for conn_type, no_relay in [("outbound-full-relay", False), ("block-relay-only", True), ("inbound", False)]:
337+
self.log.info(f'Test rate limiting of addr processing for {conn_type} peers')
338+
if conn_type == "inbound":
340339
peer = self.nodes[0].add_p2p_connection(AddrReceiver())
341340
else:
342-
peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=0, connection_type=contype)
341+
peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=0, connection_type=conn_type)
343342

344343
# Send 600 addresses. For all but the block-relay-only peer this should result in addresses being processed.
345-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 600, 600)
344+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=600, total_addrs=600)
346345

347346
# Send 600 more addresses. For the outbound-full-relay peer (which we send a GETADDR, and thus will
348347
# process up to 1001 incoming addresses), this means more addresses will be processed.
349-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 600, 1200)
348+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=600, total_addrs=1200)
350349

351350
# Send 10 more. As we reached the processing limit for all nodes, no more addresses should be procesesd.
352-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 10, 1210)
351+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=10, total_addrs=1210)
353352

354353
# Advance the time by 100 seconds, permitting the processing of 10 more addresses.
355354
# Send 200 and verify that 10 are processed.
356355
self.mocktime += 100
357356
self.nodes[0].setmocktime(self.mocktime)
358357
peer.increment_tokens(10)
359358

360-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 200, 1410)
359+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=200, total_addrs=1410)
361360

362361
# Advance the time by 1000 seconds, permitting the processing of 100 more addresses.
363362
# Send 200 and verify that 100 are processed.
364363
self.mocktime += 1000
365364
self.nodes[0].setmocktime(self.mocktime)
366365
peer.increment_tokens(100)
367366

368-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 200, 1610)
367+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=200, total_addrs=1610)
369368

370369
self.nodes[0].disconnect_p2ps()
371370

371+
372372
if __name__ == '__main__':
373373
AddrTest().main()

0 commit comments

Comments
 (0)