-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consume: add and utilize eth rpc class within consume rlp.
- Loading branch information
1 parent
0dee752
commit f57da45
Showing
3 changed files
with
130 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ Test fixtures for use by clients are available for each release on the [Github r | |
- ✨ Added `--traces` support when running with Hyperledger Besu ([#511](https://github.com/ethereum/execution-spec-tests/pull/511)). | ||
- ✨ Use pytest's "short" traceback style (`--tb=short`) for failure summaries in the test report for more compact terminal output ([#542](https://github.com/ethereum/execution-spec-tests/pull/542)). | ||
- ✨ 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 and utilize an ethereum RPC client class within consume rlp ([#556](https://github.com/ethereum/execution-spec-tests/pull/556)). | ||
|
||
|
||
Check failure on line 28 in docs/CHANGELOG.md GitHub Actions / build (macos-latest, 3.11, 0.8.22, main, tox run-parallel --parallel-no-spinner)Multiple consecutive blank lines
|
||
### 🔧 EVM Tools | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
""" | ||
JSON-RPC methods and helper functions for EEST consume based hive simulators. | ||
""" | ||
|
||
import time | ||
from typing import Dict, List, Literal, Union | ||
|
||
import requests | ||
from jwt import encode | ||
from tenacity import retry, stop_after_attempt, wait_exponential | ||
|
||
from ethereum_test_tools import Address | ||
|
||
|
||
class BaseRPC: | ||
""" | ||
Represents a base RPC class for every RPC call used within EEST based hive simulators. | ||
""" | ||
|
||
def __init__(self, client): | ||
self.client = client | ||
self.url = f"http://{client.ip}:8551" | ||
self.jwt_secret = ( | ||
b"secretsecretsecretsecretsecretse" # oh wow, guess its not a secret anymore | ||
) | ||
|
||
def generate_jwt_token(self): | ||
""" | ||
Generates a JWT token based on the issued at timestamp and JWT secret. | ||
""" | ||
iat = int(time.time()) | ||
return encode({"iat": iat}, self.jwt_secret, algorithm="HS256") | ||
|
||
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, max=10)) | ||
def post_request(self, method, params): | ||
""" | ||
Sends a JSON-RPC POST request to the client RPC server at port 8551. | ||
""" | ||
payload = { | ||
"jsonrpc": "2.0", | ||
"method": method, | ||
"params": params, | ||
"id": 1, | ||
} | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.generate_jwt_token()}", | ||
} | ||
|
||
response = requests.post(self.url, json=payload, headers=headers) | ||
response.raise_for_status() | ||
result = response.json().get("result") | ||
|
||
if result is None or "error" in result: | ||
error_info = "result is None; and therefore contains no error info" | ||
error_code = None | ||
if result is not None: | ||
error_info = result["error"] | ||
error_code = error_info["code"] | ||
raise Exception( | ||
f"Error calling JSON RPC {method}, code: {error_code}, " f"message: {error_info}" | ||
) | ||
|
||
return result | ||
|
||
|
||
class EthRPC(BaseRPC): | ||
""" | ||
Represents an `eth_X` RPC class for every default ethereum RPC method used within EEST based | ||
hive simulators. | ||
""" | ||
|
||
BlockNumberType = Union[int, Literal["latest", "earliest", "pending"]] | ||
|
||
def get_block_by_number(self, block_number: BlockNumberType = "latest", full_txs: bool = True): | ||
""" | ||
`eth_getBlockByNumber`: Returns information about a block by block number. | ||
""" | ||
block = hex(block_number) if isinstance(block_number, int) else block_number | ||
return self.post_request("eth_getBlockByNumber", [block, full_txs]) | ||
|
||
def get_balance(self, address: str, block_number: BlockNumberType = "latest"): | ||
""" | ||
`eth_getBalance`: Returns the balance of the account of given address. | ||
""" | ||
block = hex(block_number) if isinstance(block_number, int) else block_number | ||
return self.post_request("eth_getBalance", [address, block]) | ||
|
||
def get_transaction_count(self, address: Address, block_number: BlockNumberType = "latest"): | ||
""" | ||
`eth_getTransactionCount`: Returns the number of transactions sent from an address. | ||
""" | ||
block = hex(block_number) if isinstance(block_number, int) else block_number | ||
return self.post_request("eth_getTransactionCount", [address, block]) | ||
|
||
def get_storage_at( | ||
self, address: str, position: str, block_number: BlockNumberType = "latest" | ||
): | ||
""" | ||
`eth_getStorageAt`: Returns the value from a storage position at a given address. | ||
""" | ||
block = hex(block_number) if isinstance(block_number, int) else block_number | ||
return self.post_request("eth_getStorageAt", [address, position, block]) | ||
|
||
def storage_at_keys( | ||
self, account: str, keys: List[str], block_number: BlockNumberType = "latest" | ||
) -> Dict: | ||
""" | ||
Helper to retrieve the storage values for the specified keys at a given address and block | ||
number. | ||
""" | ||
if isinstance(block_number, int): | ||
block_number | ||
results: Dict = {} | ||
for key in keys: | ||
storage_value = self.get_storage_at(account, key, block_number) | ||
results[key] = storage_value | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters