コード例 #1
0
 def _get_deposit(self, deposit_count: int, deposit_index: int) -> Deposit:
     """
     Return `Deposit` according to `deposit_count` and `deposit_index`.
     It should include the deposit data at the `deposit_index`, and the merkle proof of
     the corresponding merkle tree made from deposit data of size `deposit_count`.
     """
     if deposit_index >= deposit_count:
         raise Eth1MonitorValidationError(
             "`deposit_index` should be smaller than `deposit_count`: "
             f"deposit_index={deposit_index}, deposit_count={deposit_count}"
         )
     len_deposit_data = self.total_deposit_count
     if deposit_count <= 0 or deposit_count > len_deposit_data:
         raise Eth1MonitorValidationError(
             f"invalid `deposit_count`: deposit_count={deposit_count}")
     if deposit_index < 0 or deposit_index >= len_deposit_data:
         raise Eth1MonitorValidationError(
             f"invalid `deposit_index`: deposit_index={deposit_index}")
     deposit_data_in_range = self._db.get_deposit_data_range(
         0, deposit_count)
     tree, root = make_deposit_tree_and_root(deposit_data_in_range)
     return Deposit(
         proof=make_deposit_proof(deposit_data_in_range, tree, root,
                                  deposit_index),
         data=self._db.get_deposit_data(deposit_index),
     )
コード例 #2
0
ファイル: initializer.py プロジェクト: wschwab/trinity
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_initial_validator_deposits(
        num_validators: int,
        config: BeaconConfig,
        pubkeys: Sequence[BLSPubkey],
        keymap: Dict[BLSPubkey, int]) -> Tuple[Deposit, ...]:
    # Mock data
    withdrawal_credentials = b'\x22' * 32
    randao_commitment = b'\x33' * 32
    custody_commitment = b'\x44' * 32
    deposit_timestamp = 0
    fork = Fork(
        previous_version=config.GENESIS_FORK_VERSION,
        current_version=config.GENESIS_FORK_VERSION,
        epoch=config.GENESIS_EPOCH,
    )

    initial_validator_deposits = tuple(
        Deposit(
            branch=(
                b'\x11' * 32
                for j in range(10)
            ),
            index=i,
            deposit_data=DepositData(
                deposit_input=DepositInput(
                    pubkey=pubkeys[i],
                    withdrawal_credentials=withdrawal_credentials,
                    randao_commitment=randao_commitment,
                    custody_commitment=custody_commitment,
                    proof_of_possession=sign_proof_of_possession(
                        deposit_input=DepositInput(
                            pubkey=pubkeys[i],
                            withdrawal_credentials=withdrawal_credentials,
                            randao_commitment=randao_commitment,
                            custody_commitment=custody_commitment,
                        ),
                        privkey=keymap[pubkeys[i]],
                        fork=fork,
                        slot=config.GENESIS_SLOT,
                        epoch_length=config.EPOCH_LENGTH,
                    ),
                ),
                amount=config.MAX_DEPOSIT_AMOUNT,
                timestamp=deposit_timestamp,
            ),
        )
        for i in range(num_validators)
    )

    return initial_validator_deposits
コード例 #4
0
ファイル: initializer.py プロジェクト: pamir-s/trinity
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
コード例 #5
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)
コード例 #6
0
def create_mock_genesis_validator_deposits(
        num_validators: int,
        config: BeaconConfig,
        pubkeys: Sequence[BLSPubkey],
        keymap: Dict[BLSPubkey, int]) -> Tuple[Deposit, ...]:
    # Mock data
    withdrawal_credentials = Hash32(b'\x22' * 32)
    deposit_timestamp = Timestamp(0)
    fork = Fork(
        previous_version=config.GENESIS_FORK_VERSION,
        current_version=config.GENESIS_FORK_VERSION,
        epoch=config.GENESIS_EPOCH,
    )

    genesis_validator_deposits = tuple(
        Deposit(
            branch=tuple(
                Hash32(b'\x11' * 32)
                for j in range(10)
            ),
            index=i,
            deposit_data=DepositData(
                deposit_input=DepositInput(
                    pubkey=pubkeys[i],
                    withdrawal_credentials=withdrawal_credentials,
                    proof_of_possession=sign_proof_of_possession(
                        deposit_input=DepositInput(
                            pubkey=pubkeys[i],
                            withdrawal_credentials=withdrawal_credentials,
                        ),
                        privkey=keymap[pubkeys[i]],
                        fork=fork,
                        slot=config.GENESIS_SLOT,
                        slots_per_epoch=config.SLOTS_PER_EPOCH,
                    ),
                ),
                amount=config.MAX_DEPOSIT_AMOUNT,
                timestamp=deposit_timestamp,
            ),
        )
        for i in range(num_validators)
    )

    return genesis_validator_deposits
