Skip to content

Commit

Permalink
Fix and document some usages of defaultRuntimeConfig
Browse files Browse the repository at this point in the history
Other changes:

* Make the light client store compatible with phase0-only networks
  and simulations
  • Loading branch information
zah committed Oct 10, 2022
1 parent bf0ed00 commit 9d754d3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,14 @@ proc cacheLightClientData(
if dag.lcDataStore.cache.data.hasKeyOrPut(bid, cachedData):
doAssert false, "Redundant `cacheLightClientData` call"

func shouldImportLcData(dag: ChainDAGref): bool =
dag.lcDataStore.importMode != LightClientDataImportMode.None and
dag.cfg.ALTAIR_FORK_EPOCH != FAR_FUTURE_EPOCH

proc deleteLightClientData*(dag: ChainDAGRef, bid: BlockId) =
## Delete cached light client data for a given block. This needs to be called
## when a block becomes unreachable due to finalization of a different fork.
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
if not dag.shouldImportLcData:
return

dag.lcDataStore.cache.data.del bid
Expand Down Expand Up @@ -598,7 +602,7 @@ proc createLightClientUpdates(

proc initLightClientDataCache*(dag: ChainDAGRef) =
## Initialize cached light client data
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
if not dag.shouldImportLcData:
return

# Prune non-finalized data
Expand Down Expand Up @@ -703,7 +707,7 @@ proc processNewBlockForLightClient*(
signedBlock: ForkyTrustedSignedBeaconBlock,
parentBid: BlockId) =
## Update light client data with information from a new block.
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
if not dag.shouldImportLcData:
return
if signedBlock.message.slot < dag.lcDataStore.cache.tailSlot:
return
Expand All @@ -722,7 +726,7 @@ proc processNewBlockForLightClient*(
proc processHeadChangeForLightClient*(dag: ChainDAGRef) =
## Update light client data to account for a new head block.
## Note that `dag.finalizedHead` is not yet updated when this is called.
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
if not dag.shouldImportLcData:
return
if dag.head.slot < dag.lcDataStore.cache.tailSlot:
return
Expand Down Expand Up @@ -757,7 +761,7 @@ proc processFinalizationForLightClient*(
## Prune cached data that is no longer useful for creating future
## `LightClientUpdate` and `LightClientBootstrap` instances.
## This needs to be called whenever `finalized_checkpoint` changes.
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
if not dag.shouldImportLcData:
return
let finalizedSlot = dag.finalizedHead.slot
if finalizedSlot < dag.lcDataStore.cache.tailSlot:
Expand Down
20 changes: 19 additions & 1 deletion beacon_chain/networking/network_metadata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,27 @@ proc getMetadataForNetwork*(

proc getRuntimeConfig*(
eth2Network: Option[string]): RuntimeConfig {.raises: [Defect, IOError].} =
## Returns the run-time config for a network specified on the command line
## If the network is not explicitly specified, the function will act as the
## regular Nimbus binary, returning the mainnet config.
##
## TODO the assumption that the input variable is a CLI config option is not
## quite appropriate in such as low-level function. The "assume mainnet by
## default" behavior is something that should be handled closer to the `conf`
## layer.
if eth2Network.isSome:
return getMetadataForNetwork(eth2Network.get).cfg
defaultRuntimeConfig

when const_preset == "mainnet":
when defined(gnosisChainBinary):
gnosisMetadata.cfg
else:
mainnetMetadata.cfg
else:
# This is a non-standard build (i.e. minimal), and the function was most
# likely executed in a test. The best we can do is return a fully default
# config:
defaultRuntimeConfig

proc extractGenesisValidatorRootFromSnapshot*(
snapshot: string): Eth2Digest {.raises: [Defect, IOError, SszError].} =
Expand Down
22 changes: 11 additions & 11 deletions beacon_chain/spec/presets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ when const_preset == "mainnet":
# TODO Move this to RuntimeConfig
const SECONDS_PER_SLOT* {.intdefine.}: uint64 = 12

# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/configs/mainnet.yaml
# TODO Read these from yaml file
# The default run-time config specifies the default configuration values
# that will be used if a particular run-time config is missing specific
# confugration values (which will be then taken from this config object).
# It mostly matches the mainnet config with the exception of few properties
# such as `CONFIG_NAME`, `TERMINAL_TOTAL_DIFFICULTY`, `*_FORK_EPOCH`, etc
# which must be effectively overriden in all network (including mainnet).
const defaultRuntimeConfig* = RuntimeConfig(
# Mainnet config

Expand All @@ -118,7 +122,7 @@ when const_preset == "mainnet":
# * 'ropsten' - testnet
# * 'sepolia' - testnet
# Must match the regex: [a-z0-9\-]
CONFIG_NAME: "mainnet",
CONFIG_NAME: "",

# Transition
# ---------------------------------------------------------------
Expand All @@ -130,8 +134,6 @@ when const_preset == "mainnet":
"0x0000000000000000000000000000000000000000000000000000000000000000"),
# TODO TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH: Epoch(uint64.high),



# Genesis
# ---------------------------------------------------------------
# `2**14` (= 16,384)
Expand All @@ -143,7 +145,6 @@ when const_preset == "mainnet":
# 604800 seconds (7 days)
GENESIS_DELAY: 604800,


# Forking
# ---------------------------------------------------------------
# Some forks are disabled for now:
Expand All @@ -152,17 +153,16 @@ when const_preset == "mainnet":

# Altair
ALTAIR_FORK_VERSION: Version [byte 0x01, 0x00, 0x00, 0x00],
ALTAIR_FORK_EPOCH: Epoch(74240), # Oct 27, 2021, 10:56:23am UTC
ALTAIR_FORK_EPOCH: FAR_FUTURE_EPOCH,
# Bellatrix
BELLATRIX_FORK_VERSION: Version [byte 0x02, 0x00, 0x00, 0x00],
BELLATRIX_FORK_EPOCH: Epoch(uint64.high),
BELLATRIX_FORK_EPOCH: FAR_FUTURE_EPOCH,
# Capella
CAPELLA_FORK_VERSION: Version [byte 0x03, 0x00, 0x00, 0x00],
CAPELLA_FORK_EPOCH: Epoch(uint64.high),
CAPELLA_FORK_EPOCH: FAR_FUTURE_EPOCH,
# Sharding
SHARDING_FORK_VERSION: Version [byte 0x04, 0x00, 0x00, 0x00],
SHARDING_FORK_EPOCH: Epoch(uint64.high),

SHARDING_FORK_EPOCH: FAR_FUTURE_EPOCH,

# Time parameters
# ---------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion research/block_sim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,

while true:
let nextBlockTime = lastEth1BlockAt +
max(1.0, gauss(r, float defaultRuntimeConfig.SECONDS_PER_ETH1_BLOCK, 3.0))
max(1.0, gauss(r, float cfg.SECONDS_PER_ETH1_BLOCK, 3.0))
if nextBlockTime > now:
break

Expand Down
1 change: 0 additions & 1 deletion research/simutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ proc loadGenesis*(validators: Natural, validate: bool):
&"deposit_contract_snapshot_{const_preset}_{validators}_{SPEC_VERSION}.ssz"
cfg = defaultRuntimeConfig


if fileExists(genesisFn) and fileExists(contractSnapshotFn):
let res = newClone(readSszForkedHashedBeaconState(
cfg, readAllBytes(genesisFn).tryGet()))
Expand Down

0 comments on commit 9d754d3

Please sign in to comment.