Пример #1
0
    async def do_run(cls, boot_info: BootInfo, event_bus: EndpointAPI) -> None:
        trinity_config = boot_info.trinity_config
        beacon_app_config = trinity_config.get_app_config(BeaconAppConfig)
        chain_config = beacon_app_config.get_chain_config()
        base_db = DBClient.connect(trinity_config.database_ipc_path)

        # TODO: For now we use fake eth1 monitor.
        # if boot_info.args.eth1client_rpc:
        #     w3: Web3 = Web3.HTTPProvider(boot_info.args.eth1client_rpc)
        # else:
        #     w3: Web3 = None

        # TODO: For now we use fake eth1 monitor. So we load validators data from
        # interop setting and hardcode the deposit data into fake eth1 data provider.
        chain = chain_config.beacon_chain_class(base_db,
                                                chain_config.genesis_config)
        config = chain.get_state_machine().config
        key_set = load_yaml_at(
            Path(
                "eth2/beacon/scripts/quickstart_state/keygen_16_validators.yaml"
            ))
        pubkeys, privkeys, withdrawal_credentials = create_keypair_and_mock_withdraw_credentials(
            config,
            key_set  # type: ignore
        )
        initial_deposits = (create_mock_deposit_data(
            config=config,
            pubkey=pubkey,
            privkey=privkey,
            withdrawal_credentials=withdrawal_credential,
        ) for pubkey, privkey, withdrawal_credential in zip(
            pubkeys, privkeys, withdrawal_credentials))

        # Set the timestamp of start block earlier enough so that eth1 monitor
        # can query up to 2 * `ETH1_FOLLOW_DISTANCE` of blocks in the beginning.
        start_block_timestamp = (chain_config.genesis_data.genesis_time -
                                 3 * ETH1_FOLLOW_DISTANCE * AVERAGE_BLOCK_TIME)
        with base_db:
            fake_eth1_data_provider = FakeEth1DataProvider(
                start_block_number=START_BLOCK_NUMBER,
                start_block_timestamp=Timestamp(start_block_timestamp),
                num_deposits_per_block=NUM_DEPOSITS_PER_BLOCK,
                initial_deposits=tuple(initial_deposits),
            )

            eth1_monitor_service: Service = Eth1Monitor(
                eth1_data_provider=fake_eth1_data_provider,
                num_blocks_confirmed=NUM_BLOCKS_CONFIRMED,
                polling_period=POLLING_PERIOD,
                start_block_number=BlockNumber(START_BLOCK_NUMBER - 1),
                event_bus=event_bus,
                base_db=base_db,
            )

            try:
                await TrioManager.run_service(eth1_monitor_service)
            except Exception:
                await event_bus.broadcast(
                    ShutdownRequest("Eth1 Monitor ended unexpectedly"))
                raise
Пример #2
0
def create_mock_deposits_and_root(
    pubkeys: Sequence[BLSPubkey],
    keymap: Dict[BLSPubkey, int],
    config: Eth2Config,
    withdrawal_credentials: Sequence[Hash32] = None,
    leaves: Sequence[Hash32] = None,
) -> Tuple[Tuple[Deposit, ...], Hash32]:
    """
    Creates as many new deposits as there are keys in ``pubkeys``.

    Optionally provide corresponding ``withdrawal_credentials`` to include those.

    Optionally provide the prefix in the sequence of leaves leading up to the
    new deposits made by this function to get the correct updated root. If ``leaves`` is
    empty, this function simulates the genesis deposit tree calculation.
    """
    if not withdrawal_credentials:
        withdrawal_credentials = tuple(
            Hash32(b"\x22" * 32) for _ in range(len(pubkeys)))
    else:
        assert len(withdrawal_credentials) == len(pubkeys)
    if not leaves:
        leaves = tuple()

    deposit_datas = tuple()  # type: Tuple[DepositData, ...]
    deposit_data_leaves = cast(Tuple[Hash32, ...],
                               leaves)  # type: Tuple[Hash32, ...]
    for key, credentials in zip(pubkeys, withdrawal_credentials):
        privkey = keymap[key]
        deposit_data = create_mock_deposit_data(
            config=config,
            pubkey=key,
            privkey=privkey,
            withdrawal_credentials=credentials,
        )
        item = deposit_data.hash_tree_root
        deposit_data_leaves += (item, )
        deposit_datas += (deposit_data, )

    deposits: Tuple[Deposit, ...] = tuple()
    for index, data in enumerate(deposit_datas):
        length_mix_in = Hash32((index + 1).to_bytes(32, byteorder="little"))
        tree = calc_merkle_tree_from_leaves(deposit_data_leaves[:index + 1])

        deposit = Deposit(
            proof=(get_merkle_proof(tree, item_index=index) +
                   (length_mix_in, )),
            data=data,
        )
        deposits += (deposit, )

    if len(deposit_data_leaves) > 0:
        tree_root = get_root(tree)
        return deposits, hash_eth2(tree_root + length_mix_in)
    else:
        return tuple(), ZERO_HASH32
