Skip to content

Commit 607076d

Browse files
committed
test: remove confusing MAX_BLOCK_BASE_SIZE
The constant `MAX_BLOCK_BASE_SIZE` has been removed from the core implementation years ago due to being confusing and superfluous, as it is implied by the block weight limit (see PRs #10618 and #10608). Since there is also no point in still keeping it in the functional test framework, we switch to weight-based accounting on the relevant test code parts and use `MAX_BLOCK_WEIGHT` instead for the block limit checks.
1 parent 4af97c7 commit 607076d

File tree

5 files changed

+35
-47
lines changed

5 files changed

+35
-47
lines changed

test/functional/feature_block.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
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()

test/functional/mempool_accept.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
COIN,
1515
COutPoint,
1616
CTxOut,
17-
MAX_BLOCK_BASE_SIZE,
17+
MAX_BLOCK_WEIGHT,
1818
MAX_MONEY,
1919
tx_from_hex,
2020
)
@@ -207,7 +207,7 @@ def run_test(self):
207207

208208
self.log.info('A really large transaction')
209209
tx = tx_from_hex(raw_tx_reference)
210-
tx.vin = [tx.vin[0]] * math.ceil(MAX_BLOCK_BASE_SIZE / len(tx.vin[0].serialize()))
210+
tx.vin = [tx.vin[0]] * math.ceil(MAX_BLOCK_WEIGHT // 4 / len(tx.vin[0].serialize()))
211211
self.check_mempool_result(
212212
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-oversize'}],
213213
rawtxs=[tx.serialize().hex()],

test/functional/mining_prioritisetransaction.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import time
88

9-
from test_framework.messages import COIN, MAX_BLOCK_BASE_SIZE
9+
from test_framework.messages import COIN, MAX_BLOCK_WEIGHT
1010
from test_framework.test_framework import BitcoinTestFramework
1111
from test_framework.util import assert_equal, assert_raises_rpc_error, create_confirmed_utxos, create_lots_of_big_transactions, gen_return_txouts
1212

@@ -61,15 +61,15 @@ def run_test(self):
6161
txids[i] = create_lots_of_big_transactions(self.nodes[0], self.txouts, utxos[start_range:end_range], end_range - start_range, (i+1)*base_fee)
6262

6363
# Make sure that the size of each group of transactions exceeds
64-
# MAX_BLOCK_BASE_SIZE -- otherwise the test needs to be revised to create
65-
# more transactions.
64+
# MAX_BLOCK_WEIGHT // 4 -- otherwise the test needs to be revised to
65+
# create more transactions.
6666
mempool = self.nodes[0].getrawmempool(True)
6767
sizes = [0, 0, 0]
6868
for i in range(3):
6969
for j in txids[i]:
7070
assert j in mempool
7171
sizes[i] += mempool[j]['vsize']
72-
assert sizes[i] > MAX_BLOCK_BASE_SIZE # Fail => raise utxo_count
72+
assert sizes[i] > MAX_BLOCK_WEIGHT // 4 # Fail => raise utxo_count
7373

7474
# add a fee delta to something in the cheapest bucket and make sure it gets mined
7575
# also check that a different entry in the cheapest bucket is NOT mined

test/functional/p2p_segwit.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
CTxInWitness,
2222
CTxOut,
2323
CTxWitness,
24-
MAX_BLOCK_BASE_SIZE,
24+
MAX_BLOCK_WEIGHT,
2525
MSG_BLOCK,
2626
MSG_TX,
2727
MSG_WITNESS_FLAG,
@@ -110,16 +110,6 @@ def sign_p2pk_witness_input(script, tx_to, in_idx, hashtype, value, key):
110110
tx_to.wit.vtxinwit[in_idx].scriptWitness.stack = [signature, script]
111111
tx_to.rehash()
112112

113-
def get_virtual_size(witness_block):
114-
"""Calculate the virtual size of a witness block.
115-
116-
Virtual size is base + witness/4."""
117-
base_size = len(witness_block.serialize(with_witness=False))
118-
total_size = len(witness_block.serialize())
119-
# the "+3" is so we round up
120-
vsize = int((3 * base_size + total_size + 3) / 4)
121-
return vsize
122-
123113
def test_transaction_acceptance(node, p2p, tx, with_witness, accepted, reason=None):
124114
"""Send a transaction to the node and check that it's accepted to the mempool
125115
@@ -902,7 +892,7 @@ def test_block_malleability(self):
902892
block.solve()
903893

904894
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.append(b'a' * 5000000)
905-
assert get_virtual_size(block) > MAX_BLOCK_BASE_SIZE
895+
assert block.get_weight() > MAX_BLOCK_WEIGHT
906896

907897
# We can't send over the p2p network, because this is too big to relay
908898
# TODO: repeat this test with a block that can be relayed
@@ -911,7 +901,7 @@ def test_block_malleability(self):
911901
assert self.nodes[0].getbestblockhash() != block.hash
912902

913903
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.pop()
914-
assert get_virtual_size(block) < MAX_BLOCK_BASE_SIZE
904+
assert block.get_weight() < MAX_BLOCK_WEIGHT
915905
assert_equal(None, self.nodes[0].submitblock(block.serialize().hex()))
916906

917907
assert self.nodes[0].getbestblockhash() == block.hash
@@ -974,11 +964,10 @@ def test_witness_block_size(self):
974964
child_tx.rehash()
975965
self.update_witness_block_with_transactions(block, [parent_tx, child_tx])
976966

977-
vsize = get_virtual_size(block)
978-
additional_bytes = (MAX_BLOCK_BASE_SIZE - vsize) * 4
967+
additional_bytes = MAX_BLOCK_WEIGHT - block.get_weight()
979968
i = 0
980969
while additional_bytes > 0:
981-
# Add some more bytes to each input until we hit MAX_BLOCK_BASE_SIZE+1
970+
# Add some more bytes to each input until we hit MAX_BLOCK_WEIGHT+1
982971
extra_bytes = min(additional_bytes + 1, 55)
983972
block.vtx[-1].wit.vtxinwit[int(i / (2 * NUM_DROPS))].scriptWitness.stack[i % (2 * NUM_DROPS)] = b'a' * (195 + extra_bytes)
984973
additional_bytes -= extra_bytes
@@ -987,8 +976,7 @@ def test_witness_block_size(self):
987976
block.vtx[0].vout.pop() # Remove old commitment
988977
add_witness_commitment(block)
989978
block.solve()
990-
vsize = get_virtual_size(block)
991-
assert_equal(vsize, MAX_BLOCK_BASE_SIZE + 1)
979+
assert_equal(block.get_weight(), MAX_BLOCK_WEIGHT + 1)
992980
# Make sure that our test case would exceed the old max-network-message
993981
# limit
994982
assert len(block.serialize()) > 2 * 1024 * 1024
@@ -1001,7 +989,7 @@ def test_witness_block_size(self):
1001989
block.vtx[0].vout.pop()
1002990
add_witness_commitment(block)
1003991
block.solve()
1004-
assert get_virtual_size(block) == MAX_BLOCK_BASE_SIZE
992+
assert block.get_weight() == MAX_BLOCK_WEIGHT
1005993

1006994
test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1007995

@@ -1727,7 +1715,7 @@ def test_signature_version_1(self):
17271715
block.vtx.append(tx)
17281716

17291717
# Test the block periodically, if we're close to maxblocksize
1730-
if (get_virtual_size(block) > MAX_BLOCK_BASE_SIZE - 1000):
1718+
if block.get_weight() > MAX_BLOCK_WEIGHT - 4000:
17311719
self.update_witness_block_with_transactions(block, [])
17321720
test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
17331721
block = self.build_next_block()

test/functional/test_framework/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from test_framework.util import hex_str_to_bytes, assert_equal
3333

3434
MAX_LOCATOR_SZ = 101
35-
MAX_BLOCK_BASE_SIZE = 1000000
35+
MAX_BLOCK_WEIGHT = 4000000
3636
MAX_BLOOM_FILTER_SIZE = 36000
3737
MAX_BLOOM_HASH_FUNCS = 50
3838

0 commit comments

Comments
 (0)