From 5e07b082a96a36f576bb50d8a624d20e22a32221 Mon Sep 17 00:00:00 2001 From: Pedro Miranda Date: Mon, 21 Oct 2024 11:24:23 +0100 Subject: [PATCH] copy&paste beacon node from eth2 --- .../consensus/consensus_wrapper.nim | 178 +++++++++++++++++- 1 file changed, 170 insertions(+), 8 deletions(-) diff --git a/nimbus_unified/consensus/consensus_wrapper.nim b/nimbus_unified/consensus/consensus_wrapper.nim index 0182ffb5e..956de97a9 100644 --- a/nimbus_unified/consensus/consensus_wrapper.nim +++ b/nimbus_unified/consensus/consensus_wrapper.nim @@ -5,19 +5,181 @@ # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. -import chronicles, std/[os, atomics], ../configs/nimbus_configs +import + std/[os, exitprocs], + beacon_chain/nimbus_binary_common, + beacon_chain/spec/forks, + beacon_chain/[beacon_chain_db, trusted_node_sync], + beacon_chain/networking/network_metadata_downloads, + chronos, + chronicles, + stew/io2, + ../configs/nimbus_configs + export nimbus_configs ## log logScope: topics = "Consensus layer" -proc consensusWrapper*(parameters: TaskParameters) = - info "Consensus wrapper:", worker = parameters.name - while true: - sleep(3000) - info "looping consensus" - if isShutDownRequired.load() == true: - break +## Copy paste from nimbus_beacon_node.nim Copied due to the fact that nimbus_beacon_node +## contains the programMain. +## TODO: extract from that file into a common file +var gPidFile: string + +#TODO: Investigate why the commented code triggers GC violation +proc createPidFile(filename: string) {.raises: [IOError].} = + # writeFile filename, $os.getCurrentProcessId() + writeFile filename, "222" + # gPidFile = filename + # addExitProc proc() {.noconv.} = + # ## TODO: changed from original file, fixes dprecation warning + # discard io2.removeFile(gPidFile) + +## runs beacon node +## adpated from nimbus-eth2 +proc doRunBeaconNode( + config: var BeaconNodeConf, rng: ref HmacDrbgContext +) {.raises: [CatchableError].} = + info "Launching beacon node", + version = "fullVersionStr", #TODO:changed from original version + bls_backend = $BLS_BACKEND, + const_preset, + cmdParams = commandLineParams(), + config + + template ignoreDeprecatedOption(option: untyped): untyped = + if config.option.isSome: + warn "Config option is deprecated", option = config.option.get + + ignoreDeprecatedOption requireEngineAPI + ignoreDeprecatedOption safeSlotsToImportOptimistically + ignoreDeprecatedOption terminalTotalDifficultyOverride + ignoreDeprecatedOption optimistic + ignoreDeprecatedOption validatorMonitorTotals + ignoreDeprecatedOption web3ForcePolling + + createPidFile(config.dataDir.string / "beacon_node.pid") + + config.createDumpDirs() + + #TODO: We might need to split this on the same file + # if config.metricsEnabled: + # let metricsAddress = config.metricsAddress + # notice "Starting metrics HTTP server", + # url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics" + # try: + # startMetricsHttpServer($metricsAddress, config.metricsPort) + # except CatchableError as exc: + # raise exc + # except Exception as exc: + # raiseAssert exc.msg # TODO fix metrics + +proc fetchGenesisState( + metadata: Eth2NetworkMetadata, + genesisState = none(InputFile), + genesisStateUrl = none(Uri), +): Future[ref ForkedHashedBeaconState] {.async: (raises: []).} = + let genesisBytes = + if metadata.genesis.kind != BakedIn and genesisState.isSome: + let res = io2.readAllBytes(genesisState.get.string) + res.valueOr: + error "Failed to read genesis state file", err = res.error.ioErrorMsg + quit 1 + elif metadata.hasGenesis: + try: + if metadata.genesis.kind == BakedInUrl: + info "Obtaining genesis state", + sourceUrl = $genesisStateUrl.get(parseUri metadata.genesis.url) + await metadata.fetchGenesisBytes(genesisStateUrl) + except CatchableError as err: + error "Failed to obtain genesis state", + source = metadata.genesis.sourceDesc, err = err.msg + quit 1 + else: + @[] + + if genesisBytes.len > 0: + try: + newClone readSszForkedHashedBeaconState(metadata.cfg, genesisBytes) + except CatchableError as err: + error "Invalid genesis state", + size = genesisBytes.len, digest = eth2digest(genesisBytes), err = err.msg + quit 1 + else: + nil + +proc doRunTrustedNodeSync( + db: BeaconChainDB, + metadata: Eth2NetworkMetadata, + databaseDir: string, + eraDir: string, + restUrl: string, + stateId: Option[string], + trustedBlockRoot: Option[Eth2Digest], + backfill: bool, + reindex: bool, + downloadDepositSnapshot: bool, + genesisState: ref ForkedHashedBeaconState, +) {.async.} = + let 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") + + await db.doTrustedNodeSync( + metadata.cfg, databaseDir, eraDir, restUrl, syncTarget, backfill, reindex, + downloadDepositSnapshot, genesisState, + ) + +## --end copy paste file from nimbus-eth2/nimbus_beacon_node.nim + +## Consensus wrapper +proc consensusWrapper*(parameters: TaskParameters) {.raises: [CatchableError].} = + let rng = HmacDrbgContext.new() + var config = parameters.beaconNodeConfigs + try: + doRunBeaconNode(config, rng) + except CatchableError as e: + fatal "error", message = e.msg + quit 1 + + let + metadata = loadEth2Network(config) + db = BeaconChainDB.new(config.databaseDir, metadata.cfg, inMemory = false) + genesisState = waitFor fetchGenesisState(metadata) + + try: + waitFor( + db.doRunTrustedNodeSync( + metadata, config.databaseDir, config.eraDir, "http://127.0.0.1:5052", + config.stateId, config.lcTrustedBlockRoot, config.backfillBlocks, config.reindex, + config.downloadDepositSnapshot, genesisState, + ) + ) + except CatchableError as e: + fatal "error", message = e.msg + quit 1 + + db.close() + + # --web3-url=http://127.0.0.1:8551 --jwt-secret=/tmp/jwtsecret --log-level=TRACE + # --network=${NETWORK} \ + # --data-dir="${DATA_DIR}" \ + # --tcp-port=$(( ${BASE_P2P_PORT} + ${NODE_ID} )) \ + # --udp-port=$(( ${BASE_P2P_PORT} + ${NODE_ID} )) \ + # --rest \ + # --rest-port=$(( ${BASE_REST_PORT} + ${NODE_ID} )) \ + # --metrics \ + # ${WEB3_URL_ARG} ${EXTRA_ARGS} \ + # "$@" warn "\tExiting consensus wrapper"