Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-get-invalid-block.py
Show All 17 Lines | |||||
class GetInvalidBlockTest(BitcoinTestFramework): | class GetInvalidBlockTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
def run_test(self): | def run_test(self): | ||||
node = self.nodes[0] | node = self.nodes[0] | ||||
node.add_p2p_connection(P2PInterface()) | peer = node.add_p2p_connection(P2PInterface()) | ||||
chaintip = node.getbestblockhash() | chaintip = node.getbestblockhash() | ||||
# Mine some blocks and invalidate them | # Mine some blocks and invalidate them | ||||
blocks = node.generatetoaddress( | blocks = node.generatetoaddress( | ||||
3, node.get_deterministic_priv_key().address) | 3, node.get_deterministic_priv_key().address) | ||||
assert_equal(blocks[-1], node.getbestblockhash()) | assert_equal(blocks[-1], node.getbestblockhash()) | ||||
node.invalidateblock(blocks[0]) | node.invalidateblock(blocks[0]) | ||||
assert_equal(chaintip, node.getbestblockhash()) | assert_equal(chaintip, node.getbestblockhash()) | ||||
# Clear any old messages | # Clear any old messages | ||||
with p2p_lock: | with p2p_lock: | ||||
node.p2p.last_message.pop("block", None) | peer.last_message.pop("block", None) | ||||
node.p2p.last_message.pop("cmpctblock", None) | peer.last_message.pop("cmpctblock", None) | ||||
node.p2p.last_message.pop("headers", None) | peer.last_message.pop("headers", None) | ||||
# Requests for the invalidated block and it's decendants should fail. | # Requests for the invalidated block and it's decendants should fail. | ||||
# Not doing so is a potential DoS vector. | # Not doing so is a potential DoS vector. | ||||
for b in blocks: | for b in blocks: | ||||
block_hash = int(b, 16) | block_hash = int(b, 16) | ||||
# Currently, the implementation for getblocks skips blocks which | # Currently, the implementation for getblocks skips blocks which | ||||
# are not on the currently active chain. This is the only logged | # are not on the currently active chain. This is the only logged | ||||
# indication of such. | # indication of such. | ||||
with node.assert_debug_log(expected_msgs=["getblocks -1 to"]): | with node.assert_debug_log(expected_msgs=["getblocks -1 to"]): | ||||
msg = msg_getblocks() | msg = msg_getblocks() | ||||
msg.locator.vHave = [block_hash] | msg.locator.vHave = [block_hash] | ||||
node.p2p.send_message(msg) | peer.send_message(msg) | ||||
node.p2p.sync_with_ping() | peer.sync_with_ping() | ||||
with node.assert_debug_log(expected_msgs=["ignoring request from peer=0 for old block that isn't in the main chain"]): | with node.assert_debug_log(expected_msgs=["ignoring request from peer=0 for old block that isn't in the main chain"]): | ||||
msg = msg_getdata() | msg = msg_getdata() | ||||
msg.inv.append(CInv(MSG_BLOCK, block_hash)) | msg.inv.append(CInv(MSG_BLOCK, block_hash)) | ||||
node.p2p.send_message(msg) | peer.send_message(msg) | ||||
node.p2p.sync_with_ping() | peer.sync_with_ping() | ||||
with node.assert_debug_log(expected_msgs=["ignoring request from peer=0 for old block that isn't in the main chain"]): | with node.assert_debug_log(expected_msgs=["ignoring request from peer=0 for old block that isn't in the main chain"]): | ||||
msg = msg_getdata() | msg = msg_getdata() | ||||
msg.inv.append(CInv(MSG_CMPCT_BLOCK, block_hash)) | msg.inv.append(CInv(MSG_CMPCT_BLOCK, block_hash)) | ||||
node.p2p.send_message(msg) | peer.send_message(msg) | ||||
node.p2p.sync_with_ping() | peer.sync_with_ping() | ||||
with node.assert_debug_log(expected_msgs=["ignoring request from peer=0 for old block header that isn't in the main chain"]): | with node.assert_debug_log(expected_msgs=["ignoring request from peer=0 for old block header that isn't in the main chain"]): | ||||
msg = msg_getheaders() | msg = msg_getheaders() | ||||
msg.hashstop = block_hash | msg.hashstop = block_hash | ||||
node.p2p.send_message(msg) | peer.send_message(msg) | ||||
node.p2p.sync_with_ping() | peer.sync_with_ping() | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
GetInvalidBlockTest().main() | GetInvalidBlockTest().main() |