avoid repeated integer_squareroots for 20% faster Altair epoch processing (#2797)

* avoid repeated integer_squareroots for 20% faster altair epoch processing

* the square root of a Gwei isn't a Gwei
This commit is contained in:
tersec 2021-08-20 14:41:32 +00:00 committed by GitHub
parent a67970e9a4
commit 7d048ce783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,8 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
# State transition - epoch processing, as described in # State transition - epoch processing, as described in
# https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#beacon-chain-state-transition-function # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/phase0/beacon-chain.md#epoch-processing and
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#epoch-processing
# #
# The entry point is `process_epoch`, which is at the bottom of this file. # The entry point is `process_epoch`, which is at the bottom of this file.
# #
@ -35,7 +36,7 @@ export extras, phase0, altair
logScope: topics = "consens" logScope: topics = "consens"
# Accessors that implement the max condition in `get_total_balance`: # Accessors that implement the max condition in `get_total_balance`:
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_total_balance # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_total_balance
template current_epoch*(v: TotalBalances): Gwei = template current_epoch*(v: TotalBalances): Gwei =
max(EFFECTIVE_BALANCE_INCREMENT, v.current_epoch_raw) max(EFFECTIVE_BALANCE_INCREMENT, v.current_epoch_raw)
template previous_epoch*(v: TotalBalances): Gwei = template previous_epoch*(v: TotalBalances): Gwei =
@ -158,7 +159,7 @@ func is_eligible_validator*(validator: RewardStatus): bool =
# Spec # Spec
# -------------------------------------------------------- # --------------------------------------------------------
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#get_unslashed_participating_indices # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#get_unslashed_participating_indices
func get_unslashed_participating_indices( func get_unslashed_participating_indices(
state: altair.BeaconState, flag_index: int, epoch: Epoch): state: altair.BeaconState, flag_index: int, epoch: Epoch):
HashSet[ValidatorIndex] = HashSet[ValidatorIndex] =
@ -182,7 +183,7 @@ func get_unslashed_participating_indices(
res.incl validator_index res.incl validator_index
res res
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization
proc process_justification_and_finalization*(state: var phase0.BeaconState, proc process_justification_and_finalization*(state: var phase0.BeaconState,
total_balances: TotalBalances, flags: UpdateFlags = {}) {.nbench.} = total_balances: TotalBalances, flags: UpdateFlags = {}) {.nbench.} =
# Initial FFG checkpoint values have a `0x00` stub for `root`. # Initial FFG checkpoint values have a `0x00` stub for `root`.
@ -204,7 +205,7 @@ proc process_justification_and_finalization*(state: var phase0.BeaconState,
## state.justification_bits[1:] = state.justification_bits[:-1] ## state.justification_bits[1:] = state.justification_bits[:-1]
## state.justification_bits[0] = 0b0 ## state.justification_bits[0] = 0b0
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#constants # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#constants
const JUSTIFICATION_BITS_LENGTH = 4 const JUSTIFICATION_BITS_LENGTH = 4
state.justification_bits = (state.justification_bits shl 1) and state.justification_bits = (state.justification_bits shl 1) and
@ -279,8 +280,8 @@ proc process_justification_and_finalization*(state: var phase0.BeaconState,
current_epoch = current_epoch, current_epoch = current_epoch,
checkpoint = shortLog(state.finalized_checkpoint) checkpoint = shortLog(state.finalized_checkpoint)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#justification-and-finalization # https://github.com/ethereum/consensus-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#justification-and-finalization
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/phase0/beacon-chain.md#justification-and-finalization # https://github.com/ethereum/consensus-specs/blob/v1.1.0-alpha.7/specs/phase0/beacon-chain.md#justification-and-finalization
# TODO merge these things -- effectively, the phase0 process_justification_and_finalization is mostly a stub in this world # TODO merge these things -- effectively, the phase0 process_justification_and_finalization is mostly a stub in this world
proc weigh_justification_and_finalization(state: var altair.BeaconState, proc weigh_justification_and_finalization(state: var altair.BeaconState,
total_active_balance: Gwei, total_active_balance: Gwei,
@ -300,7 +301,7 @@ proc weigh_justification_and_finalization(state: var altair.BeaconState,
## state.justification_bits[1:] = state.justification_bits[:-1] ## state.justification_bits[1:] = state.justification_bits[:-1]
## state.justification_bits[0] = 0b0 ## state.justification_bits[0] = 0b0
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#constants # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#constants
const JUSTIFICATION_BITS_LENGTH = 4 const JUSTIFICATION_BITS_LENGTH = 4
state.justification_bits = (state.justification_bits shl 1) and state.justification_bits = (state.justification_bits shl 1) and
@ -384,7 +385,7 @@ proc process_justification_and_finalization*(state: var altair.BeaconState,
return return
let let
# these ultimately differ from phase0 only in these lines # these ultimately differ from phase0 only in these lines
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/phase0/beacon-chain.md#justification-and-finalization # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/phase0/beacon-chain.md#justification-and-finalization
previous_indices = get_unslashed_participating_indices( previous_indices = get_unslashed_participating_indices(
state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state)) state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state))
current_indices = get_unslashed_participating_indices( current_indices = get_unslashed_participating_indices(
@ -395,7 +396,7 @@ proc process_justification_and_finalization*(state: var altair.BeaconState,
state, total_active_balance, previous_target_balance, state, total_active_balance, previous_target_balance,
current_target_balance, flags) current_target_balance, flags)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#helpers # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#helpers
func get_base_reward_sqrt*(state: phase0.BeaconState, index: ValidatorIndex, func get_base_reward_sqrt*(state: phase0.BeaconState, index: ValidatorIndex,
total_balance_sqrt: auto): Gwei = total_balance_sqrt: auto): Gwei =
# Spec function recalculates total_balance every time, which creates an # Spec function recalculates total_balance every time, which creates an
@ -414,7 +415,7 @@ func is_in_inactivity_leak(finality_delay: uint64): bool =
func get_finality_delay(state: SomeBeaconState): uint64 = func get_finality_delay(state: SomeBeaconState): uint64 =
get_previous_epoch(state) - state.finalized_checkpoint.epoch get_previous_epoch(state) - state.finalized_checkpoint.epoch
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/phase0/beacon-chain.md#rewards-and-penalties-1 # https://github.com/ethereum/consensus-specs/blob/v1.1.0-alpha.8/specs/phase0/beacon-chain.md#rewards-and-penalties-1
func is_in_inactivity_leak(state: altair.BeaconState): bool = func is_in_inactivity_leak(state: altair.BeaconState): bool =
# TODO remove this, see above # TODO remove this, see above
get_finality_delay(state) > MIN_EPOCHS_TO_INACTIVITY_PENALTY get_finality_delay(state) > MIN_EPOCHS_TO_INACTIVITY_PENALTY
@ -439,7 +440,7 @@ func get_attestation_component_delta(is_unslashed_attester: bool,
else: else:
RewardDelta(penalties: base_reward) RewardDelta(penalties: base_reward)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#components-of-attestation-deltas # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#components-of-attestation-deltas
func get_source_delta*(validator: RewardStatus, func get_source_delta*(validator: RewardStatus,
base_reward: uint64, base_reward: uint64,
total_balances: TotalBalances, total_balances: TotalBalances,
@ -517,7 +518,7 @@ func get_inactivity_penalty_delta*(validator: RewardStatus,
delta delta
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_attestation_deltas # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#get_attestation_deltas
func get_attestation_deltas(state: phase0.BeaconState, rewards: var RewardInfo) = func get_attestation_deltas(state: phase0.BeaconState, rewards: var RewardInfo) =
## Update rewards with attestation reward/penalty deltas for each validator. ## Update rewards with attestation reward/penalty deltas for each validator.
@ -561,27 +562,25 @@ func get_attestation_deltas(state: phase0.BeaconState, rewards: var RewardInfo)
rewards.statuses[proposer_index].delta.add( rewards.statuses[proposer_index].delta.add(
proposer_delta.get()[1]) proposer_delta.get()[1])
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/beacon-chain.md#get_base_reward_per_increment # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#get_base_reward_per_increment
func get_base_reward_per_increment( func get_base_reward_per_increment(
state: altair.BeaconState, total_active_balance: Gwei): Gwei = state: altair.BeaconState, total_active_balance_sqrt: uint64): Gwei =
# TODO hoist this integer_squareroot, as with phase 0 EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR div total_active_balance_sqrt
EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR div
integer_squareroot(total_active_balance)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/beacon-chain.md#get_base_reward # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#get_base_reward
func get_base_reward( func get_base_reward(
state: altair.BeaconState, index: ValidatorIndex, total_active_balance: Gwei): state: altair.BeaconState, index: ValidatorIndex,
Gwei = total_active_balance_sqrt: uint64): Gwei =
## Return the base reward for the validator defined by ``index`` with respect ## Return the base reward for the validator defined by ``index`` with respect
## to the current ``state``. ## to the current ``state``.
let increments = let increments =
state.validators[index].effective_balance div EFFECTIVE_BALANCE_INCREMENT state.validators[index].effective_balance div EFFECTIVE_BALANCE_INCREMENT
increments * get_base_reward_per_increment(state, total_active_balance) increments * get_base_reward_per_increment(state, total_active_balance_sqrt)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#get_flag_index_deltas # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#get_flag_index_deltas
iterator get_flag_index_deltas( iterator get_flag_index_deltas(
state: altair.BeaconState, flag_index: int, total_active_balance: Gwei): state: altair.BeaconState, flag_index: int, total_active_balance: Gwei,
(ValidatorIndex, Gwei, Gwei) = total_active_balance_sqrt: uint64): (ValidatorIndex, Gwei, Gwei) =
## Return the deltas for a given ``flag_index`` by scanning through the ## Return the deltas for a given ``flag_index`` by scanning through the
## participation flags. ## participation flags.
let let
@ -603,7 +602,7 @@ iterator get_flag_index_deltas(
continue continue
template vidx: ValidatorIndex = index.ValidatorIndex template vidx: ValidatorIndex = index.ValidatorIndex
let base_reward = get_base_reward(state, vidx, total_active_balance) let base_reward = get_base_reward(state, vidx, total_active_balance_sqrt)
yield yield
if vidx in unslashed_participating_indices: if vidx in unslashed_participating_indices:
if not is_in_inactivity_leak(state): if not is_in_inactivity_leak(state):
@ -617,7 +616,7 @@ iterator get_flag_index_deltas(
else: else:
(vidx, 0.Gwei, 0.Gwei) (vidx, 0.Gwei, 0.Gwei)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#modified-get_inactivity_penalty_deltas # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#modified-get_inactivity_penalty_deltas
iterator get_inactivity_penalty_deltas(cfg: RuntimeConfig, state: altair.BeaconState): iterator get_inactivity_penalty_deltas(cfg: RuntimeConfig, state: altair.BeaconState):
(ValidatorIndex, Gwei) = (ValidatorIndex, Gwei) =
## Return the inactivity penalty deltas by considering timely target ## Return the inactivity penalty deltas by considering timely target
@ -643,7 +642,7 @@ iterator get_inactivity_penalty_deltas(cfg: RuntimeConfig, state: altair.BeaconS
state.inactivity_scores[index] state.inactivity_scores[index]
yield (vidx, Gwei(penalty_numerator div penalty_denominator)) yield (vidx, Gwei(penalty_numerator div penalty_denominator))
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#process_rewards_and_penalties # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#rewards-and-penalties
func process_rewards_and_penalties( func process_rewards_and_penalties(
state: var phase0.BeaconState, rewards: var RewardInfo) {.nbench.} = state: var phase0.BeaconState, rewards: var RewardInfo) {.nbench.} =
# No rewards are applied at the end of `GENESIS_EPOCH` because rewards are # No rewards are applied at the end of `GENESIS_EPOCH` because rewards are
@ -664,7 +663,7 @@ func process_rewards_and_penalties(
increase_balance(state.balances.asSeq()[idx], v.delta.rewards) increase_balance(state.balances.asSeq()[idx], v.delta.rewards)
decrease_balance(state.balances.asSeq()[idx], v.delta.penalties) decrease_balance(state.balances.asSeq()[idx], v.delta.penalties)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/beacon-chain.md#rewards-and-penalties # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#rewards-and-penalties
func process_rewards_and_penalties( func process_rewards_and_penalties(
cfg: RuntimeConfig, state: var altair.BeaconState, total_active_balance: Gwei) {.nbench.} = cfg: RuntimeConfig, state: var altair.BeaconState, total_active_balance: Gwei) {.nbench.} =
if get_current_epoch(state) == GENESIS_EPOCH: if get_current_epoch(state) == GENESIS_EPOCH:
@ -680,9 +679,11 @@ func process_rewards_and_penalties(
rewards = newSeq[Gwei](state.validators.len) rewards = newSeq[Gwei](state.validators.len)
penalties = newSeq[Gwei](state.validators.len) penalties = newSeq[Gwei](state.validators.len)
let total_active_balance_sqrt = integer_squareroot(total_active_balance)
for flag_index in 0 ..< PARTICIPATION_FLAG_WEIGHTS.len: for flag_index in 0 ..< PARTICIPATION_FLAG_WEIGHTS.len:
for validator_index, reward, penalty in get_flag_index_deltas( for validator_index, reward, penalty in get_flag_index_deltas(
state, flag_index, total_active_balance): state, flag_index, total_active_balance, total_active_balance_sqrt):
rewards[validator_index] += reward rewards[validator_index] += reward
penalties[validator_index] += penalty penalties[validator_index] += penalty
@ -693,8 +694,8 @@ func process_rewards_and_penalties(
increase_balance(state, ValidatorIndex(index), rewards[index]) increase_balance(state, ValidatorIndex(index), rewards[index])
decrease_balance(state, ValidatorIndex(index), penalties[index]) decrease_balance(state, ValidatorIndex(index), penalties[index])
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#slashings # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#slashings
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/beacon-chain.md#slashings # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#slashings
func process_slashings*(state: var SomeBeaconState, total_balance: Gwei) {.nbench.}= func process_slashings*(state: var SomeBeaconState, total_balance: Gwei) {.nbench.}=
let let
epoch = get_current_epoch(state) epoch = get_current_epoch(state)
@ -722,7 +723,7 @@ func process_slashings*(state: var SomeBeaconState, total_balance: Gwei) {.nbenc
let penalty = penalty_numerator div total_balance * increment let penalty = penalty_numerator div total_balance * increment
decrease_balance(state, index.ValidatorIndex, penalty) decrease_balance(state, index.ValidatorIndex, penalty)
# https://github.com/ethereum/eth2.0-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#eth1-data-votes-updates # https://github.com/ethereum/consensus-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#eth1-data-votes-updates
func process_eth1_data_reset*(state: var SomeBeaconState) {.nbench.} = func process_eth1_data_reset*(state: var SomeBeaconState) {.nbench.} =
let next_epoch = get_current_epoch(state) + 1 let next_epoch = get_current_epoch(state) + 1
@ -730,7 +731,7 @@ func process_eth1_data_reset*(state: var SomeBeaconState) {.nbench.} =
if next_epoch mod EPOCHS_PER_ETH1_VOTING_PERIOD == 0: if next_epoch mod EPOCHS_PER_ETH1_VOTING_PERIOD == 0:
state.eth1_data_votes = default(type state.eth1_data_votes) state.eth1_data_votes = default(type state.eth1_data_votes)
# https://github.com/ethereum/eth2.0-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#effective-balances-updates # https://github.com/ethereum/consensus-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#effective-balances-updates
func process_effective_balance_updates*(state: var SomeBeaconState) {.nbench.} = func process_effective_balance_updates*(state: var SomeBeaconState) {.nbench.} =
# Update effective balances with hysteresis # Update effective balances with hysteresis
for index in 0..<state.validators.len: for index in 0..<state.validators.len:
@ -749,14 +750,14 @@ func process_effective_balance_updates*(state: var SomeBeaconState) {.nbench.} =
balance - balance mod EFFECTIVE_BALANCE_INCREMENT, balance - balance mod EFFECTIVE_BALANCE_INCREMENT,
MAX_EFFECTIVE_BALANCE) MAX_EFFECTIVE_BALANCE)
# https://github.com/ethereum/eth2.0-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#slashings-balances-updates # https://github.com/ethereum/consensus-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#slashings-balances-updates
func process_slashings_reset*(state: var SomeBeaconState) {.nbench.} = func process_slashings_reset*(state: var SomeBeaconState) {.nbench.} =
let next_epoch = get_current_epoch(state) + 1 let next_epoch = get_current_epoch(state) + 1
# Reset slashings # Reset slashings
state.slashings[int(next_epoch mod EPOCHS_PER_SLASHINGS_VECTOR)] = 0.Gwei state.slashings[int(next_epoch mod EPOCHS_PER_SLASHINGS_VECTOR)] = 0.Gwei
# https://github.com/ethereum/eth2.0-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#randao-mixes-updates # https://github.com/ethereum/consensus-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#randao-mixes-updates
func process_randao_mixes_reset*(state: var SomeBeaconState) {.nbench.} = func process_randao_mixes_reset*(state: var SomeBeaconState) {.nbench.} =
let let
current_epoch = get_current_epoch(state) current_epoch = get_current_epoch(state)
@ -766,7 +767,7 @@ func process_randao_mixes_reset*(state: var SomeBeaconState) {.nbench.} =
state.randao_mixes[next_epoch mod EPOCHS_PER_HISTORICAL_VECTOR] = state.randao_mixes[next_epoch mod EPOCHS_PER_HISTORICAL_VECTOR] =
get_randao_mix(state, current_epoch) get_randao_mix(state, current_epoch)
# https://github.com/ethereum/eth2.0-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#historical-roots-updates # https://github.com/ethereum/consensus-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#historical-roots-updates
func process_historical_roots_update*(state: var SomeBeaconState) {.nbench.} = func process_historical_roots_update*(state: var SomeBeaconState) {.nbench.} =
# Set historical root accumulator # Set historical root accumulator
let next_epoch = get_current_epoch(state) + 1 let next_epoch = get_current_epoch(state) + 1
@ -774,20 +775,20 @@ func process_historical_roots_update*(state: var SomeBeaconState) {.nbench.} =
if next_epoch mod (SLOTS_PER_HISTORICAL_ROOT div SLOTS_PER_EPOCH) == 0: if next_epoch mod (SLOTS_PER_HISTORICAL_ROOT div SLOTS_PER_EPOCH) == 0:
# Equivalent to hash_tree_root(foo: HistoricalBatch), but without using # Equivalent to hash_tree_root(foo: HistoricalBatch), but without using
# significant additional stack or heap. # significant additional stack or heap.
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#historicalbatch # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#historicalbatch
# In response to https://github.com/status-im/nimbus-eth2/issues/921 # In response to https://github.com/status-im/nimbus-eth2/issues/921
if not state.historical_roots.add hash_tree_root( if not state.historical_roots.add hash_tree_root(
[hash_tree_root(state.block_roots), hash_tree_root(state.state_roots)]): [hash_tree_root(state.block_roots), hash_tree_root(state.state_roots)]):
raiseAssert "no more room for historical roots, so long and thanks for the fish!" raiseAssert "no more room for historical roots, so long and thanks for the fish!"
# https://github.com/ethereum/eth2.0-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#participation-records-rotation # https://github.com/ethereum/consensus-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#participation-records-rotation
func process_participation_record_updates*(state: var phase0.BeaconState) {.nbench.} = func process_participation_record_updates*(state: var phase0.BeaconState) {.nbench.} =
# Rotate current/previous epoch attestations - using swap avoids copying all # Rotate current/previous epoch attestations - using swap avoids copying all
# elements using a slow genericSeqAssign # elements using a slow genericSeqAssign
state.previous_epoch_attestations.clear() state.previous_epoch_attestations.clear()
swap(state.previous_epoch_attestations, state.current_epoch_attestations) swap(state.previous_epoch_attestations, state.current_epoch_attestations)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#participation-flags-updates # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#participation-flags-updates
func process_participation_flag_updates*(state: var altair.BeaconState) = func process_participation_flag_updates*(state: var altair.BeaconState) =
state.previous_epoch_participation = state.current_epoch_participation state.previous_epoch_participation = state.current_epoch_participation
@ -801,14 +802,14 @@ func process_participation_flag_updates*(state: var altair.BeaconState) =
state.current_epoch_participation.resetCache() state.current_epoch_participation.resetCache()
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/beacon-chain.md#sync-committee-updates # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#sync-committee-updates
proc process_sync_committee_updates*(state: var altair.BeaconState) = proc process_sync_committee_updates*(state: var altair.BeaconState) =
let next_epoch = get_current_epoch(state) + 1 let next_epoch = get_current_epoch(state) + 1
if next_epoch mod EPOCHS_PER_SYNC_COMMITTEE_PERIOD == 0: if next_epoch mod EPOCHS_PER_SYNC_COMMITTEE_PERIOD == 0:
state.current_sync_committee = state.next_sync_committee state.current_sync_committee = state.next_sync_committee
state.next_sync_committee = get_next_sync_committee(state) state.next_sync_committee = get_next_sync_committee(state)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.8/specs/altair/beacon-chain.md#inactivity-scores # https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/altair/beacon-chain.md#inactivity-scores
func process_inactivity_updates*(cfg: RuntimeConfig, state: var altair.BeaconState) = func process_inactivity_updates*(cfg: RuntimeConfig, state: var altair.BeaconState) =
# Score updates based on previous epoch participation, skip genesis epoch # Score updates based on previous epoch participation, skip genesis epoch
if get_current_epoch(state) == GENESIS_EPOCH: if get_current_epoch(state) == GENESIS_EPOCH:
@ -836,7 +837,7 @@ func process_inactivity_updates*(cfg: RuntimeConfig, state: var altair.BeaconSta
if not is_in_inactivity_leak(state): if not is_in_inactivity_leak(state):
state.inactivity_scores[index] -= min(INACTIVITY_SCORE_RECOVERY_RATE.uint64, state.inactivity_scores[index]) state.inactivity_scores[index] -= min(INACTIVITY_SCORE_RECOVERY_RATE.uint64, state.inactivity_scores[index])
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#epoch-processing # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#epoch-processing
proc process_epoch*( proc process_epoch*(
cfg: RuntimeConfig, state: var phase0.BeaconState, flags: UpdateFlags, cfg: RuntimeConfig, state: var phase0.BeaconState, flags: UpdateFlags,
cache: var StateCache, rewards: var RewardInfo) {.nbench.} = cache: var StateCache, rewards: var RewardInfo) {.nbench.} =
@ -846,7 +847,7 @@ proc process_epoch*(
init(rewards, state) init(rewards, state)
rewards.process_attestations(state, cache) rewards.process_attestations(state, cache)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization
process_justification_and_finalization( process_justification_and_finalization(
state, rewards.total_balances, flags) state, rewards.total_balances, flags)
@ -860,16 +861,16 @@ proc process_epoch*(
# the finalization rules triggered. # the finalization rules triggered.
doAssert state.finalized_checkpoint.epoch + 3 >= currentEpoch doAssert state.finalized_checkpoint.epoch + 3 >= currentEpoch
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#rewards-and-penalties-1 # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#rewards-and-penalties-1
process_rewards_and_penalties(state, rewards) process_rewards_and_penalties(state, rewards)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#registry-updates # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#registry-updates
process_registry_updates(cfg, state, cache) process_registry_updates(cfg, state, cache)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#slashings # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#slashings
process_slashings(state, rewards.total_balances.current_epoch) process_slashings(state, rewards.total_balances.current_epoch)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#final-updates # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#final-updates
process_eth1_data_reset(state) process_eth1_data_reset(state)
process_effective_balance_updates(state) process_effective_balance_updates(state)
process_slashings_reset(state) process_slashings_reset(state)
@ -877,7 +878,7 @@ proc process_epoch*(
process_historical_roots_update(state) process_historical_roots_update(state)
process_participation_record_updates(state) process_participation_record_updates(state)
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#epoch-processing # https://github.com/ethereum/consensus-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#epoch-processing
proc process_epoch*( proc process_epoch*(
cfg: RuntimeConfig, state: var altair.BeaconState, flags: UpdateFlags, cfg: RuntimeConfig, state: var altair.BeaconState, flags: UpdateFlags,
cache: var StateCache, rewards: var RewardInfo) {.nbench.} = cache: var StateCache, rewards: var RewardInfo) {.nbench.} =
@ -890,7 +891,7 @@ proc process_epoch*(
let total_active_balance = state.get_total_active_balance(cache) let total_active_balance = state.get_total_active_balance(cache)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization
process_justification_and_finalization(state, total_active_balance, flags) process_justification_and_finalization(state, total_active_balance, flags)
# state.slot hasn't been incremented yet. # state.slot hasn't been incremented yet.
@ -905,13 +906,13 @@ proc process_epoch*(
process_inactivity_updates(cfg, state) # [New in Altair] process_inactivity_updates(cfg, state) # [New in Altair]
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#rewards-and-penalties-1 # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#rewards-and-penalties-1
process_rewards_and_penalties(cfg, state, total_active_balance) process_rewards_and_penalties(cfg, state, total_active_balance)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#registry-updates # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#registry-updates
process_registry_updates(cfg, state, cache) process_registry_updates(cfg, state, cache)
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#slashings # https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#slashings
process_slashings(state, total_active_balance) process_slashings(state, total_active_balance)
process_eth1_data_reset(state) process_eth1_data_reset(state)