|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2015-2018 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test BIP 34, 65, 66 activation at block 0""" |
| 6 | + |
| 7 | +from test_framework.blocktools import create_coinbase, create_block, create_transaction |
| 8 | +from test_framework.messages import msg_block |
| 9 | +from test_framework.mininode import mininode_lock, P2PInterface |
| 10 | +from test_framework.test_framework import BitcoinTestFramework |
| 11 | +from test_framework.util import assert_equal, wait_until |
| 12 | + |
| 13 | +from feature_cltv import cltv_validate, REJECT_OBSOLETE |
| 14 | + |
| 15 | +class BlockV4Test(BitcoinTestFramework): |
| 16 | + def set_test_params(self): |
| 17 | + self.num_nodes = 1 |
| 18 | + self.extra_args = [['-whitelist=127.0.0.1', '-con_bip34height=0', '-con_bip65height=0', '-con_bip66height=0']] |
| 19 | + self.setup_clean_chain = True |
| 20 | + |
| 21 | + def run_test(self): |
| 22 | + self.nodes[0].add_p2p_connection(P2PInterface()) |
| 23 | + |
| 24 | + self.nodeaddress = self.nodes[0].getnewaddress() |
| 25 | + |
| 26 | + self.log.info("Test that blocks past the genesis block must be at least version 4") |
| 27 | + |
| 28 | + # Create a v3 block |
| 29 | + tip = self.nodes[0].getbestblockhash() |
| 30 | + block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1 |
| 31 | + block = create_block(int(tip, 16), create_coinbase(1), block_time) |
| 32 | + block.nVersion = 3 |
| 33 | + block.solve() |
| 34 | + |
| 35 | + # Send it to the node |
| 36 | + self.nodes[0].p2p.send_and_ping(msg_block(block)) |
| 37 | + |
| 38 | + # The best block should not have changed, because... |
| 39 | + assert_equal(self.nodes[0].getbestblockhash(), tip) |
| 40 | + |
| 41 | + # ... we rejected it because it is v3 |
| 42 | + wait_until(lambda: "reject" in self.nodes[0].p2p.last_message.keys(), lock=mininode_lock) |
| 43 | + with mininode_lock: |
| 44 | + assert_equal(self.nodes[0].p2p.last_message["reject"].code, REJECT_OBSOLETE) |
| 45 | + assert_equal(self.nodes[0].p2p.last_message["reject"].reason, b'bad-version(0x00000003)') |
| 46 | + assert_equal(self.nodes[0].p2p.last_message["reject"].data, block.sha256) |
| 47 | + del self.nodes[0].p2p.last_message["reject"] |
| 48 | + |
| 49 | + self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted") |
| 50 | + |
| 51 | + # Generate 100 blocks so that first coinbase matures |
| 52 | + generated_blocks = self.nodes[0].generate(100) |
| 53 | + spendable_coinbase_txid = self.nodes[0].getblock(generated_blocks[0])['tx'][0] |
| 54 | + tip = generated_blocks[-1] |
| 55 | + |
| 56 | + # Construct a v4 block |
| 57 | + block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1 |
| 58 | + block = create_block(int(tip, 16), create_coinbase(len(generated_blocks) + 1), block_time) |
| 59 | + block.nVersion = 4 |
| 60 | + |
| 61 | + # Create a CLTV transaction |
| 62 | + spendtx = create_transaction(self.nodes[0], spendable_coinbase_txid, |
| 63 | + self.nodeaddress, amount=1.0) |
| 64 | + spendtx = cltv_validate(self.nodes[0], spendtx, 1) |
| 65 | + spendtx.rehash() |
| 66 | + |
| 67 | + # Add the CLTV transaction and prepare for sending |
| 68 | + block.vtx.append(spendtx) |
| 69 | + block.hashMerkleRoot = block.calc_merkle_root() |
| 70 | + block.solve() |
| 71 | + |
| 72 | + # Send block and check that it becomes new best block |
| 73 | + self.nodes[0].p2p.send_and_ping(msg_block(block)) |
| 74 | + assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.sha256) |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + BlockV4Test().main() |
0 commit comments