|
9 | 9 | from ethereum_test_exceptions import TransactionException |
10 | 10 | from ethereum_test_fixtures import BlockchainFixture, FixtureFormat, StateFixture |
11 | 11 | from ethereum_test_forks import Fork, get_deployed_forks |
| 12 | +from ethereum_test_tools import Block |
12 | 13 | from ethereum_test_types import Alloc, Environment, Storage, Transaction, TransactionReceipt |
13 | 14 |
|
| 15 | +from ..blockchain import BlockchainEngineFixture, BlockchainTest |
14 | 16 | from ..helpers import ( |
15 | 17 | TransactionExceptionMismatchError, |
16 | 18 | TransactionReceiptMismatchError, |
@@ -349,3 +351,99 @@ def test_transaction_expectation( |
349 | 351 | else: |
350 | 352 | with pytest.raises(exception_type) as _: |
351 | 353 | state_test.generate(request=None, t8n=t8n, fork=fork, fixture_format=fixture_format) |
| 354 | + |
| 355 | + |
| 356 | +@pytest.mark.parametrize( |
| 357 | + "intermediate_state,expected_exception", |
| 358 | + [ |
| 359 | + pytest.param( |
| 360 | + { |
| 361 | + TestAddress: Account(nonce=1), |
| 362 | + Address(0x01): Account(balance=1), |
| 363 | + }, |
| 364 | + None, |
| 365 | + id="NoException", |
| 366 | + ), |
| 367 | + pytest.param( |
| 368 | + { |
| 369 | + TestAddress: Account(nonce=2), |
| 370 | + Address(0x01): Account(balance=1), |
| 371 | + }, |
| 372 | + Account.NonceMismatchError, |
| 373 | + id="NonceMismatchError", |
| 374 | + ), |
| 375 | + pytest.param( |
| 376 | + { |
| 377 | + TestAddress: Account(nonce=1), |
| 378 | + Address(0x01): Account(balance=2), |
| 379 | + }, |
| 380 | + Account.BalanceMismatchError, |
| 381 | + id="BalanceMismatchError", |
| 382 | + ), |
| 383 | + ], |
| 384 | +) |
| 385 | +@pytest.mark.parametrize( |
| 386 | + "fixture_format", |
| 387 | + [ |
| 388 | + BlockchainFixture, |
| 389 | + BlockchainEngineFixture, |
| 390 | + ], |
| 391 | +) |
| 392 | +def test_block_intermediate_state( |
| 393 | + pre, t8n, fork, fixture_format: FixtureFormat, intermediate_state, expected_exception |
| 394 | +): |
| 395 | + """Validate the state when building blockchain.""" |
| 396 | + env = Environment() |
| 397 | + |
| 398 | + to = Address(0x01) |
| 399 | + tx = Transaction(gas_limit=100_000, to=to, value=1, nonce=0, secret_key=TestPrivateKey) |
| 400 | + tx_2 = Transaction(gas_limit=100_000, to=to, value=1, nonce=1, secret_key=TestPrivateKey) |
| 401 | + |
| 402 | + block_1 = Block( |
| 403 | + txs=[tx], |
| 404 | + expected_post_state={ |
| 405 | + TestAddress: Account(nonce=1), |
| 406 | + to: Account(balance=1), |
| 407 | + }, |
| 408 | + ) |
| 409 | + |
| 410 | + block_2 = Block(txs=[], expected_post_state=intermediate_state) |
| 411 | + |
| 412 | + block_3 = Block( |
| 413 | + txs=[tx_2], |
| 414 | + expected_post_state={ |
| 415 | + TestAddress: Account(nonce=2), |
| 416 | + to: Account(balance=2), |
| 417 | + }, |
| 418 | + ) |
| 419 | + |
| 420 | + if expected_exception: |
| 421 | + with pytest.raises(expected_exception) as _: |
| 422 | + BlockchainTest( |
| 423 | + genesis_environment=env, |
| 424 | + fork=fork, |
| 425 | + t8n=t8n, |
| 426 | + pre=pre, |
| 427 | + post=block_3.expected_post_state, |
| 428 | + blocks=[block_1, block_2, block_3], |
| 429 | + ).generate( |
| 430 | + request=None, # type: ignore |
| 431 | + t8n=t8n, |
| 432 | + fork=fork, |
| 433 | + fixture_format=fixture_format, |
| 434 | + ) |
| 435 | + return |
| 436 | + else: |
| 437 | + BlockchainTest( |
| 438 | + genesis_environment=env, |
| 439 | + fork=fork, |
| 440 | + t8n=t8n, |
| 441 | + pre=pre, |
| 442 | + post=block_3.expected_post_state, |
| 443 | + blocks=[block_1, block_2, block_3], |
| 444 | + ).generate( |
| 445 | + request=None, # type: ignore |
| 446 | + t8n=t8n, |
| 447 | + fork=fork, |
| 448 | + fixture_format=fixture_format, |
| 449 | + ) |
0 commit comments