Skip to content

Commit 18a1bba

Browse files
author
MarcoFalke
committed
Merge #11867: Improve node network test
ee5efad [tests] refactor node_network_limited (John Newbery) b425131 [tests] remove redundant duplicate tests from node_network_limited (John Newbery) 2e02984 [tests] node_network_limited - remove race condition (John Newbery) dbfe294 [tests] define NODE_NETWORK_LIMITED in test framework (John Newbery) 1285312 [tests] fix flake8 warnings in node_network_limited.py (John Newbery) Pull request description: Fixes race condition in the node_network_limited test case introduced in #11740. Also tidies up the test and removes redundant duplicate tests. Tree-SHA512: a5240fe35509d81a47c3d3b141a956378675926093e658d24be43027b20d3b5f0ba7c6017c8208487a1849d4fdfb911a361911d571423db7c50711250aba3011
2 parents cdd6bbf + ee5efad commit 18a1bba

File tree

2 files changed

+34
-56
lines changed

2 files changed

+34
-56
lines changed

test/functional/node_network_limited.py

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,56 @@
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."""
11+
from test_framework.messages import CInv, msg_getdata
12+
from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS, NetworkThread, P2PInterface
513
from test_framework.test_framework import BitcoinTestFramework
6-
from test_framework.util import *
7-
from test_framework.mininode import *
14+
from test_framework.util import assert_equal
815

9-
class BaseNode(P2PInterface):
10-
nServices = 0
11-
def on_version(self, message):
12-
self.nServices = message.nServices
16+
class P2PIgnoreInv(P2PInterface):
17+
def on_inv(self, message):
18+
# The node will send us invs for other blocks. Ignore them.
19+
pass
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)
1325

1426
class NodeNetworkLimitedTest(BitcoinTestFramework):
1527
def set_test_params(self):
1628
self.setup_clean_chain = True
1729
self.num_nodes = 1
1830
self.extra_args = [['-prune=550']]
1931

20-
def getSignaledServiceFlags(self):
21-
node = self.nodes[0].add_p2p_connection(BaseNode())
32+
def run_test(self):
33+
node = self.nodes[0].add_p2p_connection(P2PIgnoreInv())
2234
NetworkThread().start()
2335
node.wait_for_verack()
24-
services = node.nServices
25-
self.nodes[0].disconnect_p2ps()
26-
node.wait_for_disconnect()
27-
return services
2836

29-
def tryGetBlockViaGetData(self, blockhash, must_disconnect):
30-
node = self.nodes[0].add_p2p_connection(BaseNode())
31-
NetworkThread().start()
32-
node.wait_for_verack()
33-
node.send_message(msg_verack())
34-
getdata_request = msg_getdata()
35-
getdata_request.inv.append(CInv(2, int(blockhash, 16)))
36-
node.send_message(getdata_request)
37+
expected_services = NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED
3738

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()
39+
self.log.info("Check that node has signalled expected services.")
40+
assert_equal(node.nServices, expected_services)
4641

47-
def run_test(self):
48-
#NODE_BLOOM & NODE_WITNESS & NODE_NETWORK_LIMITED must now be signaled
49-
assert_equal(self.getSignaledServiceFlags(), 1036) #1036 == 0x40C == 0100 0000 1100
50-
# | ||
51-
# | |^--- NODE_BLOOM
52-
# | ^---- NODE_WITNESS
53-
# ^-- 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)
5444

55-
#now mine some blocks over the NODE_NETWORK_LIMITED + 2(racy buffer ext.) target
56-
firstblock = self.nodes[0].generate(1)[0]
45+
self.log.info("Mine enough blocks to reach the NODE_NETWORK_LIMITED range.")
5746
blocks = self.nodes[0].generate(292)
58-
blockWithinLimitedRange = blocks[-1]
59-
60-
#make sure we can max retrive block at tip-288
61-
#requesting block at height 2 (tip-289) must fail (ignored)
62-
self.tryGetBlockViaGetData(firstblock, True) #first block must lead to disconnect
63-
self.tryGetBlockViaGetData(blocks[1], False) #last block in valid range
64-
self.tryGetBlockViaGetData(blocks[0], True) #first block outside of the 288+2 limit
65-
66-
#NODE_NETWORK_LIMITED must still be signaled after restart
67-
self.restart_node(0)
68-
assert_equal(self.getSignaledServiceFlags(), 1036)
69-
70-
#test the RPC service flags
71-
assert_equal(self.nodes[0].getnetworkinfo()['localservices'], "000000000000040c")
7247

73-
# getdata a block above the NODE_NETWORK_LIMITED threshold must be possible
74-
self.tryGetBlockViaGetData(blockWithinLimitedRange, False)
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)
7551

76-
# getdata a block below the NODE_NETWORK_LIMITED threshold must be ignored
77-
self.tryGetBlockViaGetData(firstblock, True)
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)
7855

7956
if __name__ == '__main__':
8057
NodeNetworkLimitedTest().main()

test/functional/test_framework/messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838

3939
NODE_NETWORK = (1 << 0)
4040
# NODE_GETUTXO = (1 << 1)
41-
# NODE_BLOOM = (1 << 2)
41+
NODE_BLOOM = (1 << 2)
4242
NODE_WITNESS = (1 << 3)
4343
NODE_UNSUPPORTED_SERVICE_BIT_5 = (1 << 5)
4444
NODE_UNSUPPORTED_SERVICE_BIT_7 = (1 << 7)
45+
NODE_NETWORK_LIMITED = (1 << 10)
4546

4647
# Serialization/deserialization tools
4748
def sha256(s):

0 commit comments

Comments
 (0)