extract trusted node sync trigger into separate function (#5544)

To prepare for calling trusted node sync from the main Nimbus startup
logic, extract the trusted node sync trigger into a separate function.
Further allow passing a pre-opened database, as that will be needed to
check whether trusted node sync needs to be called during regular start.
This commit is contained in:
Etan Kissling 2023-10-31 01:56:52 +01:00 committed by GitHub
parent 62d59daaa7
commit d289da8cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 43 deletions

View File

@ -114,6 +114,57 @@ declareGauge next_action_wait,
logScope: topics = "beacnde"
proc doRunTrustedNodeSync(
db: BeaconChainDB,
metadata: Eth2NetworkMetadata,
databaseDir: string,
eraDir: string,
restUrl: string,
stateId: Option[string],
trustedBlockRoot: Option[Eth2Digest],
backfill: bool,
reindex: bool,
downloadDepositSnapshot: bool) {.async.} =
let
cfg = metadata.cfg
syncTarget =
if stateId.isSome:
if trustedBlockRoot.isSome:
warn "Ignoring `trustedBlockRoot`, `stateId` is set",
stateId, trustedBlockRoot
TrustedNodeSyncTarget(
kind: TrustedNodeSyncKind.StateId,
stateId: stateId.get)
elif trustedBlockRoot.isSome:
TrustedNodeSyncTarget(
kind: TrustedNodeSyncKind.TrustedBlockRoot,
trustedBlockRoot: trustedBlockRoot.get)
else:
TrustedNodeSyncTarget(
kind: TrustedNodeSyncKind.StateId,
stateId: "finalized")
genesis =
if metadata.hasGenesis:
let genesisBytes = try: await metadata.fetchGenesisBytes()
except CatchableError as err:
error "Failed to obtain genesis state",
source = metadata.genesis.sourceDesc,
err = err.msg
quit 1
newClone(readSszForkedHashedBeaconState(cfg, genesisBytes))
else: nil
await db.doTrustedNodeSync(
cfg,
databaseDir,
eraDir,
restUrl,
syncTarget,
backfill,
reindex,
downloadDepositSnapshot,
genesis)
func getVanityLogs(stdoutKind: StdoutLogKind): VanityLogs =
case stdoutKind
of StdoutLogKind.Auto: raiseAssert "inadmissable here"
@ -2143,51 +2194,24 @@ proc handleStartUpCmd(config: var BeaconNodeConf) {.raises: [CatchableError].} =
of BNStartUpCmd.web3: doWeb3Cmd(config, rng[])
of BNStartUpCmd.slashingdb: doSlashingInterchange(config)
of BNStartUpCmd.trustedNodeSync:
let
network = loadEth2Network(config)
cfg = network.cfg
syncTarget =
if config.stateId.isSome:
if config.lcTrustedBlockRoot.isSome:
warn "Ignoring `trustedBlockRoot`, `stateId` is set",
stateId = config.stateId,
trustedBlockRoot = config.lcTrustedBlockRoot
TrustedNodeSyncTarget(
kind: TrustedNodeSyncKind.StateId,
stateId: config.stateId.get)
elif config.lcTrustedBlockRoot.isSome:
TrustedNodeSyncTarget(
kind: TrustedNodeSyncKind.TrustedBlockRoot,
trustedBlockRoot: config.lcTrustedBlockRoot.get)
else:
TrustedNodeSyncTarget(
kind: TrustedNodeSyncKind.StateId,
stateId: "finalized")
genesis =
if network.hasGenesis:
let genesisBytes = try: waitFor network.fetchGenesisBytes()
except CatchableError as err:
error "Failed to obtain genesis state",
source = network.genesis.sourceDesc,
err = err.msg
quit 1
newClone(readSszForkedHashedBeaconState(cfg, genesisBytes))
else: nil
if config.blockId.isSome():
error "--blockId option has been removed - use --state-id instead!"
quit 1
waitFor doTrustedNodeSync(
cfg,
let
metadata = loadEth2Network(config)
db = BeaconChainDB.new(config.databaseDir, metadata.cfg, inMemory = false)
waitFor db.doRunTrustedNodeSync(
metadata,
config.databaseDir,
config.eraDir,
config.trustedNodeUrl,
syncTarget,
config.stateId,
config.lcTrustedBlockRoot,
config.backfillBlocks,
config.reindex,
config.downloadDepositSnapshot,
genesis)
config.downloadDepositSnapshot)
db.close()
{.pop.} # TODO moduletests exceptions

View File

@ -68,6 +68,7 @@ func shortLog*(v: TrustedNodeSyncTarget): auto =
chronicles.formatIt(TrustedNodeSyncTarget): shortLog(it)
proc doTrustedNodeSync*(
db: BeaconChainDB,
cfg: RuntimeConfig,
databaseDir: string,
eraDir: string,
@ -89,11 +90,6 @@ proc doTrustedNodeSync*(
error "Cannot connect to server", error = error
quit 1
let
db = BeaconChainDB.new(databaseDir, cfg, inMemory = false)
defer:
db.close()
# If possible, we'll store the genesis state in the database - this is not
# strictly necessary but renders the resulting database compatible with
# versions prior to 22.11 and makes reindexing possible
@ -551,7 +547,8 @@ when isMainModule:
kind: TrustedNodeSyncKind.StateId,
stateId: os.paramStr(5))
backfill = os.paramCount() > 5 and os.paramStr(6) == "true"
waitFor doTrustedNodeSync(
db = BeaconChainDB.new(databaseDir, cfg, inMemory = false)
waitFor db.doTrustedNodeSync(
getRuntimeConfig(some os.paramStr(1)), os.paramStr(2), os.paramStr(3),
os.paramStr(4), syncTarget, backfill, false, true)
db.close()