Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-stakewise committed Aug 2, 2024
1 parent a5d2d22 commit 45f02e8
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 81 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The Relayer example for Stakewise v3-operator service

Relayer-Operator api communication is described in [Operator docs](https://docs.stakewise.io/for-operators/operator-service/running-as-api-service).

In this example keystores and deposit-data file were not created in advance.
In this example keystores and deposit-data file were not created in advance.
Relayer generates validator credentials on the fly.

## Running with Docker
Expand Down Expand Up @@ -38,17 +38,18 @@ Relayer-example is Python app made with FastAPI.

```text
src/ # sources root
|-- common/ #
|-- common/ #
| |-- abi/ # contracts ABI
| |-- clients.py # execution client
| |-- contracts.py # validators registry contract
| |-- credentials.py # Credential and CredentialManager used to generate keystores
|-- config/
| |-- networks.py # network configs
| |-- settings.py # app settings
|-- validators/ #
| |-- credentials.py # Credential and CredentialManager used to generate keystores
| |-- endpoints.py # api endpoints
| |-- schema.py # api request/response schema
| |-- validators_manager.py # functions for working with validators manager
| |-- typings.py # dataclasses
| |-- validators.py # functions for creating validators and exit signatures
| |-- validators_manager.py # functions for working with validators manager
```
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from src.config import settings
from src.validators.endpoints import router
from src.validators.typings import AppState
from src.validators.utils import load_validators_manager_account
from src.validators.validators_manager import load_validators_manager_account

setup_logging()
logger = logging.getLogger(__name__)
Expand Down
File renamed without changes.
56 changes: 3 additions & 53 deletions src/validators/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import milagro_bls_binding as bls
from eth_typing import BLSSignature, HexAddress
from fastapi import APIRouter
from sw_utils import ConsensusFork, get_exit_message_signing_root
from web3 import Web3

from src.common.contracts import validators_registry_contract
from src.common.credentials import DEPOSIT_AMOUNT_GWEI, Credential, CredentialManager
from src.config import settings
from src.validators.schema import (
ValidatorsRequest,
ValidatorsResponse,
ValidatorsResponseItem,
)
from src.validators.signing import get_validators_manager_signature
from src.validators.typings import Validator
from src.validators.validators import generate_validators
from src.validators.validators_manager import get_validators_manager_signature

router = APIRouter()

Expand All @@ -24,7 +19,7 @@ async def create_validators(
) -> ValidatorsResponse:
validator_items = []

validators = _generate_validators(
validators = generate_validators(
request.vault, request.validator_index, request.validators_count
)

Expand All @@ -50,48 +45,3 @@ async def create_validators(
validators=validator_items,
validators_manager_signature=validators_manager_signature,
)


def _generate_validators(
withdrawal_address: HexAddress, start_index: int, count: int
) -> list[Validator]:
"""
`_generate_validators` generates validator keystores, but does not save keystores on disk.
todo: You should save keystores on disk to be able to exit validator manually.
"""
res = []
credentials = CredentialManager.generate_credentials(
count=count,
start_index=0,
network=settings.network,
withdrawal_address=withdrawal_address,
)
validator_indexes = range(start_index, start_index + count)

for validator_index, credential in zip(validator_indexes, credentials):
deposit_datum_dict = credential.deposit_datum_dict()
exit_signature = _get_exit_signature(validator_index=validator_index, credential=credential)
res.append(
Validator(
public_key=credential.public_key,
deposit_data_root=Web3.to_hex(deposit_datum_dict['deposit_data_root']),
deposit_signature=Web3.to_hex(deposit_datum_dict['signature']),
amount_gwei=DEPOSIT_AMOUNT_GWEI,
exit_signature=exit_signature,
)
)
return res


def _get_exit_signature(
validator_index: int, credential: Credential, fork: ConsensusFork | None = None
) -> BLSSignature:
fork = fork or settings.network_config.SHAPELLA_FORK

message = get_exit_message_signing_root(
validator_index=validator_index,
genesis_validators_root=settings.network_config.GENESIS_VALIDATORS_ROOT,
fork=fork,
)

return bls.Sign(Web3.to_bytes(credential.private_key), message)
23 changes: 0 additions & 23 deletions src/validators/utils.py

This file was deleted.

57 changes: 57 additions & 0 deletions src/validators/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import milagro_bls_binding as bls
from eth_typing import BLSSignature, HexAddress
from sw_utils import ConsensusFork, get_exit_message_signing_root
from web3 import Web3

from src.config import settings
from src.validators.credentials import (
DEPOSIT_AMOUNT_GWEI,
Credential,
CredentialManager,
)
from src.validators.typings import Validator


def generate_validators(
withdrawal_address: HexAddress, start_index: int, count: int
) -> list[Validator]:
"""
`_generate_validators` generates validator keystores, but does not save keystores on disk.
todo: You should save keystores on disk to be able to exit validator manually.
"""
res = []
credentials = CredentialManager.generate_credentials(
count=count,
start_index=0,
network=settings.network,
withdrawal_address=withdrawal_address,
)
validator_indexes = range(start_index, start_index + count)

for validator_index, credential in zip(validator_indexes, credentials):
deposit_datum_dict = credential.deposit_datum_dict()
exit_signature = _get_exit_signature(validator_index=validator_index, credential=credential)
res.append(
Validator(
public_key=credential.public_key,
deposit_data_root=Web3.to_hex(deposit_datum_dict['deposit_data_root']),
deposit_signature=Web3.to_hex(deposit_datum_dict['signature']),
amount_gwei=DEPOSIT_AMOUNT_GWEI,
exit_signature=exit_signature,
)
)
return res


def _get_exit_signature(
validator_index: int, credential: Credential, fork: ConsensusFork | None = None
) -> BLSSignature:
fork = fork or settings.network_config.SHAPELLA_FORK

message = get_exit_message_signing_root(
validator_index=validator_index,
genesis_validators_root=settings.network_config.GENESIS_VALIDATORS_ROOT,
fork=fork,
)

return bls.Sign(Web3.to_bytes(credential.private_key), message)
20 changes: 20 additions & 0 deletions src/validators/signing.py → src/validators/validators_manager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import json
import os
from typing import Sequence

from eth_account import Account
from eth_account.messages import encode_typed_data
from eth_account.signers.local import LocalAccount
from eth_typing import ChecksumAddress, HexStr
from web3 import Web3

from src.config import settings
from src.validators.typings import AppState, Validator


def load_validators_manager_account() -> LocalAccount:
keystore_file = settings.validators_manager_key_file
keystore_password_file = settings.validators_manager_password_file
if not os.path.isfile(keystore_file):
raise ValueError(f"Can't open key file. Path: {keystore_file}")
if not os.path.isfile(keystore_password_file):
raise ValueError(f"Can't open password file. Path: {keystore_password_file}")

with open(keystore_file, 'r', encoding='utf-8') as f:
keyfile_json = json.load(f)
with open(keystore_password_file, 'r', encoding='utf-8') as f:
password = f.read().strip()
key = Account.decrypt(keyfile_json, password)
return Account().from_key(key)


def get_validators_manager_signature(
vault: ChecksumAddress, validators_registry_root: HexStr, validators: Sequence[Validator]
) -> HexStr:
Expand Down

0 comments on commit 45f02e8

Please sign in to comment.