From 493dd17cc407765110f9e6fe4fef66b4c07965b3 Mon Sep 17 00:00:00 2001 From: protolambda Date: Sun, 21 Mar 2021 04:22:37 +0100 Subject: [PATCH 1/2] reduce justification and finalization Altair diff --- specs/altair/beacon-chain.md | 42 ++++++------------------------------ specs/phase0/beacon-chain.md | 17 +++++++++++---- 2 files changed, 20 insertions(+), 39 deletions(-) diff --git a/specs/altair/beacon-chain.md b/specs/altair/beacon-chain.md index 58e82364c..3f702dca4 100644 --- a/specs/altair/beacon-chain.md +++ b/specs/altair/beacon-chain.md @@ -575,7 +575,7 @@ def process_epoch(state: BeaconState) -> None: #### Justification and finalization -*Note*: The function `process_justification_and_finalization` is modified with `matching_target_attestations` replaced by `matching_target_indices`. +*Note*: The function `process_justification_and_finalization` is modified to adapt to the new participation records. ```python def process_justification_and_finalization(state: BeaconState) -> None: @@ -583,40 +583,12 @@ def process_justification_and_finalization(state: BeaconState) -> None: # Skip FFG updates in the first two epochs to avoid corner cases that might result in modifying this stub. if get_current_epoch(state) <= GENESIS_EPOCH + 1: return - previous_epoch = get_previous_epoch(state) - current_epoch = get_current_epoch(state) - old_previous_justified_checkpoint = state.previous_justified_checkpoint - old_current_justified_checkpoint = state.current_justified_checkpoint - - # Process justifications - state.previous_justified_checkpoint = state.current_justified_checkpoint - state.justification_bits[1:] = state.justification_bits[:JUSTIFICATION_BITS_LENGTH - 1] - state.justification_bits[0] = 0b0 - matching_target_indices = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, previous_epoch) - if get_total_balance(state, matching_target_indices) * 3 >= get_total_active_balance(state) * 2: - state.current_justified_checkpoint = Checkpoint(epoch=previous_epoch, - root=get_block_root(state, previous_epoch)) - state.justification_bits[1] = 0b1 - matching_target_indices = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, current_epoch) - if get_total_balance(state, matching_target_indices) * 3 >= get_total_active_balance(state) * 2: - state.current_justified_checkpoint = Checkpoint(epoch=current_epoch, - root=get_block_root(state, current_epoch)) - state.justification_bits[0] = 0b1 - - # Process finalizations - bits = state.justification_bits - # The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th as source - if all(bits[1:4]) and old_previous_justified_checkpoint.epoch + 3 == current_epoch: - state.finalized_checkpoint = old_previous_justified_checkpoint - # The 2nd/3rd most recent epochs are justified, the 2nd using the 3rd as source - if all(bits[1:3]) and old_previous_justified_checkpoint.epoch + 2 == current_epoch: - state.finalized_checkpoint = old_previous_justified_checkpoint - # The 1st/2nd/3rd most recent epochs are justified, the 1st using the 3rd as source - if all(bits[0:3]) and old_current_justified_checkpoint.epoch + 2 == current_epoch: - state.finalized_checkpoint = old_current_justified_checkpoint - # The 1st/2nd most recent epochs are justified, the 1st using the 2nd as source - if all(bits[0:2]) and old_current_justified_checkpoint.epoch + 1 == current_epoch: - state.finalized_checkpoint = old_current_justified_checkpoint + previous = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state)) + current = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, get_current_epoch(state)) + weigh_justification_and_finalization( + state, get_total_active_balance(state), + get_total_balance(state, previous), + get_total_balance(state, current)) ``` #### Inactivity scores diff --git a/specs/phase0/beacon-chain.md b/specs/phase0/beacon-chain.md index 9fcd11852..d978c4a75 100644 --- a/specs/phase0/beacon-chain.md +++ b/specs/phase0/beacon-chain.md @@ -1320,7 +1320,18 @@ def process_justification_and_finalization(state: BeaconState) -> None: # Skip FFG updates in the first two epochs to avoid corner cases that might result in modifying this stub. if get_current_epoch(state) <= GENESIS_EPOCH + 1: return + previous = get_matching_target_attestations(state, get_previous_epoch(state)) + current = get_matching_target_attestations(state, get_current_epoch(state)) + weigh_justification_and_finalization( + state, get_total_active_balance(state), + get_attesting_balance(state, previous), + get_attesting_balance(state, current)) +``` +```python +def weigh_justification_and_finalization(state: BeaconState, total_active_balance: Gwei, + previous_epoch_target_balance: Gwei, + current_epoch_target_balance: Gwei) -> None: previous_epoch = get_previous_epoch(state) current_epoch = get_current_epoch(state) old_previous_justified_checkpoint = state.previous_justified_checkpoint @@ -1330,13 +1341,11 @@ def process_justification_and_finalization(state: BeaconState) -> None: state.previous_justified_checkpoint = state.current_justified_checkpoint state.justification_bits[1:] = state.justification_bits[:JUSTIFICATION_BITS_LENGTH - 1] state.justification_bits[0] = 0b0 - matching_target_attestations = get_matching_target_attestations(state, previous_epoch) # Previous epoch - if get_attesting_balance(state, matching_target_attestations) * 3 >= get_total_active_balance(state) * 2: + if previous_epoch_target_balance * 3 >= total_active_balance * 2: state.current_justified_checkpoint = Checkpoint(epoch=previous_epoch, root=get_block_root(state, previous_epoch)) state.justification_bits[1] = 0b1 - matching_target_attestations = get_matching_target_attestations(state, current_epoch) # Current epoch - if get_attesting_balance(state, matching_target_attestations) * 3 >= get_total_active_balance(state) * 2: + if current_epoch_target_balance * 3 >= total_active_balance * 2: state.current_justified_checkpoint = Checkpoint(epoch=current_epoch, root=get_block_root(state, current_epoch)) state.justification_bits[0] = 0b1 From bb85ef5a579c2c2d1cb015e8de6900979e8a35af Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 22 Mar 2021 10:50:18 -0600 Subject: [PATCH 2/2] pr cleanup --- specs/altair/beacon-chain.md | 12 ++++++------ specs/phase0/beacon-chain.md | 15 ++++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/specs/altair/beacon-chain.md b/specs/altair/beacon-chain.md index 3f702dca4..8be2f04ec 100644 --- a/specs/altair/beacon-chain.md +++ b/specs/altair/beacon-chain.md @@ -583,12 +583,12 @@ def process_justification_and_finalization(state: BeaconState) -> None: # Skip FFG updates in the first two epochs to avoid corner cases that might result in modifying this stub. if get_current_epoch(state) <= GENESIS_EPOCH + 1: return - previous = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state)) - current = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, get_current_epoch(state)) - weigh_justification_and_finalization( - state, get_total_active_balance(state), - get_total_balance(state, previous), - get_total_balance(state, current)) + previous_indices = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state)) + current_indices = get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, get_current_epoch(state)) + total_active_balance = get_total_active_balance(state) + previous_target_balance = get_total_balance(state, previous_indices) + current_target_balance = get_total_balance(state, current_indices) + weigh_justification_and_finalization(state, total_active_balance, previous_target_balance, current_target_balance) ``` #### Inactivity scores diff --git a/specs/phase0/beacon-chain.md b/specs/phase0/beacon-chain.md index d978c4a75..e56a8ea22 100644 --- a/specs/phase0/beacon-chain.md +++ b/specs/phase0/beacon-chain.md @@ -1320,16 +1320,17 @@ def process_justification_and_finalization(state: BeaconState) -> None: # Skip FFG updates in the first two epochs to avoid corner cases that might result in modifying this stub. if get_current_epoch(state) <= GENESIS_EPOCH + 1: return - previous = get_matching_target_attestations(state, get_previous_epoch(state)) - current = get_matching_target_attestations(state, get_current_epoch(state)) - weigh_justification_and_finalization( - state, get_total_active_balance(state), - get_attesting_balance(state, previous), - get_attesting_balance(state, current)) + previous_attestations = get_matching_target_attestations(state, get_previous_epoch(state)) + current_attestations = get_matching_target_attestations(state, get_current_epoch(state)) + total_active_balance = get_total_active_balance(state) + previous_target_balance = get_attesting_balance(state, previous_attestations) + current_target_balance = get_attesting_balance(state, current_attestations) + weigh_justification_and_finalization(state, total_active_balance, previous_target_balance, current_target_balance) ``` ```python -def weigh_justification_and_finalization(state: BeaconState, total_active_balance: Gwei, +def weigh_justification_and_finalization(state: BeaconState, + total_active_balance: Gwei, previous_epoch_target_balance: Gwei, current_epoch_target_balance: Gwei) -> None: previous_epoch = get_previous_epoch(state)