MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / test_block_max_weight

Method test_block_max_weight

test/functional/mining_basic.py:293–382  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

291 assert_greater_than_or_equal(expected_weight, total_weight)
292
293 def test_block_max_weight(self):
294 self.log.info("Testing default and custom -blockmaxweight startup options.")
295
296 LARGE_TXS_COUNT = 10
297 LARGE_VSIZE = int(((MAX_BLOCK_WEIGHT - DEFAULT_BLOCK_RESERVED_WEIGHT) / WITNESS_SCALE_FACTOR) / LARGE_TXS_COUNT)
298 HIGH_FEERATE = Decimal("0.0003")
299
300 # Ensure the mempool is empty
301 assert_equal(len(self.nodes[0].getrawmempool()), 0)
302
303 # Generate UTXOs and send 10 large transactions with a high fee rate
304 utxos = [self.wallet.get_utxo(confirmed_only=True) for _ in range(LARGE_TXS_COUNT + 4)] # Add 4 more utxos that will be used in the test later
305 self.send_transactions(utxos[:LARGE_TXS_COUNT], HIGH_FEERATE, LARGE_VSIZE)
306
307 # Send 2 normal transactions with a lower fee rate
308 NORMAL_VSIZE = int(2000 / WITNESS_SCALE_FACTOR)
309 NORMAL_FEERATE = Decimal("0.0001")
310 self.send_transactions(utxos[LARGE_TXS_COUNT:LARGE_TXS_COUNT + 2], NORMAL_FEERATE, NORMAL_VSIZE)
311
312 # Check that the mempool contains all transactions
313 self.log.info(f"Testing that the mempool contains {LARGE_TXS_COUNT + 2} transactions.")
314 assert_equal(len(self.nodes[0].getrawmempool()), LARGE_TXS_COUNT + 2)
315
316 # Verify the block template includes only the 10 high-fee transactions
317 self.log.info("Testing that the block template includes only the 10 large transactions.")
318 self.verify_block_template(
319 expected_tx_count=LARGE_TXS_COUNT,
320 expected_weight=MAX_BLOCK_WEIGHT - DEFAULT_BLOCK_RESERVED_WEIGHT,
321 )
322
323 # Test block template creation with custom -blockmaxweight
324 custom_block_weight = MAX_BLOCK_WEIGHT - 2000
325 # Reducing the weight by 2000 units will prevent 1 large transaction from fitting into the block.
326 self.restart_node(0, extra_args=[f"-blockmaxweight={custom_block_weight}"])
327
328 self.log.info("Testing the block template with custom -blockmaxweight to include 9 large and 2 normal transactions.")
329 self.verify_block_template(
330 expected_tx_count=11,
331 expected_weight=MAX_BLOCK_WEIGHT - DEFAULT_BLOCK_RESERVED_WEIGHT - 2000,
332 )
333
334 # Ensure the block weight does not exceed the maximum
335 self.log.info(f"Testing that the block weight will never exceed {MAX_BLOCK_WEIGHT - DEFAULT_BLOCK_RESERVED_WEIGHT}.")
336 self.restart_node(0, extra_args=[f"-blockmaxweight={MAX_BLOCK_WEIGHT}"])
337 self.log.info("Sending 2 additional normal transactions to fill the mempool to the maximum block weight.")
338 self.send_transactions(utxos[LARGE_TXS_COUNT + 2:], NORMAL_FEERATE, NORMAL_VSIZE)
339 self.log.info(f"Testing that the mempool's weight matches the maximum block weight: {MAX_BLOCK_WEIGHT}.")
340 assert_equal(self.nodes[0].getmempoolinfo()['bytes'] * WITNESS_SCALE_FACTOR, MAX_BLOCK_WEIGHT)
341
342 self.log.info("Testing that the block template includes only 10 transactions and cannot reach full block weight.")
343 self.verify_block_template(
344 expected_tx_count=LARGE_TXS_COUNT,
345 expected_weight=MAX_BLOCK_WEIGHT - DEFAULT_BLOCK_RESERVED_WEIGHT,
346 )
347
348 self.log.info("Test -blockreservedweight startup option.")
349 # Lowering the -blockreservedweight by 4000 will allow for two more transactions.
350 self.restart_node(0, extra_args=["-blockreservedweight=4000"])

Callers 1

run_testMethod · 0.95

Calls 8

send_transactionsMethod · 0.95
verify_block_templateMethod · 0.95
assert_equalFunction · 0.90
infoMethod · 0.80
get_utxoMethod · 0.80
restart_nodeMethod · 0.45
stop_nodeMethod · 0.45

Tested by

no test coverage detected