consistent style in `light_client_sync.nim` (#3450)
Uses consistent formatting in `light_client_sync.nim`, always refers to fork-dependent light client objects in full qualified notation, moves `get_safety_threshold` helper function to same location as in the spec.
This commit is contained in:
parent
f0ada15dac
commit
33d084192f
|
@ -5,15 +5,15 @@
|
||||||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
# * 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.
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||||
|
|
||||||
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import
|
import
|
||||||
stew/[bitops2, objects],
|
stew/[bitops2, objects],
|
||||||
datatypes/altair,
|
datatypes/altair,
|
||||||
helpers
|
helpers
|
||||||
|
|
||||||
{.push raises: [Defect].}
|
|
||||||
|
|
||||||
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#get_active_header
|
# 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 =
|
func get_active_header(update: altair.LightClientUpdate): BeaconBlockHeader =
|
||||||
# The "active header" is the header that the update is trying to convince
|
# 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
|
# us to accept. If a finalized header is present, it's the finalized
|
||||||
# header, otherwise it's the attested header
|
# header, otherwise it's the attested header
|
||||||
|
@ -22,9 +22,17 @@ func get_active_header(update: LightClientUpdate): BeaconBlockHeader =
|
||||||
else:
|
else:
|
||||||
update.attested_header
|
update.attested_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#validate_light_client_update
|
# 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,
|
proc validate_light_client_update*(
|
||||||
update: LightClientUpdate,
|
store: LightClientStore,
|
||||||
|
update: altair.LightClientUpdate,
|
||||||
current_slot: Slot,
|
current_slot: Slot,
|
||||||
genesis_validators_root: Eth2Digest): bool =
|
genesis_validators_root: Eth2Digest): bool =
|
||||||
# Verify update slot is larger than slot of current best finalized header
|
# Verify update slot is larger than slot of current best finalized header
|
||||||
|
@ -47,10 +55,11 @@ proc validate_light_client_update*(store: LightClientStore,
|
||||||
if not update.finality_branch.isZeroMemory:
|
if not update.finality_branch.isZeroMemory:
|
||||||
return false
|
return false
|
||||||
else:
|
else:
|
||||||
if not is_valid_merkle_branch(hash_tree_root(update.finalized_header),
|
if not is_valid_merkle_branch(
|
||||||
|
hash_tree_root(update.finalized_header),
|
||||||
update.finality_branch,
|
update.finality_branch,
|
||||||
log2trunc(FINALIZED_ROOT_INDEX),
|
log2trunc(altair.FINALIZED_ROOT_INDEX),
|
||||||
get_subtree_index(FINALIZED_ROOT_INDEX),
|
get_subtree_index(altair.FINALIZED_ROOT_INDEX),
|
||||||
update.attested_header.state_root):
|
update.attested_header.state_root):
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
@ -61,10 +70,11 @@ proc validate_light_client_update*(store: LightClientStore,
|
||||||
return false
|
return false
|
||||||
unsafeAddr store.current_sync_committee
|
unsafeAddr store.current_sync_committee
|
||||||
else:
|
else:
|
||||||
if not is_valid_merkle_branch(hash_tree_root(update.next_sync_committee),
|
if not is_valid_merkle_branch(
|
||||||
|
hash_tree_root(update.next_sync_committee),
|
||||||
update.next_sync_committee_branch,
|
update.next_sync_committee_branch,
|
||||||
log2trunc(NEXT_SYNC_COMMITTEE_INDEX),
|
log2trunc(altair.NEXT_SYNC_COMMITTEE_INDEX),
|
||||||
get_subtree_index(NEXT_SYNC_COMMITTEE_INDEX),
|
get_subtree_index(altair.NEXT_SYNC_COMMITTEE_INDEX),
|
||||||
active_header.state_root):
|
active_header.state_root):
|
||||||
return false
|
return false
|
||||||
unsafeAddr store.next_sync_committee
|
unsafeAddr store.next_sync_committee
|
||||||
|
@ -92,7 +102,8 @@ proc validate_light_client_update*(store: LightClientStore,
|
||||||
|
|
||||||
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#apply_light_client_update
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/altair/sync-protocol.md#apply_light_client_update
|
||||||
func apply_light_client_update(
|
func apply_light_client_update(
|
||||||
store: var LightClientStore, update: LightClientUpdate) =
|
store: var LightClientStore,
|
||||||
|
update: altair.LightClientUpdate) =
|
||||||
let
|
let
|
||||||
active_header = get_active_header(update)
|
active_header = get_active_header(update)
|
||||||
finalized_period = sync_committee_period(store.finalized_header.slot)
|
finalized_period = sync_committee_period(store.finalized_header.slot)
|
||||||
|
@ -102,16 +113,10 @@ func apply_light_client_update(
|
||||||
store.next_sync_committee = update.next_sync_committee
|
store.next_sync_committee = update.next_sync_committee
|
||||||
store.finalized_header = active_header
|
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
|
# 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,
|
proc process_light_client_update*(
|
||||||
update: LightClientUpdate,
|
store: var LightClientStore,
|
||||||
|
update: altair.LightClientUpdate,
|
||||||
current_slot: Slot,
|
current_slot: Slot,
|
||||||
genesis_validators_root: Eth2Digest): bool =
|
genesis_validators_root: Eth2Digest): bool =
|
||||||
if not validate_light_client_update(
|
if not validate_light_client_update(
|
||||||
|
@ -145,6 +150,6 @@ proc process_light_client_update*(store: var LightClientStore,
|
||||||
not update.finalized_header.isZeroMemory:
|
not update.finalized_header.isZeroMemory:
|
||||||
# Normal update through 2/3 threshold
|
# Normal update through 2/3 threshold
|
||||||
apply_light_client_update(store, update)
|
apply_light_client_update(store, update)
|
||||||
store.best_valid_update = none(LightClientUpdate)
|
store.best_valid_update = none(altair.LightClientUpdate)
|
||||||
|
|
||||||
true
|
true
|
||||||
|
|
Loading…
Reference in New Issue