22# Copyright (c) 2015-2019 The Bitcoin Core developers
33# Distributed under the MIT software license, see the accompanying
44# 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 .
66
77This soft fork will activate the following BIPS:
88BIP 68 - nSequence relative lock times
99BIP 112 - CHECKSEQUENCEVERIFY
1010BIP 113 - MedianTimePast semantics for nLockTime
1111
12- regtest lock-in with 108/144 block signalling
13- activation after a further 144 blocks
14-
1512mine 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)
2216Test BIP 113 is enforced
2317Mine 4 blocks so next height is 580 and test BIP 68 is enforced for time and height
2418Mine 1 block so next height is 581 and test BIP 68 now passes time but not height
5852from test_framework .test_framework import BitcoinTestFramework
5953from test_framework .util import (
6054 assert_equal ,
61- get_bip9_status ,
6255 hex_str_to_bytes ,
56+ softfork_active ,
6357)
6458
6559BASE_RELATIVE_LOCKTIME = 10
60+ CSV_ACTIVATION_HEIGHT = 432
6661SEQ_DISABLE_FLAG = 1 << 31
6762SEQ_RANDOM_HIGH_BIT = 1 << 25
6863SEQ_TYPE_FLAG = 1 << 22
@@ -148,20 +143,19 @@ def set_test_params(self):
148143 def skip_test_if_missing_module (self ):
149144 self .skip_if_no_wallet ()
150145
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 = []
154148 for i in range (number ):
155- block = self .create_test_block ([], version )
149+ block = self .create_test_block ([])
156150 test_blocks .append (block )
157151 self .last_block_time += 600
158152 self .tip = block .sha256
159153 self .tipheight += 1
160154 return test_blocks
161155
162- def create_test_block (self , txs , version = 536870912 ):
156+ def create_test_block (self , txs ):
163157 block = create_block (self .tip , create_coinbase (self .tipheight + 1 ), self .last_block_time + 600 )
164- block .nVersion = version
158+ block .nVersion = 4
165159 block .vtx .extend (txs )
166160 block .hashMerkleRoot = block .calc_merkle_root ()
167161 block .rehash ()
@@ -187,45 +181,14 @@ def run_test(self):
187181 self .tip = int (self .nodes [0 ].getbestblockhash (), 16 )
188182 self .nodeaddress = self .nodes [0 ].getnewaddress ()
189183
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 )
224186 self .send_blocks (test_blocks )
187+ assert not softfork_active (self .nodes [0 ], 'csv' )
225188
226- # Inputs at height = 572
189+ # Inputs at height = 431
227190 #
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)
229192 # Note we reuse inputs for v1 and v2 txs so must test these separately
230193 # 16 normal inputs
231194 bip68inputs = []
@@ -255,19 +218,20 @@ def run_test(self):
255218 bip113input = send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks , self .nodeaddress )
256219
257220 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
259222 self .nodes [0 ].setmocktime (0 )
260223 self .tip = int (inputblockhash , 16 )
261224 self .tipheight += 1
262225 self .last_block_time += 600
263226 assert_equal (len (self .nodes [0 ].getblock (inputblockhash , True )["tx" ]), 82 + 1 )
264227
265228 # 2 more version 4 blocks
266- test_blocks = self .generate_blocks (2 , 4 )
229+ test_blocks = self .generate_blocks (2 )
267230 self .send_blocks (test_blocks )
268231
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' )
271235
272236 # Test both version 1 and version 2 transactions for all tests
273237 # BIP113 test transaction will be modified before each use to put in appropriate block time
@@ -340,10 +304,11 @@ def run_test(self):
340304 self .send_blocks ([self .create_test_block (success_txs )])
341305 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
342306
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 )
345310 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' )
347312
348313 self .log .info ("Post-Soft Fork Tests." )
349314
@@ -364,8 +329,8 @@ def run_test(self):
364329 self .send_blocks ([self .create_test_block ([bip113tx ])])
365330 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
366331
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 )
369334 self .send_blocks (test_blocks )
370335
371336 self .log .info ("BIP 68 tests" )
@@ -392,8 +357,8 @@ def run_test(self):
392357 for tx in bip68heighttxs :
393358 self .send_blocks ([self .create_test_block ([tx ])], success = False )
394359
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 )
397362 self .send_blocks (test_blocks )
398363
399364 # Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
@@ -403,8 +368,8 @@ def run_test(self):
403368 for tx in bip68heighttxs :
404369 self .send_blocks ([self .create_test_block ([tx ])], success = False )
405370
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 )
408373 self .send_blocks (test_blocks )
409374
410375 # All BIP 68 txs should pass
0 commit comments