|
1 | 1 | """Tests for the gentest CLI command.""" |
2 | 2 |
|
| 3 | +import pytest |
3 | 4 | from click.testing import CliRunner |
4 | 5 |
|
5 | 6 | from cli.gentest.cli import generate |
|
8 | 9 | from ethereum_test_base_types import Account |
9 | 10 | from ethereum_test_tools import Environment, Storage, Transaction |
10 | 11 |
|
| 12 | +transactions_by_type = { |
| 13 | + 0: { |
| 14 | + "environment": Environment( |
| 15 | + fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", |
| 16 | + gas_limit=9916577, |
| 17 | + number=9974504, |
| 18 | + timestamp=1588257377, |
| 19 | + difficulty=2315196811272822, |
| 20 | + parent_ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", |
| 21 | + extra_data=b"\x00", |
| 22 | + ), |
| 23 | + "pre_state": { |
| 24 | + "0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c": Account( |
| 25 | + nonce=6038603, balance=23760714652307793035, code=b"", storage=Storage(root={}) |
| 26 | + ), |
| 27 | + "0x8a4a4d396a06cba2a7a4a73245991de40cdec289": Account( |
| 28 | + nonce=2, balance=816540000000000000, code=b"", storage=Storage(root={}) |
| 29 | + ), |
| 30 | + "0xc6d96786477f82491bfead8f00b8294688f77abc": Account( |
| 31 | + nonce=25, balance=29020266497911578313, code=b"", storage=Storage(root={}) |
| 32 | + ), |
| 33 | + }, |
| 34 | + "transaction": Transaction( |
| 35 | + ty=0, |
| 36 | + chain_id=1, |
| 37 | + nonce=2, |
| 38 | + gas_price=10000000000, |
| 39 | + gas_limit=21000, |
| 40 | + to="0xc6d96786477f82491bfead8f00b8294688f77abc", |
| 41 | + value=668250000000000000, |
| 42 | + data=b"", |
| 43 | + v=38, |
| 44 | + r=57233334052658009540326312124836763247359579695589124499839562829147086216092, |
| 45 | + s=49687643984819828983661675232336138386174947240467726918882054280625462464348, |
| 46 | + sender="0x8a4a4d396a06cba2a7a4a73245991de40cdec289", |
| 47 | + ), |
| 48 | + }, |
| 49 | + 2: { |
| 50 | + "environment": Environment( |
| 51 | + fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", |
| 52 | + gas_limit=30172625, |
| 53 | + number=21758000, |
| 54 | + timestamp=1738489319, |
| 55 | + parent_ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", |
| 56 | + extra_data=b"\x00", |
| 57 | + ), |
| 58 | + "pre_state": { |
| 59 | + "0x24d6c74d811cfde65995ed26fd08af445f8aab06": Account( |
| 60 | + nonce=1011, balance=139840767390685635650, code=b"", storage=Storage(root={}) |
| 61 | + ), |
| 62 | + "0xd5fbda4c79f38920159fe5f22df9655fde292d47": Account( |
| 63 | + nonce=553563, balance=162510989019530720334, code=b"", storage=Storage(root={}) |
| 64 | + ), |
| 65 | + "0xe2e29f9a85cfecb9cdaa83a81c7aa2792f24d93f": Account( |
| 66 | + nonce=104, balance=553317651330968100, code=b"", storage=Storage(root={}) |
| 67 | + ), |
| 68 | + }, |
| 69 | + "transaction": Transaction( |
| 70 | + ty=2, |
| 71 | + chain_id=1, |
| 72 | + nonce=553563, |
| 73 | + max_priority_fee_per_gas=1900000, |
| 74 | + max_fee_per_gas=3992652948, |
| 75 | + gas_limit=63000, |
| 76 | + to="0xe2e29f9a85cfecb9cdaa83a81c7aa2792f24d93f", |
| 77 | + value=221305417266040400, |
| 78 | + v=1, |
| 79 | + r=23565967349511399087318407428036702220029523660288023156323795583373026415631, |
| 80 | + s=9175853102116430015855393834807954374677057556696757715994220939907579927771, |
| 81 | + sender="0xd5fbda4c79f38920159fe5f22df9655fde292d47", |
| 82 | + ), |
| 83 | + }, |
| 84 | +} |
11 | 85 |
|
12 | | -def test_generate_success(tmp_path, monkeypatch): |
13 | | - """Test the generate command with a successful scenario.""" |
14 | | - ## Arrange ## |
15 | 86 |
|
| 87 | +@pytest.fixture |
| 88 | +def transaction_hash(tx_type: int) -> str: # noqa: D103 |
| 89 | + return str(transactions_by_type[tx_type]["transaction"].hash) # type: ignore |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("tx_type", list(transactions_by_type.keys())) |
| 93 | +def test_tx_type(tmp_path, monkeypatch, tx_type, transaction_hash): |
| 94 | + """Generates a test case for any transaction type.""" |
| 95 | + ## Arrange ## |
16 | 96 | # This test is run in a CI environment, where connection to a node could be |
17 | 97 | # unreliable. Therefore, we mock the RPC request to avoid any network issues. |
18 | 98 | # This is done by patching the `get_context` method of the `StateTestProvider`. |
19 | 99 | runner = CliRunner() |
20 | | - transaction_hash = "0xa41f343be7a150b740e5c939fa4d89f3a2850dbe21715df96b612fc20d1906be" |
21 | | - output_file = str(tmp_path / "gentest.py") |
| 100 | + output_file = str(tmp_path / f"gentest_type_{tx_type}.py") |
| 101 | + |
| 102 | + tx = transactions_by_type[tx_type] |
22 | 103 |
|
23 | 104 | def get_mock_context(self: StateTestProvider) -> dict: |
24 | | - return { |
25 | | - "environment": Environment( |
26 | | - fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", |
27 | | - gas_limit=9916577, |
28 | | - number=9974504, |
29 | | - timestamp=1588257377, |
30 | | - prev_randao=None, |
31 | | - difficulty=2315196811272822, |
32 | | - base_fee_per_gas=None, |
33 | | - excess_blob_gas=None, |
34 | | - target_blobs_per_block=None, |
35 | | - parent_difficulty=None, |
36 | | - parent_timestamp=None, |
37 | | - parent_base_fee_per_gas=None, |
38 | | - parent_gas_used=None, |
39 | | - parent_gas_limit=None, |
40 | | - blob_gas_used=None, |
41 | | - parent_ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", |
42 | | - parent_blob_gas_used=None, |
43 | | - parent_excess_blob_gas=None, |
44 | | - parent_beacon_block_root=None, |
45 | | - block_hashes={}, |
46 | | - ommers=[], |
47 | | - withdrawals=None, |
48 | | - extra_data=b"\x00", |
49 | | - parent_hash=None, |
50 | | - ), |
51 | | - "pre_state": { |
52 | | - "0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c": Account( |
53 | | - nonce=6038603, balance=23760714652307793035, code=b"", storage=Storage(root={}) |
54 | | - ), |
55 | | - "0x8a4a4d396a06cba2a7a4a73245991de40cdec289": Account( |
56 | | - nonce=2, balance=816540000000000000, code=b"", storage=Storage(root={}) |
57 | | - ), |
58 | | - "0xc6d96786477f82491bfead8f00b8294688f77abc": Account( |
59 | | - nonce=25, balance=29020266497911578313, code=b"", storage=Storage(root={}) |
60 | | - ), |
61 | | - }, |
62 | | - "transaction": Transaction( |
63 | | - ty=0, |
64 | | - chain_id=1, |
65 | | - nonce=2, |
66 | | - gas_price=10000000000, |
67 | | - max_priority_fee_per_gas=None, |
68 | | - max_fee_per_gas=None, |
69 | | - gas_limit=21000, |
70 | | - to="0xc6d96786477f82491bfead8f00b8294688f77abc", |
71 | | - value=668250000000000000, |
72 | | - data=b"", |
73 | | - access_list=None, |
74 | | - max_fee_per_blob_gas=None, |
75 | | - blob_versioned_hashes=None, |
76 | | - v=38, |
77 | | - r=57233334052658009540326312124836763247359579695589124499839562829147086216092, |
78 | | - s=49687643984819828983661675232336138386174947240467726918882054280625462464348, |
79 | | - sender="0x8a4a4d396a06cba2a7a4a73245991de40cdec289", |
80 | | - authorization_list=None, |
81 | | - secret_key=None, |
82 | | - error=None, |
83 | | - protected=True, |
84 | | - rlp_override=None, |
85 | | - wrapped_blob_transaction=False, |
86 | | - blobs=None, |
87 | | - blob_kzg_commitments=None, |
88 | | - blob_kzg_proofs=None, |
89 | | - ), |
90 | | - "tx_hash": transaction_hash, |
91 | | - } |
| 105 | + return tx |
92 | 106 |
|
93 | 107 | monkeypatch.setattr(StateTestProvider, "get_context", get_mock_context) |
94 | 108 |
|
95 | | - ## Genenrate ## |
| 109 | + ## Generate ## |
96 | 110 | gentest_result = runner.invoke(generate, [transaction_hash, output_file]) |
97 | 111 | assert gentest_result.exit_code == 0 |
98 | 112 |
|
|
0 commit comments