Skip to content

test_system_contract_deployment()

Documentation for tests/prague/eip2935_historical_block_hashes_from_state/test_contract_deployment.py::test_system_contract_deployment@49a16fac.

Generate fixtures for these test cases for Prague with:

fill -v tests/prague/eip2935_historical_block_hashes_from_state/test_contract_deployment.py::test_system_contract_deployment --fork Prague

Verify deployment of the block hashes system contract.

Source code in tests/prague/eip2935_historical_block_hashes_from_state/test_contract_deployment.py
28
29
30
31
32
33
34
35
36
37
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
@generate_system_contract_deploy_test(
    fork=Prague,
    tx_json_path=Path(realpath(__file__)).parent / "contract_deploy_tx.json",
    expected_deploy_address=Address(Spec.HISTORY_STORAGE_ADDRESS),
)
def test_system_contract_deployment(
    *,
    pre: Alloc,
    post: Alloc,
    test_type: DeploymentTestType,
    **kwargs,
):
    """Verify deployment of the block hashes system contract."""
    # Deploy a contract that calls the history contract and verifies the block hashes.
    yield Block()  # Empty block just to have more history in the contract.

    # We are going to query blocks even before contract deployment.
    code = (
        sum(
            Op.MSTORE(0, block_number)
            + Op.POP(
                Op.CALL(
                    address=Spec.HISTORY_STORAGE_ADDRESS,
                    args_offset=0,
                    args_size=32,
                    ret_offset=0,
                    ret_size=32,
                ),
            )
            + Op.SSTORE(block_number, Op.ISZERO(Op.ISZERO(Op.MLOAD(0))))
            for block_number in range(1, 4)
        )
        + Op.STOP
    )
    deployed_contract = pre.deploy_contract(code)

    tx = Transaction(
        to=deployed_contract,
        gas_limit=10_000_000,
        sender=pre.fund_eoa(),
    )

    yield Block(txs=[tx])

    storage: Dict
    if test_type == DeploymentTestType.DEPLOY_BEFORE_FORK:
        # Fork happens at block 2, and the contract is already there, so from block number 1 and
        # after, the block hashes should be there.
        storage = {
            1: 1,  # Block prior to the fork, it's the first hash saved.
            2: 1,  # Fork block, hash should be there.
            3: 1,  # Empty block added at the start of this function, hash should be there.
        }
    elif test_type == DeploymentTestType.DEPLOY_AFTER_FORK:
        # The contract should have the block hashes after contract deployment.
        storage = {
            1: 0,  # Fork block, but contract is not there yet.
            2: 1,  # Deployment block, this is the first hash that gets added because it's added on
            # the next block.
            3: 1,  # Empty block added at the start of this function, hash should be there.
        }

    post[deployed_contract] = Account(
        balance=0,
        storage=storage,
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) test_type has_balance
...fork_CancunToPragueAtTime15k-blockchain_test-deploy_before_fork-nonzero_balance DeploymentTestType.DEPLOY_BEFORE_FORK ContractAddressHasBalance.NONZERO_BALANCE
...fork_CancunToPragueAtTime15k-blockchain_test-deploy_before_fork-zero_balance DeploymentTestType.DEPLOY_BEFORE_FORK ContractAddressHasBalance.ZERO_BALANCE
...fork_CancunToPragueAtTime15k-blockchain_test-deploy_after_fork-nonzero_balance DeploymentTestType.DEPLOY_AFTER_FORK ContractAddressHasBalance.NONZERO_BALANCE
...fork_CancunToPragueAtTime15k-blockchain_test-deploy_after_fork-zero_balance DeploymentTestType.DEPLOY_AFTER_FORK ContractAddressHasBalance.ZERO_BALANCE