Skip to content

Commit 06e909b

Browse files
committed
merge bitcoin#22604: address rate-limiting follow-ups
1 parent 60b3e08 commit 06e909b

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
@@ -181,7 +181,7 @@ static constexpr size_t MAX_ADDR_TO_SEND{1000};
181181
static constexpr double MAX_ADDR_RATE_PER_SECOND{0.1};
182182
/** The soft limit of the address processing token bucket (the regular MAX_ADDR_RATE_PER_SECOND
183183
* based increments won't go above this, but the MAX_ADDR_TO_SEND increment following GETADDR
184-
* is exempt from this limit. */
184+
* is exempt from this limit). */
185185
static constexpr size_t MAX_ADDR_PROCESSING_TOKEN_BUCKET{MAX_ADDR_TO_SEND};
186186

187187
struct COrphanTx {
@@ -302,14 +302,14 @@ struct Peer {
302302
std::atomic_bool m_wants_addrv2{false};
303303
/** Whether this peer has already sent us a getaddr message. */
304304
bool m_getaddr_recvd{false};
305-
/** Number of addr messages that can be processed from this peer. Start at 1 to
305+
/** Number of addresses that can be processed from this peer. Start at 1 to
306306
* permit self-announcement. */
307307
double m_addr_token_bucket{1.0};
308308
/** When m_addr_token_bucket was last updated */
309309
std::chrono::microseconds m_addr_token_timestamp{GetTime<std::chrono::microseconds>()};
310310
/** Total number of addresses that were dropped due to rate limiting. */
311311
std::atomic<uint64_t> m_addr_rate_limited{0};
312-
/** Total number of addresses that were processed (excludes rate limited ones). */
312+
/** Total number of addresses that were processed (excludes rate-limited ones). */
313313
std::atomic<uint64_t> m_addr_processed{0};
314314

315315
/** Set of txids to reconsider once their parent transactions have been accepted **/
@@ -3455,11 +3455,12 @@ void PeerManagerImpl::ProcessMessage(
34553455
return;
34563456

34573457
// Apply rate limiting.
3458-
if (rate_limited) {
3459-
if (peer->m_addr_token_bucket < 1.0) {
3458+
if (peer->m_addr_token_bucket < 1.0) {
3459+
if (rate_limited) {
34603460
++num_rate_limit;
34613461
continue;
34623462
}
3463+
} else {
34633464
peer->m_addr_token_bucket -= 1.0;
34643465
}
34653466
// We only bother storing full nodes, though this may include
@@ -3487,12 +3488,8 @@ void PeerManagerImpl::ProcessMessage(
34873488
}
34883489
peer->m_addr_processed += num_proc;
34893490
peer->m_addr_rate_limited += num_rate_limit;
3490-
LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d%s\n",
3491-
vAddr.size(),
3492-
num_proc,
3493-
num_rate_limit,
3494-
pfrom.GetId(),
3495-
fLogIPs ? ", peeraddr=" + pfrom.addr.ToString() : "");
3491+
LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d\n",
3492+
vAddr.size(), num_proc, num_rate_limit, pfrom.GetId());
34963493

34973494
m_addrman.Add(vAddrOk, pfrom.addr, 2 * 60 * 60);
34983495
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
@@ -146,6 +146,8 @@ static RPCHelpMan getpeerinfo()
146146
{RPCResult::Type::NUM, "n", "The heights of blocks we're currently asking from this peer"},
147147
}},
148148
{RPCResult::Type::BOOL, "addr_relay_enabled", "Whether we participate in address relay with this peer"},
149+
{RPCResult::Type::NUM, "addr_processed", "The total number of addresses processed, excluding those dropped due to rate limiting"},
150+
{RPCResult::Type::NUM, "addr_rate_limited", "The total number of addresses dropped due to rate limiting"},
149151
{RPCResult::Type::BOOL, "whitelisted", "Whether the peer is whitelisted"},
150152
{RPCResult::Type::ARR, "permissions", "Any special permissions that have been granted to this peer",
151153
{

test/functional/p2p_addr_relay.py

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

307307
self.nodes[0].disconnect_p2ps()
308308

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

312312
peer.send_and_ping(self.setup_rand_addr_msg(new_addrs))
@@ -324,41 +324,41 @@ def send_addrs_and_test_rate_limiting(self, peer, no_relay, new_addrs, total_add
324324
assert_equal(addrs_rate_limited, max(0, total_addrs - peer.tokens))
325325

326326
def rate_limit_tests(self):
327-
328327
self.restart_node(0, [])
329328

330-
for contype, no_relay in [("outbound-full-relay", False), ("block-relay-only", True), ("inbound", False)]:
331-
self.log.info(f'Test rate limiting of addr processing for {contype} peers')
332-
if contype == "inbound":
329+
for conn_type, no_relay in [("outbound-full-relay", False), ("block-relay-only", True), ("inbound", False)]:
330+
self.log.info(f'Test rate limiting of addr processing for {conn_type} peers')
331+
if conn_type == "inbound":
333332
peer = self.nodes[0].add_p2p_connection(AddrReceiver())
334333
else:
335-
peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=0, connection_type=contype)
334+
peer = self.nodes[0].add_outbound_p2p_connection(AddrReceiver(), p2p_idx=0, connection_type=conn_type)
336335

337336
# Send 600 addresses. For all but the block-relay-only peer this should result in addresses being processed.
338-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 600, 600)
337+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=600, total_addrs=600)
339338

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

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

347346
# Advance the time by 100 seconds, permitting the processing of 10 more addresses.
348347
# Send 200 and verify that 10 are processed.
349348
self.bump_mocktime(100)
350349
peer.increment_tokens(10)
351350

352-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 200, 1410)
351+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=200, total_addrs=1410)
353352

354353
# Advance the time by 1000 seconds, permitting the processing of 100 more addresses.
355354
# Send 200 and verify that 100 are processed.
356355
self.bump_mocktime(1000)
357356
peer.increment_tokens(100)
358357

359-
self.send_addrs_and_test_rate_limiting(peer, no_relay, 200, 1610)
358+
self.send_addrs_and_test_rate_limiting(peer, no_relay, new_addrs=200, total_addrs=1610)
360359

361360
self.nodes[0].disconnect_p2ps()
362361

362+
363363
if __name__ == '__main__':
364364
AddrTest().main()

0 commit comments

Comments
 (0)