nimbus-eth2/beacon_chain/spec/light_client_sync.nim

142 lines
6.2 KiB
Nim
Raw Permalink Normal View History

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
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#get_active_header
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
if not update.finalized_header.isZeroMemory:
update.finalized_header
else:
update.attested_header
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#validate_light_client_update
proc validate_light_client_update*(store: LightClientStore,
2021-09-08 16:57:00 +00:00
update: LightClientUpdate,
current_slot: Slot,
2021-09-08 16:57:00 +00:00
genesis_validators_root: Eth2Digest): bool =
# 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
let
finalized_period = sync_committee_period(store.finalized_header.slot)
update_period = sync_committee_period(active_header.slot)
if update_period notin [finalized_period, finalized_period + 1]:
2021-09-08 16:57:00 +00:00
return false
# 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:
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),
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`
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
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),
active_header.state_root):
2021-09-08 16:57:00 +00:00
return false
unsafeAddr store.next_sync_committee
template sync_aggregate(): auto = update.sync_aggregate
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
# 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)
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])
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
blsFastAggregateVerify(
participant_pubkeys, signing_root.data, sync_aggregate.sync_committee_signature)
2021-09-08 16:57:00 +00:00
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#apply_light_client_update
func apply_light_client_update(
store: var LightClientStore, update: LightClientUpdate) =
let
active_header = get_active_header(update)
finalized_period = sync_committee_period(store.finalized_header.slot)
update_period = sync_committee_period(active_header.slot)
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
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#get_safety_threshold
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/v1.1.9/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 =
if not validate_light_client_update(
store, update, current_slot, genesis_validators_root):
2021-09-08 16:57:00 +00:00
return false
let
sync_committee_bits = update.sync_aggregate.sync_committee_bits
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(
store.best_valid_update.get.sync_aggregate.sync_committee_bits):
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
not update.finalized_header.isZeroMemory:
# 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