Skip to content

Commit

Permalink
Fix active keys
Browse files Browse the repository at this point in the history
  • Loading branch information
antares-sw committed Oct 13, 2023
1 parent b389400 commit b876b53
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/commands/remote_signer_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import aiohttp
import click
import milagro_bls_binding as bls
from eth_typing import BLSPrivateKey, HexAddress
from eth_typing import BLSPrivateKey, HexAddress, HexStr
from py_ecc.bls import G2ProofOfPossession
from staking_deposit.key_handling.keystore import ScryptKeystore
from sw_utils import ValidatorStatus
from sw_utils.consensus import EXITED_STATUSES
from web3 import Web3

from src.common.clients import consensus_client
from src.common.contrib import bytes_to_str
from src.common.credentials import Credential
from src.common.execution import get_oracles
Expand All @@ -28,6 +31,8 @@
from src.validators.signing.remote import RemoteSignerConfiguration
from src.validators.utils import list_keystore_files, load_keystores

EXITING_STATUSES = [ValidatorStatus.ACTIVE_EXITING] + EXITED_STATUSES


@click.option(
'--vault',
Expand Down Expand Up @@ -237,20 +242,21 @@ async def main(

# Remove outdated keystores from remote signer
if remove_existing_keys:
active_pubkey_shares = {
pk for pk_list in remote_signer_config.pubkeys_to_shares.values() for pk in pk_list
}
if remote_db_url:
database = Database(
db_url=str(remote_db_url),
)
all_db_public_keys = set(database.fetch_all_public_keys())
inactive_pubkeys = all_db_public_keys - active_pubkey_shares
active_public_keys = await _fetch_active_public_keys(all_db_public_keys)
inactive_pubkeys = all_db_public_keys - active_public_keys
database.delete_keys_by_public_key(list(inactive_pubkeys))
click.echo(
f'Removed {len(inactive_pubkeys)} keys from remote database',
)
else:
active_pubkey_shares = {
pk for pk_list in remote_signer_config.pubkeys_to_shares.values() for pk in pk_list
}
async with aiohttp.ClientSession() as session:
resp = await session.get(f'{settings.remote_signer_url}/api/v1/eth2/publicKeys')
pubkeys_remote_signer = set(await resp.json())
Expand Down Expand Up @@ -387,3 +393,23 @@ def _encrypt_private_keys(private_keys: list[int], encryptor: Encryptor) -> list
key_records.append(key_record)

return key_records


async def _fetch_active_public_keys(public_keys: list[HexStr]) -> list[HexStr]:
"""Return only active public keys."""
results: list[HexStr] = []
# pylint: disable=duplicate-code
exited_statuses = [x.value for x in EXITING_STATUSES]

for i in range(0, len(public_keys), settings.validators_fetch_chunk_size):
validators = await consensus_client.get_validators_by_ids(
public_keys[i : i + settings.validators_fetch_chunk_size]
)
for beacon_validator in validators['data']:
if beacon_validator.get('status') in exited_statuses:
continue

results.append(
Web3.to_hex(beacon_validator['validator']['pubkey']),
)
return results

0 comments on commit b876b53

Please sign in to comment.