net: ignore repeated `-addnode` startup values #36014

pull w0xlt wants to merge 6 commits into bitcoin:master from w0xlt:net-deduplicate-startup-addnodes changing 7 files +223 −36
  1. w0xlt commented at 8:28 PM on August 18, 2026: 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.

  2. DrahtBot commented at 8:28 PM on August 18, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

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

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #30951 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/30951.svg"></sub> (net: option to disallow v1 connection on ipv4 and ipv6 peers by stratospher)

    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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. pablomartin4btc commented at 6:30 PM on August 19, 2026: member

    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?

  4. danielabrozzoni commented at 2:15 PM on August 27, 2026: member

    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.

  5. w0xlt force-pushed on Sep 1, 2026
  6. w0xlt force-pushed on Sep 1, 2026
  7. w0xlt commented at 11:34 PM on September 1, 2026: contributor

    @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.

  8. DrahtBot added the label CI failed on Sep 1, 2026
  9. DrahtBot removed the label CI failed on Sep 2, 2026
  10. in src/net.h:1148 in 4ebff1af2a outdated
    1150 | +        // Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
    1151 | +        // peer doesn't support it or immediately disconnects us for another reason.
    1152 | +        const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
    1153 | +        for (const std::string& added_node : connOptions.m_added_nodes) {
    1154 | +            if (!AddNode({added_node, use_v2transport})) {
    1155 | +                LogWarning("Ignoring duplicate -addnode value: %s", added_node);
    


    pablomartin4btc commented at 1:30 AM on September 11, 2026:

    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.


    w0xlt commented at 11:17 PM on November 4, 2026:

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

  11. in src/net.cpp:3849 in 4ebff1af2a outdated
    3846 | +        uint16_t existing_port{GetDefaultPort(it.m_added_node)};
    3847 | +        if (resolved_is_valid && resolved == LookupNumeric(it.m_added_node, existing_port)) return false;
    3848 | +
    3849 | +        std::string existing_host;
    3850 | +        if (host_port_is_valid && SplitHostPort(it.m_added_node, existing_port, existing_host) &&
    3851 | +            host == existing_host && port == existing_port) {
    


    pablomartin4btc commented at 1:37 AM on September 11, 2026:

    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.


    w0xlt commented at 11:20 PM on November 4, 2026:

    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.

  12. pablomartin4btc commented at 1:39 AM on September 11, 2026: member

    ACK 4ebff1af2acc6e846eb7932ff8ece03ffa3a9066

    Thanks for taking my suggestion!

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

  13. DrahtBot requested review from danielabrozzoni on Sep 11, 2026
  14. AndreaDiazCorreia commented at 11:43 PM on September 19, 2026: none

    tested ACK 4ebff1af2acc6e846eb7932ff8ece03ffa3a9066

    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.

  15. DrahtBot added the label Needs rebase on Oct 7, 2026
  16. w0xlt force-pushed on Oct 8, 2026
  17. RandyMcMillan commented at 12:10 AM on October 9, 2026: contributor

    Concept ACK

  18. DrahtBot removed the label Needs rebase on Oct 9, 2026
  19. in src/net.cpp:3872 in 91683b1202
    3870 | @@ -3871,12 +3871,24 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
    3871 |  
    3872 |  bool CConnman::AddNode(const AddedNodeParams& add)
    


    danielabrozzoni commented at 2:00 PM on November 2, 2026:

    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.
    

    w0xlt commented at 11:25 PM on November 4, 2026:

    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 59d428d730.

  20. in src/net.cpp:3874 in 91683b1202
    3870 | @@ -3871,12 +3871,24 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
    3871 |  
    3872 |  bool CConnman::AddNode(const AddedNodeParams& add)
    3873 |  {
    3874 | -    const CService resolved(LookupNumeric(add.m_added_node, GetDefaultPort(add.m_added_node)));
    3875 | +    uint16_t port{GetDefaultPort(add.m_added_node)};
    


    danielabrozzoni commented at 2:11 PM on November 2, 2026:

    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;
    

    w0xlt commented at 11:26 PM on November 4, 2026:

    Done, thanks! I split them into default_port/port and existing_default_port/existing_port as you suggested, in f2fcaeb72. I also added you as a co-author there.

  21. in test/functional/feature_config_args.py:288 in 55003ac5fb
     279 | @@ -280,6 +280,28 @@ def test_empty_addnode(self):
     280 |  
     281 |          node.replace_in_config([("addnode=\n", "")])
     282 |  
     283 | +    def test_duplicate_addnode(self):
     284 | +        self.log.info("Test equivalent addnode configuration values are ignored")
     285 | +        node = self.nodes[0]
     286 | +
     287 | +        with node.assert_debug_log(expected_msgs=[
     288 | +            "Ignoring duplicate -addnode value: first.node",
    


    danielabrozzoni commented at 3:11 PM on November 2, 2026:

    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.


    w0xlt commented at 11:27 PM on November 4, 2026:

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

  22. danielabrozzoni commented at 4:21 PM on November 3, 2026: member

    Reviewed 55003ac5fbbe4ef458f790cef4280092f203e471

    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.

  23. net: reject equivalent addnode host-port pairs
    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 <pablomartin4btc@gmail.com>
    Co-authored-by: Daniela Brozzoni <danielabrozzoni@protonmail.com>
    f2fcaeb72f
  24. net: match numeric aliases in AddedNodesContain
    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.
    486e97266a
  25. net: remove added nodes using the same equivalence as AddNode
    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 <danielabrozzoni@protonmail.com>
    59d428d730
  26. net: deduplicate startup -addnode values
    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 <danielabrozzoni@protonmail.com>
    Co-authored-by: Pablo Martin <pablomartin4btc@gmail.com>
    b8e01b0354
  27. rpc: find equivalent added nodes in getaddednodeinfo
    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.
    6d5a7119ee
  28. doc: add release note for addnode equivalence cc3246f6d8
  29. w0xlt force-pushed on Nov 4, 2026
  30. w0xlt renamed this:
    init: ignore repeated `-addnode` startup values
    net: ignore repeated `-addnode` startup values
    on Nov 4, 2026
  31. w0xlt commented at 11:30 PM on November 4, 2026: contributor

    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 59d428d730: 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 (6d5a7119ee). I added you as co-author on the fix.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-24 09:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me