2017-11-20 21:57:34 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2017 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-12-11 18:08:33 +01:00
|
|
|
from test_framework.messages import CInv, msg_getdata
|
2017-12-11 17:56:24 +01:00
|
|
|
from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS, NetworkThread, P2PInterface
|
2017-11-20 21:57:34 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2017-12-11 17:55:54 +01:00
|
|
|
from test_framework.util import assert_equal
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 18:08:33 +01:00
|
|
|
class P2PIgnoreInv(P2PInterface):
|
|
|
|
def on_inv(self, message):
|
|
|
|
# The node will send us invs for other blocks. Ignore them.
|
|
|
|
pass
|
2017-11-20 21:57:34 +01:00
|
|
|
|
|
|
|
class NodeNetworkLimitedTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 1
|
|
|
|
self.extra_args = [['-prune=550']]
|
|
|
|
|
2017-12-11 17:55:54 +01:00
|
|
|
def get_signalled_service_flags(self):
|
2017-12-11 18:08:33 +01:00
|
|
|
node = self.nodes[0].add_p2p_connection(P2PIgnoreInv())
|
2017-11-20 21:57:34 +01:00
|
|
|
NetworkThread().start()
|
|
|
|
node.wait_for_verack()
|
|
|
|
services = node.nServices
|
|
|
|
self.nodes[0].disconnect_p2ps()
|
|
|
|
node.wait_for_disconnect()
|
|
|
|
return services
|
|
|
|
|
2017-12-11 17:55:54 +01:00
|
|
|
def try_get_block_via_getdata(self, blockhash, must_disconnect):
|
2017-12-11 18:08:33 +01:00
|
|
|
node = self.nodes[0].add_p2p_connection(P2PIgnoreInv())
|
2017-11-20 21:57:34 +01:00
|
|
|
NetworkThread().start()
|
|
|
|
node.wait_for_verack()
|
|
|
|
getdata_request = msg_getdata()
|
|
|
|
getdata_request.inv.append(CInv(2, int(blockhash, 16)))
|
|
|
|
node.send_message(getdata_request)
|
|
|
|
|
|
|
|
if (must_disconnect):
|
2017-12-11 17:55:54 +01:00
|
|
|
# Ensure we get disconnected
|
2017-11-20 21:57:34 +01:00
|
|
|
node.wait_for_disconnect(5)
|
|
|
|
else:
|
|
|
|
# check if the peer sends us the requested block
|
|
|
|
node.wait_for_block(int(blockhash, 16), 3)
|
|
|
|
self.nodes[0].disconnect_p2ps()
|
|
|
|
node.wait_for_disconnect()
|
|
|
|
|
|
|
|
def run_test(self):
|
2017-12-11 17:55:54 +01:00
|
|
|
# NODE_BLOOM & NODE_WITNESS & NODE_NETWORK_LIMITED must now be signaled
|
2017-12-11 17:56:24 +01:00
|
|
|
assert_equal(self.get_signalled_service_flags(), NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED)
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 17:55:54 +01:00
|
|
|
# Now mine some blocks over the NODE_NETWORK_LIMITED + 2(racy buffer ext.) target
|
2017-11-20 21:57:34 +01:00
|
|
|
firstblock = self.nodes[0].generate(1)[0]
|
|
|
|
blocks = self.nodes[0].generate(292)
|
2017-12-11 17:55:54 +01:00
|
|
|
block_within_limited_range = blocks[-1]
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 17:55:54 +01:00
|
|
|
# Make sure we can max retrive block at tip-288
|
|
|
|
# requesting block at height 2 (tip-289) must fail (ignored)
|
|
|
|
self.try_get_block_via_getdata(firstblock, True) # first block must lead to disconnect
|
|
|
|
self.try_get_block_via_getdata(blocks[1], False) # last block in valid range
|
|
|
|
self.try_get_block_via_getdata(blocks[0], True) # first block outside of the 288+2 limit
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 17:55:54 +01:00
|
|
|
# NODE_NETWORK_LIMITED must still be signaled after restart
|
2017-11-20 21:57:34 +01:00
|
|
|
self.restart_node(0)
|
2017-12-11 17:56:24 +01:00
|
|
|
assert_equal(self.get_signalled_service_flags(), NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED)
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 17:55:54 +01:00
|
|
|
# Test the RPC service flags
|
2017-12-11 17:56:24 +01:00
|
|
|
assert_equal(int(self.nodes[0].getnetworkinfo()['localservices'], 16), NODE_BLOOM | NODE_WITNESS | NODE_NETWORK_LIMITED)
|
2017-11-20 21:57:34 +01:00
|
|
|
|
|
|
|
# getdata a block above the NODE_NETWORK_LIMITED threshold must be possible
|
2017-12-11 17:55:54 +01:00
|
|
|
self.try_get_block_via_getdata(block_within_limited_range, False)
|
2017-11-20 21:57:34 +01:00
|
|
|
|
|
|
|
# getdata a block below the NODE_NETWORK_LIMITED threshold must be ignored
|
2017-12-11 17:55:54 +01:00
|
|
|
self.try_get_block_via_getdata(firstblock, True)
|
2017-11-20 21:57:34 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
NodeNetworkLimitedTest().main()
|