From c53bea006decb8e00fa81a97aeb2f306af1cab2c Mon Sep 17 00:00:00 2001 From: mhaines Date: Fri, 16 Jan 2026 16:35:53 -0500 Subject: [PATCH] send genesis and rpc stuff --- yocto/utils/summit_client.py | 55 +++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/yocto/utils/summit_client.py b/yocto/utils/summit_client.py index 269290f5..59d46a42 100644 --- a/yocto/utils/summit_client.py +++ b/yocto/utils/summit_client.py @@ -1,11 +1,10 @@ import logging import tomllib from pathlib import Path -from typing import Any - +from typing import Any, Dict, List import requests from dataclasses import dataclass - +from pathlib import Path logger = logging.getLogger(__name__) GenesisText = str @@ -18,18 +17,44 @@ class PublicKeys: class SummitClient: + def __init__(self, url: str): self.url = url + self._request_id = 0 + print(f"{self._request_id}") - def _get(self, path: str) -> str: - response = requests.get(f"{self.url}/{path}") + + + def _rpc_call(self, path: str, params: dict[str, Any] | list | None = None) -> Any: + payload = { + "jsonrpc": "2.0", + "method": path, + "params": params or [], + "id": self._request_id + 1 + } + self._request_id += 1 + + response = requests.post( + self.url, + json=payload, + headers={"Content-Type": "application/json"} + ) response.raise_for_status() - return response.text + + result = response.json() + + if "error" in result: + error = result["error"] + raise Exception(f"RPC Error {error.get('code')}: {error.get('message')}") + + return result.get("result") + + + + def _call(self, path: str, params: List|Dict | None = None) -> Any: + # response = requests.get(f"{self.url}/{path}") + return self._rpc_call(path, params) - def _get_json(self, path: str) -> str: - response = requests.get(f"{self.url}/{path}") - response.raise_for_status() - return response.json() def _post_text(self, path: str, body: str) -> str: response = requests.post( @@ -41,18 +66,16 @@ def _post_text(self, path: str, body: str) -> str: return response.text def health(self) -> str: - return self._get("health") + return self._call("health") def get_public_keys(self) -> PublicKeys: - keys = self._get_json("get_public_keys") + keys = self._call("getPublicKeys", []) return PublicKeys(node=keys['node'], consensus=keys['consensus']) - def send_share(self, share: str) -> str: - return self._post_text("send_share", share) - + def send_genesis(self, genesis: GenesisText) -> str: self.validate_genesis_text(genesis) - return self._post_text("send_genesis", genesis) + return self._call("sendGenesis", [genesis]) def post_genesis_filepath(self, path: Path): text = self.load_genesis_file(path)