Skip to content

Commit

Permalink
Fix validator registration
Browse files Browse the repository at this point in the history
  • Loading branch information
tsudmi committed Feb 21, 2023
1 parent 4c882a2 commit fca3963
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
12 changes: 7 additions & 5 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ async def main() -> None:
start_time = time.time()
to_block = await get_safe_block_number()

# process new network validators
await network_validators_scanner.process_new_events(to_block)

# check and register new validators
await register_validators(keystores, deposit_data)
try:
# process new network validators
await network_validators_scanner.process_new_events(to_block)
# check and register new validators
await register_validators(keystores, deposit_data)
except Exception as e:
logger.error(e)

block_processing_time = time.time() - start_time
sleep_time = max(int(NETWORK_CONFIG.SECONDS_PER_BLOCK) - int(block_processing_time), 0)
Expand Down
17 changes: 4 additions & 13 deletions src/validators/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@ def process_network_validator_event(event: EventData) -> HexStr | None:


@backoff.on_exception(backoff.expo, Exception, max_time=DEFAULT_RETRY_TIME)
async def get_latest_block_number() -> BlockNumber:
"""Fetches the latest block."""
return await execution_client.eth.block_number # type: ignore


@backoff.on_exception(backoff.expo, Exception, max_time=DEFAULT_RETRY_TIME)
async def get_latest_network_validator_public_keys(block_number: BlockNumber) -> Set[HexStr]:
async def get_latest_network_validator_public_keys() -> Set[HexStr]:
"""Fetches the latest network validator public keys."""
last_validator = get_last_network_validator()
if last_validator:
Expand All @@ -123,8 +117,7 @@ async def get_latest_network_validator_public_keys(block_number: BlockNumber) ->
from_block = VALIDATORS_REGISTRY_GENESIS_BLOCK

new_events = await validators_registry_contract.events.DepositEvent.get_logs(
from_block=from_block,
to_block=block_number,
from_block=from_block
)
new_public_keys: Set[HexStr] = set()
for event in new_events:
Expand Down Expand Up @@ -162,11 +155,9 @@ async def get_withdrawable_assets() -> Wei:


@backoff.on_exception(backoff.expo, Exception, max_time=DEFAULT_RETRY_TIME)
async def get_validators_registry_root(block_number: BlockNumber) -> Bytes32:
async def get_validators_registry_root() -> Bytes32:
"""Fetches the latest validators registry root."""
return await validators_registry_contract.functions.get_deposit_root().call(
block_identifier=block_number
)
return await validators_registry_contract.functions.get_deposit_root().call()


@backoff.on_exception(backoff.expo, Exception, max_time=DEFAULT_RETRY_TIME)
Expand Down
9 changes: 2 additions & 7 deletions src/validators/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from src.validators.execution import (
check_operator_balance,
get_available_validators,
get_latest_block_number,
get_latest_network_validator_public_keys,
get_oracles,
get_validators_registry_root,
Expand All @@ -47,7 +46,6 @@
logger = logging.getLogger(__name__)


@backoff.on_exception(backoff.expo, Exception, max_time=DEFAULT_RETRY_TIME)
async def register_validators(keystores: Keystores, deposit_data: DepositData) -> None:
"""Registers vault validators."""
vault_balance = await get_withdrawable_assets()
Expand Down Expand Up @@ -85,21 +83,18 @@ async def register_validators(keystores: Keystores, deposit_data: DepositData) -
await check_operator_balance()


@backoff.on_exception(backoff.expo, Exception, max_tries=10)
async def get_oracles_approval(
keystores: Keystores, validators: list[Validator]
) -> OraclesApproval:
"""Fetches approval from oracles."""
# get latest oracles
oracles = await get_oracles()

block_number = await get_latest_block_number()

# get latest registry root
registry_root = await get_validators_registry_root(block_number)
registry_root = await get_validators_registry_root()

# get next validator index for exit signature
latest_public_keys = await get_latest_network_validator_public_keys(block_number)
latest_public_keys = await get_latest_network_validator_public_keys()
validator_index = get_next_validator_index(list(latest_public_keys))
start_validator_index = validator_index

Expand Down
35 changes: 25 additions & 10 deletions src/validators/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from os.path import isfile, join

import aiohttp
import backoff
import milagro_bls_binding as bls
from aiohttp import ClientResponseError
from eth_typing import ChecksumAddress, HexStr
from eth_utils import add_0x_prefix
from multiproof import StandardMerkleTree
Expand All @@ -23,7 +23,13 @@
KEYSTORES_PATH,
VAULT_CONTRACT_ADDRESS,
)
from src.validators.execution import _encode_tx_validator, check_deposit_data_root
from src.validators.database import get_next_validator_index
from src.validators.execution import (
_encode_tx_validator,
check_deposit_data_root,
get_latest_network_validator_public_keys,
get_validators_registry_root,
)
from src.validators.typings import (
ApprovalRequest,
BLSPrivkey,
Expand Down Expand Up @@ -69,18 +75,27 @@ async def send_approval_requests(oracles: Oracles, request: ApprovalRequest) ->
return signatures, ipfs_hash


@backoff.on_exception(backoff.expo, Exception, max_time=60)
async def send_approval_request(
session: aiohttp.ClientSession, endpoint: str, payload: dict
) -> OracleApproval:
"""Requests approval from single oracle."""
async with session.post(url=endpoint, json=payload) as response:
response.raise_for_status()
data = await response.json()

return OracleApproval(
ipfs_hash=data['ipfs_hash'], signature=Web3.to_bytes(hexstr=data['signature'])
)
while True:
try:
async with session.post(url=endpoint, json=payload) as response:
response.raise_for_status()
data = await response.json()
return OracleApproval(
ipfs_hash=data['ipfs_hash'], signature=Web3.to_bytes(hexstr=data['signature'])
)
except ClientResponseError as e:
registry_root = await get_validators_registry_root()
if Web3.to_hex(registry_root) != payload['validators_root']:
raise ValueError('Validators registry root has changed') from e

latest_public_keys = await get_latest_network_validator_public_keys()
validator_index = get_next_validator_index(list(latest_public_keys))
if validator_index != payload['validator_index']:
raise ValueError('Validator index has changed') from e


def load_keystores() -> Keystores:
Expand Down

0 comments on commit fca3963

Please sign in to comment.