Skip to content

Commit ee5efad

Browse files
committed
[tests] refactor node_network_limited
1 parent b425131 commit ee5efad

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

test/functional/node_network_limited.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# Copyright (c) 2017 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# 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."""
511
from test_framework.messages import CInv, msg_getdata
612
from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS, NetworkThread, P2PInterface
713
from test_framework.test_framework import BitcoinTestFramework
@@ -12,52 +18,40 @@ def on_inv(self, message):
1218
# The node will send us invs for other blocks. Ignore them.
1319
pass
1420

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+
1526
class NodeNetworkLimitedTest(BitcoinTestFramework):
1627
def set_test_params(self):
1728
self.setup_clean_chain = True
1829
self.num_nodes = 1
1930
self.extra_args = [['-prune=550']]
2031

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):
3133
node = self.nodes[0].add_p2p_connection(P2PIgnoreInv())
3234
NetworkThread().start()
3335
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)
3736

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
4638

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)
5041

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)
5344

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.")
5546
blocks = self.nodes[0].generate(292)
5647

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)
6155

6256
if __name__ == '__main__':
6357
NodeNetworkLimitedTest().main()

0 commit comments

Comments
 (0)