From 3a0046615840f8bf8308ede5a755db9c9e10f02b Mon Sep 17 00:00:00 2001 From: Mephisto Date: Wed, 6 Nov 2024 17:18:10 +0100 Subject: [PATCH] Fixed get_health call --- src/solana/rpc/api.py | 5 ++++- src/solana/rpc/async_api.py | 5 ++++- src/solana/rpc/core.py | 7 +++++-- src/solana/rpc/providers/async_base.py | 4 ---- src/solana/rpc/providers/async_http.py | 11 ----------- src/solana/rpc/providers/base.py | 4 ---- src/solana/rpc/providers/http.py | 11 ----------- 7 files changed, 13 insertions(+), 34 deletions(-) diff --git a/src/solana/rpc/api.py b/src/solana/rpc/api.py index c6aea847..b5d06783 100644 --- a/src/solana/rpc/api.py +++ b/src/solana/rpc/api.py @@ -25,6 +25,7 @@ GetFeeForMessageResp, GetFirstAvailableBlockResp, GetGenesisHashResp, + GetHealthResp, GetIdentityResp, GetInflationGovernorResp, GetInflationRateResp, @@ -112,7 +113,9 @@ def is_connected(self) -> bool: Returns: True if the client is connected. """ - return self._provider.is_connected() + body = self._get_health_body() + response = self._provider.make_request(body, GetHealthResp) + return response.value == "ok" def get_balance(self, pubkey: Pubkey, commitment: Optional[Commitment] = None) -> GetBalanceResp: """Returns the balance of the account of provided Pubkey. diff --git a/src/solana/rpc/async_api.py b/src/solana/rpc/async_api.py index b64a059b..c176cbdb 100644 --- a/src/solana/rpc/async_api.py +++ b/src/solana/rpc/async_api.py @@ -24,6 +24,7 @@ GetFeeForMessageResp, GetFirstAvailableBlockResp, GetGenesisHashResp, + GetHealthResp, GetIdentityResp, GetInflationGovernorResp, GetInflationRateResp, @@ -120,7 +121,9 @@ async def is_connected(self) -> bool: Returns: True if the client is connected. """ - return await self._provider.is_connected() + body = self._get_health_body() + response = await self._provider.make_request(body, GetHealthResp) + return response.value == "ok" async def get_balance(self, pubkey: Pubkey, commitment: Optional[Commitment] = None) -> GetBalanceResp: """Returns the balance of the account of provided Pubkey. diff --git a/src/solana/rpc/core.py b/src/solana/rpc/core.py index c7c155a6..10063137 100644 --- a/src/solana/rpc/core.py +++ b/src/solana/rpc/core.py @@ -42,6 +42,7 @@ GetFeeForMessage, GetFirstAvailableBlock, GetGenesisHash, + GetHealth, GetIdentity, GetInflationGovernor, GetInflationRate, @@ -77,11 +78,10 @@ ) from solders.rpc.responses import GetLatestBlockhashResp, SendTransactionResp from solders.signature import Signature -from solders.transaction import VersionedTransaction +from solders.transaction import Transaction, VersionedTransaction from solders.transaction_status import UiTransactionEncoding from solana.rpc import types -from solders.transaction import Transaction from .commitment import Commitment, Confirmed, Finalized, Processed @@ -157,6 +157,9 @@ def commitment(self) -> Commitment: """The default commitment used for requests.""" return self._commitment + def _get_health_body(self) -> GetHealth: + return GetHealth() + def _get_balance_body(self, pubkey: Pubkey, commitment: Optional[Commitment]) -> GetBalance: commitment_to_use = _COMMITMENT_TO_SOLDERS[commitment or self._commitment] return GetBalance(pubkey, RpcContextConfig(commitment=commitment_to_use)) diff --git a/src/solana/rpc/providers/async_base.py b/src/solana/rpc/providers/async_base.py index fd144338..29ce1184 100644 --- a/src/solana/rpc/providers/async_base.py +++ b/src/solana/rpc/providers/async_base.py @@ -12,7 +12,3 @@ class AsyncBaseProvider: async def make_request(self, body: Body, parser: Type[T]) -> T: """Make a request ot the rpc endpoint.""" raise NotImplementedError("Providers must implement this method") - - async def is_connected(self) -> bool: - """Health check.""" - raise NotImplementedError("Providers must implement this method") diff --git a/src/solana/rpc/providers/async_http.py b/src/solana/rpc/providers/async_http.py index e86ba566..441b1993 100644 --- a/src/solana/rpc/providers/async_http.py +++ b/src/solana/rpc/providers/async_http.py @@ -120,17 +120,6 @@ async def make_batch_request(self, reqs: Tuple[Body, ...], parsers: _Tuples) -> raw = await self.make_batch_request_unparsed(reqs) return _parse_raw_batch(raw, parsers) - async def is_connected(self) -> bool: - """Health check.""" - try: - response = await self.session.get(self.health_uri) - response.raise_for_status() - except (IOError, httpx.HTTPError) as err: - self.logger.error("Health check failed with error: %s", str(err)) - return False - - return response.status_code == httpx.codes.OK - async def __aenter__(self) -> "AsyncHTTPProvider": """Use as a context manager.""" await self.session.__aenter__() diff --git a/src/solana/rpc/providers/base.py b/src/solana/rpc/providers/base.py index be58fcc1..771cc122 100644 --- a/src/solana/rpc/providers/base.py +++ b/src/solana/rpc/providers/base.py @@ -11,7 +11,3 @@ class BaseProvider: def make_request(self, body: Body, parser: Type[T]) -> T: """Make a request to the rpc endpoint.""" raise NotImplementedError("Providers must implement this method") - - def is_connected(self) -> bool: - """Health check.""" - raise NotImplementedError("Providers must implement this method") diff --git a/src/solana/rpc/providers/http.py b/src/solana/rpc/providers/http.py index f6757e97..fd2e555e 100644 --- a/src/solana/rpc/providers/http.py +++ b/src/solana/rpc/providers/http.py @@ -108,14 +108,3 @@ def make_batch_request(self, reqs: Tuple[Body, ...], parsers: _Tuples) -> Tuple[ """ raw = self.make_batch_request_unparsed(reqs) return _parse_raw_batch(raw, parsers) - - def is_connected(self) -> bool: - """Health check.""" - try: - response = httpx.get(self.health_uri) - response.raise_for_status() - except (IOError, httpx.HTTPError) as err: - self.logger.error("Health check failed with error: %s", str(err)) - return False - - return response.status_code == httpx.codes.OK