22
22
CTransaction ,
23
23
CTxIn ,
24
24
CTxOut ,
25
- MAX_BLOCK_BASE_SIZE ,
25
+ MAX_BLOCK_WEIGHT ,
26
26
uint256_from_compact ,
27
27
uint256_from_str ,
28
28
)
@@ -307,33 +307,33 @@ def run_test(self):
307
307
b22 = self .next_block (22 , spend = out [5 ])
308
308
self .send_blocks ([b22 ], success = False , reject_reason = 'bad-txns-premature-spend-of-coinbase' , reconnect = True )
309
309
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
311
311
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
312
312
# \-> b12 (3) -> b13 (4) -> b15 (5) -> b23 (6)
313
313
# \-> b24 (6) -> b25 (7)
314
314
# \-> 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 " )
316
316
self .move_tip (15 )
317
317
b23 = self .next_block (23 , spend = out [6 ])
318
318
tx = CTransaction ()
319
- script_length = MAX_BLOCK_BASE_SIZE - len ( b23 .serialize ()) - 69
319
+ script_length = ( MAX_BLOCK_WEIGHT - b23 .get_weight () - 276 ) // 4
320
320
script_output = CScript ([b'\x00 ' * script_length ])
321
321
tx .vout .append (CTxOut (0 , script_output ))
322
322
tx .vin .append (CTxIn (COutPoint (b23 .vtx [1 ].sha256 , 0 )))
323
323
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 )
326
326
self .send_blocks ([b23 ], True )
327
327
self .save_spendable_output ()
328
328
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 " )
330
330
self .move_tip (15 )
331
331
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
333
333
script_output = CScript ([b'\x00 ' * (script_length + 1 )])
334
334
tx .vout = [CTxOut (0 , script_output )]
335
335
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 )
337
337
self .send_blocks ([b24 ], success = False , reject_reason = 'bad-blk-length' , reconnect = True )
338
338
339
339
b25 = self .next_block (25 , spend = out [7 ])
@@ -485,13 +485,13 @@ def run_test(self):
485
485
# Until block is full, add tx's with 1 satoshi to p2sh_script, the rest to OP_TRUE
486
486
tx_new = None
487
487
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 :
490
490
tx_new = self .create_tx (tx_last , 1 , 1 , p2sh_script )
491
491
tx_new .vout .append (CTxOut (tx_last .vout [1 ].nValue - 1 , CScript ([OP_TRUE ])))
492
492
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 :
495
495
break
496
496
b39 .vtx .append (tx_new ) # add tx to block
497
497
tx_last = tx_new
@@ -502,7 +502,7 @@ def run_test(self):
502
502
# Make sure we didn't accidentally make too big a block. Note that the
503
503
# size of the block has non-determinism due to the ECDSA signature in
504
504
# the first transaction.
505
- while ( len ( b39 .serialize ()) >= MAX_BLOCK_BASE_SIZE ) :
505
+ while b39 .get_weight () >= MAX_BLOCK_WEIGHT :
506
506
del b39 .vtx [- 1 ]
507
507
508
508
b39 = self .update_block (39 , [])
@@ -892,7 +892,7 @@ def run_test(self):
892
892
self .send_blocks ([b63 ], success = False , reject_reason = 'bad-txns-nonfinal' , reconnect = True )
893
893
894
894
# 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,
896
896
# does not cause a subsequent, identical block with canonical encoding to be rejected. The test does not
897
897
# care whether the bloated block is accepted or rejected; it only cares that the second block is accepted.
898
898
#
@@ -917,12 +917,12 @@ def run_test(self):
917
917
tx = CTransaction ()
918
918
919
919
# 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
921
921
script_output = CScript ([b'\x00 ' * script_length ])
922
922
tx .vout .append (CTxOut (0 , script_output ))
923
923
tx .vin .append (CTxIn (COutPoint (b64a .vtx [1 ].sha256 , 0 )))
924
924
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 )
926
926
self .send_blocks ([b64a ], success = False , reject_reason = 'non-canonical ReadCompactSize()' )
927
927
928
928
# bitcoind doesn't disconnect us for sending a bloated block, but if we subsequently
@@ -936,7 +936,7 @@ def run_test(self):
936
936
b64 = CBlock (b64a )
937
937
b64 .vtx = copy .deepcopy (b64a .vtx )
938
938
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 )
940
940
self .blocks [64 ] = b64
941
941
b64 = self .update_block (64 , [])
942
942
self .send_blocks ([b64 ], True )
@@ -1270,12 +1270,12 @@ def run_test(self):
1270
1270
for i in range (89 , LARGE_REORG_SIZE + 89 ):
1271
1271
b = self .next_block (i , spend )
1272
1272
tx = CTransaction ()
1273
- script_length = MAX_BLOCK_BASE_SIZE - len ( b . serialize ()) - 69
1273
+ script_length = ( MAX_BLOCK_WEIGHT - b . get_weight () - 276 ) // 4
1274
1274
script_output = CScript ([b'\x00 ' * script_length ])
1275
1275
tx .vout .append (CTxOut (0 , script_output ))
1276
1276
tx .vin .append (CTxIn (COutPoint (b .vtx [1 ].sha256 , 0 )))
1277
1277
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 )
1279
1279
blocks .append (b )
1280
1280
self .save_spendable_output ()
1281
1281
spend = self .get_spendable_output ()
0 commit comments