Skip to content

test_block_hashes_history_at_transition()

Documentation for tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_block_hashes_history_at_transition@83970623.

Generate fixtures for these test cases for Prague with:

fill -v tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_block_hashes_history_at_transition --fork Prague

Tests that block hashes are stored correctly at the system contract address after the fork transition. Block hashes are stored incrementally at the transition until the HISTORY_SERVE_WINDOW ring buffer is full. Afterwards the oldest block hash is replaced by the new one.

Note: The block hashes before the fork are no longer stored in the contract at the moment of the transition.

Source code in tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py
 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
@pytest.mark.parametrize(
    "blocks_before_fork, blocks_after_fork",
    [
        [1, 2],
        [Spec.BLOCKHASH_OLD_WINDOW + 1, 10],
        [1, Spec.BLOCKHASH_OLD_WINDOW + 1],
    ],
)
@pytest.mark.valid_at_transition_to("Prague")
def test_block_hashes_history_at_transition(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    blocks_before_fork: int,
    blocks_after_fork: int,
):
    """
    Tests that block hashes are stored correctly at the system contract address after the fork
    transition. Block hashes are stored incrementally at the transition until the
    `HISTORY_SERVE_WINDOW` ring buffer is full. Afterwards the oldest block hash is replaced by the
    new one.

    Note: The block hashes before the fork are no longer stored in the contract at the moment of
    the transition.
    """
    blocks: List[Block] = []
    assert blocks_before_fork >= 1 and blocks_before_fork < Spec.FORK_TIMESTAMP

    sender = pre.fund_eoa(10_000_000_000)
    post: Dict[Address, Account] = {}
    current_block_number = 1
    fork_block_number = current_block_number + blocks_before_fork

    for i in range(blocks_before_fork):
        txs: List[Transaction] = []
        if i == blocks_before_fork - 1:
            # On the last block before the fork, `BLOCKHASH` must return values for the last 256
            # blocks but not for the blocks before that.
            # And `HISTORY_STORAGE_ADDRESS` should be empty.
            code = Bytecode()
            storage = Storage()

            # Check the last block before blockhash the window
            code += generate_block_check_code(
                check_block_number=current_block_number - Spec.BLOCKHASH_OLD_WINDOW - 1,
                current_block_number=current_block_number,
                fork_block_number=fork_block_number,
                storage=storage,
            )

            # Check the first block inside blockhash the window
            code += generate_block_check_code(
                check_block_number=(
                    current_block_number - Spec.BLOCKHASH_OLD_WINDOW
                    if current_block_number > Spec.BLOCKHASH_OLD_WINDOW
                    else 0  # Entire chain is inside the window, check genesis
                ),
                current_block_number=current_block_number,
                fork_block_number=fork_block_number,
                storage=storage,
            )

            check_blocks_before_fork_address = pre.deploy_contract(code)
            txs.append(
                Transaction(
                    to=check_blocks_before_fork_address,
                    gas_limit=10_000_000,
                    sender=sender,
                )
            )
            post[check_blocks_before_fork_address] = Account(storage=storage)
        blocks.append(Block(timestamp=current_block_number, txs=txs))
        current_block_number += 1

    # Add blocks after the fork transition to gradually fill up the `HISTORY_SERVE_WINDOW`
    for i in range(blocks_after_fork):
        txs = []
        # On these blocks, `BLOCKHASH` will still return values for the last 256 blocks, and
        # `HISTORY_STORAGE_ADDRESS` should now serve values for the previous blocks in the new
        # fork.
        code = Bytecode()
        storage = Storage()

        # Check that each block can return previous blockhashes if `BLOCKHASH_OLD_WINDOW` and or
        # `HISTORY_SERVE_WINDOW`.
        for j in range(current_block_number):
            code += generate_block_check_code(
                check_block_number=j,
                current_block_number=current_block_number,
                fork_block_number=fork_block_number,
                storage=storage,
            )

        check_blocks_after_fork_address = pre.deploy_contract(code)
        txs.append(
            Transaction(
                to=check_blocks_after_fork_address,
                gas_limit=10_000_000,
                sender=sender,
            )
        )
        post[check_blocks_after_fork_address] = Account(storage=storage)

        blocks.append(Block(timestamp=Spec.FORK_TIMESTAMP + i, txs=txs))
        current_block_number += 1

    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) blocks_before_fork blocks_after_fork
...fork_CancunToPragueAtTime15k-blockchain_test-blocks_before_fork_1-blocks_after_fork_2 1 2
...fork_CancunToPragueAtTime15k-blockchain_test-blocks_before_fork_257-blocks_after_fork_10 257 10
...fork_CancunToPragueAtTime15k-blockchain_test-blocks_before_fork_1-blocks_after_fork_257 1 257