2020-11-27 22:16:13 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 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
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2020-09-22 20:42:42 +00:00
|
|
|
|
2020-11-27 22:16:13 +00:00
|
|
|
import
|
2023-05-10 08:23:59 +00:00
|
|
|
./datatypes/base, ./beaconstate, ./forks, ./helpers
|
2020-11-27 22:16:13 +00:00
|
|
|
|
2024-08-09 16:24:49 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.4/specs/phase0/weak-subjectivity.md#configuration
|
2023-05-10 08:23:59 +00:00
|
|
|
const SAFETY_DECAY* = 10'u64
|
2020-09-22 20:42:42 +00:00
|
|
|
|
2024-08-09 16:24:49 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.4/specs/phase0/weak-subjectivity.md#compute_weak_subjectivity_period
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
func compute_weak_subjectivity_period(
|
2023-05-10 08:23:59 +00:00
|
|
|
cfg: RuntimeConfig, state: ForkyBeaconState): uint64 =
|
|
|
|
## Returns the weak subjectivity period for the current ``state``.
|
|
|
|
## This computation takes into account the effect of:
|
|
|
|
## - validator set churn
|
|
|
|
## (bounded by ``get_validator_churn_limit()`` per epoch), and
|
|
|
|
## - validator balance top-ups
|
|
|
|
## (bounded by ``MAX_DEPOSITS * SLOTS_PER_EPOCH`` per epoch).
|
|
|
|
## A detailed calculation can be found at:
|
|
|
|
## https://github.com/runtimeverification/beacon-chain-verification/blob/master/weak-subjectivity/weak-subjectivity-analysis.pdf
|
|
|
|
var
|
|
|
|
cache: StateCache
|
|
|
|
ws_period = cfg.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
|
|
|
|
let
|
|
|
|
N = get_active_validator_indices_len(state, get_current_epoch(state))
|
2024-03-19 13:22:07 +00:00
|
|
|
t = (get_total_active_balance(state, cache) div N).toEther()
|
|
|
|
const T = MAX_EFFECTIVE_BALANCE.Gwei.toEther()
|
2023-05-10 08:23:59 +00:00
|
|
|
let delta = cfg.get_validator_churn_limit(state, cache)
|
|
|
|
const
|
|
|
|
Delta = MAX_DEPOSITS * SLOTS_PER_EPOCH
|
|
|
|
D = SAFETY_DECAY
|
|
|
|
|
|
|
|
if T * (200 + 3 * D) < t * (200 + 12 * D):
|
|
|
|
let
|
|
|
|
epochs_for_validator_set_churn =
|
|
|
|
N * (t * (200 + 12 * D) - T * (200 + 3 * D)) div
|
|
|
|
(600 * delta * (2 * t + T))
|
|
|
|
epochs_for_balance_top_ups =
|
|
|
|
N * (200 + 3 * D) div (600 * Delta)
|
|
|
|
ws_period += max(epochs_for_validator_set_churn, epochs_for_balance_top_ups)
|
2020-09-22 20:42:42 +00:00
|
|
|
else:
|
2023-05-10 08:23:59 +00:00
|
|
|
ws_period += 3 * N * D * t div (200 * Delta * (T - t))
|
|
|
|
|
|
|
|
ws_period
|
2020-09-22 20:42:42 +00:00
|
|
|
|
2024-08-09 16:24:49 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.4/specs/phase0/weak-subjectivity.md#is_within_weak_subjectivity_period
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
func is_within_weak_subjectivity_period*(cfg: RuntimeConfig, current_slot: Slot,
|
2021-06-11 17:51:46 +00:00
|
|
|
ws_state: ForkedHashedBeaconState,
|
2020-10-06 15:32:17 +00:00
|
|
|
ws_checkpoint: Checkpoint): bool =
|
2023-05-10 08:23:59 +00:00
|
|
|
## Clients may choose to validate the input state against the input Weak Subjectivity Checkpoint
|
2021-05-21 09:23:28 +00:00
|
|
|
doAssert getStateField(ws_state, latest_block_header).state_root ==
|
|
|
|
ws_checkpoint.root
|
2022-01-11 10:01:54 +00:00
|
|
|
doAssert epoch(getStateField(ws_state, slot)) == ws_checkpoint.epoch
|
2020-09-22 20:42:42 +00:00
|
|
|
|
|
|
|
let
|
2023-05-10 08:23:59 +00:00
|
|
|
ws_period = withState(ws_state):
|
|
|
|
cfg.compute_weak_subjectivity_period(forkyState.data)
|
2022-01-11 10:01:54 +00:00
|
|
|
ws_state_epoch = epoch(getStateField(ws_state, slot))
|
|
|
|
current_epoch = epoch(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
|