예제 #1
0
def create_mock_genesis_state_from_validators(
        genesis_time: Timestamp, genesis_eth1_data: Eth1Data,
        genesis_validators: Sequence[Validator],
        genesis_balances: Sequence[Gwei], config: Eth2Config) -> BeaconState:
    """
    Produce a valid genesis state without creating the
    corresponding deposits.

    Compare with ``eth2.beacon.genesis.get_genesis_beacon_state``.
    """
    # NOTE: does not handle nondistinct pubkeys at the moment
    _check_distinct_pubkeys(genesis_validators)
    _check_no_missing_balances(genesis_validators, genesis_balances)
    _check_sufficient_balance(genesis_balances, config.MAX_EFFECTIVE_BALANCE)
    _check_activated_validators(genesis_validators, config.GENESIS_EPOCH)
    _check_correct_eth1_data(genesis_eth1_data, genesis_validators)

    empty_state = get_genesis_beacon_state(
        genesis_deposits=tuple(),
        genesis_time=genesis_time,
        genesis_eth1_data=genesis_eth1_data,
        config=config,
    )

    state_with_validators = empty_state.copy(
        eth1_deposit_index=empty_state.eth1_deposit_index +
        len(genesis_validators),
        validators=genesis_validators,
        balances=genesis_balances,
    )

    return genesis_state_with_active_index_roots(
        state_with_validators,
        config,
    )
예제 #2
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
예제 #3
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)
예제 #4
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)