This commit is contained in:
Hsiao-Wei Wang 2020-06-19 19:09:11 +08:00
parent ea59193157
commit 3117cf3140
No known key found for this signature in database
GPG Key ID: 95B070122902DEA4
2 changed files with 50 additions and 29 deletions

View File

@ -30,7 +30,7 @@ def build_shard_block(spec,
slot = shard_parent_state.slot + 1 slot = shard_parent_state.slot + 1
if body is None: if body is None:
body = get_sample_shard_block_body() body = get_sample_shard_block_body(spec)
beacon_state, beacon_parent_root = get_state_and_beacon_parent_root_at_slot(spec, beacon_state, slot) beacon_state, beacon_parent_root = get_state_and_beacon_parent_root_at_slot(spec, beacon_state, slot)
proposer_index = spec.get_shard_proposer_index(beacon_state, slot, shard) proposer_index = spec.get_shard_proposer_index(beacon_state, slot, shard)
@ -83,5 +83,6 @@ def get_committee_index_of_shard(spec, state, slot, shard): # Optional[Committe
return None return None
def get_sample_shard_block_body(): def get_sample_shard_block_body(spec, is_max=False):
return b'\x56' * 128 size = spec.MAX_SHARD_BLOCK_SIZE if is_max else 128
return b'\x56' * size

View File

@ -8,50 +8,63 @@ from eth2spec.test.helpers.shard_transitions import run_shard_transitions_proces
from eth2spec.test.helpers.shard_block import ( from eth2spec.test.helpers.shard_block import (
build_shard_block, build_shard_block,
get_shard_transitions, get_shard_transitions,
get_sample_shard_block_body,
get_committee_index_of_shard,
) )
from eth2spec.test.helpers.state import transition_to, transition_to_valid_shard_slot, next_slot from eth2spec.test.helpers.state import transition_to, transition_to_valid_shard_slot, next_slot
def run_basic_crosslink_tests(spec, state, target_len_offset_slot, valid=True): def get_initial_env(spec, state, target_len_offset_slot):
state = transition_to_valid_shard_slot(spec, state) state = transition_to_valid_shard_slot(spec, state)
committee_index = spec.CommitteeIndex(0) committee_index = spec.CommitteeIndex(0)
target_shard_slot = state.slot + target_len_offset_slot - 1
shard = spec.compute_shard_from_committee_index(state, committee_index, target_shard_slot)
return state, shard, target_shard_slot
def get_attestations_and_shard_transitions(spec, state, shard_block_dict):
shard_transitions = get_shard_transitions(spec, state, shard_block_dict)
attestations = [
get_valid_on_time_attestation(
spec, state,
index=get_committee_index_of_shard(spec, state, state.slot, shard),
shard_transition=shard_transition,
signed=False,
)
for shard, shard_transition in enumerate(shard_transitions)
if shard_transition != spec.ShardTransition()
]
return attestations, shard_transitions
def run_basic_crosslink_tests(spec, state, target_len_offset_slot, valid=True):
state, shard, target_shard_slot = get_initial_env(spec, state, target_len_offset_slot)
init_slot = state.slot init_slot = state.slot
shard_slot = init_slot + target_len_offset_slot - 1
shard = spec.compute_shard_from_committee_index(state, committee_index, shard_slot)
assert state.shard_states[shard].slot == init_slot - 1 assert state.shard_states[shard].slot == init_slot - 1
# Create SignedShardBlock # Create SignedShardBlock at init_slot
body = b'\x56' * spec.MAX_SHARD_BLOCK_SIZE shard_block = build_shard_block(
shard_block = build_shard_block(spec, state, shard, body=body, slot=state.slot, signed=True) spec, state, shard,
shard_blocks = [shard_block] slot=init_slot, body=get_sample_shard_block_body(spec, is_max=True), signed=True
)
# Transition state to target shard slot
transition_to(spec, state, target_shard_slot)
# Create a shard_transitions that would be included at beacon block `target_shard_slot + 1`
shard_block_dict = {shard: [shard_block]}
attestations, shard_transitions = get_attestations_and_shard_transitions(spec, state, shard_block_dict)
# Transition state latest shard slot
transition_to(spec, state, shard_slot)
# Create a shard_transitions that would be included at beacon block `state.slot + target_len_offset_slot`
shard_transitions = get_shard_transitions(
spec,
state,
shard_block_dict={shard: shard_blocks},
)
shard_transition = shard_transitions[shard]
attestation = get_valid_on_time_attestation(
spec,
state,
index=committee_index,
shard_transition=shard_transition,
signed=False,
)
next_slot(spec, state) next_slot(spec, state)
pre_gasprice = state.shard_states[shard].gasprice pre_gasprice = state.shard_states[shard].gasprice
transition_to(spec, state, init_slot + target_len_offset_slot)
pre_shard_state = state.shard_states[shard] pre_shard_state = state.shard_states[shard]
yield from run_shard_transitions_processing(spec, state, shard_transitions, [attestation], valid=valid) yield from run_shard_transitions_processing(spec, state, shard_transitions, attestations, valid=valid)
if valid: if valid:
shard_state = state.shard_states[shard] shard_state = state.shard_states[shard]
assert shard_state != pre_shard_state assert shard_state != pre_shard_state
assert shard_state == shard_transition.shard_states[len(shard_transition.shard_states) - 1] assert shard_state == shard_transitions[shard].shard_states[len(shard_transitions[shard].shard_states) - 1]
assert shard_state.latest_block_root == shard_block.message.hash_tree_root() assert shard_state.latest_block_root == shard_block.message.hash_tree_root()
if target_len_offset_slot == 1: if target_len_offset_slot == 1:
assert shard_state.gasprice > pre_gasprice assert shard_state.gasprice > pre_gasprice
@ -69,3 +82,10 @@ def test_basic_crosslinks(spec, state):
def test_multiple_offset_slots(spec, state): def test_multiple_offset_slots(spec, state):
# NOTE: this test is only for full crosslink (minimal config), not for mainnet # NOTE: this test is only for full crosslink (minimal config), not for mainnet
yield from run_basic_crosslink_tests(spec, state, target_len_offset_slot=2, valid=True) yield from run_basic_crosslink_tests(spec, state, target_len_offset_slot=2, valid=True)
@with_all_phases_except([PHASE0])
@spec_state_test
def test_no_winning_root(spec, state):
# NOTE: this test is only for full crosslink (minimal config), not for mainnet
yield from run_basic_crosslink_tests(spec, state, target_len_offset_slot=1, valid=True)