-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from lidofinance/feat/health-checker
feat: add endpoints health checker
- Loading branch information
Showing
9 changed files
with
105 additions
and
18 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
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
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
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
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
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
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,47 @@ | ||
from typing import Any, Optional | ||
from abc import abstractmethod, ABC | ||
|
||
|
||
class InconsistentProviders(Exception): | ||
pass | ||
|
||
|
||
class NotHealthyProvider(Exception): | ||
pass | ||
|
||
|
||
class ProviderConsistencyModule(ABC): | ||
""" | ||
A class that provides HTTP provider ability to check that | ||
provided hosts are alive and chain ids are same. | ||
Methods must be implemented: | ||
def get_all_providers(self) -> [any]: | ||
def _get_chain_id_with_provider(self, int) -> int: | ||
""" | ||
def check_providers_consistency(self) -> Optional[int]: | ||
chain_id = None | ||
|
||
for provider_index in range(len(self.get_all_providers())): | ||
try: | ||
curr_chain_id = self._get_chain_id_with_provider(provider_index) | ||
except Exception as error: | ||
raise NotHealthyProvider(f'Provider [{provider_index}] does not responding.') from error | ||
|
||
if chain_id is None: | ||
chain_id = curr_chain_id | ||
elif chain_id != curr_chain_id: | ||
raise InconsistentProviders(f'Different chain ids detected for {provider_index=}. ' | ||
f'Expected {curr_chain_id=}, got {chain_id=}.') | ||
|
||
return chain_id | ||
|
||
@abstractmethod | ||
def get_all_providers(self) -> list[Any]: | ||
"""Returns list of hosts or providers.""" | ||
raise NotImplementedError("get_all_providers should be implemented") | ||
|
||
@abstractmethod | ||
def _get_chain_id_with_provider(self, provider_index: int) -> int: | ||
"""Does a health check call and returns chain_id for current host""" | ||
raise NotImplementedError("get_chain_id should be implemented") |
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,13 @@ | ||
from typing import Any | ||
|
||
from web3_multi_provider import FallbackProvider | ||
from src.web3py.extensions.consistency import ProviderConsistencyModule | ||
from web3 import Web3 | ||
|
||
|
||
class FallbackProviderModule(ProviderConsistencyModule, FallbackProvider): | ||
def get_all_providers(self) -> list[Any]: | ||
return self._providers # type: ignore[attr-defined] | ||
|
||
def _get_chain_id_with_provider(self, provider_index: int) -> int: | ||
return Web3.to_int(hexstr=self._providers[provider_index].make_request("eth_chainId", []).get('result')) # type: ignore[attr-defined] |
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