rename stateForkAtEpoch to consensusForkAtEpoch (#4627)

This commit is contained in:
tersec 2023-02-16 10:32:12 +01:00 committed by GitHub
parent 4597486dcc
commit e342fdd97a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 30 deletions

View File

@ -650,7 +650,7 @@ proc getState(
let state_root = db.getStateRoot(block_root, slot).valueOr: let state_root = db.getStateRoot(block_root, slot).valueOr:
return false return false
db.getState(cfg.stateForkAtEpoch(slot.epoch), state_root, state, rollback) db.getState(cfg.consensusForkAtEpoch(slot.epoch), state_root, state, rollback)
proc containsState*( proc containsState*(
db: BeaconChainDB, cfg: RuntimeConfig, block_root: Eth2Digest, db: BeaconChainDB, cfg: RuntimeConfig, block_root: Eth2Digest,
@ -659,7 +659,8 @@ proc containsState*(
while slot >= slots.a: while slot >= slots.a:
let state_root = db.getStateRoot(block_root, slot) let state_root = db.getStateRoot(block_root, slot)
if state_root.isSome() and if state_root.isSome() and
db.containsState(cfg.stateForkAtEpoch(slot.epoch), state_root.get()): db.containsState(
cfg.consensusForkAtEpoch(slot.epoch), state_root.get()):
return true return true
if slot == slots.a: # avoid underflow at genesis if slot == slots.a: # avoid underflow at genesis
@ -676,7 +677,8 @@ proc getState*(
let state_root = db.getStateRoot(block_root, slot) let state_root = db.getStateRoot(block_root, slot)
if state_root.isSome() and if state_root.isSome() and
db.getState( db.getState(
cfg.stateForkAtEpoch(slot.epoch), state_root.get(), state, rollback): cfg.consensusForkAtEpoch(slot.epoch), state_root.get(), state,
rollback):
return true return true
if slot == slots.a: # avoid underflow at genesis if slot == slots.a: # avoid underflow at genesis
@ -831,7 +833,8 @@ proc putState(dag: ChainDAGRef, state: ForkedHashedBeaconState, bid: BlockId) =
# Don't consider legacy tables here, they are slow to read so we'll want to # Don't consider legacy tables here, they are slow to read so we'll want to
# rewrite things in the new table anyway. # rewrite things in the new table anyway.
if dag.db.containsState( if dag.db.containsState(
dag.cfg.stateForkAtEpoch(slot.epoch), getStateRoot(state), legacy = false): dag.cfg.consensusForkAtEpoch(slot.epoch), getStateRoot(state),
legacy = false):
return return
let startTick = Moment.now() let startTick = Moment.now()
@ -1612,7 +1615,8 @@ proc delState(dag: ChainDAGRef, bsi: BlockSlotId) =
if (let root = dag.db.getStateRoot(bsi.bid.root, bsi.slot); root.isSome()): if (let root = dag.db.getStateRoot(bsi.bid.root, bsi.slot); root.isSome()):
dag.db.withManyWrites: dag.db.withManyWrites:
dag.db.delStateRoot(bsi.bid.root, bsi.slot) dag.db.delStateRoot(bsi.bid.root, bsi.slot)
dag.db.delState(dag.cfg.stateForkAtEpoch(bsi.slot.epoch), root.get()) dag.db.delState(
dag.cfg.consensusForkAtEpoch(bsi.slot.epoch), root.get())
proc pruneBlockSlot(dag: ChainDAGRef, bs: BlockSlot) = proc pruneBlockSlot(dag: ChainDAGRef, bs: BlockSlot) =
# TODO: should we move that disk I/O to `onSlotEnd` # TODO: should we move that disk I/O to `onSlotEnd`
@ -1944,7 +1948,7 @@ proc pruneHistory*(dag: ChainDAGRef, startup = false) =
# so as to "mostly" clean up the phase0 tables as well (which cannot be # so as to "mostly" clean up the phase0 tables as well (which cannot be
# pruned easily by fork) # pruned easily by fork)
let stateFork = dag.cfg.stateForkAtEpoch(tailSlot.epoch) let stateFork = dag.cfg.consensusForkAtEpoch(tailSlot.epoch)
if stateFork > ConsensusFork.Phase0: if stateFork > ConsensusFork.Phase0:
for fork in ConsensusFork.Phase0..<stateFork: for fork in ConsensusFork.Phase0..<stateFork:
dag.db.clearStates(fork) dag.db.clearStates(fork)
@ -2383,7 +2387,7 @@ proc rebuildIndex*(dag: ChainDAGRef) =
junk.add((k, v)) junk.add((k, v))
continue continue
if not dag.db.containsState(dag.cfg.stateForkAtEpoch(k[0].epoch), v): if not dag.db.containsState(dag.cfg.consensusForkAtEpoch(k[0].epoch), v):
continue # If it's not in the database.. continue # If it's not in the database..
canonical[k[0].epoch div EPOCHS_PER_STATE_SNAPSHOT] = v canonical[k[0].epoch div EPOCHS_PER_STATE_SNAPSHOT] = v
@ -2423,7 +2427,8 @@ proc rebuildIndex*(dag: ChainDAGRef) =
else: else:
if not dag.db.getState( if not dag.db.getState(
dag.cfg.stateForkAtEpoch(slot.epoch), state_root, state[], noRollback): dag.cfg.consensusForkAtEpoch(slot.epoch), state_root, state[],
noRollback):
fatal "Cannot load state, database corrupt or created for a different network?", fatal "Cannot load state, database corrupt or created for a different network?",
state_root, slot state_root, slot
quit 1 quit 1
@ -2446,7 +2451,9 @@ proc rebuildIndex*(dag: ChainDAGRef) =
slot, startStateRoot = canonical[i - 1], startSlot slot, startStateRoot = canonical[i - 1], startSlot
if getStateRoot(state[]) != canonical[i - 1]: if getStateRoot(state[]) != canonical[i - 1]:
if not dag.db.getState(dag.cfg.stateForkAtEpoch(startSlot.epoch), canonical[i - 1], state[], noRollback): if not dag.db.getState(
dag.cfg.consensusForkAtEpoch(startSlot.epoch), canonical[i - 1],
state[], noRollback):
error "Can't load start state, database corrupt?", error "Can't load start state, database corrupt?",
startStateRoot = shortLog(canonical[i - 1]), slot = startSlot startStateRoot = shortLog(canonical[i - 1]), slot = startSlot
return return
@ -2491,4 +2498,4 @@ proc rebuildIndex*(dag: ChainDAGRef) =
for i in junk: for i in junk:
dag.db.delStateRoot(i[0][1], i[0][0]) dag.db.delStateRoot(i[0][1], i[0][0])
dag.db.delState(dag.cfg.stateForkAtEpoch(i[0][0].epoch), i[1]) dag.db.delState(dag.cfg.consensusForkAtEpoch(i[0][0].epoch), i[1])

View File

@ -1,4 +1,4 @@
# Copyright (c) 2018-2022 Status Research & Development GmbH # Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -391,7 +391,7 @@ iterator getBlockIds*(
while true: while true:
# `case` ensures we're on a fork for which the `PartialBeaconState` # `case` ensures we're on a fork for which the `PartialBeaconState`
# definition is consistent # definition is consistent
case db.cfg.stateForkAtEpoch(slot.epoch) case db.cfg.consensusForkAtEpoch(slot.epoch)
of ConsensusFork.Phase0 .. ConsensusFork.EIP4844: of ConsensusFork.Phase0 .. ConsensusFork.EIP4844:
let stateSlot = (slot.era() + 1).start_slot() let stateSlot = (slot.era() + 1).start_slot()
if not getPartialState(db, historical_roots, stateSlot, state[]): if not getPartialState(db, historical_roots, stateSlot, state[]):

View File

@ -287,7 +287,7 @@ proc installMessageValidators*(
validatorProcName: untyped): ValidationResult = validatorProcName: untyped): ValidationResult =
msg.logReceived() msg.logReceived()
if contextFork != lightClient.cfg.stateForkAtEpoch(msg.contextEpoch): if contextFork != lightClient.cfg.consensusForkAtEpoch(msg.contextEpoch):
msg.logDropped( msg.logDropped(
(ValidationResult.Reject, cstring "Invalid context fork")) (ValidationResult.Reject, cstring "Invalid context fork"))
return ValidationResult.Reject return ValidationResult.Reject

View File

@ -151,7 +151,7 @@ proc loadChainDag(
withForkyFinalityUpdate(data): withForkyFinalityUpdate(data):
when lcDataFork > LightClientDataFork.None: when lcDataFork > LightClientDataFork.None:
let contextFork = let contextFork =
dag.cfg.stateForkAtEpoch(forkyFinalityUpdate.contextEpoch) dag.cfg.consensusForkAtEpoch(forkyFinalityUpdate.contextEpoch)
eventBus.finUpdateQueue.emit( eventBus.finUpdateQueue.emit(
RestVersioned[ForkedLightClientFinalityUpdate]( RestVersioned[ForkedLightClientFinalityUpdate](
data: data, data: data,
@ -162,7 +162,7 @@ proc loadChainDag(
withForkyOptimisticUpdate(data): withForkyOptimisticUpdate(data):
when lcDataFork > LightClientDataFork.None: when lcDataFork > LightClientDataFork.None:
let contextFork = let contextFork =
dag.cfg.stateForkAtEpoch(forkyOptimisticUpdate.contextEpoch) dag.cfg.consensusForkAtEpoch(forkyOptimisticUpdate.contextEpoch)
eventBus.optUpdateQueue.emit( eventBus.optUpdateQueue.emit(
RestVersioned[ForkedLightClientOptimisticUpdate]( RestVersioned[ForkedLightClientOptimisticUpdate](
data: data, data: data,
@ -889,7 +889,7 @@ func getSyncCommitteeSubnets(node: BeaconNode, epoch: Epoch): SyncnetBits =
# but more than SYNC_COMMITTEE_SUBNET_COUNT epochs from when the next sync # but more than SYNC_COMMITTEE_SUBNET_COUNT epochs from when the next sync
# committee period begins, in which case `epochsToNextSyncPeriod` is none. # committee period begins, in which case `epochsToNextSyncPeriod` is none.
if epochsToSyncPeriod.isNone or if epochsToSyncPeriod.isNone or
node.dag.cfg.stateForkAtEpoch(epoch + epochsToSyncPeriod.get) < node.dag.cfg.consensusForkAtEpoch(epoch + epochsToSyncPeriod.get) <
ConsensusFork.Altair: ConsensusFork.Altair:
return subnets return subnets

View File

@ -840,7 +840,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
let let
currentEpochFork = currentEpochFork =
node.dag.cfg.stateForkAtEpoch(node.currentSlot().epoch()) node.dag.cfg.consensusForkAtEpoch(node.currentSlot().epoch())
version = request.headers.getString("eth-consensus-version") version = request.headers.getString("eth-consensus-version")
body = contentBody.get() body = contentBody.get()

View File

@ -38,7 +38,7 @@ proc installLightClientApiHandlers*(router: var RestRouter, node: BeaconNode) =
when lcDataFork > LightClientDataFork.None: when lcDataFork > LightClientDataFork.None:
let let
contextEpoch = forkyBootstrap.contextEpoch contextEpoch = forkyBootstrap.contextEpoch
contextFork = node.dag.cfg.stateForkAtEpoch(contextEpoch) contextFork = node.dag.cfg.consensusForkAtEpoch(contextEpoch)
return return
if contentType == sszMediaType: if contentType == sszMediaType:
let headers = [("eth-consensus-version", contextFork.toString())] let headers = [("eth-consensus-version", contextFork.toString())]
@ -101,7 +101,7 @@ proc installLightClientApiHandlers*(router: var RestRouter, node: BeaconNode) =
forkyUpdate.contextEpoch forkyUpdate.contextEpoch
else: else:
continue continue
contextFork = node.dag.cfg.stateForkAtEpoch(contextEpoch) contextFork = node.dag.cfg.consensusForkAtEpoch(contextEpoch)
updates.add RestVersioned[ForkedLightClientUpdate]( updates.add RestVersioned[ForkedLightClientUpdate](
data: update, data: update,
jsonVersion: contextFork, jsonVersion: contextFork,
@ -133,7 +133,7 @@ proc installLightClientApiHandlers*(router: var RestRouter, node: BeaconNode) =
when lcDataFork > LightClientDataFork.None: when lcDataFork > LightClientDataFork.None:
let let
contextEpoch = forkyFinalityUpdate.contextEpoch contextEpoch = forkyFinalityUpdate.contextEpoch
contextFork = node.dag.cfg.stateForkAtEpoch(contextEpoch) contextFork = node.dag.cfg.consensusForkAtEpoch(contextEpoch)
return return
if contentType == sszMediaType: if contentType == sszMediaType:
let headers = [("eth-consensus-version", contextFork.toString())] let headers = [("eth-consensus-version", contextFork.toString())]
@ -164,7 +164,7 @@ proc installLightClientApiHandlers*(router: var RestRouter, node: BeaconNode) =
when lcDataFork > LightClientDataFork.None: when lcDataFork > LightClientDataFork.None:
let let
contextEpoch = forkyOptimisticUpdate.contextEpoch contextEpoch = forkyOptimisticUpdate.contextEpoch
contextFork = node.dag.cfg.stateForkAtEpoch(contextEpoch) contextFork = node.dag.cfg.consensusForkAtEpoch(contextEpoch)
return return
if contentType == sszMediaType: if contentType == sszMediaType:
let headers = [("eth-consensus-version", contextFork.toString())] let headers = [("eth-consensus-version", contextFork.toString())]

View File

@ -638,7 +638,7 @@ func getStateRoot*(x: ForkedHashedBeaconState): Eth2Digest =
func setStateRoot*(x: var ForkedHashedBeaconState, root: Eth2Digest) = func setStateRoot*(x: var ForkedHashedBeaconState, root: Eth2Digest) =
withState(x): forkyState.root = root withState(x): forkyState.root = root
func stateForkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): ConsensusFork = func consensusForkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): ConsensusFork =
## Return the current fork for the given epoch. ## Return the current fork for the given epoch.
static: static:
doAssert high(ConsensusFork) == ConsensusFork.EIP4844 doAssert high(ConsensusFork) == ConsensusFork.EIP4844
@ -695,7 +695,7 @@ func atStateFork*(
template atEpoch*( template atEpoch*(
forkDigests: ForkDigests, epoch: Epoch, cfg: RuntimeConfig): ForkDigest = forkDigests: ForkDigests, epoch: Epoch, cfg: RuntimeConfig): ForkDigest =
forkDigests.atStateFork(cfg.stateForkAtEpoch(epoch)) forkDigests.atStateFork(cfg.consensusForkAtEpoch(epoch))
template asSigned*( template asSigned*(
x: ForkedMsgTrustedSignedBeaconBlock | x: ForkedMsgTrustedSignedBeaconBlock |
@ -898,7 +898,7 @@ func denebFork*(cfg: RuntimeConfig): Fork =
epoch: cfg.DENEB_FORK_EPOCH) epoch: cfg.DENEB_FORK_EPOCH)
func forkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Fork = func forkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Fork =
case cfg.stateForkAtEpoch(epoch) case cfg.consensusForkAtEpoch(epoch)
of ConsensusFork.EIP4844: cfg.denebFork of ConsensusFork.EIP4844: cfg.denebFork
of ConsensusFork.Capella: cfg.capellaFork of ConsensusFork.Capella: cfg.capellaFork
of ConsensusFork.Bellatrix: cfg.bellatrixFork of ConsensusFork.Bellatrix: cfg.bellatrixFork
@ -906,7 +906,7 @@ func forkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Fork =
of ConsensusFork.Phase0: cfg.genesisFork of ConsensusFork.Phase0: cfg.genesisFork
func forkVersionAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Version = func forkVersionAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Version =
case cfg.stateForkAtEpoch(epoch) case cfg.consensusForkAtEpoch(epoch)
of ConsensusFork.EIP4844: cfg.DENEB_FORK_VERSION of ConsensusFork.EIP4844: cfg.DENEB_FORK_VERSION
of ConsensusFork.Capella: cfg.CAPELLA_FORK_VERSION of ConsensusFork.Capella: cfg.CAPELLA_FORK_VERSION
of ConsensusFork.Bellatrix: cfg.BELLATRIX_FORK_VERSION of ConsensusFork.Bellatrix: cfg.BELLATRIX_FORK_VERSION
@ -914,7 +914,8 @@ func forkVersionAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Version =
of ConsensusFork.Phase0: cfg.GENESIS_FORK_VERSION of ConsensusFork.Phase0: cfg.GENESIS_FORK_VERSION
func nextForkEpochAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Epoch = func nextForkEpochAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Epoch =
case cfg.stateForkAtEpoch(epoch) static: doAssert high(ConsensusFork) == ConsensusFork.EIP4844
case cfg.consensusForkAtEpoch(epoch)
of ConsensusFork.EIP4844: FAR_FUTURE_EPOCH of ConsensusFork.EIP4844: FAR_FUTURE_EPOCH
of ConsensusFork.Capella: cfg.DENEB_FORK_EPOCH of ConsensusFork.Capella: cfg.DENEB_FORK_EPOCH
of ConsensusFork.Bellatrix: cfg.CAPELLA_FORK_EPOCH of ConsensusFork.Bellatrix: cfg.CAPELLA_FORK_EPOCH
@ -961,7 +962,7 @@ func readSszForkedHashedBeaconState*(
ForkedHashedBeaconState {.raises: [Defect, SszError].} = ForkedHashedBeaconState {.raises: [Defect, SszError].} =
# TODO https://github.com/nim-lang/Nim/issues/19357 # TODO https://github.com/nim-lang/Nim/issues/19357
result = ForkedHashedBeaconState( result = ForkedHashedBeaconState(
kind: cfg.stateForkAtEpoch(slot.epoch())) kind: cfg.consensusForkAtEpoch(slot.epoch()))
withState(result): withState(result):
readSszBytes(data, forkyState.data) readSszBytes(data, forkyState.data)

View File

@ -173,7 +173,7 @@ proc readChunkPayload*(
conn, peer, MsgType.Forky(lcDataFork)) conn, peer, MsgType.Forky(lcDataFork))
if res.isOk: if res.isOk:
if contextFork != if contextFork !=
peer.network.cfg.stateForkAtEpoch(res.get.contextEpoch): peer.network.cfg.consensusForkAtEpoch(res.get.contextEpoch):
return neterr InvalidContextBytes return neterr InvalidContextBytes
var obj = ok MsgType(kind: lcDataFork) var obj = ok MsgType(kind: lcDataFork)
obj.get.forky(lcDataFork) = res.get obj.get.forky(lcDataFork) = res.get

View File

@ -83,7 +83,7 @@ proc doTrustedNodeSync*(
genesisRoot = genesisRoot.get() genesisRoot = genesisRoot.get()
quit 1 quit 1
genesisStateRoot = getForkedBlockField(genesisBlock, state_root) genesisStateRoot = getForkedBlockField(genesisBlock, state_root)
stateFork = cfg.stateForkAtEpoch(GENESIS_EPOCH) stateFork = cfg.consensusForkAtEpoch(GENESIS_EPOCH)
tmp = (ref ForkedHashedBeaconState)(kind: stateFork) tmp = (ref ForkedHashedBeaconState)(kind: stateFork)
if not db.getState(stateFork, genesisStateRoot, tmp[], noRollback): if not db.getState(stateFork, genesisStateRoot, tmp[], noRollback):

View File

@ -700,7 +700,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
if blockRatio > 0.0: if blockRatio > 0.0:
withTimer(timers[t]): withTimer(timers[t]):
case dag.cfg.stateForkAtEpoch(slot.epoch) case dag.cfg.consensusForkAtEpoch(slot.epoch)
of ConsensusFork.EIP4844: proposeEIP4844Block(slot) of ConsensusFork.EIP4844: proposeEIP4844Block(slot)
of ConsensusFork.Capella: proposeCapellaBlock(slot) of ConsensusFork.Capella: proposeCapellaBlock(slot)
of ConsensusFork.Bellatrix: proposeBellatrixBlock(slot) of ConsensusFork.Bellatrix: proposeBellatrixBlock(slot)