예제 #1
0
    def _build_attestation_index(
            self, state: BeaconState,
            attestation_pool: AttestationPool) -> AttestationIndex:
        """
        Assembles a dictionary of latest attestations keyed by validator index.
        Any attestation made by a validator in the ``attestation_pool`` that occur after the
        last known attestation according to the state take precedence.

        We start by building a 'pre-index' from all known attestations which map validator
        indices to a pair of slot and attestation data. A final index is built from all
        pre-indices by keeping the entry with the highest slot across the set of all
        duplicates in the pre-indices keyed by validator index.
        """
        previous_epoch_index = self._mk_pre_index_from_attestations(
            state, state.previous_epoch_attestations)

        current_epoch_index = self._mk_pre_index_from_attestations(
            state, state.current_epoch_attestations)

        pool_index = self._mk_pre_index_from_attestations(
            state, tuple(attestation for _, attestation in attestation_pool))

        index_by_latest_slot = merge_with(
            _take_latest_attestation_by_slot,
            previous_epoch_index,
            current_epoch_index,
            pool_index,
        )
        # convert the index to a mapping of ValidatorIndex -> (latest) Attestation
        return valmap(
            second,
            index_by_latest_slot,
        )
예제 #2
0
def _read_request_from_metafunc(metafunc: Metafunc) -> Dict[str, Any]:
    """
    The ``metafunc.function`` has an ad-hoc property given in the top-level test harness
    decorator that communicates the caller's request to this library.

    This function also checks the ``metafunc`` for an eth2 config option and applies it.
    Supplying the configuration via the command line parameter overwrites any configuration
    given in the written request.
    """
    fn = metafunc.function
    request = fn.__eth2_fixture_config
    if "config_types" in request:
        return merge_with(_keep_first_some, request,
                          {"config_types": _eth2_config_from(metafunc)})
    else:
        return request
예제 #3
0
def test_store_get_latest_attestation(genesis_state, genesis_block, config,
                                      collisions_from_another_epoch):
    """
    Given some attestations across the various sources, can we
    find the latest ones for each validator?
    """
    some_epoch = 3
    state = genesis_state.set(
        "slot", compute_start_slot_at_epoch(some_epoch,
                                            config.SLOTS_PER_EPOCH))
    some_time = (_compute_seconds_since_genesis_for_epoch(some_epoch, config) +
                 state.genesis_time)
    previous_epoch = state.previous_epoch(config.SLOTS_PER_EPOCH,
                                          config.GENESIS_EPOCH)
    previous_epoch_committee_count = _get_committee_count(
        state, previous_epoch, config)

    current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH)
    current_epoch_committee_count = _get_committee_count(
        state, current_epoch, config)
    number_of_committee_samples = 4

    assert number_of_committee_samples <= previous_epoch_committee_count
    assert number_of_committee_samples <= current_epoch_committee_count

    block_producer = _mk_block_at_slot(genesis_block)

    # prepare samples from previous epoch
    previous_epoch_attestations_by_index = _mk_attestations_for_epoch_by_count(
        number_of_committee_samples, previous_epoch, block_producer, state,
        config)
    previous_epoch_attestations = _extract_attestations_from_index_keying(
        previous_epoch_attestations_by_index.values())

    # prepare samples from current epoch
    current_epoch_attestations_by_index = _mk_attestations_for_epoch_by_count(
        number_of_committee_samples, current_epoch, block_producer, state,
        config)
    current_epoch_attestations_by_index = keyfilter(
        lambda index: index not in previous_epoch_attestations_by_index,
        current_epoch_attestations_by_index,
    )
    current_epoch_attestations = _extract_attestations_from_index_keying(
        current_epoch_attestations_by_index.values())

    pool_attestations_by_index = _mk_attestations_for_epoch_by_count(
        number_of_committee_samples, current_epoch, block_producer, state,
        config)
    pool_attestations_by_index = keyfilter(
        lambda index: (index not in previous_epoch_attestations_by_index or
                       index not in current_epoch_attestations_by_index),
        pool_attestations_by_index,
    )
    pool_attestations = _extract_attestations_from_index_keying(
        pool_attestations_by_index.values())

    all_attestations_by_index = (
        previous_epoch_attestations_by_index,
        current_epoch_attestations_by_index,
        pool_attestations_by_index,
    )

    if collisions_from_another_epoch:
        (
            previous_epoch_attestations_by_index,
            current_epoch_attestations_by_index,
            pool_attestations_by_index,
        ) = _introduce_collisions(all_attestations_by_index, block_producer,
                                  state, config)

        previous_epoch_attestations = _extract_attestations_from_index_keying(
            previous_epoch_attestations_by_index.values())
        current_epoch_attestations = _extract_attestations_from_index_keying(
            current_epoch_attestations_by_index.values())
        pool_attestations = _extract_attestations_from_index_keying(
            pool_attestations_by_index.values())

    # build expected results
    expected_index = merge_with(
        _keep_by_latest_slot,
        previous_epoch_attestations_by_index,
        current_epoch_attestations_by_index,
        pool_attestations_by_index,
    )

    chain_db = None  # not relevant for this test
    context = Context.from_genesis(genesis_state, genesis_block)
    context.time = some_time
    store = Store(chain_db, SignedBeaconBlock, config, context)

    for attestations in (
            previous_epoch_attestations,
            current_epoch_attestations,
            pool_attestations,
    ):
        for attestation in attestations:
            # NOTE: we need to synchronize the context w/ chain data used to construct
            # attestations above; this synchronization takes advantage of some of the
            # internals of ``on_attestation`` to shortcut constructing the complete network
            # state needed to test the function of the ``Store``.
            block = block_producer(attestation.data.slot)
            context.blocks[block.message.hash_tree_root] = block
            context.block_states[block.message.hash_tree_root] = genesis_state

            store.on_attestation(attestation, validate_signature=False)

    # sanity check
    assert expected_index.keys() == store._context.latest_messages.keys()

    for validator_index in range(len(state.validators)):
        expected_attestation_data = expected_index.get(validator_index, None)
        target = expected_attestation_data.target
        expected_message = LatestMessage(epoch=target.epoch, root=target.root)
        stored_message = store._context.latest_messages.get(
            validator_index, None)
        assert expected_message == stored_message
