Repository navigation
Conversation
On some occasions, a peer will send us addresses a few seconds after the connection is established. As peers start with token=1 and only get an additional token only every 10 seconds, some of these addresses might be rate-limited. The following log snippet contains an example: ``` 04:12:02 [net] Added connection to [redacted] peer=1234 ... 04:12:03 [net] Received addr: 1 addresses (1 processed, 0 rate-limited) from peer=1234 04:12:34 [net] Received addr: 4 addresses (3 processed, 1 rate-limited) from peer=1234 04:13:09 [net] Received addr: 2 addresses (2 processed, 0 rate-limited) from peer=1234 ``` The peer starts with token=1 and uses one for its self-announcement at 04:12:03. Until the next addr message arrives at 04:12:34, it re-gains 3 tokens and uses them, however, the fourth address in this addr message is rate-limited. At 04:13:09, the peer has token=3 and uses 2. This change gives peers token=5 to start with. To choose this value, I looked at the address relay of freshly connected peers. The data was sampled from the debug.logs from different nodes. It appears, that only very few peers require more than 5 initial tokens. (see image in PR) By increasing the initial tokens to 5, someone could connect, send 5 addresses, disconnect, and repeat to spam the node with addresses. Previously, the same was possible but only one address was processed. I don't think this is a problem since we have bitcoin#30568.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33699. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste |
mzumsande
left a comment
There was a problem hiding this comment.
utACK fdf4863
I think that while the existing rate limit is sufficient, the observed events where the rate limit kicks in are just due to statistical fluctuation (which wouldn't lead to rate limiting later on because the node will usually have saved up some cushion) - having fewer of those could prevent a peer's self-announcement from being ignored, and could make monitoring a bit easier.
|
crACK fdf4863 The graph is helpful, I wonder what's going on with those peers above and to the left of the staircase? |
|
cc @ajtowns |
What's the drawback of ignoring these addresses? If we'd connected a few seconds/a minute later, we'd have missed them anyway. An advantage of starting with an (almost) empty bucket is that someone attempting an address spamming attack has to pay for that attack by maintaining a connection to us. Increasing the initial bucket for outbound/manual connections but not inbound connections might be better? |
Ignoring the initial self-announcement of the peer if it get mixed up with other addrs would feel strange. |
|
Ignoring self-announcements seems to occur only in the time between our inbound peer (outbound from their perspective) 1) calling If we are being connected to, I think our self-announcement won't be rate-limited because we'll wait until receiving getaddr, call |
|
If these are Bitcoin Core nodes (so our sender-side logic is seemingly incompatible with our receiver-side logic), is an alternative approach to increase the delay after which we'd send addrs? |
Yeah, you're right. Though as glozow points out, it's also strange to have our sender/receiver side logic randomly mismatch depending on network behaviour (with this PR making the random mismatch much less frequent). Would it make sense to change the logic to be: void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time)
{
...
if (... && peer.m_next_local_addr_send < current_time) {
...
if (peer.m_next_local_addr_send != 0us) {
peer.m_addr_known->reset();
}
if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
if (peer.m_next_local_addr_send == 0us) {
// first local addr announcement, send as a single message
if (peer.m_wants_addrv2) {
MakeAndPushMessage(node, NetMsgType::ADDRV2, CAddress::V2_NETWORK(local_addr));
} else {
MakeAndPushMessage(node, NetMsgType::ADDR, CAddress::V1_NETWORK(local_addr));
}
} else {
// normally, include local addr with any others queued up
PushAddress(peer, local_addr);
}
}
peer.m_next_local_addr_send = current_time + m_rng.rand_exp_duration(AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
}
...so that we push our local address as a single message first, using up the bucket preallocation, then allow other addresses to be sent and rate-limited as normal? That seems like it would automatically help existing nodes not rate-limit self announcements from future releases. |
I think shuffling helps avoid an attacker being able to (a) prevent honest nodes' addresses from being relayed (by filling up the buffer, so that later honest announcements get dropped) and (b) (easily?) detect how many addresses are being rate limited. So I don't think removing the shuffle would be a good idea. |
True, I have observed this effect too recently, it isn't really clean though - I don't think anyone intended it to work such that a GETADDR answers would contain 999 addresses from addrman and a self announcement replacing a random one - so aj's suggestion makes change to me.
That should work too, but it would also slow things down if the receiver waits for addrs (ADDRFETCH connections). |
Thanks for that digging, I found that helpful.
I'm actually not sure yet if any addresses my nodes rate-limited are actually self-announcements. Looking at the logs, there are certainly initial addr(v2) messages I received shortly after connection open (from inbound peers) with at least one address being rate-limited by me. However, I think if the peer doesn't self-announce (e.g.
I picked that up in https://github.com/0xB10C/bitcoin/tree/2025-11-self-advertise-in-separate-message - but will hold off PRing it until I'm sure we actually rate-limit self-announcements. Most of the work was the function test for the self-announcements, which is good to have regardless1. Footnotes
|
|
Saw one today with 0xB10C@159b6fd. Will wait for a few more. edit: saw more, but interestingly only from older releases e.g. |
To avoid that our first (i.e. right after connection open) self- announcement is rate-limited due to being put in a message with other addresses, send it in a separate message first. A new connection starts with one token in the addr rate-limiter. It can happen that the self-announcement is sent in a addr(v2) message along with other to-be-relayed addresses. Since order of the addresses is shuffeld on receiving, the self-announcement in might be rate-limited. This is inspired by and based on bitcoin#33699 (comment) Co-Authored-By: Anthony Towns <[email protected]>
Opened a PR for the self-announcement test in #34039 The logic change (c1514bf) might be good to be explicit about our self-announcement in case we somehow start sending them along with other addresses, but doesn't seem to be needed now. I will keep an eye on rate-limited self-announcements for a bit longer to see if anything changes my impression there. |
|
I'm becoming less sure that this PR is actually worthwhile doing. I've not seen us rate-limit a self-announcement from a recent node version yet. For all other addresses (that aren't direct self-announcements from our peer) it probably hardly matters if we randomly rate-limit a few gossiped addresses.
Agree, generally, it might be better to keep the start token at 1 to make it more costly for an attacker to send us addresses that we will process. Especially with e.g. LinkingLion now shifting behavior to mass self-announce IPs of their new, listening nodes.
Yet another approach: On the sender side in Going to close this PR for now. Might reopen if there are new insights from collected data. |
mzumsande
left a comment
There was a problem hiding this comment.
I think the suggestion by @ajtowns (#33699 (comment)) to send the first self-advertisement separately would make sense on its own - currently it will usually (I think it depends a bit on timing) get mixed up with the GETADDR answer and replaces one of the 1000 addrs from it, which isn't clean.
done in #34146 |
This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on bitcoin#33699 (comment) Co-Authored-By: Anthony Towns <[email protected]>
…age 🎄 792e2ed p2p: first addr self-announcement in separate msg (0xb10c) Pull request description: This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on #33699 (comment). The discussion there should be helpful for reviewers. ACKs for top commit: bensig: ACK 792e2ed achow101: ACK 792e2ed fjahr: Code review ACK 792e2ed frankomosh: Code Review ACK [792e2ed](792e2ed) Tree-SHA512: e3d39b1e3ae6208b54df4b36c624a32d70a442e01681f49e0c8a65076a818b5bf203c2e51011dc32edbbe3637b3c0b5f18de26e3461c288aa3806646a209a260
This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on bitcoin/bitcoin#33699 (comment) Co-Authored-By: Anthony Towns <[email protected]>
…n separate message 🎄 3f3a057 p2p: first addr self-announcement in separate msg (0xb10c) Pull request description: This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on bitcoin/bitcoin#33699 (comment). The discussion there should be helpful for reviewers. ACKs for top commit: bensig: ACK 3f3a057 achow101: ACK 3f3a057 fjahr: Code review ACK 3f3a057 frankomosh: Code Review ACK [3f3a057](bitcoin/bitcoin@3f3a057) Tree-SHA512: e3d39b1e3ae6208b54df4b36c624a32d70a442e01681f49e0c8a65076a818b5bf203c2e51011dc32edbbe3637b3c0b5f18de26e3461c288aa3806646a209a260
This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on bitcoin/bitcoin#33699 (comment) Co-Authored-By: Anthony Towns <[email protected]>
…n separate message 🎄 89dee11 p2p: first addr self-announcement in separate msg (0xb10c) Pull request description: This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on bitcoin/bitcoin#33699 (comment). The discussion there should be helpful for reviewers. ACKs for top commit: bensig: ACK 89dee11 achow101: ACK 89dee11 fjahr: Code review ACK 89dee11 frankomosh: Code Review ACK [89dee11](bitcoin/bitcoin@89dee11) Tree-SHA512: e3d39b1e3ae6208b54df4b36c624a32d70a442e01681f49e0c8a65076a818b5bf203c2e51011dc32edbbe3637b3c0b5f18de26e3461c288aa3806646a209a260
This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on bitcoin/bitcoin#33699 (comment) Co-Authored-By: Anthony Towns <[email protected]>
…n separate message 🎄 792e2edf57ab31ae5c6f98acf33af8f67506630f p2p: first addr self-announcement in separate msg (0xb10c) Pull request description: This makes sure the initial address self-announcement a node sends to a peer happends in a separate P2P message. This has benefits for both inbound and outbound connections: For inbound connections from a peer to us, previously, we might send the self-announcement along with our response to a GETADDR request. However, the self-announcement might replace an address from the GETADDR response. This isn't clean. For outbound connections from us to a peer, previously, it could have happend that we send the self-announcement along with other addresses. Since shortly after connection open, the peer might only have one rate-limiting token for us, and the addresses are shuffeld on arrival, it's possible that the self-announcement gets rate-limited. However, note that these rate-limitings seem to be rare in practice. This is inspired by and based on bitcoin/bitcoin#33699 (comment). The discussion there should be helpful for reviewers. ACKs for top commit: bensig: ACK 792e2edf57ab31ae5c6f98acf33af8f67506630f achow101: ACK 792e2edf57ab31ae5c6f98acf33af8f67506630f fjahr: Code review ACK 792e2edf57ab31ae5c6f98acf33af8f67506630f frankomosh: Code Review ACK [792e2ed](bitcoin/bitcoin@792e2ed) Tree-SHA512: e3d39b1e3ae6208b54df4b36c624a32d70a442e01681f49e0c8a65076a818b5bf203c2e51011dc32edbbe3637b3c0b5f18de26e3461c288aa3806646a209a260

Start the rate-limiter with 5 tokens instead of 1 token.
On some occasions, a peer will send us addresses a few seconds after the connection is established. As peers start with token=1 and only get an additional token only every 10 seconds, some of these addresses might be rate-limited. The following log snippet contains an example:
The peer starts with token=1 and uses one for its self-announcement at 04:12:03. Until the next addr message arrives at 04:12:34, it re-gains 3 tokens and uses them, however, the fourth address in this addr message is rate-limited. At 04:13:09, the peer has token=3 and uses 2.
This change gives peers token=5 to start with. To choose this value, I looked at the address relay of freshly connected peers. The data was sampled from the debug.logs from different nodes. It appears, that only very few peers require more than 5 initial tokens.
By increasing the initial tokens to 5, someone could connect, send 5 addresses, disconnect, and repeat to spam the node with addresses. Previously, the same was possible but only one address was processed. I don't think this is a problem anymore since #30568.