Esempio n. 1
0
def test_get_beacon_proposer_index(genesis_state, config):
    # TODO(hwwhww) find a way to normalize validator effective balance distribution.
    state = genesis_state
    validators = tuple([
        state.validators[index].set("effective_balance",
                                    config.MAX_EFFECTIVE_BALANCE)
        for index in range(len(state.validators))
    ])
    for slot in range(0, config.SLOTS_PER_EPOCH):
        state = state.mset("slot", slot, "validators", validators)
        proposer_index = get_beacon_proposer_index(state, config)
        assert proposer_index

        current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH)
        domain_type = signature_domain_to_domain_type(
            SignatureDomain.DOMAIN_BEACON_PROPOSER)
        seed = hash_eth2(
            get_seed(state, current_epoch, domain_type, config) +
            state.slot.to_bytes(8, "little"))
        random_byte = hash_eth2(seed +
                                (proposer_index //
                                 32).to_bytes(8, "little"))[proposer_index %
                                                            32]
        # Verify if proposer_index matches the condition.
        assert (state.validators[proposer_index].effective_balance *
                MAX_RANDOM_BYTE >= config.MAX_EFFECTIVE_BALANCE * random_byte)
Esempio n. 2
0
def get_beacon_committee(
        state: BeaconState, slot: Slot, index: CommitteeIndex,
        config: CommitteeConfig) -> Tuple[ValidatorIndex, ...]:
    epoch = compute_epoch_at_slot(slot, config.SLOTS_PER_EPOCH)
    committees_per_slot = get_committee_count_at_slot(
        state,
        slot,
        config.MAX_COMMITTEES_PER_SLOT,
        config.SLOTS_PER_EPOCH,
        config.TARGET_COMMITTEE_SIZE,
    )

    active_validator_indices = get_active_validator_indices(
        state.validators, epoch)

    domain_type = signature_domain_to_domain_type(
        SignatureDomain.DOMAIN_BEACON_ATTESTER)

    return _compute_committee(
        indices=active_validator_indices,
        seed=get_seed(state, epoch, domain_type, config),
        index=(slot % config.SLOTS_PER_EPOCH) * committees_per_slot + index,
        count=committees_per_slot * config.SLOTS_PER_EPOCH,
        shuffle_round_count=config.SHUFFLE_ROUND_COUNT,
    )
Esempio n. 3
0
def sign(duty: Duty, operation: Operation,
         private_key_provider: PrivateKeyProvider) -> BLSSignature:
    message = operation.hash_tree_root
    private_key = private_key_provider(duty.validator_public_key)
    # TODO use correct ``domain`` value
    # NOTE currently only uses part of the domain value
    # need to get fork from the state and compute the full domain value locally
    domain = Domain(b"\x00" * 4 +
                    signature_domain_to_domain_type(duty.signature_domain))
    return bls.sign(message, private_key, domain)
Esempio n. 4
0
 def _randao_provider_of_epoch_signature(
     public_key: BLSPubkey, epoch: Epoch
 ) -> BLSSignature:
     private_key = private_key_provider(public_key)
     # TODO: fix how we get the signing root
     message = Hash32(epoch.to_bytes(32, byteorder="big"))
     domain = Domain(
         b"\x00" * 4 + signature_domain_to_domain_type(SignatureDomain.DOMAIN_RANDAO)
     )
     sig = bls.sign(message, private_key, domain)
     return sig
Esempio n. 5
0
def test_get_seed(
    genesis_state,
    config,
    slots_per_epoch,
    min_seed_lookahead,
    max_seed_lookahead,
    epochs_per_historical_vector,
):
    def mock_get_randao_mix(state, epoch, epochs_per_historical_vector):
        return hash_eth2(
            state.hash_tree_root
            + epoch.to_bytes(32, byteorder="little")
            + epochs_per_historical_vector.to_bytes(32, byteorder="little")
        )

    state = genesis_state
    epoch = 1
    state = state.set(
        "slot", compute_start_slot_at_epoch(epoch, config.SLOTS_PER_EPOCH)
    )

    epoch_as_bytes = epoch.to_bytes(32, "little")
    domain_type = signature_domain_to_domain_type(
        SignatureDomain.DOMAIN_BEACON_PROPOSER
    )
    seed = _get_seed(
        state=state,
        epoch=epoch,
        domain_type=domain_type,
        randao_provider=mock_get_randao_mix,
        epoch_provider=lambda *_: epoch_as_bytes,
        config=config,
    )
    assert seed == hash_eth2(
        domain_type
        + epoch_as_bytes
        + mock_get_randao_mix(
            state=state,
            epoch=(epoch + epochs_per_historical_vector - min_seed_lookahead - 1),
            epochs_per_historical_vector=epochs_per_historical_vector,
        )
    )
Esempio n. 6
0
def get_beacon_proposer_index(state: BeaconState,
                              config: Eth2Config) -> ValidatorIndex:
    """
    Return the current beacon proposer index.
    """
    current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH)
    domain_type = signature_domain_to_domain_type(
        SignatureDomain.DOMAIN_BEACON_PROPOSER)

    seed = hash_eth2(
        get_seed(state, current_epoch, domain_type, config) +
        state.slot.to_bytes(8, "little"))
    indices = get_active_validator_indices(state.validators, current_epoch)
    return compute_proposer_index(
        state.validators,
        indices,
        seed,
        config.MAX_EFFECTIVE_BALANCE,
        config.SHUFFLE_ROUND_COUNT,
    )