コード例 #7
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
コード例 #8
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
コード例 #9
0
def test_get_genesis_beacon_state(
        privkeys, pubkeys, num_validators, genesis_epoch, genesis_slot,
        genesis_fork_version, genesis_start_shard, shard_count,
        min_seed_lookahead, latest_block_roots_length,
        latest_active_index_roots_length, slots_per_epoch, max_deposit_amount,
        latest_slashed_exit_length, latest_randao_mixes_length,
        activation_exit_delay, sample_eth1_data_params):
    withdrawal_credentials = b'\x22' * 32
    fork = Fork(
        previous_version=genesis_fork_version,
        current_version=genesis_fork_version,
        epoch=genesis_epoch,
    )

    validator_count = 5

    genesis_validator_deposits = tuple(
        Deposit(
            branch=(b'\x11' * 32 for j in range(10)),
            index=i,
            deposit_data=DepositData(
                deposit_input=DepositInput(
                    pubkey=pubkeys[i],
                    withdrawal_credentials=withdrawal_credentials,
                    proof_of_possession=sign_proof_of_possession(
                        deposit_input=DepositInput(
                            pubkey=pubkeys[i],
                            withdrawal_credentials=withdrawal_credentials,
                        ),
                        privkey=privkeys[i],
                        fork=fork,
                        slot=genesis_slot,
                        slots_per_epoch=slots_per_epoch,
                    ),
                ),
                amount=max_deposit_amount,
                timestamp=0,
            ),
        ) for i in range(validator_count))
    genesis_time = 10
    latest_eth1_data = Eth1Data(**sample_eth1_data_params)

    state = get_genesis_beacon_state(
        genesis_validator_deposits=genesis_validator_deposits,
        genesis_time=genesis_time,
        latest_eth1_data=latest_eth1_data,
        genesis_epoch=genesis_epoch,
        genesis_slot=genesis_slot,
        genesis_fork_version=genesis_fork_version,
        genesis_start_shard=genesis_start_shard,
        shard_count=shard_count,
        min_seed_lookahead=min_seed_lookahead,
        latest_block_roots_length=latest_block_roots_length,
        latest_active_index_roots_length=latest_active_index_roots_length,
        slots_per_epoch=slots_per_epoch,
        max_deposit_amount=max_deposit_amount,
        latest_slashed_exit_length=latest_slashed_exit_length,
        latest_randao_mixes_length=latest_randao_mixes_length,
        activation_exit_delay=activation_exit_delay,
    )

    # Misc
    assert state.slot == genesis_slot
    assert state.genesis_time == genesis_time
    assert state.fork.previous_version == genesis_fork_version
    assert state.fork.current_version == genesis_fork_version
    assert state.fork.epoch == genesis_epoch

    # Validator registry
    assert len(state.validator_registry) == validator_count
    assert len(state.validator_balances) == validator_count
    assert state.validator_registry_update_epoch == genesis_epoch

    # Randomness and committees
    assert len(state.latest_randao_mixes) == latest_randao_mixes_length
    assert state.previous_shuffling_start_shard == genesis_start_shard
    assert state.current_shuffling_start_shard == genesis_start_shard
    assert state.previous_shuffling_epoch == genesis_epoch
    assert state.current_shuffling_epoch == genesis_epoch
    assert state.previous_shuffling_seed == ZERO_HASH32

    # Finality
    assert state.previous_justified_epoch == genesis_epoch
    assert state.justified_epoch == genesis_epoch
    assert state.justification_bitfield == 0
    assert state.finalized_epoch == genesis_epoch

    # Recent state
    assert len(state.latest_crosslinks) == shard_count
    assert state.latest_crosslinks[0] == CrosslinkRecord(
        epoch=genesis_epoch,
        crosslink_data_root=ZERO_HASH32,
    )
    assert len(state.latest_block_roots) == latest_block_roots_length
    assert state.latest_block_roots[0] == ZERO_HASH32
    assert len(state.latest_slashed_balances) == latest_slashed_exit_length
    assert state.latest_slashed_balances[0] == Gwei(0)

    assert len(state.latest_attestations) == 0
    assert len(state.batched_block_roots) == 0

    # Ethereum 1.0 chain data
    assert state.latest_eth1_data == latest_eth1_data
    assert len(state.eth1_data_votes) == 0
    assert state.deposit_index == len(genesis_validator_deposits)

    assert state.validator_registry[0].is_active(genesis_epoch)
コード例 #10
0
ファイル: initializer.py プロジェクト: pamir-s/trinity
def create_deposit(deposit_data: DepositData, tree: MerkleTree, index: int,
                   leaf_count: int) -> Deposit:
    proof = create_deposit_proof(tree, index, leaf_count)
    return Deposit.create(proof=proof, data=deposit_data)
