Skip to content

Commit

Permalink
Add proof to validation request
Browse files Browse the repository at this point in the history
Signed-off-by: cyc60 <[email protected]>
  • Loading branch information
cyc60 committed Aug 22, 2023
1 parent 0a7f9bb commit b05859b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
22 changes: 4 additions & 18 deletions src/validators/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from multiproof import StandardMerkleTree
from sw_utils import (
EventProcessor,
compute_deposit_data,
get_eth1_withdrawal_credentials,
is_valid_deposit_data_signature,
)
Expand All @@ -23,8 +22,9 @@
from src.common.ipfs import fetch_harvest_params
from src.common.metrics import metrics
from src.config.networks import ETH_NETWORKS
from src.config.settings import DEPOSIT_AMOUNT, DEPOSIT_AMOUNT_GWEI, settings
from src.config.settings import DEPOSIT_AMOUNT, settings
from src.validators.database import NetworkValidatorCrud
from src.validators.signing import encode_tx_validator, get_single_validator_proof
from src.validators.typings import (
DepositData,
Keystores,
Expand Down Expand Up @@ -232,9 +232,7 @@ async def register_single_validator(
if settings.network not in ETH_NETWORKS:
raise NotImplementedError('networks other than Ethereum not supported')

credentials = get_eth1_withdrawal_credentials(settings.vault)
tx_validator = _encode_tx_validator(credentials, validator)
proof = tree.get_proof([tx_validator, validator.deposit_data_index])
tx_validator, proof = get_single_validator_proof(tree=tree, validator=validator)

logger.info('Submitting registration transaction')
register_call_args = [
Expand Down Expand Up @@ -275,7 +273,7 @@ async def register_multiple_validator(
tx_validators: list[bytes] = []
leaves: list[tuple[bytes, int]] = []
for validator in validators:
tx_validator = _encode_tx_validator(credentials, validator)
tx_validator = encode_tx_validator(credentials, validator)
tx_validators.append(tx_validator)
leaves.append((tx_validator, validator.deposit_data_index))

Expand Down Expand Up @@ -306,15 +304,3 @@ async def register_multiple_validator(

logger.info('Waiting for transaction %s confirmation', Web3.to_hex(tx))
await execution_client.eth.wait_for_transaction_receipt(tx, timeout=300)


def _encode_tx_validator(withdrawal_credentials: bytes, validator: Validator) -> bytes:
public_key = Web3.to_bytes(hexstr=validator.public_key)
signature = Web3.to_bytes(hexstr=validator.signature)
deposit_root = compute_deposit_data(
public_key=public_key,
withdrawal_credentials=withdrawal_credentials,
amount_gwei=DEPOSIT_AMOUNT_GWEI,
signature=signature,
).hash_tree_root
return public_key + signature + deposit_root
29 changes: 26 additions & 3 deletions src/validators/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import milagro_bls_binding as bls
from Cryptodome.Random.random import randint
from eth_typing import HexStr
from multiproof import StandardMerkleTree
from py_ecc.optimized_bls12_381.optimized_curve import curve_order
from sw_utils.signing import get_exit_message_signing_root
from sw_utils import get_eth1_withdrawal_credentials
from sw_utils.signing import compute_deposit_data, get_exit_message_signing_root
from sw_utils.typings import ConsensusFork
from web3 import Web3

from src.common.typings import Oracles
from src.config.settings import settings
from src.validators.typings import BLSPrivkey, ExitSignatureShards
from src.config.settings import DEPOSIT_AMOUNT_GWEI, settings
from src.validators.typings import BLSPrivkey, ExitSignatureShards, Validator


def get_polynomial_points(coefficients: list[int], num_points: int) -> list[bytes]:
Expand Down Expand Up @@ -60,3 +62,24 @@ def get_exit_signature_shards(
public_keys=[Web3.to_hex(bls.SkToPk(priv_key)) for priv_key in private_keys],
exit_signatures=exit_signature_shards,
)


def get_single_validator_proof(
tree: StandardMerkleTree, validator: Validator
) -> tuple[bytes, list[HexStr]]:
credentials = get_eth1_withdrawal_credentials(settings.vault)
tx_validator = encode_tx_validator(credentials, validator)
proof = tree.get_proof([tx_validator, validator.deposit_data_index]) # type: ignore
return tx_validator, proof


def encode_tx_validator(withdrawal_credentials: bytes, validator: Validator) -> bytes:
public_key = Web3.to_bytes(hexstr=validator.public_key)
signature = Web3.to_bytes(hexstr=validator.signature)
deposit_root = compute_deposit_data(
public_key=public_key,
withdrawal_credentials=withdrawal_credentials,
amount_gwei=DEPOSIT_AMOUNT_GWEI,
signature=signature,
).hash_tree_root
return public_key + signature + deposit_root
16 changes: 12 additions & 4 deletions src/validators/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sw_utils.typings import Bytes32
from web3 import Web3
from web3.types import BlockNumber, Wei
from web3.types import BlockNumber, HexStr, Wei

from src.common.clients import consensus_client, ipfs_fetch_client
from src.common.contracts import validators_registry_contract
Expand All @@ -20,7 +20,7 @@
register_multiple_validator,
register_single_validator,
)
from src.validators.signing import get_exit_signature_shards
from src.validators.signing import get_exit_signature_shards, get_single_validator_proof
from src.validators.typings import (
ApprovalRequest,
DepositData,
Expand All @@ -34,6 +34,7 @@
logger = logging.getLogger(__name__)


# pylint: disable-next=too-many-locals
async def register_validators(keystores: Keystores, deposit_data: DepositData) -> None:
"""Registers vault validators."""
vault_balance, update_state_call = await get_withdrawable_assets()
Expand Down Expand Up @@ -79,11 +80,14 @@ async def register_validators(keystores: Keystores, deposit_data: DepositData) -
while True:
if not registry_root or registry_root != latest_registry_root:
registry_root = latest_registry_root
_, proof = get_single_validator_proof(tree=deposit_data.tree, validator=validators[0])

oracles_request = await get_oracles_request(
registry_root=registry_root,
oracles=oracles,
keystores=keystores,
validators=validators,
proof=proof,
)

try:
Expand Down Expand Up @@ -117,7 +121,11 @@ async def register_validators(keystores: Keystores, deposit_data: DepositData) -


async def get_oracles_request(
oracles: Oracles, keystores: Keystores, validators: list[Validator], registry_root: Bytes32
oracles: Oracles,
keystores: Keystores,
validators: list[Validator],
registry_root: Bytes32,
proof: list[HexStr],
) -> ApprovalRequest:
"""Generate validator registration request data"""

Expand All @@ -139,7 +147,7 @@ async def get_oracles_request(
deposit_signatures=[],
public_key_shards=[],
exit_signature_shards=[],
proof=Bytes32(b''),
proof=proof,
)
for validator in validators:
shards = get_exit_signature_shards(
Expand Down
2 changes: 1 addition & 1 deletion src/validators/typings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ApprovalRequest:
deposit_signatures: list[HexStr]
public_key_shards: list[list[HexStr]]
exit_signature_shards: list[list[HexStr]]
proof: Bytes32
proof: list[HexStr]


@dataclass
Expand Down
8 changes: 3 additions & 5 deletions src/validators/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
RegistryRootChangedError,
ValidatorIndexChangedError,
)
from src.validators.execution import (
_encode_tx_validator,
get_latest_network_validator_public_keys,
)
from src.validators.execution import get_latest_network_validator_public_keys
from src.validators.signing import encode_tx_validator
from src.validators.typings import (
ApprovalRequest,
BLSPrivkey,
Expand Down Expand Up @@ -176,7 +174,7 @@ def load_deposit_data(vault: HexAddress, deposit_data_file: Path) -> DepositData
public_key=add_0x_prefix(data['pubkey']),
signature=add_0x_prefix(data['signature']),
)
leaves.append((_encode_tx_validator(credentials, validator), i))
leaves.append((encode_tx_validator(credentials, validator), i))
validators.append(validator)

tree = StandardMerkleTree.of(leaves, ['bytes', 'uint256'])
Expand Down

0 comments on commit b05859b

Please sign in to comment.