Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_unrequested_blocks.py
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | from test_framework.messages import ( | ||||
CBlockHeader, | CBlockHeader, | ||||
CInv, | CInv, | ||||
msg_block, | msg_block, | ||||
msg_headers, | msg_headers, | ||||
msg_inv, | msg_inv, | ||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
mininode_lock, | mininode_lock, | ||||
network_thread_join, | |||||
network_thread_start, | |||||
P2PInterface, | P2PInterface, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
connect_nodes, | connect_nodes, | ||||
sync_blocks, | sync_blocks, | ||||
Show All 12 Lines | def setup_network(self): | ||||
# Node0 will be used to test behavior of processing unrequested blocks | # Node0 will be used to test behavior of processing unrequested blocks | ||||
# from peers which are not whitelisted, while Node1 will be used for | # from peers which are not whitelisted, while Node1 will be used for | ||||
# the whitelisted case. | # the whitelisted case. | ||||
# Node2 will be used for non-whitelisted peers to test the interaction | # Node2 will be used for non-whitelisted peers to test the interaction | ||||
# with nMinimumChainWork. | # with nMinimumChainWork. | ||||
self.setup_nodes() | self.setup_nodes() | ||||
def run_test(self): | def run_test(self): | ||||
# Setup the p2p connections and start up the network thread. | # Setup the p2p connections | ||||
# test_node connects to node0 (not whitelisted) | # 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 connects to node1 (whitelisted) | ||||
min_work_node = self.nodes[1].add_p2p_connection(P2PInterface()) | min_work_node = self.nodes[1].add_p2p_connection(P2PInterface()) | ||||
network_thread_start() | |||||
# Test logic begins here | |||||
test_node.wait_for_verack() | test_node.wait_for_verack() | ||||
min_work_node.wait_for_verack() | min_work_node.wait_for_verack() | ||||
# 1. Have nodes mine a block (leave IBD) | # 1. Have nodes mine a block (leave IBD) | ||||
[n.generate(1) for n in self.nodes] | [n.generate(1) 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 116 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# 5. Test handling of unrequested block on the node that didn't process | # 5. Test handling of unrequested block on the node that didn't process | ||||
# Should still not be processed (even though it has a child that has more | # Should still not be processed (even though it has a child that has more | ||||
# work). | # work). | ||||
# The node should have requested the blocks at some point, so | # The node should have requested the blocks at some point, so | ||||
# disconnect/reconnect first | # disconnect/reconnect first | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
self.nodes[1].disconnect_p2ps() | self.nodes[1].disconnect_p2ps() | ||||
network_thread_join() | |||||
test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
network_thread_start() | |||||
test_node.wait_for_verack() | test_node.wait_for_verack() | ||||
test_node.send_message(msg_block(block_h1f)) | test_node.send_message(msg_block(block_h1f)) | ||||
test_node.sync_with_ping() | test_node.sync_with_ping() | ||||
assert_equal(self.nodes[0].getblockcount(), 2) | assert_equal(self.nodes[0].getblockcount(), 2) | ||||
self.log.info( | self.log.info( | ||||
"Unrequested block that would complete more-work chain was ignored") | "Unrequested block that would complete more-work chain was ignored") | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# Only wait a short while so the test doesn't take forever if we do get | # Only wait a short while so the test doesn't take forever if we do get | ||||
# disconnected | # disconnected | ||||
test_node.sync_with_ping(timeout=1) | test_node.sync_with_ping(timeout=1) | ||||
except AssertionError: | except AssertionError: | ||||
test_node.wait_for_disconnect() | test_node.wait_for_disconnect() | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
network_thread_start() | |||||
test_node.wait_for_verack() | test_node.wait_for_verack() | ||||
# We should have failed reorg and switched back to 290 (but have block 291) | # We should have failed reorg and switched back to 290 (but have block 291) | ||||
assert_equal(self.nodes[0].getblockcount(), 290) | assert_equal(self.nodes[0].getblockcount(), 290) | ||||
assert_equal(self.nodes[0].getbestblockhash(), all_blocks[286].hash) | assert_equal(self.nodes[0].getbestblockhash(), all_blocks[286].hash) | ||||
assert_equal(self.nodes[0].getblock( | assert_equal(self.nodes[0].getblock( | ||||
block_291.hash)["confirmations"], -1) | block_291.hash)["confirmations"], -1) | ||||
Show All 17 Lines |