Skip to content

Commit

Permalink
Remove _make_request and replace using it to EthRPC methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Art authored and Art committed May 22, 2024
1 parent cb60a37 commit b345f0f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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)).
- 🔀 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)).

### 🔧 EVM Tools

Expand Down
55 changes: 11 additions & 44 deletions src/cli/gentest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import os
import urllib
from dataclasses import asdict, dataclass
from sys import stderr
from typing import Dict, List, TextIO
Expand All @@ -12,6 +13,7 @@
import requests

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


@click.command()
Expand Down Expand Up @@ -229,42 +231,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]

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))
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)

return RequestManager.RemoteTransaction(
Expand All @@ -290,13 +278,7 @@ def eth_get_block_by_number(self, block_number: str) -> 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 @@ -311,22 +293,7 @@ def debug_trace_call(self, tr: RemoteTransaction) -> Dict[Address, Account]:
"""
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(tr)
if "error" in response:
raise Exception(response["error"]["message"])
assert "result" in response, "No result in response on debug_traceCall"
Expand Down
22 changes: 22 additions & 0 deletions src/ethereum_test_tools/rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Dict, List, Literal, Optional, Union

import requests
from cli.gentest import RequestManager
from tenacity import retry, stop_after_attempt, wait_exponential

from ethereum_test_tools import Address
Expand Down Expand Up @@ -91,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):
"""
`eth_getTransactionByHash`: Returns transation 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 +120,18 @@ 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: RequestManager.RemoteTransaction):
"""
`debug_traceCall`: Returns pre state required for transaction
"""
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"},
]
return self.post_request("debug_traceCall", params)

0 comments on commit b345f0f

Please sign in to comment.