mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-24 21:40:03 +00:00
146fa48454
* use ForkedHashedBeaconState in StateData * fix FAR_FUTURE_EPOCH -> slot overflow; almost always use assign() * avoid stack allocation in maybeUpgradeStateToAltair() * create and use dispatch functions for check_attester_slashing(), check_proposer_slashing(), and check_voluntary_exit() * use getStateRoot() instead of various state.data.hbsPhase0.root * remove withStateVars.hashedState(), which doesn't work as a design anymore * introduce spec/datatypes/altair into beacon_chain_db * fix inefficient codegen for getStateField(largeStateField) * state_transition_slots() doesn't either need/use blocks or runtime presets * combine process_slots(HBS)/state_transition_slots(HBS) which differ only in last-slot htr optimization * getStateField(StateData, ...) was replaced by getStateField(ForkedHashedBeaconState, ...) * fix rollback * switch some state_transition(), process_slots, makeTestBlocks(), etc to use ForkedHashedBeaconState * remove state_transition(phase0.HashedBeaconState) * remove process_slots(phase0.HashedBeaconState) * remove state_transition_block(phase0.HashedBeaconState) * remove unused callWithBS(); separate case expression from if statement * switch back from nested-ref-object construction to (ref Foo)(Bar())
44 lines
2.1 KiB
Nim
44 lines
2.1 KiB
Nim
# beacon_chain
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
# Licensed and distributed under either of
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
import
|
|
./datatypes, ./digest, ./forkedbeaconstate_helpers, ./helpers
|
|
|
|
const
|
|
SAFETY_DECAY* = 10'u64
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/weak-subjectivity.md#calculating-the-weak-subjectivity-period
|
|
func compute_weak_subjectivity_period(state: ForkedHashedBeaconState): uint64 =
|
|
var weak_subjectivity_period = MIN_VALIDATOR_WITHDRAWABILITY_DELAY
|
|
let validator_count =
|
|
get_active_validator_indices_len(state.hbsPhase0.data, get_current_epoch(state))
|
|
if validator_count >= MIN_PER_EPOCH_CHURN_LIMIT * CHURN_LIMIT_QUOTIENT:
|
|
weak_subjectivity_period += SAFETY_DECAY * CHURN_LIMIT_QUOTIENT div (2 * 100)
|
|
else:
|
|
weak_subjectivity_period += SAFETY_DECAY * validator_count div (2 * 100 * MIN_PER_EPOCH_CHURN_LIMIT)
|
|
return weak_subjectivity_period
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/weak-subjectivity.md#checking-for-stale-weak-subjectivity-checkpoint
|
|
func is_within_weak_subjectivity_period*(current_slot: Slot,
|
|
ws_state: ForkedHashedBeaconState,
|
|
ws_checkpoint: Checkpoint): bool =
|
|
# Clients may choose to validate the input state against the input Weak Subjectivity Checkpoint
|
|
doAssert getStateField(ws_state, latest_block_header).state_root ==
|
|
ws_checkpoint.root
|
|
doAssert compute_epoch_at_slot(getStateField(ws_state, slot)) ==
|
|
ws_checkpoint.epoch
|
|
|
|
let
|
|
ws_period = compute_weak_subjectivity_period(ws_state)
|
|
ws_state_epoch = compute_epoch_at_slot(getStateField(ws_state, slot))
|
|
current_epoch = compute_epoch_at_slot(current_slot)
|
|
|
|
current_epoch <= ws_state_epoch + ws_period
|
|
|