From ca489630325aebffca2a425fe1a5b69573140e6e Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 28 May 2020 21:38:11 +0800 Subject: [PATCH 1/7] Rename `head_shard_root` to `shard_head_root` --- specs/phase1/beacon-chain.md | 4 ++-- tests/core/pyspec/eth2spec/test/helpers/attestations.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 0ae4bae2b..bc5a6385c 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -130,7 +130,7 @@ class AttestationData(Container): source: Checkpoint target: Checkpoint # Current-slot shard block root - head_shard_root: Root + shard_head_root: Root # Shard transition root shard_transition_root: Root ``` @@ -823,7 +823,7 @@ 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.head_shard_root == shard_transition.shard_data_roots[ + assert attestation.data.shard_head_root == shard_transition.shard_data_roots[ len(shard_transition.shard_data_roots) - 1 ] diff --git a/tests/core/pyspec/eth2spec/test/helpers/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/attestations.py index 4d246c8c6..c533182ef 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/attestations.py @@ -78,7 +78,7 @@ def build_attestation_data(spec, state, slot, index, shard_transition=None, on_t if spec.fork == PHASE1: if shard_transition is not None: lastest_shard_data_root_index = len(shard_transition.shard_data_roots) - 1 - attestation_data.head_shard_root = shard_transition.shard_data_roots[lastest_shard_data_root_index] + attestation_data.shard_head_root = shard_transition.shard_data_roots[lastest_shard_data_root_index] attestation_data.shard_transition_root = shard_transition.hash_tree_root() else: # No shard transition @@ -88,10 +88,10 @@ def build_attestation_data(spec, state, slot, index, shard_transition=None, on_t next_slot(spec, temp_state) shard_transition = spec.get_shard_transition(temp_state, shard, []) lastest_shard_data_root_index = len(shard_transition.shard_data_roots) - 1 - attestation_data.head_shard_root = shard_transition.shard_data_roots[lastest_shard_data_root_index] + attestation_data.shard_head_root = shard_transition.shard_data_roots[lastest_shard_data_root_index] attestation_data.shard_transition_root = shard_transition.hash_tree_root() else: - attestation_data.head_shard_root = state.shard_states[shard].transition_digest + attestation_data.shard_head_root = state.shard_states[shard].transition_digest attestation_data.shard_transition_root = spec.Root() return attestation_data From 8c9bbc48d8720ed621ba54ab7ae12696ca5279e8 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 28 May 2020 21:49:36 +0800 Subject: [PATCH 2/7] Rework `is_shard_attestation` Change it to `is_on_time_attestation` so that it could be reused in `validate_attestation`. --- specs/phase1/beacon-chain.md | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index bc5a6385c..4f4713f70 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -53,7 +53,7 @@ - [`get_offset_slots`](#get_offset_slots) - [Predicates](#predicates) - [Updated `is_valid_indexed_attestation`](#updated-is_valid_indexed_attestation) - - [`is_shard_attestation`](#is_shard_attestation) + - [`is_on_time_attestation`](#is_on_time_attestation) - [`is_winning_attestation`](#is_winning_attestation) - [`optional_aggregate_verify`](#optional_aggregate_verify) - [`optional_fast_aggregate_verify`](#optional_fast_aggregate_verify) @@ -602,20 +602,16 @@ def is_valid_indexed_attestation(state: BeaconState, indexed_attestation: Indexe return bls.AggregateVerify(all_pubkeys, all_signing_roots, signature=attestation.signature) ``` -#### `is_shard_attestation` +#### `is_on_time_attestation` ```python -def is_shard_attestation(state: BeaconState, - attestation: Attestation, - committee_index: CommitteeIndex) -> bool: - if not ( - attestation.data.index == committee_index - and attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY == state.slot # Must be on-time attestation - # TODO: MIN_ATTESTATION_INCLUSION_DELAY should always be 1 - ): - return False - - return True +def is_on_time_attestation(state: BeaconState, + attestation: Attestation) -> bool: + """ + Check if the given attestation is on-time. + """ + # TODO: MIN_ATTESTATION_INCLUSION_DELAY should always be 1 + return attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY == state.slot ``` #### `is_winning_attestation` @@ -730,7 +726,7 @@ def validate_attestation(state: BeaconState, attestation: Attestation) -> None: # Type 1: on-time attestations, the custody bits should be non-empty. if attestation.custody_bits_blocks != []: # Ensure on-time attestation - assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY == state.slot + assert is_on_time_attestation(state, attestation) # Correct data root count assert len(attestation.custody_bits_blocks) == len(get_offset_slots(state, shard)) # Correct parent block root @@ -875,7 +871,7 @@ def process_crosslinks(state: BeaconState, shard_transition = shard_transitions[shard] shard_attestations = [ attestation for attestation in attestations - if is_shard_attestation(state, attestation, committee_index) + if is_on_time_attestation(state, attestation) and attestation.data.index == committee_index ] winning_root = process_crosslink_for_shard(state, committee_index, shard_transition, shard_attestations) From 19262888e4997659f1a6c015562284213526d0fa Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 28 May 2020 21:55:49 +0800 Subject: [PATCH 3/7] Rename `verify_shard_transition_false_positives` to `verify_empty_shard_transition` --- specs/phase1/beacon-chain.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 4f4713f70..e536ad6ef 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -66,7 +66,7 @@ - [`process_crosslinks`](#process_crosslinks) - [`process_attestation`](#process_attestation) - [New Attester slashing processing](#new-attester-slashing-processing) - - [Shard transition false positives](#shard-transition-false-positives) + - [Verify empty shard transition](#verify-empty-shard-transition) - [Light client processing](#light-client-processing) - [Epoch transition](#epoch-transition) - [Custody game updates](#custody-game-updates) @@ -671,7 +671,7 @@ def process_block(state: BeaconState, block: BeaconBlock) -> None: process_eth1_data(state, block.body) process_light_client_signatures(state, block.body) process_operations(state, block.body) - verify_shard_transition_false_positives(state, block.body) + verify_empty_shard_transition(state, block.body) ``` #### Operations @@ -943,11 +943,13 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla assert slashed_any ``` -#### Shard transition false positives +#### Verify empty shard transition ```python -def verify_shard_transition_false_positives(state: BeaconState, block_body: BeaconBlockBody) -> None: - # Verify that a `shard_transition` in a block is empty if an attestation was not processed for it +def verify_empty_shard_transition(state: BeaconState, block_body: BeaconBlockBody) -> None: + """ + Verify that a `shard_transition` in a block is empty if an attestation was not processed for it. + """ for shard in range(get_active_shard_count(state)): if state.shard_states[shard].slot != compute_previous_slot(state.slot): assert block_body.shard_transitions[shard] == ShardTransition() From b16e6d7a8623d6d90c90ea0fb64cc29a993952b5 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 29 May 2020 12:58:19 +0800 Subject: [PATCH 4/7] PR feedback from Danny Co-authored-by: Danny Ryan --- specs/phase1/beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index e536ad6ef..f087247f4 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -948,7 +948,7 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla ```python def verify_empty_shard_transition(state: BeaconState, block_body: BeaconBlockBody) -> None: """ - Verify that a `shard_transition` in a block is empty if an attestation was not processed for it. + Verify that ``shard_transitions`` are empty if a crosslink was not formed for the associated shard in this slot. """ for shard in range(get_active_shard_count(state)): if state.shard_states[shard].slot != compute_previous_slot(state.slot): From cceeab265769cb9a39362fe81de42436b74e3f90 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 29 May 2020 03:14:43 +0800 Subject: [PATCH 5/7] Combine `process_crosslinks` and `verify_empty_shard_transition` into `process_shard_transitions` --- specs/phase1/beacon-chain.md | 24 +++++++++++++++---- .../{crosslinks.py => shard_transitions.py} | 8 +++---- ...nk.py => test_process_shard_transition.py} | 4 ++-- tests/generators/README.md | 2 +- 4 files changed, 26 insertions(+), 12 deletions(-) rename tests/core/pyspec/eth2spec/test/helpers/{crosslinks.py => shard_transitions.py} (65%) rename tests/core/pyspec/eth2spec/test/phase_1/block_processing/{test_process_crosslink.py => test_process_shard_transition.py} (93%) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index f087247f4..853160292 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -64,6 +64,7 @@ - [`apply_shard_transition`](#apply_shard_transition) - [`process_crosslink_for_shard`](#process_crosslink_for_shard) - [`process_crosslinks`](#process_crosslinks) + - [`process_shard_transitions`](#process_shard_transitions) - [`process_attestation`](#process_attestation) - [New Attester slashing processing](#new-attester-slashing-processing) - [Verify empty shard transition](#verify-empty-shard-transition) @@ -671,7 +672,6 @@ def process_block(state: BeaconState, block: BeaconBlock) -> None: process_eth1_data(state, block.body) process_light_client_signatures(state, block.body) process_operations(state, block.body) - verify_empty_shard_transition(state, block.body) ``` #### Operations @@ -695,7 +695,7 @@ def process_operations(state: BeaconState, body: BeaconBlockBody) -> None: # See custody game spec. process_custody_game_operations(state, body) - process_crosslinks(state, body.shard_transitions, body.attestations) + process_shard_transitions(state, body.shard_transitions, body.attestations) # TODO process_operations(body.shard_receipt_proofs, process_shard_receipt_proofs) ``` @@ -882,6 +882,18 @@ def process_crosslinks(state: BeaconState, pending_attestation.crosslink_success = True ``` +###### `process_shard_transitions` + +```python +def process_shard_transitions(state: BeaconState, + shard_transitions: Sequence[ShardTransition], + attestations: Sequence[Attestation]) -> None: + # Process crosslinks + process_crosslinks(state, shard_transitions, attestations) + # Verify the empty proposal shard states + assert verify_empty_shard_transition(state, shard_transitions) +``` + ###### `process_attestation` ```python @@ -893,7 +905,7 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None: data=attestation.data, inclusion_delay=state.slot - attestation.data.slot, proposer_index=get_beacon_proposer_index(state), - crosslink_success=False, # To be filled in during process_crosslinks + crosslink_success=False, # To be filled in during process_shard_transitions ) if attestation.data.target.epoch == get_current_epoch(state): state.current_epoch_attestations.append(pending_attestation) @@ -946,13 +958,15 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla #### Verify empty shard transition ```python -def verify_empty_shard_transition(state: BeaconState, block_body: BeaconBlockBody) -> None: +def verify_empty_shard_transition(state: BeaconState, shard_transitions: Sequence[ShardTransition]) -> bool: """ Verify that ``shard_transitions`` are empty if a crosslink was not formed for the associated shard in this slot. """ for shard in range(get_active_shard_count(state)): if state.shard_states[shard].slot != compute_previous_slot(state.slot): - assert block_body.shard_transitions[shard] == ShardTransition() + if shard_transitions[shard] != ShardTransition(): + return False + return True ``` #### Light client processing diff --git a/tests/core/pyspec/eth2spec/test/helpers/crosslinks.py b/tests/core/pyspec/eth2spec/test/helpers/shard_transitions.py similarity index 65% rename from tests/core/pyspec/eth2spec/test/helpers/crosslinks.py rename to tests/core/pyspec/eth2spec/test/helpers/shard_transitions.py index ea5da89d9..4ac0ddcfb 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/crosslinks.py +++ b/tests/core/pyspec/eth2spec/test/helpers/shard_transitions.py @@ -1,9 +1,9 @@ from eth2spec.test.context import expect_assertion_error -def run_crosslinks_processing(spec, state, shard_transitions, attestations, valid=True): +def run_shard_transitions_processing(spec, state, shard_transitions, attestations, valid=True): """ - Run ``process_attestation``, yielding: + Run ``process_shard_transitions``, yielding: - pre-state ('pre') - shard_transitions ('shard_transitions') - attestations ('attestations') @@ -17,12 +17,12 @@ def run_crosslinks_processing(spec, state, shard_transitions, attestations, vali # If the attestation is invalid, processing is aborted, and there is no post-state. if not valid: - expect_assertion_error(lambda: spec.process_crosslinks(state, shard_transitions, attestations)) + expect_assertion_error(lambda: spec.process_shard_transitions(state, shard_transitions, attestations)) yield 'post', None return # process crosslinks - spec.process_crosslinks(state, shard_transitions, attestations) + spec.process_shard_transitions(state, shard_transitions, attestations) # yield post-state yield 'post', state diff --git a/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_crosslink.py b/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_shard_transition.py similarity index 93% rename from tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_crosslink.py rename to tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_shard_transition.py index 1f066b344..b4721da5e 100644 --- a/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_crosslink.py +++ b/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_shard_transition.py @@ -4,7 +4,7 @@ from eth2spec.test.context import ( spec_state_test, always_bls, ) -from eth2spec.test.helpers.crosslinks import run_crosslinks_processing +from eth2spec.test.helpers.shard_transitions import run_shard_transitions_processing from eth2spec.test.helpers.shard_block import ( build_attestation_with_shard_transition, build_shard_block, @@ -46,7 +46,7 @@ def run_basic_crosslink_tests(spec, state, target_len_offset_slot, valid=True): transition_to(spec, state, state.slot + target_len_offset_slot) pre_shard_state = state.shard_states[shard] - yield from run_crosslinks_processing(spec, state, shard_transitions, [attestation], valid=valid) + yield from run_shard_transitions_processing(spec, state, shard_transitions, [attestation], valid=valid) if valid: # After state transition, diff --git a/tests/generators/README.md b/tests/generators/README.md index 77a50606b..9446551fb 100644 --- a/tests/generators/README.md +++ b/tests/generators/README.md @@ -184,7 +184,7 @@ def create_provider(handler_name: str, tests_src, config_name: str) -> gen_typin if __name__ == "__main__": gen_runner.run_generator("epoch_processing", [ - create_provider('crosslinks', test_process_crosslinks, 'minimal'), + create_provider('final_updates', test_process_final_updates, 'minimal'), ... ]) From 327deb40b276ecf1e8c180d2f2c33b4f5196483c Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 29 May 2020 03:20:36 +0800 Subject: [PATCH 6/7] Adjust function blocks --- specs/phase1/beacon-chain.md | 73 +++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 853160292..d939d1c9f 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -61,13 +61,14 @@ - [Operations](#operations) - [New Attestation processing](#new-attestation-processing) - [`validate_attestation`](#validate_attestation) + - [Updated `process_attestation`](#updated-process_attestation) + - [Shard transition processing](#shard-transition-processing) - [`apply_shard_transition`](#apply_shard_transition) - [`process_crosslink_for_shard`](#process_crosslink_for_shard) - [`process_crosslinks`](#process_crosslinks) + - [`verify_empty_shard_transition`](#verify_empty_shard_transition) - [`process_shard_transitions`](#process_shard_transitions) - - [`process_attestation`](#process_attestation) - [New Attester slashing processing](#new-attester-slashing-processing) - - [Verify empty shard transition](#verify-empty-shard-transition) - [Light client processing](#light-client-processing) - [Epoch transition](#epoch-transition) - [Custody game updates](#custody-game-updates) @@ -742,6 +743,27 @@ def validate_attestation(state: BeaconState, attestation: Attestation) -> None: assert is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation)) ``` +###### Updated `process_attestation` + +```python +def process_attestation(state: BeaconState, attestation: Attestation) -> None: + validate_attestation(state, attestation) + # Store pending attestation for epoch processing + pending_attestation = PendingAttestation( + aggregation_bits=attestation.aggregation_bits, + data=attestation.data, + inclusion_delay=state.slot - attestation.data.slot, + proposer_index=get_beacon_proposer_index(state), + crosslink_success=False, # To be filled in during process_shard_transitions + ) + if attestation.data.target.epoch == get_current_epoch(state): + state.current_epoch_attestations.append(pending_attestation) + else: + state.previous_epoch_attestations.append(pending_attestation) +``` + +##### Shard transition processing + ###### `apply_shard_transition` ```python @@ -882,6 +904,20 @@ def process_crosslinks(state: BeaconState, pending_attestation.crosslink_success = True ``` +###### `verify_empty_shard_transition` + +```python +def verify_empty_shard_transition(state: BeaconState, shard_transitions: Sequence[ShardTransition]) -> bool: + """ + Verify that a `shard_transition` in a block is empty if an attestation was not processed for it. + """ + for shard in range(get_active_shard_count(state)): + if state.shard_states[shard].slot != compute_previous_slot(state.slot): + if shard_transitions[shard] != ShardTransition(): + return False + return True +``` + ###### `process_shard_transitions` ```python @@ -894,25 +930,6 @@ def process_shard_transitions(state: BeaconState, assert verify_empty_shard_transition(state, shard_transitions) ``` -###### `process_attestation` - -```python -def process_attestation(state: BeaconState, attestation: Attestation) -> None: - validate_attestation(state, attestation) - # Store pending attestation for epoch processing - pending_attestation = PendingAttestation( - aggregation_bits=attestation.aggregation_bits, - data=attestation.data, - inclusion_delay=state.slot - attestation.data.slot, - proposer_index=get_beacon_proposer_index(state), - crosslink_success=False, # To be filled in during process_shard_transitions - ) - if attestation.data.target.epoch == get_current_epoch(state): - state.current_epoch_attestations.append(pending_attestation) - else: - state.previous_epoch_attestations.append(pending_attestation) -``` - ##### New Attester slashing processing ```python @@ -955,20 +972,6 @@ def process_attester_slashing(state: BeaconState, attester_slashing: AttesterSla assert slashed_any ``` -#### Verify empty shard transition - -```python -def verify_empty_shard_transition(state: BeaconState, shard_transitions: Sequence[ShardTransition]) -> bool: - """ - Verify that ``shard_transitions`` are empty if a crosslink was not formed for the associated shard in this slot. - """ - for shard in range(get_active_shard_count(state)): - if state.shard_states[shard].slot != compute_previous_slot(state.slot): - if shard_transitions[shard] != ShardTransition(): - return False - return True -``` - #### Light client processing ```python From 437b2ebb90d3f26b2fb92d23792c560f38208603 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Fri, 29 May 2020 18:16:06 -0700 Subject: [PATCH 7/7] Update fork choice spec comment for clarity I think this change more clearly specifies the intended behavior. Given the terseness of the spec's code representation, I think we should aim for as much clarity as possible. --- specs/phase0/fork-choice.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/phase0/fork-choice.md b/specs/phase0/fork-choice.md index 4ed2733e1..baf7b7b7b 100644 --- a/specs/phase0/fork-choice.md +++ b/specs/phase0/fork-choice.md @@ -150,7 +150,7 @@ def get_ancestor(store: Store, root: Root, slot: Slot) -> Root: elif block.slot == slot: return root else: - # root is older than queried slot, thus a skip slot. Return earliest root prior to slot + # root is older than queried slot, thus a skip slot. Return most recent root prior to slot return root ```