Skip to content

net: ignore repeated -addnode startup values - #36014

Open
w0xlt wants to merge 6 commits into
bitcoin:masterfrom
w0xlt:net-deduplicate-startup-addnodes
Open

w0xlt wants to merge 6 commits into
bitcoin:masterfrom
w0xlt:net-deduplicate-startup-addnodes

Conversation

@w0xlt

@w0xlt w0xlt commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Startup -addnode values are copied directly into the connection manager's added-node list. Unlike runtime addnode add calls, this path does not reject repeated values.

For example:

-addnode=example.com
-addnode=example.com
-addnode=example.com:8333

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. getaddednodeinfo also reports the repeated entries.

This PR checks each startup value with the same lookup that addnode add uses, 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.com and example.com:8333 on mainnet). No DNS lookups are done.

To keep the added-node handling consistent, it also:

  • makes addnode add reject a host that was already added with or without the default port;
  • makes addnode remove and getaddednodeinfo <node> accept any equivalent value, so they no longer report a value as not added after addnode add rejected it as already added;
  • makes 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 -addnode values 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.

@DrahtBot

DrahtBot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK pablomartin4btc
Concept ACK danielabrozzoni, RandyMcMillan
Stale ACK AndreaDiazCorreia

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36990 (test: replace hard-coded RPC error codes by l0rinc)

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 pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 danielabrozzoni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@w0xlt
w0xlt force-pushed the net-deduplicate-startup-addnodes branch 2 times, most recently from 22499b4 to 4ebff1a Compare September 1, 2026 23:33
@w0xlt

w0xlt commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@pablomartin4btc @danielabrozzoni
Thanks for the suggestions. Great catches.
I split the change into two commits and credited each of you as a co-author on the respective commit.

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 4ebff1a

Thanks for taking my suggestion!

Left a couple of inline nits I noticed on a re-read — nothing blocking.

Comment thread src/net.h Outdated
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good idea. Done in b8e01b0. Added you as a co-author on that commit.

Comment thread src/net.cpp

std::string existing_host;
if (host_port_is_valid && SplitHostPort(it.m_added_node, existing_port, existing_host) &&
host == existing_host && port == existing_port) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@AndreaDiazCorreia

Copy link
Copy Markdown

tested ACK 4ebff1a

Built and tested both commits separately. Also checked on regtest that -addnode=first.node -addnode=first.node:18444 keeps a single entry in getaddednodeinfo.

The only thing I found is the case-sensitive hostname comparison @pablomartin4btc already flagged, which seems fine as a follow-up.

@w0xlt
w0xlt force-pushed the net-deduplicate-startup-addnodes branch from 4ebff1a to 55003ac Compare October 8, 2026 23:36
@RandyMcMillan

Copy link
Copy Markdown
Contributor

Concept ACK

@danielabrozzoni danielabrozzoni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/net.cpp Outdated
@@ -3871,12 +3871,24 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres

bool CConnman::AddNode(const AddedNodeParams& add)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/net.cpp Outdated
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)};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/functional/feature_config_args.py Outdated
node = self.nodes[0]

with node.assert_debug_log(expected_msgs=[
"Ignoring duplicate -addnode value: first.node",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Fixed in b8e01b0: the expected messages now end with \n, so each one has to match a whole log line.

w0xlt and others added 6 commits November 4, 2026 14:54
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.
@w0xlt
w0xlt force-pushed the net-deduplicate-startup-addnodes branch from 55003ac to cc3246f Compare November 4, 2026 22:57
@w0xlt w0xlt changed the title init: ignore repeated -addnode startup values net: ignore repeated -addnode startup values Nov 4, 2026
@w0xlt

w0xlt commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

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.

@danielabrozzoni Thanks for the thorough review. I fixed this in 59d428d: add and remove now share the same lookup, so addnode abc.com:8333 remove removes abc.com. For consistency, getaddednodeinfo <node> uses the same lookup too (6d5a711). I added you as co-author on the fix.

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 remove and getaddednodeinfo <node> (extended by w0xlt for consistency) now use the same equivalence as add via the shared FindAddedNode helper, plus the explanatory comment, the default_port/port split, and the \n fix in the functional test.
  • AddedNodesContain now 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.py checks for addnode remove and getaddednodeinfo with an equivalent address) and release note.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants