Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fw): Remove _make_request and replace using it to EthRPC methods #568

Merged
merged 11 commits into from
Jun 14, 2024
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Test fixtures for use by clients are available for each release on the [Github r
- ✨ Add more Transaction and Block exceptions from existing ethereum/tests repo ([#572](https://github.com/ethereum/execution-spec-tests/pull/572)).
- ✨ Add "description" and "url" fields containing test case documentation and a source code permalink to fixtures during `fill` and use them in `consume`-generated Hive test reports ([#579](https://github.com/ethereum/execution-spec-tests/pull/579)).
- ✨ Add git workflow evmone coverage script for any new lines mentioned in converted_ethereum_tests.txt ([#503](https://github.com/ethereum/execution-spec-tests/pull/503)).
- 🔀 Remove `_make_request` from `RequestManager` in `cli.gentest.py`. Replace using `_make_request` to next `EthRPC` methods: `get_block_by_number`, `get_transaction_by_hash`, `debug_trace_call`. Add 2 more new methods to `EthRPC`: `get_transaction_by_hash`, `debug_trace_call` ([#568](https://github.com/ethereum/execution-spec-tests/pull/568)).
danceratopz marked this conversation as resolved.
Show resolved Hide resolved


### 🔧 EVM Tools

Expand Down
70 changes: 21 additions & 49 deletions src/cli/gentest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@

import json
import os
import urllib
from dataclasses import asdict, dataclass
from sys import stderr
from typing import Dict, List, TextIO

import click
import requests

from ethereum_test_tools import Account, Address, Transaction, common
from ethereum_test_tools.rpc.rpc import BlockNumberType, EthRPC


@click.command()
Expand Down Expand Up @@ -96,7 +97,7 @@ def make_test(transaction_hash: str, output_file: TextIO, config_file: TextIO):
state = request.debug_trace_call(tr)

print("Perform eth_get_block_by_number", file=stderr)
block = request.eth_get_block_by_number(tr.block_number)
block = request.eth_get_block_by_number(int(tr.block_number))

print("Generate py test", file=stderr)
constructor = TestConstructor(PYTEST_TEMPLATE)
Expand Down Expand Up @@ -278,43 +279,28 @@ class RemoteBlock:
node_url: str
headers: dict[str, str]

@staticmethod
def _get_ip_from_url(node_url):
return urllib.parse.urlsplit(node_url).netloc.split(":")[0]
danceratopz marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, node_config: Config.RemoteNode):
"""
Initialize the RequestManager with specific client config.
"""
self.node_url = node_config.node_url
client_ip = self._get_ip_from_url(node_config.node_url)
self.rpc = EthRPC(client_ip)
self.headers = {
"CF-Access-Client-Id": node_config.client_id,
"CF-Access-Client-Secret": node_config.secret,
"Content-Type": "application/json",
}

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:
print(error_str + response.text, file=stderr)
raise requests.exceptions.HTTPError
except requests.exceptions.RequestException as e:
print(error_str, e, file=stderr)
raise e

def eth_get_transaction_by_hash(self, transaction_hash: str) -> RemoteTransaction:
"""
Get transaction data.
"""
data = {
"jsonrpc": "2.0",
"method": "eth_getTransactionByHash",
"params": [f"{transaction_hash}"],
"id": 1,
}

response = self._make_request(data)
response = self.rpc.get_transaction_by_hash(transaction_hash)
res = response.json().get("result", None)

assert (
Expand All @@ -340,17 +326,11 @@ def eth_get_transaction_by_hash(self, transaction_hash: str) -> RemoteTransactio
),
)

def eth_get_block_by_number(self, block_number: str) -> RemoteBlock:
def eth_get_block_by_number(self, block_number: BlockNumberType) -> RemoteBlock:
"""
Get block by number
"""
data = {
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": [f"{block_number}", False],
"id": 1,
}
response = self._make_request(data)
response = self.rpc.get_block_by_number(block_number)
res = response.json().get("result", None)

return RequestManager.RemoteBlock(
Expand All @@ -363,24 +343,16 @@ def eth_get_block_by_number(self, block_number: str) -> RemoteBlock:

def debug_trace_call(self, tr: RemoteTransaction) -> Dict[Address, Account]:
"""
Get pre state required for transaction
Get pre-state required for transaction
"""
data = {
"jsonrpc": "2.0",
"method": "debug_traceCall",
"params": [
{
"from": f"{str(tr.transaction.sender)}",
"to": f"{str(tr.transaction.to)}",
"data": f"{str(tr.transaction.data)}",
},
f"{tr.block_number}",
{"tracer": "prestateTracer"},
],
"id": 1,
}

response = self._make_request(data).json()
response = self.rpc.debug_trace_call(
{
"from": f"{str(tr.transaction.sender)}",
"to": f"{str(tr.transaction.to)}",
"data": f"{str(tr.transaction.data)}",
},
tr.block_number,
)
danceratopz marked this conversation as resolved.
Show resolved Hide resolved
if "error" in response:
raise Exception(response["error"]["message"])
assert "result" in response, "No result in response on debug_traceCall"
Expand Down
17 changes: 17 additions & 0 deletions src/ethereum_test_tools/rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def get_transaction_count(self, address: Address, block_number: BlockNumberType
block = hex(block_number) if isinstance(block_number, int) else block_number
return self.post_request("eth_getTransactionCount", [address, block])

def get_transaction_by_hash(self, transaction_hash: str):
danceratopz marked this conversation as resolved.
Show resolved Hide resolved
"""
`eth_getTransactionByHash`: Returns transaction details.
"""
return self.post_request("eth_getTransactionByHash", [f"{transaction_hash}"])

def get_storage_at(
self, address: str, position: str, block_number: BlockNumberType = "latest"
):
Expand All @@ -113,3 +119,14 @@ def storage_at_keys(
storage_value = self.get_storage_at(account, key, block_number)
results[key] = storage_value
return results

def debug_trace_call(self, tr: dict[str, str], block_number: str):
"""
`debug_traceCall`: Returns pre state required for transaction
"""
params = [
tr,
block_number,
{"tracer": "prestateTracer"},
]
return self.post_request("debug_traceCall", params)
danceratopz marked this conversation as resolved.
Show resolved Hide resolved