2017-11-20 21:57:34 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-04-07 00:38:51 +02:00
|
|
|
# Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-11-20 21:57:34 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-12-11 18:51:14 +01:00
|
|
|
"""Tests NODE_NETWORK_LIMITED.
|
|
|
|
|
|
|
|
Tests that a node configured with -prune=550 signals NODE_NETWORK_LIMITED correctly
|
|
|
|
and that it responds to getdata requests for blocks correctly:
|
|
|
|
- send a block within 288 + 2 of the tip
|
|
|
|
- disconnect peers who request blocks older than that."""
|
2020-04-25 13:44:44 +02:00
|
|
|
from test_framework.messages import CInv, MSG_BLOCK, msg_getdata, msg_verack, NODE_NETWORK_LIMITED, NODE_WITNESS
|
2020-08-17 17:50:47 +02:00
|
|
|
from test_framework.p2p import P2PInterface
|
2017-11-20 21:57:34 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2019-04-07 00:38:51 +02:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
disconnect_nodes,
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes,
|
2019-04-07 00:38:51 +02:00
|
|
|
)
|
|
|
|
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 18:08:33 +01:00
|
|
|
class P2PIgnoreInv(P2PInterface):
|
2018-01-09 08:00:35 +01:00
|
|
|
firstAddrnServices = 0
|
2017-12-11 18:08:33 +01:00
|
|
|
def on_inv(self, message):
|
|
|
|
# The node will send us invs for other blocks. Ignore them.
|
|
|
|
pass
|
2018-01-09 08:00:35 +01:00
|
|
|
def on_addr(self, message):
|
|
|
|
self.firstAddrnServices = message.addrs[0].nServices
|
|
|
|
def wait_for_addr(self, timeout=5):
|
|
|
|
test_function = lambda: self.last_message.get("addr")
|
2020-08-17 17:50:47 +02:00
|
|
|
self.wait_until(test_function, timeout=timeout)
|
2017-12-11 18:51:14 +01:00
|
|
|
def send_getdata_for_block(self, blockhash):
|
|
|
|
getdata_request = msg_getdata()
|
2020-04-25 13:44:44 +02:00
|
|
|
getdata_request.inv.append(CInv(MSG_BLOCK, int(blockhash, 16)))
|
2017-12-11 18:51:14 +01:00
|
|
|
self.send_message(getdata_request)
|
|
|
|
|
2017-11-20 21:57:34 +01:00
|
|
|
class NodeNetworkLimitedTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.setup_clean_chain = True
|
2018-01-09 08:00:35 +01:00
|
|
|
self.num_nodes = 3
|
|
|
|
self.extra_args = [['-prune=550', '-addrmantest'], [], []]
|
|
|
|
|
|
|
|
def disconnect_all(self):
|
|
|
|
disconnect_nodes(self.nodes[0], 1)
|
|
|
|
disconnect_nodes(self.nodes[0], 2)
|
|
|
|
disconnect_nodes(self.nodes[1], 2)
|
|
|
|
|
|
|
|
def setup_network(self):
|
2018-10-16 17:21:07 +02:00
|
|
|
self.add_nodes(self.num_nodes, self.extra_args)
|
|
|
|
self.start_nodes()
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 18:51:14 +01:00
|
|
|
def run_test(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
|
|
|
|
Disable bloom filtering by default.
BIP 37 bloom filters have been well-known to be a significant DoS
target for some time. However, in order to provide continuity for
SPV clients relying on it, the NODE_BLOOM service flag was added,
and left as a default, to ensure sufficient nodes exist with such a
flag.
NODE_BLOOM is, at this point, well-established and, as long as
there exist 0.18 nodes with default config (which I'd anticipate
will be true for many years), will be available from some peers. By
that time, the continued slowdown of BIP 37-based filtering will
likely have rendered it useless (though this is already largely the
case). Further, BIP 37 was deliberately never updated to support
witness-based filtering as newer wallets are expected to migrate to
some yet-to-be-network-exposed filters.
2019-06-05 17:02:35 +02:00
|
|
|
expected_services = NODE_WITNESS | NODE_NETWORK_LIMITED
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 18:51:14 +01:00
|
|
|
self.log.info("Check that node has signalled expected services.")
|
|
|
|
assert_equal(node.nServices, expected_services)
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2017-12-11 18:51:14 +01:00
|
|
|
self.log.info("Check that the localservices is as expected.")
|
|
|
|
assert_equal(int(self.nodes[0].getnetworkinfo()['localservices'], 16), expected_services)
|
2017-12-11 18:38:50 +01:00
|
|
|
|
2017-12-11 18:51:14 +01:00
|
|
|
self.log.info("Mine enough blocks to reach the NODE_NETWORK_LIMITED range.")
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2018-09-15 11:32:12 +02:00
|
|
|
blocks = self.nodes[1].generatetoaddress(292, self.nodes[1].get_deterministic_priv_key().address)
|
2019-04-07 00:19:45 +02:00
|
|
|
self.sync_blocks([self.nodes[0], self.nodes[1]])
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2018-03-18 15:26:45 +01:00
|
|
|
self.log.info("Make sure we can max retrieve block at tip-288.")
|
2017-12-11 18:51:14 +01:00
|
|
|
node.send_getdata_for_block(blocks[1]) # last block in valid range
|
|
|
|
node.wait_for_block(int(blocks[1], 16), timeout=3)
|
|
|
|
|
|
|
|
self.log.info("Requesting block at height 2 (tip-289) must fail (ignored).")
|
|
|
|
node.send_getdata_for_block(blocks[0]) # first block outside of the 288+2 limit
|
|
|
|
node.wait_for_disconnect(5)
|
2017-11-20 21:57:34 +01:00
|
|
|
|
2018-01-09 08:00:35 +01:00
|
|
|
self.log.info("Check local address relay, do a fresh connection.")
|
|
|
|
self.nodes[0].disconnect_p2ps()
|
|
|
|
node1 = self.nodes[0].add_p2p_connection(P2PIgnoreInv())
|
|
|
|
node1.send_message(msg_verack())
|
|
|
|
|
|
|
|
node1.wait_for_addr()
|
|
|
|
#must relay address with NODE_NETWORK_LIMITED
|
Disable bloom filtering by default.
BIP 37 bloom filters have been well-known to be a significant DoS
target for some time. However, in order to provide continuity for
SPV clients relying on it, the NODE_BLOOM service flag was added,
and left as a default, to ensure sufficient nodes exist with such a
flag.
NODE_BLOOM is, at this point, well-established and, as long as
there exist 0.18 nodes with default config (which I'd anticipate
will be true for many years), will be available from some peers. By
that time, the continued slowdown of BIP 37-based filtering will
likely have rendered it useless (though this is already largely the
case). Further, BIP 37 was deliberately never updated to support
witness-based filtering as newer wallets are expected to migrate to
some yet-to-be-network-exposed filters.
2019-06-05 17:02:35 +02:00
|
|
|
assert_equal(node1.firstAddrnServices, expected_services)
|
2018-01-09 08:00:35 +01:00
|
|
|
|
|
|
|
self.nodes[0].disconnect_p2ps()
|
|
|
|
|
|
|
|
# connect unsynced node 2 with pruned NODE_NETWORK_LIMITED peer
|
|
|
|
# because node 2 is in IBD and node 0 is a NODE_NETWORK_LIMITED peer, sync must not be possible
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 2)
|
2018-01-09 08:00:35 +01:00
|
|
|
try:
|
2019-04-07 00:19:45 +02:00
|
|
|
self.sync_blocks([self.nodes[0], self.nodes[2]], timeout=5)
|
2018-01-09 08:00:35 +01:00
|
|
|
except:
|
|
|
|
pass
|
2018-08-14 09:27:30 +02:00
|
|
|
# node2 must remain at height 0
|
2018-01-09 08:00:35 +01:00
|
|
|
assert_equal(self.nodes[2].getblockheader(self.nodes[2].getbestblockhash())['height'], 0)
|
|
|
|
|
|
|
|
# now connect also to node 1 (non pruned)
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[1], 2)
|
2018-01-09 08:00:35 +01:00
|
|
|
|
|
|
|
# sync must be possible
|
2019-04-07 00:19:45 +02:00
|
|
|
self.sync_blocks()
|
2018-01-09 08:00:35 +01:00
|
|
|
|
|
|
|
# disconnect all peers
|
|
|
|
self.disconnect_all()
|
|
|
|
|
|
|
|
# mine 10 blocks on node 0 (pruned node)
|
2018-09-15 11:32:12 +02:00
|
|
|
self.nodes[0].generatetoaddress(10, self.nodes[0].get_deterministic_priv_key().address)
|
2018-01-09 08:00:35 +01:00
|
|
|
|
|
|
|
# connect node1 (non pruned) with node0 (pruned) and check if the can sync
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2018-01-09 08:00:35 +01:00
|
|
|
|
|
|
|
# sync must be possible, node 1 is no longer in IBD and should therefore connect to node 0 (NODE_NETWORK_LIMITED)
|
2019-04-07 00:19:45 +02:00
|
|
|
self.sync_blocks([self.nodes[0], self.nodes[1]])
|
2018-01-09 08:00:35 +01:00
|
|
|
|
2017-11-20 21:57:34 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
NodeNetworkLimitedTest().main()
|