2020-11-27 22:16:13 +00:00
|
|
|
# beacon_chain
|
2021-02-25 13:37:22 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2020-11-27 22:16:13 +00:00
|
|
|
# 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.
|
2020-09-22 20:42:42 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-11-27 22:16:13 +00:00
|
|
|
import
|
|
|
|
./datatypes, ./digest, ./helpers
|
|
|
|
|
2020-09-22 20:42:42 +00:00
|
|
|
const
|
|
|
|
SAFETY_DECAY* = 10'u64
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/weak-subjectivity.md#calculating-the-weak-subjectivity-period
|
2020-09-22 20:42:42 +00:00
|
|
|
func compute_weak_subjectivity_period*(state: BeaconState): uint64 =
|
|
|
|
var weak_subjectivity_period = MIN_VALIDATOR_WITHDRAWABILITY_DELAY
|
|
|
|
let validator_count = get_active_validator_indices_len(state, 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
|
|
|
|
|
2021-02-25 13:37:22 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/weak-subjectivity.md#checking-for-stale-weak-subjectivity-checkpoint
|
2020-09-22 20:42:42 +00:00
|
|
|
func is_within_weak_subjectivity_period*(current_slot: Slot,
|
|
|
|
ws_state: BeaconState,
|
2020-10-06 15:32:17 +00:00
|
|
|
ws_checkpoint: Checkpoint): bool =
|
2020-09-22 20:42:42 +00:00
|
|
|
# Clients may choose to validate the input state against the input Weak Subjectivity Checkpoint
|
|
|
|
doAssert ws_state.latest_block_header.state_root == ws_checkpoint.root
|
|
|
|
doAssert compute_epoch_at_slot(ws_state.slot) == ws_checkpoint.epoch
|
|
|
|
|
|
|
|
let
|
|
|
|
ws_period = compute_weak_subjectivity_period(ws_state)
|
|
|
|
ws_state_epoch = compute_epoch_at_slot(ws_state.slot)
|
|
|
|
current_epoch = compute_epoch_at_slot(current_slot)
|
2020-11-27 22:16:13 +00:00
|
|
|
|
2020-09-22 20:42:42 +00:00
|
|
|
current_epoch <= ws_state_epoch + ws_period
|
|
|
|
|