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.
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
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.
33 tx
.vin
[0].scriptSig
= CScript([OP_1NEGATE
, OP_CHECKLOCKTIMEVERIFY
, OP_DROP
] +
34 list(CScript(tx
.vin
[0].scriptSig
)))
37 class BIP65Test(ComparisonTestFramework
):
42 self
.extra_args
= [['-whitelist=127.0.0.1', '-blockversion=3']]
45 test
= TestManager(self
, self
.options
.tmpdir
)
46 test
.add_all_connections(self
.nodes
)
47 NetworkThread().start() # Start up network handling in another thread
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
)
57 f
= BytesIO(hex_str_to_bytes(signresult
['hex']))
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 '''
72 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
76 test_blocks
.append([block
, True])
77 self
.last_block_time
+= 1
78 self
.tip
= block
.sha256
80 yield TestInstance(test_blocks
, sync_every_block
=False)
82 ''' Mine 749 version 4 blocks '''
85 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
89 test_blocks
.append([block
, True])
90 self
.last_block_time
+= 1
91 self
.tip
= block
.sha256
93 yield TestInstance(test_blocks
, sync_every_block
=False)
96 Check that the new CLTV rules are not enforced in the 750th
99 spendtx
= self
.create_transaction(self
.nodes
[0],
100 self
.coinbase_blocks
[0], self
.nodeaddress
, 1.0)
101 cltv_invalidate(spendtx
)
104 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
106 block
.vtx
.append(spendtx
)
107 block
.hashMerkleRoot
= block
.calc_merkle_root()
111 self
.last_block_time
+= 1
112 self
.tip
= block
.sha256
114 yield TestInstance([[block
, True]])
116 ''' Mine 199 new version blocks on last valid tip '''
119 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
123 test_blocks
.append([block
, True])
124 self
.last_block_time
+= 1
125 self
.tip
= block
.sha256
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)
134 self
.last_block_time
+= 1
135 self
.tip
= block
.sha256
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)
144 self
.last_block_time
+= 1
145 self
.tip
= block
.sha256
147 yield TestInstance([[block
, True]])
150 Check that the new CLTV rules are enforced in the 951st version 4
153 spendtx
= self
.create_transaction(self
.nodes
[0],
154 self
.coinbase_blocks
[1], self
.nodeaddress
, 1.0)
155 cltv_invalidate(spendtx
)
158 block
= create_block(self
.tip
, create_coinbase(height
), self
.last_block_time
+ 1)
160 block
.vtx
.append(spendtx
)
161 block
.hashMerkleRoot
= block
.calc_merkle_root()
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)
172 self
.last_block_time
+= 1
173 yield TestInstance([[block
, False]])
175 if __name__
== '__main__':