From 6d11d627f8b125166947d055d055c439baaa4aea Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Wed, 9 Feb 2022 18:06:10 -0700 Subject: [PATCH] Refactoring: add `is_finality_update` helper --- specs/altair/sync-protocol.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/specs/altair/sync-protocol.md b/specs/altair/sync-protocol.md index 36b634742..f6809eab5 100644 --- a/specs/altair/sync-protocol.md +++ b/specs/altair/sync-protocol.md @@ -16,6 +16,7 @@ - [`LightClientUpdate`](#lightclientupdate) - [`LightClientStore`](#lightclientstore) - [Helper functions](#helper-functions) + - [`is_finality_update`](#is_finality_update) - [`get_subtree_index`](#get_subtree_index) - [`get_active_header`](#get_active_header) - [`get_safety_threshold`](#get_safety_threshold) @@ -95,6 +96,13 @@ class LightClientStore(object): ## Helper functions +### `is_finality_update` + +```python +def is_finality_update(update: LightClientUpdate) -> bool: + return update.finalized_header != BeaconBlockHeader() +``` + ### `get_subtree_index` ```python @@ -109,7 +117,7 @@ def 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 update.finalized_header != BeaconBlockHeader(): + if is_finality_update(update): return update.finalized_header else: return update.attested_header @@ -163,7 +171,7 @@ def validate_light_client_update(store: LightClientStore, # Verify that the `finalized_header`, if present, actually is the finalized header saved in the # state of the `attested header` - if update.finalized_header == BeaconBlockHeader(): + if not is_finality_update(update): assert update.finality_branch == [Bytes32() for _ in range(floorlog2(FINALIZED_ROOT_INDEX))] else: assert is_valid_merkle_branch( @@ -252,7 +260,7 @@ def process_light_client_update(store: LightClientStore, # Update finalized header if ( sum(sync_committee_bits) * 3 >= len(sync_committee_bits) * 2 - and update.finalized_header != BeaconBlockHeader() + and is_finality_update(update) ): # Normal update through 2/3 threshold apply_light_client_update(store, update)