Skip to content

Commit

Permalink
fix(cli): update gentest to use pydantic models (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
danceratopz authored May 30, 2024
1 parent c82ce6e commit 06499f5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Test fixtures for use by clients are available for each release on the [Github r
- ✨ The `fill` command now generates HTML test reports with links to the JSON fixtures and debug information ([#537](https://github.com/ethereum/execution-spec-tests/pull/537)).
- ✨ Add an Ethereum RPC client class for use with consume commands ([#556](https://github.com/ethereum/execution-spec-tests/pull/556)).
- ✨ Add a "slow" pytest marker, in order to be able to limit the filled tests until release ([#562](https://github.com/ethereum/execution-spec-tests/pull/562)).
- ✨ Add a CLI tool that generates blockchain tests as Python from a transaction hash ([#470](https://github.com/ethereum/execution-spec-tests/pull/470), [#576](https://github.com/ethereum/execution-spec-tests/pull/576)).

### 🔧 EVM Tools

Expand Down
60 changes: 57 additions & 3 deletions src/cli/gentest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
"""
Define an entry point wrapper for test generator.
Generate a Python blockchain test from a transaction hash.
This script can be used to generate Python source for a blockchain test case
that replays a mainnet or testnet transaction from its transaction hash.
Note:
Requirements:
1. Access to an archive node for the network where the transaction
originates. A provider may be used.
2. A config file with the remote node data in JSON format
```json
{
"remote_nodes" : [
{
"name" : "mainnet_archive",
"node_url" : "https://example.archive.node.url/v1
"client_id" : "",
"secret" : ""
}
]
}
```
`client_id` and `secret` may be left empty if the node does not require
them.
3. The transaction hash of a type 0 transaction (currently only legacy
transactions are supported).
Example Usage:
1. Generate a test for a transaction with hash
```console
gentest -c config.json \
0xa41f343be7a150b740e5c939fa4d89f3a2850dbe21715df96b612fc20d1906be \
tests/paris/test_0xa41f.py
```
2. Fill the test:
```console
fill --fork=Paris tests/paris/test_0xa41f.py
```
Limitations:
1. Only legacy transaction types (type 0) are currently supported.
"""

import json
Expand Down Expand Up @@ -115,7 +164,7 @@ def _make_pre_state(
state_str += pad + "storage={\n"

if account_obj.storage is not None:
for record, value in account_obj.storage.items():
for record, value in account_obj.storage.root.items():
pad_record = common.ZeroPaddedHexNumber(record)
pad_value = common.ZeroPaddedHexNumber(value)
state_str += f'{pad} "{pad_record}" : "{pad_value}",\n'
Expand All @@ -141,7 +190,7 @@ def _make_transaction(self, test: str, tr: "RequestManager.RemoteTransaction") -
"gas_limit",
"value",
]
for field, value in asdict(tr.transaction).items():
for field, value in iter(tr.transaction):
if value is None:
continue

Expand Down Expand Up @@ -244,6 +293,7 @@ def _make_request(self, data) -> requests.Response:
error_str = "An error occurred while making remote request: "
try:
response = requests.post(self.node_url, headers=self.headers, data=json.dumps(data))
response.raise_for_status()
if response.status_code >= 200 and response.status_code < 300:
return response
else:
Expand All @@ -267,6 +317,10 @@ def eth_get_transaction_by_hash(self, transaction_hash: str) -> RemoteTransactio
response = self._make_request(data)
res = response.json().get("result", None)

assert (
res["type"] == "0x0"
), f"Transaction has type {res['type']}: Currently only type 0 transactions are supported."

return RequestManager.RemoteTransaction(
block_number=res["blockNumber"],
tr_hash=res["hash"],
Expand Down
1 change: 1 addition & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ chainid
changelog
chfast
classdict
cli
cli2
codeAddr
codecopy
Expand Down

0 comments on commit 06499f5

Please sign in to comment.