|
| 1 | +from argparse import ( |
| 2 | + ArgumentParser, |
| 3 | + _SubParsersAction, |
| 4 | +) |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +import time |
| 8 | + |
| 9 | +from async_service import Service, TrioManager |
| 10 | + |
| 11 | +from eth_typing import BlockNumber |
| 12 | + |
| 13 | +from lahja import EndpointAPI |
| 14 | + |
| 15 | +# from web3 import Web3 |
| 16 | + |
| 17 | +from eth2.beacon.tools.builder.initializer import ( |
| 18 | + create_keypair_and_mock_withdraw_credentials, |
| 19 | +) |
| 20 | +from eth2.beacon.tools.builder.validator import create_mock_deposit_data |
| 21 | +from eth2.beacon.tools.fixtures.loading import load_yaml_at |
| 22 | +from eth2.beacon.typing import Timestamp |
| 23 | +from trinity.boot_info import BootInfo |
| 24 | +from trinity.components.eth2.eth1_monitor.configs import deposit_contract_json |
| 25 | +from trinity.components.eth2.eth1_monitor.eth1_data_provider import FakeEth1DataProvider |
| 26 | +from trinity.config import BeaconAppConfig |
| 27 | +from trinity.db.manager import DBClient |
| 28 | +from trinity.events import ShutdownRequest |
| 29 | +from trinity.extensibility import ( |
| 30 | + TrioIsolatedComponent, |
| 31 | +) |
| 32 | + |
| 33 | +from .eth1_monitor import Eth1Monitor |
| 34 | + |
| 35 | + |
| 36 | +# Fake eth1 monitor config |
| 37 | +# TODO: These configs should be read from a config file, e.g., `eth1_monitor_config.yaml`. |
| 38 | +DEPOSIT_CONTRACT_ABI = json.loads(deposit_contract_json)["abi"] |
| 39 | +DEPOSIT_CONTRACT_ADDRESS = b"\x12" * 20 |
| 40 | +NUM_BLOCKS_CONFIRMED = 100 |
| 41 | +POLLING_PERIOD = 10 |
| 42 | +START_BLOCK_NUMBER = BlockNumber(1000) |
| 43 | + |
| 44 | +# Configs for fake Eth1DataProvider |
| 45 | +NUM_DEPOSITS_PER_BLOCK = 0 |
| 46 | +START_BLOCK_TIMESTAMP = Timestamp(int(time.time()) - 2100) # Around 45 mins ago |
| 47 | + |
| 48 | + |
| 49 | +class Eth1MonitorComponent(TrioIsolatedComponent): |
| 50 | + |
| 51 | + name = "Eth1 Monitor" |
| 52 | + endpoint_name = "eth1-monitor" |
| 53 | + |
| 54 | + @property |
| 55 | + def is_enabled(self) -> bool: |
| 56 | + return self._boot_info.trinity_config.has_app_config(BeaconAppConfig) |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def configure_parser(cls, |
| 60 | + arg_parser: ArgumentParser, |
| 61 | + subparser: _SubParsersAction) -> None: |
| 62 | + # TODO: For now we use fake eth1 monitor. |
| 63 | + pass |
| 64 | + # arg_parser.add_argument( |
| 65 | + # "--eth1client-rpc", |
| 66 | + # help="RPC HTTP endpoint of Eth1 client ", |
| 67 | + # ) |
| 68 | + |
| 69 | + @classmethod |
| 70 | + async def do_run(cls, boot_info: BootInfo, event_bus: EndpointAPI) -> None: |
| 71 | + trinity_config = boot_info.trinity_config |
| 72 | + beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) |
| 73 | + chain_config = beacon_app_config.get_chain_config() |
| 74 | + base_db = DBClient.connect(trinity_config.database_ipc_path) |
| 75 | + |
| 76 | + # TODO: For now we use fake eth1 monitor. |
| 77 | + # if boot_info.args.eth1client_rpc: |
| 78 | + # w3: Web3 = Web3.HTTPProvider(boot_info.args.eth1client_rpc) |
| 79 | + # else: |
| 80 | + # w3: Web3 = None |
| 81 | + |
| 82 | + # TODO: For now we use fake eth1 monitor. So we load validators data from |
| 83 | + # interop setting and hardcode the deposit data into fake eth1 data provider. |
| 84 | + chain = chain_config.beacon_chain_class( |
| 85 | + base_db, |
| 86 | + chain_config.genesis_config, |
| 87 | + ) |
| 88 | + config = chain.get_state_machine().config |
| 89 | + key_set = load_yaml_at( |
| 90 | + Path('eth2/beacon/scripts/quickstart_state/keygen_16_validators.yaml') |
| 91 | + ) |
| 92 | + pubkeys, privkeys, withdrawal_credentials = create_keypair_and_mock_withdraw_credentials( |
| 93 | + config, |
| 94 | + key_set, # type: ignore |
| 95 | + ) |
| 96 | + initial_deposits = ( |
| 97 | + create_mock_deposit_data( |
| 98 | + config=config, |
| 99 | + pubkey=pubkey, |
| 100 | + privkey=privkey, |
| 101 | + withdrawal_credentials=withdrawal_credential, |
| 102 | + ) |
| 103 | + for pubkey, privkey, withdrawal_credential in zip( |
| 104 | + pubkeys, privkeys, withdrawal_credentials) |
| 105 | + ) |
| 106 | + |
| 107 | + with base_db: |
| 108 | + fake_eth1_data_provider = FakeEth1DataProvider( |
| 109 | + start_block_number=START_BLOCK_NUMBER, |
| 110 | + start_block_timestamp=START_BLOCK_TIMESTAMP, |
| 111 | + num_deposits_per_block=NUM_DEPOSITS_PER_BLOCK, |
| 112 | + initial_deposits=tuple(initial_deposits), |
| 113 | + ) |
| 114 | + |
| 115 | + eth1_monitor_service: Service = Eth1Monitor( |
| 116 | + eth1_data_provider=fake_eth1_data_provider, |
| 117 | + num_blocks_confirmed=NUM_BLOCKS_CONFIRMED, |
| 118 | + polling_period=POLLING_PERIOD, |
| 119 | + start_block_number=START_BLOCK_NUMBER, |
| 120 | + event_bus=event_bus, |
| 121 | + base_db=base_db, |
| 122 | + ) |
| 123 | + |
| 124 | + try: |
| 125 | + await TrioManager.run_service(eth1_monitor_service) |
| 126 | + except Exception: |
| 127 | + await event_bus.broadcast(ShutdownRequest("Eth1 Monitor ended unexpectedly")) |
| 128 | + raise |
0 commit comments