2018-07-23 12:58:41 +00:00
|
|
|
# beacon_chain
|
2022-01-04 06:08:19 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2018-07-23 12:58:41 +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).
|
2018-07-23 12:58:41 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-11-28 19:49:03 +00:00
|
|
|
# Uncategorized helper functions from the spec
|
|
|
|
|
2020-04-22 05:53:02 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# References to `vFuture` refer to the pre-release proposal of the libp2p based
|
|
|
|
# light client sync protocol. Conflicting release versions are not in use.
|
|
|
|
# https://github.com/ethereum/consensus-specs/pull/2802
|
|
|
|
|
2019-09-25 17:07:08 +00:00
|
|
|
import
|
|
|
|
# Standard lib
|
2022-06-26 17:33:06 +00:00
|
|
|
std/[algorithm, math, sets, tables],
|
2021-10-14 08:28:22 +00:00
|
|
|
# Status libraries
|
2022-05-23 12:02:54 +00:00
|
|
|
stew/[bitops2, byteutils, endians2, objects],
|
2022-07-10 15:26:29 +00:00
|
|
|
chronicles,
|
2019-09-25 17:07:08 +00:00
|
|
|
# Internal
|
2022-01-06 11:25:35 +00:00
|
|
|
./datatypes/[phase0, altair, bellatrix],
|
2021-11-05 07:34:34 +00:00
|
|
|
"."/[eth2_merkleization, forks, ssz_codec]
|
2021-08-12 13:08:20 +00:00
|
|
|
|
2021-08-18 18:57:58 +00:00
|
|
|
# TODO although eth2_merkleization already exports ssz_codec, *sometimes* code
|
|
|
|
# fails to compile if the export is not done here also
|
|
|
|
export
|
2021-11-05 07:34:34 +00:00
|
|
|
forks, eth2_merkleization, ssz_codec
|
2018-07-23 12:58:41 +00:00
|
|
|
|
2022-07-10 15:26:29 +00:00
|
|
|
type FinalityCheckpoints* = object
|
|
|
|
justified*: Checkpoint
|
|
|
|
finalized*: Checkpoint
|
|
|
|
|
|
|
|
func shortLog*(v: FinalityCheckpoints): auto =
|
|
|
|
(
|
|
|
|
justified: shortLog(v.justified),
|
|
|
|
finalized: shortLog(v.finalized)
|
|
|
|
)
|
|
|
|
|
|
|
|
chronicles.formatIt FinalityCheckpoints: it.shortLog
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#integer_squareroot
|
2018-12-19 04:36:10 +00:00
|
|
|
func integer_squareroot*(n: SomeInteger): SomeInteger =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the largest integer ``x`` such that ``x**2 <= n``.
|
2019-03-16 19:52:37 +00:00
|
|
|
doAssert n >= 0'u64
|
|
|
|
|
2018-12-03 21:41:24 +00:00
|
|
|
var
|
|
|
|
x = n
|
|
|
|
y = (x + 1) div 2
|
|
|
|
while y < x:
|
|
|
|
x = y
|
|
|
|
y = (x + n div x) div 2
|
|
|
|
x
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#is_active_validator
|
2019-02-20 01:33:58 +00:00
|
|
|
func is_active_validator*(validator: Validator, epoch: Epoch): bool =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Check if ``validator`` is active
|
2019-01-29 03:22:22 +00:00
|
|
|
validator.activation_epoch <= epoch and epoch < validator.exit_epoch
|
2019-01-16 11:07:41 +00:00
|
|
|
|
2021-12-20 19:20:31 +00:00
|
|
|
func is_exited_validator*(validator: Validator, epoch: Epoch): bool =
|
|
|
|
## Check if ``validator`` is exited
|
|
|
|
validator.exit_epoch <= epoch
|
|
|
|
|
|
|
|
func is_withdrawable_validator*(validator: Validator, epoch: Epoch): bool =
|
|
|
|
epoch >= validator.withdrawable_epoch
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#get_active_validator_indices
|
2021-11-05 07:34:34 +00:00
|
|
|
iterator get_active_validator_indices*(state: ForkyBeaconState, epoch: Epoch):
|
2021-08-24 20:09:03 +00:00
|
|
|
ValidatorIndex =
|
2022-05-23 23:39:08 +00:00
|
|
|
for vidx in state.validators.vindices:
|
|
|
|
if is_active_validator(state.validators[vidx], epoch):
|
|
|
|
yield vidx
|
2021-08-24 20:09:03 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_active_validator_indices*(state: ForkyBeaconState, epoch: Epoch):
|
2019-05-09 12:27:37 +00:00
|
|
|
seq[ValidatorIndex] =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the sequence of active validator indices at ``epoch``.
|
2021-08-24 20:09:03 +00:00
|
|
|
var res = newSeqOfCap[ValidatorIndex](state.validators.len)
|
2022-05-23 23:39:08 +00:00
|
|
|
for vidx in get_active_validator_indices(state, epoch):
|
|
|
|
res.add vidx
|
2021-08-24 20:09:03 +00:00
|
|
|
res
|
2019-05-09 12:27:37 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_active_validator_indices_len*(state: ForkyBeaconState, epoch: Epoch):
|
2021-05-28 15:25:58 +00:00
|
|
|
uint64 =
|
2022-05-23 23:39:08 +00:00
|
|
|
for vidx in state.validators.vindices:
|
2022-05-30 13:30:42 +00:00
|
|
|
if is_active_validator(state.validators.item(vidx), epoch):
|
2020-09-22 20:42:42 +00:00
|
|
|
inc result
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_active_validator_indices_len*(
|
|
|
|
state: ForkedHashedBeaconState; epoch: Epoch): uint64 =
|
|
|
|
withState(state):
|
|
|
|
get_active_validator_indices_len(state.data, epoch)
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#get_current_epoch
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_current_epoch*(state: ForkyBeaconState): Epoch =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the current epoch.
|
2022-01-11 10:01:54 +00:00
|
|
|
state.slot.epoch
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#get_current_epoch
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_current_epoch*(state: ForkedHashedBeaconState): Epoch =
|
|
|
|
## Return the current epoch.
|
2022-01-11 10:01:54 +00:00
|
|
|
withState(state): get_current_epoch(state.data)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2022-07-06 10:33:02 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#get_previous_epoch
|
|
|
|
func get_previous_epoch*(
|
|
|
|
state: ForkyBeaconState | ForkedHashedBeaconState): Epoch =
|
|
|
|
## Return the previous epoch (unless the current epoch is ``GENESIS_EPOCH``).
|
|
|
|
get_previous_epoch(get_current_epoch(state))
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#get_randao_mix
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_randao_mix*(state: ForkyBeaconState, epoch: Epoch): Eth2Digest =
|
2020-07-13 14:44:58 +00:00
|
|
|
## Returns the randao mix at a recent ``epoch``.
|
|
|
|
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR]
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func bytes_to_uint64*(data: openArray[byte]): uint64 =
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert data.len == 8
|
2019-02-13 10:26:32 +00:00
|
|
|
|
|
|
|
# Little-endian data representation
|
2021-02-08 15:13:02 +00:00
|
|
|
uint64.fromBytesLE(data)
|
2019-02-13 10:26:32 +00:00
|
|
|
|
2022-01-08 20:06:34 +00:00
|
|
|
func uint_to_bytes*(x: uint64): array[8, byte] = toBytesLE(x)
|
|
|
|
func uint_to_bytes*(x: uint32): array[4, byte] = toBytesLE(x)
|
|
|
|
func uint_to_bytes*(x: uint16): array[2, byte] = toBytesLE(x)
|
|
|
|
func uint_to_bytes*(x: uint8): array[1, byte] = toBytesLE(x)
|
2019-02-13 10:26:32 +00:00
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#compute_domain
|
2019-09-01 15:02:49 +00:00
|
|
|
func compute_domain*(
|
|
|
|
domain_type: DomainType,
|
2020-07-07 23:02:14 +00:00
|
|
|
fork_version: Version,
|
2021-03-19 02:22:45 +00:00
|
|
|
genesis_validators_root: Eth2Digest = ZERO_HASH): Eth2Domain =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the domain for the ``domain_type`` and ``fork_version``.
|
2022-07-12 17:50:12 +00:00
|
|
|
#
|
|
|
|
# TODO Can't be used as part of a const/static expression:
|
|
|
|
# https://github.com/nim-lang/Nim/issues/15952
|
|
|
|
# https://github.com/nim-lang/Nim/issues/19969
|
2020-03-22 16:03:07 +00:00
|
|
|
let fork_data_root =
|
|
|
|
compute_fork_data_root(fork_version, genesis_validators_root)
|
2022-01-08 20:06:34 +00:00
|
|
|
result[0..3] = domain_type.data
|
2020-08-27 06:32:51 +00:00
|
|
|
result[4..31] = fork_data_root.data.toOpenArray(0, 27)
|
2019-06-14 16:21:04 +00:00
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#get_domain
|
2019-03-18 15:42:42 +00:00
|
|
|
func get_domain*(
|
2021-03-19 02:22:45 +00:00
|
|
|
fork: Fork,
|
|
|
|
domain_type: DomainType,
|
|
|
|
epoch: Epoch,
|
|
|
|
genesis_validators_root: Eth2Digest): Eth2Domain =
|
2019-04-29 16:48:30 +00:00
|
|
|
## Return the signature domain (fork version concatenated with domain type)
|
|
|
|
## of a message.
|
2020-03-30 11:31:44 +00:00
|
|
|
let fork_version =
|
|
|
|
if epoch < fork.epoch:
|
|
|
|
fork.previous_version
|
|
|
|
else:
|
|
|
|
fork.current_version
|
|
|
|
compute_domain(domain_type, fork_version, genesis_validators_root)
|
2019-03-18 15:42:42 +00:00
|
|
|
|
2019-11-21 09:57:59 +00:00
|
|
|
func get_domain*(
|
2021-11-05 07:34:34 +00:00
|
|
|
state: ForkyBeaconState, domain_type: DomainType, epoch: Epoch): Eth2Domain =
|
2019-11-21 09:57:59 +00:00
|
|
|
## Return the signature domain (fork version concatenated with domain type)
|
|
|
|
## of a message.
|
2020-04-22 23:35:55 +00:00
|
|
|
get_domain(state.fork, domain_type, epoch, state.genesis_validators_root)
|
2019-04-29 16:48:30 +00:00
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#compute_signing_root
|
2021-03-19 02:22:45 +00:00
|
|
|
func compute_signing_root*(ssz_object: auto, domain: Eth2Domain): Eth2Digest =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the signing root of an object by calculating the root of the
|
|
|
|
## object-domain tree.
|
2020-06-29 18:08:58 +00:00
|
|
|
let domain_wrapped_object = SigningData(
|
|
|
|
object_root: hash_tree_root(ssz_object),
|
|
|
|
domain: domain
|
|
|
|
)
|
2020-01-30 14:03:26 +00:00
|
|
|
hash_tree_root(domain_wrapped_object)
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/beacon-chain.md#get_seed
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_seed*(state: ForkyBeaconState, epoch: Epoch, domain_type: DomainType):
|
2021-05-28 15:25:58 +00:00
|
|
|
Eth2Digest =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the seed at ``epoch``.
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2019-11-10 00:03:41 +00:00
|
|
|
var seed_input : array[4+8+32, byte]
|
2019-04-05 14:18:13 +00:00
|
|
|
|
0.6.2 updates (#275)
* update process_justification_and_finalization to 0.6.2; mark AttesterSlashing as 0.6.2
* replace get_effective_balance(...) with state.validator_registry[idx].effective_balance; rm get_effective_balance, process_ejections, should_update_validator_registry, update_validator_registry, and update_registry_and_shuffling_data; update get_total_balance to 0.6.2; implement process_registry_updates
* rm exit_validator; implement is_slashable_attestation_data; partly update processAttesterSlashings
* mark HistoricalBatch and Eth1Data as 0.6.2; implement get_shard_delta(...); replace 0.5 finish_epoch_update with 0.6 process_final_updates
* mark increase_balance, decrease_balance, get_delayed_activation_exit_epoch, bls_aggregate_pubkeys, bls_verify_multiple, Attestation, Transfer, slot_to_epoch, Crosslink, get_current_epoch, int_to_bytes*, various constants, processEth1Data, processTransfers, and verifyStateRoot as 0.6.2; rm is_double_vote and is_surround_vote
* mark get_bitfield_bit, verify_bitfield, ProposerSlashing, DepositData, VoluntaryExit, PendingAttestation, Fork, integer_squareroot, get_epoch_start_slot, is_active_validator, generate_seed, some constants to 0.6.2; rename MIN_PENALTY_QUOTIENT to MIN_SLASHING_PENALTY_QUOTIENT
* rm get_previous_total_balance, get_current_epoch_boundary_attestations, get_previous_epoch_boundary_attestations, and get_previous_epoch_matching_head_attestations
* update BeaconState to 0.6.2; simplify legacy get_crosslink_committees_at_slot infrastructure a bit by noting that registry_change is always false; reimplment 0.5 get_crosslink_committees_at_slot in terms of 0.6 get_crosslink_committee
* mark process_deposit(...), get_block_root_at_slot(...), get_block_root(...), Deposit, BeaconBlockHeader, BeaconBlockBody, hash(...), get_active_index_root(...), various constants, get_shard_delta(...), get_epoch_start_shard(...), get_crosslink_committee(...), processRandao(...), processVoluntaryExits(...), cacheState(...) as 0.6.2
* rm removed-since-0.5 split(...), is_power_of_2(...), get_shuffling(...); rm 0.5 versions of get_active_validator_indices and get_epoch_committee_count; add a few tests for integer_squareroot
* mark bytes_to_int(...) and advanceState(...) as 0.6.2
* rm 0.5 get_attesting_indices; update get_attesting_balance to 0.6.2
* another tiny commit to poke AppVeyor to maybe not timeout at connecting to GitHub partway through CI: mark get_churn_limit(...), initiate_validator_exit(...), and Validator as 0.6.2
* mark get_attestation_slot(...), AttestationDataAndCustodyBit, and BeaconBlock as 0.6.2
2019-06-03 10:31:04 +00:00
|
|
|
# Detect potential underflow
|
2019-11-10 00:03:41 +00:00
|
|
|
static:
|
|
|
|
doAssert EPOCHS_PER_HISTORICAL_VECTOR > MIN_SEED_LOOKAHEAD
|
0.6.2 updates (#275)
* update process_justification_and_finalization to 0.6.2; mark AttesterSlashing as 0.6.2
* replace get_effective_balance(...) with state.validator_registry[idx].effective_balance; rm get_effective_balance, process_ejections, should_update_validator_registry, update_validator_registry, and update_registry_and_shuffling_data; update get_total_balance to 0.6.2; implement process_registry_updates
* rm exit_validator; implement is_slashable_attestation_data; partly update processAttesterSlashings
* mark HistoricalBatch and Eth1Data as 0.6.2; implement get_shard_delta(...); replace 0.5 finish_epoch_update with 0.6 process_final_updates
* mark increase_balance, decrease_balance, get_delayed_activation_exit_epoch, bls_aggregate_pubkeys, bls_verify_multiple, Attestation, Transfer, slot_to_epoch, Crosslink, get_current_epoch, int_to_bytes*, various constants, processEth1Data, processTransfers, and verifyStateRoot as 0.6.2; rm is_double_vote and is_surround_vote
* mark get_bitfield_bit, verify_bitfield, ProposerSlashing, DepositData, VoluntaryExit, PendingAttestation, Fork, integer_squareroot, get_epoch_start_slot, is_active_validator, generate_seed, some constants to 0.6.2; rename MIN_PENALTY_QUOTIENT to MIN_SLASHING_PENALTY_QUOTIENT
* rm get_previous_total_balance, get_current_epoch_boundary_attestations, get_previous_epoch_boundary_attestations, and get_previous_epoch_matching_head_attestations
* update BeaconState to 0.6.2; simplify legacy get_crosslink_committees_at_slot infrastructure a bit by noting that registry_change is always false; reimplment 0.5 get_crosslink_committees_at_slot in terms of 0.6 get_crosslink_committee
* mark process_deposit(...), get_block_root_at_slot(...), get_block_root(...), Deposit, BeaconBlockHeader, BeaconBlockBody, hash(...), get_active_index_root(...), various constants, get_shard_delta(...), get_epoch_start_shard(...), get_crosslink_committee(...), processRandao(...), processVoluntaryExits(...), cacheState(...) as 0.6.2
* rm removed-since-0.5 split(...), is_power_of_2(...), get_shuffling(...); rm 0.5 versions of get_active_validator_indices and get_epoch_committee_count; add a few tests for integer_squareroot
* mark bytes_to_int(...) and advanceState(...) as 0.6.2
* rm 0.5 get_attesting_indices; update get_attesting_balance to 0.6.2
* another tiny commit to poke AppVeyor to maybe not timeout at connecting to GitHub partway through CI: mark get_churn_limit(...), initiate_validator_exit(...), and Validator as 0.6.2
* mark get_attestation_slot(...), AttestationDataAndCustodyBit, and BeaconBlock as 0.6.2
2019-06-03 10:31:04 +00:00
|
|
|
|
2022-01-08 20:06:34 +00:00
|
|
|
seed_input[0..3] = domain_type.data
|
|
|
|
seed_input[4..11] = uint_to_bytes(epoch.uint64)
|
2019-11-10 00:03:41 +00:00
|
|
|
seed_input[12..43] =
|
2019-12-17 16:25:13 +00:00
|
|
|
get_randao_mix(state, # Avoid underflow
|
2019-09-04 13:57:18 +00:00
|
|
|
epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD - 1).data
|
2020-06-16 12:16:43 +00:00
|
|
|
eth2digest(seed_input)
|
2021-04-04 16:24:45 +00:00
|
|
|
|
2022-05-23 19:30:24 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/altair/beacon-chain.md#add_flag
|
2021-04-04 16:24:45 +00:00
|
|
|
func add_flag*(flags: ParticipationFlags, flag_index: int): ParticipationFlags =
|
|
|
|
let flag = ParticipationFlags(1'u8 shl flag_index)
|
|
|
|
flags or flag
|
|
|
|
|
2022-05-23 19:30:24 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/altair/beacon-chain.md#has_flag
|
2021-04-04 16:24:45 +00:00
|
|
|
func has_flag*(flags: ParticipationFlags, flag_index: int): bool =
|
|
|
|
let flag = ParticipationFlags(1'u8 shl flag_index)
|
|
|
|
(flags and flag) == flag
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#is_sync_committee_update
|
|
|
|
template is_sync_committee_update*(update: SomeLightClientUpdate): bool =
|
|
|
|
when update is SomeLightClientUpdateWithSyncCommittee:
|
|
|
|
not isZeroMemory(update.next_sync_committee_branch)
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/altair/sync-protocol.md#get_active_header
|
2022-05-23 12:02:54 +00:00
|
|
|
template is_finality_update*(update: SomeLightClientUpdate): bool =
|
|
|
|
when update is SomeLightClientUpdateWithFinality:
|
|
|
|
not isZeroMemory(update.finality_branch)
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#is_next_sync_committee_known
|
|
|
|
template is_next_sync_committee_known*(store: LightClientStore): bool =
|
|
|
|
not isZeroMemory(store.next_sync_committee)
|
|
|
|
|
2022-05-24 08:26:35 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/altair/sync-protocol.md#get_safety_threshold
|
2022-05-23 12:02:54 +00:00
|
|
|
func get_safety_threshold*(store: LightClientStore): uint64 =
|
|
|
|
max(
|
|
|
|
store.previous_max_active_participants,
|
|
|
|
store.current_max_active_participants
|
|
|
|
) div 2
|
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#is_better_update
|
|
|
|
type LightClientUpdateMetadata* = object
|
|
|
|
attested_slot*, finalized_slot*, signature_slot*: Slot
|
|
|
|
has_sync_committee*, has_finality*: bool
|
|
|
|
num_active_participants*: uint64
|
|
|
|
|
|
|
|
func toMeta*(update: SomeLightClientUpdate): LightClientUpdateMetadata =
|
|
|
|
var meta {.noinit.}: LightClientUpdateMetadata
|
|
|
|
meta.attested_slot =
|
|
|
|
update.attested_header.slot
|
|
|
|
meta.finalized_slot =
|
|
|
|
when update is SomeLightClientUpdateWithFinality:
|
|
|
|
update.finalized_header.slot
|
|
|
|
else:
|
|
|
|
GENESIS_SLOT
|
|
|
|
meta.signature_slot =
|
|
|
|
update.signature_slot
|
|
|
|
meta.has_sync_committee =
|
|
|
|
when update is SomeLightClientUpdateWithSyncCommittee:
|
|
|
|
not update.next_sync_committee_branch.isZeroMemory
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
meta.has_finality =
|
|
|
|
when update is SomeLightClientUpdateWithFinality:
|
|
|
|
not update.finality_branch.isZeroMemory
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
meta.num_active_participants =
|
|
|
|
countOnes(update.sync_aggregate.sync_committee_bits).uint64
|
|
|
|
meta
|
|
|
|
|
|
|
|
func is_better_data*(new_meta, old_meta: LightClientUpdateMetadata): bool =
|
|
|
|
# Compare supermajority (> 2/3) sync committee participation
|
|
|
|
const max_active_participants = SYNC_COMMITTEE_SIZE.uint64
|
|
|
|
let
|
|
|
|
new_has_supermajority =
|
|
|
|
new_meta.num_active_participants * 3 >= max_active_participants * 2
|
|
|
|
old_has_supermajority =
|
|
|
|
old_meta.num_active_participants * 3 >= max_active_participants * 2
|
|
|
|
if new_has_supermajority != old_has_supermajority:
|
|
|
|
return new_has_supermajority > old_has_supermajority
|
|
|
|
if not new_has_supermajority:
|
|
|
|
if new_meta.num_active_participants != old_meta.num_active_participants:
|
|
|
|
return new_meta.num_active_participants > old_meta.num_active_participants
|
|
|
|
|
|
|
|
# Compare presence of relevant sync committee
|
|
|
|
let
|
|
|
|
new_has_relevant_sync_committee = new_meta.has_sync_committee and
|
|
|
|
new_meta.attested_slot.sync_committee_period ==
|
|
|
|
new_meta.signature_slot.sync_committee_period
|
|
|
|
old_has_relevant_sync_committee = old_meta.has_sync_committee and
|
|
|
|
old_meta.attested_slot.sync_committee_period ==
|
|
|
|
old_meta.signature_slot.sync_committee_period
|
|
|
|
if new_has_relevant_sync_committee != old_has_relevant_sync_committee:
|
|
|
|
return new_has_relevant_sync_committee > old_has_relevant_sync_committee
|
|
|
|
|
|
|
|
# Compare indication of any finality
|
|
|
|
if new_meta.has_finality != old_meta.has_finality:
|
|
|
|
return new_meta.has_finality > old_meta.has_finality
|
|
|
|
|
|
|
|
# Compare sync committee finality
|
|
|
|
if new_meta.has_finality:
|
|
|
|
let
|
|
|
|
new_has_sync_committee_finality =
|
|
|
|
new_meta.finalized_slot.sync_committee_period ==
|
|
|
|
new_meta.attested_slot.sync_committee_period
|
|
|
|
old_has_sync_committee_finality =
|
|
|
|
old_meta.finalized_slot.sync_committee_period ==
|
|
|
|
old_meta.attested_slot.sync_committee_period
|
|
|
|
if new_has_sync_committee_finality != old_has_sync_committee_finality:
|
|
|
|
return new_has_sync_committee_finality > old_has_sync_committee_finality
|
|
|
|
|
|
|
|
# Tiebreaker 1: Sync committee participation beyond supermajority
|
|
|
|
if new_meta.num_active_participants != old_meta.num_active_participants:
|
|
|
|
return new_meta.num_active_participants > old_meta.num_active_participants
|
|
|
|
|
|
|
|
# Tiebreaker 2: Prefer older data (fewer changes to best data)
|
|
|
|
new_meta.attested_slot < old_meta.attested_slot
|
|
|
|
|
|
|
|
template is_better_update*[A, B: SomeLightClientUpdate](
|
|
|
|
new_update: A, old_update: B): bool =
|
|
|
|
is_better_data(toMeta(new_update), toMeta(old_update))
|
|
|
|
|
2022-05-23 19:30:24 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/bellatrix/beacon-chain.md#is_merge_transition_complete
|
2022-01-06 11:25:35 +00:00
|
|
|
func is_merge_transition_complete*(state: bellatrix.BeaconState): bool =
|
2022-02-16 07:16:01 +00:00
|
|
|
const defaultExecutionPayloadHeader = default(ExecutionPayloadHeader)
|
|
|
|
state.latest_execution_payload_header != defaultExecutionPayloadHeader
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/sync/optimistic.md#helpers
|
2022-07-04 20:35:33 +00:00
|
|
|
func is_execution_block*(blck: SomeForkyBeaconBlock): bool =
|
|
|
|
when typeof(blck).toFork >= BeaconBlockFork.Bellatrix:
|
|
|
|
const defaultExecutionPayload =
|
|
|
|
default(typeof(blck.body.execution_payload))
|
|
|
|
blck.body.execution_payload != defaultExecutionPayload
|
|
|
|
else:
|
|
|
|
false
|
2022-06-10 14:16:37 +00:00
|
|
|
|
2022-05-23 19:30:24 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/bellatrix/beacon-chain.md#is_merge_transition_block
|
2021-12-17 06:56:33 +00:00
|
|
|
func is_merge_transition_block(
|
2022-01-06 11:25:35 +00:00
|
|
|
state: bellatrix.BeaconState,
|
|
|
|
body: bellatrix.BeaconBlockBody | bellatrix.TrustedBeaconBlockBody |
|
|
|
|
bellatrix.SigVerifiedBeaconBlockBody): bool =
|
2022-02-16 07:16:01 +00:00
|
|
|
const defaultBellatrixExecutionPayload = default(bellatrix.ExecutionPayload)
|
2021-12-14 21:02:29 +00:00
|
|
|
not is_merge_transition_complete(state) and
|
2022-02-16 07:16:01 +00:00
|
|
|
body.execution_payload != defaultBellatrixExecutionPayload
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2022-05-23 19:30:24 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/bellatrix/beacon-chain.md#is_execution_enabled
|
2021-09-27 14:22:58 +00:00
|
|
|
func is_execution_enabled*(
|
2022-01-06 11:25:35 +00:00
|
|
|
state: bellatrix.BeaconState,
|
|
|
|
body: bellatrix.BeaconBlockBody | bellatrix.TrustedBeaconBlockBody |
|
|
|
|
bellatrix.SigVerifiedBeaconBlockBody): bool =
|
2021-12-17 06:56:33 +00:00
|
|
|
is_merge_transition_block(state, body) or is_merge_transition_complete(state)
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2022-05-23 19:30:24 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/bellatrix/beacon-chain.md#compute_timestamp_at_slot
|
2021-11-05 07:34:34 +00:00
|
|
|
func compute_timestamp_at_slot*(state: ForkyBeaconState, slot: Slot): uint64 =
|
2021-09-27 14:22:58 +00:00
|
|
|
# Note: This function is unsafe with respect to overflows and underflows.
|
|
|
|
let slots_since_genesis = slot - GENESIS_SLOT
|
|
|
|
state.genesis_time + slots_since_genesis * SECONDS_PER_SLOT
|
2022-04-14 20:15:34 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.10/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py#L1-L31
|
|
|
|
func build_empty_execution_payload*(state: bellatrix.BeaconState): ExecutionPayload =
|
|
|
|
## Assuming a pre-state of the same slot, build a valid ExecutionPayload
|
|
|
|
## without any transactions.
|
|
|
|
let
|
|
|
|
latest = state.latest_execution_payload_header
|
|
|
|
timestamp = compute_timestamp_at_slot(state, state.slot)
|
|
|
|
randao_mix = get_randao_mix(state, get_current_epoch(state))
|
|
|
|
|
|
|
|
var payload = ExecutionPayload(
|
|
|
|
parent_hash: latest.block_hash,
|
|
|
|
state_root: latest.state_root, # no changes to the state
|
|
|
|
receipts_root: static(Eth2Digest.fromHex(
|
|
|
|
"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
|
|
|
|
block_number: latest.block_number + 1,
|
|
|
|
prev_randao: randao_mix,
|
|
|
|
gas_limit: latest.gas_limit, # retain same limit
|
|
|
|
gas_used: 0, # empty block, 0 gas
|
|
|
|
timestamp: timestamp,
|
|
|
|
base_fee_per_gas: latest.base_fee_per_gas) # retain same base_fee
|
|
|
|
|
|
|
|
payload.block_hash = withEth2Hash:
|
|
|
|
h.update payload.hash_tree_root().data
|
|
|
|
h.update cast[array[13, uint8]]("FAKE RLP HASH")
|
|
|
|
|
|
|
|
payload
|