Пример #1
0
def create_mock_deposit_data(*,
                             config: Eth2Config,
                             pubkey: BLSPubkey,
                             privkey: int,
                             withdrawal_credentials: Hash32,
                             amount: Gwei = None) -> DepositData:
    if amount is None:
        amount = config.MAX_EFFECTIVE_BALANCE

    data = DepositData(pubkey=pubkey,
                       withdrawal_credentials=withdrawal_credentials,
                       amount=amount)
    signature = sign_proof_of_possession(deposit_data=data, privkey=privkey)
    return data.copy(signature=signature)
Пример #2
0
def create_deposit_data(*,
                        config: Eth2Config,
                        pubkey: BLSPubkey,
                        privkey: int,
                        withdrawal_credentials: Hash32,
                        fork: Fork,
                        deposit_timestamp: Timestamp,
                        amount: Gwei = None) -> DepositData:
    if amount is None:
        amount = config.MAX_DEPOSIT_AMOUNT

    return DepositData(
        deposit_input=DepositInput(
            pubkey=pubkey,
            withdrawal_credentials=withdrawal_credentials,
            proof_of_possession=sign_proof_of_possession(
                deposit_input=DepositInput(
                    pubkey=pubkey,
                    withdrawal_credentials=withdrawal_credentials,
                ),
                privkey=privkey,
                fork=fork,
                slot=config.GENESIS_SLOT,
                slots_per_epoch=config.SLOTS_PER_EPOCH,
            ),
        ),
        amount=amount,
        timestamp=deposit_timestamp,
    )
Пример #3
0
def convert_deposit_log_to_deposit_data(deposit_log: DepositLog) -> DepositData:
    return DepositData(
        pubkey=deposit_log.pubkey,
        withdrawal_credentials=deposit_log.withdrawal_credentials,
        amount=deposit_log.amount,
        signature=deposit_log.signature,
    )
Пример #4
0
 def _process_log(self, log: DepositLog, block_number: BlockNumber) -> None:
     """
     Simply store the deposit data from the log, and increase the corresponding block's
     `deposit_count`.
     """
     self._deposit_data.append(
         DepositData(
             pubkey=log.pubkey,
             withdrawal_credentials=log.withdrawal_credentials,
             amount=log.amount,
             signature=log.signature,
         ))
Пример #5
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
Пример #6
0
 def _process_logs(self, logs: Sequence[DepositLog],
                   block_number: BlockNumber) -> None:
     """
     Simply store the deposit data from the log, and increase the corresponding block's
     `deposit_count`.
     """
     seq_deposit_data = tuple(
         DepositData(
             pubkey=log.pubkey,
             withdrawal_credentials=log.withdrawal_credentials,
             amount=log.amount,
             signature=log.signature,
         ) for log in logs)
     self._db.add_deposit_data_batch(seq_deposit_data, block_number)
Пример #7
0
def create_deposit_data(
    public_key: BLSPubkey,
    private_key: int,
    withdrawal_credentials: Hash32,
    amount: Gwei,
) -> DepositData:
    message = DepositMessage.create(
        pubkey=public_key, withdrawal_credentials=withdrawal_credentials, amount=amount
    )
    signature = sign_proof_of_possession(deposit_message=message, privkey=private_key)
    return DepositData.create(
        pubkey=public_key,
        withdrawal_credentials=withdrawal_credentials,
        amount=amount,
        signature=signature,
    )
Пример #8
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
Пример #9
0
def deposit(w3, deposit_contract) -> int:
    amount = get_random_valid_deposit_amount()
    deposit_input = (
        SAMPLE_PUBKEY,
        SAMPLE_WITHDRAWAL_CREDENTIALS,
        SAMPLE_VALID_SIGNATURE,
        ssz.get_hash_tree_root(
            DepositData(
                pubkey=SAMPLE_PUBKEY,
                withdrawal_credentials=SAMPLE_WITHDRAWAL_CREDENTIALS,
                amount=amount,
                signature=SAMPLE_VALID_SIGNATURE,
            )),
    )
    tx_hash = deposit_contract.functions.deposit(*deposit_input).transact(
        {"value": amount * eth_utils.denoms.gwei})
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    assert tx_receipt["status"]
    return amount
Пример #10
0
def sample_deposit_params(sample_deposit_data_params):
    return {
        'branch': (),
        'index': 5,
        'deposit_data': DepositData(**sample_deposit_data_params)
    }
Пример #11
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)
Пример #12
0
def sample_deposit_params(sample_deposit_data_params, deposit_contract_tree_depth):
    return {
        "proof": (b"\x22" * 32,) * (deposit_contract_tree_depth + 1),
        "data": DepositData.create(**sample_deposit_data_params),
    }
Пример #13
0
def test_defaults(sample_deposit_data_params):
    deposit_data = DepositData(**sample_deposit_data_params)

    assert deposit_data.pubkey == sample_deposit_data_params["pubkey"]
    assert deposit_data.amount == sample_deposit_data_params["amount"]
Пример #14
0
def test_defaults(sample_deposit_data_params):
    deposit_data = DepositData(**sample_deposit_data_params)

    assert deposit_data.deposit_input.pubkey == sample_deposit_data_params['deposit_input'].pubkey
    assert deposit_data.amount == sample_deposit_data_params['amount']
    assert deposit_data.timestamp == sample_deposit_data_params['timestamp']
Пример #15
0
def sample_deposit_params(sample_deposit_data_params):
    return {
        'merkle_branch': (),
        'merkle_tree_index': 5,
        'deposit_data': DepositData(**sample_deposit_data_params)
    }
Пример #16
0
def sample_deposit_params(sample_deposit_data_params,
                          deposit_contract_tree_depth):
    return {
        'proof': (b'\x22' * 32, ) * (deposit_contract_tree_depth + 1),
        'data': DepositData(**sample_deposit_data_params)
    }
Пример #17
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)