Skip to content

Commit e054d0e

Browse files
committed
[QA] Add node_network_limited test
1 parent bd09416 commit e054d0e

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
from test_framework.test_framework import BitcoinTestFramework
6+
from test_framework.util import *
7+
from test_framework.mininode import *
8+
9+
class BaseNode(P2PInterface):
10+
nServices = 0
11+
firstAddrnServices = 0
12+
def on_version(self, message):
13+
self.nServices = message.nServices
14+
15+
class NodeNetworkLimitedTest(BitcoinTestFramework):
16+
def set_test_params(self):
17+
self.setup_clean_chain = True
18+
self.num_nodes = 1
19+
self.extra_args = [['-prune=550']]
20+
21+
def getSignaledServiceFlags(self):
22+
node = self.nodes[0].add_p2p_connection(BaseNode())
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 tryGetBlockViaGetData(self, blockhash, must_disconnect):
31+
node = self.nodes[0].add_p2p_connection(BaseNode())
32+
NetworkThread().start()
33+
node.wait_for_verack()
34+
node.send_message(msg_verack())
35+
getdata_request = msg_getdata()
36+
getdata_request.inv.append(CInv(2, int(blockhash, 16)))
37+
node.send_message(getdata_request)
38+
39+
if (must_disconnect):
40+
#ensure we get disconnected
41+
node.wait_for_disconnect(5)
42+
else:
43+
# check if the peer sends us the requested block
44+
node.wait_for_block(int(blockhash, 16), 3)
45+
self.nodes[0].disconnect_p2ps()
46+
node.wait_for_disconnect()
47+
48+
def run_test(self):
49+
#NODE_BLOOM & NODE_WITNESS & NODE_NETWORK_LIMITED must now be signaled
50+
assert_equal(self.getSignaledServiceFlags(), 1036) #1036 == 0x40C == 0100 0000 1100
51+
# | ||
52+
# | |^--- NODE_BLOOM
53+
# | ^---- NODE_WITNESS
54+
# ^-- NODE_NETWORK_LIMITED
55+
56+
#now mine some blocks over the NODE_NETWORK_LIMITED + 2(racy buffer ext.) target
57+
firstblock = self.nodes[0].generate(1)[0]
58+
blocks = self.nodes[0].generate(292)
59+
blockWithinLimitedRange = blocks[-1]
60+
61+
#make sure we can max retrive block at tip-288
62+
#requesting block at height 2 (tip-289) must fail (ignored)
63+
self.tryGetBlockViaGetData(firstblock, True) #first block must lead to disconnect
64+
self.tryGetBlockViaGetData(blocks[1], False) #last block in valid range
65+
self.tryGetBlockViaGetData(blocks[0], True) #first block outside of the 288+2 limit
66+
67+
#NODE_NETWORK_LIMITED must still be signaled after restart
68+
self.restart_node(0)
69+
assert_equal(self.getSignaledServiceFlags(), 1036)
70+
71+
#test the RPC service flags
72+
assert_equal(self.nodes[0].getnetworkinfo()['localservices'], "000000000000040c")
73+
74+
# getdata a block above the NODE_NETWORK_LIMITED threshold must be possible
75+
self.tryGetBlockViaGetData(blockWithinLimitedRange, False)
76+
77+
# getdata a block below the NODE_NETWORK_LIMITED threshold must be ignored
78+
self.tryGetBlockViaGetData(firstblock, True)
79+
80+
if __name__ == '__main__':
81+
NodeNetworkLimitedTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
'uacomment.py',
129129
'p2p-acceptblock.py',
130130
'feature_logging.py',
131+
'node_network_limited.py',
131132
]
132133

133134
EXTENDED_SCRIPTS = [

0 commit comments

Comments
 (0)