Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_unrequested_blocks.py
Show First 20 Lines • Show All 182 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
assert tip_entry_found | assert tip_entry_found | ||||
self.nodes[0].getblock(block_h3.hash) | self.nodes[0].getblock(block_h3.hash) | ||||
# But this block should be accepted by node since it has more work. | # But this block should be accepted by node since it has more work. | ||||
self.nodes[0].getblock(block_h3.hash) | self.nodes[0].getblock(block_h3.hash) | ||||
self.log.info("Unrequested more-work block accepted") | self.log.info("Unrequested more-work block accepted") | ||||
# 4c. Now mine 288 more blocks and deliver; all should be processed but | # 4c. Now mine 288 more blocks and deliver; all should be processed but | ||||
# the last (height-too-high) on node (as long as it is not missing any headers) | # the last (height-too-high) on node (as long as it is not missing any | ||||
# headers) | |||||
tip = block_h3 | tip = block_h3 | ||||
all_blocks = [] | all_blocks = [] | ||||
for i in range(288): | for i in range(288): | ||||
next_block = create_block( | next_block = create_block( | ||||
tip.sha256, create_coinbase(i + 4), tip.nTime + 1) | tip.sha256, create_coinbase(i + 4), tip.nTime + 1) | ||||
next_block.solve() | next_block.solve() | ||||
all_blocks.append(next_block) | all_blocks.append(next_block) | ||||
tip = next_block | tip = next_block | ||||
# Now send the block at height 5 and check that it wasn't accepted (missing header) | # Now send the block at height 5 and check that it wasn't accepted | ||||
# (missing header) | |||||
test_node.send_message(msg_block(all_blocks[1])) | test_node.send_message(msg_block(all_blocks[1])) | ||||
test_node.sync_with_ping() | test_node.sync_with_ping() | ||||
assert_raises_rpc_error(-5, "Block not found", | assert_raises_rpc_error(-5, "Block not found", | ||||
self.nodes[0].getblock, all_blocks[1].hash) | self.nodes[0].getblock, all_blocks[1].hash) | ||||
assert_raises_rpc_error(-5, "Block not found", | assert_raises_rpc_error(-5, "Block not found", | ||||
self.nodes[0].getblockheader, all_blocks[1].hash) | self.nodes[0].getblockheader, all_blocks[1].hash) | ||||
# The block at height 5 should be accepted if we provide the missing header, though | # The block at height 5 should be accepted if we provide the missing | ||||
# header, though | |||||
headers_message = msg_headers() | headers_message = msg_headers() | ||||
headers_message.headers.append(CBlockHeader(all_blocks[0])) | headers_message.headers.append(CBlockHeader(all_blocks[0])) | ||||
test_node.send_message(headers_message) | test_node.send_message(headers_message) | ||||
test_node.send_message(msg_block(all_blocks[1])) | test_node.send_message(msg_block(all_blocks[1])) | ||||
test_node.sync_with_ping() | test_node.sync_with_ping() | ||||
self.nodes[0].getblock(all_blocks[1].hash) | self.nodes[0].getblock(all_blocks[1].hash) | ||||
# Now send the blocks in all_blocks | # Now send the blocks in all_blocks | ||||
for i in range(288): | for i in range(288): | ||||
test_node.send_message(msg_block(all_blocks[i])) | test_node.send_message(msg_block(all_blocks[i])) | ||||
test_node.sync_with_ping() | test_node.sync_with_ping() | ||||
# Blocks 1-287 should be accepted, block 288 should be ignored because it's too far ahead | # Blocks 1-287 should be accepted, block 288 should be ignored because | ||||
# it's too far ahead | |||||
for x in all_blocks[:-1]: | for x in all_blocks[:-1]: | ||||
self.nodes[0].getblock(x.hash) | self.nodes[0].getblock(x.hash) | ||||
assert_raises_rpc_error( | assert_raises_rpc_error( | ||||
-1, "Block not found on disk", self.nodes[0].getblock, all_blocks[-1].hash) | -1, "Block not found on disk", self.nodes[0].getblock, all_blocks[-1].hash) | ||||
# 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). | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
block_291.vtx.append(create_tx_with_script( | block_291.vtx.append(create_tx_with_script( | ||||
block_290f.vtx[0], 0, script_sig=b"42", amount=1)) | block_290f.vtx[0], 0, script_sig=b"42", amount=1)) | ||||
block_291.hashMerkleRoot = block_291.calc_merkle_root() | block_291.hashMerkleRoot = block_291.calc_merkle_root() | ||||
block_291.solve() | block_291.solve() | ||||
block_292 = create_block( | block_292 = create_block( | ||||
block_291.sha256, create_coinbase(292), block_291.nTime + 1) | block_291.sha256, create_coinbase(292), block_291.nTime + 1) | ||||
block_292.solve() | block_292.solve() | ||||
# Now send all the headers on the chain and enough blocks to trigger reorg | # Now send all the headers on the chain and enough blocks to trigger | ||||
# reorg | |||||
headers_message = msg_headers() | headers_message = msg_headers() | ||||
headers_message.headers.append(CBlockHeader(block_289f)) | headers_message.headers.append(CBlockHeader(block_289f)) | ||||
headers_message.headers.append(CBlockHeader(block_290f)) | headers_message.headers.append(CBlockHeader(block_290f)) | ||||
headers_message.headers.append(CBlockHeader(block_291)) | headers_message.headers.append(CBlockHeader(block_291)) | ||||
headers_message.headers.append(CBlockHeader(block_292)) | headers_message.headers.append(CBlockHeader(block_292)) | ||||
test_node.send_message(headers_message) | test_node.send_message(headers_message) | ||||
test_node.sync_with_ping() | test_node.sync_with_ping() | ||||
Show All 22 Lines | def run_test(self): | ||||
# 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()) | ||||
# 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) | ||||
# Now send a new header on the invalid chain, indicating we're forked off, and expect to get disconnected | # Now send a new header on the invalid chain, indicating we're forked | ||||
# off, and expect to get disconnected | |||||
block_293 = create_block( | block_293 = create_block( | ||||
block_292.sha256, create_coinbase(293), block_292.nTime + 1) | block_292.sha256, create_coinbase(293), block_292.nTime + 1) | ||||
block_293.solve() | block_293.solve() | ||||
headers_message = msg_headers() | headers_message = msg_headers() | ||||
headers_message.headers.append(CBlockHeader(block_293)) | headers_message.headers.append(CBlockHeader(block_293)) | ||||
test_node.send_message(headers_message) | test_node.send_message(headers_message) | ||||
test_node.wait_for_disconnect() | test_node.wait_for_disconnect() | ||||
# 9. Connect node1 to node0 and ensure it is able to sync | # 9. Connect node1 to node0 and ensure it is able to sync | ||||
connect_nodes(self.nodes[0], self.nodes[1]) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
sync_blocks([self.nodes[0], self.nodes[1]]) | sync_blocks([self.nodes[0], self.nodes[1]]) | ||||
self.log.info("Successfully synced nodes 1 and 0") | self.log.info("Successfully synced nodes 1 and 0") | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
AcceptBlockTest().main() | AcceptBlockTest().main() |