rm blockForkAtEpoch and switch callers to consensusForkAtEpoch (#4634)

This commit is contained in:
tersec 2023-02-16 21:16:54 +01:00 committed by GitHub
parent 8202d4a84e
commit dc0bbe3a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 24 deletions

View File

@ -206,7 +206,7 @@ func getBlockIdAtSlot*(dag: ChainDAGRef, slot: Slot): Opt[BlockSlotId] =
proc containsBlock( proc containsBlock(
cfg: RuntimeConfig, db: BeaconChainDB, slot: Slot, root: Eth2Digest): bool = cfg: RuntimeConfig, db: BeaconChainDB, slot: Slot, root: Eth2Digest): bool =
db.containsBlock(root, cfg.blockForkAtEpoch(slot.epoch)) db.containsBlock(root, cfg.consensusForkAtEpoch(slot.epoch))
proc getForkedBlock*(db: BeaconChainDB, root: Eth2Digest): proc getForkedBlock*(db: BeaconChainDB, root: Eth2Digest):
Opt[ForkedTrustedSignedBeaconBlock] = Opt[ForkedTrustedSignedBeaconBlock] =
@ -232,7 +232,7 @@ proc getForkedBlock*(db: BeaconChainDB, root: Eth2Digest):
err() err()
proc containsBlock(dag: ChainDAGRef, bid: BlockId): bool = proc containsBlock(dag: ChainDAGRef, bid: BlockId): bool =
let fork = dag.cfg.blockForkAtEpoch(bid.slot.epoch) let fork = dag.cfg.consensusForkAtEpoch(bid.slot.epoch)
if dag.db.containsBlock(bid.root, fork): if dag.db.containsBlock(bid.root, fork):
return true return true
@ -254,7 +254,7 @@ proc getBlock*(
proc getBlockSSZ*(dag: ChainDAGRef, bid: BlockId, bytes: var seq[byte]): bool = proc getBlockSSZ*(dag: ChainDAGRef, bid: BlockId, bytes: var seq[byte]): bool =
# Load the SSZ-encoded data of a block into `bytes`, overwriting the existing # Load the SSZ-encoded data of a block into `bytes`, overwriting the existing
# content # content
let fork = dag.cfg.blockForkAtEpoch(bid.slot.epoch) let fork = dag.cfg.consensusForkAtEpoch(bid.slot.epoch)
dag.db.getBlockSSZ(bid.root, bytes, fork) or dag.db.getBlockSSZ(bid.root, bytes, fork) or
(bid.slot <= dag.finalizedHead.slot and (bid.slot <= dag.finalizedHead.slot and
getBlockSSZ( getBlockSSZ(
@ -266,7 +266,7 @@ proc getBlockSZ*(dag: ChainDAGRef, bid: BlockId, bytes: var seq[byte]): bool =
# `bytes`, overwriting the existing content # `bytes`, overwriting the existing content
# careful: there are two snappy encodings in use, with and without framing! # careful: there are two snappy encodings in use, with and without framing!
# Returns true if the block is found, false if not # Returns true if the block is found, false if not
let fork = dag.cfg.blockForkAtEpoch(bid.slot.epoch) let fork = dag.cfg.consensusForkAtEpoch(bid.slot.epoch)
dag.db.getBlockSZ(bid.root, bytes, fork) or dag.db.getBlockSZ(bid.root, bytes, fork) or
(bid.slot <= dag.finalizedHead.slot and (bid.slot <= dag.finalizedHead.slot and
getBlockSZ( getBlockSZ(
@ -276,7 +276,7 @@ proc getBlockSZ*(dag: ChainDAGRef, bid: BlockId, bytes: var seq[byte]): bool =
proc getForkedBlock*( proc getForkedBlock*(
dag: ChainDAGRef, bid: BlockId): Opt[ForkedTrustedSignedBeaconBlock] = dag: ChainDAGRef, bid: BlockId): Opt[ForkedTrustedSignedBeaconBlock] =
let fork = dag.cfg.blockForkAtEpoch(bid.slot.epoch) let fork = dag.cfg.consensusForkAtEpoch(bid.slot.epoch)
result.ok(ForkedTrustedSignedBeaconBlock(kind: fork)) result.ok(ForkedTrustedSignedBeaconBlock(kind: fork))
withBlck(result.get()): withBlck(result.get()):
type T = type(blck) type T = type(blck)
@ -883,7 +883,7 @@ proc applyBlock(
loadStateCache(dag, cache, bid, getStateField(state, slot).epoch) loadStateCache(dag, cache, bid, getStateField(state, slot).epoch)
case dag.cfg.blockForkAtEpoch(bid.slot.epoch) case dag.cfg.consensusForkAtEpoch(bid.slot.epoch)
of ConsensusFork.Phase0: of ConsensusFork.Phase0:
let data = getBlock(dag, bid, phase0.TrustedSignedBeaconBlock).valueOr: let data = getBlock(dag, bid, phase0.TrustedSignedBeaconBlock).valueOr:
return err("Block load failed") return err("Block load failed")
@ -1629,7 +1629,7 @@ proc pruneBlockSlot(dag: ChainDAGRef, bs: BlockSlot) =
dag.optimisticRoots.excl bs.blck.root dag.optimisticRoots.excl bs.blck.root
dag.forkBlocks.excl(KeyedBlockRef.init(bs.blck)) dag.forkBlocks.excl(KeyedBlockRef.init(bs.blck))
discard dag.db.delBlock( discard dag.db.delBlock(
dag.cfg.blockForkAtEpoch(bs.blck.slot.epoch), bs.blck.root) dag.cfg.consensusForkAtEpoch(bs.blck.slot.epoch), bs.blck.root)
proc pruneBlocksDAG(dag: ChainDAGRef) = proc pruneBlocksDAG(dag: ChainDAGRef) =
## This prunes the block DAG ## This prunes the block DAG
@ -1929,7 +1929,7 @@ proc pruneHistory*(dag: ChainDAGRef, startup = false) =
while cur.isSome: while cur.isSome:
let let
bid = cur.get() bid = cur.get()
fork = dag.cfg.blockForkAtEpoch(bid.slot.epoch) fork = dag.cfg.consensusForkAtEpoch(bid.slot.epoch)
if bid.slot == GENESIS_SLOT: if bid.slot == GENESIS_SLOT:
# Leave genesis block for nostalgia and the REST API # Leave genesis block for nostalgia and the REST API
@ -1953,14 +1953,14 @@ proc pruneHistory*(dag: ChainDAGRef, startup = false) =
for fork in ConsensusFork.Phase0..<stateFork: for fork in ConsensusFork.Phase0..<stateFork:
dag.db.clearStates(fork) dag.db.clearStates(fork)
let blockFork = dag.cfg.blockForkAtEpoch(blockHorizon.epoch) let blockFork = dag.cfg.consensusForkAtEpoch(blockHorizon.epoch)
if blockFork > ConsensusFork.Phase0: if blockFork > ConsensusFork.Phase0:
for fork in ConsensusFork.Phase0..<blockFork: for fork in ConsensusFork.Phase0..<blockFork:
dag.db.clearBlocks(fork) dag.db.clearBlocks(fork)
proc loadExecutionBlockRoot*(dag: ChainDAGRef, bid: BlockId): Eth2Digest = proc loadExecutionBlockRoot*(dag: ChainDAGRef, bid: BlockId): Eth2Digest =
if dag.cfg.blockForkAtEpoch(bid.slot.epoch) < ConsensusFork.Bellatrix: if dag.cfg.consensusForkAtEpoch(bid.slot.epoch) < ConsensusFork.Bellatrix:
return ZERO_HASH return ZERO_HASH
let blockData = dag.getForkedBlock(bid).valueOr: let blockData = dag.getForkedBlock(bid).valueOr:

View File

@ -796,7 +796,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
$error) $error)
forked = ForkedSignedBeaconBlock(restBlock) forked = ForkedSignedBeaconBlock(restBlock)
if forked.kind != node.dag.cfg.blockForkAtEpoch( if forked.kind != node.dag.cfg.consensusForkAtEpoch(
getForkedBlockField(forked, slot).epoch): getForkedBlockField(forked, slot).epoch):
doAssert strictVerification notin node.dag.updateFlags doAssert strictVerification notin node.dag.updateFlags
return RestApiResponse.jsonError(Http400, InvalidBlockObjectError) return RestApiResponse.jsonError(Http400, InvalidBlockObjectError)
@ -895,7 +895,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
$error) $error)
forked = ForkedSignedBeaconBlock(restBlock) forked = ForkedSignedBeaconBlock(restBlock)
if forked.kind != node.dag.cfg.blockForkAtEpoch( if forked.kind != node.dag.cfg.consensusForkAtEpoch(
getForkedBlockField(forked, slot).epoch): getForkedBlockField(forked, slot).epoch):
return RestApiResponse.jsonError(Http400, InvalidBlockObjectError) return RestApiResponse.jsonError(Http400, InvalidBlockObjectError)
@ -950,7 +950,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
return RestApiResponse.jsonError(Http404, BlockNotFoundError) return RestApiResponse.jsonError(Http404, BlockNotFoundError)
let let
fork = node.dag.cfg.blockForkAtEpoch(bid.slot.epoch) fork = node.dag.cfg.consensusForkAtEpoch(bid.slot.epoch)
headers = [("eth-consensus-version", fork.toString())] headers = [("eth-consensus-version", fork.toString())]
RestApiResponse.sszResponsePlain(data, headers) RestApiResponse.sszResponsePlain(data, headers)

View File

@ -654,15 +654,6 @@ func consensusForkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): ConsensusFork =
elif epoch >= cfg.ALTAIR_FORK_EPOCH: ConsensusFork.Altair elif epoch >= cfg.ALTAIR_FORK_EPOCH: ConsensusFork.Altair
else: ConsensusFork.Phase0 else: ConsensusFork.Phase0
func blockForkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): ConsensusFork =
## Return the current fork for the given epoch.
static: doAssert high(ConsensusFork) == ConsensusFork.EIP4844
if epoch >= cfg.DENEB_FORK_EPOCH: ConsensusFork.EIP4844
elif epoch >= cfg.CAPELLA_FORK_EPOCH: ConsensusFork.Capella
elif epoch >= cfg.BELLATRIX_FORK_EPOCH: ConsensusFork.Bellatrix
elif epoch >= cfg.ALTAIR_FORK_EPOCH: ConsensusFork.Altair
else: ConsensusFork.Phase0
func stateForkForDigest*( func stateForkForDigest*(
forkDigests: ForkDigests, forkDigest: ForkDigest): Opt[ConsensusFork] = forkDigests: ForkDigests, forkDigest: ForkDigest): Opt[ConsensusFork] =
static: doAssert high(ConsensusFork) == ConsensusFork.EIP4844 static: doAssert high(ConsensusFork) == ConsensusFork.EIP4844
@ -1000,7 +991,7 @@ func readSszForkedSignedBeaconBlock*(
# TODO https://github.com/nim-lang/Nim/issues/19357 # TODO https://github.com/nim-lang/Nim/issues/19357
result = ForkedSignedBeaconBlock( result = ForkedSignedBeaconBlock(
kind: cfg.blockForkAtEpoch(header.slot.epoch())) kind: cfg.consensusForkAtEpoch(header.slot.epoch()))
withBlck(result): withBlck(result):
readSszBytes(data, blck) readSszBytes(data, blck)

View File

@ -250,7 +250,7 @@ proc cmdBench(conf: DbConf, cfg: RuntimeConfig) =
let blck = blockRefs[blockRefs.len - b - 1] let blck = blockRefs[blockRefs.len - b - 1]
withTimer(timers[tLoadBlock]): withTimer(timers[tLoadBlock]):
case cfg.blockForkAtEpoch(blck.slot.epoch) case cfg.consensusForkAtEpoch(blck.slot.epoch)
of ConsensusFork.Phase0: of ConsensusFork.Phase0:
blocks[0].add dag.db.getBlock( blocks[0].add dag.db.getBlock(
blck.root, phase0.TrustedSignedBeaconBlock).get() blck.root, phase0.TrustedSignedBeaconBlock).get()