Beispiel #1
0
def apply_beacon_block_to_store(spec, state, store, beacon_block):
    signed_beacon_block = state_transition_and_sign_block(
        spec, state, beacon_block)  # transition!
    store.time = store.time + spec.SECONDS_PER_SLOT
    add_block_to_store(spec, store, signed_beacon_block)
    apply_all_attestation_to_store(
        spec, store, signed_beacon_block.message.body.attestations)
def test_shorter_chain_but_heavier_weight(spec, state):
    genesis_state = state.copy()

    # Initialization
    store = get_genesis_forkchoice_store(spec, state)
    anchor_root = get_anchor_root(spec, state)
    assert spec.get_head(store) == anchor_root

    # build longer tree
    long_state = genesis_state.copy()
    for _ in range(3):
        long_block = build_empty_block_for_next_slot(spec, long_state)
        signed_long_block = state_transition_and_sign_block(spec, long_state, long_block)
        add_block_to_store(spec, store, signed_long_block)

    # build short tree
    short_state = genesis_state.copy()
    short_block = build_empty_block_for_next_slot(spec, short_state)
    short_block.body.graffiti = b'\x42' * 32
    signed_short_block = state_transition_and_sign_block(spec, short_state, short_block)
    add_block_to_store(spec, store, signed_short_block)

    short_attestation = get_valid_attestation(spec, short_state, short_block.slot, signed=True)
    add_attestation_to_store(spec, store, short_attestation)

    assert spec.get_head(store) == spec.hash_tree_root(short_block)
Beispiel #3
0
def apply_shard_and_beacon(spec, state, store, shard_store,
                           shard_blocks_buffer):
    store.time = store.time + spec.SECONDS_PER_SLOT * spec.SLOTS_PER_EPOCH

    shard = shard_store.shard
    committee_index = get_committee_index_of_shard(spec, state, state.slot,
                                                   shard)
    has_shard_committee = committee_index is not None  # has committee of `shard` at this slot

    beacon_block = build_empty_block(spec, state, slot=state.slot + 1)

    # If next slot has committee of `shard`, add `shard_transtion` to the proposing beacon block
    if has_shard_committee and len(shard_blocks_buffer) > 0:
        # Sanity check `get_pending_shard_blocks` function
        check_pending_shard_blocks(spec, store, shard_store,
                                   shard_blocks_buffer)
        # Use temporary next state to get ShardTransition of shard block
        shard_transitions = get_shard_transitions(
            spec,
            state,
            shard_block_dict={shard: shard_blocks_buffer},
        )
        shard_transition = shard_transitions[shard]
        attestation = get_valid_on_time_attestation(
            spec,
            state,
            index=committee_index,
            shard_transition=shard_transition,
            signed=False,
        )
        assert attestation.data.shard == shard
        beacon_block.body.attestations = [attestation]
        beacon_block.body.shard_transitions = shard_transitions

        # Clear buffer
        shard_blocks_buffer.clear()

    signed_beacon_block = state_transition_and_sign_block(
        spec, state, beacon_block)  # transition!
    add_block_to_store(spec, store, signed_beacon_block)
    assert spec.get_head(store) == beacon_block.hash_tree_root()

    # On shard block at transitioned `state.slot`
    if is_in_offset_sets(spec, state, shard):
        # The created shard block would be appended to `shard_blocks_buffer`
        apply_shard_block(spec, store, shard_store, state, shard_blocks_buffer)

    return has_shard_committee
def test_chain_no_attestations(spec, state):
    # Initialization
    store = get_genesis_forkchoice_store(spec, state)
    anchor_root = get_anchor_root(spec, state)
    assert spec.get_head(store) == anchor_root

    # On receiving a block of `GENESIS_SLOT + 1` slot
    block_1 = build_empty_block_for_next_slot(spec, state)
    signed_block_1 = state_transition_and_sign_block(spec, state, block_1)
    add_block_to_store(spec, store, signed_block_1)

    # On receiving a block of next epoch
    block_2 = build_empty_block_for_next_slot(spec, state)
    signed_block_2 = state_transition_and_sign_block(spec, state, block_2)
    add_block_to_store(spec, store, signed_block_2)

    assert spec.get_head(store) == spec.hash_tree_root(block_2)
def test_split_tie_breaker_no_attestations(spec, state):
    genesis_state = state.copy()

    # Initialization
    store = get_genesis_forkchoice_store(spec, state)
    anchor_root = get_anchor_root(spec, state)
    assert spec.get_head(store) == anchor_root

    # block at slot 1
    block_1_state = genesis_state.copy()
    block_1 = build_empty_block_for_next_slot(spec, block_1_state)
    signed_block_1 = state_transition_and_sign_block(spec, block_1_state, block_1)
    add_block_to_store(spec, store, signed_block_1)

    # additional block at slot 1
    block_2_state = genesis_state.copy()
    block_2 = build_empty_block_for_next_slot(spec, block_2_state)
    block_2.body.graffiti = b'\x42' * 32
    signed_block_2 = state_transition_and_sign_block(spec, block_2_state, block_2)
    add_block_to_store(spec, store, signed_block_2)

    highest_root = max(spec.hash_tree_root(block_1), spec.hash_tree_root(block_2))

    assert spec.get_head(store) == highest_root