Make sure we use nTxConfirmTarget during Qt fee bumps
[bitcoinplatinum.git] / test / functional / bip65-cltv-p2p.py
blobbb83042f35002ea1167caaad11e60a2abe385aa9
1 #!/usr/bin/env python3
2 # Copyright (c) 2015-2016 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 BIP65 (CHECKLOCKTIMEVERIFY).
7 Connect to a single node.
8 Mine 2 (version 3) blocks (save the coinbases for later).
9 Generate 98 more version 3 blocks, verify the node accepts.
10 Mine 749 version 4 blocks, verify the node accepts.
11 Check that the new CLTV rules are not enforced on the 750th version 4 block.
12 Check that the new CLTV rules are enforced on the 751st version 4 block.
13 Mine 199 new version blocks.
14 Mine 1 old-version block.
15 Mine 1 new version block.
16 Mine 1 old version block, see that the node rejects.
17 """
19 from test_framework.test_framework import ComparisonTestFramework
20 from test_framework.util import *
21 from test_framework.mininode import CTransaction, NetworkThread
22 from test_framework.blocktools import create_coinbase, create_block
23 from test_framework.comptool import TestInstance, TestManager
24 from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP
25 from io import BytesIO
26 import time
28 def cltv_invalidate(tx):
29 '''Modify the signature in vin 0 of the tx to fail CLTV
31 Prepends -1 CLTV DROP in the scriptSig itself.
32 '''
33 tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
34 list(CScript(tx.vin[0].scriptSig)))
37 class BIP65Test(ComparisonTestFramework):
39 def __init__(self):
40 super().__init__()
41 self.num_nodes = 1
42 self.extra_args = [['-whitelist=127.0.0.1', '-blockversion=3']]
44 def run_test(self):
45 test = TestManager(self, self.options.tmpdir)
46 test.add_all_connections(self.nodes)
47 NetworkThread().start() # Start up network handling in another thread
48 test.run()
50 def create_transaction(self, node, coinbase, to_address, amount):
51 from_txid = node.getblock(coinbase)['tx'][0]
52 inputs = [{ "txid" : from_txid, "vout" : 0}]
53 outputs = { to_address : amount }
54 rawtx = node.createrawtransaction(inputs, outputs)
55 signresult = node.signrawtransaction(rawtx)
56 tx = CTransaction()
57 f = BytesIO(hex_str_to_bytes(signresult['hex']))
58 tx.deserialize(f)
59 return tx
61 def get_tests(self):
63 self.coinbase_blocks = self.nodes[0].generate(2)
64 height = 3 # height of the next block to build
65 self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
66 self.nodeaddress = self.nodes[0].getnewaddress()
67 self.last_block_time = int(time.time())
69 ''' 398 more version 3 blocks '''
70 test_blocks = []
71 for i in range(398):
72 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
73 block.nVersion = 3
74 block.rehash()
75 block.solve()
76 test_blocks.append([block, True])
77 self.last_block_time += 1
78 self.tip = block.sha256
79 height += 1
80 yield TestInstance(test_blocks, sync_every_block=False)
82 ''' Mine 749 version 4 blocks '''
83 test_blocks = []
84 for i in range(749):
85 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
86 block.nVersion = 4
87 block.rehash()
88 block.solve()
89 test_blocks.append([block, True])
90 self.last_block_time += 1
91 self.tip = block.sha256
92 height += 1
93 yield TestInstance(test_blocks, sync_every_block=False)
95 '''
96 Check that the new CLTV rules are not enforced in the 750th
97 version 3 block.
98 '''
99 spendtx = self.create_transaction(self.nodes[0],
100 self.coinbase_blocks[0], self.nodeaddress, 1.0)
101 cltv_invalidate(spendtx)
102 spendtx.rehash()
104 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
105 block.nVersion = 4
106 block.vtx.append(spendtx)
107 block.hashMerkleRoot = block.calc_merkle_root()
108 block.rehash()
109 block.solve()
111 self.last_block_time += 1
112 self.tip = block.sha256
113 height += 1
114 yield TestInstance([[block, True]])
116 ''' Mine 199 new version blocks on last valid tip '''
117 test_blocks = []
118 for i in range(199):
119 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
120 block.nVersion = 4
121 block.rehash()
122 block.solve()
123 test_blocks.append([block, True])
124 self.last_block_time += 1
125 self.tip = block.sha256
126 height += 1
127 yield TestInstance(test_blocks, sync_every_block=False)
129 ''' Mine 1 old version block '''
130 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
131 block.nVersion = 3
132 block.rehash()
133 block.solve()
134 self.last_block_time += 1
135 self.tip = block.sha256
136 height += 1
137 yield TestInstance([[block, True]])
139 ''' Mine 1 new version block '''
140 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
141 block.nVersion = 4
142 block.rehash()
143 block.solve()
144 self.last_block_time += 1
145 self.tip = block.sha256
146 height += 1
147 yield TestInstance([[block, True]])
150 Check that the new CLTV rules are enforced in the 951st version 4
151 block.
153 spendtx = self.create_transaction(self.nodes[0],
154 self.coinbase_blocks[1], self.nodeaddress, 1.0)
155 cltv_invalidate(spendtx)
156 spendtx.rehash()
158 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
159 block.nVersion = 4
160 block.vtx.append(spendtx)
161 block.hashMerkleRoot = block.calc_merkle_root()
162 block.rehash()
163 block.solve()
164 self.last_block_time += 1
165 yield TestInstance([[block, False]])
167 ''' Mine 1 old version block, should be invalid '''
168 block = create_block(self.tip, create_coinbase(height), self.last_block_time + 1)
169 block.nVersion = 3
170 block.rehash()
171 block.solve()
172 self.last_block_time += 1
173 yield TestInstance([[block, False]])
175 if __name__ == '__main__':
176 BIP65Test().main()