2019-06-28 13:44:44 +00:00
|
|
|
# beacon_chain
|
2021-02-14 19:31:01 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-06-28 13:44:44 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * 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).
|
2019-06-28 13:44:44 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# State transition - epoch processing, as described in
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#epoch-processing and
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#epoch-processing
|
2019-06-28 13:44:44 +00:00
|
|
|
#
|
2020-06-09 09:42:53 +00:00
|
|
|
# The entry point is `process_epoch`, which is at the bottom of this file.
|
2019-06-28 13:44:44 +00:00
|
|
|
#
|
2020-11-04 21:52:47 +00:00
|
|
|
# General notes about the code:
|
2019-06-28 13:44:44 +00:00
|
|
|
# * Weird styling - the sections taken from the spec use python styling while
|
|
|
|
# the others use NEP-1 - helps grepping identifiers in spec
|
2020-11-04 21:52:47 +00:00
|
|
|
# * When updating the code, add TODO sections to mark where there are clear
|
|
|
|
# improvements to be made - other than that, keep things similar to spec unless
|
|
|
|
# motivated by security or performance considerations
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2019-11-12 05:35:52 +00:00
|
|
|
import
|
2021-10-12 11:36:52 +00:00
|
|
|
std/[algorithm, math],
|
2021-08-25 14:43:00 +00:00
|
|
|
stew/bitops2, chronicles,
|
2020-10-22 11:08:46 +00:00
|
|
|
../extras,
|
2021-09-27 14:22:58 +00:00
|
|
|
./datatypes/[phase0, altair, merge],
|
2021-08-18 18:57:58 +00:00
|
|
|
"."/[beaconstate, eth2_merkleization, helpers, validator],
|
2019-12-20 16:14:43 +00:00
|
|
|
../../nbench/bench_lab
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-08-12 13:08:20 +00:00
|
|
|
export extras, phase0, altair
|
|
|
|
|
2019-09-23 13:48:25 +00:00
|
|
|
# Logging utilities
|
|
|
|
# --------------------------------------------------------
|
|
|
|
|
|
|
|
logScope: topics = "consens"
|
|
|
|
|
2020-10-22 11:08:46 +00:00
|
|
|
# Accessors that implement the max condition in `get_total_balance`:
|
2021-10-13 16:49:06 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#get_total_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
template current_epoch*(v: TotalBalances): Gwei =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, v.current_epoch_raw)
|
|
|
|
template previous_epoch*(v: TotalBalances): Gwei =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, v.previous_epoch_raw)
|
|
|
|
template current_epoch_attesters*(v: TotalBalances): Gwei =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, v.current_epoch_attesters_raw)
|
|
|
|
template current_epoch_target_attesters*(v: TotalBalances): Gwei =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, v.current_epoch_target_attesters_raw)
|
|
|
|
template previous_epoch_attesters*(v: TotalBalances): Gwei =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, v.previous_epoch_attesters_raw)
|
|
|
|
template previous_epoch_target_attesters*(v: TotalBalances): Gwei =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, v.previous_epoch_target_attesters_raw)
|
|
|
|
template previous_epoch_head_attesters*(v: TotalBalances): Gwei =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, v.previous_epoch_head_attesters_raw)
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
func init*(info: var phase0.EpochInfo, state: phase0.BeaconState) =
|
|
|
|
info.total_balances = TotalBalances()
|
|
|
|
info.statuses.setLen(state.validators.len)
|
2021-05-07 11:36:21 +00:00
|
|
|
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
for i in 0..<state.validators.len:
|
|
|
|
let v = unsafeAddr state.validators[i]
|
2021-05-07 11:36:21 +00:00
|
|
|
var flags: set[RewardFlags]
|
|
|
|
|
|
|
|
if v[].slashed:
|
2021-05-28 15:25:58 +00:00
|
|
|
flags.incl(RewardFlags.isSlashed)
|
2021-05-07 11:36:21 +00:00
|
|
|
if state.get_current_epoch() >= v[].withdrawable_epoch:
|
2021-05-28 15:25:58 +00:00
|
|
|
flags.incl RewardFlags.canWithdrawInCurrentEpoch
|
2020-10-22 11:08:46 +00:00
|
|
|
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
if v[].is_active_validator(state.get_current_epoch()):
|
2021-10-13 14:24:36 +00:00
|
|
|
info.total_balances.current_epoch_raw += v[].effective_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
if v[].is_active_validator(state.get_previous_epoch()):
|
2021-05-28 15:25:58 +00:00
|
|
|
flags.incl RewardFlags.isActiveInPreviousEpoch
|
2021-10-13 14:24:36 +00:00
|
|
|
info.total_balances.previous_epoch_raw += v[].effective_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
info.statuses[i] = RewardStatus(
|
2021-05-07 11:36:21 +00:00
|
|
|
current_epoch_effective_balance: v[].effective_balance,
|
|
|
|
flags: flags,
|
|
|
|
)
|
|
|
|
|
|
|
|
func add(a: var RewardDelta, b: RewardDelta) =
|
2020-10-22 11:08:46 +00:00
|
|
|
a.rewards += b.rewards
|
|
|
|
a.penalties += b.penalties
|
|
|
|
|
|
|
|
func process_attestation(
|
2021-10-13 14:24:36 +00:00
|
|
|
info: var phase0.EpochInfo, state: phase0.BeaconState, a: PendingAttestation,
|
2020-10-22 11:08:46 +00:00
|
|
|
cache: var StateCache) =
|
|
|
|
# Collect information about the attestation
|
|
|
|
var
|
2021-05-07 11:36:21 +00:00
|
|
|
flags: set[RewardFlags]
|
2020-10-22 11:08:46 +00:00
|
|
|
is_previous_epoch_attester: Option[InclusionInfo]
|
|
|
|
|
|
|
|
if a.data.target.epoch == state.get_current_epoch():
|
2021-05-28 15:25:58 +00:00
|
|
|
flags.incl RewardFlags.isCurrentEpochAttester
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
if a.data.target.root == get_block_root(state, state.get_current_epoch()):
|
2021-05-28 15:25:58 +00:00
|
|
|
flags.incl RewardFlags.isCurrentEpochTargetAttester
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
elif a.data.target.epoch == state.get_previous_epoch():
|
|
|
|
is_previous_epoch_attester = some(InclusionInfo(
|
|
|
|
delay: a.inclusion_delay,
|
|
|
|
proposer_index: a.proposer_index,
|
|
|
|
))
|
|
|
|
|
|
|
|
if a.data.target.root == get_block_root(state, state.get_previous_epoch()):
|
2021-05-28 15:25:58 +00:00
|
|
|
flags.incl RewardFlags.isPreviousEpochTargetAttester
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
if a.data.beacon_block_root == get_block_root_at_slot(state, a.data.slot):
|
2021-05-28 15:25:58 +00:00
|
|
|
flags.incl RewardFlags.isPreviousEpochHeadAttester
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
# Update the cache for all participants
|
|
|
|
for validator_index in get_attesting_indices(
|
|
|
|
state, a.data, a.aggregation_bits, cache):
|
2021-10-13 14:24:36 +00:00
|
|
|
template v(): untyped = info.statuses[validator_index]
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-05-07 11:36:21 +00:00
|
|
|
v.flags = v.flags + flags
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
if is_previous_epoch_attester.isSome:
|
2021-10-13 14:24:36 +00:00
|
|
|
if v.is_previous_epoch_attester.isSome:
|
2020-10-22 11:08:46 +00:00
|
|
|
if is_previous_epoch_attester.get().delay <
|
|
|
|
v.is_previous_epoch_attester.get().delay:
|
|
|
|
v.is_previous_epoch_attester = is_previous_epoch_attester
|
|
|
|
else:
|
|
|
|
v.is_previous_epoch_attester = is_previous_epoch_attester
|
|
|
|
|
|
|
|
func process_attestations*(
|
2021-10-13 14:24:36 +00:00
|
|
|
info: var phase0.EpochInfo, state: phase0.BeaconState, cache: var StateCache) =
|
2020-10-22 11:08:46 +00:00
|
|
|
# Walk state attestations and update the status information
|
|
|
|
for a in state.previous_epoch_attestations:
|
2021-10-13 14:24:36 +00:00
|
|
|
process_attestation(info, state, a, cache)
|
2020-10-22 11:08:46 +00:00
|
|
|
for a in state.current_epoch_attestations:
|
2021-10-13 14:24:36 +00:00
|
|
|
process_attestation(info, state, a, cache)
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
for idx, v in info.statuses:
|
2021-05-28 15:25:58 +00:00
|
|
|
if v.flags.contains RewardFlags.isSlashed:
|
2020-10-22 11:08:46 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
let validator_balance = state.validators[idx].effective_balance
|
|
|
|
|
2021-05-28 15:25:58 +00:00
|
|
|
if v.flags.contains RewardFlags.isCurrentEpochAttester:
|
2021-10-13 14:24:36 +00:00
|
|
|
info.total_balances.current_epoch_attesters_raw += validator_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-05-28 15:25:58 +00:00
|
|
|
if v.flags.contains RewardFlags.isCurrentEpochTargetAttester:
|
2021-10-13 14:24:36 +00:00
|
|
|
info.total_balances.current_epoch_target_attesters_raw += validator_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
if v.is_previous_epoch_attester.isSome():
|
2021-10-13 14:24:36 +00:00
|
|
|
info.total_balances.previous_epoch_attesters_raw += validator_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-05-28 15:25:58 +00:00
|
|
|
if v.flags.contains RewardFlags.isPreviousEpochTargetAttester:
|
2021-10-13 14:24:36 +00:00
|
|
|
info.total_balances.previous_epoch_target_attesters_raw += validator_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-05-28 15:25:58 +00:00
|
|
|
if v.flags.contains RewardFlags.isPreviousEpochHeadAttester:
|
2021-10-13 14:24:36 +00:00
|
|
|
info.total_balances.previous_epoch_head_attesters_raw += validator_balance
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/phase0/beacon-chain.md#helpers
|
|
|
|
# get_eligible_validator_indices
|
2021-05-07 11:36:21 +00:00
|
|
|
func is_eligible_validator*(validator: RewardStatus): bool =
|
2021-05-28 15:25:58 +00:00
|
|
|
validator.flags.contains(RewardFlags.isActiveInPreviousEpoch) or
|
|
|
|
(validator.flags.contains(RewardFlags.isSlashed) and not
|
|
|
|
(validator.flags.contains RewardFlags.canWithdrawInCurrentEpoch))
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
func is_eligible_validator*(validator: Validator, previous_epoch: Epoch): bool =
|
|
|
|
is_active_validator(validator, previous_epoch) or
|
|
|
|
(validator.slashed and previous_epoch + 1 < validator.withdrawable_epoch)
|
|
|
|
|
|
|
|
func is_eligible_validator*(validator: ParticipationInfo): bool =
|
|
|
|
validator.flags.contains(ParticipationFlag.eligible)
|
|
|
|
|
2019-09-23 13:48:25 +00:00
|
|
|
# Spec
|
|
|
|
# --------------------------------------------------------
|
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#get_unslashed_participating_indices
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#get_total_balance
|
2021-09-27 14:22:58 +00:00
|
|
|
func get_unslashed_participating_balances*(state: altair.BeaconState | merge.BeaconState):
|
2021-08-25 14:43:00 +00:00
|
|
|
UnslashedParticipatingBalances =
|
|
|
|
let
|
|
|
|
previous_epoch = get_previous_epoch(state)
|
|
|
|
current_epoch = get_current_epoch(state)
|
|
|
|
var res: UnslashedParticipatingBalances
|
|
|
|
|
|
|
|
for validator_index in 0'u64 ..< state.validators.lenu64:
|
2021-10-13 14:24:36 +00:00
|
|
|
let
|
|
|
|
is_active_current_epoch = is_active_validator(
|
|
|
|
state.validators[validator_index], current_epoch)
|
|
|
|
validator_effective_balance =
|
|
|
|
state.validators[validator_index].effective_balance
|
|
|
|
|
|
|
|
if is_active_current_epoch:
|
|
|
|
# Active balance counted also for slashed validators
|
|
|
|
res.current_epoch += validator_effective_balance
|
|
|
|
|
2021-08-25 14:43:00 +00:00
|
|
|
if state.validators[validator_index].slashed:
|
|
|
|
continue
|
2021-10-13 14:24:36 +00:00
|
|
|
|
2021-08-25 14:43:00 +00:00
|
|
|
let
|
|
|
|
is_active_previous_epoch = is_active_validator(
|
|
|
|
state.validators[validator_index], previous_epoch)
|
|
|
|
previous_epoch_participation =
|
|
|
|
state.previous_epoch_participation[validator_index]
|
2021-10-13 14:24:36 +00:00
|
|
|
|
2021-08-25 14:43:00 +00:00
|
|
|
if is_active_previous_epoch:
|
|
|
|
for flag_index in 0 ..< PARTICIPATION_FLAG_WEIGHTS.len:
|
|
|
|
if has_flag(previous_epoch_participation, flag_index):
|
|
|
|
res.previous_epoch[flag_index] += validator_effective_balance
|
|
|
|
|
|
|
|
# Only TIMELY_TARGET_FLAG_INDEX is used with the current epoch in Altair
|
|
|
|
if is_active_current_epoch and has_flag(
|
|
|
|
state.current_epoch_participation[validator_index],
|
|
|
|
TIMELY_TARGET_FLAG_INDEX):
|
|
|
|
res.current_epoch_TIMELY_TARGET += validator_effective_balance
|
|
|
|
|
|
|
|
for flag_index in 0 ..< PARTICIPATION_FLAG_WEIGHTS.len:
|
|
|
|
res.previous_epoch[flag_index] =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, res.previous_epoch[flag_index])
|
|
|
|
|
|
|
|
res.current_epoch_TIMELY_TARGET =
|
|
|
|
max(EFFECTIVE_BALANCE_INCREMENT, res.current_epoch_TIMELY_TARGET)
|
2021-08-24 20:09:03 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
res.current_epoch = max(EFFECTIVE_BALANCE_INCREMENT, res.current_epoch)
|
|
|
|
|
2021-08-24 20:09:03 +00:00
|
|
|
res
|
|
|
|
|
2021-08-25 14:43:00 +00:00
|
|
|
func is_unslashed_participating_index(
|
2021-09-27 14:22:58 +00:00
|
|
|
state: altair.BeaconState | merge.BeaconState, flag_index: int, epoch: Epoch,
|
2021-08-25 14:43:00 +00:00
|
|
|
validator_index: ValidatorIndex): bool =
|
2021-05-28 15:25:58 +00:00
|
|
|
doAssert epoch in [get_previous_epoch(state), get_current_epoch(state)]
|
2021-08-25 14:43:00 +00:00
|
|
|
# TODO hoist this conditional
|
2021-08-24 20:09:03 +00:00
|
|
|
let epoch_participation =
|
|
|
|
if epoch == get_current_epoch(state):
|
|
|
|
unsafeAddr state.current_epoch_participation
|
|
|
|
else:
|
|
|
|
unsafeAddr state.previous_epoch_participation
|
2021-05-30 20:23:10 +00:00
|
|
|
|
2021-08-25 14:43:00 +00:00
|
|
|
is_active_validator(state.validators[validator_index], epoch) and
|
|
|
|
has_flag(epoch_participation[].asSeq()[validator_index], flag_index) and
|
|
|
|
not state.validators[validator_index].slashed
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-08-20 14:41:32 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization
|
2021-05-28 15:25:58 +00:00
|
|
|
proc process_justification_and_finalization*(state: var phase0.BeaconState,
|
2021-05-05 06:54:21 +00:00
|
|
|
total_balances: TotalBalances, flags: UpdateFlags = {}) {.nbench.} =
|
2020-09-24 17:04:10 +00:00
|
|
|
# Initial FFG checkpoint values have a `0x00` stub for `root`.
|
|
|
|
# Skip FFG updates in the first two epochs to avoid corner cases that might
|
|
|
|
# result in modifying this stub.
|
2019-06-28 13:44:44 +00:00
|
|
|
if get_current_epoch(state) <= GENESIS_EPOCH + 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
let
|
|
|
|
previous_epoch = get_previous_epoch(state)
|
|
|
|
current_epoch = get_current_epoch(state)
|
2019-07-16 07:34:11 +00:00
|
|
|
old_previous_justified_checkpoint = state.previous_justified_checkpoint
|
|
|
|
old_current_justified_checkpoint = state.current_justified_checkpoint
|
|
|
|
|
2019-06-28 13:44:44 +00:00
|
|
|
# Process justifications
|
2019-07-16 07:34:11 +00:00
|
|
|
state.previous_justified_checkpoint = state.current_justified_checkpoint
|
|
|
|
|
|
|
|
## Spec:
|
|
|
|
## state.justification_bits[1:] = state.justification_bits[:-1]
|
|
|
|
## state.justification_bits[0] = 0b0
|
2020-11-12 19:24:07 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#misc
|
2019-09-27 16:40:33 +00:00
|
|
|
const JUSTIFICATION_BITS_LENGTH = 4
|
2020-11-12 19:24:07 +00:00
|
|
|
|
2019-09-27 16:40:33 +00:00
|
|
|
state.justification_bits = (state.justification_bits shl 1) and
|
|
|
|
cast[uint8]((2^JUSTIFICATION_BITS_LENGTH) - 1)
|
2019-07-16 07:34:11 +00:00
|
|
|
|
2020-10-22 11:08:46 +00:00
|
|
|
let total_active_balance = total_balances.current_epoch
|
|
|
|
if total_balances.previous_epoch_target_attesters * 3 >=
|
|
|
|
total_active_balance * 2:
|
2019-07-16 07:34:11 +00:00
|
|
|
state.current_justified_checkpoint =
|
|
|
|
Checkpoint(epoch: previous_epoch,
|
|
|
|
root: get_block_root(state, previous_epoch))
|
2019-12-20 13:25:33 +00:00
|
|
|
state.justification_bits.setBit 1
|
2019-07-16 07:34:11 +00:00
|
|
|
|
2020-08-19 08:03:50 +00:00
|
|
|
trace "Justified with previous epoch",
|
2019-09-23 13:48:25 +00:00
|
|
|
current_epoch = current_epoch,
|
2020-07-16 13:16:51 +00:00
|
|
|
checkpoint = shortLog(state.current_justified_checkpoint)
|
2021-05-05 06:54:21 +00:00
|
|
|
elif verifyFinalization in flags:
|
2020-10-22 11:08:46 +00:00
|
|
|
warn "Low attestation participation in previous epoch",
|
|
|
|
total_balances, epoch = get_current_epoch(state)
|
2019-09-23 13:48:25 +00:00
|
|
|
|
2020-10-22 11:08:46 +00:00
|
|
|
if total_balances.current_epoch_target_attesters * 3 >=
|
|
|
|
total_active_balance * 2:
|
2019-07-16 07:34:11 +00:00
|
|
|
state.current_justified_checkpoint =
|
|
|
|
Checkpoint(epoch: current_epoch,
|
|
|
|
root: get_block_root(state, current_epoch))
|
2019-12-20 13:25:33 +00:00
|
|
|
state.justification_bits.setBit 0
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2020-08-19 08:03:50 +00:00
|
|
|
trace "Justified with current epoch",
|
2019-09-23 13:48:25 +00:00
|
|
|
current_epoch = current_epoch,
|
2020-07-16 13:16:51 +00:00
|
|
|
checkpoint = shortLog(state.current_justified_checkpoint)
|
2019-09-23 13:48:25 +00:00
|
|
|
|
2019-06-28 13:44:44 +00:00
|
|
|
# Process finalizations
|
2019-07-01 09:42:37 +00:00
|
|
|
let bitfield = state.justification_bits
|
2019-06-28 13:44:44 +00:00
|
|
|
|
|
|
|
## The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th
|
|
|
|
## as source
|
2019-07-03 07:35:05 +00:00
|
|
|
if (bitfield and 0b1110) == 0b1110 and
|
|
|
|
old_previous_justified_checkpoint.epoch + 3 == current_epoch:
|
2019-07-16 07:34:11 +00:00
|
|
|
state.finalized_checkpoint = old_previous_justified_checkpoint
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2020-08-19 08:03:50 +00:00
|
|
|
trace "Finalized with rule 234",
|
2019-09-23 13:48:25 +00:00
|
|
|
current_epoch = current_epoch,
|
2020-07-16 13:16:51 +00:00
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
2019-09-23 13:48:25 +00:00
|
|
|
|
2019-06-28 13:44:44 +00:00
|
|
|
## The 2nd/3rd most recent epochs are justified, the 2nd using the 3rd as
|
|
|
|
## source
|
2019-07-03 07:35:05 +00:00
|
|
|
if (bitfield and 0b110) == 0b110 and
|
|
|
|
old_previous_justified_checkpoint.epoch + 2 == current_epoch:
|
2019-07-16 07:34:11 +00:00
|
|
|
state.finalized_checkpoint = old_previous_justified_checkpoint
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2020-08-19 08:03:50 +00:00
|
|
|
trace "Finalized with rule 23",
|
2019-09-23 13:48:25 +00:00
|
|
|
current_epoch = current_epoch,
|
2020-07-16 13:16:51 +00:00
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
2019-09-23 13:48:25 +00:00
|
|
|
|
2019-06-28 13:44:44 +00:00
|
|
|
## The 1st/2nd/3rd most recent epochs are justified, the 1st using the 3rd as
|
|
|
|
## source
|
2019-07-03 07:35:05 +00:00
|
|
|
if (bitfield and 0b111) == 0b111 and
|
|
|
|
old_current_justified_checkpoint.epoch + 2 == current_epoch:
|
2019-07-16 07:34:11 +00:00
|
|
|
state.finalized_checkpoint = old_current_justified_checkpoint
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2020-08-19 08:03:50 +00:00
|
|
|
trace "Finalized with rule 123",
|
2019-09-23 13:48:25 +00:00
|
|
|
current_epoch = current_epoch,
|
2020-07-16 13:16:51 +00:00
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
2019-09-23 13:48:25 +00:00
|
|
|
|
2019-06-28 13:44:44 +00:00
|
|
|
## The 1st/2nd most recent epochs are justified, the 1st using the 2nd as
|
|
|
|
## source
|
2019-07-03 07:35:05 +00:00
|
|
|
if (bitfield and 0b11) == 0b11 and
|
|
|
|
old_current_justified_checkpoint.epoch + 1 == current_epoch:
|
2019-07-16 07:34:11 +00:00
|
|
|
state.finalized_checkpoint = old_current_justified_checkpoint
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2020-08-19 08:03:50 +00:00
|
|
|
trace "Finalized with rule 12",
|
2019-09-23 13:48:25 +00:00
|
|
|
current_epoch = current_epoch,
|
2020-07-16 13:16:51 +00:00
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
2019-09-23 13:48:25 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#justification-and-finalization
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#justification-and-finalization
|
2021-05-28 15:25:58 +00:00
|
|
|
# TODO merge these things -- effectively, the phase0 process_justification_and_finalization is mostly a stub in this world
|
2021-09-27 14:22:58 +00:00
|
|
|
proc weigh_justification_and_finalization(state: var (altair.BeaconState | merge.BeaconState),
|
2021-05-30 20:23:10 +00:00
|
|
|
total_active_balance: Gwei,
|
2021-05-28 15:25:58 +00:00
|
|
|
previous_epoch_target_balance: Gwei,
|
|
|
|
current_epoch_target_balance: Gwei,
|
|
|
|
flags: UpdateFlags = {}) =
|
|
|
|
let
|
|
|
|
previous_epoch = get_previous_epoch(state)
|
|
|
|
current_epoch = get_current_epoch(state)
|
|
|
|
old_previous_justified_checkpoint = state.previous_justified_checkpoint
|
|
|
|
old_current_justified_checkpoint = state.current_justified_checkpoint
|
|
|
|
|
|
|
|
# Process justifications
|
|
|
|
state.previous_justified_checkpoint = state.current_justified_checkpoint
|
|
|
|
|
|
|
|
## Spec:
|
|
|
|
## state.justification_bits[1:] = state.justification_bits[:-1]
|
|
|
|
## state.justification_bits[0] = 0b0
|
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#misc
|
2021-05-28 15:25:58 +00:00
|
|
|
const JUSTIFICATION_BITS_LENGTH = 4
|
|
|
|
|
|
|
|
state.justification_bits = (state.justification_bits shl 1) and
|
|
|
|
cast[uint8]((2^JUSTIFICATION_BITS_LENGTH) - 1)
|
|
|
|
|
2021-05-30 20:23:10 +00:00
|
|
|
if previous_epoch_target_balance * 3 >= total_active_balance * 2:
|
2021-05-28 15:25:58 +00:00
|
|
|
state.current_justified_checkpoint =
|
|
|
|
Checkpoint(epoch: previous_epoch,
|
|
|
|
root: get_block_root(state, previous_epoch))
|
|
|
|
state.justification_bits.setBit 1
|
|
|
|
|
|
|
|
trace "Justified with previous epoch",
|
|
|
|
current_epoch = current_epoch,
|
|
|
|
checkpoint = shortLog(state.current_justified_checkpoint)
|
|
|
|
elif verifyFinalization in flags:
|
|
|
|
warn "Low attestation participation in previous epoch",
|
2021-05-30 20:23:10 +00:00
|
|
|
total_active_balance,
|
|
|
|
previous_epoch_target_balance,
|
|
|
|
current_epoch_target_balance,
|
|
|
|
epoch = get_current_epoch(state)
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-05-30 20:23:10 +00:00
|
|
|
if current_epoch_target_balance * 3 >= total_active_balance * 2:
|
2021-05-28 15:25:58 +00:00
|
|
|
state.current_justified_checkpoint =
|
|
|
|
Checkpoint(epoch: current_epoch,
|
|
|
|
root: get_block_root(state, current_epoch))
|
|
|
|
state.justification_bits.setBit 0
|
|
|
|
|
|
|
|
trace "Justified with current epoch",
|
|
|
|
current_epoch = current_epoch,
|
|
|
|
checkpoint = shortLog(state.current_justified_checkpoint)
|
|
|
|
|
|
|
|
# Process finalizations
|
|
|
|
let bitfield = state.justification_bits
|
|
|
|
|
|
|
|
## The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th
|
|
|
|
## as source
|
|
|
|
if (bitfield and 0b1110) == 0b1110 and
|
|
|
|
old_previous_justified_checkpoint.epoch + 3 == current_epoch:
|
|
|
|
state.finalized_checkpoint = old_previous_justified_checkpoint
|
|
|
|
|
|
|
|
trace "Finalized with rule 234",
|
|
|
|
current_epoch = current_epoch,
|
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
|
|
|
|
|
|
|
## The 2nd/3rd most recent epochs are justified, the 2nd using the 3rd as
|
|
|
|
## source
|
|
|
|
if (bitfield and 0b110) == 0b110 and
|
|
|
|
old_previous_justified_checkpoint.epoch + 2 == current_epoch:
|
|
|
|
state.finalized_checkpoint = old_previous_justified_checkpoint
|
|
|
|
|
|
|
|
trace "Finalized with rule 23",
|
|
|
|
current_epoch = current_epoch,
|
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
|
|
|
|
|
|
|
## The 1st/2nd/3rd most recent epochs are justified, the 1st using the 3rd as
|
|
|
|
## source
|
|
|
|
if (bitfield and 0b111) == 0b111 and
|
|
|
|
old_current_justified_checkpoint.epoch + 2 == current_epoch:
|
|
|
|
state.finalized_checkpoint = old_current_justified_checkpoint
|
|
|
|
|
|
|
|
trace "Finalized with rule 123",
|
|
|
|
current_epoch = current_epoch,
|
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
|
|
|
|
|
|
|
## The 1st/2nd most recent epochs are justified, the 1st using the 2nd as
|
|
|
|
## source
|
|
|
|
if (bitfield and 0b11) == 0b11 and
|
|
|
|
old_current_justified_checkpoint.epoch + 1 == current_epoch:
|
|
|
|
state.finalized_checkpoint = old_current_justified_checkpoint
|
|
|
|
|
|
|
|
trace "Finalized with rule 12",
|
|
|
|
current_epoch = current_epoch,
|
|
|
|
checkpoint = shortLog(state.finalized_checkpoint)
|
|
|
|
|
2021-09-27 14:22:58 +00:00
|
|
|
proc process_justification_and_finalization*(state: var (altair.BeaconState | merge.BeaconState),
|
2021-10-13 14:24:36 +00:00
|
|
|
balances: UnslashedParticipatingBalances,
|
2021-08-25 14:43:00 +00:00
|
|
|
flags: UpdateFlags = {}) {.nbench.} =
|
2021-05-28 15:25:58 +00:00
|
|
|
# Initial FFG checkpoint values have a `0x00` stub for `root`.
|
|
|
|
# Skip FFG updates in the first two epochs to avoid corner cases that might
|
|
|
|
# result in modifying this stub.
|
|
|
|
if get_current_epoch(state) <= GENESIS_EPOCH + 1:
|
|
|
|
return
|
2021-08-24 20:09:03 +00:00
|
|
|
|
|
|
|
# These ultimately differ from phase0 only in these lines, with the phase 0
|
|
|
|
# version effectively embedding weigh_justification_and_finalization(), for
|
|
|
|
# historical reasons.
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.2/specs/phase0/beacon-chain.md#justification-and-finalization
|
2021-05-30 20:23:10 +00:00
|
|
|
weigh_justification_and_finalization(
|
2021-10-13 14:24:36 +00:00
|
|
|
state, balances.current_epoch,
|
|
|
|
balances.previous_epoch[TIMELY_TARGET_FLAG_INDEX],
|
|
|
|
balances.current_epoch_TIMELY_TARGET, flags)
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#helpers
|
2021-05-28 15:25:58 +00:00
|
|
|
func get_base_reward_sqrt*(state: phase0.BeaconState, index: ValidatorIndex,
|
improve slot processing speeds (#1670)
about 40% better slot processing times (with LTO enabled) - these don't
do BLS but are used
heavily during replay (state transition = slot + block transition)
tests using a recent medalla state and advancing it 1000 slots:
```
./ncli slots --preState2:state-302271-3c1dbf19-c1f944bf.ssz --slot:1000
--postState2:xx.ssz
```
pre:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
39.236, 0.000, 39.236, 39.236, 1,
Load state from file
0.049, 0.002, 0.046, 0.063, 968,
Apply slot
256.504, 81.008, 213.471, 591.902, 32,
Apply epoch slot
28.597, 0.000, 28.597, 28.597, 1,
Save state to file
```
cast:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
37.079, 0.000, 37.079, 37.079, 1,
Load state from file
0.042, 0.002, 0.040, 0.090, 968,
Apply slot
215.552, 68.763, 180.155, 500.103, 32,
Apply epoch slot
25.106, 0.000, 25.106, 25.106, 1,
Save state to file
```
cast+rewards:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
40.049, 0.000, 40.049, 40.049, 1,
Load state from file
0.048, 0.001, 0.045, 0.060, 968,
Apply slot
164.981, 76.273, 142.099, 477.868, 32,
Apply epoch slot
28.498, 0.000, 28.498, 28.498, 1,
Save state to file
```
cast+rewards+shr
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
12.898, 0.000, 12.898, 12.898, 1,
Load state from file
0.039, 0.002, 0.038, 0.054, 968,
Apply slot
139.971, 68.797, 120.088, 428.844, 32,
Apply epoch slot
24.761, 0.000, 24.761, 24.761, 1,
Save state to file
```
2020-09-16 20:59:33 +00:00
|
|
|
total_balance_sqrt: auto): Gwei =
|
2019-11-15 22:37:39 +00:00
|
|
|
# Spec function recalculates total_balance every time, which creates an
|
|
|
|
# O(n^2) situation.
|
2021-08-24 20:09:03 +00:00
|
|
|
let effective_balance = state.validators.asSeq()[index].effective_balance
|
2019-06-28 13:44:44 +00:00
|
|
|
effective_balance * BASE_REWARD_FACTOR div
|
improve slot processing speeds (#1670)
about 40% better slot processing times (with LTO enabled) - these don't
do BLS but are used
heavily during replay (state transition = slot + block transition)
tests using a recent medalla state and advancing it 1000 slots:
```
./ncli slots --preState2:state-302271-3c1dbf19-c1f944bf.ssz --slot:1000
--postState2:xx.ssz
```
pre:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
39.236, 0.000, 39.236, 39.236, 1,
Load state from file
0.049, 0.002, 0.046, 0.063, 968,
Apply slot
256.504, 81.008, 213.471, 591.902, 32,
Apply epoch slot
28.597, 0.000, 28.597, 28.597, 1,
Save state to file
```
cast:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
37.079, 0.000, 37.079, 37.079, 1,
Load state from file
0.042, 0.002, 0.040, 0.090, 968,
Apply slot
215.552, 68.763, 180.155, 500.103, 32,
Apply epoch slot
25.106, 0.000, 25.106, 25.106, 1,
Save state to file
```
cast+rewards:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
40.049, 0.000, 40.049, 40.049, 1,
Load state from file
0.048, 0.001, 0.045, 0.060, 968,
Apply slot
164.981, 76.273, 142.099, 477.868, 32,
Apply epoch slot
28.498, 0.000, 28.498, 28.498, 1,
Save state to file
```
cast+rewards+shr
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
12.898, 0.000, 12.898, 12.898, 1,
Load state from file
0.039, 0.002, 0.038, 0.054, 968,
Apply slot
139.971, 68.797, 120.088, 428.844, 32,
Apply epoch slot
24.761, 0.000, 24.761, 24.761, 1,
Save state to file
```
2020-09-16 20:59:33 +00:00
|
|
|
total_balance_sqrt div BASE_REWARDS_PER_EPOCH
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2020-10-22 11:08:46 +00:00
|
|
|
func get_proposer_reward(base_reward: Gwei): Gwei =
|
2020-07-07 16:52:12 +00:00
|
|
|
# Spec version recalculates get_total_active_balance(state) quadratically
|
2020-10-22 11:08:46 +00:00
|
|
|
base_reward div PROPOSER_REWARD_QUOTIENT
|
|
|
|
|
|
|
|
func is_in_inactivity_leak(finality_delay: uint64): bool =
|
|
|
|
finality_delay > MIN_EPOCHS_TO_INACTIVITY_PENALTY
|
2020-06-08 18:41:50 +00:00
|
|
|
|
2021-05-28 15:25:58 +00:00
|
|
|
func get_finality_delay(state: SomeBeaconState): uint64 =
|
2020-06-08 18:41:50 +00:00
|
|
|
get_previous_epoch(state) - state.finalized_checkpoint.epoch
|
|
|
|
|
2021-08-20 14:41:32 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-alpha.8/specs/phase0/beacon-chain.md#rewards-and-penalties-1
|
2021-09-27 14:22:58 +00:00
|
|
|
func is_in_inactivity_leak(state: altair.BeaconState | merge.BeaconState): bool =
|
2021-05-28 15:25:58 +00:00
|
|
|
# TODO remove this, see above
|
|
|
|
get_finality_delay(state) > MIN_EPOCHS_TO_INACTIVITY_PENALTY
|
|
|
|
|
2020-10-22 11:08:46 +00:00
|
|
|
func get_attestation_component_delta(is_unslashed_attester: bool,
|
|
|
|
attesting_balance: Gwei,
|
|
|
|
total_balance: Gwei,
|
|
|
|
base_reward: uint64,
|
2021-05-07 11:36:21 +00:00
|
|
|
finality_delay: uint64): RewardDelta =
|
2020-06-08 18:41:50 +00:00
|
|
|
# Helper with shared logic for use by get source, target, and head deltas
|
|
|
|
# functions
|
2020-10-22 11:08:46 +00:00
|
|
|
if is_unslashed_attester:
|
|
|
|
if is_in_inactivity_leak(finality_delay):
|
|
|
|
# Since full base reward will be canceled out by inactivity penalty deltas,
|
|
|
|
# optimal participation receives full base reward compensation here.
|
2021-05-07 11:36:21 +00:00
|
|
|
RewardDelta(rewards: base_reward)
|
2020-06-08 18:41:50 +00:00
|
|
|
else:
|
2020-10-22 11:08:46 +00:00
|
|
|
let reward_numerator =
|
|
|
|
base_reward * (attesting_balance div EFFECTIVE_BALANCE_INCREMENT)
|
2021-05-07 11:36:21 +00:00
|
|
|
RewardDelta(rewards:
|
2020-10-22 11:08:46 +00:00
|
|
|
reward_numerator div (total_balance div EFFECTIVE_BALANCE_INCREMENT))
|
|
|
|
else:
|
2021-05-07 11:36:21 +00:00
|
|
|
RewardDelta(penalties: base_reward)
|
2020-06-08 18:41:50 +00:00
|
|
|
|
2021-09-28 14:01:46 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/phase0/beacon-chain.md#components-of-attestation-deltas
|
2021-05-07 11:36:21 +00:00
|
|
|
func get_source_delta*(validator: RewardStatus,
|
2020-10-22 11:08:46 +00:00
|
|
|
base_reward: uint64,
|
|
|
|
total_balances: TotalBalances,
|
2021-05-07 11:36:21 +00:00
|
|
|
finality_delay: uint64): RewardDelta =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return attester micro-rewards/penalties for source-vote for each validator.
|
2020-10-22 11:08:46 +00:00
|
|
|
get_attestation_component_delta(
|
2021-05-07 11:36:21 +00:00
|
|
|
validator.is_previous_epoch_attester.isSome() and
|
2021-05-28 15:25:58 +00:00
|
|
|
not (validator.flags.contains RewardFlags.isSlashed),
|
2020-10-22 11:08:46 +00:00
|
|
|
total_balances.previous_epoch_attesters,
|
|
|
|
total_balances.current_epoch,
|
|
|
|
base_reward,
|
|
|
|
finality_delay)
|
|
|
|
|
2021-05-07 11:36:21 +00:00
|
|
|
func get_target_delta*(validator: RewardStatus,
|
2020-10-22 11:08:46 +00:00
|
|
|
base_reward: uint64,
|
|
|
|
total_balances: TotalBalances,
|
2021-05-07 11:36:21 +00:00
|
|
|
finality_delay: uint64): RewardDelta =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return attester micro-rewards/penalties for target-vote for each validator.
|
2020-10-22 11:08:46 +00:00
|
|
|
get_attestation_component_delta(
|
2021-05-28 15:25:58 +00:00
|
|
|
validator.flags.contains(RewardFlags.isPreviousEpochTargetAttester) and
|
|
|
|
not (validator.flags.contains(RewardFlags.isSlashed)),
|
2020-10-22 11:08:46 +00:00
|
|
|
total_balances.previous_epoch_target_attesters,
|
|
|
|
total_balances.current_epoch,
|
|
|
|
base_reward,
|
|
|
|
finality_delay)
|
|
|
|
|
2021-05-07 11:36:21 +00:00
|
|
|
func get_head_delta*(validator: RewardStatus,
|
2020-10-22 11:08:46 +00:00
|
|
|
base_reward: uint64,
|
|
|
|
total_balances: TotalBalances,
|
2021-05-07 11:36:21 +00:00
|
|
|
finality_delay: uint64): RewardDelta =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return attester micro-rewards/penalties for head-vote for each validator.
|
2020-10-22 11:08:46 +00:00
|
|
|
get_attestation_component_delta(
|
2021-05-28 15:25:58 +00:00
|
|
|
validator.flags.contains(RewardFlags.isPreviousEpochHeadAttester) and
|
|
|
|
((not validator.flags.contains(RewardFlags.isSlashed))),
|
2020-10-22 11:08:46 +00:00
|
|
|
total_balances.previous_epoch_head_attesters,
|
|
|
|
total_balances.current_epoch,
|
|
|
|
base_reward,
|
|
|
|
finality_delay)
|
|
|
|
|
2021-05-07 11:36:21 +00:00
|
|
|
func get_inclusion_delay_delta*(validator: RewardStatus,
|
2020-10-22 11:08:46 +00:00
|
|
|
base_reward: uint64):
|
2021-05-07 11:36:21 +00:00
|
|
|
(RewardDelta, Option[(uint64, RewardDelta)]) =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return proposer and inclusion delay micro-rewards/penalties for each validator.
|
2021-05-28 15:25:58 +00:00
|
|
|
if validator.is_previous_epoch_attester.isSome() and ((not validator.flags.contains(RewardFlags.isSlashed))):
|
2020-10-22 11:08:46 +00:00
|
|
|
let
|
|
|
|
inclusion_info = validator.is_previous_epoch_attester.get()
|
|
|
|
proposer_reward = get_proposer_reward(base_reward)
|
2021-05-07 11:36:21 +00:00
|
|
|
proposer_delta = RewardDelta(rewards: proposer_reward)
|
2020-07-10 09:24:04 +00:00
|
|
|
|
2020-06-08 18:41:50 +00:00
|
|
|
let
|
2020-10-22 11:08:46 +00:00
|
|
|
max_attester_reward = base_reward - proposer_reward
|
2021-05-07 11:36:21 +00:00
|
|
|
delta = RewardDelta(rewards: max_attester_reward div inclusion_info.delay)
|
2020-10-22 11:08:46 +00:00
|
|
|
proposer_index = inclusion_info.proposer_index;
|
|
|
|
return (delta, some((proposer_index, proposer_delta)))
|
|
|
|
|
2021-05-07 11:36:21 +00:00
|
|
|
func get_inactivity_penalty_delta*(validator: RewardStatus,
|
2020-10-22 11:08:46 +00:00
|
|
|
base_reward: Gwei,
|
2021-05-07 11:36:21 +00:00
|
|
|
finality_delay: uint64): RewardDelta =
|
2020-10-22 11:08:46 +00:00
|
|
|
## Return inactivity reward/penalty deltas for each validator.
|
2021-05-07 11:36:21 +00:00
|
|
|
var delta: RewardDelta
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
if is_in_inactivity_leak(finality_delay):
|
|
|
|
# If validator is performing optimally this cancels all rewards for a neutral balance
|
|
|
|
delta.penalties +=
|
|
|
|
BASE_REWARDS_PER_EPOCH * base_reward - get_proposer_reward(base_reward)
|
|
|
|
|
|
|
|
# Additionally, all validators whose FFG target didn't match are penalized extra
|
|
|
|
# This condition is equivalent to this condition from the spec:
|
|
|
|
# `index not in get_unslashed_attesting_indices(state, matching_target_attestations)`
|
2021-05-28 15:25:58 +00:00
|
|
|
if (validator.flags.contains(RewardFlags.isSlashed)) or
|
|
|
|
((not validator.flags.contains(RewardFlags.isPreviousEpochTargetAttester))):
|
2021-05-07 11:36:21 +00:00
|
|
|
delta.penalties +=
|
|
|
|
validator.current_epoch_effective_balance * finality_delay div
|
|
|
|
INACTIVITY_PENALTY_QUOTIENT
|
2020-10-22 11:08:46 +00:00
|
|
|
|
|
|
|
delta
|
2020-06-08 18:41:50 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#get_attestation_deltas
|
2021-10-13 14:24:36 +00:00
|
|
|
func get_attestation_deltas(state: phase0.BeaconState, info: var phase0.EpochInfo) =
|
2021-05-07 11:36:21 +00:00
|
|
|
## Update rewards with attestation reward/penalty deltas for each validator.
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2020-06-29 18:08:58 +00:00
|
|
|
let
|
2020-10-22 11:08:46 +00:00
|
|
|
finality_delay = get_finality_delay(state)
|
2021-10-13 14:24:36 +00:00
|
|
|
total_balance = info.total_balances.current_epoch
|
2020-10-22 11:08:46 +00:00
|
|
|
total_balance_sqrt = integer_squareroot(total_balance)
|
|
|
|
# Filter out ineligible validators. All sub-functions of the spec do this
|
|
|
|
# except for `get_inclusion_delay_deltas`. It's safe to do so here because
|
|
|
|
# any validator that is in the unslashed indices of the matching source
|
|
|
|
# attestations is active, and therefore eligible.
|
2021-10-13 14:24:36 +00:00
|
|
|
for index, validator in info.statuses.mpairs():
|
2020-10-22 11:08:46 +00:00
|
|
|
if not is_eligible_validator(validator):
|
|
|
|
continue
|
|
|
|
|
|
|
|
let
|
|
|
|
base_reward = get_base_reward_sqrt(
|
|
|
|
state, index.ValidatorIndex, total_balance_sqrt)
|
improve slot processing speeds (#1670)
about 40% better slot processing times (with LTO enabled) - these don't
do BLS but are used
heavily during replay (state transition = slot + block transition)
tests using a recent medalla state and advancing it 1000 slots:
```
./ncli slots --preState2:state-302271-3c1dbf19-c1f944bf.ssz --slot:1000
--postState2:xx.ssz
```
pre:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
39.236, 0.000, 39.236, 39.236, 1,
Load state from file
0.049, 0.002, 0.046, 0.063, 968,
Apply slot
256.504, 81.008, 213.471, 591.902, 32,
Apply epoch slot
28.597, 0.000, 28.597, 28.597, 1,
Save state to file
```
cast:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
37.079, 0.000, 37.079, 37.079, 1,
Load state from file
0.042, 0.002, 0.040, 0.090, 968,
Apply slot
215.552, 68.763, 180.155, 500.103, 32,
Apply epoch slot
25.106, 0.000, 25.106, 25.106, 1,
Save state to file
```
cast+rewards:
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
40.049, 0.000, 40.049, 40.049, 1,
Load state from file
0.048, 0.001, 0.045, 0.060, 968,
Apply slot
164.981, 76.273, 142.099, 477.868, 32,
Apply epoch slot
28.498, 0.000, 28.498, 28.498, 1,
Save state to file
```
cast+rewards+shr
```
All time are ms
Average, StdDev, Min, Max, Samples,
Test
Validation is turned off meaning that no BLS operations are performed
12.898, 0.000, 12.898, 12.898, 1,
Load state from file
0.039, 0.002, 0.038, 0.054, 968,
Apply slot
139.971, 68.797, 120.088, 428.844, 32,
Apply epoch slot
24.761, 0.000, 24.761, 24.761, 1,
Save state to file
```
2020-09-16 20:59:33 +00:00
|
|
|
|
2020-10-22 11:08:46 +00:00
|
|
|
let
|
|
|
|
source_delta = get_source_delta(
|
2021-10-13 14:24:36 +00:00
|
|
|
validator, base_reward, info.total_balances, finality_delay)
|
2020-10-22 11:08:46 +00:00
|
|
|
target_delta = get_target_delta(
|
2021-10-13 14:24:36 +00:00
|
|
|
validator, base_reward, info.total_balances, finality_delay)
|
2020-10-22 11:08:46 +00:00
|
|
|
head_delta = get_head_delta(
|
2021-10-13 14:24:36 +00:00
|
|
|
validator, base_reward, info.total_balances, finality_delay)
|
2020-10-22 11:08:46 +00:00
|
|
|
(inclusion_delay_delta, proposer_delta) =
|
|
|
|
get_inclusion_delay_delta(validator, base_reward)
|
|
|
|
inactivity_delta = get_inactivity_penalty_delta(
|
|
|
|
validator, base_reward, finality_delay)
|
|
|
|
|
|
|
|
validator.delta.add source_delta
|
|
|
|
validator.delta.add target_delta
|
|
|
|
validator.delta.add head_delta
|
|
|
|
validator.delta.add inclusion_delay_delta
|
|
|
|
validator.delta.add inactivity_delta
|
|
|
|
|
|
|
|
if proposer_delta.isSome:
|
|
|
|
let proposer_index = proposer_delta.get()[0]
|
2021-10-13 14:24:36 +00:00
|
|
|
if proposer_index < info.statuses.lenu64:
|
|
|
|
info.statuses[proposer_index].delta.add(
|
2020-10-22 11:08:46 +00:00
|
|
|
proposer_delta.get()[1])
|
2020-06-08 18:41:50 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#get_base_reward
|
2021-10-13 14:24:36 +00:00
|
|
|
func get_base_reward_increment(
|
2021-09-27 14:22:58 +00:00
|
|
|
state: altair.BeaconState | merge.BeaconState, index: ValidatorIndex,
|
2021-10-13 14:24:36 +00:00
|
|
|
base_reward_per_increment: Gwei): Gwei =
|
2021-05-28 15:25:58 +00:00
|
|
|
## Return the base reward for the validator defined by ``index`` with respect
|
|
|
|
## to the current ``state``.
|
|
|
|
let increments =
|
|
|
|
state.validators[index].effective_balance div EFFECTIVE_BALANCE_INCREMENT
|
2021-10-13 14:24:36 +00:00
|
|
|
increments * base_reward_per_increment
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#get_flag_index_deltas
|
2021-06-01 12:40:13 +00:00
|
|
|
iterator get_flag_index_deltas(
|
2021-10-13 14:24:36 +00:00
|
|
|
state: altair.BeaconState | merge.BeaconState, flag_index: int,
|
|
|
|
base_reward_per_increment: Gwei,
|
|
|
|
info: altair.EpochInfo):
|
|
|
|
(ValidatorIndex, RewardDelta) =
|
2021-05-28 15:25:58 +00:00
|
|
|
## Return the deltas for a given ``flag_index`` by scanning through the
|
|
|
|
## participation flags.
|
|
|
|
let
|
|
|
|
previous_epoch = get_previous_epoch(state)
|
|
|
|
weight = PARTICIPATION_FLAG_WEIGHTS[flag_index].uint64 # safe
|
2021-06-01 12:40:13 +00:00
|
|
|
unslashed_participating_balance =
|
2021-10-13 14:24:36 +00:00
|
|
|
info.balances.previous_epoch[flag_index]
|
2021-06-01 12:40:13 +00:00
|
|
|
unslashed_participating_increments =
|
|
|
|
unslashed_participating_balance div EFFECTIVE_BALANCE_INCREMENT
|
2021-10-13 14:24:36 +00:00
|
|
|
active_increments =
|
|
|
|
info.balances.current_epoch div EFFECTIVE_BALANCE_INCREMENT
|
2021-05-28 15:25:58 +00:00
|
|
|
|
|
|
|
for index in 0 ..< state.validators.len:
|
2021-10-13 14:24:36 +00:00
|
|
|
if not is_eligible_validator(info.validators[index]):
|
2021-05-28 15:25:58 +00:00
|
|
|
continue
|
|
|
|
|
2021-06-01 12:40:13 +00:00
|
|
|
template vidx: ValidatorIndex = index.ValidatorIndex
|
2021-10-13 14:24:36 +00:00
|
|
|
let base_reward = get_base_reward_increment(state, vidx, base_reward_per_increment)
|
2021-06-01 12:40:13 +00:00
|
|
|
yield
|
2021-08-25 14:43:00 +00:00
|
|
|
if is_unslashed_participating_index(
|
|
|
|
state, flag_index, previous_epoch, vidx):
|
2021-06-01 12:40:13 +00:00
|
|
|
if not is_in_inactivity_leak(state):
|
|
|
|
let reward_numerator =
|
|
|
|
base_reward * weight * unslashed_participating_increments
|
2021-10-13 14:24:36 +00:00
|
|
|
(vidx, RewardDelta(
|
|
|
|
rewards: reward_numerator div (active_increments * WEIGHT_DENOMINATOR),
|
|
|
|
penalties: 0.Gwei))
|
2021-06-01 12:40:13 +00:00
|
|
|
else:
|
2021-10-13 14:24:36 +00:00
|
|
|
(vidx, RewardDelta(rewards: 0.Gwei, penalties: 0.Gwei))
|
2021-06-01 12:40:13 +00:00
|
|
|
elif flag_index != TIMELY_HEAD_FLAG_INDEX:
|
2021-10-13 14:24:36 +00:00
|
|
|
(vidx, RewardDelta(
|
|
|
|
rewards: 0.Gwei,
|
|
|
|
penalties: base_reward * weight div WEIGHT_DENOMINATOR))
|
2021-06-01 12:40:13 +00:00
|
|
|
else:
|
2021-10-13 14:24:36 +00:00
|
|
|
(vidx, RewardDelta(rewards: 0.Gwei, penalties: 0.Gwei))
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#modified-get_inactivity_penalty_deltas
|
2021-10-13 14:24:36 +00:00
|
|
|
iterator get_inactivity_penalty_deltas(
|
|
|
|
cfg: RuntimeConfig, state: altair.BeaconState | merge.BeaconState,
|
|
|
|
info: altair.EpochInfo):
|
2021-06-01 12:40:13 +00:00
|
|
|
(ValidatorIndex, Gwei) =
|
2021-05-28 15:25:58 +00:00
|
|
|
## Return the inactivity penalty deltas by considering timely target
|
|
|
|
## participation flags and inactivity scores.
|
|
|
|
let
|
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
|
|
|
penalty_denominator =
|
|
|
|
cfg.INACTIVITY_SCORE_BIAS * INACTIVITY_PENALTY_QUOTIENT_ALTAIR
|
2021-08-24 20:09:03 +00:00
|
|
|
previous_epoch = get_previous_epoch(state)
|
|
|
|
|
2021-05-28 15:25:58 +00:00
|
|
|
for index in 0 ..< state.validators.len:
|
2021-10-13 14:24:36 +00:00
|
|
|
if not is_eligible_validator(info.validators[index]):
|
2021-05-28 15:25:58 +00:00
|
|
|
continue
|
|
|
|
|
2021-06-01 12:40:13 +00:00
|
|
|
template vidx: untyped = index.ValidatorIndex
|
2021-08-25 14:43:00 +00:00
|
|
|
if not is_unslashed_participating_index(
|
|
|
|
state, TIMELY_TARGET_FLAG_INDEX, previous_epoch, vidx):
|
2021-05-28 15:25:58 +00:00
|
|
|
let
|
2021-06-01 12:40:13 +00:00
|
|
|
penalty_numerator = state.validators[index].effective_balance *
|
|
|
|
state.inactivity_scores[index]
|
|
|
|
yield (vidx, Gwei(penalty_numerator div penalty_denominator))
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#rewards-and-penalties-1
|
2019-06-28 13:44:44 +00:00
|
|
|
func process_rewards_and_penalties(
|
2021-10-13 14:24:36 +00:00
|
|
|
state: var phase0.BeaconState, info: var phase0.EpochInfo) {.nbench.} =
|
2020-09-24 17:04:10 +00:00
|
|
|
# No rewards are applied at the end of `GENESIS_EPOCH` because rewards are
|
|
|
|
# for work done in the previous epoch
|
2021-10-13 14:24:36 +00:00
|
|
|
doAssert info.statuses.len == state.validators.len
|
2020-10-22 11:08:46 +00:00
|
|
|
|
2019-06-28 13:44:44 +00:00
|
|
|
if get_current_epoch(state) == GENESIS_EPOCH:
|
|
|
|
return
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
get_attestation_deltas(state, info)
|
2019-10-01 14:40:41 +00:00
|
|
|
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
# Here almost all balances are updated (assuming most validators are active) -
|
|
|
|
# clearing the cache becomes a bottleneck if done item by item because of the
|
|
|
|
# recursive nature of cache clearing - instead, we clear the whole cache then
|
|
|
|
# update the raw list directly
|
|
|
|
state.balances.clearCache()
|
2021-10-13 14:24:36 +00:00
|
|
|
for idx, v in info.statuses:
|
2021-08-24 20:09:03 +00:00
|
|
|
var balance = state.balances.asSeq()[idx]
|
|
|
|
increase_balance(balance, v.delta.rewards)
|
|
|
|
decrease_balance(balance, v.delta.penalties)
|
|
|
|
state.balances.asSeq()[idx] = balance
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#rewards-and-penalties
|
2021-06-03 09:42:25 +00:00
|
|
|
func process_rewards_and_penalties(
|
2021-09-27 14:22:58 +00:00
|
|
|
cfg: RuntimeConfig, state: var (altair.BeaconState | merge.BeaconState),
|
2021-10-13 14:24:36 +00:00
|
|
|
info: var altair.EpochInfo)
|
2021-08-25 14:43:00 +00:00
|
|
|
{.nbench.} =
|
2021-05-28 15:25:58 +00:00
|
|
|
if get_current_epoch(state) == GENESIS_EPOCH:
|
|
|
|
return
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
let
|
|
|
|
total_active_balance = info.balances.current_epoch
|
|
|
|
base_reward_per_increment = get_base_reward_per_increment(
|
|
|
|
total_active_balance)
|
2021-08-20 14:41:32 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
doAssert state.validators.len() == info.validators.len()
|
2021-06-01 12:40:13 +00:00
|
|
|
for flag_index in 0 ..< PARTICIPATION_FLAG_WEIGHTS.len:
|
2021-10-13 14:24:36 +00:00
|
|
|
for validator_index, delta in get_flag_index_deltas(
|
|
|
|
state, flag_index, base_reward_per_increment, info):
|
|
|
|
info.validators[validator_index].delta.add(delta)
|
2021-06-01 12:40:13 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
for validator_index, penalty in get_inactivity_penalty_deltas(
|
|
|
|
cfg, state, info):
|
|
|
|
info.validators[validator_index].delta.penalties += penalty
|
2021-06-01 12:40:13 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
# Here almost all balances are updated (assuming most validators are active) -
|
|
|
|
# clearing the cache becomes a bottleneck if done item by item because of the
|
|
|
|
# recursive nature of cache clearing - instead, we clear the whole cache then
|
|
|
|
# update the raw list directly
|
2021-08-24 20:09:03 +00:00
|
|
|
state.balances.clearCache()
|
2021-06-01 12:40:13 +00:00
|
|
|
for index in 0 ..< len(state.validators):
|
2021-08-24 20:09:03 +00:00
|
|
|
var balance = state.balances.asSeq()[index]
|
2021-10-13 14:24:36 +00:00
|
|
|
increase_balance(balance, info.validators[index].delta.rewards)
|
|
|
|
decrease_balance(balance, info.validators[index].delta.penalties)
|
2021-08-24 20:09:03 +00:00
|
|
|
state.balances.asSeq()[index] = balance
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-12 11:36:52 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#registry-updates
|
|
|
|
func process_registry_updates*(
|
|
|
|
cfg: RuntimeConfig, state: var SomeBeaconState, cache: var StateCache) {.nbench.} =
|
|
|
|
## Process activation eligibility and ejections
|
|
|
|
|
|
|
|
# Make visible, e.g.,
|
|
|
|
# https://github.com/status-im/nimbus-eth2/pull/608
|
|
|
|
# https://github.com/sigp/lighthouse/pull/657
|
|
|
|
let epoch {.used.} = get_current_epoch(state)
|
|
|
|
trace "process_registry_updates validator balances",
|
|
|
|
balances=state.balances,
|
|
|
|
active_validator_indices=get_active_validator_indices(state, epoch),
|
|
|
|
epoch=epoch
|
|
|
|
|
|
|
|
# is_active_validator(...) is activation_epoch <= epoch < exit_epoch,
|
|
|
|
# and changes here to either activation_epoch or exit_epoch only take
|
|
|
|
# effect with a compute_activation_exit_epoch(...) delay of, based on
|
|
|
|
# the current epoch, 1 + MAX_SEED_LOOKAHEAD epochs ahead. Thus caches
|
|
|
|
# remain valid for this epoch through though this function along with
|
|
|
|
# the rest of the epoch transition.
|
|
|
|
for index in 0..<state.validators.len():
|
|
|
|
if is_eligible_for_activation_queue(state.validators.asSeq()[index]):
|
|
|
|
state.validators[index].activation_eligibility_epoch =
|
|
|
|
get_current_epoch(state) + 1
|
|
|
|
|
|
|
|
if is_active_validator(state.validators.asSeq()[index], get_current_epoch(state)) and
|
|
|
|
state.validators.asSeq()[index].effective_balance <= cfg.EJECTION_BALANCE:
|
|
|
|
initiate_validator_exit(cfg, state, index.ValidatorIndex, cache)
|
|
|
|
|
|
|
|
## Queue validators eligible for activation and not dequeued for activation
|
|
|
|
var activation_queue : seq[tuple[a: Epoch, b: int]] = @[]
|
|
|
|
for index in 0..<state.validators.len():
|
|
|
|
let validator = unsafeAddr state.validators.asSeq()[index]
|
|
|
|
if is_eligible_for_activation(state, validator[]):
|
|
|
|
activation_queue.add (
|
|
|
|
validator[].activation_eligibility_epoch, index)
|
|
|
|
|
|
|
|
activation_queue.sort(system.cmp)
|
|
|
|
|
|
|
|
## Dequeued validators for activation up to churn limit (without resetting
|
|
|
|
## activation epoch)
|
|
|
|
let churn_limit = get_validator_churn_limit(cfg, state, cache)
|
|
|
|
for i, epoch_and_index in activation_queue:
|
|
|
|
if i.uint64 >= churn_limit:
|
|
|
|
break
|
|
|
|
let
|
|
|
|
(_, index) = epoch_and_index
|
|
|
|
state.validators[index].activation_epoch =
|
|
|
|
compute_activation_exit_epoch(get_current_epoch(state))
|
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#slashings
|
2021-09-10 18:56:03 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.4/specs/altair/beacon-chain.md#slashings
|
2021-08-25 14:43:00 +00:00
|
|
|
func process_slashings*(state: var SomeBeaconState, total_balance: Gwei) {.nbench.} =
|
2019-06-28 13:44:44 +00:00
|
|
|
let
|
More 0.8.0 updates (#311)
* replace BeaconState.finalized_{epoch,root} with BeaconState.finalized_checkpoint; rename get_delayed_activation_exit_epoch(...) to compute_activation_exit_epoch(...) and mark as 0.8.0; update get_churn_limit(...)/get_validator_churn_limit(...) to 0.8.0; update process_registry_updates(...) to 0.8.0
* update process_crosslinks(...) to 0.8.0; mark compute_start_slot_of_epoch(...) and get_committee_count(...) as 0.8.0
* mark Fork, is_slashable_validator(...), and get_beacon_proposer_index(...) as 0.8.0
* rename LATEST_SLASHED_EXIT_LENGTH to EPOCHS_PER_SLASHINGS_VECTOR; update process_slashings(...) to 0.8.0; remove pointless type conversion warning in get_previous_epoch(...)
* convert remaining references to finalized_epoch to finalized_checkpoint.epoch
* update slash_validator(...) to 0.8.0; mark inital value, Gwei, and time constants as 0.8.0; mark hash(...) and processBlockHeader(...) as 0.8.0
* rename WHISTLEBLOWING_REWARD_QUOTIENT to WHISTLEBLOWER_REWARD_QUOTIENT; rename LATEST_ACTIVE_INDEX_ROOTS_LENGTH to EPOCHS_PER_HISTORICAL_VECTOR (randao will also get merged into this); remove get_active_index_root(...); mark time parameter, signature domain types, and max operations per block constants as 0.8.0; update rewards and penalties constants to 0.8.0
* update is_valid_indexed_attestation(...) to 0.8.0; mark process_slot(...) as 0.8.0
* replace BeaconState.{current,previous}_justified_{epoch,root} with BeaconState.{current,previous}_justified_checkpoint
2019-07-05 08:30:05 +00:00
|
|
|
epoch = get_current_epoch(state)
|
2021-05-28 15:25:58 +00:00
|
|
|
multiplier =
|
|
|
|
# tradeoff here about interleaving phase0/altair, but for these
|
|
|
|
# single-constant changes...
|
|
|
|
uint64(when state is phase0.BeaconState:
|
|
|
|
PROPORTIONAL_SLASHING_MULTIPLIER
|
2021-09-27 14:22:58 +00:00
|
|
|
elif state is altair.BeaconState or state is merge.BeaconState:
|
2021-05-28 15:25:58 +00:00
|
|
|
PROPORTIONAL_SLASHING_MULTIPLIER_ALTAIR
|
|
|
|
else:
|
|
|
|
raiseAssert "process_slashings: incorrect BeaconState type")
|
2020-10-08 18:00:07 +00:00
|
|
|
adjusted_total_slashing_balance =
|
2021-09-27 14:22:58 +00:00
|
|
|
min(sum(state.slashings.data) * multiplier, total_balance)
|
2019-06-28 13:44:44 +00:00
|
|
|
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
for index in 0..<state.validators.len:
|
|
|
|
let validator = unsafeAddr state.validators.asSeq()[index]
|
|
|
|
if validator[].slashed and epoch + EPOCHS_PER_SLASHINGS_VECTOR div 2 ==
|
|
|
|
validator[].withdrawable_epoch:
|
2021-08-25 14:43:00 +00:00
|
|
|
const increment = EFFECTIVE_BALANCE_INCREMENT # Factored out from penalty
|
|
|
|
# numerator to avoid uint64 overflow
|
2019-09-03 03:12:09 +00:00
|
|
|
let penalty_numerator =
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
validator[].effective_balance div increment *
|
2020-10-08 18:00:07 +00:00
|
|
|
adjusted_total_slashing_balance
|
2019-09-03 03:12:09 +00:00
|
|
|
let penalty = penalty_numerator div total_balance * increment
|
2019-06-28 13:44:44 +00:00
|
|
|
decrease_balance(state, index.ValidatorIndex, penalty)
|
|
|
|
|
2021-08-20 14:41:32 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/34cea67b91/specs/phase0/beacon-chain.md#eth1-data-votes-updates
|
2021-05-28 15:25:58 +00:00
|
|
|
func process_eth1_data_reset*(state: var SomeBeaconState) {.nbench.} =
|
2021-02-14 19:31:01 +00:00
|
|
|
let next_epoch = get_current_epoch(state) + 1
|
2019-06-28 13:44:44 +00:00
|
|
|
|
|
|
|
# Reset eth1 data votes
|
2020-03-14 21:54:45 +00:00
|
|
|
if next_epoch mod EPOCHS_PER_ETH1_VOTING_PERIOD == 0:
|
2020-05-27 11:36:02 +00:00
|
|
|
state.eth1_data_votes = default(type state.eth1_data_votes)
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#effective-balances-updates
|
2021-05-28 15:25:58 +00:00
|
|
|
func process_effective_balance_updates*(state: var SomeBeaconState) {.nbench.} =
|
2019-06-28 13:44:44 +00:00
|
|
|
# Update effective balances with hysteresis
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
for index in 0..<state.validators.len:
|
|
|
|
let balance = state.balances.asSeq()[index]
|
2020-03-14 21:54:45 +00:00
|
|
|
const
|
|
|
|
HYSTERESIS_INCREMENT =
|
|
|
|
EFFECTIVE_BALANCE_INCREMENT div HYSTERESIS_QUOTIENT
|
|
|
|
DOWNWARD_THRESHOLD =
|
|
|
|
HYSTERESIS_INCREMENT * HYSTERESIS_DOWNWARD_MULTIPLIER
|
|
|
|
UPWARD_THRESHOLD = HYSTERESIS_INCREMENT * HYSTERESIS_UPWARD_MULTIPLIER
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
let effective_balance = state.validators.asSeq()[index].effective_balance
|
|
|
|
if balance + DOWNWARD_THRESHOLD < effective_balance or
|
|
|
|
effective_balance + UPWARD_THRESHOLD < balance:
|
2019-07-01 09:13:14 +00:00
|
|
|
state.validators[index].effective_balance =
|
2019-06-28 13:44:44 +00:00
|
|
|
min(
|
|
|
|
balance - balance mod EFFECTIVE_BALANCE_INCREMENT,
|
|
|
|
MAX_EFFECTIVE_BALANCE)
|
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#slashings-balances-updates
|
2021-05-28 15:25:58 +00:00
|
|
|
func process_slashings_reset*(state: var SomeBeaconState) {.nbench.} =
|
2021-02-14 19:31:01 +00:00
|
|
|
let next_epoch = get_current_epoch(state) + 1
|
|
|
|
|
2019-07-10 12:23:02 +00:00
|
|
|
# Reset slashings
|
2020-06-12 16:43:20 +00:00
|
|
|
state.slashings[int(next_epoch mod EPOCHS_PER_SLASHINGS_VECTOR)] = 0.Gwei
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#randao-mixes-updates
|
2021-05-28 15:25:58 +00:00
|
|
|
func process_randao_mixes_reset*(state: var SomeBeaconState) {.nbench.} =
|
2021-02-14 19:31:01 +00:00
|
|
|
let
|
|
|
|
current_epoch = get_current_epoch(state)
|
|
|
|
next_epoch = current_epoch + 1
|
|
|
|
|
2019-06-28 13:44:44 +00:00
|
|
|
# Set randao mix
|
2019-09-04 13:57:18 +00:00
|
|
|
state.randao_mixes[next_epoch mod EPOCHS_PER_HISTORICAL_VECTOR] =
|
2019-06-28 13:44:44 +00:00
|
|
|
get_randao_mix(state, current_epoch)
|
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#historical-roots-updates
|
2021-05-28 15:25:58 +00:00
|
|
|
func process_historical_roots_update*(state: var SomeBeaconState) {.nbench.} =
|
2021-10-12 10:17:37 +00:00
|
|
|
## Set historical root accumulator
|
2021-02-14 19:31:01 +00:00
|
|
|
let next_epoch = get_current_epoch(state) + 1
|
|
|
|
|
2020-07-13 14:44:58 +00:00
|
|
|
if next_epoch mod (SLOTS_PER_HISTORICAL_ROOT div SLOTS_PER_EPOCH) == 0:
|
2020-04-23 11:39:38 +00:00
|
|
|
# Equivalent to hash_tree_root(foo: HistoricalBatch), but without using
|
|
|
|
# significant additional stack or heap.
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#historicalbatch
|
2020-10-08 19:02:05 +00:00
|
|
|
# In response to https://github.com/status-im/nimbus-eth2/issues/921
|
2021-04-08 10:11:04 +00:00
|
|
|
if not state.historical_roots.add hash_tree_root(
|
|
|
|
[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!"
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#participation-records-rotation
|
2021-05-28 15:25:58 +00:00
|
|
|
func process_participation_record_updates*(state: var phase0.BeaconState) {.nbench.} =
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
# Rotate current/previous epoch attestations - using swap avoids copying all
|
|
|
|
# elements using a slow genericSeqAssign
|
|
|
|
state.previous_epoch_attestations.clear()
|
|
|
|
swap(state.previous_epoch_attestations, state.current_epoch_attestations)
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-09-28 18:23:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/altair/beacon-chain.md#participation-flags-updates
|
2021-09-27 14:22:58 +00:00
|
|
|
func process_participation_flag_updates*(state: var (altair.BeaconState | merge.BeaconState)) =
|
2021-05-28 15:25:58 +00:00
|
|
|
state.previous_epoch_participation = state.current_epoch_participation
|
|
|
|
|
2021-06-24 18:34:08 +00:00
|
|
|
const zero = 0.ParticipationFlags
|
|
|
|
for i in 0 ..< state.current_epoch_participation.len:
|
|
|
|
state.current_epoch_participation.data[i] = zero
|
|
|
|
|
|
|
|
# Shouldn't be wasted zeroing, because state.current_epoch_participation only
|
|
|
|
# grows. New elements are automatically initialized to 0, as required.
|
|
|
|
doAssert state.current_epoch_participation.data.setLen(state.validators.len)
|
|
|
|
|
|
|
|
state.current_epoch_participation.resetCache()
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-09-28 18:23:15 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/altair/beacon-chain.md#sync-committee-updates
|
2021-09-27 14:22:58 +00:00
|
|
|
proc process_sync_committee_updates*(state: var (altair.BeaconState | merge.BeaconState)) =
|
2021-05-28 15:25:58 +00:00
|
|
|
let next_epoch = get_current_epoch(state) + 1
|
|
|
|
if next_epoch mod EPOCHS_PER_SYNC_COMMITTEE_PERIOD == 0:
|
|
|
|
state.current_sync_committee = state.next_sync_committee
|
|
|
|
state.next_sync_committee = get_next_sync_committee(state)
|
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#inactivity-scores
|
2021-10-13 14:24:36 +00:00
|
|
|
func process_inactivity_updates*(
|
|
|
|
cfg: RuntimeConfig, state: var (altair.BeaconState | merge.BeaconState),
|
|
|
|
info: altair.EpochInfo) =
|
2021-05-28 15:25:58 +00:00
|
|
|
# Score updates based on previous epoch participation, skip genesis epoch
|
|
|
|
if get_current_epoch(state) == GENESIS_EPOCH:
|
|
|
|
return
|
|
|
|
|
2021-06-21 08:35:24 +00:00
|
|
|
let
|
|
|
|
previous_epoch = get_previous_epoch(state) # get_eligible_validator_indices()
|
2021-08-25 14:43:00 +00:00
|
|
|
not_in_inactivity_leak = not is_in_inactivity_leak(state)
|
|
|
|
|
|
|
|
state.inactivity_scores.clearCache()
|
2021-05-28 15:25:58 +00:00
|
|
|
for index in 0'u64 ..< state.validators.lenu64:
|
2021-10-13 14:24:36 +00:00
|
|
|
if not is_eligible_validator(info.validators[index]):
|
2021-05-28 15:25:58 +00:00
|
|
|
continue
|
|
|
|
|
2021-06-21 08:35:24 +00:00
|
|
|
# Increase the inactivity score of inactive validators
|
2021-08-25 14:43:00 +00:00
|
|
|
var inactivity_score = state.inactivity_scores.asSeq()[index]
|
|
|
|
# TODO activeness already checked; remove redundant checks between
|
|
|
|
# is_active_validator and is_unslashed_participating_index
|
|
|
|
if is_unslashed_participating_index(
|
|
|
|
state, TIMELY_TARGET_FLAG_INDEX, previous_epoch, index.ValidatorIndex):
|
2021-08-24 20:09:03 +00:00
|
|
|
inactivity_score -= min(1'u64, inactivity_score)
|
2021-05-28 15:25:58 +00:00
|
|
|
else:
|
2021-08-24 20:09:03 +00:00
|
|
|
inactivity_score += cfg.INACTIVITY_SCORE_BIAS
|
2021-06-21 08:35:24 +00:00
|
|
|
# Decrease the inactivity score of all eligible validators during a
|
|
|
|
# leak-free epoch
|
2021-08-24 20:09:03 +00:00
|
|
|
if not_in_inactivity_leak:
|
|
|
|
inactivity_score -= min(INACTIVITY_SCORE_RECOVERY_RATE.uint64, inactivity_score)
|
2021-08-25 14:43:00 +00:00
|
|
|
state.inactivity_scores.asSeq()[index] = inactivity_score
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#epoch-processing
|
2021-05-05 06:54:21 +00:00
|
|
|
proc process_epoch*(
|
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
|
|
|
cfg: RuntimeConfig, state: var phase0.BeaconState, flags: UpdateFlags,
|
2021-10-13 14:24:36 +00:00
|
|
|
cache: var StateCache, info: var phase0.EpochInfo) {.nbench.} =
|
2020-05-11 06:25:49 +00:00
|
|
|
let currentEpoch = get_current_epoch(state)
|
2019-08-19 16:41:13 +00:00
|
|
|
trace "process_epoch",
|
2020-05-11 06:25:49 +00:00
|
|
|
current_epoch = currentEpoch
|
2021-10-13 14:24:36 +00:00
|
|
|
init(info, state)
|
|
|
|
info.process_attestations(state, cache)
|
2019-08-14 08:56:32 +00:00
|
|
|
|
2021-08-20 14:41:32 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#justification-and-finalization
|
2021-10-13 14:24:36 +00:00
|
|
|
process_justification_and_finalization(state, info.total_balances, flags)
|
2020-05-09 12:43:15 +00:00
|
|
|
|
|
|
|
# state.slot hasn't been incremented yet.
|
2021-05-05 06:54:21 +00:00
|
|
|
if verifyFinalization in flags and currentEpoch >= 2:
|
2020-07-28 17:35:32 +00:00
|
|
|
doAssert state.current_justified_checkpoint.epoch + 2 >= currentEpoch
|
|
|
|
|
2021-05-05 06:54:21 +00:00
|
|
|
if verifyFinalization in flags and currentEpoch >= 3:
|
2020-05-09 12:43:15 +00:00
|
|
|
# Rule 2/3/4 finalization results in the most pessimal case. The other
|
|
|
|
# three finalization rules finalize more quickly as long as the any of
|
|
|
|
# the finalization rules triggered.
|
2020-05-11 06:25:49 +00:00
|
|
|
doAssert state.finalized_checkpoint.epoch + 3 >= currentEpoch
|
2019-08-14 08:56:32 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#rewards-and-penalties-1
|
2021-10-13 14:24:36 +00:00
|
|
|
process_rewards_and_penalties(state, info)
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#registry-updates
|
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
|
|
|
process_registry_updates(cfg, state, cache)
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#slashings
|
2021-10-13 14:24:36 +00:00
|
|
|
process_slashings(state, info.total_balances.current_epoch)
|
2019-06-28 13:44:44 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#eth1-data-votes-updates
|
2021-06-03 09:42:25 +00:00
|
|
|
process_eth1_data_reset(state)
|
2021-09-28 14:01:46 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#effective-balances-updates
|
2021-06-03 09:42:25 +00:00
|
|
|
process_effective_balance_updates(state)
|
2021-09-28 14:01:46 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#slashings-balances-updates
|
2021-06-03 09:42:25 +00:00
|
|
|
process_slashings_reset(state)
|
2021-09-28 14:01:46 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#randao-mixes-updates
|
2021-06-03 09:42:25 +00:00
|
|
|
process_randao_mixes_reset(state)
|
2021-09-28 14:01:46 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#historical-roots-updates
|
2021-06-03 09:42:25 +00:00
|
|
|
process_historical_roots_update(state)
|
2021-09-28 14:01:46 +00:00
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#participation-records-rotation
|
2021-06-03 09:42:25 +00:00
|
|
|
process_participation_record_updates(state)
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
func init*(
|
|
|
|
info: var altair.EpochInfo,
|
|
|
|
state: altair.BeaconState | merge.BeaconState) =
|
|
|
|
# init participation, overwriting the full structure
|
|
|
|
info.balances = get_unslashed_participating_balances(state)
|
|
|
|
info.validators.setLen(state.validators.len())
|
|
|
|
|
|
|
|
let previous_epoch = get_previous_epoch(state)
|
|
|
|
for index in 0..<state.validators.len():
|
|
|
|
var flags: set[ParticipationFlag]
|
|
|
|
if is_eligible_validator(state.validators[index], previous_epoch):
|
|
|
|
flags.incl ParticipationFlag.eligible
|
|
|
|
|
|
|
|
info.validators[index] = ParticipationInfo(
|
|
|
|
flags: flags
|
|
|
|
)
|
|
|
|
|
|
|
|
func init*(
|
|
|
|
T: type altair.EpochInfo, state: altair.BeaconState | merge.BeaconState): T =
|
|
|
|
init(result, state)
|
|
|
|
|
2021-10-12 08:09:59 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/altair/beacon-chain.md#epoch-processing
|
2021-05-28 15:25:58 +00:00
|
|
|
proc process_epoch*(
|
2021-09-27 14:22:58 +00:00
|
|
|
cfg: RuntimeConfig, state: var (altair.BeaconState | merge.BeaconState),
|
2021-10-13 14:24:36 +00:00
|
|
|
flags: UpdateFlags, cache: var StateCache, info: var altair.EpochInfo)
|
2021-09-27 14:22:58 +00:00
|
|
|
{.nbench.} =
|
2021-05-28 15:25:58 +00:00
|
|
|
let currentEpoch = get_current_epoch(state)
|
|
|
|
trace "process_epoch",
|
|
|
|
current_epoch = currentEpoch
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
info.init(state)
|
2021-05-30 20:23:10 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#justification-and-finalization
|
2021-10-13 14:24:36 +00:00
|
|
|
process_justification_and_finalization(state, info.balances, flags)
|
2021-05-28 15:25:58 +00:00
|
|
|
|
|
|
|
# state.slot hasn't been incremented yet.
|
|
|
|
if verifyFinalization in flags and currentEpoch >= 2:
|
|
|
|
doAssert state.current_justified_checkpoint.epoch + 2 >= currentEpoch
|
|
|
|
|
|
|
|
if verifyFinalization in flags and currentEpoch >= 3:
|
|
|
|
# Rule 2/3/4 finalization results in the most pessimal case. The other
|
|
|
|
# three finalization rules finalize more quickly as long as the any of
|
|
|
|
# the finalization rules triggered.
|
|
|
|
doAssert state.finalized_checkpoint.epoch + 3 >= currentEpoch
|
|
|
|
|
2021-10-13 14:24:36 +00:00
|
|
|
process_inactivity_updates(cfg, state, info) # [New in Altair]
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-09-29 23:43:28 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/phase0/beacon-chain.md#rewards-and-penalties-1
|
2021-10-13 14:24:36 +00:00
|
|
|
process_rewards_and_penalties(cfg, state, info)
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-10-12 10:17:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.2/specs/phase0/beacon-chain.md#registry-updates
|
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
|
|
|
process_registry_updates(cfg, state, cache)
|
2021-05-28 15:25:58 +00:00
|
|
|
|
2021-08-20 14:41:32 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#slashings
|
2021-10-13 14:24:36 +00:00
|
|
|
process_slashings(state, info.balances.current_epoch)
|
2021-05-28 15:25:58 +00:00
|
|
|
|
|
|
|
process_eth1_data_reset(state)
|
|
|
|
|
|
|
|
process_effective_balance_updates(state)
|
|
|
|
|
|
|
|
process_slashings_reset(state)
|
|
|
|
|
|
|
|
process_randao_mixes_reset(state)
|
|
|
|
|
|
|
|
process_historical_roots_update(state)
|
|
|
|
|
|
|
|
process_participation_flag_updates(state) # [New in Altair]
|
|
|
|
|
|
|
|
process_sync_committee_updates(state) # [New in Altair]
|