Skip to content

Commit 6b3f6ea

Browse files
committed
test: avoid generating non-loopback traffic from p2p_seednode.py
`p2p_seednode.py` would try to connect to `0.0.0.1` and `0.0.0.2` as seed nodes. This sends outbound TCP packets on a non-loopback interface to the default router. Configure an unavailable proxy for all executions of `bitcoind` during this test. Also change `0.0.0.1` and `0.0.0.2` because connecting to them would skip the `-proxy=` setting because for such an address: * `CNetAddr::IsLocal()` is true, thus * `CNetAddr::IsRoutable()` is false, thus * `CNetAddr::GetNetwork()` is `NET_UNROUTABLE`, even though `CNetAddr::m_net` is `NET_IPV4`. This speeds up the execution time of `p2p_seednode.py` from 12.5s to 2.5s.
1 parent 35bf426 commit 6b3f6ea

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

test/functional/p2p_seednode.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import random
1010
import time
1111

12+
from test_framework.netutil import UNREACHABLE_PROXY_ARG
1213
from test_framework.test_framework import BitcoinTestFramework
1314

1415
ADD_NEXT_SEEDNODE = 10
@@ -17,32 +18,34 @@
1718
class P2PSeedNodes(BitcoinTestFramework):
1819
def set_test_params(self):
1920
self.num_nodes = 1
21+
# Specify a non-working proxy to make sure no actual connections to random IPs are attempted.
22+
self.extra_args = [[UNREACHABLE_PROXY_ARG]]
2023
self.disable_autoconnect = False
2124

2225
def test_no_seednode(self):
2326
self.log.info("Check that if no seednode is provided, the node proceeds as usual (without waiting)")
2427
with self.nodes[0].assert_debug_log(expected_msgs=[], unexpected_msgs=["Empty addrman, adding seednode", f"Couldn't connect to peers from addrman after {ADD_NEXT_SEEDNODE} seconds. Adding seednode"], timeout=ADD_NEXT_SEEDNODE):
25-
self.restart_node(0)
28+
self.restart_node(0, extra_args=self.nodes[0].extra_args)
2629

2730
def test_seednode_empty_addrman(self):
28-
seed_node = "0.0.0.1"
31+
seed_node = "25.0.0.1"
2932
self.log.info("Check that the seednode is immediately added on bootstrap on an empty addrman")
3033
with self.nodes[0].assert_debug_log(expected_msgs=[f"Empty addrman, adding seednode ({seed_node}) to addrfetch"], timeout=ADD_NEXT_SEEDNODE):
31-
self.restart_node(0, extra_args=[f'-seednode={seed_node}'])
34+
self.restart_node(0, extra_args=self.nodes[0].extra_args + [f'-seednode={seed_node}'])
3235

3336
def test_seednode_non_empty_addrman(self):
3437
self.log.info("Check that if addrman is non-empty, seednodes are queried with a delay")
35-
seed_node = "0.0.0.2"
38+
seed_node = "25.0.0.2"
3639
node = self.nodes[0]
3740
# Fill the addrman with unreachable nodes
3841
for i in range(10):
3942
ip = f"{random.randrange(128,169)}.{random.randrange(1,255)}.{random.randrange(1,255)}.{random.randrange(1,255)}"
4043
port = 8333 + i
4144
node.addpeeraddress(ip, port)
4245

43-
# Restart the node so seednode is processed again. Specify a non-working proxy to make sure no actual connections to random IPs are attempted.
46+
# Restart the node so seednode is processed again.
4447
with node.assert_debug_log(expected_msgs=["trying v1 connection"], timeout=ADD_NEXT_SEEDNODE):
45-
self.restart_node(0, extra_args=[f'-seednode={seed_node}', '-proxy=127.0.0.1:1'])
48+
self.restart_node(0, extra_args=self.nodes[0].extra_args + [f'-seednode={seed_node}'])
4649

4750
with node.assert_debug_log(expected_msgs=[f"Couldn't connect to peers from addrman after {ADD_NEXT_SEEDNODE} seconds. Adding seednode ({seed_node}) to addrfetch"], unexpected_msgs=["Empty addrman, adding seednode"], timeout=ADD_NEXT_SEEDNODE * 1.5):
4851
node.setmocktime(int(time.time()) + ADD_NEXT_SEEDNODE + 1)

test/functional/test_framework/netutil.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import array
1414
import os
1515

16+
# Easily unreachable address. Attempts to connect to it will stay within the machine.
17+
# Used to avoid non-loopback traffic or DNS queries.
18+
UNREACHABLE_PROXY_ARG = '-proxy=127.0.0.1:1'
19+
1620
# STATE_ESTABLISHED = '01'
1721
# STATE_SYN_SENT = '02'
1822
# STATE_SYN_RECV = '03'

0 commit comments

Comments
 (0)