2
2
# Copyright (c) 2015-2019 The Bitcoin Core developers
3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
- """Test activation of the first version bits soft fork.
5
+ """Test CSV soft fork activation .
6
6
7
7
This soft fork will activate the following BIPS:
8
8
BIP 68 - nSequence relative lock times
9
9
BIP 112 - CHECKSEQUENCEVERIFY
10
10
BIP 113 - MedianTimePast semantics for nLockTime
11
11
12
- regtest lock-in with 108/144 block signalling
13
- activation after a further 144 blocks
14
-
15
12
mine 82 blocks whose coinbases will be used to generate inputs for our tests
16
- mine 61 blocks to transition from DEFINED to STARTED
17
- mine 144 blocks only 100 of which are signaling readiness in order to fail to change state this period
18
- mine 144 blocks with 108 signaling and verify STARTED->LOCKED_IN
19
- mine 140 blocks and seed block chain with the 82 inputs will use for our tests at height 572
20
- mine 3 blocks and verify still at LOCKED_IN and test that enforcement has not triggered
21
- mine 1 block and test that enforcement has triggered (which triggers ACTIVE)
13
+ mine 345 blocks and seed block chain with the 82 inputs will use for our tests at height 427
14
+ mine 2 blocks and verify soft fork not yet activated
15
+ mine 1 block and test that soft fork is activated (rules enforced for next block)
22
16
Test BIP 113 is enforced
23
17
Mine 4 blocks so next height is 580 and test BIP 68 is enforced for time and height
24
18
Mine 1 block so next height is 581 and test BIP 68 now passes time but not height
58
52
from test_framework .test_framework import BitcoinTestFramework
59
53
from test_framework .util import (
60
54
assert_equal ,
61
- get_bip9_status ,
62
55
hex_str_to_bytes ,
56
+ softfork_active ,
63
57
)
64
58
65
59
BASE_RELATIVE_LOCKTIME = 10
60
+ CSV_ACTIVATION_HEIGHT = 432
66
61
SEQ_DISABLE_FLAG = 1 << 31
67
62
SEQ_RANDOM_HIGH_BIT = 1 << 25
68
63
SEQ_TYPE_FLAG = 1 << 22
@@ -148,20 +143,19 @@ def set_test_params(self):
148
143
def skip_test_if_missing_module (self ):
149
144
self .skip_if_no_wallet ()
150
145
151
- def generate_blocks (self , number , version , test_blocks = None ):
152
- if test_blocks is None :
153
- test_blocks = []
146
+ def generate_blocks (self , number ):
147
+ test_blocks = []
154
148
for i in range (number ):
155
- block = self .create_test_block ([], version )
149
+ block = self .create_test_block ([])
156
150
test_blocks .append (block )
157
151
self .last_block_time += 600
158
152
self .tip = block .sha256
159
153
self .tipheight += 1
160
154
return test_blocks
161
155
162
- def create_test_block (self , txs , version = 536870912 ):
156
+ def create_test_block (self , txs ):
163
157
block = create_block (self .tip , create_coinbase (self .tipheight + 1 ), self .last_block_time + 600 )
164
- block .nVersion = version
158
+ block .nVersion = 4
165
159
block .vtx .extend (txs )
166
160
block .hashMerkleRoot = block .calc_merkle_root ()
167
161
block .rehash ()
@@ -187,45 +181,14 @@ def run_test(self):
187
181
self .tip = int (self .nodes [0 ].getbestblockhash (), 16 )
188
182
self .nodeaddress = self .nodes [0 ].getnewaddress ()
189
183
190
- self .log .info ("Test that the csv softfork is DEFINED" )
191
- assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'defined' )
192
- test_blocks = self .generate_blocks (61 , 4 )
193
- self .send_blocks (test_blocks )
194
-
195
- self .log .info ("Advance from DEFINED to STARTED, height = 143" )
196
- assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'started' )
197
-
198
- self .log .info ("Fail to achieve LOCKED_IN" )
199
- # 100 out of 144 signal bit 0. Use a variety of bits to simulate multiple parallel softforks
200
-
201
- test_blocks = self .generate_blocks (50 , 536870913 ) # 0x20000001 (signalling ready)
202
- test_blocks = self .generate_blocks (20 , 4 , test_blocks ) # 0x00000004 (signalling not)
203
- test_blocks = self .generate_blocks (50 , 536871169 , test_blocks ) # 0x20000101 (signalling ready)
204
- test_blocks = self .generate_blocks (24 , 536936448 , test_blocks ) # 0x20010000 (signalling not)
205
- self .send_blocks (test_blocks )
206
-
207
- self .log .info ("Failed to advance past STARTED, height = 287" )
208
- assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'started' )
209
-
210
- self .log .info ("Generate blocks to achieve LOCK-IN" )
211
- # 108 out of 144 signal bit 0 to achieve lock-in
212
- # using a variety of bits to simulate multiple parallel softforks
213
- test_blocks = self .generate_blocks (58 , 536870913 ) # 0x20000001 (signalling ready)
214
- test_blocks = self .generate_blocks (26 , 4 , test_blocks ) # 0x00000004 (signalling not)
215
- test_blocks = self .generate_blocks (50 , 536871169 , test_blocks ) # 0x20000101 (signalling ready)
216
- test_blocks = self .generate_blocks (10 , 536936448 , test_blocks ) # 0x20010000 (signalling not)
217
- self .send_blocks (test_blocks )
218
-
219
- self .log .info ("Advanced from STARTED to LOCKED_IN, height = 431" )
220
- assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'locked_in' )
221
-
222
- # Generate 140 more version 4 blocks
223
- test_blocks = self .generate_blocks (140 , 4 )
184
+ # Activation height is hardcoded
185
+ test_blocks = self .generate_blocks (345 )
224
186
self .send_blocks (test_blocks )
187
+ assert not softfork_active (self .nodes [0 ], 'csv' )
225
188
226
- # Inputs at height = 572
189
+ # Inputs at height = 431
227
190
#
228
- # Put inputs for all tests in the chain at height 572 (tip now = 571 ) (time increases by 600s per block)
191
+ # Put inputs for all tests in the chain at height 431 (tip now = 430 ) (time increases by 600s per block)
229
192
# Note we reuse inputs for v1 and v2 txs so must test these separately
230
193
# 16 normal inputs
231
194
bip68inputs = []
@@ -255,19 +218,20 @@ def run_test(self):
255
218
bip113input = send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks , self .nodeaddress )
256
219
257
220
self .nodes [0 ].setmocktime (self .last_block_time + 600 )
258
- inputblockhash = self .nodes [0 ].generate (1 )[0 ] # 1 block generated for inputs to be in chain at height 572
221
+ inputblockhash = self .nodes [0 ].generate (1 )[0 ] # 1 block generated for inputs to be in chain at height 431
259
222
self .nodes [0 ].setmocktime (0 )
260
223
self .tip = int (inputblockhash , 16 )
261
224
self .tipheight += 1
262
225
self .last_block_time += 600
263
226
assert_equal (len (self .nodes [0 ].getblock (inputblockhash , True )["tx" ]), 82 + 1 )
264
227
265
228
# 2 more version 4 blocks
266
- test_blocks = self .generate_blocks (2 , 4 )
229
+ test_blocks = self .generate_blocks (2 )
267
230
self .send_blocks (test_blocks )
268
231
269
- self .log .info ("Not yet advanced to ACTIVE, height = 574 (will activate for block 576, not 575)" )
270
- assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'locked_in' )
232
+ assert_equal (self .tipheight , CSV_ACTIVATION_HEIGHT - 2 )
233
+ self .log .info ("Height = {}, CSV not yet active (will activate for block {}, not {})" .format (self .tipheight , CSV_ACTIVATION_HEIGHT , CSV_ACTIVATION_HEIGHT - 1 ))
234
+ assert not softfork_active (self .nodes [0 ], 'csv' )
271
235
272
236
# Test both version 1 and version 2 transactions for all tests
273
237
# BIP113 test transaction will be modified before each use to put in appropriate block time
@@ -340,10 +304,11 @@ def run_test(self):
340
304
self .send_blocks ([self .create_test_block (success_txs )])
341
305
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
342
306
343
- # 1 more version 4 block to get us to height 575 so the fork should now be active for the next block
344
- test_blocks = self .generate_blocks (1 , 4 )
307
+ # 1 more version 4 block to get us to height 432 so the fork should now be active for the next block
308
+ assert not softfork_active (self .nodes [0 ], 'csv' )
309
+ test_blocks = self .generate_blocks (1 )
345
310
self .send_blocks (test_blocks )
346
- assert_equal ( get_bip9_status ( self .nodes [0 ], 'csv' )[ 'status' ], 'active ' )
311
+ assert softfork_active ( self .nodes [0 ], 'csv' )
347
312
348
313
self .log .info ("Post-Soft Fork Tests." )
349
314
@@ -364,8 +329,8 @@ def run_test(self):
364
329
self .send_blocks ([self .create_test_block ([bip113tx ])])
365
330
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
366
331
367
- # Next block height = 580 after 4 blocks of random version
368
- test_blocks = self .generate_blocks (4 , 1234 )
332
+ # Next block height = 437 after 4 blocks of random version
333
+ test_blocks = self .generate_blocks (4 )
369
334
self .send_blocks (test_blocks )
370
335
371
336
self .log .info ("BIP 68 tests" )
@@ -392,8 +357,8 @@ def run_test(self):
392
357
for tx in bip68heighttxs :
393
358
self .send_blocks ([self .create_test_block ([tx ])], success = False )
394
359
395
- # Advance one block to 581
396
- test_blocks = self .generate_blocks (1 , 1234 )
360
+ # Advance one block to 438
361
+ test_blocks = self .generate_blocks (1 )
397
362
self .send_blocks (test_blocks )
398
363
399
364
# Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
@@ -403,8 +368,8 @@ def run_test(self):
403
368
for tx in bip68heighttxs :
404
369
self .send_blocks ([self .create_test_block ([tx ])], success = False )
405
370
406
- # Advance one block to 582
407
- test_blocks = self .generate_blocks (1 , 1234 )
371
+ # Advance one block to 439
372
+ test_blocks = self .generate_blocks (1 )
408
373
self .send_blocks (test_blocks )
409
374
410
375
# All BIP 68 txs should pass
0 commit comments