예제 #4
0
def test_store_get_latest_attestation(genesis_state,
                                      empty_attestation_pool,
                                      config,
                                      collisions_from_another_epoch):
    """
    Given some attestations across the various sources, can we
    find the latest ones for each validator?
    """
    some_epoch = 3
    state = genesis_state.copy(
        slot=compute_start_slot_of_epoch(some_epoch, config.SLOTS_PER_EPOCH),
    )
    previous_epoch = state.previous_epoch(config.SLOTS_PER_EPOCH, config.GENESIS_EPOCH)
    previous_epoch_committee_count = _get_committee_count(
        state,
        previous_epoch,
        config,
    )

    current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH)
    current_epoch_committee_count = _get_committee_count(
        state,
        current_epoch,
        config,
    )

    next_epoch = state.next_epoch(config.SLOTS_PER_EPOCH)
    next_epoch_committee_count = _get_committee_count(
        state,
        next_epoch,
        config,
    )

    number_of_committee_samples = 4
    assert number_of_committee_samples <= previous_epoch_committee_count
    assert number_of_committee_samples <= current_epoch_committee_count
    assert number_of_committee_samples <= next_epoch_committee_count

    # prepare samples from previous epoch
    previous_epoch_attestations_by_index = _mk_attestations_for_epoch_by_count(
        number_of_committee_samples,
        previous_epoch,
        state,
        config,
    )
    previous_epoch_attestations = _extract_attestations_from_index_keying(
        previous_epoch_attestations_by_index.values(),
    )

    # prepare samples from current epoch
    current_epoch_attestations_by_index = _mk_attestations_for_epoch_by_count(
        number_of_committee_samples,
        current_epoch,
        state,
        config,
    )
    current_epoch_attestations_by_index = keyfilter(
        lambda index: index not in previous_epoch_attestations_by_index,
        current_epoch_attestations_by_index,
    )
    current_epoch_attestations = _extract_attestations_from_index_keying(
        current_epoch_attestations_by_index.values(),
    )

    # prepare samples for pool, taking half from the current epoch and half from the next epoch
    pool_attestations_in_current_epoch_by_index = _mk_attestations_for_epoch_by_count(
        number_of_committee_samples // 2,
        current_epoch,
        state,
        config,
    )
    pool_attestations_in_next_epoch_by_index = _mk_attestations_for_epoch_by_count(
        number_of_committee_samples // 2,
        next_epoch,
        state,
        config,
    )
    pool_attestations_by_index = merge(
        pool_attestations_in_current_epoch_by_index,
        pool_attestations_in_next_epoch_by_index,
    )
    pool_attestations_by_index = keyfilter(
        lambda index: (
            index not in previous_epoch_attestations_by_index or
            index not in current_epoch_attestations_by_index
        ),
        pool_attestations_by_index,
    )
    pool_attestations = _extract_attestations_from_index_keying(
        pool_attestations_by_index.values(),
    )

    all_attestations_by_index = (
        previous_epoch_attestations_by_index,
        current_epoch_attestations_by_index,
        pool_attestations_by_index,
    )

    if collisions_from_another_epoch:
        (
            previous_epoch_attestations_by_index,
            current_epoch_attestations_by_index,
            pool_attestations_by_index,
        ) = _introduce_collisions(
            all_attestations_by_index,
            state,
            config,
        )

        previous_epoch_attestations = _extract_attestations_from_index_keying(
            previous_epoch_attestations_by_index.values(),
        )
        current_epoch_attestations = _extract_attestations_from_index_keying(
            current_epoch_attestations_by_index.values(),
        )
        pool_attestations = _extract_attestations_from_index_keying(
            pool_attestations_by_index.values(),
        )

    # build expected results
    expected_index = merge_with(
        _keep_by_latest_slot,
        previous_epoch_attestations_by_index,
        current_epoch_attestations_by_index,
        pool_attestations_by_index,
    )

    # ensure we get the expected results
    state = state.copy(
        previous_epoch_attestations=previous_epoch_attestations,
        current_epoch_attestations=current_epoch_attestations,
    )

    pool = empty_attestation_pool
    for attestation in pool_attestations:
        pool.add(attestation)

    chain_db = None  # not relevant for this test
    store = Store(chain_db, state, pool, BeaconBlock, config)

    # sanity check
    assert expected_index.keys() == store._attestation_index.keys()

    for validator_index in range(len(state.validators)):
        expected_attestation_data = expected_index.get(validator_index, None)
        stored_attestation_data = store._get_latest_attestation(validator_index)
        assert expected_attestation_data == stored_attestation_data
예제 #5
0
def deep_merge(*dicts: Dict[Any, Any]) -> Dict[Any, Any]:
    return merge_with(merge_if_dicts, *dicts)
예제 #6
0
def merge_if_dicts(values: Sequence[Dict[Any, Any]]) -> Any:
    if all(isinstance(item, Mapping) for item in values):
        return merge_with(merge_if_dicts, *values)
    else:
        return values[-1]