Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
pablomartin4btc
left a comment
There was a problem hiding this comment.
Concept ACK
Comparing this patch to the validation performed in CConnman::AddNode() (the RPC path), which resolves the target — full resolution is out of scope as clearly explained in the description and commit body. What about also checking the port (perhaps using SplitHostPort), so e.g. node.example and node.example:8333 (mainnet default port) would be treated as the same target, and the second one excluded as a duplicate?
danielabrozzoni
left a comment
There was a problem hiding this comment.
Concept ACK on ignoring repeated values.
When looking at how addnode handles repeated values, I noticed that it uses CConnMan::AddNode, which invokes LookupNumeric to parse IP addresses and remove equivalent representations (it doesn't do any DNS resolution). For consistency, it may make sense for -addnode values to use the same check.
One way would be to call AddNode inside of CConnman::Init, but I'm not sure if it's okay to call LogWarning from there:
diff --git a/src/net.h b/src/net.h
index ea0c651d11..d69b9a4df3 100644
--- a/src/net.h
+++ b/src/net.h
@@ -1140,12 +1140,13 @@ public:
vWhitelistedRangeIncoming = connOptions.vWhitelistedRangeIncoming;
vWhitelistedRangeOutgoing = connOptions.vWhitelistedRangeOutgoing;
{
- LOCK(m_added_nodes_mutex);
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
// peer doesn't support it or immediately disconnects us for another reason.
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
for (const std::string& added_node : connOptions.m_added_nodes) {
- m_added_node_params.push_back({added_node, use_v2transport});
+ if (!AddNode({added_node, use_v2transport})) {
+ LogWarning("Ignoring duplicate -addnode value: %s", added_node);
+ }
}
}Otherwise, you can duplicate the logic in init.cpp.
To be clear, I'm okay even with the code as-is, if it gets too complicated to add the LookupNumeric checks.
22499b4 to
4ebff1a
Compare
|
@pablomartin4btc @danielabrozzoni |
pablomartin4btc
left a comment
There was a problem hiding this comment.
ACK 4ebff1a
Thanks for taking my suggestion!
Left a couple of inline nits I noticed on a re-read — nothing blocking.
| const bool use_v2transport(GetLocalServices() & NODE_P2P_V2); | ||
| for (const std::string& added_node : connOptions.m_added_nodes) { | ||
| if (!AddNode({added_node, use_v2transport})) { | ||
| LogWarning("Ignoring duplicate -addnode value: %s", added_node); |
There was a problem hiding this comment.
minor nit: this logs only the rejected value, not what it matched against. For a user staring at their config wondering why an entry got dropped, "Ignoring duplicate -addnode value: node.example:8333 (matches node.example)" would be more actionable — especially since the whole point of this PR is catching non-obvious duplicates (different port representations), where "this is a duplicate" alone doesn't explain why. Not blocking, just a nice-to-have.
There was a problem hiding this comment.
Thanks, good idea. Done in b8e01b0. Added you as a co-author on that commit.
|
|
||
| std::string existing_host; | ||
| if (host_port_is_valid && SplitHostPort(it.m_added_node, existing_port, existing_host) && | ||
| host == existing_host && port == existing_port) { |
There was a problem hiding this comment.
One more equivalence worth considering, in the same spirit as the host/port work here: hostname comparison is case-sensitive (host == existing_host), so -addnode=Example.com and -addnode=example.com wouldn't be caught as duplicates, since DNS hostnames are case-insensitive. This isn't new — the original exact-string check had the same gap — and it's arguably out of scope for this PR. But since the whole point here is catching duplicate representations of the same target rather than just literal string matches, case-folding the hostname comparison feels like a natural extension of the same fix rather than a separate concern, so wanted to flag it while this code is already being touched. No test currently covers it either way, consistent with it not being handled. Happy to leave it out if you'd rather keep this PR narrowly scoped to the port-equivalence case.
There was a problem hiding this comment.
Thanks! I tried this, but it has edge cases, because not every host is a DNS name. IPv6 zone IDs are case-sensitive interface names, so [fe80::1%ETH0] would be treated as a duplicate of [fe80::1%eth0], and the valid spelling could be dropped.
I'd rather keep this PR focused and leave case-insensitive matching for a follow-up as suggested.
|
tested ACK 4ebff1a Built and tested both commits separately. Also checked on regtest that The only thing I found is the case-sensitive hostname comparison @pablomartin4btc already flagged, which seems fine as a follow-up. |
4ebff1a to
55003ac
Compare
|
Concept ACK |
danielabrozzoni
left a comment
There was a problem hiding this comment.
Reviewed 55003ac
I want to mention this explicitly, but I don't think it's really problematic, and I'm fine if it doesn't get fixed - we are changing how add checks for duplicates, but remove still compares exact strings. It means that one could do:
addnode abc.com add # ok!
addnode abc.com:8333 add # Error: Node already added
addnode abc.com:8333 remove # Error: Node could not be removed. It has not been added previously.
This is a bit confusing for the user - has abc.com:8333 been added or not?
This is also present in master, it's not unique to this PR, since in master add already rejects numeric equivalents.
The fix would be to also check for equivalence in RemoveAddedNode.
| @@ -3871,12 +3871,24 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres | |||
|
|
|||
| bool CConnman::AddNode(const AddedNodeParams& add) | |||
There was a problem hiding this comment.
nit: I found this function a bit hard to go through (although I'm very new to the net code :)), I think one comment explaining the three checks we're applying would be very helpful:
// We want to avoid storing the same peer twice, so we check for duplicates in
// three different ways:
// 1. Exact string match.
// 2. Same address, compared as a CService. We use LookupNumeric to parse
// addresses and fill in the default port if needed, so different spellings
// of one address match (e.g. "[::1]" and "[0:0:0:0:0:0:0:1]"). This doesn't
// apply to names, for which resolved_is_valid is false.
// 3. Same host text and effective port, using SplitHostPort. This one works
// for names, so "node.example" and "node.example:8333" are one peer.
There was a problem hiding this comment.
Thanks, added! The checks now live in a FindAddedNode helper (shared by addnode add/remove, getaddednodeinfo and the startup -addnode handling) so I put your comment on its declaration, with the wording adapted slightly to fit the helper. See 59d428d.
| bool CConnman::AddNode(const AddedNodeParams& add) | ||
| { | ||
| const CService resolved(LookupNumeric(add.m_added_node, GetDefaultPort(add.m_added_node))); | ||
| uint16_t port{GetDefaultPort(add.m_added_node)}; |
There was a problem hiding this comment.
nit: I was a bit confused because port here is used as two different things: it's the default port that goes into LookupNumeric, and then it gets overwritten by SplitHostPort with the effective port. It might be clearer to use separate variables, although it might be a bit too verbose, feel free to ignore:
diff --git a/src/net.cpp b/src/net.cpp
index 1b6f6263fe..1f9923b873 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -3871,20 +3871,22 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
bool CConnman::AddNode(const AddedNodeParams& add)
{
- uint16_t port{GetDefaultPort(add.m_added_node)};
- const CService resolved(LookupNumeric(add.m_added_node, port));
+ const uint16_t default_port{GetDefaultPort(add.m_added_node)};
+ const CService resolved(LookupNumeric(add.m_added_node, default_port));
const bool resolved_is_valid{resolved.IsValid()};
std::string host;
+ uint16_t port{default_port};
const bool host_port_is_valid{SplitHostPort(add.m_added_node, port, host)};
LOCK(m_added_nodes_mutex);
for (const auto& it : m_added_node_params) {
if (add.m_added_node == it.m_added_node) return false;
- uint16_t existing_port{GetDefaultPort(it.m_added_node)};
- if (resolved_is_valid && resolved == LookupNumeric(it.m_added_node, existing_port)) return false;
+ const uint16_t existing_default_port{GetDefaultPort(it.m_added_node)};
+ if (resolved_is_valid && resolved == LookupNumeric(it.m_added_node, existing_default_port)) return false;
std::string existing_host;
+ uint16_t existing_port{existing_default_port};
if (host_port_is_valid && SplitHostPort(it.m_added_node, existing_port, existing_host) &&
host == existing_host && port == existing_port) {
return false;There was a problem hiding this comment.
Done, thanks! I split them into default_port/port and existing_default_port/existing_port as you suggested, in f2fcaeb. I also added you as a co-author there.
| node = self.nodes[0] | ||
|
|
||
| with node.assert_debug_log(expected_msgs=[ | ||
| "Ignoring duplicate -addnode value: first.node", |
There was a problem hiding this comment.
You should include a "\n" at the end of this sentence, otherwise the line in the logs for Ignoring duplicate -addnode value: first.node:18444 will also match this one (assert_debug_log checks the presence of a substring). Currently, if you remove the duplicate -addnode=first.node line, the test still passes.
There was a problem hiding this comment.
Good catch, thanks! Fixed in b8e01b0: the expected messages now end with \n, so each one has to match a whole log line.
CConnman::AddNode rejects equivalent numeric addresses, but compares unresolved hostnames as raw strings. As a result, the same hostname with and without an explicit default port can be stored twice. Compare the host text and effective port of values that SplitHostPort can parse. Do not perform DNS resolution, and keep non-default ports distinct. Co-authored-by: Pablo Martin <[email protected]> Co-authored-by: Daniela Brozzoni <[email protected]>
AddedNodesContain only matches an added node that is spelled exactly as the address's canonical string. An equivalent numeric spelling, such as an expanded IPv6 address or a bracketed IPv6 address without a port, is never matched, so the automatic outbound logic can use a slot on that peer. AddNode already rejects the canonical spelling as a duplicate of such an entry, so it cannot be added alongside it. Parse each added node as a numeric address and compare it with the address. An entry without a port still matches the address at any port. Parse after releasing m_added_nodes_mutex, as GetAddedNodeInfo does.
CConnman::AddNode rejects a value that is equivalent to an existing entry, but RemoveAddedNode only removes an exact string match. As a result, `addnode <node> remove` can fail for a value that `addnode <node> add` has just reported as already added. Move the duplicate lookup into a FindAddedNode helper and use it in both functions, so removal accepts any value that addition considers a duplicate. Co-authored-by: Daniela Brozzoni <[email protected]>
Startup -addnode values are copied directly into CConnman state, bypassing the duplicate checks used by the addnode RPC. Equivalent values are consequently stored and retried independently. Check each startup value with FindAddedNode, the lookup AddNode uses, while holding m_added_nodes_mutex. Keep the first entry, and log each ignored value together with the entry it matched, since equivalent values can differ in port or numeric address spelling. Co-authored-by: Daniela Brozzoni <[email protected]> Co-authored-by: Pablo Martin <[email protected]>
getaddednodeinfo only matches its node argument against the exact string that was passed to addnode. A value that `addnode add` rejects as already added, and that `addnode remove` accepts, is still reported as not added. Let GetAddedNodeInfo optionally return only the entry equivalent to a node value, found with FindAddedNode under the same lock that takes the snapshot, and use it in getaddednodeinfo.
55003ac to
cc3246f
Compare
-addnode startup values-addnode startup values
@danielabrozzoni Thanks for the thorough review. I fixed this in 59d428d: add and remove now share the same lookup, so |
pablomartin4btc
left a comment
There was a problem hiding this comment.
re-ACK cc3246f
Left a non-blocking nit on the release note.
Thanks for taking my suggestion.
Agree with leaving case-insensitive matching for a follow-up, and good point on IPv6 zone IDs. I think it could be scoped to names only, e.g. by only applying CaseInsensitiveEqual in the host/port check when the host contains no ':' (IPv6 literals always do after SplitHostPort strips the brackets, and onion/ i2p are already handled by LookupNumeric since base32 decoding is case-insensitive). Happy to review or open that follow-up.
Changes since my last ACK:
- @danielabrozzoni's feedback addressed:
addnode removeandgetaddednodeinfo <node>(extended by w0xlt for consistency) now use the same equivalence asaddvia the sharedFindAddedNodehelper, plus the explanatory comment, thedefault_port/portsplit, and the\nfix in the functional test. AddedNodesContainnow matches numeric aliases (486e972), so expanded/ bracketed IPv6 spellings no longer let automatic outbound use a slot on an added peer.- New unit (
added_nodes_contain_numeric_aliases,remove_added_node_equivalence,get_added_node_info_equivalence)/ functional tests (rpc_net.pychecks foraddnode removeandgetaddednodeinfowith an equivalent address) and release note.
There was a problem hiding this comment.
nit: AddedNodesContain matching numeric aliases (486e972) also changes behaviour (automatic outbound now skips an added peer written as e.g. [2001:db8::1] or an expanded IPv6 address), maybe worth a line under "P2P and network changes"? Something like:
P2P and network changes
-----------------------
- Automatic outbound connections now skip a peer that was added with
`-addnode` or `addnode add` using a different spelling of its numeric
address, such as an expanded IPv6 address or a bracketed IPv6 address
without a port. Previously, only the canonical spelling was recognised.
(#36014)

Startup
-addnodevalues are copied directly into the connection manager's added-node list. Unlike runtimeaddnode addcalls, this path does not reject repeated values.For example:
All three entries are currently stored, although on mainnet they refer to the same peer. While the destination is disconnected,
ThreadOpenAddedConnections()processes each entry during every retry cycle.This can cause redundant DNS lookups, connection attempts, and log messages.
getaddednodeinfoalso reports the repeated entries.This PR checks each startup value with the same lookup that
addnode adduses, keeps the first entry, and logs a warning for each ignored value naming the entry it matched.Two values refer to the same peer if they are the same string, the same numeric address (e.g.
[::1]and[0:0:0:0:0:0:0:1]), or the same host and effective port (e.g.example.comandexample.com:8333on mainnet). No DNS lookups are done.To keep the added-node handling consistent, it also:
addnode addreject a host that was already added with or without the default port;addnode removeandgetaddednodeinfo <node>accept any equivalent value, so they no longer report a value as not added afteraddnode addrejected it as already added;AddedNodesContain(), which keeps automatic outbound connections away from addnode peers, match numeric aliases. On master, an added node written as[2001:db8::1]or as an expanded IPv6 address is never matched, and deduplicating startup values could otherwise keep such a spelling instead of one that matches.A release note is included.
This change is complementary to, and independent of, #35600.
#35600 prevents overlapping manual connection attempts by tracking destinations while a connection attempt is in progress.
This PR instead removes repeated
-addnodevalues before the connection threads start, preventing duplicate stored entries and sequential redundant retries.It does not replace or broaden #35600's in-flight connection handling.