コード例 #11
0
def test_get_initial_beacon_state(
        privkeys, pubkeys, num_validators, genesis_epoch, genesis_slot,
        genesis_fork_version, genesis_start_shard, shard_count, seed_lookahead,
        latest_block_roots_length, latest_index_roots_length, epoch_length,
        max_deposit_amount, latest_penalized_exit_length,
        latest_randao_mixes_length, entry_exit_delay, sample_eth1_data_params):
    withdrawal_credentials = b'\x22' * 32
    randao_commitment = b'\x33' * 32
    custody_commitment = b'\x44' * 32
    fork = Fork(
        previous_version=genesis_fork_version,
        current_version=genesis_fork_version,
        epoch=genesis_epoch,
    )

    validator_count = 5

    initial_validator_deposits = (Deposit(
        branch=(b'\x11' * 32 for j in range(10)),
        index=i,
        deposit_data=DepositData(
            deposit_input=DepositInput(
                pubkey=pubkeys[i],
                withdrawal_credentials=withdrawal_credentials,
                randao_commitment=randao_commitment,
                custody_commitment=custody_commitment,
                proof_of_possession=sign_proof_of_possession(
                    deposit_input=DepositInput(
                        pubkey=pubkeys[i],
                        withdrawal_credentials=withdrawal_credentials,
                        randao_commitment=randao_commitment,
                        custody_commitment=custody_commitment,
                    ),
                    privkey=privkeys[i],
                    fork=fork,
                    slot=genesis_slot,
                    epoch_length=epoch_length,
                ),
            ),
            amount=max_deposit_amount,
            timestamp=0,
        ),
    ) for i in range(validator_count))
    genesis_time = 10
    latest_eth1_data = Eth1Data(**sample_eth1_data_params)

    state = get_initial_beacon_state(
        initial_validator_deposits=initial_validator_deposits,
        genesis_time=genesis_time,
        latest_eth1_data=latest_eth1_data,
        genesis_epoch=genesis_epoch,
        genesis_slot=genesis_slot,
        genesis_fork_version=genesis_fork_version,
        genesis_start_shard=genesis_start_shard,
        shard_count=shard_count,
        seed_lookahead=seed_lookahead,
        latest_block_roots_length=latest_block_roots_length,
        latest_index_roots_length=latest_index_roots_length,
        epoch_length=epoch_length,
        max_deposit_amount=max_deposit_amount,
        latest_penalized_exit_length=latest_penalized_exit_length,
        latest_randao_mixes_length=latest_randao_mixes_length,
        entry_exit_delay=entry_exit_delay,
    )

    # Misc
    assert state.slot == genesis_slot
    assert state.genesis_time == genesis_time
    assert state.fork.previous_version == genesis_fork_version
    assert state.fork.current_version == genesis_fork_version
    assert state.fork.epoch == genesis_epoch

    # Validator registry
    assert len(state.validator_registry) == validator_count
    assert len(state.validator_balances) == validator_count
    assert state.validator_registry_update_epoch == genesis_epoch
    assert state.validator_registry_exit_count == 0

    # Randomness and committees
    assert len(state.latest_randao_mixes) == latest_randao_mixes_length
    assert len(
        state.latest_vdf_outputs) == latest_randao_mixes_length // epoch_length

    # TODO: `persistent_committees`, `persistent_committee_reassignments` will be removed
    assert len(state.persistent_committees) == 0
    assert len(state.persistent_committee_reassignments) == 0
    assert state.previous_epoch_start_shard == genesis_start_shard
    assert state.current_epoch_start_shard == genesis_start_shard
    assert state.previous_calculation_epoch == genesis_epoch
    assert state.current_calculation_epoch == genesis_epoch
    assert state.previous_epoch_seed == ZERO_HASH32

    # Custody challenges
    assert len(state.custody_challenges) == 0

    # Finality
    assert state.previous_justified_epoch == genesis_epoch
    assert state.justified_epoch == genesis_epoch
    assert state.justification_bitfield == 0
    assert state.finalized_epoch == genesis_epoch

    # Recent state
    assert len(state.latest_crosslinks) == shard_count
    assert state.latest_crosslinks[0] == CrosslinkRecord(
        epoch=genesis_epoch,
        shard_block_root=ZERO_HASH32,
    )
    assert len(state.latest_block_roots) == latest_block_roots_length
    assert state.latest_block_roots[0] == ZERO_HASH32
    assert len(state.latest_penalized_balances) == latest_penalized_exit_length
    assert state.latest_penalized_balances[0] == Gwei(0)

    assert len(state.latest_attestations) == 0
    assert len(state.batched_block_roots) == 0

    # Ethereum 1.0 chain data
    assert state.latest_eth1_data == latest_eth1_data
    assert len(state.eth1_data_votes) == 0

    assert state.validator_registry[0].is_active(genesis_epoch)
コード例 #12
0
def test_defaults(sample_deposit_params):
    deposit = Deposit(**sample_deposit_params)

    assert deposit.data == sample_deposit_params['data']
コード例 #13
0
def test_defaults(sample_deposit_params):
    deposit = Deposit(**sample_deposit_params)

    assert deposit.deposit_data.timestamp == sample_deposit_params[
        'deposit_data'].timestamp
コード例 #14
0
def test_defaults(sample_deposit_params):
    deposit = Deposit.create(**sample_deposit_params)

    assert deposit.data == sample_deposit_params["data"]