2022-02-27 16:55:02 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2022-03-02 10:44:42 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-03-08 12:21:56 +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
|
|
|
|
|
2021-09-08 16:57:00 +00:00
|
|
|
import
|
2021-09-13 16:47:39 +00:00
|
|
|
stew/[bitops2, objects],
|
2021-09-08 16:57:00 +00:00
|
|
|
datatypes/altair,
|
|
|
|
helpers
|
|
|
|
|
2022-03-14 09:25:54 +00:00
|
|
|
from ../consensus_object_pools/block_pools_types import BlockError
|
|
|
|
|
2022-03-04 16:09:33 +00:00
|
|
|
func period_contains_fork_version(
|
|
|
|
cfg: RuntimeConfig,
|
|
|
|
period: SyncCommitteePeriod,
|
|
|
|
fork_version: Version): bool =
|
|
|
|
## Determine whether a given `fork_version` is used during a given `period`.
|
|
|
|
let
|
|
|
|
periodStartEpoch = period.start_epoch
|
|
|
|
periodEndEpoch = periodStartEpoch + EPOCHS_PER_SYNC_COMMITTEE_PERIOD - 1
|
|
|
|
return
|
|
|
|
if fork_version == cfg.SHARDING_FORK_VERSION:
|
|
|
|
periodEndEpoch >= cfg.SHARDING_FORK_EPOCH
|
|
|
|
elif fork_version == cfg.BELLATRIX_FORK_VERSION:
|
|
|
|
periodStartEpoch < cfg.SHARDING_FORK_EPOCH and
|
|
|
|
cfg.SHARDING_FORK_EPOCH != cfg.BELLATRIX_FORK_EPOCH and
|
|
|
|
periodEndEpoch >= cfg.BELLATRIX_FORK_EPOCH
|
|
|
|
elif fork_version == cfg.ALTAIR_FORK_VERSION:
|
|
|
|
periodStartEpoch < cfg.BELLATRIX_FORK_EPOCH and
|
|
|
|
cfg.BELLATRIX_FORK_EPOCH != cfg.ALTAIR_FORK_EPOCH and
|
|
|
|
periodEndEpoch >= cfg.ALTAIR_FORK_EPOCH
|
|
|
|
elif fork_version == cfg.GENESIS_FORK_VERSION:
|
|
|
|
# Light client sync protocol requires Altair
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
# Unviable fork
|
|
|
|
false
|
|
|
|
|
2022-03-03 13:03:08 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/altair/sync-protocol.md#get_active_header
|
2022-03-17 22:26:56 +00:00
|
|
|
func is_finality_update*(update: altair.LightClientUpdate): bool =
|
2022-03-03 13:03:08 +00:00
|
|
|
not update.finalized_header.isZeroMemory
|
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/altair/sync-protocol.md#get_active_header
|
2022-03-17 22:26:56 +00:00
|
|
|
func get_active_header*(update: altair.LightClientUpdate): BeaconBlockHeader =
|
2022-01-03 13:06:14 +00:00
|
|
|
# The "active header" is the header that the update is trying to convince
|
|
|
|
# us to accept. If a finalized header is present, it's the finalized
|
|
|
|
# header, otherwise it's the attested header
|
2022-03-03 13:03:08 +00:00
|
|
|
if update.is_finality_update:
|
2022-01-03 13:06:14 +00:00
|
|
|
update.finalized_header
|
|
|
|
else:
|
|
|
|
update.attested_header
|
|
|
|
|
2022-03-03 13:03:08 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/altair/sync-protocol.md#get_safety_threshold
|
2022-03-02 10:44:42 +00:00
|
|
|
func get_safety_threshold(store: LightClientStore): uint64 =
|
|
|
|
max(
|
|
|
|
store.previous_max_active_participants,
|
|
|
|
store.current_max_active_participants
|
|
|
|
) div 2
|
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#initialize_light_client_store
|
|
|
|
func initialize_light_client_store*(
|
|
|
|
trusted_block_root: Eth2Digest,
|
|
|
|
bootstrap: altair.LightClientBootstrap
|
2022-03-14 09:25:54 +00:00
|
|
|
): Result[LightClientStore, BlockError] =
|
2022-03-08 12:21:56 +00:00
|
|
|
if hash_tree_root(bootstrap.header) != trusted_block_root:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
if not is_valid_merkle_branch(
|
|
|
|
hash_tree_root(bootstrap.current_sync_committee),
|
|
|
|
bootstrap.current_sync_committee_branch,
|
|
|
|
log2trunc(altair.CURRENT_SYNC_COMMITTEE_INDEX),
|
|
|
|
get_subtree_index(altair.CURRENT_SYNC_COMMITTEE_INDEX),
|
|
|
|
bootstrap.header.state_root):
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
return ok(LightClientStore(
|
|
|
|
finalized_header: bootstrap.header,
|
|
|
|
current_sync_committee: bootstrap.current_sync_committee,
|
|
|
|
optimistic_header: bootstrap.header))
|
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#validate_light_client_update
|
2022-03-02 10:44:42 +00:00
|
|
|
proc validate_light_client_update*(
|
|
|
|
store: LightClientStore,
|
|
|
|
update: altair.LightClientUpdate,
|
|
|
|
current_slot: Slot,
|
2022-03-04 16:09:33 +00:00
|
|
|
cfg: RuntimeConfig,
|
2022-03-14 09:25:54 +00:00
|
|
|
genesis_validators_root: Eth2Digest): Result[void, BlockError] =
|
2022-03-08 12:21:56 +00:00
|
|
|
# Verify sync committee has sufficient participants
|
|
|
|
template sync_aggregate(): auto = update.sync_aggregate
|
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
|
|
|
if num_active_participants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# Determine update header
|
|
|
|
template attested_header(): auto = update.attested_header
|
|
|
|
if current_slot < attested_header.slot:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.UnviableFork)
|
2022-01-03 13:06:14 +00:00
|
|
|
let active_header = get_active_header(update)
|
2022-03-08 12:21:56 +00:00
|
|
|
if attested_header.slot < active_header.slot:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# Verify update is relevant
|
|
|
|
let is_next_sync_committee_known = not store.next_sync_committee.isZeroMemory
|
|
|
|
if is_next_sync_committee_known:
|
|
|
|
if active_header.slot < store.finalized_header.slot:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Duplicate)
|
2022-03-08 12:21:56 +00:00
|
|
|
if active_header.slot == store.finalized_header.slot:
|
|
|
|
if attested_header.slot <= store.optimistic_header.slot:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Duplicate)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
2021-09-08 16:57:00 +00:00
|
|
|
# Verify update does not skip a sync committee period
|
2021-11-02 20:32:34 +00:00
|
|
|
let
|
2022-03-08 12:21:56 +00:00
|
|
|
finalized_period = store.finalized_header.slot.sync_committee_period
|
|
|
|
update_period = active_header.slot.sync_committee_period
|
2022-03-14 09:25:54 +00:00
|
|
|
if update_period < finalized_period:
|
|
|
|
return err(BlockError.Duplicate)
|
|
|
|
if update_period > finalized_period + 1:
|
|
|
|
return err(BlockError.MissingParent)
|
2022-03-08 12:21:56 +00:00
|
|
|
let
|
|
|
|
is_signed_by_next_sync_committee =
|
|
|
|
update.next_sync_committee.isZeroMemory
|
|
|
|
signature_period =
|
|
|
|
if is_signed_by_next_sync_committee:
|
|
|
|
update_period + 1
|
|
|
|
else:
|
|
|
|
update_period
|
|
|
|
current_period = current_slot.sync_committee_period
|
|
|
|
if current_period < signature_period:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.UnviableFork)
|
2022-03-08 12:21:56 +00:00
|
|
|
if is_next_sync_committee_known:
|
|
|
|
if signature_period notin [finalized_period, finalized_period + 1]:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.MissingParent)
|
2022-03-08 12:21:56 +00:00
|
|
|
else:
|
|
|
|
if signature_period != finalized_period:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.MissingParent)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-03-04 16:09:33 +00:00
|
|
|
# Verify fork version is acceptable
|
|
|
|
let fork_version = update.fork_version
|
2022-03-08 12:21:56 +00:00
|
|
|
if not cfg.period_contains_fork_version(signature_period, fork_version):
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.UnviableFork)
|
2022-03-04 16:09:33 +00:00
|
|
|
|
2022-01-03 13:06:14 +00:00
|
|
|
# Verify that the `finalized_header`, if present, actually is the finalized
|
2022-03-08 12:21:56 +00:00
|
|
|
# header saved in the state of the `attested_header`
|
2022-03-03 13:03:08 +00:00
|
|
|
if not update.is_finality_update:
|
2021-09-13 16:47:39 +00:00
|
|
|
if not update.finality_branch.isZeroMemory:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2021-09-08 16:57:00 +00:00
|
|
|
else:
|
2022-03-02 10:44:42 +00:00
|
|
|
if not is_valid_merkle_branch(
|
|
|
|
hash_tree_root(update.finalized_header),
|
|
|
|
update.finality_branch,
|
|
|
|
log2trunc(altair.FINALIZED_ROOT_INDEX),
|
|
|
|
get_subtree_index(altair.FINALIZED_ROOT_INDEX),
|
|
|
|
update.attested_header.state_root):
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# Verify that the `next_sync_committee`, if present, actually is the
|
|
|
|
# next sync committee saved in the state of the `active_header`
|
|
|
|
if is_signed_by_next_sync_committee:
|
2021-09-13 16:47:39 +00:00
|
|
|
if not update.next_sync_committee_branch.isZeroMemory:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2021-09-08 16:57:00 +00:00
|
|
|
else:
|
2022-03-08 12:21:56 +00:00
|
|
|
if update_period == finalized_period and is_next_sync_committee_known:
|
|
|
|
if update.next_sync_committee != store.next_sync_committee:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.UnviableFork)
|
2022-03-02 10:44:42 +00:00
|
|
|
if not is_valid_merkle_branch(
|
|
|
|
hash_tree_root(update.next_sync_committee),
|
|
|
|
update.next_sync_committee_branch,
|
|
|
|
log2trunc(altair.NEXT_SYNC_COMMITTEE_INDEX),
|
|
|
|
get_subtree_index(altair.NEXT_SYNC_COMMITTEE_INDEX),
|
|
|
|
active_header.state_root):
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-01-03 13:06:14 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# Verify sync committee aggregate signature
|
|
|
|
let sync_committee =
|
|
|
|
if signature_period == finalized_period:
|
|
|
|
unsafeAddr store.current_sync_committee
|
|
|
|
else:
|
|
|
|
unsafeAddr store.next_sync_committee
|
|
|
|
var participant_pubkeys =
|
|
|
|
newSeqOfCap[ValidatorPubKey](num_active_participants)
|
|
|
|
for idx, bit in sync_aggregate.sync_committee_bits:
|
|
|
|
if bit:
|
|
|
|
participant_pubkeys.add(sync_committee.pubkeys[idx])
|
|
|
|
let
|
|
|
|
domain = compute_domain(
|
|
|
|
DOMAIN_SYNC_COMMITTEE, fork_version, genesis_validators_root)
|
|
|
|
signing_root = compute_signing_root(attested_header, domain)
|
|
|
|
if not blsFastAggregateVerify(
|
|
|
|
participant_pubkeys, signing_root.data,
|
|
|
|
sync_aggregate.sync_committee_signature):
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
2022-03-14 09:25:54 +00:00
|
|
|
ok()
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#validate_optimistic_light_client_update
|
|
|
|
proc validate_optimistic_light_client_update*(
|
|
|
|
store: LightClientStore,
|
|
|
|
optimistic_update: OptimisticLightClientUpdate,
|
|
|
|
current_slot: Slot,
|
|
|
|
cfg: RuntimeConfig,
|
2022-03-14 09:25:54 +00:00
|
|
|
genesis_validators_root: Eth2Digest): Result[void, BlockError] =
|
2021-09-08 16:57:00 +00:00
|
|
|
# Verify sync committee has sufficient participants
|
2022-03-08 12:21:56 +00:00
|
|
|
template sync_aggregate(): auto = optimistic_update.sync_aggregate
|
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
|
|
|
if num_active_participants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# Determine update header
|
|
|
|
template attested_header(): auto = optimistic_update.attested_header
|
|
|
|
if current_slot < attested_header.slot:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
template active_header(): auto = attested_header
|
|
|
|
|
|
|
|
# Verify update is relevant
|
|
|
|
if attested_header.slot <= store.optimistic_header.slot:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Duplicate)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# Verify update does not skip a sync committee period
|
|
|
|
let
|
|
|
|
finalized_period = store.finalized_header.slot.sync_committee_period
|
|
|
|
update_period = active_header.slot.sync_committee_period
|
2022-03-14 09:25:54 +00:00
|
|
|
if update_period < finalized_period:
|
|
|
|
return err(BlockError.Duplicate)
|
|
|
|
if update_period > finalized_period + 1:
|
|
|
|
return err(BlockError.MissingParent)
|
2022-03-08 12:21:56 +00:00
|
|
|
let
|
|
|
|
is_signed_by_next_sync_committee =
|
|
|
|
optimistic_update.is_signed_by_next_sync_committee
|
|
|
|
signature_period =
|
|
|
|
if is_signed_by_next_sync_committee:
|
|
|
|
update_period + 1
|
|
|
|
else:
|
|
|
|
update_period
|
|
|
|
current_period = current_slot.sync_committee_period
|
|
|
|
if current_period < signature_period:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
let is_next_sync_committee_known = not store.next_sync_committee.isZeroMemory
|
|
|
|
if is_next_sync_committee_known:
|
|
|
|
if signature_period notin [finalized_period, finalized_period + 1]:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.MissingParent)
|
2022-03-08 12:21:56 +00:00
|
|
|
else:
|
|
|
|
if signature_period != finalized_period:
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.MissingParent)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# Verify fork version is acceptable
|
|
|
|
let fork_version = optimistic_update.fork_version
|
|
|
|
if not cfg.period_contains_fork_version(signature_period, fork_version):
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.UnviableFork)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
|
|
|
# Verify sync committee aggregate signature
|
2022-03-08 12:21:56 +00:00
|
|
|
let sync_committee =
|
|
|
|
if signature_period == finalized_period:
|
|
|
|
unsafeAddr store.current_sync_committee
|
|
|
|
else:
|
|
|
|
unsafeAddr store.next_sync_committee
|
|
|
|
var participant_pubkeys =
|
|
|
|
newSeqOfCap[ValidatorPubKey](num_active_participants)
|
2022-01-03 13:06:14 +00:00
|
|
|
for idx, bit in sync_aggregate.sync_committee_bits:
|
2021-09-08 16:57:00 +00:00
|
|
|
if bit:
|
|
|
|
participant_pubkeys.add(sync_committee.pubkeys[idx])
|
2022-03-04 16:09:33 +00:00
|
|
|
let
|
|
|
|
domain = compute_domain(
|
|
|
|
DOMAIN_SYNC_COMMITTEE, fork_version, genesis_validators_root)
|
2022-03-08 12:21:56 +00:00
|
|
|
signing_root = compute_signing_root(attested_header, domain)
|
2022-03-04 16:09:33 +00:00
|
|
|
if not blsFastAggregateVerify(
|
|
|
|
participant_pubkeys, signing_root.data,
|
|
|
|
sync_aggregate.sync_committee_signature):
|
2022-03-14 09:25:54 +00:00
|
|
|
return err(BlockError.Invalid)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-03-14 09:25:54 +00:00
|
|
|
ok()
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#apply_light_client_update
|
2022-01-03 13:06:14 +00:00
|
|
|
func apply_light_client_update(
|
2022-03-02 10:44:42 +00:00
|
|
|
store: var LightClientStore,
|
2022-03-14 09:25:54 +00:00
|
|
|
update: altair.LightClientUpdate): bool =
|
|
|
|
var didProgress = false
|
2021-11-02 20:32:34 +00:00
|
|
|
let
|
2022-01-03 13:06:14 +00:00
|
|
|
active_header = get_active_header(update)
|
2022-03-08 12:21:56 +00:00
|
|
|
finalized_period = store.finalized_header.slot.sync_committee_period
|
|
|
|
update_period = active_header.slot.sync_committee_period
|
|
|
|
if store.next_sync_committee.isZeroMemory:
|
|
|
|
assert update_period == finalized_period
|
|
|
|
store.next_sync_committee = update.next_sync_committee
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
2022-03-08 12:21:56 +00:00
|
|
|
elif update_period == finalized_period + 1:
|
|
|
|
store.previous_max_active_participants =
|
|
|
|
store.current_max_active_participants
|
|
|
|
store.current_max_active_participants = 0
|
2022-01-03 13:06:14 +00:00
|
|
|
store.current_sync_committee = store.next_sync_committee
|
|
|
|
store.next_sync_committee = update.next_sync_committee
|
2022-03-08 12:21:56 +00:00
|
|
|
assert not store.next_sync_committee.isZeroMemory
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
2022-03-08 12:21:56 +00:00
|
|
|
if active_header.slot > store.finalized_header.slot:
|
|
|
|
store.finalized_header = active_header
|
2022-03-16 11:56:38 +00:00
|
|
|
if store.finalized_header.slot > store.optimistic_header.slot:
|
|
|
|
store.optimistic_header = store.finalized_header
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
|
|
|
didProgress
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#apply_optimistic_light_client_header
|
|
|
|
func apply_optimistic_light_client_header(
|
|
|
|
store: var LightClientStore,
|
|
|
|
attested_header: BeaconBlockHeader,
|
2022-03-14 09:25:54 +00:00
|
|
|
num_active_participants: uint64): bool =
|
|
|
|
var didProgress = false
|
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
if store.current_max_active_participants < num_active_participants:
|
|
|
|
store.current_max_active_participants = num_active_participants
|
|
|
|
|
|
|
|
if num_active_participants > get_safety_threshold(store) and
|
|
|
|
attested_header.slot > store.optimistic_header.slot:
|
|
|
|
store.optimistic_header = attested_header
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
|
|
|
|
|
|
|
didProgress
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/altair/sync-protocol.md#process_slot_for_light_client_store
|
2022-03-14 09:25:54 +00:00
|
|
|
type
|
|
|
|
ProcessSlotForLightClientStoreResult* = enum
|
|
|
|
NoUpdate,
|
|
|
|
UpdatedWithoutSupermajority,
|
|
|
|
UpdatedWithoutFinalityProof
|
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
func process_slot_for_light_client_store*(
|
|
|
|
store: var LightClientStore,
|
2022-03-14 09:25:54 +00:00
|
|
|
current_slot: Slot): ProcessSlotForLightClientStoreResult {.discardable.} =
|
|
|
|
var res = NoUpdate
|
2022-03-08 12:21:56 +00:00
|
|
|
if store.best_valid_update.isSome and
|
|
|
|
current_slot > store.finalized_header.slot + UPDATE_TIMEOUT:
|
2022-03-14 09:25:54 +00:00
|
|
|
template sync_aggregate(): auto = store.best_valid_update.get.sync_aggregate
|
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
|
|
|
if apply_light_client_update(store, store.best_valid_update.get):
|
|
|
|
if num_active_participants * 3 < static(sync_committee_bits.len * 2):
|
|
|
|
res = UpdatedWithoutSupermajority
|
|
|
|
else:
|
|
|
|
res = UpdatedWithoutFinalityProof
|
2022-03-08 12:21:56 +00:00
|
|
|
store.best_valid_update = none(altair.LightClientUpdate)
|
2022-03-14 09:25:54 +00:00
|
|
|
res
|
2022-01-03 13:06:14 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#process_light_client_update
|
2022-03-02 10:44:42 +00:00
|
|
|
proc process_light_client_update*(
|
|
|
|
store: var LightClientStore,
|
|
|
|
update: altair.LightClientUpdate,
|
|
|
|
current_slot: Slot,
|
2022-03-04 16:09:33 +00:00
|
|
|
cfg: RuntimeConfig,
|
2022-03-08 12:21:56 +00:00
|
|
|
genesis_validators_root: Eth2Digest,
|
2022-03-14 09:25:54 +00:00
|
|
|
allowForceUpdate = true): Result[void, BlockError] =
|
|
|
|
? validate_light_client_update(
|
|
|
|
store, update, current_slot, cfg, genesis_validators_root)
|
|
|
|
|
|
|
|
var didProgress = false
|
2022-01-03 13:06:14 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
template sync_aggregate(): auto = update.sync_aggregate
|
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
2022-01-03 13:06:14 +00:00
|
|
|
|
|
|
|
# Update the optimistic header
|
2022-03-14 09:25:54 +00:00
|
|
|
if apply_optimistic_light_client_header(
|
|
|
|
store, update.attested_header, num_active_participants):
|
|
|
|
didProgress = true
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# Update the best update in case we have to force-update to it
|
|
|
|
# if the timeout elapses
|
|
|
|
let best_active_participants =
|
|
|
|
if store.best_valid_update.isNone:
|
|
|
|
0.uint64
|
|
|
|
else:
|
|
|
|
template best_sync_aggregate(): auto =
|
|
|
|
store.best_valid_update.get.sync_aggregate
|
|
|
|
countOnes(best_sync_aggregate.sync_committee_bits).uint64
|
|
|
|
if num_active_participants > best_active_participants:
|
|
|
|
store.best_valid_update = some(update)
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
2022-01-03 13:06:14 +00:00
|
|
|
|
|
|
|
# Update finalized header
|
2022-03-08 12:21:56 +00:00
|
|
|
if num_active_participants * 3 >= static(sync_committee_bits.len * 2) and
|
2022-03-03 13:03:08 +00:00
|
|
|
update.is_finality_update:
|
2022-01-03 13:06:14 +00:00
|
|
|
# Normal update through 2/3 threshold
|
2022-03-14 09:25:54 +00:00
|
|
|
if apply_light_client_update(store, update):
|
|
|
|
didProgress = true
|
2022-03-02 10:44:42 +00:00
|
|
|
store.best_valid_update = none(altair.LightClientUpdate)
|
2022-03-08 12:21:56 +00:00
|
|
|
else:
|
|
|
|
if allowForceUpdate:
|
|
|
|
# Force-update to best update if the timeout elapsed
|
2022-03-14 09:25:54 +00:00
|
|
|
case process_slot_for_light_client_store(store, current_slot)
|
|
|
|
of UpdatedWithoutSupermajority, UpdatedWithoutFinalityProof:
|
|
|
|
didProgress = true
|
|
|
|
of NoUpdate: discard
|
2022-03-08 12:21:56 +00:00
|
|
|
|
2022-03-14 09:25:54 +00:00
|
|
|
if not didProgress:
|
|
|
|
err(BlockError.Duplicate)
|
|
|
|
else:
|
|
|
|
ok()
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/vFuture/specs/altair/sync-protocol.md#process_light_client_update
|
|
|
|
proc process_optimistic_light_client_update*(
|
|
|
|
store: var LightClientStore,
|
|
|
|
optimistic_update: OptimisticLightClientUpdate,
|
|
|
|
current_slot: Slot,
|
|
|
|
cfg: RuntimeConfig,
|
2022-03-14 09:25:54 +00:00
|
|
|
genesis_validators_root: Eth2Digest): Result[void, BlockError] =
|
|
|
|
? validate_optimistic_light_client_update(
|
|
|
|
store, optimistic_update, current_slot, cfg, genesis_validators_root)
|
|
|
|
|
|
|
|
var didProgress = false
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
template sync_aggregate(): auto = optimistic_update.sync_aggregate
|
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
|
|
|
|
|
|
|
# Update the optimistic header
|
2022-03-14 09:25:54 +00:00
|
|
|
if apply_optimistic_light_client_header(
|
|
|
|
store, optimistic_update.attested_header, num_active_participants):
|
|
|
|
didProgress = true
|
2022-01-03 13:06:14 +00:00
|
|
|
|
2022-03-14 09:25:54 +00:00
|
|
|
if not didProgress:
|
|
|
|
err(BlockError.Duplicate)
|
|
|
|
else:
|
|
|
|
ok()
|