|
| 1 | +""" |
| 2 | +Tests for the fixture `post` (expect) section. |
| 3 | +""" |
| 4 | + |
| 5 | +from collections import namedtuple |
| 6 | +from typing import Any, Mapping, Optional |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from ethereum_test_forks import Fork, Shanghai |
| 11 | +from evm_transition_tool import GethTransitionTool |
| 12 | + |
| 13 | +from ..common import Account, Environment, Transaction |
| 14 | +from ..common.types import Storage |
| 15 | +from ..filling import fill_test |
| 16 | +from ..spec import BaseTestConfig, StateTest |
| 17 | + |
| 18 | +test_fork = Shanghai |
| 19 | +sender_address = "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" |
| 20 | +test_address = "0x1000000000000000000000000000000000000000" |
| 21 | +test_transaction = Transaction( |
| 22 | + ty=0x0, |
| 23 | + chain_id=0x0, |
| 24 | + nonce=0, |
| 25 | + to=test_address, |
| 26 | + gas_limit=100000000, |
| 27 | + gas_price=10, |
| 28 | + protected=False, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def run_test( |
| 33 | + pre: Mapping[Any, Any], post: Mapping[Any, Any], tx: Transaction, fork: Fork, exception: Any |
| 34 | +) -> Optional[pytest.ExceptionInfo]: |
| 35 | + """ |
| 36 | + Perform the test execution and post state verification given pre and post |
| 37 | + """ |
| 38 | + state_test = StateTest( |
| 39 | + env=Environment(), |
| 40 | + pre=pre, |
| 41 | + post=post, |
| 42 | + txs=[tx], |
| 43 | + tag="post_storage_value_mismatch", |
| 44 | + base_test_config=BaseTestConfig(enable_hive=False), |
| 45 | + ) |
| 46 | + |
| 47 | + t8n = GethTransitionTool() |
| 48 | + |
| 49 | + e_info: pytest.ExceptionInfo |
| 50 | + if exception is not None: |
| 51 | + with pytest.raises(exception) as e_info: |
| 52 | + fixture = { |
| 53 | + f"000/my_chain_id_test/{fork}": fill_test( |
| 54 | + t8n=t8n, |
| 55 | + test_spec=state_test, |
| 56 | + fork=fork, |
| 57 | + spec=None, |
| 58 | + ), |
| 59 | + } |
| 60 | + return e_info |
| 61 | + else: |
| 62 | + fixture = { |
| 63 | + f"000/my_chain_id_test/{fork}": fill_test( |
| 64 | + t8n=t8n, |
| 65 | + test_spec=state_test, |
| 66 | + fork=fork, |
| 67 | + spec=None, |
| 68 | + ), |
| 69 | + } |
| 70 | + return None |
| 71 | + |
| 72 | + |
| 73 | +def test_post_account_mismatch_nonce(): |
| 74 | + """ |
| 75 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 76 | + """ |
| 77 | + pre = { |
| 78 | + sender_address: Account(balance=1000000000000000000000), |
| 79 | + test_address: Account(nonce=1), |
| 80 | + } |
| 81 | + |
| 82 | + post = { |
| 83 | + test_address: Account(nonce=2), |
| 84 | + } |
| 85 | + |
| 86 | + e_info = run_test(pre, post, test_transaction, test_fork, Account.NonceMismatch) |
| 87 | + assert e_info.value.want == 2 |
| 88 | + assert e_info.value.got == 1 |
| 89 | + assert e_info.value.address == test_address |
| 90 | + assert "unexpected nonce for account" in str(e_info.value) |
| 91 | + |
| 92 | + |
| 93 | +def test_post_account_mismatch_nonce_a(): |
| 94 | + """ |
| 95 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 96 | + """ |
| 97 | + pre = { |
| 98 | + sender_address: Account(balance=1000000000000000000000), |
| 99 | + test_address: Account(nonce=1), |
| 100 | + } |
| 101 | + |
| 102 | + post = { |
| 103 | + test_address: Account(), |
| 104 | + } |
| 105 | + |
| 106 | + run_test(pre, post, test_transaction, test_fork, None) |
| 107 | + |
| 108 | + |
| 109 | +def test_post_account_mismatch_nonce_b(): |
| 110 | + """ |
| 111 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 112 | + """ |
| 113 | + pre = { |
| 114 | + sender_address: Account(balance=1000000000000000000000), |
| 115 | + test_address: Account(nonce=1), |
| 116 | + } |
| 117 | + |
| 118 | + post = { |
| 119 | + test_address: Account(nonce=0), |
| 120 | + } |
| 121 | + |
| 122 | + e_info = run_test(pre, post, test_transaction, test_fork, Account.NonceMismatch) |
| 123 | + assert e_info.value.want == 0 |
| 124 | + assert e_info.value.got == 1 |
| 125 | + assert e_info.value.address == test_address |
| 126 | + assert "unexpected nonce for account" in str(e_info.value) |
| 127 | + |
| 128 | + |
| 129 | +def test_post_account_mismatch_code(): |
| 130 | + """ |
| 131 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 132 | + """ |
| 133 | + pre = { |
| 134 | + sender_address: Account(balance=1000000000000000000000), |
| 135 | + test_address: Account(code="0x02"), |
| 136 | + } |
| 137 | + |
| 138 | + post = { |
| 139 | + test_address: Account(code="0x01"), |
| 140 | + } |
| 141 | + |
| 142 | + e_info = run_test(pre, post, test_transaction, test_fork, Account.CodeMismatch) |
| 143 | + assert e_info.value.want == "0x01" |
| 144 | + assert e_info.value.got == "0x02" |
| 145 | + assert e_info.value.address == test_address |
| 146 | + assert "unexpected code for account" in str(e_info.value) |
| 147 | + |
| 148 | + |
| 149 | +def test_post_account_mismatch_code_a(): |
| 150 | + """ |
| 151 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 152 | + """ |
| 153 | + pre = { |
| 154 | + sender_address: Account(balance=1000000000000000000000), |
| 155 | + test_address: Account(code="0x02"), |
| 156 | + } |
| 157 | + |
| 158 | + post = { |
| 159 | + test_address: Account(), |
| 160 | + } |
| 161 | + |
| 162 | + run_test(pre, post, test_transaction, test_fork, None) |
| 163 | + |
| 164 | + |
| 165 | +def test_post_account_mismatch_code_b(): |
| 166 | + """ |
| 167 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 168 | + """ |
| 169 | + pre = { |
| 170 | + sender_address: Account(balance=1000000000000000000000), |
| 171 | + test_address: Account(code="0x02"), |
| 172 | + } |
| 173 | + |
| 174 | + post = { |
| 175 | + test_address: Account(code=""), |
| 176 | + } |
| 177 | + |
| 178 | + e_info = run_test(pre, post, test_transaction, test_fork, Account.CodeMismatch) |
| 179 | + assert e_info.value.want == "0x" |
| 180 | + assert e_info.value.got == "0x02" |
| 181 | + assert e_info.value.address == test_address |
| 182 | + assert "unexpected code for account" in str(e_info.value) |
| 183 | + |
| 184 | + |
| 185 | +def test_post_account_mismatch_balance(): |
| 186 | + """ |
| 187 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 188 | + """ |
| 189 | + pre = { |
| 190 | + sender_address: Account(balance=1000000000000000000000), |
| 191 | + test_address: Account(balance=1), |
| 192 | + } |
| 193 | + |
| 194 | + post = { |
| 195 | + test_address: Account(balance=2), |
| 196 | + } |
| 197 | + |
| 198 | + test_transaction.value = 0 |
| 199 | + e_info = run_test(pre, post, test_transaction, test_fork, Account.BalanceMismatch) |
| 200 | + assert e_info.value.want == 2 |
| 201 | + assert e_info.value.got == 1 |
| 202 | + assert e_info.value.address == test_address |
| 203 | + assert "unexpected balance for account" in str(e_info.value) |
| 204 | + |
| 205 | + |
| 206 | +def test_post_account_mismatch_balance_a(): |
| 207 | + """ |
| 208 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 209 | + """ |
| 210 | + pre = { |
| 211 | + sender_address: Account(balance=1000000000000000000000), |
| 212 | + test_address: Account(balance=1), |
| 213 | + } |
| 214 | + |
| 215 | + post = { |
| 216 | + test_address: Account(), |
| 217 | + } |
| 218 | + |
| 219 | + test_transaction.value = 0 |
| 220 | + run_test(pre, post, test_transaction, test_fork, None) |
| 221 | + |
| 222 | + |
| 223 | +def test_post_account_mismatch_balance_b(): |
| 224 | + """ |
| 225 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 226 | + """ |
| 227 | + pre = { |
| 228 | + sender_address: Account(balance=1000000000000000000000), |
| 229 | + test_address: Account(balance=1), |
| 230 | + } |
| 231 | + |
| 232 | + post = { |
| 233 | + test_address: Account(balance=0), |
| 234 | + } |
| 235 | + |
| 236 | + e_info = run_test(pre, post, test_transaction, test_fork, Account.BalanceMismatch) |
| 237 | + assert e_info.value.want == 0 |
| 238 | + assert e_info.value.got == 1 |
| 239 | + assert e_info.value.address == test_address |
| 240 | + assert "unexpected balance for account" in str(e_info.value) |
| 241 | + |
| 242 | + |
| 243 | +def test_post_account_mismatch_account(): |
| 244 | + """ |
| 245 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 246 | + """ |
| 247 | + pre = { |
| 248 | + sender_address: Account(balance=1000000000000000000000), |
| 249 | + test_address: Account(balance=1), |
| 250 | + } |
| 251 | + |
| 252 | + post = { |
| 253 | + test_address: Account(), |
| 254 | + } |
| 255 | + |
| 256 | + run_test(pre, post, test_transaction, test_fork, None) |
| 257 | + |
| 258 | + |
| 259 | +def test_post_account_mismatch_account_a(): |
| 260 | + """ |
| 261 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 262 | + """ |
| 263 | + pre = { |
| 264 | + sender_address: Account(balance=1000000000000000000000), |
| 265 | + test_address: Account(balance=1), |
| 266 | + } |
| 267 | + |
| 268 | + post = { |
| 269 | + 0x1000000000000000000000000000000000000001: Account(), |
| 270 | + } |
| 271 | + |
| 272 | + e_info = run_test(pre, post, test_transaction, test_fork, Exception) |
| 273 | + assert "expected account not found" in str(e_info.value) |
| 274 | + |
| 275 | + |
| 276 | +def test_post_account_mismatch_account_b(): |
| 277 | + """ |
| 278 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 279 | + """ |
| 280 | + pre = { |
| 281 | + sender_address: Account(balance=1000000000000000000000), |
| 282 | + test_address: Account(balance=1), |
| 283 | + } |
| 284 | + |
| 285 | + post = {} |
| 286 | + |
| 287 | + run_test(pre, post, test_transaction, test_fork, None) |
| 288 | + |
| 289 | + |
| 290 | +def test_post_account_mismatch_account_c(): |
| 291 | + """ |
| 292 | + Test `ethereum_test.filler.fill_fixtures` with `StateTest` and post state verification. |
| 293 | + """ |
| 294 | + pre = { |
| 295 | + sender_address: Account(balance=1000000000000000000000), |
| 296 | + test_address: Account(balance=1), |
| 297 | + } |
| 298 | + |
| 299 | + post = {test_address: Account.NONEXISTENT} |
| 300 | + |
| 301 | + e_info = run_test(pre, post, test_transaction, test_fork, Exception) |
| 302 | + assert "found unexpected account" in str(e_info.value) |
0 commit comments