2222 CTransaction ,
2323 CTxIn ,
2424 CTxOut ,
25- MAX_BLOCK_BASE_SIZE ,
25+ MAX_BLOCK_WEIGHT ,
2626 uint256_from_compact ,
2727 uint256_from_str ,
2828)
@@ -307,33 +307,33 @@ def run_test(self):
307307 b22 = self .next_block (22 , spend = out [5 ])
308308 self .send_blocks ([b22 ], success = False , reject_reason = 'bad-txns-premature-spend-of-coinbase' , reconnect = True )
309309
310- # Create a block on either side of MAX_BLOCK_BASE_SIZE and make sure its accepted/rejected
310+ # Create a block on either side of MAX_BLOCK_WEIGHT and make sure its accepted/rejected
311311 # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
312312 # \-> b12 (3) -> b13 (4) -> b15 (5) -> b23 (6)
313313 # \-> b24 (6) -> b25 (7)
314314 # \-> b3 (1) -> b4 (2)
315- self .log .info ("Accept a block of size MAX_BLOCK_BASE_SIZE " )
315+ self .log .info ("Accept a block of weight MAX_BLOCK_WEIGHT " )
316316 self .move_tip (15 )
317317 b23 = self .next_block (23 , spend = out [6 ])
318318 tx = CTransaction ()
319- script_length = MAX_BLOCK_BASE_SIZE - len ( b23 .serialize ()) - 69
319+ script_length = ( MAX_BLOCK_WEIGHT - b23 .get_weight () - 276 ) // 4
320320 script_output = CScript ([b'\x00 ' * script_length ])
321321 tx .vout .append (CTxOut (0 , script_output ))
322322 tx .vin .append (CTxIn (COutPoint (b23 .vtx [1 ].sha256 , 0 )))
323323 b23 = self .update_block (23 , [tx ])
324- # Make sure the math above worked out to produce a max-sized block
325- assert_equal (len ( b23 .serialize ()), MAX_BLOCK_BASE_SIZE )
324+ # Make sure the math above worked out to produce a max-weighted block
325+ assert_equal (b23 .get_weight (), MAX_BLOCK_WEIGHT )
326326 self .send_blocks ([b23 ], True )
327327 self .save_spendable_output ()
328328
329- self .log .info ("Reject a block of size MAX_BLOCK_BASE_SIZE + 1 " )
329+ self .log .info ("Reject a block of weight MAX_BLOCK_WEIGHT + 4 " )
330330 self .move_tip (15 )
331331 b24 = self .next_block (24 , spend = out [6 ])
332- script_length = MAX_BLOCK_BASE_SIZE - len ( b24 .serialize ()) - 69
332+ script_length = ( MAX_BLOCK_WEIGHT - b24 .get_weight () - 276 ) // 4
333333 script_output = CScript ([b'\x00 ' * (script_length + 1 )])
334334 tx .vout = [CTxOut (0 , script_output )]
335335 b24 = self .update_block (24 , [tx ])
336- assert_equal (len ( b24 .serialize ()), MAX_BLOCK_BASE_SIZE + 1 )
336+ assert_equal (b24 .get_weight (), MAX_BLOCK_WEIGHT + 1 * 4 )
337337 self .send_blocks ([b24 ], success = False , reject_reason = 'bad-blk-length' , reconnect = True )
338338
339339 b25 = self .next_block (25 , spend = out [7 ])
@@ -485,13 +485,13 @@ def run_test(self):
485485 # Until block is full, add tx's with 1 satoshi to p2sh_script, the rest to OP_TRUE
486486 tx_new = None
487487 tx_last = tx
488- total_size = len ( b39 .serialize () )
489- while ( total_size < MAX_BLOCK_BASE_SIZE ) :
488+ total_weight = b39 .get_weight ( )
489+ while total_weight < MAX_BLOCK_WEIGHT :
490490 tx_new = self .create_tx (tx_last , 1 , 1 , p2sh_script )
491491 tx_new .vout .append (CTxOut (tx_last .vout [1 ].nValue - 1 , CScript ([OP_TRUE ])))
492492 tx_new .rehash ()
493- total_size += len ( tx_new .serialize () )
494- if total_size >= MAX_BLOCK_BASE_SIZE :
493+ total_weight += tx_new .get_weight ( )
494+ if total_weight >= MAX_BLOCK_WEIGHT :
495495 break
496496 b39 .vtx .append (tx_new ) # add tx to block
497497 tx_last = tx_new
@@ -502,7 +502,7 @@ def run_test(self):
502502 # Make sure we didn't accidentally make too big a block. Note that the
503503 # size of the block has non-determinism due to the ECDSA signature in
504504 # the first transaction.
505- while ( len ( b39 .serialize ()) >= MAX_BLOCK_BASE_SIZE ) :
505+ while b39 .get_weight () >= MAX_BLOCK_WEIGHT :
506506 del b39 .vtx [- 1 ]
507507
508508 b39 = self .update_block (39 , [])
@@ -892,7 +892,7 @@ def run_test(self):
892892 self .send_blocks ([b63 ], success = False , reject_reason = 'bad-txns-nonfinal' , reconnect = True )
893893
894894 # This checks that a block with a bloated VARINT between the block_header and the array of tx such that
895- # the block is > MAX_BLOCK_BASE_SIZE with the bloated varint, but <= MAX_BLOCK_BASE_SIZE without the bloated varint,
895+ # the block is > MAX_BLOCK_WEIGHT with the bloated varint, but <= MAX_BLOCK_WEIGHT without the bloated varint,
896896 # does not cause a subsequent, identical block with canonical encoding to be rejected. The test does not
897897 # care whether the bloated block is accepted or rejected; it only cares that the second block is accepted.
898898 #
@@ -917,12 +917,12 @@ def run_test(self):
917917 tx = CTransaction ()
918918
919919 # use canonical serialization to calculate size
920- script_length = MAX_BLOCK_BASE_SIZE - len (b64a .normal_serialize ()) - 69
920+ script_length = ( MAX_BLOCK_WEIGHT - 4 * len (b64a .normal_serialize ()) - 276 ) // 4
921921 script_output = CScript ([b'\x00 ' * script_length ])
922922 tx .vout .append (CTxOut (0 , script_output ))
923923 tx .vin .append (CTxIn (COutPoint (b64a .vtx [1 ].sha256 , 0 )))
924924 b64a = self .update_block ("64a" , [tx ])
925- assert_equal (len ( b64a .serialize ()), MAX_BLOCK_BASE_SIZE + 8 )
925+ assert_equal (b64a .get_weight (), MAX_BLOCK_WEIGHT + 8 * 4 )
926926 self .send_blocks ([b64a ], success = False , reject_reason = 'non-canonical ReadCompactSize()' )
927927
928928 # bitcoind doesn't disconnect us for sending a bloated block, but if we subsequently
@@ -936,7 +936,7 @@ def run_test(self):
936936 b64 = CBlock (b64a )
937937 b64 .vtx = copy .deepcopy (b64a .vtx )
938938 assert_equal (b64 .hash , b64a .hash )
939- assert_equal (len ( b64 .serialize ()), MAX_BLOCK_BASE_SIZE )
939+ assert_equal (b64 .get_weight (), MAX_BLOCK_WEIGHT )
940940 self .blocks [64 ] = b64
941941 b64 = self .update_block (64 , [])
942942 self .send_blocks ([b64 ], True )
@@ -1270,12 +1270,12 @@ def run_test(self):
12701270 for i in range (89 , LARGE_REORG_SIZE + 89 ):
12711271 b = self .next_block (i , spend )
12721272 tx = CTransaction ()
1273- script_length = MAX_BLOCK_BASE_SIZE - len ( b . serialize ()) - 69
1273+ script_length = ( MAX_BLOCK_WEIGHT - b . get_weight () - 276 ) // 4
12741274 script_output = CScript ([b'\x00 ' * script_length ])
12751275 tx .vout .append (CTxOut (0 , script_output ))
12761276 tx .vin .append (CTxIn (COutPoint (b .vtx [1 ].sha256 , 0 )))
12771277 b = self .update_block (i , [tx ])
1278- assert_equal (len ( b . serialize ()), MAX_BLOCK_BASE_SIZE )
1278+ assert_equal (b . get_weight (), MAX_BLOCK_WEIGHT )
12791279 blocks .append (b )
12801280 self .save_spendable_output ()
12811281 spend = self .get_spendable_output ()
0 commit comments