From b345f0fcd0bb0e692c1ca3432dcdfdb4dff9f544 Mon Sep 17 00:00:00 2001 From: Art Date: Wed, 22 May 2024 20:01:25 +0300 Subject: [PATCH] Remove _make_request and replace using it to EthRPC methods --- docs/CHANGELOG.md | 1 + src/cli/gentest.py | 55 ++++++------------------------ src/ethereum_test_tools/rpc/rpc.py | 22 ++++++++++++ 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d1d1b3876c..85b89932bb 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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 diff --git a/src/cli/gentest.py b/src/cli/gentest.py index 1eb0715961..03fddfe873 100644 --- a/src/cli/gentest.py +++ b/src/cli/gentest.py @@ -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 @@ -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() @@ -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( @@ -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( @@ -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" diff --git a/src/ethereum_test_tools/rpc/rpc.py b/src/ethereum_test_tools/rpc/rpc.py index 8b6393482f..11ef3ba882 100644 --- a/src/ethereum_test_tools/rpc/rpc.py +++ b/src/ethereum_test_tools/rpc/rpc.py @@ -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 @@ -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" @@ -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)