extract chain DAG loading to separate function (#3527)
When transitioning from light client to full node the chain DAG will be loaded separately from the rest of the beacon node initialization. Extracting chain DAG loading to a separate function will allow reusing a lot of the existing code. This code move doesn't change semantics.
This commit is contained in:
parent
ea1acd7397
commit
ca045900c8
|
@ -143,6 +143,65 @@ versionGauge.set(1, labelValues=[fullVersionStr, gitRevision])
|
||||||
|
|
||||||
logScope: topics = "beacnde"
|
logScope: topics = "beacnde"
|
||||||
|
|
||||||
|
proc loadChainDag(
|
||||||
|
config: BeaconNodeConf,
|
||||||
|
cfg: RuntimeConfig,
|
||||||
|
db: BeaconChainDB,
|
||||||
|
eventBus: AsyncEventBus,
|
||||||
|
validatorMonitor: ref ValidatorMonitor,
|
||||||
|
networkGenesisValidatorsRoot: Option[Eth2Digest]): ChainDAGRef =
|
||||||
|
info "Loading block DAG from database", path = config.databaseDir
|
||||||
|
|
||||||
|
proc onBlockAdded(data: ForkedTrustedSignedBeaconBlock) =
|
||||||
|
eventBus.emit("signed-beacon-block", data)
|
||||||
|
proc onHeadChanged(data: HeadChangeInfoObject) =
|
||||||
|
eventBus.emit("head-change", data)
|
||||||
|
proc onChainReorg(data: ReorgInfoObject) =
|
||||||
|
eventBus.emit("chain-reorg", data)
|
||||||
|
proc onOptimisticLightClientUpdate(data: OptimisticLightClientUpdate) =
|
||||||
|
discard
|
||||||
|
|
||||||
|
let
|
||||||
|
chainDagFlags =
|
||||||
|
if config.verifyFinalization: {verifyFinalization}
|
||||||
|
else: {}
|
||||||
|
onOptimisticLightClientUpdateCb =
|
||||||
|
if config.serveLightClientData: onOptimisticLightClientUpdate
|
||||||
|
else: nil
|
||||||
|
dag = ChainDAGRef.init(
|
||||||
|
cfg, db, validatorMonitor, chainDagFlags,
|
||||||
|
onBlockAdded, onHeadChanged, onChainReorg,
|
||||||
|
onOptimisticLCUpdateCb = onOptimisticLightClientUpdateCb,
|
||||||
|
serveLightClientData = config.serveLightClientData,
|
||||||
|
importLightClientData = config.importLightClientData)
|
||||||
|
databaseGenesisValidatorsRoot =
|
||||||
|
getStateField(dag.headState, genesis_validators_root)
|
||||||
|
|
||||||
|
if networkGenesisValidatorsRoot.isSome:
|
||||||
|
if networkGenesisValidatorsRoot.get != databaseGenesisValidatorsRoot:
|
||||||
|
fatal "The specified --data-dir contains data for a different network",
|
||||||
|
networkGenesisValidatorsRoot = networkGenesisValidatorsRoot.get,
|
||||||
|
databaseGenesisValidatorsRoot,
|
||||||
|
dataDir = config.dataDir
|
||||||
|
quit 1
|
||||||
|
|
||||||
|
dag
|
||||||
|
|
||||||
|
proc checkWeakSubjectivityCheckpoint(
|
||||||
|
dag: ChainDAGRef,
|
||||||
|
wsCheckpoint: Checkpoint,
|
||||||
|
beaconClock: BeaconClock) =
|
||||||
|
let
|
||||||
|
currentSlot = beaconClock.now.slotOrZero
|
||||||
|
isCheckpointStale = not is_within_weak_subjectivity_period(
|
||||||
|
dag.cfg, currentSlot, dag.headState, wsCheckpoint)
|
||||||
|
|
||||||
|
if isCheckpointStale:
|
||||||
|
error "Weak subjectivity checkpoint is stale",
|
||||||
|
currentSlot, checkpoint = wsCheckpoint,
|
||||||
|
headStateSlot = getStateField(dag.headState, slot)
|
||||||
|
quit 1
|
||||||
|
|
||||||
const SlashingDbName = "slashing_protection"
|
const SlashingDbName = "slashing_protection"
|
||||||
# changing this requires physical file rename as well or history is lost.
|
# changing this requires physical file rename as well or history is lost.
|
||||||
|
|
||||||
|
@ -195,12 +254,6 @@ proc init*(T: type BeaconNode,
|
||||||
eventBus.emit("attestation-received", data)
|
eventBus.emit("attestation-received", data)
|
||||||
proc onVoluntaryExitAdded(data: SignedVoluntaryExit) =
|
proc onVoluntaryExitAdded(data: SignedVoluntaryExit) =
|
||||||
eventBus.emit("voluntary-exit", data)
|
eventBus.emit("voluntary-exit", data)
|
||||||
proc onBlockAdded(data: ForkedTrustedSignedBeaconBlock) =
|
|
||||||
eventBus.emit("signed-beacon-block", data)
|
|
||||||
proc onHeadChanged(data: HeadChangeInfoObject) =
|
|
||||||
eventBus.emit("head-change", data)
|
|
||||||
proc onChainReorg(data: ReorgInfoObject) =
|
|
||||||
eventBus.emit("chain-reorg", data)
|
|
||||||
proc makeOnFinalizationCb(
|
proc makeOnFinalizationCb(
|
||||||
# This `nimcall` functions helps for keeping track of what
|
# This `nimcall` functions helps for keeping track of what
|
||||||
# needs to be captured by the onFinalization closure.
|
# needs to be captured by the onFinalization closure.
|
||||||
|
@ -216,8 +269,6 @@ proc init*(T: type BeaconNode,
|
||||||
eventBus.emit("finalization", data)
|
eventBus.emit("finalization", data)
|
||||||
proc onSyncContribution(data: SignedContributionAndProof) =
|
proc onSyncContribution(data: SignedContributionAndProof) =
|
||||||
eventBus.emit("sync-contribution-and-proof", data)
|
eventBus.emit("sync-contribution-and-proof", data)
|
||||||
proc onOptimisticLightClientUpdate(data: OptimisticLightClientUpdate) =
|
|
||||||
discard
|
|
||||||
|
|
||||||
if config.finalizedCheckpointState.isSome:
|
if config.finalizedCheckpointState.isSome:
|
||||||
let checkpointStatePath = config.finalizedCheckpointState.get.string
|
let checkpointStatePath = config.finalizedCheckpointState.get.string
|
||||||
|
@ -379,52 +430,21 @@ proc init*(T: type BeaconNode,
|
||||||
for key in config.validatorMonitorPubkeys:
|
for key in config.validatorMonitorPubkeys:
|
||||||
validatorMonitor[].addMonitor(key, none(ValidatorIndex))
|
validatorMonitor[].addMonitor(key, none(ValidatorIndex))
|
||||||
|
|
||||||
info "Loading block DAG from database", path = config.databaseDir
|
|
||||||
|
|
||||||
let
|
let
|
||||||
chainDagFlags =
|
networkGenesisValidatorsRoot: Option[Eth2Digest] =
|
||||||
if config.verifyFinalization: {verifyFinalization}
|
|
||||||
else: {}
|
|
||||||
onOptimisticLightClientUpdateCb =
|
|
||||||
if config.serveLightClientData: onOptimisticLightClientUpdate
|
|
||||||
else: nil
|
|
||||||
dag = ChainDAGRef.init(
|
|
||||||
cfg, db, validatorMonitor, chainDagFlags, onBlockAdded, onHeadChanged,
|
|
||||||
onChainReorg, onOptimisticLCUpdateCb = onOptimisticLightClientUpdateCb,
|
|
||||||
serveLightClientData = config.serveLightClientData,
|
|
||||||
importLightClientData = config.importLightClientData)
|
|
||||||
quarantine = newClone(Quarantine.init())
|
|
||||||
databaseGenesisValidatorsRoot =
|
|
||||||
getStateField(dag.headState, genesis_validators_root)
|
|
||||||
|
|
||||||
if genesisStateContents.len != 0:
|
if genesisStateContents.len != 0:
|
||||||
let
|
some(extractGenesisValidatorRootFromSnapshot(genesisStateContents))
|
||||||
networkGenesisValidatorsRoot =
|
else:
|
||||||
extractGenesisValidatorRootFromSnapshot(genesisStateContents)
|
none(Eth2Digest)
|
||||||
|
dag = loadChainDag(
|
||||||
if networkGenesisValidatorsRoot != databaseGenesisValidatorsRoot:
|
config, cfg, db, eventBus,
|
||||||
fatal "The specified --data-dir contains data for a different network",
|
validatorMonitor, networkGenesisValidatorsRoot)
|
||||||
networkGenesisValidatorsRoot, databaseGenesisValidatorsRoot,
|
beaconClock = BeaconClock.init(
|
||||||
dataDir = config.dataDir
|
getStateField(dag.headState, genesis_time))
|
||||||
quit 1
|
|
||||||
|
|
||||||
let beaconClock = BeaconClock.init(getStateField(dag.headState, genesis_time))
|
|
||||||
|
|
||||||
if config.weakSubjectivityCheckpoint.isSome:
|
if config.weakSubjectivityCheckpoint.isSome:
|
||||||
let
|
dag.checkWeakSubjectivityCheckpoint(
|
||||||
currentSlot = beaconClock.now.slotOrZero
|
config.weakSubjectivityCheckpoint.get, beaconClock)
|
||||||
isCheckpointStale = not is_within_weak_subjectivity_period(
|
|
||||||
cfg,
|
|
||||||
currentSlot,
|
|
||||||
dag.headState,
|
|
||||||
config.weakSubjectivityCheckpoint.get)
|
|
||||||
|
|
||||||
if isCheckpointStale:
|
|
||||||
error "Weak subjectivity checkpoint is stale",
|
|
||||||
currentSlot,
|
|
||||||
checkpoint = config.weakSubjectivityCheckpoint.get,
|
|
||||||
headStateSlot = getStateField(dag.headState, slot)
|
|
||||||
quit 1
|
|
||||||
|
|
||||||
if eth1Monitor.isNil and config.web3Urls.len > 0:
|
if eth1Monitor.isNil and config.web3Urls.len > 0:
|
||||||
eth1Monitor = Eth1Monitor.init(
|
eth1Monitor = Eth1Monitor.init(
|
||||||
|
@ -498,6 +518,7 @@ proc init*(T: type BeaconNode,
|
||||||
network = createEth2Node(
|
network = createEth2Node(
|
||||||
rng, config, netKeys, cfg, dag.forkDigests, getBeaconTime,
|
rng, config, netKeys, cfg, dag.forkDigests, getBeaconTime,
|
||||||
getStateField(dag.headState, genesis_validators_root))
|
getStateField(dag.headState, genesis_validators_root))
|
||||||
|
quarantine = newClone(Quarantine.init())
|
||||||
attestationPool = newClone(
|
attestationPool = newClone(
|
||||||
AttestationPool.init(
|
AttestationPool.init(
|
||||||
dag, quarantine, onAttestationReceived, config.proposerBoosting))
|
dag, quarantine, onAttestationReceived, config.proposerBoosting))
|
||||||
|
|
Loading…
Reference in New Issue