From ea591931570a67f27647651635d8d291105026e9 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 18:11:28 +0800 Subject: [PATCH 01/13] Rename some `shard_blocks` to `shard_block_dict` --- .../eth2spec/test/fork_choice/test_on_shard_head.py | 2 +- .../core/pyspec/eth2spec/test/helpers/shard_block.py | 10 +++++++--- .../test_process_shard_transition.py | 4 ++-- .../eth2spec/test/phase1/sanity/test_blocks.py | 12 ++++++------ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_shard_head.py b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_shard_head.py index c84f073b2..f7b888a2e 100644 --- a/tests/core/pyspec/eth2spec/test/fork_choice/test_on_shard_head.py +++ b/tests/core/pyspec/eth2spec/test/fork_choice/test_on_shard_head.py @@ -72,7 +72,7 @@ def apply_shard_and_beacon(spec, state, store, shard_store, shard_blocks_buffer) shard_transitions = get_shard_transitions( spec, state, - shard_blocks={shard: shard_blocks_buffer}, + shard_block_dict={shard: shard_blocks_buffer}, ) shard_transition = shard_transitions[shard] attestation = get_valid_on_time_attestation( diff --git a/tests/core/pyspec/eth2spec/test/helpers/shard_block.py b/tests/core/pyspec/eth2spec/test/helpers/shard_block.py index 6957f2283..1193f05e5 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/shard_block.py +++ b/tests/core/pyspec/eth2spec/test/helpers/shard_block.py @@ -30,7 +30,7 @@ def build_shard_block(spec, slot = shard_parent_state.slot + 1 if body is None: - body = b'\x56' * 128 + body = get_sample_shard_block_body() 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) @@ -52,10 +52,10 @@ def build_shard_block(spec, return signed_block -def get_shard_transitions(spec, parent_beacon_state, shard_blocks): +def get_shard_transitions(spec, parent_beacon_state, shard_block_dict): shard_transitions = [spec.ShardTransition()] * spec.MAX_SHARDS on_time_slot = parent_beacon_state.slot + 1 - for shard, blocks in shard_blocks.items(): + for shard, blocks in shard_block_dict.items(): shard_transition = spec.get_shard_transition(parent_beacon_state, shard, blocks) offset_slots = spec.compute_offset_slots( spec.get_latest_slot_for_shard(parent_beacon_state, shard), @@ -81,3 +81,7 @@ def get_committee_index_of_shard(spec, state, slot, shard): # Optional[Committe if (start_shard + committee_index) % active_shard_count == shard: return committee_index return None + + +def get_sample_shard_block_body(): + return b'\x56' * 128 diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index 6d8878177..0c9293b74 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -16,7 +16,7 @@ def run_basic_crosslink_tests(spec, state, target_len_offset_slot, valid=True): state = transition_to_valid_shard_slot(spec, state) committee_index = spec.CommitteeIndex(0) init_slot = state.slot - shard_slot = state.slot + target_len_offset_slot - 1 + 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 @@ -31,7 +31,7 @@ def run_basic_crosslink_tests(spec, state, target_len_offset_slot, valid=True): shard_transitions = get_shard_transitions( spec, state, - shard_blocks={shard: shard_blocks}, + shard_block_dict={shard: shard_blocks}, ) shard_transition = shard_transitions[shard] attestation = get_valid_on_time_attestation( diff --git a/tests/core/pyspec/eth2spec/test/phase1/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/phase1/sanity/test_blocks.py index 33b0beac7..aa24bfc1e 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/phase1/sanity/test_blocks.py @@ -19,9 +19,9 @@ def run_beacon_block_with_shard_blocks(spec, state, target_len_offset_slot, comm body = b'\x56' * spec.MAX_SHARD_BLOCK_SIZE shard_block = build_shard_block(spec, state, shard, body=body, slot=state.slot, signed=True) - shard_blocks: Dict[spec.Shard, Sequence[spec.SignedShardBlock]] = {shard: [shard_block]} + shard_block_dict: Dict[spec.Shard, Sequence[spec.SignedShardBlock]] = {shard: [shard_block]} - shard_transitions = get_shard_transitions(spec, state, shard_blocks) + shard_transitions = get_shard_transitions(spec, state, shard_block_dict) attestations = [ get_valid_on_time_attestation( spec, @@ -30,7 +30,7 @@ def run_beacon_block_with_shard_blocks(spec, state, target_len_offset_slot, comm shard_transition=shard_transitions[shard], signed=True, ) - for shard in shard_blocks.keys() + for shard in shard_block_dict.keys() ] beacon_block = build_empty_block(spec, state, slot=state.slot + 1) @@ -50,16 +50,16 @@ def run_beacon_block_with_shard_blocks(spec, state, target_len_offset_slot, comm for shard in range(spec.get_active_shard_count(state)): post_shard_state = state.shard_states[shard] - if shard in shard_blocks: + if shard in shard_block_dict: # Shard state has been changed to state_transition result assert post_shard_state == shard_transitions[shard].shard_states[ len(shard_transitions[shard].shard_states) - 1 ] assert post_shard_state.slot == state.slot - 1 - if len(shard_blocks[shard]) == 0: + if len((shard_block_dict[shard])) == 0: # `latest_block_root` is the same assert post_shard_state.latest_block_root == pre_shard_states[shard].latest_block_root - if target_len_offset_slot == 1 and len(shard_blocks) > 0: + if target_len_offset_slot == 1 and len(shard_block_dict[shard]) > 0: assert post_shard_state.gasprice > pre_gasprice From 3117cf3140c400c706e2f5de082b5c13771dc58a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 19:09:11 +0800 Subject: [PATCH 02/13] Refactor --- .../eth2spec/test/helpers/shard_block.py | 7 +- .../test_process_shard_transition.py | 72 ++++++++++++------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/helpers/shard_block.py b/tests/core/pyspec/eth2spec/test/helpers/shard_block.py index 1193f05e5..c8d60938b 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/shard_block.py +++ b/tests/core/pyspec/eth2spec/test/helpers/shard_block.py @@ -30,7 +30,7 @@ def build_shard_block(spec, slot = shard_parent_state.slot + 1 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) 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 -def get_sample_shard_block_body(): - return b'\x56' * 128 +def get_sample_shard_block_body(spec, is_max=False): + size = spec.MAX_SHARD_BLOCK_SIZE if is_max else 128 + return b'\x56' * size diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index 0c9293b74..afc4828f8 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -8,50 +8,63 @@ from eth2spec.test.helpers.shard_transitions import run_shard_transitions_proces from eth2spec.test.helpers.shard_block import ( build_shard_block, 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 -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) 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 - 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 - # Create SignedShardBlock - body = b'\x56' * spec.MAX_SHARD_BLOCK_SIZE - shard_block = build_shard_block(spec, state, shard, body=body, slot=state.slot, signed=True) - shard_blocks = [shard_block] + # Create SignedShardBlock at init_slot + shard_block = build_shard_block( + spec, state, shard, + 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) pre_gasprice = state.shard_states[shard].gasprice - transition_to(spec, state, init_slot + target_len_offset_slot) 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: shard_state = state.shard_states[shard] 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() if target_len_offset_slot == 1: assert shard_state.gasprice > pre_gasprice @@ -69,3 +82,10 @@ def test_basic_crosslinks(spec, state): def test_multiple_offset_slots(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=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) From c2c2b4c4441590da1e7c2286176d883f1578638a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 22:19:21 +0800 Subject: [PATCH 03/13] Crosslink bugfix 1. Fix `is_winning_attestation` condition 2. Fix `process_crosslink_for_shard`: we can only check `shard_data_roots` if `shard_transition != ShardTransition()` --- specs/phase1/beacon-chain.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index a1a940adf..aa250f9c4 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -671,7 +671,7 @@ def is_winning_attestation(state: BeaconState, ``winning_root`` formed by ``committee_index`` committee at the current slot. """ return ( - attestation.data.slot == state.slot + is_on_time_attestation(state, attestation) and attestation.data.index == committee_index and attestation.data.shard_transition_root == winning_root ) @@ -886,9 +886,10 @@ def process_crosslink_for_shard(state: BeaconState, for attestation in transition_attestations: participants = get_attesting_indices(state, attestation.data, attestation.aggregation_bits) transition_participants = transition_participants.union(participants) - assert attestation.data.shard_head_root == shard_transition.shard_data_roots[ - len(shard_transition.shard_data_roots) - 1 - ] + if len(shard_transition.shard_data_roots) > 0: + # Is the `shard_transition` candidate + last_offset_index = len(shard_transition.shard_data_roots) - 1 + assert attestation.data.shard_head_root == shard_transition.shard_data_roots[last_offset_index] enough_online_stake = ( get_total_balance(state, online_indices.intersection(transition_participants)) * 3 >= From 43605eebe8f24a5509c78cac47547e3fb5c518b1 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 22:21:29 +0800 Subject: [PATCH 04/13] Add `test_no_winning_root` --- .../test_process_shard_transition.py | 73 ++++++++++++++++--- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index afc4828f8..ead555098 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -3,7 +3,12 @@ from eth2spec.test.context import ( with_all_phases_except, spec_state_test, ) -from eth2spec.test.helpers.attestations import get_valid_on_time_attestation +from eth2spec.test.helpers.attestations import ( + get_valid_attestation, + get_valid_on_time_attestation, + run_attestation_processing, + sign_aggregate_attestation, +) from eth2spec.test.helpers.shard_transitions import run_shard_transitions_processing from eth2spec.test.helpers.shard_block import ( build_shard_block, @@ -19,6 +24,7 @@ def get_initial_env(spec, state, target_len_offset_slot): 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) + assert state.shard_states[shard].slot == state.slot - 1 return state, shard, target_shard_slot @@ -29,7 +35,7 @@ def get_attestations_and_shard_transitions(spec, state, shard_block_dict): spec, state, index=get_committee_index_of_shard(spec, state, state.slot, shard), shard_transition=shard_transition, - signed=False, + signed=True, ) for shard, shard_transition in enumerate(shard_transitions) if shard_transition != spec.ShardTransition() @@ -37,10 +43,9 @@ def get_attestations_and_shard_transitions(spec, state, shard_block_dict): return attestations, shard_transitions -def run_basic_crosslink_tests(spec, state, target_len_offset_slot, valid=True): +def run_successful_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 - assert state.shard_states[shard].slot == init_slot - 1 # Create SignedShardBlock at init_slot shard_block = build_shard_block( @@ -56,36 +61,86 @@ def run_basic_crosslink_tests(spec, state, target_len_offset_slot, valid=True): attestations, shard_transitions = get_attestations_and_shard_transitions(spec, state, shard_block_dict) next_slot(spec, state) - pre_gasprice = state.shard_states[shard].gasprice + for attestation in attestations: + _, _, _ = run_attestation_processing(spec, state, attestation) + + pre_gasprice = state.shard_states[shard].gasprice pre_shard_state = state.shard_states[shard] yield from run_shard_transitions_processing(spec, state, shard_transitions, attestations, valid=valid) if valid: shard_state = state.shard_states[shard] + shard_transition = shard_transitions[shard] assert shard_state != pre_shard_state - assert shard_state == shard_transitions[shard].shard_states[len(shard_transitions[shard].shard_states) - 1] + assert shard_state == shard_transition.shard_states[len(shard_transition.shard_states) - 1] assert shard_state.latest_block_root == shard_block.message.hash_tree_root() if target_len_offset_slot == 1: assert shard_state.gasprice > pre_gasprice + for pending_attestation in state.current_epoch_attestations: + assert bool(pending_attestation.crosslink_success) is True + @with_all_phases_except([PHASE0]) @spec_state_test def test_basic_crosslinks(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) + yield from run_successful_crosslink_tests(spec, state, target_len_offset_slot=1) @with_all_phases_except([PHASE0]) @spec_state_test def test_multiple_offset_slots(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=2, valid=True) + yield from run_successful_crosslink_tests(spec, state, target_len_offset_slot=2) @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) + state, shard, target_shard_slot = get_initial_env(spec, state, target_len_offset_slot=1) + init_slot = state.slot + + # Create SignedShardBlock at init_slot + shard_block = build_shard_block( + spec, state, shard, + 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_transitions = get_shard_transitions(spec, state, {shard: [shard_block]}) + shard_transition = shard_transitions[shard] + committee_index = get_committee_index_of_shard(spec, state, state.slot, shard) + attestation = get_valid_attestation( + spec, state, + index=committee_index, + shard_transition=shard_transition, + signed=True, + on_time=True, + ) + + # Decrease attested participants to 1/3 committee + beacon_committee = spec.get_beacon_committee(state, state.slot, committee_index) + attested_participants = beacon_committee[:len(beacon_committee) // 3] + for i in range(len(beacon_committee)): + attestation.aggregation_bits[i] = beacon_committee[i] in attested_participants + attestation.signature = sign_aggregate_attestation(spec, state, attestation.data, attested_participants) + + next_slot(spec, state) + + _, _, _ = run_attestation_processing(spec, state, attestation) + + # No winning root, shard_transitions[shard] is empty + shard_transitions = [spec.ShardTransition()] * spec.MAX_SHARDS + pre_shard_state = state.shard_states[shard] + yield from run_shard_transitions_processing(spec, state, shard_transitions, [attestation]) + + for pending_attestation in state.current_epoch_attestations: + assert bool(pending_attestation.crosslink_success) is False + + assert state.shard_states[shard] == pre_shard_state From 37d1a0750db7f01c3781638f8a35f36a1a78dc3f Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 22:29:49 +0800 Subject: [PATCH 05/13] Use `get_shard_winning_roots` to verify --- .../block_processing/test_process_shard_transition.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index ead555098..0a0722b9c 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -65,6 +65,10 @@ def run_successful_crosslink_tests(spec, state, target_len_offset_slot, valid=Tr for attestation in attestations: _, _, _ = run_attestation_processing(spec, state, attestation) + _, winning_roots = spec.get_shard_winning_roots(state, attestations) + assert len(winning_roots) == 1 + assert winning_roots[0] == shard_transitions[shard].hash_tree_root() + pre_gasprice = state.shard_states[shard].gasprice pre_shard_state = state.shard_states[shard] yield from run_shard_transitions_processing(spec, state, shard_transitions, attestations, valid=valid) @@ -135,6 +139,9 @@ def test_no_winning_root(spec, state): _, _, _ = run_attestation_processing(spec, state, attestation) + _, winning_roots = spec.get_shard_winning_roots(state, [attestation]) + assert len(winning_roots) == 0 + # No winning root, shard_transitions[shard] is empty shard_transitions = [spec.ShardTransition()] * spec.MAX_SHARDS pre_shard_state = state.shard_states[shard] From a9f8411a51ea55f1506ec538d47ac8d56fe5e74d Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 22:42:18 +0800 Subject: [PATCH 06/13] Verify all shard states --- .../test_process_shard_transition.py | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index 0a0722b9c..8d89e3911 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -67,20 +67,23 @@ def run_successful_crosslink_tests(spec, state, target_len_offset_slot, valid=Tr _, winning_roots = spec.get_shard_winning_roots(state, attestations) assert len(winning_roots) == 1 - assert winning_roots[0] == shard_transitions[shard].hash_tree_root() + shard_transition = shard_transitions[shard] + assert winning_roots[0] == shard_transition.hash_tree_root() pre_gasprice = state.shard_states[shard].gasprice - pre_shard_state = state.shard_states[shard] + pre_shard_states = state.shard_states.copy() yield from run_shard_transitions_processing(spec, state, shard_transitions, attestations, valid=valid) if valid: - shard_state = state.shard_states[shard] - shard_transition = shard_transitions[shard] - assert shard_state != pre_shard_state - assert shard_state == shard_transition.shard_states[len(shard_transition.shard_states) - 1] - assert shard_state.latest_block_root == shard_block.message.hash_tree_root() - if target_len_offset_slot == 1: - assert shard_state.gasprice > pre_gasprice + for index, shard_state in enumerate(state.shard_states): + if index == shard: + assert shard_state != pre_shard_states[index] + assert shard_state == shard_transition.shard_states[len(shard_transition.shard_states) - 1] + assert shard_state.latest_block_root == shard_block.message.hash_tree_root() + if target_len_offset_slot == 1: + assert shard_state.gasprice > pre_gasprice + else: + assert shard_state == pre_shard_states[index] for pending_attestation in state.current_epoch_attestations: assert bool(pending_attestation.crosslink_success) is True @@ -144,10 +147,10 @@ def test_no_winning_root(spec, state): # No winning root, shard_transitions[shard] is empty shard_transitions = [spec.ShardTransition()] * spec.MAX_SHARDS - pre_shard_state = state.shard_states[shard] + pre_shard_states = state.shard_states.copy() yield from run_shard_transitions_processing(spec, state, shard_transitions, [attestation]) for pending_attestation in state.current_epoch_attestations: assert bool(pending_attestation.crosslink_success) is False - assert state.shard_states[shard] == pre_shard_state + assert state.shard_states == pre_shard_states From b8caa6ee85c53a717cd394479134961e01be359a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 22:52:38 +0800 Subject: [PATCH 07/13] Add `test_wrong_shard_transition_root` --- .../test_process_shard_transition.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index 8d89e3911..ad8e7a633 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -154,3 +154,46 @@ def test_no_winning_root(spec, state): assert bool(pending_attestation.crosslink_success) is False assert state.shard_states == pre_shard_states + + +@with_all_phases_except([PHASE0]) +@spec_state_test +def test_wrong_shard_transition_root(spec, state): + state, shard, target_shard_slot = get_initial_env(spec, state, target_len_offset_slot=1) + init_slot = state.slot + + # Create SignedShardBlock at init_slot + shard_block = build_shard_block( + spec, state, shard, + 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_transitions = get_shard_transitions(spec, state, {shard: [shard_block]}) + shard_transition = shard_transitions[shard] + wrong_shard_transition = shard_transition.copy() + wrong_shard_transition.shard_states[shard].gasprice = shard_transition.shard_states[shard].gasprice + 1 + committee_index = get_committee_index_of_shard(spec, state, state.slot, shard) + attestation = get_valid_attestation( + spec, state, + index=committee_index, + shard_transition=wrong_shard_transition, + signed=True, + on_time=True, + ) + attestations = [attestation] + + next_slot(spec, state) + + run_attestation_processing(spec, state, attestation) + + # Check if winning root != shard_transition.hash_tree_root() + _, winning_roots = spec.get_shard_winning_roots(state, attestations) + assert len(winning_roots) == 1 + shard_transition = shard_transitions[shard] + assert winning_roots[0] != shard_transition.hash_tree_root() + + yield from run_shard_transitions_processing(spec, state, shard_transitions, attestations, valid=False) From 80c6c0b5f1ff62b74d7fc6850a453494f00d4e69 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 23:07:39 +0800 Subject: [PATCH 08/13] valid==True for `run_successful_crosslink_tests` helper --- .../test_process_shard_transition.py | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index ad8e7a633..caae1a1c7 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -43,7 +43,7 @@ def get_attestations_and_shard_transitions(spec, state, shard_block_dict): return attestations, shard_transitions -def run_successful_crosslink_tests(spec, state, target_len_offset_slot, valid=True): +def run_successful_crosslink_tests(spec, state, target_len_offset_slot): state, shard, target_shard_slot = get_initial_env(spec, state, target_len_offset_slot) init_slot = state.slot @@ -72,21 +72,20 @@ def run_successful_crosslink_tests(spec, state, target_len_offset_slot, valid=Tr pre_gasprice = state.shard_states[shard].gasprice pre_shard_states = state.shard_states.copy() - yield from run_shard_transitions_processing(spec, state, shard_transitions, attestations, valid=valid) + yield from run_shard_transitions_processing(spec, state, shard_transitions, attestations) - if valid: - for index, shard_state in enumerate(state.shard_states): - if index == shard: - assert shard_state != pre_shard_states[index] - assert shard_state == shard_transition.shard_states[len(shard_transition.shard_states) - 1] - assert shard_state.latest_block_root == shard_block.message.hash_tree_root() - if target_len_offset_slot == 1: - assert shard_state.gasprice > pre_gasprice - else: - assert shard_state == pre_shard_states[index] + for index, shard_state in enumerate(state.shard_states): + if index == shard: + assert shard_state != pre_shard_states[index] + assert shard_state == shard_transition.shard_states[len(shard_transition.shard_states) - 1] + assert shard_state.latest_block_root == shard_block.message.hash_tree_root() + if target_len_offset_slot == 1: + assert shard_state.gasprice > pre_gasprice + else: + assert shard_state == pre_shard_states[index] - for pending_attestation in state.current_epoch_attestations: - assert bool(pending_attestation.crosslink_success) is True + for pending_attestation in state.current_epoch_attestations: + assert bool(pending_attestation.crosslink_success) is True @with_all_phases_except([PHASE0]) From c28857e4e1f7226cb177f60c94364398428d5b77 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 23:34:51 +0800 Subject: [PATCH 09/13] Fix `attestation.data.shard_head_root` bug --- specs/phase1/beacon-chain.md | 7 ++++--- tests/core/pyspec/eth2spec/test/helpers/attestations.py | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index aa250f9c4..f90d61f26 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -886,10 +886,11 @@ def process_crosslink_for_shard(state: BeaconState, for attestation in transition_attestations: participants = get_attesting_indices(state, attestation.data, attestation.aggregation_bits) transition_participants = transition_participants.union(participants) - if len(shard_transition.shard_data_roots) > 0: + if len(shard_transition.shard_states) > 0: # Is the `shard_transition` candidate - last_offset_index = len(shard_transition.shard_data_roots) - 1 - assert attestation.data.shard_head_root == shard_transition.shard_data_roots[last_offset_index] + last_offset_index = len(shard_transition.shard_states) - 1 + shard_head_root = shard_transition.shard_states[last_offset_index].latest_block_root + assert attestation.data.shard_head_root == shard_head_root enough_online_stake = ( get_total_balance(state, online_indices.intersection(transition_participants)) * 3 >= diff --git a/tests/core/pyspec/eth2spec/test/helpers/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/attestations.py index 0c3f012f5..3445f9ff6 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/attestations.py @@ -83,14 +83,14 @@ def build_attestation_data(spec, state, slot, index, shard=None, shard_transitio attestation_data.shard = shard if shard_transition is not None: - lastest_shard_data_root_index = len(shard_transition.shard_data_roots) - 1 - attestation_data.shard_head_root = shard_transition.shard_data_roots[lastest_shard_data_root_index] + last_offset_index = len(shard_transition.shard_data_roots) - 1 + attestation_data.shard_head_root = shard_transition.shard_states[last_offset_index].latest_block_root attestation_data.shard_transition_root = shard_transition.hash_tree_root() else: if on_time: shard_transition = spec.get_shard_transition(state, shard, shard_blocks=[]) - lastest_shard_data_root_index = len(shard_transition.shard_data_roots) - 1 - attestation_data.shard_head_root = shard_transition.shard_data_roots[lastest_shard_data_root_index] + last_offset_index = len(shard_transition.shard_data_roots) - 1 + attestation_data.shard_head_root = shard_transition.shard_states[last_offset_index].latest_block_root attestation_data.shard_transition_root = shard_transition.hash_tree_root() else: attestation_data.shard_head_root = state.shard_states[shard].latest_block_root From 1a5016157a24f059afcab33a309adc2f21e47543 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 19 Jun 2020 23:46:01 +0800 Subject: [PATCH 10/13] Fix 1. To make it more compatible, update `is_on_time_attestation` argument: replace `attestation: Attestation` with `attestation_data: AttestationData` 2. Fix `get_sample_shard_transition` --- specs/phase1/beacon-chain.md | 16 ++++++++-------- specs/phase1/validator.md | 2 +- .../core/pyspec/eth2spec/test/helpers/custody.py | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index f90d61f26..f9b123d3b 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -652,11 +652,11 @@ def get_offset_slots(state: BeaconState, shard: Shard) -> Sequence[Slot]: ```python def is_on_time_attestation(state: BeaconState, - attestation: Attestation) -> bool: + attestation_data: AttestationData) -> bool: """ - Check if the given attestation is on-time. + Check if the given ``attestation_data`` is on-time. """ - return attestation.data.slot == compute_previous_slot(state.slot) + return attestation_data.slot == compute_previous_slot(state.slot) ``` #### `is_winning_attestation` @@ -667,11 +667,11 @@ def is_winning_attestation(state: BeaconState, committee_index: CommitteeIndex, winning_root: Root) -> bool: """ - Check if ``attestation`` helped contribute to the successful crosslink of - ``winning_root`` formed by ``committee_index`` committee at the current slot. + Check if on-time ``attestation`` helped contribute to the successful crosslink of + ``winning_root`` formed by ``committee_index`` committee. """ return ( - is_on_time_attestation(state, attestation) + is_on_time_attestation(state, attestation.data) and attestation.data.index == committee_index and attestation.data.shard_transition_root == winning_root ) @@ -766,7 +766,7 @@ def validate_attestation(state: BeaconState, attestation: Attestation) -> None: assert attestation.data.source == state.previous_justified_checkpoint # Type 1: on-time attestations - if is_on_time_attestation(state, attestation): + if is_on_time_attestation(state, attestation.data): # Correct parent block root assert data.beacon_block_root == get_block_root_at_slot(state, compute_previous_slot(state.slot)) # Correct shard number @@ -941,7 +941,7 @@ def process_crosslinks(state: BeaconState, # Since the attestations are validated, all `shard_attestations` satisfy `attestation.data.shard == shard` shard_attestations = [ attestation for attestation in attestations - if is_on_time_attestation(state, attestation) and attestation.data.index == committee_index + if is_on_time_attestation(state, attestation.data) and attestation.data.index == committee_index ] winning_root = process_crosslink_for_shard( state, committee_index, shard_transitions[shard], shard_attestations diff --git a/specs/phase1/validator.md b/specs/phase1/validator.md index f347f8757..fd3b7fef6 100644 --- a/specs/phase1/validator.md +++ b/specs/phase1/validator.md @@ -157,7 +157,7 @@ def get_shard_winning_roots(state: BeaconState, # All attestations in the block for this committee/shard and are "on time" shard_attestations = [ attestation for attestation in attestations - if is_on_time_attestation(state, attestation) and attestation.data.index == committee_index + if is_on_time_attestation(state, attestation.data) and attestation.data.index == committee_index ] committee = get_beacon_committee(state, on_time_attestation_slot, committee_index) diff --git a/tests/core/pyspec/eth2spec/test/helpers/custody.py b/tests/core/pyspec/eth2spec/test/helpers/custody.py index f63a07099..d25b91a41 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/custody.py +++ b/tests/core/pyspec/eth2spec/test/helpers/custody.py @@ -172,7 +172,7 @@ def get_sample_shard_transition(spec, start_slot, block_lengths): start_slot=start_slot, shard_block_lengths=block_lengths, shard_data_roots=b, - shard_states=[spec.Root() for x in block_lengths], + shard_states=[spec.ShardState() for x in block_lengths], proposer_signature_aggregate=spec.BLSSignature(), ) return shard_transition From 97d0048eaa86cebcd2d4f89c511753bde474af2a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 23 Jun 2020 00:06:35 +0800 Subject: [PATCH 11/13] PR feedback: use condition to determine if the test should be skipped --- .../test_process_shard_transition.py | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index caae1a1c7..0f610c161 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -43,6 +43,11 @@ def get_attestations_and_shard_transitions(spec, state, shard_block_dict): return attestations, shard_transitions +def is_full_crosslink(spec, state): + epoch = spec.compute_epoch_at_slot(state.slot) + return spec.get_committee_count_per_slot(state, epoch) >= spec.get_active_shard_count(state) + + def run_successful_crosslink_tests(spec, state, target_len_offset_slot): state, shard, target_shard_slot = get_initial_env(spec, state, target_len_offset_slot) init_slot = state.slot @@ -91,21 +96,30 @@ def run_successful_crosslink_tests(spec, state, target_len_offset_slot): @with_all_phases_except([PHASE0]) @spec_state_test def test_basic_crosslinks(spec, state): - # NOTE: this test is only for full crosslink (minimal config), not for mainnet + if not is_full_crosslink(spec, state): + # Skip this test + return + yield from run_successful_crosslink_tests(spec, state, target_len_offset_slot=1) @with_all_phases_except([PHASE0]) @spec_state_test def test_multiple_offset_slots(spec, state): - # NOTE: this test is only for full crosslink (minimal config), not for mainnet + if not is_full_crosslink(spec, state): + # Skip this test + return + yield from run_successful_crosslink_tests(spec, state, target_len_offset_slot=2) @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 + if not is_full_crosslink(spec, state): + # Skip this test + return + state, shard, target_shard_slot = get_initial_env(spec, state, target_len_offset_slot=1) init_slot = state.slot @@ -158,6 +172,10 @@ def test_no_winning_root(spec, state): @with_all_phases_except([PHASE0]) @spec_state_test def test_wrong_shard_transition_root(spec, state): + if not is_full_crosslink(spec, state): + # Skip this test + return + state, shard, target_shard_slot = get_initial_env(spec, state, target_len_offset_slot=1) init_slot = state.slot From 5357bddcf8e49ff8d3a13938f71aa8fb14e5691c Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 23 Jun 2020 00:13:48 +0800 Subject: [PATCH 12/13] PR feedback: use `filter_participant_set` --- .../block_processing/test_process_shard_transition.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py index 0f610c161..b14b836c0 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py +++ b/tests/core/pyspec/eth2spec/test/phase1/block_processing/test_process_shard_transition.py @@ -7,7 +7,6 @@ from eth2spec.test.helpers.attestations import ( get_valid_attestation, get_valid_on_time_attestation, run_attestation_processing, - sign_aggregate_attestation, ) from eth2spec.test.helpers.shard_transitions import run_shard_transitions_processing from eth2spec.test.helpers.shard_block import ( @@ -140,17 +139,12 @@ def test_no_winning_root(spec, state): spec, state, index=committee_index, shard_transition=shard_transition, + # Decrease attested participants to 1/3 committee + filter_participant_set=lambda committee: set(list(committee)[:len(committee) // 3]), signed=True, on_time=True, ) - # Decrease attested participants to 1/3 committee - beacon_committee = spec.get_beacon_committee(state, state.slot, committee_index) - attested_participants = beacon_committee[:len(beacon_committee) // 3] - for i in range(len(beacon_committee)): - attestation.aggregation_bits[i] = beacon_committee[i] in attested_participants - attestation.signature = sign_aggregate_attestation(spec, state, attestation.data, attested_participants) - next_slot(spec, state) _, _, _ = run_attestation_processing(spec, state, attestation) From e52c19896da33751ad85cbfed3315c6f4f607f04 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 23 Jun 2020 18:22:18 +0800 Subject: [PATCH 13/13] Update 1. Check on-time attestations fit `attestation.data.shard_transition_root != hash_tree_root(ShardTransition())` 2. Move `attestation.data.shard_head_root` check to after winning root --- specs/phase1/beacon-chain.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index f9b123d3b..5a5200c9e 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -772,6 +772,8 @@ def validate_attestation(state: BeaconState, attestation: Attestation) -> None: # Correct shard number shard = compute_shard_from_committee_index(state, attestation.data.index, attestation.data.slot) assert attestation.data.shard == shard + # On-time attestations should have a non-empty shard transition root + assert attestation.data.shard_transition_root != hash_tree_root(ShardTransition()) # Type 2: no shard transition else: # Ensure delayed attestation @@ -886,11 +888,6 @@ def process_crosslink_for_shard(state: BeaconState, for attestation in transition_attestations: participants = get_attesting_indices(state, attestation.data, attestation.aggregation_bits) transition_participants = transition_participants.union(participants) - if len(shard_transition.shard_states) > 0: - # Is the `shard_transition` candidate - last_offset_index = len(shard_transition.shard_states) - 1 - shard_head_root = shard_transition.shard_states[last_offset_index].latest_block_root - assert attestation.data.shard_head_root == shard_head_root enough_online_stake = ( get_total_balance(state, online_indices.intersection(transition_participants)) * 3 >= @@ -903,6 +900,12 @@ def process_crosslink_for_shard(state: BeaconState, # Attestation <-> shard transition consistency assert shard_transition_root == hash_tree_root(shard_transition) + # Check `shard_head_root` of the winning root + last_offset_index = len(shard_transition.shard_states) - 1 + shard_head_root = shard_transition.shard_states[last_offset_index].latest_block_root + for attestation in transition_attestations: + assert attestation.data.shard_head_root == shard_head_root + # Apply transition apply_shard_transition(state, shard, shard_transition) # Apply proposer reward and cost