Skip to content

Commit

Permalink
feat(fw): update rpc methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-tb committed May 17, 2024
1 parent 7d8c28b commit 5f6311b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/ethereum_test_tools/rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
JSON-RPC methods and helper functions for EEST consume based hive simulators.
"""

from typing import Dict, List, Literal, Union
from abc import ABC
from typing import Any, Dict, List, Literal, Optional, Union

import requests
from tenacity import retry, stop_after_attempt, wait_exponential
Expand All @@ -12,19 +13,19 @@
BlockNumberType = Union[int, Literal["latest", "earliest", "pending"]]


from abc import ABC

class BaseRPC(ABC):
"""
Represents a base RPC class for every RPC call used within EEST based hive simulators.
"""

def __init__(self, client, port):
self.client = client
self.url = f"http://{client.ip}:{port}"
def __init__(self, client_ip: str, port: int):
self.ip = client_ip
self.url = f"http://{client_ip}:{port}"

@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, max=10))
def post_request(self, method, params):
def post_request(
self, method: str, params: List[Any], extra_headers: Optional[Dict] = None
) -> Dict:
"""
Sends a JSON-RPC POST request to the client RPC server at port defined in the url.
"""
Expand All @@ -34,9 +35,10 @@ def post_request(self, method, params):
"params": params,
"id": 1,
}
headers = {
base_header = {
"Content-Type": "application/json",
}
headers = base_header if extra_headers is None else {**base_header, **extra_headers}

response = requests.post(self.url, json=payload, headers=headers)
response.raise_for_status()
Expand All @@ -47,7 +49,7 @@ def post_request(self, method, params):
error_code = None
if result is not None:
error_info = result["error"]
error_code = error_info["code"]
error_code = result["error"]["code"]
raise Exception(
f"Error calling JSON RPC {method}, code: {error_code}, " f"message: {error_info}"
)
Expand All @@ -61,11 +63,11 @@ class EthRPC(BaseRPC):
hive simulators.
"""

def __init__(self, client):
def __init__(self, client_ip):
"""
Initializes the EthRPC class with the http port 8545, which requires no authentication.
"""
super().__init__(client, port=8545)
super().__init__(client_ip, port=8545)

BlockNumberType = Union[int, Literal["latest", "earliest", "pending"]]

Expand Down
4 changes: 2 additions & 2 deletions tests_consume/test_via_rlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ def eth_rpc(client: Client) -> EthRPC:
"""
Initialize ethereum RPC client for the execution client under test.
"""
return EthRPC(client)
return EthRPC(client_ip=client.ip)


def compare_models(expected: FixtureHeader, got: FixtureHeader) -> dict:
"""
Compare two FixtureHeader model instances and return their differences.
"""
differences = {}
for (exp_name, exp_value), (got_name, got_value) in zip(expected, got):
for (exp_name, exp_value), (_, got_value) in zip(expected, got):
if exp_value != got_value:
differences[exp_name] = {
"expected ": str(exp_value),
Expand Down

0 comments on commit 5f6311b

Please sign in to comment.