2022-02-27 16:55:02 +00:00
|
|
|
# beacon_chain
|
2023-01-06 16:28:46 +00:00
|
|
|
# Copyright (c) 2021-2023 Status Research & Development GmbH
|
2022-02-27 16:55:02 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2022-03-02 10:44:42 +00:00
|
|
|
|
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-11-10 17:40:27 +00:00
|
|
|
from ../consensus_object_pools/block_pools_types import VerifierError
|
|
|
|
export block_pools_types.VerifierError
|
2022-03-02 10:44:42 +00:00
|
|
|
|
2023-06-24 15:43:30 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.0/specs/altair/light-client/sync-protocol.md#initialize_light_client_store
|
2022-03-08 12:21:56 +00:00
|
|
|
func initialize_light_client_store*(
|
|
|
|
trusted_block_root: Eth2Digest,
|
2023-01-14 21:19:50 +00:00
|
|
|
bootstrap: ForkyLightClientBootstrap,
|
|
|
|
cfg: RuntimeConfig
|
|
|
|
): auto =
|
|
|
|
type ResultType =
|
|
|
|
Result[typeof(bootstrap).kind.LightClientStore, VerifierError]
|
|
|
|
|
|
|
|
if not is_valid_light_client_header(bootstrap.header, cfg):
|
|
|
|
return ResultType.err(VerifierError.Invalid)
|
|
|
|
if hash_tree_root(bootstrap.header.beacon) != trusted_block_root:
|
|
|
|
return ResultType.err(VerifierError.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),
|
2023-01-13 15:46:35 +00:00
|
|
|
bootstrap.header.beacon.state_root):
|
2023-01-14 21:19:50 +00:00
|
|
|
return ResultType.err(VerifierError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
return ResultType.ok(typeof(bootstrap).kind.LightClientStore(
|
2022-03-08 12:21:56 +00:00
|
|
|
finalized_header: bootstrap.header,
|
|
|
|
current_sync_committee: bootstrap.current_sync_committee,
|
|
|
|
optimistic_header: bootstrap.header))
|
|
|
|
|
2023-06-24 15:43:30 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.0/specs/altair/light-client/sync-protocol.md#validate_light_client_update
|
2022-03-02 10:44:42 +00:00
|
|
|
proc validate_light_client_update*(
|
2023-01-14 21:19:50 +00:00
|
|
|
store: ForkyLightClientStore,
|
|
|
|
update: SomeForkyLightClientUpdate,
|
2022-03-02 10:44:42 +00:00
|
|
|
current_slot: Slot,
|
2022-03-04 16:09:33 +00:00
|
|
|
cfg: RuntimeConfig,
|
2022-11-10 17:40:27 +00:00
|
|
|
genesis_validators_root: Eth2Digest): Result[void, VerifierError] =
|
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-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
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
|
2023-01-14 21:19:50 +00:00
|
|
|
if not is_valid_light_client_header(update.attested_header, cfg):
|
2023-01-13 15:46:35 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithFinality:
|
2023-01-13 15:46:35 +00:00
|
|
|
if update.attested_header.beacon.slot < update.finalized_header.beacon.slot:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2023-01-13 15:46:35 +00:00
|
|
|
if update.signature_slot <= update.attested_header.beacon.slot:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2022-05-23 12:02:54 +00:00
|
|
|
if current_slot < update.signature_slot:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.UnviableFork)
|
2022-05-23 12:02:54 +00:00
|
|
|
let
|
2023-01-13 15:46:35 +00:00
|
|
|
store_period = store.finalized_header.beacon.slot.sync_committee_period
|
2022-05-23 12:02:54 +00:00
|
|
|
signature_period = update.signature_slot.sync_committee_period
|
|
|
|
is_next_sync_committee_known = store.is_next_sync_committee_known
|
2022-03-08 12:21:56 +00:00
|
|
|
if is_next_sync_committee_known:
|
2022-05-23 12:02:54 +00:00
|
|
|
if signature_period notin [store_period, store_period + 1]:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.MissingParent)
|
2022-03-08 12:21:56 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
if signature_period != store_period:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.MissingParent)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-03-08 12:21:56 +00:00
|
|
|
# Verify update is relevant
|
2023-01-13 15:46:35 +00:00
|
|
|
let attested_period = update.attested_header.beacon.slot.sync_committee_period
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
2022-05-23 12:02:54 +00:00
|
|
|
let is_sync_committee_update = update.is_sync_committee_update
|
2023-01-13 15:46:35 +00:00
|
|
|
if update.attested_header.beacon.slot <= store.finalized_header.beacon.slot:
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
2022-05-23 12:02:54 +00:00
|
|
|
if is_next_sync_committee_known:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Duplicate)
|
2022-05-23 12:02:54 +00:00
|
|
|
if attested_period != store_period or not is_sync_committee_update:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Duplicate)
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Duplicate)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
2023-03-11 01:11:51 +00:00
|
|
|
# Verify that the `finality_branch`, if present, confirms `finalized_header`
|
|
|
|
# to match the finalized checkpoint root saved in the state of
|
|
|
|
# `attested_header`. Note that the genesis finalized checkpoint root is
|
|
|
|
# represented as a zero hash.
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithFinality:
|
2022-05-23 12:02:54 +00:00
|
|
|
if not update.is_finality_update:
|
2023-01-14 21:19:50 +00:00
|
|
|
if update.finalized_header != default(typeof(update.finalized_header)):
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
|
|
|
var finalized_root {.noinit.}: Eth2Digest
|
2023-01-13 15:46:35 +00:00
|
|
|
if update.finalized_header.beacon.slot != GENESIS_SLOT:
|
2023-01-14 21:19:50 +00:00
|
|
|
if not is_valid_light_client_header(update.finalized_header, cfg):
|
2023-01-13 15:46:35 +00:00
|
|
|
return err(VerifierError.Invalid)
|
|
|
|
finalized_root = hash_tree_root(update.finalized_header.beacon)
|
2023-01-14 21:19:50 +00:00
|
|
|
elif update.finalized_header == default(typeof(update.finalized_header)):
|
2022-05-23 12:02:54 +00:00
|
|
|
finalized_root.reset()
|
2022-03-08 12:21:56 +00:00
|
|
|
else:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2022-05-23 12:02:54 +00:00
|
|
|
if not is_valid_merkle_branch(
|
|
|
|
finalized_root,
|
|
|
|
update.finality_branch,
|
|
|
|
log2trunc(altair.FINALIZED_ROOT_INDEX),
|
|
|
|
get_subtree_index(altair.FINALIZED_ROOT_INDEX),
|
2023-01-13 15:46:35 +00:00
|
|
|
update.attested_header.beacon.state_root):
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2022-03-08 12:21:56 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# Verify that the `next_sync_committee`, if present, actually is the
|
|
|
|
# next sync committee saved in the state of the `attested_header`
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
2022-05-23 12:02:54 +00:00
|
|
|
if not is_sync_committee_update:
|
2023-01-14 21:19:50 +00:00
|
|
|
if update.next_sync_committee !=
|
|
|
|
default(typeof(update.next_sync_committee)):
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
|
|
|
if attested_period == store_period and is_next_sync_committee_known:
|
|
|
|
if update.next_sync_committee != store.next_sync_committee:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.UnviableFork)
|
2022-05-23 12:02:54 +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),
|
2023-01-13 15:46:35 +00:00
|
|
|
update.attested_header.beacon.state_root):
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Invalid)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
|
|
|
# Verify sync committee aggregate signature
|
2022-03-08 12:21:56 +00:00
|
|
|
let sync_committee =
|
2022-05-23 12:02:54 +00:00
|
|
|
if signature_period == store_period:
|
2022-03-08 12:21:56 +00:00
|
|
|
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:
|
2022-05-30 13:30:42 +00:00
|
|
|
participant_pubkeys.add(sync_committee.pubkeys.data[idx])
|
2022-03-04 16:09:33 +00:00
|
|
|
let
|
2023-03-08 18:59:21 +00:00
|
|
|
fork_version_slot = max(update.signature_slot, 1.Slot) - 1
|
|
|
|
fork_version = cfg.forkVersionAtEpoch(fork_version_slot.epoch)
|
2022-03-04 16:09:33 +00:00
|
|
|
domain = compute_domain(
|
|
|
|
DOMAIN_SYNC_COMMITTEE, fork_version, genesis_validators_root)
|
2023-01-13 15:46:35 +00:00
|
|
|
signing_root = compute_signing_root(update.attested_header.beacon, domain)
|
2022-03-04 16:09:33 +00:00
|
|
|
if not blsFastAggregateVerify(
|
|
|
|
participant_pubkeys, signing_root.data,
|
|
|
|
sync_aggregate.sync_committee_signature):
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.UnviableFork)
|
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
|
|
|
|
2023-06-24 15:43:30 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.0/specs/altair/light-client/sync-protocol.md#apply_light_client_update
|
2022-01-03 13:06:14 +00:00
|
|
|
func apply_light_client_update(
|
2023-01-14 21:19:50 +00:00
|
|
|
store: var ForkyLightClientStore,
|
|
|
|
update: SomeForkyLightClientUpdate): bool =
|
2022-03-14 09:25:54 +00:00
|
|
|
var didProgress = false
|
2021-11-02 20:32:34 +00:00
|
|
|
let
|
2023-01-13 15:46:35 +00:00
|
|
|
store_period = store.finalized_header.beacon.slot.sync_committee_period
|
|
|
|
finalized_period = update.finalized_header.beacon.slot.sync_committee_period
|
2022-05-23 12:02:54 +00:00
|
|
|
if not store.is_next_sync_committee_known:
|
|
|
|
assert finalized_period == store_period
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
2022-05-23 12:02:54 +00:00
|
|
|
store.next_sync_committee = update.next_sync_committee
|
|
|
|
if store.is_next_sync_committee_known:
|
|
|
|
didProgress = true
|
|
|
|
elif finalized_period == store_period + 1:
|
|
|
|
store.current_sync_committee = store.next_sync_committee
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
2022-05-23 12:02:54 +00:00
|
|
|
store.next_sync_committee = update.next_sync_committee
|
|
|
|
else:
|
|
|
|
store.next_sync_committee.reset()
|
2022-03-08 12:21:56 +00:00
|
|
|
store.previous_max_active_participants =
|
|
|
|
store.current_max_active_participants
|
|
|
|
store.current_max_active_participants = 0
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
2023-01-13 15:46:35 +00:00
|
|
|
if update.finalized_header.beacon.slot > store.finalized_header.beacon.slot:
|
2022-05-23 12:02:54 +00:00
|
|
|
store.finalized_header = update.finalized_header
|
2023-01-13 15:46:35 +00:00
|
|
|
if store.finalized_header.beacon.slot > store.optimistic_header.beacon.slot:
|
2022-03-16 11:56:38 +00:00
|
|
|
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
|
|
|
|
2023-06-24 15:43:30 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.0/specs/altair/light-client/sync-protocol.md#process_light_client_store_force_update
|
2022-03-14 09:25:54 +00:00
|
|
|
type
|
2022-05-23 12:02:54 +00:00
|
|
|
ForceUpdateResult* = enum
|
2022-03-14 09:25:54 +00:00
|
|
|
NoUpdate,
|
2022-05-23 12:02:54 +00:00
|
|
|
DidUpdateWithoutSupermajority,
|
|
|
|
DidUpdateWithoutFinality
|
2022-03-14 09:25:54 +00:00
|
|
|
|
2022-07-23 05:54:01 +00:00
|
|
|
func process_light_client_store_force_update*(
|
2023-01-14 21:19:50 +00:00
|
|
|
store: var ForkyLightClientStore,
|
2022-05-23 12:02:54 +00:00
|
|
|
current_slot: Slot): ForceUpdateResult {.discardable.} =
|
2022-03-14 09:25:54 +00:00
|
|
|
var res = NoUpdate
|
2022-03-08 12:21:56 +00:00
|
|
|
if store.best_valid_update.isSome and
|
2023-01-13 15:46:35 +00:00
|
|
|
current_slot > store.finalized_header.beacon.slot + UPDATE_TIMEOUT:
|
2023-03-11 01:11:51 +00:00
|
|
|
# Forced best update when the update timeout has elapsed.
|
2023-01-13 15:46:35 +00:00
|
|
|
# Because the apply logic waits for `finalized_header.beacon.slot`
|
|
|
|
# to indicate sync committee finality, the `attested_header` may be
|
|
|
|
# treated as `finalized_header` in extended periods of non-finality
|
|
|
|
# to guarantee progression into later sync committee periods according
|
|
|
|
# to `is_better_update`.
|
2022-05-23 12:02:54 +00:00
|
|
|
template best(): auto = store.best_valid_update.get
|
2023-01-13 15:46:35 +00:00
|
|
|
if best.finalized_header.beacon.slot <= store.finalized_header.beacon.slot:
|
2022-05-23 12:02:54 +00:00
|
|
|
best.finalized_header = best.attested_header
|
|
|
|
if apply_light_client_update(store, best):
|
|
|
|
template sync_aggregate(): auto = best.sync_aggregate
|
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
2022-03-14 09:25:54 +00:00
|
|
|
if num_active_participants * 3 < static(sync_committee_bits.len * 2):
|
2022-05-23 12:02:54 +00:00
|
|
|
res = DidUpdateWithoutSupermajority
|
2022-03-14 09:25:54 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
res = DidUpdateWithoutFinality
|
|
|
|
store.best_valid_update.reset()
|
2022-03-14 09:25:54 +00:00
|
|
|
res
|
2022-01-03 13:06:14 +00:00
|
|
|
|
2023-06-24 15:43:30 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.0/specs/altair/light-client/sync-protocol.md#process_light_client_update
|
2022-03-02 10:44:42 +00:00
|
|
|
proc process_light_client_update*(
|
2023-01-14 21:19:50 +00:00
|
|
|
store: var ForkyLightClientStore,
|
|
|
|
update: SomeForkyLightClientUpdate,
|
2022-03-02 10:44:42 +00:00
|
|
|
current_slot: Slot,
|
2022-03-04 16:09:33 +00:00
|
|
|
cfg: RuntimeConfig,
|
2022-11-10 17:40:27 +00:00
|
|
|
genesis_validators_root: Eth2Digest): Result[void, VerifierError] =
|
2022-03-14 09:25:54 +00:00
|
|
|
? 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
|
|
|
# Update the best update in case we have to force-update to it
|
|
|
|
# if the timeout elapses
|
2022-05-23 12:02:54 +00:00
|
|
|
if store.best_valid_update.isNone or
|
|
|
|
is_better_update(update, store.best_valid_update.get):
|
2023-01-11 12:29:21 +00:00
|
|
|
store.best_valid_update = Opt.some(update.toFull)
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
2022-01-03 13:06:14 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# Track the maximum number of active participants in the committee signatures
|
|
|
|
template sync_aggregate(): auto = update.sync_aggregate
|
2022-03-08 12:21:56 +00:00
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
2022-05-23 12:02:54 +00:00
|
|
|
if num_active_participants > store.current_max_active_participants:
|
|
|
|
store.current_max_active_participants = num_active_participants
|
2022-03-08 12:21:56 +00:00
|
|
|
|
|
|
|
# Update the optimistic header
|
2022-05-23 12:02:54 +00:00
|
|
|
if num_active_participants > get_safety_threshold(store) and
|
2023-01-13 15:46:35 +00:00
|
|
|
update.attested_header.beacon.slot > store.optimistic_header.beacon.slot:
|
2022-05-23 12:02:54 +00:00
|
|
|
store.optimistic_header = update.attested_header
|
2022-03-14 09:25:54 +00:00
|
|
|
didProgress = true
|
2022-01-03 13:06:14 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# Update finalized header
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithFinality:
|
2022-05-23 12:02:54 +00:00
|
|
|
if num_active_participants * 3 >= static(sync_committee_bits.len * 2):
|
|
|
|
var improvesFinality =
|
2023-01-13 15:46:35 +00:00
|
|
|
update.finalized_header.beacon.slot > store.finalized_header.beacon.slot
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
2022-05-23 12:02:54 +00:00
|
|
|
if not improvesFinality and not store.is_next_sync_committee_known:
|
|
|
|
improvesFinality =
|
|
|
|
update.is_sync_committee_update and update.is_finality_update and
|
2023-01-13 15:46:35 +00:00
|
|
|
update.finalized_header.beacon.slot.sync_committee_period ==
|
|
|
|
update.attested_header.beacon.slot.sync_committee_period
|
2022-05-23 12:02:54 +00:00
|
|
|
if improvesFinality:
|
|
|
|
# Normal update through 2/3 threshold
|
|
|
|
if apply_light_client_update(store, update):
|
|
|
|
didProgress = true
|
|
|
|
store.best_valid_update.reset()
|
|
|
|
|
2022-03-14 09:25:54 +00:00
|
|
|
if not didProgress:
|
2022-11-10 17:40:27 +00:00
|
|
|
return err(VerifierError.Duplicate)
|
2022-05-23 12:02:54 +00:00
|
|
|
ok()
|