Split up big function into two smaller functions

This commit is contained in:
vbuterin 2019-03-01 19:34:26 -06:00 committed by GitHub
parent 7c6232a602
commit 2d4c8af489
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 80 additions and 59 deletions

View File

@ -1825,6 +1825,14 @@ def get_previous_epoch_boundary_attestations(state: BeaconState) -> List[Pending
] ]
``` ```
```python
def get_previous_epoch_matching_head_attestations(state: BeaconState) -> List[PendingAttestation]:
return [
a for a in get_previous_epoch_attestations(state) if
a.data.beacon_block_root == get_block_root(state, a.data.slot)
]
```
**Note**: Total balances computed for the previous epoch might be marginally different than the actual total balances during the previous epoch transition. Due to the tight bound on validator churn each epoch and small per-epoch rewards/penalties, the potential balance difference is very low and only marginally affects consensus safety. **Note**: Total balances computed for the previous epoch might be marginally different than the actual total balances during the previous epoch transition. Due to the tight bound on validator churn each epoch and small per-epoch rewards/penalties, the potential balance difference is very low and only marginally affects consensus safety.
```python ```python
@ -1957,24 +1965,28 @@ Note: When applying penalties in the following balance recalculations implemente
##### Justification and finalization ##### Justification and finalization
```python ```python
def get_justification_and_finalization_deltas(state: BeaconState) -> Dict[ValidatorIndex, Gwei]: def get_justification_and_finalization_deltas(state: BeaconState) -> List[Gwei]:
active_validator_indices = get_active_validator_indices(state.validator_registry, previous_epoch)
epochs_since_finality = get_current_epoch(state) + 1 - state.finalized_epoch epochs_since_finality = get_current_epoch(state) + 1 - state.finalized_epoch
# Initialize deltas that we are returning to zero if epochs_since_finality <= 4:
deltas = {index: 0 for index in range(len(state.validator_registry))} return compute_normal_justification_and_finalization_deltas(state)
else:
return compute_inactivity_leak_deltas(state)
```
When blocks are finalizing normally...
```python
def compute_normal_justification_and_finalization_deltas(state: BeaconState) -> List[Gwei]:
deltas = [0 for index in range(len(state.validator_registry))]
# Some helper variables # Some helper variables
boundary_attestations = get_previous_epoch_boundary_attestations(state) boundary_attestations = get_previous_epoch_boundary_attestations(state)
boundary_attesting_balance = get_attesting_balance(boundary_attestations) boundary_attesting_balance = get_attesting_balance(boundary_attestations)
total_balance = get_previous_total_balance(state) total_balance = get_previous_total_balance(state)
total_attesting_balance = get_attesting_balance(get_previous_epoch_attestations(state)) total_attesting_balance = get_attesting_balance(get_previous_epoch_attestations(state))
matching_head_attestations = [ matching_head_attestations = get_previous_epoch_matching_head_attestations(state)
a for a in get_previous_epoch_attestations(state) if
a.data.beacon_block_root == get_block_root(state, a.data.slot)
]
matching_head_balance = get_attesting_balance(matching_head_attestations) matching_head_balance = get_attesting_balance(matching_head_attestations)
# Normal case # Process rewards or penalties for all validators
if epochs_since_finality <= 4: for index in get_active_validator_indices(state.validator_registry, previous_epoch):
for index in active_validator_indices:
# Expected FFG source # Expected FFG source
if index in get_attesting_indices(get_previous_attestations(state)): if index in get_attesting_indices(get_previous_attestations(state)):
deltas[index] += get_base_reward(state, index) * total_attesting_balance // total_balance deltas[index] += get_base_reward(state, index) * total_attesting_balance // total_balance
@ -1998,8 +2010,17 @@ def get_justification_and_finalization_deltas(state: BeaconState) -> Dict[Valida
# Proposer bonus # Proposer bonus
proposer_index = get_beacon_proposer_index(state, inclusion_slot(state, index)) proposer_index = get_beacon_proposer_index(state, inclusion_slot(state, index))
deltas[proposer_index] += base_reward(state, index) // ATTESTATION_INCLUSION_REWARD_QUOTIENT deltas[proposer_index] += base_reward(state, index) // ATTESTATION_INCLUSION_REWARD_QUOTIENT
# Quadratic leak case ```
else:
When blocks are not finalizing normally...
```python
def compute_inactivity_leak_deltas(state: BeaconState) -> List[Gwei]:
deltas = [0 for index in range(len(state.validator_registry))]
boundary_attestations = get_previous_epoch_boundary_attestations(state)
matching_head_attestations = get_previous_epoch_matching_head_attestations(state)
active_validator_indices = get_active_validator_indices(state.validator_registry, previous_epoch)
epochs_since_finality = get_current_epoch(state) + 1 - state.finalized_epoch
for index in active_validator_indices: for index in active_validator_indices:
if index not in get_attesting_indices(get_previous_epoch_attestations(state)): if index not in get_attesting_indices(get_previous_epoch_attestations(state)):
deltas[index] -= get_inactivity_penalty(state, index, epochs_since_finality) deltas[index] -= get_inactivity_penalty(state, index, epochs_since_finality)
@ -2028,8 +2049,8 @@ def get_justification_and_finalization_deltas(state: BeaconState) -> Dict[Valida
##### Crosslinks ##### Crosslinks
```python ```python
def get_crosslink_deltas(state: BeaconState) -> Dict[ValidatorIndex, Gwei]: def get_crosslink_deltas(state: BeaconState) -> List[Gwei]:
deltas = {index: 0 for index in range(len(state.validator_registry))} deltas = [0 for index in range(len(state.validator_registry))]
for slot in range(get_epoch_start_slot(previous_epoch), get_epoch_start_slot(current_epoch)): for slot in range(get_epoch_start_slot(previous_epoch), get_epoch_start_slot(current_epoch)):
for crosslink_committee, shard in get_crosslink_committees_at_slot(state, slot): for crosslink_committee, shard in get_crosslink_committees_at_slot(state, slot):
winning_root, participants = get_winning_root_and_participants(state, shard) winning_root, participants = get_winning_root_and_participants(state, shard)