Skip to content

Commit 6ee36a2

Browse files
author
MarcoFalke
committed
Merge #19473: net: Add -networkactive option
2aac093 test: Add test coverage for -networkactive option (Hennadii Stepanov) 3c58129 net: Log network activity status change unconditionally (Hennadii Stepanov) 62fe6aa net: Add -networkactive option (Hennadii Stepanov) Pull request description: Some Bitcoin Core activity is completely local (offline), e.g., reindexing. The `setnetworkactive` RPC command is already present. This PR adds the corresponding command-line argument / config option, and allows to start the client with disabled p2p network by providing `-networkactive=0` or `-nonetworkactive`. This was done while reviewing #16981. ACKs for top commit: MarcoFalke: re-ACK 2aac093 🏠 LarryRuane: ACK 2aac093 Tree-SHA512: 446d791b46d7b556d7694df7b1f88cd4fbc09301fe4eaf036b45cb8166ed806156353cc03788a07b633d5887d5eee30a7c02a2d4307141c8ccc75e0a88145636
2 parents 9d4b3d8 + 2aac093 commit 6ee36a2

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ void SetupServerArgs(NodeContext& node)
455455
gArgs.AddArg("-proxy=<ip:port>", "Connect through SOCKS5 proxy, set -noproxy to disable (default: disabled)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
456456
gArgs.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
457457
gArgs.AddArg("-seednode=<ip>", "Connect to a node to retrieve peer addresses, and disconnect. This option can be specified multiple times to connect to multiple nodes.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
458+
gArgs.AddArg("-networkactive", "Enable all P2P network activity (default: 1). Can be changed by the setnetworkactive RPC command", ArgsManager::ALLOW_BOOL, OptionsCategory::CONNECTION);
458459
gArgs.AddArg("-timeout=<n>", strprintf("Specify connection timeout in milliseconds (minimum: 1, default: %d)", DEFAULT_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
459460
gArgs.AddArg("-peertimeout=<n>", strprintf("Specify p2p connection timeout in seconds. This option determines the amount of time a peer may be inactive before the connection to it is dropped. (minimum: 1, default: %d)", DEFAULT_PEER_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CONNECTION);
460461
gArgs.AddArg("-torcontrol=<ip>:<port>", strprintf("Tor control port to use if onion listening enabled (default: %s)", DEFAULT_TOR_CONTROL), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1372,7 +1373,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
13721373
assert(!node.banman);
13731374
node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
13741375
assert(!node.connman);
1375-
node.connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
1376+
node.connman = MakeUnique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), gArgs.GetBoolArg("-networkactive", true));
13761377
// Make mempool generally available in the node context. For example the connection manager, wallet, or RPC threads,
13771378
// which are all started after this, may use it from the node context.
13781379
assert(!node.mempool);

src/net.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,7 +2253,7 @@ void Discover()
22532253

22542254
void CConnman::SetNetworkActive(bool active)
22552255
{
2256-
LogPrint(BCLog::NET, "SetNetworkActive: %s\n", active);
2256+
LogPrintf("%s: %s\n", __func__, active);
22572257

22582258
if (fNetworkActive == active) {
22592259
return;
@@ -2264,12 +2264,14 @@ void CConnman::SetNetworkActive(bool active)
22642264
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
22652265
}
22662266

2267-
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2267+
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, bool network_active)
2268+
: nSeed0(nSeed0In), nSeed1(nSeed1In)
22682269
{
22692270
SetTryNewOutboundPeer(false);
22702271

22712272
Options connOptions;
22722273
Init(connOptions);
2274+
SetNetworkActive(network_active);
22732275
}
22742276

22752277
NodeId CConnman::GetNewNodeId()

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class CConnman
181181
}
182182
}
183183

184-
CConnman(uint64_t seed0, uint64_t seed1);
184+
CConnman(uint64_t seed0, uint64_t seed1, bool network_active = true);
185185
~CConnman();
186186
bool Start(CScheduler& scheduler, const Options& options);
187187

test/functional/feature_config_args.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,38 @@ def test_args_log(self):
112112
])
113113
self.stop_node(0)
114114

115+
def test_networkactive(self):
116+
self.log.info('Test -networkactive option')
117+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
118+
self.start_node(0)
119+
self.stop_node(0)
120+
121+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
122+
self.start_node(0, extra_args=['-networkactive'])
123+
self.stop_node(0)
124+
125+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
126+
self.start_node(0, extra_args=['-networkactive=1'])
127+
self.stop_node(0)
128+
129+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
130+
self.start_node(0, extra_args=['-networkactive=0'])
131+
self.stop_node(0)
132+
133+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
134+
self.start_node(0, extra_args=['-nonetworkactive'])
135+
self.stop_node(0)
136+
137+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
138+
self.start_node(0, extra_args=['-nonetworkactive=1'])
139+
self.stop_node(0)
140+
115141
def run_test(self):
116142
self.stop_node(0)
117143

118144
self.test_log_buffer()
119145
self.test_args_log()
146+
self.test_networkactive()
120147

121148
self.test_config_file_parser()
122149

test/functional/rpc_net.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ def _test_getnetworkinfo(self):
102102
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], True)
103103
assert_equal(self.nodes[0].getnetworkinfo()['connections'], 2)
104104

105-
self.nodes[0].setnetworkactive(state=False)
105+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: false\n']):
106+
self.nodes[0].setnetworkactive(state=False)
106107
assert_equal(self.nodes[0].getnetworkinfo()['networkactive'], False)
107108
# Wait a bit for all sockets to close
108109
wait_until(lambda: self.nodes[0].getnetworkinfo()['connections'] == 0, timeout=3)
109110

110-
self.nodes[0].setnetworkactive(state=True)
111+
with self.nodes[0].assert_debug_log(expected_msgs=['SetNetworkActive: true\n']):
112+
self.nodes[0].setnetworkactive(state=True)
111113
self.log.info('Connect nodes both way')
112114
connect_nodes(self.nodes[0], 1)
113115
connect_nodes(self.nodes[1], 0)

0 commit comments

Comments
 (0)