6
6
Test P2P behaviour during the handshake phase (VERSION, VERACK messages).
7
7
"""
8
8
import itertools
9
+ import time
9
10
10
11
from test_framework .test_framework import BitcoinTestFramework
11
12
from test_framework .messages import (
12
13
NODE_NETWORK ,
14
+ NODE_NETWORK_LIMITED ,
13
15
NODE_NONE ,
14
16
NODE_P2P_V2 ,
15
17
NODE_WITNESS ,
16
18
)
17
19
from test_framework .p2p import P2PInterface
18
20
19
21
20
- # usual desirable service flags for outbound non-pruned peers
21
- DESIRABLE_SERVICE_FLAGS = NODE_NETWORK | NODE_WITNESS
22
+ # Desirable service flags for outbound non-pruned and pruned peers. Note that
23
+ # the desirable service flags for pruned peers are dynamic and only apply if
24
+ # 1. the peer's service flag NODE_NETWORK_LIMITED is set *and*
25
+ # 2. the local chain is close to the tip (<24h)
26
+ DESIRABLE_SERVICE_FLAGS_FULL = NODE_NETWORK | NODE_WITNESS
27
+ DESIRABLE_SERVICE_FLAGS_PRUNED = NODE_NETWORK_LIMITED | NODE_WITNESS
22
28
23
29
24
30
class P2PHandshakeTest (BitcoinTestFramework ):
@@ -36,7 +42,7 @@ def add_outbound_connection(self, node, connection_type, services, wait_for_disc
36
42
peer .peer_disconnect ()
37
43
peer .wait_for_disconnect ()
38
44
39
- def test_desirable_service_flags (self , node , service_flag_tests , expect_disconnect ):
45
+ def test_desirable_service_flags (self , node , service_flag_tests , desirable_service_flags , expect_disconnect ):
40
46
"""Check that connecting to a peer either fails or succeeds depending on its offered
41
47
service flags in the VERSION message. The test is exercised for all relevant
42
48
outbound connection types where the desirable service flags check is done."""
@@ -47,18 +53,30 @@ def test_desirable_service_flags(self, node, service_flag_tests, expect_disconne
47
53
expected_result = "disconnect" if expect_disconnect else "connect"
48
54
self .log .info (f' - services 0x{ services :08x} , type "{ conn_type } " [{ expected_result } ]' )
49
55
if expect_disconnect :
56
+ assert (services & desirable_service_flags ) != desirable_service_flags
50
57
expected_debug_log = f'does not offer the expected services ' \
51
- f'({ services :08x} offered, { DESIRABLE_SERVICE_FLAGS :08x} expected)'
58
+ f'({ services :08x} offered, { desirable_service_flags :08x} expected)'
52
59
with node .assert_debug_log ([expected_debug_log ]):
53
60
self .add_outbound_connection (node , conn_type , services , wait_for_disconnect = True )
54
61
else :
62
+ assert (services & desirable_service_flags ) == desirable_service_flags
55
63
self .add_outbound_connection (node , conn_type , services , wait_for_disconnect = False )
56
64
57
65
def run_test (self ):
58
66
node = self .nodes [0 ]
59
67
self .log .info ("Check that lacking desired service flags leads to disconnect (non-pruned peers)" )
60
- self .test_desirable_service_flags (node , [NODE_NONE , NODE_NETWORK , NODE_WITNESS ], expect_disconnect = True )
61
- self .test_desirable_service_flags (node , [NODE_NETWORK | NODE_WITNESS ], expect_disconnect = False )
68
+ self .test_desirable_service_flags (node , [NODE_NONE , NODE_NETWORK , NODE_WITNESS ],
69
+ DESIRABLE_SERVICE_FLAGS_FULL , expect_disconnect = True )
70
+ self .test_desirable_service_flags (node , [NODE_NETWORK | NODE_WITNESS ],
71
+ DESIRABLE_SERVICE_FLAGS_FULL , expect_disconnect = False )
72
+
73
+ self .log .info ("Check that limited peers are only desired if the local chain is close to the tip (<24h)" )
74
+ node .setmocktime (int (time .time ()) + 25 * 3600 ) # tip outside the 24h window, should fail
75
+ self .test_desirable_service_flags (node , [NODE_NETWORK_LIMITED | NODE_WITNESS ],
76
+ DESIRABLE_SERVICE_FLAGS_FULL , expect_disconnect = True )
77
+ node .setmocktime (int (time .time ()) + 23 * 3600 ) # tip inside the 24h window, should succeed
78
+ self .test_desirable_service_flags (node , [NODE_NETWORK_LIMITED | NODE_WITNESS ],
79
+ DESIRABLE_SERVICE_FLAGS_PRUNED , expect_disconnect = False )
62
80
63
81
self .log .info ("Check that feeler connections get disconnected immediately" )
64
82
with node .assert_debug_log ([f"feeler connection completed" ]):
0 commit comments