Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p-acceptblock.py
Show All 9 Lines | |||||
from test_framework.blocktools import create_block, create_coinbase, create_transaction | from test_framework.blocktools import create_block, create_coinbase, create_transaction | ||||
''' | ''' | ||||
AcceptBlockTest -- test processing of unrequested blocks. | AcceptBlockTest -- 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 NodeConn 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. | ||||
2. Mine a new block on each tip, and deliver to each node from node's peer. | 2. Mine a new block on each tip, and deliver to each node from node's peer. | ||||
The tip should advance for node0, but node1 should skip processing due to | The tip should advance for node0, but node1 should skip processing due to | ||||
nMinimumChainWork. | nMinimumChainWork. | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | def setup_network(self): | ||||
# 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 and start up the network thread. | ||||
# test_node connects to node0 (not whitelisted) | # test_node connects to node0 (not whitelisted) | ||||
test_node = self.nodes[0].add_p2p_connection(NodeConnCB()) | test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
# min_work_node connects to node1 | # min_work_node connects to node1 (whitelisted) | ||||
min_work_node = self.nodes[1].add_p2p_connection(NodeConnCB()) | min_work_node = self.nodes[1].add_p2p_connection(P2PInterface()) | ||||
# Start up network handling in another thread | # Start up network handling in another thread | ||||
NetworkThread().start() | NetworkThread().start() | ||||
# Test logic begins here | # 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() | ||||
▲ Show 20 Lines • Show All 120 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() | ||||
test_node = self.nodes[0].add_p2p_connection(NodeConnCB()) | test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
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 77 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
try: | try: | ||||
# 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(NodeConnCB()) | test_node = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
NetworkThread().start() # Start up network handling in another thread | NetworkThread().start() # Start up network handling in another thread | ||||
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( | ||||
Show All 21 Lines |