Skip to content

Commit

Permalink
feat: get_module_validators_by_node_operators
Browse files Browse the repository at this point in the history
  • Loading branch information
vgorkavenko committed Apr 24, 2024
1 parent 466cf47 commit d64178c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/providers/consensus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def get_state_block_roots(self, state_id: SlotNumber) -> list[BlockRoot]:
streamed_json = self._get_stream(
self.API_GET_STATE,
path_params=(state_id,),
force_raise=self.__raise_on_prysm_error
)
return list(streamed_json['data']['block_roots'])

Expand Down
9 changes: 9 additions & 0 deletions src/providers/keys/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from src.typings import BlockStamp
from src.utils.dataclass import list_of_dataclasses
from src.utils.cache import global_lru_cache as lru_cache
from src.web3py.extensions.lido_validators import StakingModuleId


class KeysOutdatedException(Exception):
Expand All @@ -25,6 +26,7 @@ class KeysAPIClient(HTTPProvider):
"""
PROMETHEUS_HISTOGRAM = KEYS_API_REQUESTS_DURATION

MODULE_OPERATORS_KEYS = 'v1/modules/{}/operators/keys'
USED_KEYS = 'v1/keys?used=true'
STATUS = 'v1/status'

Expand All @@ -50,6 +52,13 @@ def get_used_lido_keys(self, blockstamp: BlockStamp) -> list[dict]:
"""Docs: https://keys-api.lido.fi/api/static/index.html#/keys/KeysController_get"""
return cast(list[dict], self._get_with_blockstamp(self.USED_KEYS, blockstamp))

@lru_cache(maxsize=1)
def get_module_operators_keys(self, module_id: StakingModuleId, blockstamp: BlockStamp) -> dict:
"""
Docs: https://keys-api.lido.fi/api/static/index.html#/operators-keys/SRModulesOperatorsKeysController_getOperatorsKeys
"""
return cast(dict, self._get_with_blockstamp(self.MODULE_OPERATORS_KEYS.format(module_id), blockstamp))

def get_status(self) -> KeysApiStatus:
"""Docs: https://keys-api.lido.fi/api/static/index.html#/status/StatusController_get"""
data, _ = self._get(self.STATUS)
Expand Down
24 changes: 23 additions & 1 deletion src/web3py/extensions/lido_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,29 @@ def get_lido_validators_by_node_operators(self, blockstamp: BlockStamp) -> Valid
@lru_cache(maxsize=1)
def get_module_validators_by_node_operators(self, module_id: StakingModuleId, blockstamp: BlockStamp) -> ValidatorsByNodeOperator:
"""Get module validators by querying the KeysAPI for the module keys"""
raise NotImplementedError()
kapi = self.w3.kac.get_module_operators_keys(module_id, blockstamp)
operators = kapi['operators']
keys = {k['key']: k for k in kapi['keys']}
validators = self.w3.cc.get_validators(blockstamp)

# Make sure even empty NO will be presented in dict
no_validators: ValidatorsByNodeOperator = {
(module_id, NodeOperatorId(int(operator['index']))): [] for operator in operators
}

for validator in validators:
lido_key = keys.get(validator.validator.pubkey)
if not lido_key:
continue
global_id = (module_id, lido_key['operatorIndex'])
no_validators[global_id].append(
LidoValidator(
lido_id=LidoKey.from_response(**lido_key),
**asdict(validator),
)
)

return no_validators

@lru_cache(maxsize=1)
def get_lido_node_operators(self, blockstamp: BlockStamp) -> list[NodeOperator]:
Expand Down

0 comments on commit d64178c

Please sign in to comment.