Пример #3
0
def create_mock_deposits_and_root(
    pubkeys: Sequence[BLSPubkey],
    keymap: Dict[BLSPubkey, int],
    config: Eth2Config,
    withdrawal_credentials: Sequence[Hash32] = None,
    leaves: Sequence[Hash32] = None,
) -> Tuple[Tuple[Deposit, ...], Hash32]:
    """
    Creates as many new deposits as there are keys in ``pubkeys``.

    Optionally provide corresponding ``withdrawal_credentials`` to include those.

    Optionally provide the prefix in the sequence of leaves leading up to the
    new deposits made by this function to get the correct updated root. If ``leaves`` is
    empty, this function simulates the genesis deposit tree calculation.
    """
    if not withdrawal_credentials:
        withdrawal_credentials = tuple(
            Hash32(b"\x22" * 32) for _ in range(len(pubkeys)))
    else:
        assert len(withdrawal_credentials) == len(pubkeys)
    if not leaves:
        leaves = tuple()

    deposit_datas = tuple()  # type: Tuple[DepositData, ...]
    for key, credentials in zip(pubkeys, withdrawal_credentials):
        privkey = keymap[key]
        deposit_data = create_mock_deposit_data(
            config=config,
            pubkey=key,
            privkey=privkey,
            withdrawal_credentials=credentials,
        )
        deposit_datas += (deposit_data, )

    deposits: Tuple[Deposit, ...] = tuple()
    for index, data in enumerate(deposit_datas):
        deposit_datas_at_count = deposit_datas[:index + 1]
        tree, root = make_deposit_tree_and_root(deposit_datas_at_count)
        proof = make_deposit_proof(deposit_datas_at_count, tree, root, index)

        deposit = Deposit.create(proof=proof, data=data)
        deposits += (deposit, )

    if len(deposit_datas) > 0:
        return deposits, root
    else:
        return tuple(), ZERO_HASH32
Пример #4
0
def create_mock_deposits_and_root(
        pubkeys: Sequence[BLSPubkey],
        keymap: Dict[BLSPubkey, int],
        config: Eth2Config,
        withdrawal_credentials: Sequence[Hash32] = None,
        leaves: Sequence[Hash32] = None) -> Tuple[Tuple[Deposit, ...], Hash32]:
    """
    Creates as many new deposits as there are keys in ``pubkeys``.

    Optionally provide corresponding ``withdrawal_credentials`` to include those.

    Optionally provide the prefix in the sequence of leaves leading up to the
    new deposits made by this function to get the correct updated root. If ``leaves`` is
    empty, this function simulates the genesis deposit tree calculation.
    """
    if not withdrawal_credentials:
        withdrawal_credentials = tuple(
            Hash32(b'\x22' * 32) for _ in range(len(pubkeys)))
    else:
        assert len(withdrawal_credentials) == len(pubkeys)
    if not leaves:
        leaves = tuple()

    deposit_datas = tuple()  # type: Tuple[DepositData, ...]
    deposit_data_leaves = cast(Tuple[Hash32, ...],
                               leaves)  # type: Tuple[Hash32, ...]

    for key, credentials in zip(pubkeys, withdrawal_credentials):
        privkey = keymap[key]
        deposit_data = create_mock_deposit_data(
            config=config,
            pubkey=key,
            privkey=privkey,
            withdrawal_credentials=credentials,
        )
        item = deposit_data.root
        deposit_data_leaves += (item, )
        deposit_datas += (deposit_data, )

    tree = calc_merkle_tree_from_leaves(deposit_data_leaves)

    deposits = tuple(
        Deposit(
            proof=get_merkle_proof(tree, item_index=i),
            data=data,
        ) for i, data in enumerate(deposit_datas))

    return deposits, get_root(tree)
Пример #5
0
def create_mock_genesis_validator_deposits_and_root(
        num_validators: int,
        config: Eth2Config,
        pubkeys: Sequence[BLSPubkey],
        keymap: Dict[BLSPubkey, int]) -> Tuple[Tuple[Deposit, ...], Hash32]:
    # Mock data
    withdrawal_credentials = Hash32(b'\x22' * 32)
    fork = Fork(
        previous_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'),
        current_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'),
        epoch=config.GENESIS_EPOCH,
    )

    deposit_data_array = tuple()  # type: Tuple[DepositData, ...]
    deposit_data_leaves = tuple()  # type: Tuple[Hash32, ...]

    for i in range(num_validators):
        deposit_data = create_mock_deposit_data(
            config=config,
            pubkeys=pubkeys,
            keymap=keymap,
            validator_index=ValidatorIndex(i),
            withdrawal_credentials=withdrawal_credentials,
            fork=fork,
        )
        item = hash_eth2(ssz.encode(deposit_data))
        deposit_data_leaves += (item,)
        deposit_data_array += (deposit_data,)

    tree = calc_merkle_tree_from_leaves(deposit_data_leaves)
    root = get_merkle_root(deposit_data_leaves)

    genesis_validator_deposits = tuple(
        Deposit(
            proof=get_merkle_proof(tree, item_index=i),
            index=i,
            deposit_data=deposit_data_array[i],
        )
        for i in range(num_validators)
    )

    return genesis_validator_deposits, root
Пример #6
0
def create_mock_deposit(config, sample_beacon_state_params, keymap, pubkeys,
                        withdrawal_credentials, validator_index):
    state = BeaconState(**sample_beacon_state_params).copy(
        slot=1,
        validator_registry=(),
    )
    fork = Fork(
        previous_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'),
        current_version=config.GENESIS_FORK_VERSION.to_bytes(4, 'little'),
        epoch=config.GENESIS_EPOCH,
    )
    deposit_data = create_mock_deposit_data(
        config=config,
        pubkeys=pubkeys,
        keymap=keymap,
        validator_index=validator_index,
        withdrawal_credentials=withdrawal_credentials,
        fork=fork,
    )

    item = hash_eth2(ssz.encode(deposit_data))
    test_deposit_data_leaves = (item, )
    tree = calc_merkle_tree_from_leaves(test_deposit_data_leaves)
    root = get_merkle_root(test_deposit_data_leaves)
    proof = list(get_merkle_proof(tree, item_index=validator_index))

    state = state.copy(latest_eth1_data=state.latest_eth1_data.copy(
        deposit_root=root, ), )

    deposit = Deposit(
        proof=proof,
        index=validator_index,
        deposit_data=deposit_data,
    )

    return state, deposit