From 38737549acad4ae15b77c4a32bb60adbadf280ac Mon Sep 17 00:00:00 2001 From: tersec Date: Sat, 4 Jun 2022 21:15:15 +0200 Subject: [PATCH] refactor fork consistency checking and gate compilation on it (#3704) --- .../consensus_object_pools/blockchain_dag.nim | 27 +------------------ beacon_chain/networking/network_metadata.nim | 5 ++++ beacon_chain/spec/datatypes/base.nim | 26 ++++++++++++++++++ 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/beacon_chain/consensus_object_pools/blockchain_dag.nim b/beacon_chain/consensus_object_pools/blockchain_dag.nim index 403f7b78b..9e5f74689 100644 --- a/beacon_chain/consensus_object_pools/blockchain_dag.nim +++ b/beacon_chain/consensus_object_pools/blockchain_dag.nim @@ -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" diff --git a/beacon_chain/networking/network_metadata.nim b/beacon_chain/networking/network_metadata.nim index c4c1e212c..64068b81c 100644 --- a/beacon_chain/networking/network_metadata.nim +++ b/beacon_chain/networking/network_metadata.nim @@ -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"]: diff --git a/beacon_chain/spec/datatypes/base.nim b/beacon_chain/spec/datatypes/base.nim index 4b7117b41..234bafc84 100644 --- a/beacon_chain/spec/datatypes/base.nim +++ b/beacon_chain/spec/datatypes/base.nim @@ -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)