Example #1
0
def create_block_header_with_signature(
    state: BeaconState,
    body_root: Hash32,
    privkey: int,
    slots_per_epoch: int,
    parent_root: Root = SAMPLE_HASH_1,
    state_root: Hash32 = SAMPLE_HASH_2,
) -> SignedBeaconBlockHeader:
    block_header = BeaconBlockHeader.create(
        slot=state.slot,
        parent_root=parent_root,
        state_root=state_root,
        body_root=body_root,
    )
    block_header_signature = sign_transaction(
        message_hash=block_header.hash_tree_root,
        privkey=privkey,
        state=state,
        slot=block_header.slot,
        signature_domain=SignatureDomain.DOMAIN_BEACON_PROPOSER,
        slots_per_epoch=slots_per_epoch,
    )
    return SignedBeaconBlockHeader.create(
        message=block_header, signature=block_header_signature
    )
Example #2
0
def create_block_header_with_signature(
    state: BeaconState,
    body_root: Root,
    privkey: int,
    slots_per_epoch: int,
    proposer_index: ValidatorIndex,
    parent_root: Root = SAMPLE_HASH_1,
    state_root: Root = SAMPLE_HASH_2,
) -> SignedBeaconBlockHeader:
    block_header = BeaconBlockHeader.create(
        slot=Slot(state.slot),
        proposer_index=proposer_index,
        parent_root=parent_root,
        state_root=state_root,
        body_root=body_root,
    )
    block_header_signature = sign_transaction(
        object=block_header,
        privkey=privkey,
        state=state,
        slot=block_header.slot,
        signature_domain=SignatureDomain.DOMAIN_BEACON_PROPOSER,
        slots_per_epoch=slots_per_epoch,
    )
    return SignedBeaconBlockHeader.create(message=block_header,
                                          signature=block_header_signature)
Example #3
0
def initialize_beacon_state_from_eth1(*, eth1_block_hash: Hash32,
                                      eth1_timestamp: Timestamp,
                                      deposits: Sequence[Deposit],
                                      config: Eth2Config) -> BeaconState:
    fork = Fork.create(
        previous_version=config.GENESIS_FORK_VERSION,
        current_version=config.GENESIS_FORK_VERSION,
        epoch=GENESIS_EPOCH,
    )

    state = BeaconState.create(
        genesis_time=_genesis_time_from_eth1_timestamp(eth1_timestamp,
                                                       config.GENESIS_DELAY),
        fork=fork,
        eth1_data=Eth1Data.create(block_hash=eth1_block_hash,
                                  deposit_count=len(deposits)),
        latest_block_header=BeaconBlockHeader.create(
            body_root=BeaconBlockBody.create().hash_tree_root),
        block_roots=(ZERO_ROOT, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        state_roots=(ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT,
        randao_mixes=(eth1_block_hash, ) * config.EPOCHS_PER_HISTORICAL_VECTOR,
        slashings=(Gwei(0), ) * config.EPOCHS_PER_SLASHINGS_VECTOR,
        config=config,
    )

    # Process genesis deposits
    for index, deposit in enumerate(deposits):
        deposit_data_list = tuple(deposit.data
                                  for deposit in deposits[:index + 1])
        deposit_root = ssz.get_hash_tree_root(
            deposit_data_list,
            ssz.List(DepositData, 2**DEPOSIT_CONTRACT_TREE_DEPTH))
        state = state.transform(("eth1_data", "deposit_root"), deposit_root)
        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.transform(
            ("validators", validator_index, "effective_balance"),
            effective_balance)

        if effective_balance == config.MAX_EFFECTIVE_BALANCE:
            activated_validator = activate_validator(
                state.validators[validator_index], GENESIS_EPOCH)
            state = state.transform(("validators", validator_index),
                                    activated_validator)

    return state.set("genesis_validators_root",
                     state.validators.hash_tree_root)
Example #4
0
def process_block_header(state: BeaconState, block: BaseBeaconBlock,
                         config: Eth2Config) -> BeaconState:
    validate_block_slot(state, block)
    validate_block_parent_root(state, block)
    validate_proposer_is_not_slashed(state, block.hash_tree_root, config)

    return state.set(
        "latest_block_header",
        BeaconBlockHeader.create(
            slot=block.slot,
            parent_root=block.parent_root,
            # `state_root` is zeroed and overwritten in the next `process_slot` call
            body_root=block.body.hash_tree_root,
            # `signature` is zeroed
        ),
    )
Example #5
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
Example #6
0
def process_block_header(
    state: BeaconState,
    block: BaseBeaconBlock,
    config: Eth2Config,
    check_proposer_signature: bool,
) -> BeaconState:
    validate_block_slot(state, block)
    validate_block_parent_root(state, block)
    validate_proposer_is_not_slashed(state, block.signing_root,
                                     CommitteeConfig(config))

    if check_proposer_signature:
        validate_proposer_signature(state,
                                    block,
                                    committee_config=CommitteeConfig(config))

    return state.copy(latest_block_header=BeaconBlockHeader(
        slot=block.slot,
        parent_root=block.parent_root,
        body_root=block.body.hash_tree_root,
    ))
Example #7
0
def get_genesis_beacon_state(*,
                             genesis_deposits: Sequence[Deposit],
                             genesis_time: Timestamp,
                             genesis_eth1_data: Eth1Data,
                             config: Eth2Config) -> BeaconState:
    state = BeaconState(
        genesis_time=genesis_time,
        eth1_data=genesis_eth1_data,
        latest_block_header=BeaconBlockHeader(
            body_root=BeaconBlockBody().root,
        ),
        config=config,
    )

    # Process genesis deposits
    for deposit in genesis_deposits:
        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)
        effective_balance = state.validators[validator_index].effective_balance
        is_enough_effective_balance = effective_balance >= config.MAX_EFFECTIVE_BALANCE
        if is_enough_effective_balance:
            state = state.update_validator_with_fn(
                validator_index,
                activate_validator,
                config.GENESIS_EPOCH,
            )

    return genesis_state_with_active_index_roots(
        state,
        config,
    )
Example #8
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.create(
        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.create()

    # History
    assert state.latest_block_header == BeaconBlockHeader.create(
        body_root=BeaconBlockBody.create().hash_tree_root)
    assert len(state.block_roots) == slots_per_historical_root
    assert tuple(
        state.block_roots) == (ZERO_HASH32, ) * slots_per_historical_root
    assert len(state.state_roots) == slots_per_historical_root
    assert tuple(
        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 (tuple(state.randao_mixes) == (eth1_block_hash, ) *
            epochs_per_historical_vector)

    # Slashings
    assert len(state.slashings) == epochs_per_slashings_vector
    assert tuple(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)
Example #9
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)