diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -288,7 +288,7 @@ /** * The soft limit of the address processing token bucket (the regular * MAX_ADDR_RATE_PER_SECOND based increments won't go above this, but the - * MAX_ADDR_TO_SEND increment following GETADDR is exempt from this limit. + * MAX_ADDR_TO_SEND increment following GETADDR is exempt from this limit). */ static constexpr size_t MAX_ADDR_PROCESSING_TOKEN_BUCKET{MAX_ADDR_TO_SEND}; @@ -425,7 +425,7 @@ /** Whether this peer has already sent us a getaddr message. */ bool m_getaddr_recvd{false}; /** - * Number of addr messages that can be processed from this peer. Start at 1 + * Number of addresses that can be processed from this peer. Start at 1 * to permit self-announcement. */ double m_addr_token_bucket{1.0}; @@ -435,7 +435,7 @@ /** Total number of addresses that were dropped due to rate limiting. */ std::atomic m_addr_rate_limited{0}; /** - * Total number of addresses that were processed (excludes rate limited + * Total number of addresses that were processed (excludes rate-limited * ones). */ std::atomic m_addr_processed{0}; @@ -3732,11 +3732,12 @@ } // Apply rate limiting. - if (rate_limited) { - if (peer->m_addr_token_bucket < 1.0) { + if (peer->m_addr_token_bucket < 1.0) { + if (rate_limited) { ++num_rate_limit; continue; } + } else { peer->m_addr_token_bucket -= 1.0; } @@ -3774,9 +3775,8 @@ peer->m_addr_rate_limited += num_rate_limit; LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) " - "from peer=%d%s\n", - vAddr.size(), num_proc, num_rate_limit, pfrom.GetId(), - fLogIPs ? ", peeraddr=" + pfrom.addr.ToString() : ""); + "from peer=%d\n", + vAddr.size(), num_proc, num_rate_limit, pfrom.GetId()); m_connman.AddNewAddresses(vAddrOk, pfrom.addr, 2 * 60 * 60); if (vAddr.size() < 1000) { diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -108,6 +108,12 @@ "(ip:port) Local address as reported by the peer"}, {RPCResult::Type::BOOL, "addr_relay_enabled", "Whether we participate in address relay with this peer"}, + {RPCResult::Type::NUM, "addr_processed", + "The total number of addresses processed, excluding those " + "dropped due to rate limiting"}, + {RPCResult::Type::NUM, "addr_rate_limited", + "The total number of addresses dropped due to rate " + "limiting"}, {RPCResult::Type::STR, "network", "Network (ipv4, ipv6, or onion) the peer connected " "through"}, diff --git a/test/functional/p2p_addr_relay.py b/test/functional/p2p_addr_relay.py --- a/test/functional/p2p_addr_relay.py +++ b/test/functional/p2p_addr_relay.py @@ -358,7 +358,7 @@ self.nodes[0].disconnect_p2ps() - def send_addrs_and_test_rate_limiting(self, peer, no_relay, new_addrs, + def send_addrs_and_test_rate_limiting(self, peer, no_relay, *, new_addrs, total_addrs): """Send an addr message and check that the number of addresses processed and rate-limited is as expected @@ -383,31 +383,34 @@ self.restart_node(0, []) self.nodes[0].setmocktime(self.mocktime) - for contype, no_relay in [ + for conn_type, no_relay in [ ("outbound-full-relay", False), ("block-relay-only", True), ("inbound", False) ]: self.log.info( - f'Test rate limiting of addr processing for {contype} peers') - if contype == "inbound": + f'Test rate limiting of addr processing for {conn_type} peers') + if conn_type == "inbound": peer = self.nodes[0].add_p2p_connection(AddrReceiver()) else: peer = self.nodes[0].add_outbound_p2p_connection( - AddrReceiver(), p2p_idx=0, connection_type=contype) + AddrReceiver(), p2p_idx=0, connection_type=conn_type) # Send 600 addresses. For all but the block-relay-only peer this # should result in addresses being processed. - self.send_addrs_and_test_rate_limiting(peer, no_relay, 600, 600) + self.send_addrs_and_test_rate_limiting( + peer, no_relay, new_addrs=600, total_addrs=600) # Send 600 more addresses. For the outbound-full-relay peer (which # we send a GETADDR, and thus will process up to 1001 incoming # addresses), this means more addresses will be processed. - self.send_addrs_and_test_rate_limiting(peer, no_relay, 600, 1200) + self.send_addrs_and_test_rate_limiting( + peer, no_relay, new_addrs=600, total_addrs=1200) # Send 10 more. As we reached the processing limit for all nodes, # no more addresses should be procesesd. - self.send_addrs_and_test_rate_limiting(peer, no_relay, 10, 1210) + self.send_addrs_and_test_rate_limiting( + peer, no_relay, new_addrs=10, total_addrs=1210) # Advance the time by 100 seconds, permitting the processing of 10 # more addresses. @@ -416,7 +419,8 @@ self.nodes[0].setmocktime(self.mocktime) peer.increment_tokens(10) - self.send_addrs_and_test_rate_limiting(peer, no_relay, 200, 1410) + self.send_addrs_and_test_rate_limiting( + peer, no_relay, new_addrs=200, total_addrs=1410) # Advance the time by 1000 seconds, permitting the processing of 100 # more addresses. @@ -425,7 +429,8 @@ self.nodes[0].setmocktime(self.mocktime) peer.increment_tokens(100) - self.send_addrs_and_test_rate_limiting(peer, no_relay, 200, 1610) + self.send_addrs_and_test_rate_limiting( + peer, no_relay, new_addrs=200, total_addrs=1610) self.nodes[0].disconnect_p2ps()