21
21
22
22
23
23
class P2PAddrFetch (BitcoinTestFramework ):
24
-
25
24
def set_test_params (self ):
26
25
self .setup_clean_chain = True
27
26
self .num_nodes = 1
28
27
28
+ def assert_getpeerinfo (self , * , peer_ids ):
29
+ num_peers = len (peer_ids )
30
+ info = self .nodes [0 ].getpeerinfo ()
31
+ assert_equal (len (info ), num_peers )
32
+ for n in range (0 , num_peers ):
33
+ assert_equal (info [n ]['id' ], peer_ids [n ])
34
+ assert_equal (info [n ]['connection_type' ], 'addr-fetch' )
35
+
29
36
def run_test (self ):
30
37
node = self .nodes [0 ]
31
38
self .log .info ("Connect to an addr-fetch peer" )
32
- peer = node .add_outbound_p2p_connection (P2PInterface (), p2p_idx = 0 , connection_type = "addr-fetch" )
33
- info = node .getpeerinfo ()
34
- assert_equal (len (info ), 1 )
35
- assert_equal (info [0 ]['connection_type' ], 'addr-fetch' )
39
+ peer_id = 0
40
+ peer = node .add_outbound_p2p_connection (P2PInterface (), p2p_idx = peer_id , connection_type = "addr-fetch" )
41
+ self .assert_getpeerinfo (peer_ids = [peer_id ])
36
42
37
43
self .log .info ("Check that we send getaddr but don't try to sync headers with the addr-fetch peer" )
38
44
peer .sync_send_with_ping ()
@@ -45,17 +51,28 @@ def run_test(self):
45
51
msg = msg_addr ()
46
52
msg .addrs = [ADDR ]
47
53
peer .send_and_ping (msg )
48
- assert_equal ( len ( node . getpeerinfo ()), 1 )
54
+ self . assert_getpeerinfo ( peer_ids = [ peer_id ] )
49
55
50
56
self .log .info ("Check that answering with larger addr messages leads to disconnect" )
51
57
msg .addrs = [ADDR ] * 2
52
58
peer .send_message (msg )
53
59
peer .wait_for_disconnect (timeout = 5 )
54
60
55
61
self .log .info ("Check timeout for addr-fetch peer that does not send addrs" )
56
- peer = node .add_outbound_p2p_connection (P2PInterface (), p2p_idx = 1 , connection_type = "addr-fetch" )
57
- node .setmocktime (int (time .time ()) + 301 ) # Timeout: 5 minutes
62
+ peer_id = 1
63
+ peer = node .add_outbound_p2p_connection (P2PInterface (), p2p_idx = peer_id , connection_type = "addr-fetch" )
64
+
65
+ time_now = int (time .time ())
66
+ self .assert_getpeerinfo (peer_ids = [peer_id ])
67
+
68
+ # Expect addr-fetch peer connection to be maintained up to 5 minutes.
69
+ node .setmocktime (time_now + 295 )
70
+ self .assert_getpeerinfo (peer_ids = [peer_id ])
71
+
72
+ # Expect addr-fetch peer connection to be disconnected after 5 minutes.
73
+ node .setmocktime (time_now + 301 )
58
74
peer .wait_for_disconnect (timeout = 5 )
75
+ self .assert_getpeerinfo (peer_ids = [])
59
76
60
77
61
78
if __name__ == '__main__' :
0 commit comments