def next_epoch_with_attestations(spec, state, fill_cur_epoch, fill_prev_epoch):
    assert state.slot % spec.SLOTS_PER_EPOCH == 0

    post_state = state.copy()
    signed_blocks = []
    for _ in range(spec.SLOTS_PER_EPOCH):
        block = build_empty_block_for_next_slot(spec, post_state)
        if fill_cur_epoch and post_state.slot >= spec.MIN_ATTESTATION_INCLUSION_DELAY:
            slot_to_attest = post_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY + 1
            committees_per_slot = spec.get_committee_count_per_slot(
                state, spec.compute_epoch_at_slot(slot_to_attest))
            if slot_to_attest >= spec.compute_start_slot_at_epoch(
                    spec.get_current_epoch(post_state)):
                for index in range(committees_per_slot):
                    if spec.fork == PHASE1:
                        shard = spec.compute_shard_from_committee_index(
                            post_state, index, slot_to_attest)
                        shard_transition = get_shard_transition_of_committee(
                            spec, post_state, index)
                        block.body.shard_transitions[shard] = shard_transition
                    else:
                        shard_transition = None

                    cur_attestation = get_valid_attestation(
                        spec,
                        post_state,
                        slot_to_attest,
                        shard_transition=shard_transition,
                        index=index,
                        signed=True,
                        on_time=True)
                    block.body.attestations.append(cur_attestation)

        if fill_prev_epoch:
            slot_to_attest = post_state.slot - spec.SLOTS_PER_EPOCH + 1
            committees_per_slot = spec.get_committee_count_per_slot(
                state, spec.compute_epoch_at_slot(slot_to_attest))
            for index in range(committees_per_slot):
                prev_attestation = get_valid_attestation(spec,
                                                         post_state,
                                                         slot_to_attest,
                                                         index=index,
                                                         signed=True,
                                                         on_time=False)
                block.body.attestations.append(prev_attestation)

        signed_block = state_transition_and_sign_block(spec, post_state, block)
        signed_blocks.append(signed_block)

    return state, signed_blocks, post_state
Example #2
0
def test_attestation(spec, state):
    next_epoch(spec, state)

    yield 'pre', state

    attestation_block = build_empty_block(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY)

    index = 0
    if spec.fork == PHASE1:
        shard = spec.compute_shard_from_committee_index(state, index, state.slot)
        shard_transition = get_shard_transition_of_committee(spec, state, index)
        attestation_block.body.shard_transitions[shard] = shard_transition
    else:
        shard_transition = None

    attestation = get_valid_attestation(
        spec, state, shard_transition=shard_transition, index=index, signed=True, on_time=True
    )

    # Add to state via block transition
    pre_current_attestations_len = len(state.current_epoch_attestations)
    attestation_block.body.attestations.append(attestation)
    signed_attestation_block = state_transition_and_sign_block(spec, state, attestation_block)

    assert len(state.current_epoch_attestations) == pre_current_attestations_len + 1

    # Epoch transition should move to previous_epoch_attestations
    pre_current_attestations_root = spec.hash_tree_root(state.current_epoch_attestations)

    epoch_block = build_empty_block(spec, state, state.slot + spec.SLOTS_PER_EPOCH)
    signed_epoch_block = state_transition_and_sign_block(spec, state, epoch_block)

    yield 'blocks', [signed_attestation_block, signed_epoch_block]
    yield 'post', state

    assert len(state.current_epoch_attestations) == 0
    assert spec.hash_tree_root(state.previous_epoch_attestations) == pre_current_attestations_root