nimbus-eth2/beacon_chain/spec/light_client_sync.nim

114 lines
5.7 KiB
Nim
Raw Normal View History

2021-09-08 16:57:00 +00:00
import
std/sets,
2021-09-13 16:47:39 +00:00
stew/[bitops2, objects],
2021-09-08 16:57:00 +00:00
datatypes/altair,
helpers
2021-09-28 14:01:46 +00:00
# https://github.com/ethereum/consensus-specs/blob/v1.1.0/specs/altair/sync-protocol.md#validate_light_client_update
2021-09-08 16:57:00 +00:00
proc validate_light_client_update*(snapshot: LightClientSnapshot,
update: LightClientUpdate,
genesis_validators_root: Eth2Digest): bool =
# Verify update slot is larger than snapshot slot
if update.header.slot <= snapshot.header.slot:
return false
# Verify update does not skip a sync committee period
var snapshot_period = compute_epoch_at_slot(snapshot.header.slot) div EPOCHS_PER_SYNC_COMMITTEE_PERIOD
var update_period = compute_epoch_at_slot(update.header.slot) div EPOCHS_PER_SYNC_COMMITTEE_PERIOD
if update_period notin [snapshot_period, snapshot_period + 1]:
return false
# Verify update header root is the finalized root of the finality header, if specified
# TODO: Use a view type instead of `unsafeAddr`
2021-09-13 16:47:39 +00:00
let signed_header = if update.finality_header.isZeroMemory:
if not update.finality_branch.isZeroMemory:
2021-09-08 16:57:00 +00:00
return false
unsafeAddr update.header
else:
if not is_valid_merkle_branch(hash_tree_root(update.header),
update.finality_branch,
log2trunc(FINALIZED_ROOT_INDEX),
get_subtree_index(FINALIZED_ROOT_INDEX),
update.finality_header.state_root):
return false
unsafeAddr update.finality_header
# Verify update next sync committee if the update period incremented
# TODO: Use a view type instead of `unsafeAddr`
let sync_committee = if update_period == snapshot_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
unsafeAddr snapshot.current_sync_committee
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),
update.header.state_root):
return false
unsafeAddr snapshot.next_sync_committee
2021-09-13 16:47:39 +00:00
let sync_committee_participants_count = countOnes(update.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
# participant_pubkeys = [pubkey for (bit, pubkey) in zip(update.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)
2021-09-08 16:57:00 +00:00
for idx, bit in update.sync_committee_bits:
if bit:
participant_pubkeys.add(sync_committee.pubkeys[idx])
let domain = compute_domain(DOMAIN_SYNC_COMMITTEE, update.fork_version, genesis_validators_root)
let signing_root = compute_signing_root(signed_header[], domain)
blsFastAggregateVerify(participant_pubkeys, signing_root.data, update.sync_committee_signature)
2021-09-13 16:47:39 +00:00
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.3/specs/altair/sync-protocol.md#apply_light_client_update
2021-09-08 16:57:00 +00:00
proc apply_light_client_update(snapshot: var LightClientSnapshot, update: LightClientUpdate) =
let snapshot_period = compute_epoch_at_slot(snapshot.header.slot) div EPOCHS_PER_SYNC_COMMITTEE_PERIOD
let update_period = compute_epoch_at_slot(update.header.slot) div EPOCHS_PER_SYNC_COMMITTEE_PERIOD
if update_period == snapshot_period + 1:
2021-09-13 16:47:39 +00:00
snapshot.current_sync_committee = snapshot.next_sync_committee
snapshot.next_sync_committee = update.next_sync_committee
2021-09-08 16:57:00 +00:00
snapshot.header = update.header
2021-09-13 16:47:39 +00:00
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.3/specs/altair/sync-protocol.md#process_light_client_update
proc process_light_client_update*(store: var LightClientStore,
update: LightClientUpdate,
current_slot: Slot,
genesis_validators_root: Eth2Digest): bool =
2021-09-08 16:57:00 +00:00
if not validate_light_client_update(store.snapshot, update, genesis_validators_root):
return false
store.valid_updates.incl(update)
var update_timeout = SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD
let sync_committee_participants_count = countOnes(update.sync_committee_bits)
if sync_committee_participants_count * 3 >= update.sync_committee_bits.len * 2 and
2021-09-13 16:47:39 +00:00
not update.finality_header.isZeroMemory:
2021-09-08 16:57:00 +00:00
# Apply update if (1) 2/3 quorum is reached and (2) we have a finality proof.
# Note that (2) means that the current light client design needs finality.
# It may be changed to re-organizable light client design. See the on-going issue eth2.0-specs#2182.
apply_light_client_update(store.snapshot, update)
store.valid_updates.clear()
elif current_slot > store.snapshot.header.slot + update_timeout:
var best_update_participants = 0
2021-09-13 16:47:39 +00:00
# TODO:
# Use a view type to avoid the copying when a new best update
# is discovered.
# Alterantively, we could have a `set.max` operation returning
# the best item as a `lent` value. We would need an `OrderedSet`
# type with support for a custom comparison operation.
2021-09-08 16:57:00 +00:00
var best_update: LightClientUpdate
for update in store.valid_updates:
let update_participants = countOnes(update.sync_committee_bits)
if update_participants > best_update_participants:
best_update = update
best_update_participants = update_participants
# Forced best update when the update timeout has elapsed
apply_light_client_update(store.snapshot, best_update)
store.valid_updates.clear()
2021-09-13 16:47:39 +00:00
true