|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# |
| 3 | +# Distributed under the MIT/X11 software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +# |
| 6 | + |
| 7 | +from test_framework.test_framework import ComparisonTestFramework |
| 8 | +from test_framework.util import * |
| 9 | +from test_framework.mininode import CTransaction, NetworkThread |
| 10 | +from test_framework.blocktools import create_coinbase, create_block |
| 11 | +from test_framework.comptool import TestInstance, TestManager |
| 12 | +from test_framework.script import CScript, OP_1NEGATE, OP_NOP2, OP_DROP |
| 13 | +from binascii import hexlify, unhexlify |
| 14 | +import cStringIO |
| 15 | +import time |
| 16 | + |
| 17 | +def cltv_invalidate(tx): |
| 18 | + '''Modify the signature in vin 0 of the tx to fail CLTV |
| 19 | +
|
| 20 | + Prepends -1 CLTV DROP in the scriptSig itself. |
| 21 | + ''' |
| 22 | + tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_NOP2, OP_DROP] + |
| 23 | + list(CScript(tx.vin[0].scriptSig))) |
| 24 | + |
| 25 | +''' |
| 26 | +This test is meant to exercise BIP65 (CHECKLOCKTIMEVERIFY) |
| 27 | +Connect to a single node. |
| 28 | +Mine 2 (version 3) blocks (save the coinbases for later). |
| 29 | +Generate 98 more version 3 blocks, verify the node accepts. |
| 30 | +Mine 749 version 4 blocks, verify the node accepts. |
| 31 | +Check that the new CLTV rules are not enforced on the 750th version 4 block. |
| 32 | +Check that the new CLTV rules are enforced on the 751st version 4 block. |
| 33 | +Mine 199 new version blocks. |
| 34 | +Mine 1 old-version block. |
| 35 | +Mine 1 new version block. |
| 36 | +Mine 1 old version block, see that the node rejects. |
| 37 | +''' |
| 38 | + |
| 39 | +class BIP65Test(ComparisonTestFramework): |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + self.num_nodes = 1 |
| 43 | + |
| 44 | + def setup_network(self): |
| 45 | + # Must set the blockversion for this test |
| 46 | + self.nodes = start_nodes(1, self.options.tmpdir, |
| 47 | + extra_args=[['-debug', '-whitelist=127.0.0.1', '-blockversion=3']], |
| 48 | + binary=[self.options.testbinary]) |
| 49 | + |
| 50 | + def run_test(self): |
| 51 | + test = TestManager(self, self.options.tmpdir) |
| 52 | + test.add_all_connections(self.nodes) |
| 53 | + NetworkThread().start() # Start up network handling in another thread |
| 54 | + test.run() |
| 55 | + |
| 56 | + def create_transaction(self, node, coinbase, to_address, amount): |
| 57 | + from_txid = node.getblock(coinbase)['tx'][0] |
| 58 | + inputs = [{ "txid" : from_txid, "vout" : 0}] |
| 59 | + outputs = { to_address : amount } |
| 60 | + rawtx = node.createrawtransaction(inputs, outputs) |
| 61 | + signresult = node.signrawtransaction(rawtx) |
| 62 | + tx = CTransaction() |
| 63 | + f = cStringIO.StringIO(unhexlify(signresult['hex'])) |
| 64 | + tx.deserialize(f) |
| 65 | + return tx |
| 66 | + |
| 67 | + def get_tests(self): |
| 68 | + |
| 69 | + self.coinbase_blocks = self.nodes[0].generate(2) |
| 70 | + self.tip = int ("0x" + self.nodes[0].getbestblockhash() + "L", 0) |
| 71 | + self.nodeaddress = self.nodes[0].getnewaddress() |
| 72 | + self.last_block_time = time.time() |
| 73 | + |
| 74 | + ''' 98 more version 3 blocks ''' |
| 75 | + test_blocks = [] |
| 76 | + for i in xrange(98): |
| 77 | + block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1) |
| 78 | + block.nVersion = 3 |
| 79 | + block.rehash() |
| 80 | + block.solve() |
| 81 | + test_blocks.append([block, True]) |
| 82 | + self.last_block_time += 1 |
| 83 | + self.tip = block.sha256 |
| 84 | + yield TestInstance(test_blocks, sync_every_block=False) |
| 85 | + |
| 86 | + ''' Mine 749 version 4 blocks ''' |
| 87 | + test_blocks = [] |
| 88 | + for i in xrange(749): |
| 89 | + block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1) |
| 90 | + block.nVersion = 4 |
| 91 | + block.rehash() |
| 92 | + block.solve() |
| 93 | + test_blocks.append([block, True]) |
| 94 | + self.last_block_time += 1 |
| 95 | + self.tip = block.sha256 |
| 96 | + yield TestInstance(test_blocks, sync_every_block=False) |
| 97 | + |
| 98 | + ''' |
| 99 | + Check that the new CLTV rules are not enforced in the 750th |
| 100 | + version 3 block. |
| 101 | + ''' |
| 102 | + spendtx = self.create_transaction(self.nodes[0], |
| 103 | + self.coinbase_blocks[0], self.nodeaddress, 1.0) |
| 104 | + cltv_invalidate(spendtx) |
| 105 | + spendtx.rehash() |
| 106 | + |
| 107 | + block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1) |
| 108 | + block.nVersion = 4 |
| 109 | + block.vtx.append(spendtx) |
| 110 | + block.hashMerkleRoot = block.calc_merkle_root() |
| 111 | + block.rehash() |
| 112 | + block.solve() |
| 113 | + |
| 114 | + self.last_block_time += 1 |
| 115 | + self.tip = block.sha256 |
| 116 | + yield TestInstance([[block, True]]) |
| 117 | + |
| 118 | + ''' |
| 119 | + Check that the new CLTV rules are enforced in the 751st version 4 |
| 120 | + block. |
| 121 | + ''' |
| 122 | + spendtx = self.create_transaction(self.nodes[0], |
| 123 | + self.coinbase_blocks[1], self.nodeaddress, 1.0) |
| 124 | + cltv_invalidate(spendtx) |
| 125 | + spendtx.rehash() |
| 126 | + |
| 127 | + block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1) |
| 128 | + block.nVersion = 4 |
| 129 | + block.vtx.append(spendtx) |
| 130 | + block.hashMerkleRoot = block.calc_merkle_root() |
| 131 | + block.rehash() |
| 132 | + block.solve() |
| 133 | + self.last_block_time += 1 |
| 134 | + yield TestInstance([[block, False]]) |
| 135 | + |
| 136 | + ''' Mine 199 new version blocks on last valid tip ''' |
| 137 | + test_blocks = [] |
| 138 | + for i in xrange(199): |
| 139 | + block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1) |
| 140 | + block.nVersion = 4 |
| 141 | + block.rehash() |
| 142 | + block.solve() |
| 143 | + test_blocks.append([block, True]) |
| 144 | + self.last_block_time += 1 |
| 145 | + self.tip = block.sha256 |
| 146 | + yield TestInstance(test_blocks, sync_every_block=False) |
| 147 | + |
| 148 | + ''' Mine 1 old version block ''' |
| 149 | + block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1) |
| 150 | + block.nVersion = 3 |
| 151 | + block.rehash() |
| 152 | + block.solve() |
| 153 | + self.last_block_time += 1 |
| 154 | + self.tip = block.sha256 |
| 155 | + yield TestInstance([[block, True]]) |
| 156 | + |
| 157 | + ''' Mine 1 new version block ''' |
| 158 | + block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1) |
| 159 | + block.nVersion = 4 |
| 160 | + block.rehash() |
| 161 | + block.solve() |
| 162 | + self.last_block_time += 1 |
| 163 | + self.tip = block.sha256 |
| 164 | + yield TestInstance([[block, True]]) |
| 165 | + |
| 166 | + ''' Mine 1 old version block, should be invalid ''' |
| 167 | + block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1) |
| 168 | + block.nVersion = 3 |
| 169 | + block.rehash() |
| 170 | + block.solve() |
| 171 | + self.last_block_time += 1 |
| 172 | + yield TestInstance([[block, False]]) |
| 173 | + |
| 174 | +if __name__ == '__main__': |
| 175 | + BIP65Test().main() |
0 commit comments