Skip to content

test_use_value_in_tx()

Documentation for tests/shanghai/eip4895_withdrawals/test_withdrawals.py::TestUseValueInTx::test_use_value_in_tx@21fb11c8.

Generate fixtures for these test cases for Prague with:

fill -v tests/shanghai/eip4895_withdrawals/test_withdrawals.py::TestUseValueInTx::test_use_value_in_tx --fork Prague

Test that the value from a withdrawal can be used in a transaction.

  1. tx_in_withdrawals_block: Test that the withdrawal value can not be used by a transaction in the same block as the withdrawal.

  2. tx_after_withdrawals_block: Test that the withdrawal value can be used by a transaction in the subsequent block.

Source code in tests/shanghai/eip4895_withdrawals/test_withdrawals.py
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
@pytest.mark.parametrize(
    "test_case",
    ["tx_in_withdrawals_block", "tx_after_withdrawals_block"],
    ids=lambda x: x,
)
class TestUseValueInTx:
    """
    Test that the value from a withdrawal can be used in a transaction.

    1. `tx_in_withdrawals_block`: Test that the withdrawal value can not be used by a transaction
        in the same block as the withdrawal.

    2. `tx_after_withdrawals_block`: Test that the withdrawal value can be used by a transaction
        in the subsequent block.
    """

    @pytest.fixture
    def sender(self, pre: Alloc) -> EOA:
        """Funded EOA used for sending transactions."""
        return pre.fund_eoa(1)

    @pytest.fixture
    def recipient(self, pre: Alloc) -> EOA:
        """Funded EOA used for sending transactions."""
        return pre.fund_eoa(0)

    @pytest.fixture
    def tx(self, sender: EOA, recipient: EOA):  # noqa: D102
        # Transaction sent from the `sender`, which has 1 wei balance at start
        return Transaction(
            gas_price=ONE_GWEI,
            gas_limit=21_000,
            to=recipient,
            sender=sender,
        )

    @pytest.fixture
    def withdrawal(self, tx: Transaction, sender: EOA):  # noqa: D102
        return Withdrawal(
            index=0,
            validator_index=0,
            address=sender,
            amount=tx.gas_limit + 1,
        )

    @pytest.fixture
    def blocks(self, tx: Transaction, withdrawal: Withdrawal, test_case):  # noqa: D102
        if test_case == "tx_in_withdrawals_block":
            return [
                Block(
                    txs=[tx.with_error(TransactionException.INSUFFICIENT_ACCOUNT_FUNDS)],
                    withdrawals=[
                        withdrawal,
                    ],
                    exception=TransactionException.INSUFFICIENT_ACCOUNT_FUNDS,
                )
            ]
        if test_case == "tx_after_withdrawals_block":
            return [
                Block(
                    txs=[],
                    withdrawals=[
                        withdrawal,
                    ],
                ),
                Block(
                    txs=[tx],
                    withdrawals=[],
                ),
            ]
        raise Exception("Invalid test case.")

    @pytest.fixture
    def post(self, sender: EOA, test_case: str) -> Dict:  # noqa: D102
        if test_case == "tx_in_withdrawals_block":
            return {}
        if test_case == "tx_after_withdrawals_block":
            return {sender: Account(balance=ONE_GWEI + 1)}
        raise Exception("Invalid test case.")

    def test_use_value_in_tx(
        self,
        pre: Alloc,
        blockchain_test: BlockchainTestFiller,
        post: dict,
        blocks: List[Block],
    ):
        """Test sending withdrawal value in a transaction."""
        blockchain_test(pre=pre, post=post, blocks=blocks)

test_use_value_in_tx(pre, blockchain_test, post, blocks)

Test sending withdrawal value in a transaction.

Source code in tests/shanghai/eip4895_withdrawals/test_withdrawals.py
118
119
120
121
122
123
124
125
126
def test_use_value_in_tx(
    self,
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    post: dict,
    blocks: List[Block],
):
    """Test sending withdrawal value in a transaction."""
    blockchain_test(pre=pre, post=post, blocks=blocks)

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) test_case
...fork_Shanghai-blockchain_test-tx_in_withdrawals_block tx_in_withdrawals_block
...fork_Shanghai-blockchain_test-tx_after_withdrawals_block tx_after_withdrawals_block
...fork_Cancun-blockchain_test-tx_in_withdrawals_block tx_in_withdrawals_block
...fork_Cancun-blockchain_test-tx_after_withdrawals_block tx_after_withdrawals_block
...fork_Prague-blockchain_test-tx_in_withdrawals_block tx_in_withdrawals_block
...fork_Prague-blockchain_test-tx_after_withdrawals_block tx_after_withdrawals_block
...fork_Osaka-blockchain_test-tx_in_withdrawals_block tx_in_withdrawals_block
...fork_Osaka-blockchain_test-tx_after_withdrawals_block tx_after_withdrawals_block