Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_unrequested_blocks.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2019 The Bitcoin Core developers | # Copyright (c) 2015-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test processing of unrequested blocks. | """Test processing of unrequested blocks. | ||||
Setup: two nodes, node0+node1, not connected to each other. Node1 will have | Setup: two nodes, node0 + node1, not connected to each other. Node1 will have | ||||
nMinimumChainWork set to 0x10, so it won't process low-work unrequested blocks. | nMinimumChainWork set to 0x10, so it won't process low-work unrequested blocks. | ||||
We have one P2PInterface connection to node0 called test_node, and one to node1 | We have one P2PInterface connection to node0 called test_node, and one to node1 | ||||
called min_work_node. | called min_work_node. | ||||
The test: | The test: | ||||
1. Generate one block on each node, to leave IBD. | 1. Generate one block on each node, to leave IBD. | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | class AcceptBlockTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
self.extra_args = [["-noparkdeepreorg"], | self.extra_args = [["-noparkdeepreorg"], | ||||
["-minimumchainwork=0x10"]] | ["-minimumchainwork=0x10"]] | ||||
def setup_network(self): | def setup_network(self): | ||||
# Node0 will be used to test behavior of processing unrequested blocks | |||||
# from peers which are not whitelisted, while Node1 will be used for | |||||
# the whitelisted case. | |||||
# Node2 will be used for non-whitelisted peers to test the interaction | |||||
# with nMinimumChainWork. | |||||
self.setup_nodes() | self.setup_nodes() | ||||
def run_test(self): | def run_test(self): | ||||
# Setup the p2p connections | |||||
# test_node connects to node0 (not whitelisted) | |||||
test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
# min_work_node connects to node1 (whitelisted) | |||||
min_work_node = self.nodes[1].add_p2p_connection(P2PInterface()) | min_work_node = self.nodes[1].add_p2p_connection(P2PInterface()) | ||||
# 1. Have nodes mine a block (leave IBD) | # 1. Have nodes mine a block (leave IBD) | ||||
[n.generatetoaddress(1, n.get_deterministic_priv_key().address) | [n.generatetoaddress(1, n.get_deterministic_priv_key().address) | ||||
for n in self.nodes] | for n in self.nodes] | ||||
tips = [int("0x" + n.getbestblockhash(), 0) for n in self.nodes] | tips = [int("0x" + n.getbestblockhash(), 0) for n in self.nodes] | ||||
# 2. Send one block that builds on each tip. | # 2. Send one block that builds on each tip. | ||||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# 7. Send the missing block for the third time (now it is requested) | # 7. Send the missing block for the third time (now it is requested) | ||||
test_node.send_and_ping(msg_block(block_h1f)) | test_node.send_and_ping(msg_block(block_h1f)) | ||||
assert_equal(self.nodes[0].getblockcount(), 290) | assert_equal(self.nodes[0].getblockcount(), 290) | ||||
self.nodes[0].getblock(all_blocks[286].hash) | self.nodes[0].getblock(all_blocks[286].hash) | ||||
assert_equal(self.nodes[0].getbestblockhash(), all_blocks[286].hash) | assert_equal(self.nodes[0].getbestblockhash(), all_blocks[286].hash) | ||||
assert_raises_rpc_error(-1, "Block not found on disk", | assert_raises_rpc_error(-1, "Block not found on disk", | ||||
self.nodes[0].getblock, all_blocks[287].hash) | self.nodes[0].getblock, all_blocks[287].hash) | ||||
self.log.info( | self.log.info("Successfully reorged to longer chain") | ||||
"Successfully reorged to longer chain from non-whitelisted peer") | |||||
# 8. Create a chain which is invalid at a height longer than the | # 8. Create a chain which is invalid at a height longer than the | ||||
# current chain, but which has more blocks on top of that | # current chain, but which has more blocks on top of that | ||||
block_289f = create_block( | block_289f = create_block( | ||||
all_blocks[284].sha256, create_coinbase(289), all_blocks[284].nTime + 1) | all_blocks[284].sha256, create_coinbase(289), all_blocks[284].nTime + 1) | ||||
block_289f.solve() | block_289f.solve() | ||||
block_290f = create_block( | block_290f = create_block( | ||||
block_289f.sha256, create_coinbase(290), block_289f.nTime + 1) | block_289f.sha256, create_coinbase(290), block_289f.nTime + 1) | ||||
▲ Show 20 Lines • Show All 75 Lines • Show Last 20 Lines |