2
2
# Copyright (c) 2017 The Bitcoin Core developers
3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
+ """Tests NODE_NETWORK_LIMITED.
6
+
7
+ Tests that a node configured with -prune=550 signals NODE_NETWORK_LIMITED correctly
8
+ and that it responds to getdata requests for blocks correctly:
9
+ - send a block within 288 + 2 of the tip
10
+ - disconnect peers who request blocks older than that."""
5
11
from test_framework .messages import CInv , msg_getdata
6
12
from test_framework .mininode import NODE_BLOOM , NODE_NETWORK_LIMITED , NODE_WITNESS , NetworkThread , P2PInterface
7
13
from test_framework .test_framework import BitcoinTestFramework
@@ -12,52 +18,40 @@ def on_inv(self, message):
12
18
# The node will send us invs for other blocks. Ignore them.
13
19
pass
14
20
21
+ def send_getdata_for_block (self , blockhash ):
22
+ getdata_request = msg_getdata ()
23
+ getdata_request .inv .append (CInv (2 , int (blockhash , 16 )))
24
+ self .send_message (getdata_request )
25
+
15
26
class NodeNetworkLimitedTest (BitcoinTestFramework ):
16
27
def set_test_params (self ):
17
28
self .setup_clean_chain = True
18
29
self .num_nodes = 1
19
30
self .extra_args = [['-prune=550' ]]
20
31
21
- def get_signalled_service_flags (self ):
22
- node = self .nodes [0 ].add_p2p_connection (P2PIgnoreInv ())
23
- NetworkThread ().start ()
24
- node .wait_for_verack ()
25
- services = node .nServices
26
- self .nodes [0 ].disconnect_p2ps ()
27
- node .wait_for_disconnect ()
28
- return services
29
-
30
- def try_get_block_via_getdata (self , blockhash , must_disconnect ):
32
+ def run_test (self ):
31
33
node = self .nodes [0 ].add_p2p_connection (P2PIgnoreInv ())
32
34
NetworkThread ().start ()
33
35
node .wait_for_verack ()
34
- getdata_request = msg_getdata ()
35
- getdata_request .inv .append (CInv (2 , int (blockhash , 16 )))
36
- node .send_message (getdata_request )
37
36
38
- if (must_disconnect ):
39
- # Ensure we get disconnected
40
- node .wait_for_disconnect (5 )
41
- else :
42
- # check if the peer sends us the requested block
43
- node .wait_for_block (int (blockhash , 16 ), 3 )
44
- self .nodes [0 ].disconnect_p2ps ()
45
- node .wait_for_disconnect ()
37
+ expected_services = NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED
46
38
47
- def run_test (self ):
48
- # NODE_BLOOM & NODE_WITNESS & NODE_NETWORK_LIMITED must now be signaled
49
- assert_equal (self .get_signalled_service_flags (), NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED )
39
+ self .log .info ("Check that node has signalled expected services." )
40
+ assert_equal (node .nServices , expected_services )
50
41
51
- # Test the RPC service flags
52
- assert_equal (int (self .nodes [0 ].getnetworkinfo ()['localservices' ], 16 ), NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED )
42
+ self . log . info ( "Check that the localservices is as expected." )
43
+ assert_equal (int (self .nodes [0 ].getnetworkinfo ()['localservices' ], 16 ), expected_services )
53
44
54
- # Now mine some blocks over the NODE_NETWORK_LIMITED + 2(racy buffer ext.) target
45
+ self . log . info ( "Mine enough blocks to reach the NODE_NETWORK_LIMITED range." )
55
46
blocks = self .nodes [0 ].generate (292 )
56
47
57
- # Make sure we can max retrive block at tip-288
58
- # requesting block at height 2 (tip-289) must fail (ignored)
59
- self .try_get_block_via_getdata (blocks [1 ], False ) # last block in valid range
60
- self .try_get_block_via_getdata (blocks [0 ], True ) # first block outside of the 288+2 limit
48
+ self .log .info ("Make sure we can max retrive block at tip-288." )
49
+ node .send_getdata_for_block (blocks [1 ]) # last block in valid range
50
+ node .wait_for_block (int (blocks [1 ], 16 ), timeout = 3 )
51
+
52
+ self .log .info ("Requesting block at height 2 (tip-289) must fail (ignored)." )
53
+ node .send_getdata_for_block (blocks [0 ]) # first block outside of the 288+2 limit
54
+ node .wait_for_disconnect (5 )
61
55
62
56
if __name__ == '__main__' :
63
57
NodeNetworkLimitedTest ().main ()
0 commit comments