reduce forks usage

This commit is contained in:
tersec 2024-03-22 05:32:38 +02:00
parent f8d3534c21
commit dc9449743e
6 changed files with 26 additions and 146 deletions

View File

@ -13,7 +13,7 @@ export
beacon_clock, conf,
el_manager,
forks,
base, message_router
message_router
type
BeaconNode* = ref object
@ -21,7 +21,6 @@ type
elManager*: ELManager
genesisSnapshotContent*: string
beaconClock*: BeaconClock
router*: ref MessageRouter
cfg*: RuntimeConfig
genesisState*: ref ForkedHashedBeaconState

View File

@ -14,8 +14,7 @@ import libp2p/protocols/pubsub/gossipsub
proc initFullNode(
node: BeaconNode,
getBeaconTime: GetBeaconTimeFn) {.async.} =
node.router = new MessageRouter
getBeaconTime: GetBeaconTimeFn) {.async.} = discard
import
"."/spec/forks,
@ -26,8 +25,8 @@ proc preInit(
doAssert getStateField(state, slot).is_epoch,
"Can only initialize database from epoch states"
withState(state):
if forkyState.data.slot == GENESIS_SLOT:
withConsensusFork(state.kind):
if false:
discard 0
else:
discard 0
@ -46,7 +45,7 @@ proc init*(T: type BeaconNode,
except CatchableError:
quit 1
if not getStateField(tmp[], slot).is_epoch:
if not true:
quit 1
tmp
else:

View File

@ -6,9 +6,6 @@ import
./datatypes/[phase0, altair, bellatrix, capella, deneb, electra],
./mev/bellatrix_mev, ./mev/capella_mev, ./mev/deneb_mev
export
eth2_merkleization, eth2_ssz_serialization
type
ConsensusFork* {.pure.} = enum
Phase0,
@ -19,7 +16,7 @@ type
Electra
ForkedHashedBeaconState* = object
case kind: ConsensusFork
case kind*: ConsensusFork
of ConsensusFork.Phase0: phase0Data: phase0.HashedBeaconState
of ConsensusFork.Altair: altairData: altair.HashedBeaconState
of ConsensusFork.Bellatrix: bellatrixData: bellatrix.HashedBeaconState
@ -35,9 +32,6 @@ type
deneb.BeaconBlock |
electra.BeaconBlock
SomeForkyBeaconBlock* =
ForkyBeaconBlock
ForkyExecutionPayloadForSigning* =
bellatrix.ExecutionPayloadForSigning |
capella.ExecutionPayloadForSigning |
@ -79,13 +73,6 @@ type
of ConsensusFork.Deneb: denebData: deneb.SignedBeaconBlock
of ConsensusFork.Electra: electraData: electra.SignedBeaconBlock
ForkySignedBlindedBeaconBlock* =
phase0.SignedBeaconBlock |
altair.SignedBeaconBlock |
bellatrix_mev.SignedBlindedBeaconBlock |
capella_mev.SignedBlindedBeaconBlock |
deneb_mev.SignedBlindedBeaconBlock
ForkedSignedBlindedBeaconBlock = object
case kind: ConsensusFork
of ConsensusFork.Phase0: phase0Data: phase0.SignedBeaconBlock
@ -95,9 +82,6 @@ type
of ConsensusFork.Deneb: denebData: deneb_mev.SignedBlindedBeaconBlock
of ConsensusFork.Electra: electraData: electra.SignedBeaconBlock
SomeForkySignedBeaconBlock =
ForkySignedBeaconBlock
template kind*(
x: typedesc[
phase0.BeaconState |
@ -183,28 +167,6 @@ template SignedBeaconBlock*(kind: static ConsensusFork): auto =
else:
static: raiseAssert "Unreachable"
template ExecutionPayloadForSigning*(kind: static ConsensusFork): auto =
when kind == ConsensusFork.Electra:
typedesc[electra.ExecutionPayloadForSigning]
elif kind == ConsensusFork.Deneb:
typedesc[deneb.ExecutionPayloadForSigning]
elif kind == ConsensusFork.Capella:
typedesc[capella.ExecutionPayloadForSigning]
elif kind == ConsensusFork.Bellatrix:
typedesc[bellatrix.ExecutionPayloadForSigning]
else:
static: raiseAssert "Unreachable"
template SignedBlindedBeaconBlock*(kind: static ConsensusFork): auto =
when kind == ConsensusFork.Deneb:
typedesc[deneb_mev.SignedBlindedBeaconBlock]
elif kind == ConsensusFork.Capella:
typedesc[capella_mev.SignedBlindedBeaconBlock]
elif kind == ConsensusFork.Bellatrix:
static: raiseAssert "Unsupported"
else:
static: raiseAssert "Unreachable"
template withConsensusFork*(
x: ConsensusFork, body: untyped): untyped =
case x
@ -240,7 +202,7 @@ template init*(T: type ForkedSignedBeaconBlock, blck: deneb.SignedBeaconBlock):
template init*(T: type ForkedSignedBeaconBlock, blck: electra.SignedBeaconBlock): T =
T(kind: ConsensusFork.Electra, electraData: blck)
template withState*(x: ForkedHashedBeaconState, body: untyped): untyped =
template withState(x: ForkedHashedBeaconState, body: untyped): untyped =
case x.kind
of ConsensusFork.Electra:
const consensusFork {.inject, used.} = ConsensusFork.Electra
@ -271,7 +233,7 @@ template getStateField*(x: ForkedHashedBeaconState, y: untyped): untyped =
(block:
withState(x): unsafeAddr forkyState.data.y)[]
func consensusForkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): ConsensusFork =
func consensusForkAtEpoch(cfg: RuntimeConfig, epoch: Epoch): ConsensusFork =
if epoch >= cfg.ELECTRA_FORK_EPOCH: ConsensusFork.Electra
elif epoch >= cfg.DENEB_FORK_EPOCH: ConsensusFork.Deneb
elif epoch >= cfg.CAPELLA_FORK_EPOCH: ConsensusFork.Capella
@ -311,98 +273,13 @@ template withBlck*(
template forkyBlck: untyped {.inject, used.} = x.electraData
body
func proposer_index(x: ForkedBeaconBlock): uint64 =
withBlck(x): forkyBlck.proposer_index
func hash_tree_root(x: ForkedBeaconBlock): Eth2Digest =
withBlck(x): hash_tree_root(forkyBlck)
func hash_tree_root(_: Opt[auto]) {.error.}
template signature(x: ForkedSignedBeaconBlock |
ForkedSignedBlindedBeaconBlock): ValidatorSig =
withBlck(x): forkyBlck.signature
template slot(x: ForkedSignedBeaconBlock): Slot =
withBlck(x): forkyBlck.message.slot
func toBeaconBlockHeader(
blck: SomeForkyBeaconBlock |
capella_mev.BlindedBeaconBlock |
deneb_mev.BlindedBeaconBlock
): BeaconBlockHeader =
BeaconBlockHeader(
slot: blck.slot,
proposer_index: blck.proposer_index,
parent_root: blck.parent_root,
state_root: blck.state_root,
body_root: blck.body.hash_tree_root())
template toBeaconBlockHeader(
blck: SomeForkySignedBeaconBlock): BeaconBlockHeader =
blck.message.toBeaconBlockHeader()
func toSignedBeaconBlockHeader*(
signedBlock: SomeForkySignedBeaconBlock |
capella_mev.SignedBlindedBeaconBlock |
deneb_mev.SignedBlindedBeaconBlock
): SignedBeaconBlockHeader =
SignedBeaconBlockHeader(
message: signedBlock.message.toBeaconBlockHeader(),
signature: signedBlock.signature)
func genesisFork(cfg: RuntimeConfig): Fork =
Fork(
previous_version: cfg.GENESIS_FORK_VERSION,
current_version: cfg.GENESIS_FORK_VERSION,
epoch: GENESIS_EPOCH)
func altairFork(cfg: RuntimeConfig): Fork =
Fork(
previous_version: cfg.GENESIS_FORK_VERSION,
current_version: cfg.ALTAIR_FORK_VERSION,
epoch: cfg.ALTAIR_FORK_EPOCH)
func bellatrixFork(cfg: RuntimeConfig): Fork =
Fork(
previous_version: cfg.ALTAIR_FORK_VERSION,
current_version: cfg.BELLATRIX_FORK_VERSION,
epoch: cfg.BELLATRIX_FORK_EPOCH)
func capellaFork(cfg: RuntimeConfig): Fork =
Fork(
previous_version: cfg.BELLATRIX_FORK_VERSION,
current_version: cfg.CAPELLA_FORK_VERSION,
epoch: cfg.CAPELLA_FORK_EPOCH)
func denebFork(cfg: RuntimeConfig): Fork =
Fork(
previous_version: cfg.CAPELLA_FORK_VERSION,
current_version: cfg.DENEB_FORK_VERSION,
epoch: cfg.DENEB_FORK_EPOCH)
func electraFork(cfg: RuntimeConfig): Fork =
Fork(
previous_version: cfg.DENEB_FORK_VERSION,
current_version: cfg.ELECTRA_FORK_VERSION,
epoch: cfg.ELECTRA_FORK_EPOCH)
func forkAtEpoch*(cfg: RuntimeConfig, epoch: Epoch): Fork =
case cfg.consensusForkAtEpoch(epoch)
of ConsensusFork.Electra: cfg.electraFork
of ConsensusFork.Deneb: cfg.denebFork
of ConsensusFork.Capella: cfg.capellaFork
of ConsensusFork.Bellatrix: cfg.bellatrixFork
of ConsensusFork.Altair: cfg.altairFork
of ConsensusFork.Phase0: cfg.genesisFork
type
BeaconStateHeader* = object
genesis_time: uint64
genesis_time*: uint64
genesis_validators_root*: Eth2Digest
slot: Slot
slot*: Slot
func readSszForkedHashedBeaconState*(
func readSszForkedHashedBeaconState(
consensusFork: ConsensusFork, data: openArray[byte]):
ForkedHashedBeaconState {.raises: [SszError].} =
result = ForkedHashedBeaconState(kind: consensusFork)
@ -411,7 +288,7 @@ func readSszForkedHashedBeaconState*(
readSszBytes(data, forkyState.data)
forkyState.root = hash_tree_root(forkyState.data)
template readSszForkedHashedBeaconState*(
template readSszForkedHashedBeaconState(
cfg: RuntimeConfig, slot: Slot, data: openArray[byte]):
ForkedHashedBeaconState =
cfg.consensusForkAtEpoch(slot.epoch()).readSszForkedHashedBeaconState(data)

View File

@ -60,6 +60,7 @@ proc getValidatorForDuties*(
validator: Opt.some Validator(pubkey: ValidatorPubKey.fromHex("891c64850444b66331ef7888c907b4af71ab6b2c883affe2cebd15d6c3644ac7ce6af96334192efdf95a64bab8ea425a")[]))
from ".."/spec/datatypes/capella import shortLog
from ".."/spec/datatypes/phase0 import BeaconBlock, shortLog
proc makeBeaconBlock(): Result[phase0.BeaconBlock, cstring] = ok(default(phase0.BeaconBlock))
proc getProposalState(
@ -78,7 +79,7 @@ proc makeBeaconBlockForHeadAndSlot(
var cache = StateCache()
let maybeState = getProposalState(head, slot, cache)
let consensusFork = ConsensusFork.Bellatrix
if maybeState.isErr:
return err($maybeState.error)
@ -87,7 +88,7 @@ proc makeBeaconBlockForHeadAndSlot(
payloadFut =
if execution_payload.isSome:
var modified_execution_payload = execution_payload
withState(state[]):
withConsensusFork(consensusFork):
discard
let fut = Future[Opt[PayloadType]].Raising([CancelledError]).init(
"given-payload")
@ -145,7 +146,8 @@ proc getUnsignedBlindedBeaconBlock[
validator_index: ValidatorIndex, forkedBlock: ForkedBeaconBlock,
executionPayloadHeader: capella.ExecutionPayloadHeader):
Result[T, string] =
withBlck(forkedBlock):
var fork = ConsensusFork.Altair
withConsensusFork(fork):
return err("")
proc getBlindedBlockParts[
@ -311,7 +313,7 @@ proc proposeBlockAux(
if notSlashable.isErr:
warn "Slashing protection activated for block proposal",
blockRoot = shortLog(blockRoot),
blck = shortLog(forkyBlck),
blck = shortLog(default(phase0.BeaconBlock)),
signingRoot = shortLog(signingRoot),
existingProposal = notSlashable.error
return head

View File

@ -11,22 +11,25 @@ from ".."/spec/datatypes/deneb import BlobSidecar
import ".."/spec/forks
type
BlobSidecars* = seq[ref BlobSidecar]
MessageRouter* = object
VerifierError* {.pure.} = enum
BlobSidecars = seq[ref BlobSidecar]
MessageRouter = object
VerifierError {.pure.} = enum
Invalid
MissingParent
UnviableFork
Duplicate
from ".."/consensus_object_pools/block_dag import BlockRef, init
import ".."/spec/digest
import ".."/spec/datatypes/base
func getBlockRef(root: Eth2Digest): Opt[BlockRef] =
let newRef = BlockRef.init(
root,
0.Slot)
return ok(newRef)
from ".."/spec/datatypes/altair import shortLog
proc addBlock(
blck: ForkedSignedBeaconBlock,
blobs: Opt[BlobSidecars], maybeFinalized = false,

View File

@ -43,7 +43,7 @@ switch("passC", "-fvisibility=hidden")
switch("passC", "-fno-omit-frame-pointer")
switch("passL", "-fno-omit-frame-pointer")
when true:
when false:
--define:nimStackTraceOverride
switch("import", "libbacktrace")
else: