Skip to content

test_tx_invalid_first_in_block()

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

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
@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.