From 443786017fb76f4ab9b86e67b7f5666c893c251a Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 18 Apr 2019 13:31:19 +1000 Subject: [PATCH 1/3] Further decouple justification and finalisation (#958) Addresses @djrtwo's comment [here](https://github.com/ethereum/eth2.0-specs/pull/925#issuecomment-484123950). --- specs/core/0_beacon-chain.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index e0556a7f3..1ee9108ad 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -1683,7 +1683,8 @@ def update_justification_and_finalization(state: BeaconState) -> None: if get_current_epoch(state) <= GENESIS_EPOCH + 1: return - antepenultimate_justified_epoch = state.previous_justified_epoch + old_previous_justified_epoch = state.previous_justified_epoch + old_current_justified_epoch = state.current_justified_epoch # Process justifications state.previous_justified_epoch = state.current_justified_epoch @@ -1704,20 +1705,20 @@ def update_justification_and_finalization(state: BeaconState) -> None: bitfield = state.justification_bitfield current_epoch = get_current_epoch(state) # The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th as source - if (bitfield >> 1) % 8 == 0b111 and antepenultimate_justified_epoch == current_epoch - 3: - state.finalized_epoch = antepenultimate_justified_epoch + if (bitfield >> 1) % 8 == 0b111 and old_previous_justified_epoch == current_epoch - 3: + state.finalized_epoch = old_previous_justified_epoch state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch)) # The 2nd/3rd most recent epochs are justified, the 2nd using the 3rd as source - if (bitfield >> 1) % 4 == 0b11 and antepenultimate_justified_epoch == current_epoch - 2: - state.finalized_epoch = antepenultimate_justified_epoch + if (bitfield >> 1) % 4 == 0b11 and old_previous_justified_epoch == current_epoch - 2: + state.finalized_epoch = old_previous_justified_epoch state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch)) # The 1st/2nd/3rd most recent epochs are justified, the 1st using the 3rd as source - if (bitfield >> 0) % 8 == 0b111 and state.previous_justified_epoch == current_epoch - 2: - state.finalized_epoch = state.previous_justified_epoch + if (bitfield >> 0) % 8 == 0b111 and old_current_justified_epoch == current_epoch - 2: + state.finalized_epoch = old_current_justified_epoch state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch)) # The 1st/2nd most recent epochs are justified, the 1st using the 2nd as source - if (bitfield >> 0) % 4 == 0b11 and state.previous_justified_epoch == current_epoch - 1: - state.finalized_epoch = state.previous_justified_epoch + if (bitfield >> 0) % 4 == 0b11 and old_current_justified_epoch == current_epoch - 1: + state.finalized_epoch = old_current_justified_epoch state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch)) ``` From 4bca7f984d9e86ef21a54875e8fcd466ab9c348a Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 18 Apr 2019 00:43:22 -0700 Subject: [PATCH 2/3] Add the prefix `state` when accessing the latest deposit count (#961) --- specs/core/0_beacon-chain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 1ee9108ad..e6f9872c9 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -2155,7 +2155,7 @@ def process_proposer_attestation_rewards(state: BeaconState) -> None: ##### Deposits -Verify that `len(block.body.deposits) == min(MAX_DEPOSITS, latest_eth1_data.deposit_count - state.deposit_index)`. +Verify that `len(block.body.deposits) == min(MAX_DEPOSITS, state.latest_eth1_data.deposit_count - state.deposit_index)`. For each `deposit` in `block.body.deposits`, run the following function: From d4ce0d20a0ee89d6df411d4b7cd8ab0d7dd3d6ef Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 18 Apr 2019 19:08:34 +1000 Subject: [PATCH 3/3] Cleanup per-epoch processing presentation (#959) --- specs/core/0_beacon-chain.md | 71 ++++++------------- .../eth2spec/phase0/state_transition.py | 9 ++- 2 files changed, 25 insertions(+), 55 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index e6f9872c9..1ccc0823e 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -113,21 +113,17 @@ - [State caching](#state-caching) - [Per-epoch processing](#per-epoch-processing) - [Helper functions](#helper-functions-1) - - [Justification](#justification) + - [Justification and finalization](#justification-and-finalization) - [Crosslinks](#crosslinks) - [Rewards and penalties](#rewards-and-penalties) - - [Justification and finalization](#justification-and-finalization) - - [Crosslinks](#crosslinks-1) - - [Apply rewards](#apply-rewards) - - [Balance-driven status transitions](#balance-driven-status-transitions) - - [Activation queue and start shard](#activation-queue-and-start-shard) + - [Registry updates](#registry-updates) - [Slashings](#slashings) - [Final updates](#final-updates) - [Per-slot processing](#per-slot-processing) - [Per-block processing](#per-block-processing) - [Block header](#block-header) - [RANDAO](#randao) - - [Eth1 data](#eth1-data-1) + - [Eth1 data](#eth1-data) - [Operations](#operations) - [Proposer slashings](#proposer-slashings) - [Attester slashings](#attester-slashings) @@ -575,7 +571,7 @@ The types are defined topologically to aid in facilitating an executable version # Randomness and committees 'latest_randao_mixes': ['bytes32', LATEST_RANDAO_MIXES_LENGTH], 'latest_start_shard': 'uint64', - + # Finality 'previous_epoch_attestations': [PendingAttestation], 'current_epoch_attestations': [PendingAttestation], @@ -648,7 +644,7 @@ Note: We aim to migrate to a S[T/N]ARK-friendly hash function in a future Ethere ```python def get_temporary_block_header(block: BeaconBlock) -> BeaconBlockHeader: """ - Return the block header corresponding to a block with ``state_root`` set to ``ZERO_HASH``. + Return the block header corresponding to a block with ``state_root`` set to ``ZERO_HASH``. """ return BeaconBlockHeader( slot=block.slot, @@ -793,7 +789,7 @@ def get_permuted_index(index: int, list_size: int, seed: Bytes32) -> int: """ assert index < list_size assert list_size <= 2**40 - + for round in range(SHUFFLE_ROUND_COUNT): pivot = bytes_to_int(hash(seed + int_to_bytes1(round))[0:8]) % list_size flip = (pivot - index) % list_size @@ -1674,12 +1670,12 @@ def get_earliest_attestation(state: BeaconState, attestations: List[PendingAttes ], key=lambda a: a.inclusion_slot) ``` -#### Justification +#### Justification and finalization Run the following function: ```python -def update_justification_and_finalization(state: BeaconState) -> None: +def process_justification_and_finalization(state: BeaconState) -> None: if get_current_epoch(state) <= GENESIS_EPOCH + 1: return @@ -1744,7 +1740,7 @@ def process_crosslinks(state: BeaconState) -> None: #### Rewards and penalties -First, we define some additional helpers: +First, we define additional helpers: ```python def get_base_reward_from_total_balance(state: BeaconState, total_balance: Gwei, index: ValidatorIndex) -> Gwei: @@ -1769,10 +1765,6 @@ def get_inactivity_penalty(state: BeaconState, index: ValidatorIndex, epochs_sin return get_base_reward(state, index) + extra_penalty ``` -Note: When applying penalties in the following balance recalculations, implementers should make sure the `uint64` does not underflow. - -##### Justification and finalization - ```python def get_justification_and_finalization_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: current_epoch = get_current_epoch(state) @@ -1821,8 +1813,6 @@ def get_justification_and_finalization_deltas(state: BeaconState) -> Tuple[List[ return [rewards, penalties] ``` -##### Crosslinks - ```python def get_crosslink_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: rewards = [0 for index in range(len(state.validator_registry))] @@ -1842,12 +1832,10 @@ def get_crosslink_deltas(state: BeaconState) -> Tuple[List[Gwei], List[Gwei]]: return [rewards, penalties] ``` -#### Apply rewards - -Run the following: +Run the following function: ```python -def apply_rewards(state: BeaconState) -> None: +def process_rewards_and_penalties(state: BeaconState) -> None: if get_current_epoch(state) == GENESIS_EPOCH: return @@ -1858,16 +1846,13 @@ def apply_rewards(state: BeaconState) -> None: decrease_balance(state, i, penalties1[i] + penalties2[i]) ``` -#### Balance-driven status transitions +#### Registry updates -Run `process_balance_driven_status_transitions(state)`. +Run the following function: ```python -def process_balance_driven_status_transitions(state: BeaconState) -> None: - """ - Iterate through the validator registry - and deposit or eject active validators with sufficiently high or low balances - """ +def process_registry_updates(state: BeaconState) -> None: + # Process activation eligibility and ejections for index, validator in enumerate(state.validator_registry): balance = get_balance(state, index) if validator.activation_eligibility_epoch == FAR_FUTURE_EPOCH and balance >= MAX_DEPOSIT_AMOUNT: @@ -1875,39 +1860,23 @@ def process_balance_driven_status_transitions(state: BeaconState) -> None: if is_active_validator(validator, get_current_epoch(state)) and balance < EJECTION_BALANCE: initiate_validator_exit(state, index) -``` -#### Activation queue and start shard - -Run the following function: - -```python -def update_registry(state: BeaconState) -> None: + # Process activations activation_queue = sorted([ index for index, validator in enumerate(state.validator_registry) if validator.activation_eligibility_epoch != FAR_FUTURE_EPOCH and validator.activation_epoch >= get_delayed_activation_exit_epoch(state.finalized_epoch) ], key=lambda index: state.validator_registry[index].activation_eligibility_epoch) - for index in activation_queue[:get_churn_limit(state)]: - activate_validator(state, index, is_genesis=False) - - state.latest_start_shard = ( - state.latest_start_shard + - get_shard_delta(state, get_current_epoch(state)) - ) % SHARD_COUNT + activate_validator(state, index) ``` #### Slashings -Run `process_slashings(state)`: +Run the following function: ```python def process_slashings(state: BeaconState) -> None: - """ - Process the slashings. - Note that this function mutates ``state``. - """ current_epoch = get_current_epoch(state) active_validator_indices = get_active_validator_indices(state, current_epoch) total_balance = get_total_balance(state, active_validator_indices) @@ -1931,12 +1900,14 @@ def process_slashings(state: BeaconState) -> None: Run the following function: ```python -def finish_epoch_update(state: BeaconState) -> None: +def process_final_updates(state: BeaconState) -> None: current_epoch = get_current_epoch(state) next_epoch = current_epoch + 1 # Reset eth1 data votes if state.slot % SLOTS_PER_ETH1_VOTING_PERIOD == 0: state.eth1_data_votes = [] + # Update start shard + state.latest_start_shard = (state.latest_start_shard + get_shard_delta(state, current_epoch)) % SHARD_COUNT # Set active index root index_root_position = (next_epoch + ACTIVATION_EXIT_DELAY) % LATEST_ACTIVE_INDEX_ROOTS_LENGTH state.latest_active_index_roots[index_root_position] = hash_tree_root( diff --git a/test_libs/pyspec/eth2spec/phase0/state_transition.py b/test_libs/pyspec/eth2spec/phase0/state_transition.py index f43e2791b..38ecd2a02 100644 --- a/test_libs/pyspec/eth2spec/phase0/state_transition.py +++ b/test_libs/pyspec/eth2spec/phase0/state_transition.py @@ -91,13 +91,12 @@ def process_block(state: BeaconState, def process_epoch_transition(state: BeaconState) -> None: - spec.update_justification_and_finalization(state) + spec.process_justification_and_finalization(state) spec.process_crosslinks(state) - spec.apply_rewards(state) - spec.process_balance_driven_status_transitions(state) - spec.update_registry(state) + spec.process_rewards_and_penalties(state) + spec.process_registry_updates(state) spec.process_slashings(state) - spec.finish_epoch_update(state) + spec.process_final_updates(state) def state_transition_to(state: BeaconState, up_to: Slot) -> BeaconState: