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-02-01 07:31:53 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#get_active_header
|
2022-01-03 13:06:14 +00:00
|
|
|
func get_active_header(update: LightClientUpdate): BeaconBlockHeader =
|
|
|
|
# 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-01-05 13:41:39 +00:00
|
|
|
if not update.finalized_header.isZeroMemory:
|
2022-01-03 13:06:14 +00:00
|
|
|
update.finalized_header
|
|
|
|
else:
|
|
|
|
update.attested_header
|
|
|
|
|
2022-02-01 07:31:53 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#validate_light_client_update
|
2022-01-03 13:06:14 +00:00
|
|
|
proc validate_light_client_update*(store: LightClientStore,
|
2021-09-08 16:57:00 +00:00
|
|
|
update: LightClientUpdate,
|
2022-01-03 13:06:14 +00:00
|
|
|
current_slot: Slot,
|
2021-09-08 16:57:00 +00:00
|
|
|
genesis_validators_root: Eth2Digest): bool =
|
2022-01-03 13:06:14 +00:00
|
|
|
# Verify update slot is larger than slot of current best finalized header
|
|
|
|
let active_header = get_active_header(update)
|
|
|
|
if not (current_slot >= active_header.slot and
|
|
|
|
active_header.slot > store.finalized_header.slot):
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# Verify update does not skip a sync committee period
|
2021-11-02 20:32:34 +00:00
|
|
|
let
|
2022-01-11 10:01:54 +00:00
|
|
|
finalized_period = sync_committee_period(store.finalized_header.slot)
|
|
|
|
update_period = sync_committee_period(active_header.slot)
|
2022-01-03 13:06:14 +00:00
|
|
|
|
|
|
|
if update_period notin [finalized_period, finalized_period + 1]:
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
|
|
|
|
2022-01-03 13:06:14 +00:00
|
|
|
# Verify that the `finalized_header`, if present, actually is the finalized
|
|
|
|
# header saved in the state of the `attested header`
|
|
|
|
if update.finalized_header.isZeroMemory:
|
2021-09-13 16:47:39 +00:00
|
|
|
if not update.finality_branch.isZeroMemory:
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
|
|
|
else:
|
2022-01-03 13:06:14 +00:00
|
|
|
if not is_valid_merkle_branch(hash_tree_root(update.finalized_header),
|
2021-09-08 16:57:00 +00:00
|
|
|
update.finality_branch,
|
|
|
|
log2trunc(FINALIZED_ROOT_INDEX),
|
|
|
|
get_subtree_index(FINALIZED_ROOT_INDEX),
|
2022-01-03 13:06:14 +00:00
|
|
|
update.attested_header.state_root):
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# Verify update next sync committee if the update period incremented
|
|
|
|
# TODO: Use a view type instead of `unsafeAddr`
|
2022-01-03 13:06:14 +00:00
|
|
|
let sync_committee = if update_period == finalized_period:
|
2021-09-13 16:47:39 +00:00
|
|
|
if not update.next_sync_committee_branch.isZeroMemory:
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
2022-01-03 13:06:14 +00:00
|
|
|
unsafeAddr store.current_sync_committee
|
2021-09-08 16:57:00 +00:00
|
|
|
else:
|
|
|
|
if not is_valid_merkle_branch(hash_tree_root(update.next_sync_committee),
|
|
|
|
update.next_sync_committee_branch,
|
|
|
|
log2trunc(NEXT_SYNC_COMMITTEE_INDEX),
|
|
|
|
get_subtree_index(NEXT_SYNC_COMMITTEE_INDEX),
|
2022-01-03 13:06:14 +00:00
|
|
|
active_header.state_root):
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
2022-01-03 13:06:14 +00:00
|
|
|
unsafeAddr store.next_sync_committee
|
|
|
|
|
2022-02-01 07:31:53 +00:00
|
|
|
template sync_aggregate(): auto = update.sync_aggregate
|
2022-01-03 13:06:14 +00:00
|
|
|
let sync_committee_participants_count = countOnes(sync_aggregate.sync_committee_bits)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
|
|
|
# Verify sync committee has sufficient participants
|
2021-09-13 16:47:39 +00:00
|
|
|
if sync_committee_participants_count < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
# Verify sync committee aggregate signature
|
2022-01-03 13:06:14 +00:00
|
|
|
# participant_pubkeys = [pubkey for (bit, pubkey) in zip(sync_aggregate.sync_committee_bits, sync_committee.pubkeys) if bit]
|
2021-09-13 16:47:39 +00:00
|
|
|
var participant_pubkeys = newSeqOfCap[ValidatorPubKey](sync_committee_participants_count)
|
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-01-03 13:06:14 +00:00
|
|
|
let domain = compute_domain(
|
|
|
|
DOMAIN_SYNC_COMMITTEE, update.fork_version, genesis_validators_root)
|
|
|
|
let signing_root = compute_signing_root(update.attested_header, domain)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-01-03 13:06:14 +00:00
|
|
|
blsFastAggregateVerify(
|
|
|
|
participant_pubkeys, signing_root.data, sync_aggregate.sync_committee_signature)
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2022-02-01 07:31:53 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#apply_light_client_update
|
2022-01-03 13:06:14 +00:00
|
|
|
func apply_light_client_update(
|
|
|
|
store: var LightClientStore, update: LightClientUpdate) =
|
2021-11-02 20:32:34 +00:00
|
|
|
let
|
2022-01-03 13:06:14 +00:00
|
|
|
active_header = get_active_header(update)
|
2022-01-11 10:01:54 +00:00
|
|
|
finalized_period = sync_committee_period(store.finalized_header.slot)
|
|
|
|
update_period = sync_committee_period(active_header.slot)
|
2022-01-03 13:06:14 +00:00
|
|
|
if update_period == finalized_period + 1:
|
|
|
|
store.current_sync_committee = store.next_sync_committee
|
|
|
|
store.next_sync_committee = update.next_sync_committee
|
|
|
|
store.finalized_header = active_header
|
|
|
|
|
2022-02-01 07:31:53 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#get_safety_threshold
|
2022-01-03 13:06:14 +00:00
|
|
|
func get_safety_threshold(store: LightClientStore): uint64 =
|
|
|
|
max(
|
|
|
|
store.previous_max_active_participants,
|
|
|
|
store.current_max_active_participants
|
|
|
|
) div 2
|
|
|
|
|
2022-02-01 07:31:53 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#process_light_client_update
|
2021-09-13 16:35:13 +00:00
|
|
|
proc process_light_client_update*(store: var LightClientStore,
|
|
|
|
update: LightClientUpdate,
|
|
|
|
current_slot: Slot,
|
|
|
|
genesis_validators_root: Eth2Digest): bool =
|
2022-01-03 13:06:14 +00:00
|
|
|
if not validate_light_client_update(
|
|
|
|
store, update, current_slot, genesis_validators_root):
|
2021-09-08 16:57:00 +00:00
|
|
|
return false
|
2022-01-03 13:06:14 +00:00
|
|
|
|
|
|
|
let
|
2022-02-01 07:31:53 +00:00
|
|
|
sync_committee_bits = update.sync_aggregate.sync_committee_bits
|
2022-01-03 13:06:14 +00:00
|
|
|
sum_sync_committee_bits = countOnes(sync_committee_bits)
|
|
|
|
|
|
|
|
# Update the best update in case we have to force-update to it if the
|
|
|
|
# timeout elapses
|
|
|
|
if store.best_valid_update.isNone or
|
|
|
|
sum_sync_committee_bits > countOnes(
|
2022-02-01 07:31:53 +00:00
|
|
|
store.best_valid_update.get.sync_aggregate.sync_committee_bits):
|
2022-01-03 13:06:14 +00:00
|
|
|
store.best_valid_update = some(update)
|
|
|
|
|
|
|
|
# Track the maximum number of active participants in the committee signatures
|
|
|
|
store.current_max_active_participants = max(
|
|
|
|
store.current_max_active_participants,
|
|
|
|
sum_sync_committee_bits.uint64,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Update the optimistic header
|
|
|
|
if sum_sync_committee_bits.uint64 > get_safety_threshold(store) and
|
|
|
|
update.attested_header.slot > store.optimistic_header.slot:
|
|
|
|
store.optimistic_header = update.attested_header
|
|
|
|
|
|
|
|
# Update finalized header
|
|
|
|
if sum_sync_committee_bits * 3 >= len(sync_committee_bits) * 2 and
|
2022-01-05 13:41:39 +00:00
|
|
|
not update.finalized_header.isZeroMemory:
|
2022-01-03 13:06:14 +00:00
|
|
|
# Normal update through 2/3 threshold
|
|
|
|
apply_light_client_update(store, update)
|
|
|
|
store.best_valid_update = none(LightClientUpdate)
|
|
|
|
|
2021-09-13 16:47:39 +00:00
|
|
|
true
|