Skip to content

Commit

Permalink
Add validator registration deadline (#156)
Browse files Browse the repository at this point in the history
* Add validator registration deadline

* Use signature_validity_period for validators deadline

* Review fix
  • Loading branch information
cyc60 authored Sep 14, 2023
1 parent 92015b2 commit 3c049bc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/common/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ async def get_oracles() -> Oracles:
if exit_signature_recover_threshold > validators_threshold:
raise ValueError('Invalid exit signature threshold')

exit_signature_deadline = config['exit_signature_deadline']
signature_validity_period = config['signature_validity_period']

if exit_signature_deadline < 0:
raise ValueError('Invalid exit signature deadline')
if signature_validity_period < 0:
raise ValueError('Invalid signature validity period')

if len(public_keys) != len(set(public_keys)):
raise ValueError('Duplicate public keys in oracles config')
Expand All @@ -87,7 +87,7 @@ async def get_oracles() -> Oracles:
rewards_threshold=rewards_threshold,
validators_threshold=validators_threshold,
exit_signature_recover_threshold=exit_signature_recover_threshold,
exit_signature_deadline=exit_signature_deadline,
signature_validity_period=signature_validity_period,
public_keys=public_keys,
endpoints=endpoints,
validators_approval_batch_limit=validators_approval_batch_limit,
Expand Down
10 changes: 9 additions & 1 deletion src/common/typings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from datetime import datetime
from functools import cached_property

from eth_keys.datatypes import PublicKey
Expand All @@ -13,7 +14,7 @@ class Oracles:
rewards_threshold: int
validators_threshold: int
exit_signature_recover_threshold: int
exit_signature_deadline: int
signature_validity_period: int
public_keys: list[HexStr]
endpoints: list[list[str]]

Expand Down Expand Up @@ -47,3 +48,10 @@ class HarvestParams:
class OracleApproval:
signature: bytes
ipfs_hash: str


@dataclass
class OraclesApproval:
signatures: bytes
ipfs_hash: str
deadline: datetime
2 changes: 1 addition & 1 deletion src/exits/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def get_oracles_approval(
) -> OraclesApproval:
"""Fetches approval from oracles."""
fork = await consensus_client.get_consensus_fork()
deadline = datetime.now(timezone.utc) + timedelta(seconds=oracles.exit_signature_deadline)
deadline = datetime.now(timezone.utc) + timedelta(seconds=oracles.signature_validity_period)

# get exit signature shards
request = SignatureRotationRequest(
Expand Down
11 changes: 4 additions & 7 deletions src/validators/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
)
from src.common.ipfs import fetch_harvest_params
from src.common.metrics import metrics
from src.common.typings import OraclesApproval
from src.config.networks import ETH_NETWORKS
from src.config.settings import DEPOSIT_AMOUNT, settings
from src.validators.database import NetworkValidatorCrud
from src.validators.typings import (
DepositData,
Keystores,
NetworkValidator,
OraclesApproval,
Validator,
)
from src.validators.typings import DepositData, Keystores, NetworkValidator, Validator

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -234,6 +229,7 @@ async def register_single_validator(
tx_validators[0],
approval.signatures,
approval.ipfs_hash,
approval.deadline,
),
multi_proof.proof,
]
Expand Down Expand Up @@ -270,6 +266,7 @@ async def register_multiple_validator(
b''.join(tx_validators),
approval.signatures,
approval.ipfs_hash,
approval.deadline,
),
indexes,
multi_proof.proof_flags,
Expand Down
21 changes: 17 additions & 4 deletions src/validators/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from datetime import datetime, timedelta, timezone

from multiproof.standart import MultiProof
from sw_utils.typings import Bytes32
Expand All @@ -9,7 +10,7 @@
from src.common.contracts import validators_registry_contract
from src.common.execution import check_gas_price, get_oracles
from src.common.metrics import metrics
from src.common.typings import Oracles
from src.common.typings import Oracles, OraclesApproval
from src.common.utils import MGNO_RATE, WAD
from src.config.networks import GNOSIS
from src.config.settings import DEPOSIT_AMOUNT, settings
Expand All @@ -27,7 +28,6 @@
DepositData,
Keystores,
NetworkValidator,
OraclesApproval,
Validator,
)
from src.validators.utils import send_approval_requests
Expand Down Expand Up @@ -78,18 +78,27 @@ async def register_validators(keystores: Keystores, deposit_data: DepositData) -
validators=validators,
)
registry_root = None
deadline = None
while True:
latest_registry_root = await validators_registry_contract.get_registry_root()

if not registry_root or registry_root != latest_registry_root:
latest_deadline = datetime.now(timezone.utc) + timedelta(
seconds=oracles.signature_validity_period
)
if (
not registry_root
or registry_root != latest_registry_root
or deadline <= datetime.now(timezone.utc)
):
registry_root = latest_registry_root
deadline = latest_deadline
logger.debug('Fetched latest validators registry root: %s', registry_root)

oracles_request = await create_approval_request(
registry_root=registry_root,
oracles=oracles,
keystores=keystores,
validators=validators,
deadline=deadline,
multi_proof=multi_proof,
)

Expand Down Expand Up @@ -122,12 +131,14 @@ async def register_validators(keystores: Keystores, deposit_data: DepositData) -
logger.info('Successfully registered validators with public keys %s', pub_keys)


# pylint: disable-next=too-many-arguments
async def create_approval_request(
oracles: Oracles,
keystores: Keystores,
validators: list[Validator],
registry_root: Bytes32,
multi_proof: MultiProof,
deadline: datetime,
) -> ApprovalRequest:
"""Generate validator registration request data"""

Expand All @@ -151,6 +162,7 @@ async def create_approval_request(
exit_signature_shards=[],
proof=multi_proof.proof,
proof_flags=multi_proof.proof_flags,
deadline=deadline,
)
for validator in validators:
shards = get_exit_signature_shards(
Expand Down Expand Up @@ -186,6 +198,7 @@ async def get_oracles_approval(oracles: Oracles, request: ApprovalRequest) -> Or
return OraclesApproval(
signatures=signatures,
ipfs_hash=ipfs_hash,
deadline=request.deadline,
)


Expand Down
17 changes: 11 additions & 6 deletions src/validators/typings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dataclasses
from dataclasses import dataclass
from datetime import datetime
from typing import NewType

from eth_typing import BlockNumber, ChecksumAddress, HexStr
Expand Down Expand Up @@ -34,12 +36,6 @@ class ExitSignatureShards:
exit_signatures: list[HexStr]


@dataclass
class OraclesApproval:
signatures: bytes
ipfs_hash: str


@dataclass
# pylint: disable-next=too-many-instance-attributes
class ApprovalRequest:
Expand All @@ -52,6 +48,15 @@ class ApprovalRequest:
exit_signature_shards: list[list[HexStr]]
proof: list[HexStr]
proof_flags: list[bool]
deadline: datetime

def as_json_dict(self) -> dict:
"""
:return: dict which can be serialized by `json.dumps()`
"""
res = dataclasses.asdict(self)
res['deadline'] = int(self.deadline.timestamp())
return res


@dataclass
Expand Down

0 comments on commit 3c049bc

Please sign in to comment.