示例#1
0
def make_state_hashable(num_validators):
    state = HashableState.create(
        validators=HashableList.from_iterable(
            (HashableValidator.create(
                pubkey=i.to_bytes(48, "little"),
                withdrawal_credentials=i.to_bytes(32, "little"),
            ) for i in range(num_validators)),
            element_sedes=HashableState.fields[0][1].element_sedes,
            max_length=HashableState.fields[0][1].max_length,
        ),
        balances=HashableList.from_iterable(
            (i + 1000 for i in range(num_validators)),
            element_sedes=HashableState.fields[1][1].element_sedes,
            max_length=HashableState.fields[1][1].max_length,
        ),
        randao_mixes=HashableVector.from_iterable(
            (i.to_bytes(32, "little")
             for i in range(EPOCHS_PER_HISTORICAL_VECTOR)),
            element_sedes=HashableState.fields[2][1].element_sedes,
        ),
        latest_block_header=HashableBeaconBlockHeader.create(
            slot=1,
            parent_root=b"\x22" * 32,
            state_root=b"\x22" * 32,
            body_root=b"\x22" * 32,
            signature=b"\x55" * 96,
        ),
        eth1_data=HashableEth1Data.create(deposit_root=b"\x12" * 32,
                                          deposit_count=1,
                                          block_hash=b"\x12" * 32),
    )

    return state
示例#2
0
def _determine_next_eth1_votes(state: BeaconState,
                               config: Eth2Config) -> HashableList[Eth1Data]:
    if (state.next_epoch(config.SLOTS_PER_EPOCH) %
            config.EPOCHS_PER_ETH1_VOTING_PERIOD == 0):
        return HashableList.from_iterable((), state.eth1_data_votes.sedes)
    else:
        return state.eth1_data_votes
示例#3
0
def test_not_serializable():
    octopi = (octopus, octopus, octopus)
    sedes = List(Animal, 2**32)
    output = to_formatted_dict(octopi, sedes)

    hashable_octopi = HashableList.from_iterable(octopi, sedes)
    assert hashable_octopi == from_formatted_dict(output, List(Animal, 2**32))
示例#4
0
def to_hashable_value(value, sedes):
    if isinstance(sedes, (BasicSedes, Bitlist, Bitvector, ByteVector)):
        return value

    elif isinstance(sedes, List):
        elements = (to_hashable_value(element, sedes.element_sedes)
                    for element in value)
        return HashableList.from_iterable(elements, sedes)

    elif isinstance(sedes, Vector) and not isinstance(sedes, ByteVector):
        elements = (to_hashable_value(element, sedes.element_sedes)
                    for element in value)
        return HashableVector.from_iterable(value, sedes)

    elif isinstance(sedes, Container):
        field_names = [f"field{index}" for index in range(len(value))]

        class ValueClass(HashableContainer):
            fields = tuple((field_name, field_sedes)
                           for field_name, field_sedes in zip(
                               field_names, sedes.field_sedes))

        kwargs = {
            field_name: to_hashable_value(field_value, field_sedes)
            for field_name, field_value, field_sedes in zip(
                field_names, value, sedes.field_sedes)
        }

        return ValueClass.create(**kwargs)

    else:
        assert False
示例#5
0
def genesis_validators(validator_count, pubkeys, config):
    """
    Returns ``validator_count`` number of activated validators.
    """
    return HashableList.from_iterable(
        (create_mock_validator(pubkey=pubkey, config=config)
         for pubkey in pubkeys[:validator_count]),
        List(Validator, config.VALIDATOR_REGISTRY_LIMIT),
    )
示例#6
0
def hashablify_value(value: Any, sedes: BaseSedes) -> Any:
    if isinstance(value, (HashableContainer, HashableList, HashableVector)):
        return value

    if isinstance(sedes, List):
        return HashableList.from_iterable(value, sedes)
    elif isinstance(sedes, Vector):
        if isinstance(sedes, ByteVector):
            return value
        else:
            return HashableVector.from_iterable(value, sedes)
    else:
        return value
示例#7
0
def process_rewards_and_penalties(state: BeaconState,
                                  config: Eth2Config) -> BeaconState:
    current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH)
    if current_epoch == GENESIS_EPOCH:
        return state

    rewards_for_attestations, penalties_for_attestations = get_attestation_deltas(
        state, config)

    new_balances = (max(balance + reward - penalty, 0)
                    for balance, reward, penalty in zip(
                        state.balances, rewards_for_attestations,
                        penalties_for_attestations))

    return state.set(
        "balances",
        HashableList.from_iterable(new_balances, state.balances.sedes))
示例#8
0
def process_final_updates(state: BeaconState,
                          config: Eth2Config) -> BeaconState:
    new_eth1_data_votes = _determine_next_eth1_votes(state, config)
    new_validators = _update_effective_balances(state, config)
    new_slashings = _compute_next_slashings(state, config)
    new_randao_mixes = _compute_next_randao_mixes(state, config)
    new_historical_roots = _compute_next_historical_roots(state, config)
    return state.mset(
        "eth1_data_votes",
        new_eth1_data_votes,
        "validators",
        new_validators,
        "slashings",
        new_slashings,
        "randao_mixes",
        new_randao_mixes,
        "historical_roots",
        new_historical_roots,
        "previous_epoch_attestations",
        state.current_epoch_attestations,
        "current_epoch_attestations",
        HashableList.from_iterable(
            (), sedes=state.current_epoch_attestations.sedes),
    )
示例#9
0
def genesis_balances(validator_count, max_effective_balance, config):
    return HashableList.from_iterable(
        (max_effective_balance,) * validator_count,
        List(uint64, config.VALIDATOR_REGISTRY_LIMIT),
    )
示例#10
0
 def _deserialize_stream(self,
                         stream: IO[bytes]) -> HashableList[TDeserialized]:
     elements = self._deserialize_stream_to_tuple(stream)
     return HashableList.from_iterable(elements, sedes=self)
示例#11
0
def parse_list(value, sedes, codec):
    if not isinstance(value, Sequence):
        raise ValueError(f"Expected Sequence, got {type(value)}")
    elements = tuple(
        parse(element, sedes.element_sedes, codec) for element in value)
    return HashableList.from_iterable(elements, sedes=sedes)