Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-get-invalid-block.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2019 The Bitcoin developers | # Copyright (c) 2019 The Bitcoin 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 requesting invalid blocks behaves safely.""" | """Test requesting invalid blocks behaves safely.""" | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
CInv, | CInv, | ||||
msg_getblocks, | msg_getblocks, | ||||
msg_getdata, | msg_getdata, | ||||
msg_getheaders, | msg_getheaders, | ||||
MSG_BLOCK, | MSG_BLOCK, | ||||
MSG_CMPCT_BLOCK, | MSG_CMPCT_BLOCK, | ||||
) | ) | ||||
from test_framework.mininode import mininode_lock, P2PInterface | from test_framework.mininode import p2p_lock, P2PInterface | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import assert_equal | from test_framework.util import assert_equal | ||||
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()) | 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 mininode_lock: | with p2p_lock: | ||||
node.p2p.last_message.pop("block", None) | node.p2p.last_message.pop("block", None) | ||||
node.p2p.last_message.pop("cmpctblock", None) | node.p2p.last_message.pop("cmpctblock", None) | ||||
node.p2p.last_message.pop("headers", None) | node.p2p.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) | ||||
Show All 31 Lines |