Beispiel #1
0
def sample_beacon_state_params(
    config,
    genesis_slot,
    genesis_epoch,
    sample_fork_params,
    sample_eth1_data_params,
    sample_block_header_params,
    sample_crosslink_record_params,
):
    return {
        # Versioning
        "genesis_time":
        0,
        "slot":
        genesis_slot + 100,
        "fork":
        Fork(**sample_fork_params),
        # History
        "latest_block_header":
        BeaconBlockHeader(**sample_block_header_params),
        "block_roots": (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        "state_roots": (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        "historical_roots": (),
        # Eth1
        "eth1_data":
        Eth1Data(**sample_eth1_data_params),
        "eth1_data_votes": (),
        "eth1_deposit_index":
        0,
        # Registry
        "validators": (),
        "balances": (),
        # Shuffling
        "start_shard":
        1,
        "randao_mixes": (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR,
        "active_index_roots":
        (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR,
        "compact_committees_roots":
        (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR,
        # Slashings
        "slashings": (0, ) * config.EPOCHS_PER_SLASHINGS_VECTOR,
        # Attestations
        "previous_epoch_attestations": (),
        "current_epoch_attestations": (),
        # Crosslinks
        "previous_crosslinks":
        ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT),
        "current_crosslinks":
        ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT),
        # Justification
        "justification_bits": (False, ) * JUSTIFICATION_BITS_LENGTH,
        "previous_justified_checkpoint":
        Checkpoint(epoch=0, root=b"\x99" * 32),
        "current_justified_checkpoint":
        Checkpoint(epoch=0, root=b"\x55" * 32),
        # Finality
        "finalized_checkpoint":
        Checkpoint(epoch=0, root=b"\x33" * 32),
    }
Beispiel #2
0
def sample_beacon_state_params(sample_fork_params, sample_eth1_data_params):
    return {
        'slot': 0,
        'genesis_time': 0,
        'fork': Fork(**sample_fork_params),
        'validator_registry': (),
        'validator_balances': (),
        'validator_registry_update_epoch': 0,
        'latest_randao_mixes': (),
        'previous_epoch_start_shard': 1,
        'current_epoch_start_shard': 2,
        'previous_calculation_epoch': 0,
        'current_calculation_epoch': 0,
        'previous_epoch_seed': b'\x77' * 32,
        'current_epoch_seed': b'\x88' * 32,
        'previous_justified_epoch': 0,
        'justified_epoch': 0,
        'justification_bitfield': 0,
        'finalized_epoch': 0,
        'latest_crosslinks': (),
        'latest_block_roots': (),
        'latest_index_roots': (),
        'latest_penalized_balances': (),
        'latest_attestations': (),
        'batched_block_roots': (),
        'latest_eth1_data': Eth1Data(**sample_eth1_data_params),
        'eth1_data_votes': (),
        'deposit_index': 0,
    }
Beispiel #3
0
def sample_beacon_state_params(config, genesis_slot, genesis_epoch,
                               sample_fork_params, sample_eth1_data_params,
                               sample_block_header_params,
                               sample_crosslink_record_params):
    return {
        # Versioning
        'genesis_time':
        0,
        'slot':
        genesis_slot + 100,
        'fork':
        Fork(**sample_fork_params),
        # History
        'latest_block_header':
        BeaconBlockHeader(**sample_block_header_params),
        'block_roots': (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        'state_roots': (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        'historical_roots': (),
        # Eth1
        'eth1_data':
        Eth1Data(**sample_eth1_data_params),
        'eth1_data_votes': (),
        'eth1_deposit_index':
        0,
        # Registry
        'validators': (),
        'balances': (),
        # Shuffling
        'start_shard':
        1,
        'randao_mixes': (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR,
        'active_index_roots':
        (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR,
        # Slashings
        'slashed_balances': (0, ) * config.EPOCHS_PER_SLASHED_BALANCES_VECTOR,
        # Attestations
        'previous_epoch_attestations': (),
        'current_epoch_attestations': (),
        # Crosslinks
        'previous_crosslinks':
        ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT),
        'current_crosslinks':
        ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT),
        # Justification
        'previous_justified_epoch':
        0,
        'previous_justified_root':
        b'\x99' * 32,
        'current_justified_epoch':
        0,
        'current_justified_root':
        b'\x55' * 32,
        'justification_bitfield':
        0,
        # Finality
        'finalized_epoch':
        0,
        'finalized_root':
        b'\x33' * 32,
    }
Beispiel #4
0
def create_mock_genesis(
    num_validators: int,
    config: Eth2Config,
    keymap: Dict[BLSPubkey, int],
    genesis_block_class: Type[BaseBeaconBlock],
    genesis_time: Timestamp = ZERO_TIMESTAMP
) -> Tuple[BeaconState, BaseBeaconBlock]:
    assert num_validators <= len(keymap)

    genesis_deposits, deposit_root = create_mock_deposits_and_root(
        pubkeys=list(keymap)[:num_validators],
        keymap=keymap,
        config=config,
    )

    genesis_eth1_data = Eth1Data(
        deposit_root=deposit_root,
        deposit_count=len(genesis_deposits),
        block_hash=ZERO_HASH32,
    )

    state = get_genesis_beacon_state(
        genesis_deposits=genesis_deposits,
        genesis_time=genesis_time,
        genesis_eth1_data=genesis_eth1_data,
        config=config,
    )

    block = get_genesis_block(
        genesis_state_root=state.root,
        block_class=genesis_block_class,
    )
    assert len(state.validators) == num_validators

    return state, block
Beispiel #5
0
def create_mock_genesis(
    pubkeys: Sequence[BLSPubkey],
    config: Eth2Config,
    keymap: Dict[BLSPubkey, int],
    genesis_block_class: Type[BaseBeaconBlock],
    genesis_time: Timestamp = ZERO_TIMESTAMP,
) -> Tuple[BeaconState, BaseBeaconBlock]:
    genesis_deposits, deposit_root = create_mock_deposits_and_root(
        pubkeys=pubkeys, keymap=keymap, config=config)

    genesis_eth1_data = Eth1Data(
        deposit_root=deposit_root,
        deposit_count=len(genesis_deposits),
        block_hash=ZERO_HASH32,
    )

    state = initialize_beacon_state_from_eth1(
        eth1_block_hash=genesis_eth1_data.block_hash,
        eth1_timestamp=genesis_time,
        deposits=genesis_deposits,
        config=config,
    )

    block = get_genesis_block(genesis_state_root=state.hash_tree_root,
                              block_class=genesis_block_class)
    assert len(state.validators) == len(pubkeys)

    return state, block
Beispiel #6
0
def sample_beacon_state_params(sample_fork_params, sample_eth1_data_params):
    return {
        'slot': 0,
        'genesis_time': 0,
        'fork': Fork(**sample_fork_params),
        'validator_registry': (),
        'validator_balances': (),
        'validator_registry_update_slot': 10,
        'validator_registry_exit_count': 10,
        'latest_randao_mixes': (),
        'latest_vdf_outputs': (),
        'persistent_committees': (),
        'persistent_committee_reassignments': (),
        'previous_epoch_start_shard': 1,
        'current_epoch_start_shard': 2,
        'previous_epoch_calculation_slot': 5,
        'current_epoch_calculation_slot': 10,
        'previous_epoch_seed': b'\x77' * 32,
        'current_epoch_seed': b'\x88' * 32,
        'custody_challenges': (),
        'previous_justified_slot': 0,
        'justified_slot': 0,
        'justification_bitfield': 0,
        'finalized_slot': 0,
        'latest_crosslinks': (),
        'latest_block_roots': (),
        'latest_index_roots': (),
        'latest_penalized_balances': (),
        'latest_attestations': (),
        'batched_block_roots': (),
        'latest_eth1_data': Eth1Data(**sample_eth1_data_params),
        'eth1_data_votes': (),
    }
Beispiel #7
0
def test_ensure_update_eth1_vote_if_exists(genesis_state,
                                           config,
                                           vote_offsets):
    # one less than a majority is the majority divided by 2
    threshold = config.SLOTS_PER_ETH1_VOTING_PERIOD // 2
    data_votes = tuple(
        concat(
            (
                Eth1Data(
                    block_hash=(i).to_bytes(32, "little"),
                ),
            ) * (threshold + offset)
            for i, offset in enumerate(vote_offsets)
        )
    )
    state = genesis_state

    for vote in data_votes:
        state = process_eth1_data(state, BeaconBlock(
            body=BeaconBlockBody(
                eth1_data=vote,
            )
        ), config)

    if not vote_offsets:
        assert state.eth1_data == genesis_state.eth1_data

    # we should update the 'latest' entry if we have a majority
    for offset in vote_offsets:
        if offset <= 0:
            assert genesis_state.eth1_data == state.eth1_data
        else:
            assert state.eth1_data == data_votes[0]
Beispiel #8
0
def test_process_eth1_data(original_votes,
                           block_data,
                           expected_votes,
                           sample_beacon_state_params,
                           sample_beacon_block_params,
                           sample_beacon_block_body_params,
                           config):
    eth1_data_votes = tuple(mapcat(
        _expand_eth1_votes,
        original_votes,
    ))
    state = BeaconState(**sample_beacon_state_params).copy(
        eth1_data_votes=eth1_data_votes,
    )

    block_body = BeaconBlockBody(**sample_beacon_block_body_params).copy(
        eth1_data=Eth1Data(
            block_hash=block_data,
        ),
    )

    block = BeaconBlock(**sample_beacon_block_params).copy(
        body=block_body,
    )

    updated_state = process_eth1_data(state, block, config)
    updated_votes = updated_state.eth1_data_votes
    expanded_expected_votes = tuple(mapcat(
        _expand_eth1_votes,
        expected_votes,
    ))

    assert updated_votes == expanded_expected_votes
Beispiel #9
0
def genesis_state(genesis_validators, genesis_balances, genesis_time,
                  sample_eth1_data_params, config):
    genesis_eth1_data = Eth1Data(**sample_eth1_data_params).copy(
        deposit_count=len(genesis_validators))

    return create_mock_genesis_state_from_validators(genesis_time,
                                                     genesis_eth1_data,
                                                     genesis_validators,
                                                     genesis_balances, config)
Beispiel #10
0
def sample_beacon_block_body_params(sample_signature, sample_eth1_data_params):
    return {
        "randao_reveal": sample_signature,
        "eth1_data": Eth1Data(**sample_eth1_data_params),
        "graffiti": ZERO_HASH32,
        "proposer_slashings": (),
        "attester_slashings": (),
        "attestations": (),
        "deposits": (),
        "voluntary_exits": (),
    }
Beispiel #11
0
def sample_beacon_block_body_params(sample_eth1_data_params):
    return {
        'randao_reveal': SAMPLE_SIGNATURE,
        'eth1_data': Eth1Data(**sample_eth1_data_params),
        'proposer_slashings': (),
        'attester_slashings': (),
        'attestations': (),
        'deposits': (),
        'voluntary_exits': (),
        'transfers': (),
    }
Beispiel #12
0
def sample_beacon_block_params(sample_beacon_block_body_params,
                               sample_eth1_data_params):
    return {
        'slot': 10,
        'parent_root': ZERO_HASH32,
        'state_root': b'\x55' * 32,
        'randao_reveal': EMPTY_SIGNATURE,
        'eth1_data': Eth1Data(**sample_eth1_data_params),
        'signature': EMPTY_SIGNATURE,
        'body': BeaconBlockBody(**sample_beacon_block_body_params)
    }
Beispiel #13
0
def sample_beacon_block_body_params(sample_signature, sample_eth1_data_params):
    return {
        'randao_reveal': sample_signature,
        'eth1_data': Eth1Data(**sample_eth1_data_params),
        'graffiti': ZERO_HASH32,
        'proposer_slashings': (),
        'attester_slashings': (),
        'attestations': (),
        'deposits': (),
        'voluntary_exits': (),
        'transfers': (),
    }
Beispiel #14
0
def create_mock_genesis(
        num_validators: int,
        config: Eth2Config,
        keymap: Dict[BLSPubkey, int],
        genesis_block_class: Type[BaseBeaconBlock],
        genesis_time: Timestamp=ZERO_TIMESTAMP) -> Tuple[BeaconState, BaseBeaconBlock]:
    assert num_validators <= len(keymap)

    pubkeys = list(keymap)[:num_validators]

    genesis_validator_deposits, deposit_root = create_mock_genesis_validator_deposits_and_root(
        num_validators=num_validators,
        config=config,
        pubkeys=pubkeys,
        keymap=keymap,
    )

    genesis_eth1_data = Eth1Data(
        deposit_root=deposit_root,
        block_hash=ZERO_HASH32,
    )

    state = get_genesis_beacon_state(
        genesis_validator_deposits=genesis_validator_deposits,
        genesis_time=genesis_time,
        genesis_eth1_data=genesis_eth1_data,
        genesis_slot=config.GENESIS_SLOT,
        genesis_epoch=config.GENESIS_EPOCH,
        genesis_fork_version=config.GENESIS_FORK_VERSION,
        genesis_start_shard=config.GENESIS_START_SHARD,
        shard_count=config.SHARD_COUNT,
        min_seed_lookahead=config.MIN_SEED_LOOKAHEAD,
        slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT,
        latest_active_index_roots_length=config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH,
        slots_per_epoch=config.SLOTS_PER_EPOCH,
        max_deposit_amount=config.MAX_DEPOSIT_AMOUNT,
        latest_slashed_exit_length=config.LATEST_SLASHED_EXIT_LENGTH,
        latest_randao_mixes_length=config.LATEST_RANDAO_MIXES_LENGTH,
        activation_exit_delay=config.ACTIVATION_EXIT_DELAY,
        deposit_contract_tree_depth=config.DEPOSIT_CONTRACT_TREE_DEPTH,
        block_class=genesis_block_class,
    )

    block = get_genesis_block(
        genesis_state_root=state.root,
        genesis_slot=config.GENESIS_SLOT,
        block_class=genesis_block_class,
    )
    assert len(state.validator_registry) == num_validators

    return state, block
Beispiel #15
0
    def _get_eth1_data(
            self, distance: BlockNumber,
            eth1_voting_period_start_timestamp: Timestamp) -> Eth1Data:
        """
        Return `Eth1Data` at `distance` relative to the eth1 block earlier and closest to the
        timestamp `eth1_voting_period_start_timestamp`.
        Ref: https://github.com/ethereum/eth2.0-specs/blob/61f2a0662ebcfb4c097360cc1835c5f01872705c/specs/validator/0_beacon-chain-validator.md#eth1-data  # noqa: E501

        First, we find the `eth1_block` whose timestamp is the largest timestamp which is smaller
        than `eth1_voting_period_start_timestamp`. Then, find the block `target_block` at number
        `eth1_block.number - distance`. Therefore, we can return `Eth1Data` according to the
        information of this block.
        """
        eth1_voting_period_start_block_number = self._get_closest_eth1_voting_period_start_block(
            eth1_voting_period_start_timestamp)
        target_block_number = BlockNumber(
            eth1_voting_period_start_block_number - distance)
        if target_block_number < 0:
            raise Eth1MonitorValidationError(
                f"`distance` is larger than `eth1_voting_period_start_block_number`: "
                f"`distance`={distance}, ",
                f"eth1_voting_period_start_block_number={eth1_voting_period_start_block_number}",
            )
        block_hash = _w3_get_block(self._w3, target_block_number).block_hash
        # `Eth1Data.deposit_count`: get the `deposit_count` corresponding to the block.
        accumulated_deposit_count = self._get_accumulated_deposit_count(
            target_block_number)
        if accumulated_deposit_count == 0:
            raise Eth1MonitorValidationError(
                f"failed to make `Eth1Data`: `deposit_count = 0` at block #{target_block_number}"
            )
        deposit_data_in_range = self._db.get_deposit_data_range(
            0, accumulated_deposit_count)
        _, deposit_root = make_deposit_tree_and_root(deposit_data_in_range)
        contract_deposit_root = self._get_deposit_root_from_contract(
            target_block_number)
        if contract_deposit_root != deposit_root:
            raise DepositDataCorrupted(
                "deposit root built locally mismatches the one in the contract on chain: "
                f"contract_deposit_root={contract_deposit_root.hex()}, "
                f"deposit_root={deposit_root.hex()}")
        return Eth1Data(
            deposit_root=deposit_root,
            deposit_count=accumulated_deposit_count,
            block_hash=block_hash,
        )
Beispiel #16
0
def initialize_beacon_state_from_eth1(*, eth1_block_hash: Hash32,
                                      eth1_timestamp: Timestamp,
                                      deposits: Sequence[Deposit],
                                      config: Eth2Config) -> BeaconState:
    state = BeaconState(
        genesis_time=_genesis_time_from_eth1_timestamp(eth1_timestamp),
        eth1_data=Eth1Data(block_hash=eth1_block_hash,
                           deposit_count=len(deposits)),
        latest_block_header=BeaconBlockHeader(
            body_root=BeaconBlockBody().hash_tree_root),
        randao_mixes=(eth1_block_hash, ) * config.EPOCHS_PER_HISTORICAL_VECTOR,
        config=config,
    )

    # Process genesis deposits
    for index, deposit in enumerate(deposits):
        deposit_data_list = tuple(deposit.data
                                  for deposit in deposits[:index + 1])
        state = state.copy(eth1_data=state.eth1_data.copy(
            deposit_root=ssz.get_hash_tree_root(
                deposit_data_list,
                ssz.List(DepositData, 2**DEPOSIT_CONTRACT_TREE_DEPTH),
            )))
        state = process_deposit(state=state, deposit=deposit, config=config)

    # Process genesis activations
    for validator_index in range(len(state.validators)):
        validator_index = ValidatorIndex(validator_index)
        balance = state.balances[validator_index]
        effective_balance = calculate_effective_balance(balance, config)

        state = state.update_validator_with_fn(
            validator_index,
            lambda v, *_: v.copy(effective_balance=effective_balance))

        if effective_balance == config.MAX_EFFECTIVE_BALANCE:
            state = state.update_validator_with_fn(validator_index,
                                                   activate_validator,
                                                   config.GENESIS_EPOCH)

    return state
Beispiel #17
0
def sample_beacon_state_params(genesis_slot,
                               genesis_epoch,
                               sample_fork_params,
                               sample_eth1_data_params,
                               sample_block_header_params):
    return {
        'slot': genesis_slot + 100,
        'genesis_time': 0,
        'fork': Fork(**sample_fork_params),
        'validator_registry': (),
        'validator_balances': (),
        'validator_registry_update_epoch': 0,
        'latest_randao_mixes': (),
        'previous_shuffling_start_shard': 1,
        'current_shuffling_start_shard': 2,
        'previous_shuffling_epoch': genesis_epoch,
        'current_shuffling_epoch': genesis_epoch,
        'previous_shuffling_seed': b'\x77' * 32,
        'current_shuffling_seed': b'\x88' * 32,
        'previous_epoch_attestations': (),
        'current_epoch_attestations': (),
        'previous_justified_epoch': 0,
        'current_justified_epoch': 0,
        'previous_justified_root': b'\x99' * 32,
        'current_justified_root': b'\x55' * 32,
        'justification_bitfield': 0,
        'finalized_epoch': 0,
        'finalized_root': b'\x33' * 32,
        'latest_crosslinks': (),
        'latest_block_roots': (),
        'latest_state_roots': (),
        'latest_active_index_roots': (),
        'latest_slashed_balances': (),
        'latest_block_header': BeaconBlockHeader(**sample_block_header_params),
        'historical_roots': (),
        'latest_eth1_data': Eth1Data(**sample_eth1_data_params),
        'eth1_data_votes': (),
        'deposit_index': 0,
    }
Beispiel #18
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)
Beispiel #19
0
def test_get_genesis_beacon_state(validator_count, pubkeys, genesis_epoch,
                                  genesis_slot, shard_count,
                                  slots_per_historical_root,
                                  epochs_per_slashed_balances_vector,
                                  epochs_per_historical_vector, config,
                                  keymap):
    genesis_deposits, deposit_root = create_mock_deposits_and_root(
        pubkeys=pubkeys[:validator_count],
        keymap=keymap,
        config=config,
    )

    genesis_eth1_data = Eth1Data(
        deposit_count=len(genesis_deposits),
        deposit_root=deposit_root,
        block_hash=ZERO_HASH32,
    )
    genesis_time = 10

    state = get_genesis_beacon_state(
        genesis_deposits=genesis_deposits,
        genesis_time=genesis_time,
        genesis_eth1_data=genesis_eth1_data,
        config=config,
    )

    # Versioning
    assert state.slot == genesis_slot
    assert state.genesis_time == genesis_time
    assert state.fork == Fork()

    # History
    assert state.latest_block_header == BeaconBlockHeader(
        body_root=BeaconBlockBody().root, )
    assert len(state.block_roots) == slots_per_historical_root
    assert state.block_roots == (ZERO_HASH32, ) * slots_per_historical_root
    assert len(state.state_roots) == slots_per_historical_root
    assert state.block_roots == (ZERO_HASH32, ) * slots_per_historical_root
    assert len(state.historical_roots) == 0

    # Ethereum 1.0 chain data
    assert state.eth1_data == genesis_eth1_data
    assert len(state.eth1_data_votes) == 0
    assert state.eth1_deposit_index == len(genesis_deposits)

    # Validator registry
    assert len(state.validators) == validator_count
    assert len(state.balances) == validator_count

    # Shuffling
    assert state.start_shard == 0
    assert len(state.randao_mixes) == epochs_per_historical_vector
    assert state.randao_mixes == (ZERO_HASH32, ) * epochs_per_historical_vector
    assert len(state.active_index_roots) == epochs_per_historical_vector

    # Slashings
    assert len(state.slashed_balances) == epochs_per_slashed_balances_vector
    assert state.slashed_balances == (
        Gwei(0), ) * epochs_per_slashed_balances_vector

    # Attestations
    assert len(state.previous_epoch_attestations) == 0
    assert len(state.current_epoch_attestations) == 0

    # Crosslinks
    assert len(state.current_crosslinks) == shard_count
    assert state.current_crosslinks == (Crosslink(), ) * shard_count
    assert len(state.previous_crosslinks) == shard_count
    assert state.previous_crosslinks == (Crosslink(), ) * shard_count

    # Justification
    assert state.previous_justified_epoch == genesis_epoch
    assert state.previous_justified_root == ZERO_HASH32
    assert state.current_justified_epoch == genesis_epoch
    assert state.current_justified_root == ZERO_HASH32
    assert state.justification_bitfield == 0

    # Finalization
    assert state.finalized_epoch == genesis_epoch
    assert state.finalized_root == ZERO_HASH32

    for i in range(len(genesis_deposits)):
        assert state.validators[i].is_active(genesis_epoch)
Beispiel #20
0
def sample_eth1_data_vote_params(sample_eth1_data_params):
    return {
        'eth1_data': Eth1Data(**sample_eth1_data_params),
        'vote_count': 10,
    }
def _expand_eth1_votes(args):
    block_hash, vote_count = args
    return (Eth1Data(block_hash=block_hash), ) * vote_count
Beispiel #22
0
def test_get_genesis_beacon_state(
    validator_count,
    pubkeys,
    genesis_epoch,
    genesis_slot,
    max_committees_per_slot,
    slots_per_historical_root,
    epochs_per_slashings_vector,
    epochs_per_historical_vector,
    config,
    keymap,
):
    genesis_deposits, deposit_root = create_mock_deposits_and_root(
        pubkeys=pubkeys[:validator_count], keymap=keymap, config=config
    )

    genesis_eth1_data = Eth1Data(
        deposit_count=len(genesis_deposits),
        deposit_root=deposit_root,
        block_hash=ZERO_HASH32,
    )
    eth1_timestamp = 10
    eth1_block_hash = genesis_eth1_data.block_hash

    state = initialize_beacon_state_from_eth1(
        eth1_block_hash=eth1_block_hash,
        eth1_timestamp=eth1_timestamp,
        deposits=genesis_deposits,
        config=config,
    )

    # Versioning
    assert state.slot == genesis_slot
    assert state.genesis_time == _genesis_time_from_eth1_timestamp(eth1_timestamp)
    assert state.fork == Fork()

    # History
    assert state.latest_block_header == BeaconBlockHeader(
        body_root=BeaconBlockBody().hash_tree_root
    )
    assert len(state.block_roots) == slots_per_historical_root
    assert state.block_roots == (ZERO_HASH32,) * slots_per_historical_root
    assert len(state.state_roots) == slots_per_historical_root
    assert state.block_roots == (ZERO_HASH32,) * slots_per_historical_root
    assert len(state.historical_roots) == 0

    # Ethereum 1.0 chain data
    assert state.eth1_data == genesis_eth1_data
    assert len(state.eth1_data_votes) == 0
    assert state.eth1_deposit_index == len(genesis_deposits)

    # Validator registry
    assert len(state.validators) == validator_count
    assert len(state.balances) == validator_count

    # Shuffling
    assert len(state.randao_mixes) == epochs_per_historical_vector
    assert state.randao_mixes == (eth1_block_hash,) * epochs_per_historical_vector

    # Slashings
    assert len(state.slashings) == epochs_per_slashings_vector
    assert state.slashings == (Gwei(0),) * epochs_per_slashings_vector

    # Attestations
    assert len(state.previous_epoch_attestations) == 0
    assert len(state.current_epoch_attestations) == 0

    # Justification
    assert state.previous_justified_checkpoint.epoch == genesis_epoch
    assert state.previous_justified_checkpoint.root == ZERO_HASH32
    assert state.current_justified_checkpoint.epoch == genesis_epoch
    assert state.current_justified_checkpoint.root == ZERO_HASH32
    assert state.justification_bits == (False,) * JUSTIFICATION_BITS_LENGTH

    # Finalization
    assert state.finalized_checkpoint.epoch == genesis_epoch
    assert state.finalized_checkpoint.root == ZERO_HASH32

    for i in range(len(genesis_deposits)):
        assert state.validators[i].is_active(genesis_epoch)
Beispiel #23
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)
Beispiel #24
0
def sample_beacon_state_params(config, genesis_slot, genesis_epoch,
                               sample_fork_params, sample_eth1_data_params,
                               sample_block_header_params,
                               sample_crosslink_record_params):
    return {
        'slot':
        genesis_slot + 100,
        'genesis_time':
        0,
        'fork':
        Fork(**sample_fork_params),
        'validator_registry': (),
        'validator_balances': (),
        'validator_registry_update_epoch':
        0,
        'latest_randao_mixes':
        (ZERO_HASH32, ) * config.LATEST_RANDAO_MIXES_LENGTH,
        'previous_shuffling_start_shard':
        1,
        'current_shuffling_start_shard':
        2,
        'previous_shuffling_epoch':
        genesis_epoch,
        'current_shuffling_epoch':
        genesis_epoch,
        'previous_shuffling_seed':
        b'\x77' * 32,
        'current_shuffling_seed':
        b'\x88' * 32,
        'previous_epoch_attestations': (),
        'current_epoch_attestations': (),
        'previous_justified_epoch':
        0,
        'current_justified_epoch':
        0,
        'previous_justified_root':
        b'\x99' * 32,
        'current_justified_root':
        b'\x55' * 32,
        'justification_bitfield':
        0,
        'finalized_epoch':
        0,
        'finalized_root':
        b'\x33' * 32,
        'latest_crosslinks':
        ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT),
        'latest_block_roots':
        (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        'latest_state_roots':
        (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        'latest_active_index_roots':
        (ZERO_HASH32, ) * config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH,
        'latest_slashed_balances': (0, ) * config.LATEST_SLASHED_EXIT_LENGTH,
        'latest_block_header':
        BeaconBlockHeader(**sample_block_header_params),
        'historical_roots': (),
        'latest_eth1_data':
        Eth1Data(**sample_eth1_data_params),
        'eth1_data_votes': (),
        'deposit_index':
        0,
    }
Beispiel #25
0
def test_defaults(sample_eth1_data_params):
    eth1_data = Eth1Data(
        **sample_eth1_data_params,
    )
    assert eth1_data.deposit_root == sample_eth1_data_params['deposit_root']
    assert eth1_data.block_hash == sample_eth1_data_params['block_hash']
Beispiel #26
0
def test_defaults(sample_eth1_data_params):
    eth1_data = Eth1Data(**sample_eth1_data_params)
    assert eth1_data.deposit_root == sample_eth1_data_params["deposit_root"]
    assert eth1_data.block_hash == sample_eth1_data_params["block_hash"]
Beispiel #27
0
def test_get_genesis_beacon_state(num_validators, pubkeys, genesis_epoch,
                                  genesis_slot, genesis_fork_version,
                                  genesis_start_shard, shard_count,
                                  slots_per_historical_root,
                                  latest_slashed_exit_length,
                                  latest_randao_mixes_length, config, keymap):
    validator_count = 5

    genesis_validator_deposits, deposit_root = create_mock_genesis_validator_deposits_and_root(
        num_validators=validator_count,
        config=config,
        pubkeys=pubkeys,
        keymap=keymap,
    )

    genesis_eth1_data = Eth1Data(
        deposit_root=deposit_root,
        block_hash=ZERO_HASH32,
    )
    genesis_time = 10

    state = get_genesis_beacon_state(
        genesis_validator_deposits=genesis_validator_deposits,
        genesis_time=genesis_time,
        genesis_eth1_data=genesis_eth1_data,
        config=config,
    )

    # Misc
    assert state.slot == genesis_slot
    assert state.genesis_time == genesis_time
    assert state.fork.previous_version == genesis_fork_version.to_bytes(
        4, 'little')
    assert state.fork.current_version == genesis_fork_version.to_bytes(
        4, 'little')
    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 len(state.previous_epoch_attestations) == 0
    assert len(state.current_epoch_attestations) == 0
    assert state.previous_justified_epoch == genesis_epoch
    assert state.current_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] == Crosslink(
        epoch=genesis_epoch,
        crosslink_data_root=ZERO_HASH32,
    )
    assert len(state.latest_block_roots) == slots_per_historical_root
    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.historical_roots) == 0

    # Ethereum 1.0 chain data
    assert state.latest_eth1_data == genesis_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)