refactor fork consistency checking and gate compilation on it (#3704)

This commit is contained in:
tersec 2022-06-04 21:15:15 +02:00 committed by GitHub
parent 21200f4a64
commit 38737549ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 26 deletions

View File

@ -665,32 +665,7 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,
serveLightClientData = false,
importLightClientData = ImportLightClientData.None,
vanityLogs = default(VanityLogs)): ChainDAGRef =
# TODO move fork version sanity checking elsewhere?
# TODO re-add cfg.CAPELLA_FORK_VERSION once eth-clients repos include it and
# fix SHARDING_FORK_VERSION to be its new FORK_VERSION. Until then make sure
# that it will never actually use the Capella fork.
doAssert cfg.CAPELLA_FORK_EPOCH == FAR_FUTURE_EPOCH
let forkVersions =
[cfg.GENESIS_FORK_VERSION, cfg.ALTAIR_FORK_VERSION,
cfg.BELLATRIX_FORK_VERSION, cfg.SHARDING_FORK_VERSION]
for i in 0 ..< forkVersions.len:
for j in i+1 ..< forkVersions.len:
doAssert forkVersions[i] != forkVersions[j]
template assertForkEpochOrder(
firstForkEpoch: Epoch, secondForkEpoch: Epoch) =
doAssert firstForkEpoch <= secondForkEpoch
# TODO https://github.com/ethereum/consensus-specs/issues/2902 multiple
# fork transitions per epoch don't work in a well-defined way.
doAssert firstForkEpoch < secondForkEpoch or
firstForkEpoch in [GENESIS_EPOCH, FAR_FUTURE_EPOCH]
assertForkEpochOrder(cfg.ALTAIR_FORK_EPOCH, cfg.BELLATRIX_FORK_EPOCH)
assertForkEpochOrder(cfg.BELLATRIX_FORK_EPOCH, cfg.CAPELLA_FORK_EPOCH)
assertForkEpochOrder(cfg.CAPELLA_FORK_EPOCH, cfg.SHARDING_FORK_EPOCH)
cfg.checkForkConsistency()
doAssert updateFlags in [{}, {verifyFinalization}],
"Other flags not supported in ChainDAG"

View File

@ -240,6 +240,9 @@ when not defined(gnosisChainBinary):
mainnetMetadata* = eth2Network("shared/mainnet", mainnet)
praterMetadata* = eth2Network("shared/prater", goerli)
ropstenMetadata = mergeTestnet("ropsten-beacon-chain", ropsten)
static:
for network in [mainnetMetadata, praterMetadata, ropstenMetadata]:
checkForkConsistency(network.cfg)
proc getMetadataForNetwork*(networkName: string): Eth2NetworkMetadata {.raises: [Defect, IOError].} =
template loadRuntimeMetadata: auto =
@ -284,6 +287,8 @@ else:
gnosisMetadata* = loadCompileTimeNetworkMetadata(
currentSourcePath.parentDir.replace('\\', '/') & "/../../media/gnosis")
static: checkForkConsistency(gnosisMetadata.cfg)
proc checkNetworkParameterUse*(eth2Network: Option[string]) =
# Support `gnosis-chain` as network name which was used in v22.3
if eth2Network.isSome and eth2Network.get notin ["gnosis", "gnosis-chain"]:

View File

@ -958,3 +958,29 @@ func clear*(cache: var StateCache) =
cache.shuffled_active_validator_indices.clear
cache.beacon_proposer_indices.clear
cache.sync_committees.clear
func checkForkConsistency*(cfg: RuntimeConfig) =
# TODO re-add cfg.CAPELLA_FORK_VERSION once eth-clients repos include it and
# fix SHARDING_FORK_VERSION to be its new FORK_VERSION. Until then make sure
# that it will never actually use the Capella fork.
doAssert cfg.CAPELLA_FORK_EPOCH == FAR_FUTURE_EPOCH
let forkVersions =
[cfg.GENESIS_FORK_VERSION, cfg.ALTAIR_FORK_VERSION,
cfg.BELLATRIX_FORK_VERSION, cfg.SHARDING_FORK_VERSION]
for i in 0 ..< forkVersions.len:
for j in i+1 ..< forkVersions.len:
doAssert distinctBase(forkVersions[i]) != distinctBase(forkVersions[j])
template assertForkEpochOrder(
firstForkEpoch: Epoch, secondForkEpoch: Epoch) =
doAssert distinctBase(firstForkEpoch) <= distinctBase(secondForkEpoch)
# TODO https://github.com/ethereum/consensus-specs/issues/2902 multiple
# fork transitions per epoch don't work in a well-defined way.
doAssert distinctBase(firstForkEpoch) < distinctBase(secondForkEpoch) or
firstForkEpoch in [GENESIS_EPOCH, FAR_FUTURE_EPOCH]
assertForkEpochOrder(cfg.ALTAIR_FORK_EPOCH, cfg.BELLATRIX_FORK_EPOCH)
assertForkEpochOrder(cfg.BELLATRIX_FORK_EPOCH, cfg.CAPELLA_FORK_EPOCH)
assertForkEpochOrder(cfg.CAPELLA_FORK_EPOCH, cfg.SHARDING_FORK_EPOCH)