p2p_seednode.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2019-present The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 6 """ 7 Test seednode interaction with the AddrMan 8 """ 9 import random 10 import time 11 12 from test_framework.netutil import UNREACHABLE_PROXY_ARG 13 from test_framework.test_framework import BitcoinTestFramework 14 15 ADD_NEXT_SEEDNODE = 10 16 17 18 class P2PSeedNodes(BitcoinTestFramework): 19 def set_test_params(self): 20 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]] 23 self.disable_autoconnect = False 24 25 def test_no_seednode(self): 26 self.log.info("Check that if no seednode is provided, the node proceeds as usual (without waiting)") 27 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): 28 self.restart_node(0, extra_args=self.nodes[0].extra_args) 29 30 def test_seednode_empty_addrman(self): 31 seed_node = "25.0.0.1" 32 self.log.info("Check that the seednode is immediately added on bootstrap on an empty addrman") 33 with self.nodes[0].assert_debug_log(expected_msgs=[f"Empty addrman, adding seednode ({seed_node}) to addrfetch"], timeout=ADD_NEXT_SEEDNODE): 34 self.restart_node(0, extra_args=self.nodes[0].extra_args + [f'-seednode={seed_node}']) 35 36 def test_seednode_non_empty_addrman(self): 37 self.log.info("Check that if addrman is non-empty, seednodes are queried with a delay") 38 seed_node = "25.0.0.2" 39 node = self.nodes[0] 40 # Fill the addrman with unreachable nodes 41 for i in range(10): 42 ip = f"{random.randrange(128,169)}.{random.randrange(1,255)}.{random.randrange(1,255)}.{random.randrange(1,255)}" 43 port = 8333 + i 44 node.addpeeraddress(ip, port) 45 46 # Restart the node so seednode is processed again. 47 with node.assert_debug_log(expected_msgs=["trying v1 connection"], timeout=ADD_NEXT_SEEDNODE): 48 self.restart_node(0, extra_args=self.nodes[0].extra_args + [f'-seednode={seed_node}']) 49 50 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): 51 node.setmocktime(int(time.time()) + ADD_NEXT_SEEDNODE + 1) 52 53 def run_test(self): 54 self.test_no_seednode() 55 self.test_seednode_empty_addrman() 56 self.test_seednode_non_empty_addrman() 57 58 59 if __name__ == '__main__': 60 P2PSeedNodes(__file__).main()