Skip to content

test_tx_invalid_first_in_block()

Documentation for tests/frontier/validation/test_transaction.py::test_tx_invalid_first_in_block@02aafd78.

Generate fixtures for these test cases for Bogota with:

fill -v tests/frontier/validation/test_transaction.py::test_tx_invalid_first_in_block --fork Bogota

The first transaction of a block is invalid and the valid one after it would have made it valid had it run first; the block is rejected at the first transaction.

This guards clients that execute a block's transactions in parallel, as block access lists (EIP-7928) allow: they must reject the block without help from the transactions after the invalid one, which a sequential client never looks at. insufficient_funds: the later transaction funds the unfunded sender. nonce_too_high: the sender's nonce 1 arrives before its nonce 0. The later transaction also puts the sender in the block access list, which a client reading state through the list needs before it reaches the check under test. The next block replays it at the same nonce, so it must have left no trace.

Source code in tests/frontier/validation/test_transaction.py
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
@pytest.mark.valid_from("Frontier")
@pytest.mark.invalid_tx_not_last
@pytest.mark.exception_test
@pytest.mark.parametrize("cause", ["insufficient_funds", "nonce_too_high"])
def test_tx_invalid_first_in_block(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: BaseFork,
    cause: str,
) -> None:
    """
    The first transaction of a block is invalid and the valid one after it
    would have made it valid had it run first; the block is rejected at the
    first transaction.

    This guards clients that execute a block's transactions in parallel, as
    block access lists (EIP-7928) allow: they must reject the block without
    help from the transactions after the invalid one, which a sequential
    client never looks at. `insufficient_funds`: the later transaction funds
    the unfunded sender. `nonce_too_high`: the sender's nonce 1 arrives
    before its nonce 0. The later transaction also puts the sender in the
    block access list, which a client reading state through the list needs
    before it reaches the check under test. The next block replays it at the
    same nonce, so it must have left no trace.
    """
    bob_balance = 10**18
    bob = pre.fund_eoa(amount=bob_balance)

    if cause == "insufficient_funds":
        sender = pre.fund_eoa(amount=0)
        carol = pre.fund_eoa()
        error = TransactionException.INSUFFICIENT_ACCOUNT_FUNDS
        invalid_tx = Transaction(
            sender=sender, to=bob, value=1, protected=False, error=error
        )
        trailing_tx = Transaction(
            sender=carol, to=sender, value=10**18, protected=False
        )
        post = {
            sender: Account(nonce=0, balance=10**18),
            bob: Account(balance=bob_balance),
        }
        rejected_entry = BalAccountExpectation(
            balance_changes=[
                BalBalanceChange(block_access_index=2, post_balance=10**18)
            ]
        )
        replay_entry = BalAccountExpectation(
            balance_changes=[
                BalBalanceChange(block_access_index=1, post_balance=10**18)
            ]
        )
    elif cause == "nonce_too_high":
        sender = pre.fund_eoa()
        error = TransactionException.NONCE_MISMATCH_TOO_HIGH
        invalid_tx = Transaction(
            sender=sender,
            nonce=1,
            to=bob,
            value=1,
            protected=False,
            error=error,
        )
        trailing_tx = Transaction(
            sender=sender, nonce=0, to=bob, value=1, protected=False
        )
        post = {
            sender: Account(nonce=1),
            bob: Account(balance=bob_balance + 1),
        }
        rejected_entry = BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=2, post_nonce=1)]
        )
        replay_entry = BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)]
        )
    else:
        raise ValueError(f"unknown cause: {cause}")

    # The trailing transaction lists the sender at its own position: index
    # 2 in the rejected block, index 1 in the replay.
    rejected_list: BlockAccessListExpectation | None = None
    replay_list: BlockAccessListExpectation | None = None
    if fork.is_eip_enabled(7928):
        rejected_list = BlockAccessListExpectation(
            account_expectations={sender: rejected_entry}
        )
        replay_list = BlockAccessListExpectation(
            account_expectations={sender: replay_entry}
        )

    blockchain_test(
        pre=pre,
        post=post,
        blocks=[
            Block(
                txs=[invalid_tx, trailing_tx],
                exception=error,
                expected_block_access_list=rejected_list,
            ),
            Block(txs=[trailing_tx], expected_block_access_list=replay_list),
        ],
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 17 forks.