net: Reduce local network activity when networkactive=0 #34486

pull willcl-ark wants to merge 3 commits into bitcoin:master from willcl-ark:respect-networkactive changing 10 files +151 −18
  1. willcl-ark commented at 5:46 pm on February 2, 2026: member

    Fixes #34190

    When networkactive=0 is set, NAT-PMP port mapping and Tor control connections still run in the background, mapping ports and logging retry attempts despite the node being “inactive.”

    This wires both subsystems to CConnman::SetNetworkActive so they start and stop with the network state:

    • mapport: EnableMapPort is injected as a callback via CConnman::Options. SetNetworkActive and SetMapPortEnabled both gate on network state.

    • torcontrol: is injected similarly. The callback dispatches onto the tor event loop via event_base_once to respect thread ownership. EVLOOP_NO_EXIT_ON_EMPTY is used to keep the event loop alive without keepalive timers, so the controller starts disconnected and waits for CConnman::Start() to signal.

  2. net: wire mapport lifecycle to CConnman
    When -networkactive=0 is set, NAT-PMP port mapping was still running and
    attempting gateway queries. Move mapport lifecycle management into
    CConnman, which already owns SetNetworkActive, so it can control mapport
    based on both the -natpmp setting and network state.
    
    EnableMapPort is injected as a callback via CConnman::Options to avoid a
    circular dependency between net and mapport.
    a0cfdb8c31
  3. test: add test for mapport networkactive
    Add functional test to verify that PCP/NAT-PMP port mapping respects the
    networkactive state:
    
    - Does not run when started with -networkactive=0
    - Starts when network is activated via setnetworkactive RPC
    - Stops when network is deactivated
    - Resumes when network is reactivated
    396b56af0a
  4. DrahtBot added the label P2P on Feb 2, 2026
  5. willcl-ark commented at 5:47 pm on February 2, 2026: member

    This is an alternative to #34467

    I’m also unsure how this will conflict with the work currently being done on libevent.

  6. DrahtBot commented at 5:47 pm on February 2, 2026: contributor

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

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    Concept ACK sedited, fanquake, Jhackman2019

    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:

    • #34467 (net: don’t perform network activity when networkactive=0 by saksham-1304)
    • #34158 (torcontrol: Remove libevent usage by fjahr)
    • #30951 (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.

    LLM Linter (✨ experimental)

    Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

    • event_base_once(gBase, -1, EV_TIMEOUT, SetTorNetworkActive, nullptr, nullptr) in src/torcontrol.cpp

    2026-02-03 09:26:57

  7. sedited commented at 5:50 pm on February 2, 2026: contributor
    Concept ACK
  8. bradleystachurski commented at 9:42 pm on February 2, 2026: none

    Reviewed 35da4e03b18b7ef5eb23e1b038f5755fe37825a1

    Tested on Linux: started with -networkactive=0 -natpmp=1 -debug=net -debug=tor, confirmed no portmap/tor activity at startup, toggled via setnetworkactive, confirmed mapport thread start/stop and tor connection attempts follow network state.

    Verified feature_mapport.py fails when the m_mapport callback is removed from SetNetworkActive.

    nit: TorController::SetNetworkActive doesn’t reset reconnect_timeout, so re-enabling network after backoff has grown continues from the stale value. Verified this fixes it:

     0diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp
     1index d5aeb55a72..934499d72b 100644
     2--- a/src/torcontrol.cpp
     3+++ b/src/torcontrol.cpp
     4@@ -677,6 +677,8 @@ void TorController::SetNetworkActive(bool set_active)
     5         // Disconnect if currently connected
     6         conn.Disconnect();
     7     }
     8+    // Reset backoff when re-enabling network
     9+    reconnect_timeout = RECONNECT_TIMEOUT_START;
    10     // Connect handles both cases: connects if active, reschedules if inactive
    11     Connect();
    12 }
    
  9. net: run tor control based on networkactive
    When -networkactive=0 is set, Tor control connection attempts were still
    running and logging retry messages. Wire tor control lifecycle to
    CConnman similar to mapport: inject EnableTorControl as a callback via
    CConnman::Options, called from Start() and SetNetworkActive().
    
    TorController starts disconnected and waits for the callback from
    CConnman::Start() before connecting.
    
    The callback dispatches onto the tor control event loop via
    event_base_once rather than calling TorController::SetNetworkActive
    directly, since TorController state and libevent structures are owned by
    the event loop thread. This also means the gTorController pointer cannot
    be observed after destruction: once event_base_dispatch returns, no
    further callbacks execute.
    745aed3722
  10. willcl-ark force-pushed on Feb 3, 2026
  11. willcl-ark commented at 9:28 am on February 3, 2026: member

    Thanks @bradleystachurski that’s a nice suggestion which I’ve taken in 745aed37224102888332823aadc6d766e701aa0b

    I also reworked the if logic slightly to make the flow of this function clearer (and not always call Connect()).

  12. fanquake commented at 10:10 am on February 3, 2026: member
    Concept ACK
  13. willcl-ark commented at 10:25 am on February 3, 2026: member
    I will address the LLM linter suggestion if I push again.
  14. Jhackman2019 commented at 3:37 am on February 7, 2026: none

    Tested this on my Pi 5 (ARM64, Debian Bookworm). Builds clean, all the networking-related tests pass:

    0feature_mapport.py                  PASSED
    1feature_proxy.py                    PASSED
    2p2p_addr_relay.py                   PASSED
    3p2p_disconnect_ban.py --v1transport PASSED
    4p2p_disconnect_ban.py --v2transport PASSED
    5p2p_dns_seeds.py                    PASSED
    

    Had to clear my test/cache first since I had a stale cache from an autotools build — after that everything was smooth.

    Concept ACK


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-02-17 12:13 UTC

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