Skip to content

Commit

Permalink
encapsulate LC data variables into single structure (#3777)
Browse files Browse the repository at this point in the history
Combines the LC data configuration options (serve / importMode), the
callbacks (finality / optimistic LC update) as well as the cache storing
light client data, into a new `LightClientDataStore` structure.
Also moves the structure into a light client specific file.
  • Loading branch information
etan-status authored Jun 24, 2022
1 parent c45c017 commit 2e98c77
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 129 deletions.
15 changes: 3 additions & 12 deletions beacon_chain/consensus_object_pools/block_pools_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,6 @@ type

cfg*: RuntimeConfig

lightClientDataServe*: bool
## Whether to make local light client data available or not

lightClientDataImportMode*: LightClientDataImportMode
## Which classes of light client data to import

epochRefs*: array[32, EpochRef]
## Cached information about a particular epoch ending with the given
## block - we limit the number of held EpochRefs to put a cap on
Expand All @@ -209,9 +203,10 @@ type
## called several times.

# -----------------------------------
# Data to enable light clients to stay in sync with the network
# Light client data

lightClientCache*: LightClientCache
lcDataStore*: LightClientDataStore
# Data store to enable light clients to sync with the network

# -----------------------------------
# Callbacks
Expand All @@ -224,10 +219,6 @@ type
## On beacon chain reorganization
onFinHappened*: OnFinalizedCallback
## On finalization callback
onLightClientFinalityUpdate*: OnLightClientFinalityUpdateCallback
## On new `LightClientFinalityUpdate` callback
onLightClientOptimisticUpdate*: OnLightClientOptimisticUpdateCallback
## On new `LightClientOptimisticUpdate` callback

headSyncCommittees*: SyncCommitteeCache
## A cache of the sync committees, as they appear in the head state -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ import
./block_dag

type
OnLightClientFinalityUpdateCallback* =
proc(data: altair.LightClientFinalityUpdate) {.gcsafe, raises: [Defect].}
OnLightClientOptimisticUpdateCallback* =
proc(data: altair.LightClientOptimisticUpdate) {.gcsafe, raises: [Defect].}

LightClientDataImportMode* {.pure.} = enum
## Controls which classes of light client data are imported.
None = "none"
Expand All @@ -34,6 +29,11 @@ type
OnDemand = "on-demand"
## Don't precompute historic data. Slow, may miss validator duties.

OnLightClientFinalityUpdateCallback* =
proc(data: altair.LightClientFinalityUpdate) {.gcsafe, raises: [Defect].}
OnLightClientOptimisticUpdateCallback* =
proc(data: altair.LightClientOptimisticUpdate) {.gcsafe, raises: [Defect].}

CachedLightClientData* = object
## Cached data from historical non-finalized states to improve speed when
## creating future `LightClientUpdate` and `LightClientBootstrap` instances.
Expand All @@ -52,7 +52,7 @@ type
current_sync_committee_branch*:
array[log2trunc(altair.CURRENT_SYNC_COMMITTEE_INDEX), Eth2Digest]

LightClientCache* = object
LightClientDataCache* = object
data*: Table[BlockId, CachedLightClientData]
## Cached data for creating future `LightClientUpdate` instances.
## Key is the block ID of which the post state was used to get the data.
Expand All @@ -79,3 +79,26 @@ type

importTailSlot*: Slot
## The earliest slot for which light client data is imported.

LightClientDataStore* = object
# -----------------------------------
# Light client data

cache*: LightClientDataCache
## Cached data to accelerate serving light client data

# -----------------------------------
# Config

serve*: bool
## Whether to make local light client data available or not
importMode*: LightClientDataImportMode
## Which classes of light client data to import

# -----------------------------------
# Callbacks

onLightClientFinalityUpdate*: OnLightClientFinalityUpdateCallback
## On new `LightClientFinalityUpdate` callback
onLightClientOptimisticUpdate*: OnLightClientOptimisticUpdateCallback
## On new `LightClientOptimisticUpdate` callback
15 changes: 8 additions & 7 deletions beacon_chain/consensus_object_pools/blockchain_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,
vanityLogs = default(VanityLogs)): ChainDAGRef =
cfg.checkForkConsistency()

doAssert updateFlags in [{}, {verifyFinalization}],
doAssert updateFlags - {verifyFinalization, enableTestFeatures} == {},
"Other flags not supported in ChainDAG"

# TODO we require that the db contains both a head and a tail block -
Expand Down Expand Up @@ -725,15 +725,16 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,

vanityLogs: vanityLogs,

lightClientDataServe: lightClientDataServe,
lightClientDataImportMode: lightClientDataImportMode,
lcDataStore: initLightClientDataStore(
serve = lightClientDataServe,
importMode = lightClientDataImportMode,
onLCFinalityUpdateCb = onLCFinalityUpdateCb,
onLCOptimisticUpdateCb = onLCOptimisticUpdateCb),

onBlockAdded: onBlockCb,
onHeadChanged: onHeadCb,
onReorgHappened: onReorgCb,
onFinHappened: onFinCb,
onLightClientFinalityUpdate: onLCFinalityUpdateCb,
onLightClientOptimisticUpdate: onLCOptimisticUpdateCb
onFinHappened: onFinCb
)
loadTick = Moment.now()

Expand Down Expand Up @@ -953,7 +954,7 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,
frontfillDur = frontfillTick - finalizedTick,
keysDur = Moment.now() - frontfillTick

dag.initLightClientCache()
dag.initLightClientDataCache()

dag

Expand Down
Loading

0 comments on commit 2e98c77

Please sign in to comment.