Skip to content

Commit

Permalink
inital external sync structure
Browse files Browse the repository at this point in the history
  • Loading branch information
advaita-saha committed Aug 16, 2024
1 parent 4dbc165 commit 347e40e
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions nimbus/external_sync.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Nimbus
# Copyright (c) 2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
std/[os, strutils, net],
chronicles,
./constants,
./nimbus_desc,
./db/core_db/persistent,
./utils/era_helpers,
beacon_chain/spec/eth2_apis/[rest_types, rest_beacon_calls],
beacon_chain/networking/network_metadata,
eth/async_utils

var running {.volatile.} = true

proc loadBlocksFromBeaconChain(conf: NimbusConf) {.async.} =
let coreDB =
# Resolve statically for database type
case conf.chainDbMode:
of Aristo,AriPrune:
AristoDbRocks.newCoreDbRef(string conf.dataDir, conf.dbOptions())

let com = CommonRef.new(
db = coreDB,
pruneHistory = (conf.chainDbMode == AriPrune),
networkId = conf.networkId,
params = conf.networkParams)

defer:
com.db.finish()

let
currentBlockNumber = com.db.getSavedStateBlockNumber()
chain = com.newChain()
clConfig = getMetadataForNetwork("sepolia").cfg

var
client = RestClientRef.new("http://192.168.29.44:3000").valueOr:
error "Cannot connect to server"
quit(QuitFailure)

let
clHead =
try:
awaitWithTimeout(
client.getBlockV2(BlockIdent.init(BlockIdentType.Head), clConfig),
30.seconds):
error "Failed to get CL head"
quit(QuitFailure)
except CatchableError as exc:
error "Error getting CL head", error = exc.msg
quit(QuitFailure)

if clHead.isSome():
let data = clHead.get()[]
let blck = data.asTrusted()
notice "CL head is available", data

let elBlock =
blck.getEthBlock().valueOr:
error "Failed to get Eth block"
quit(QuitFailure)

notice "Eth block", elBlock



when isMainModule:
## Ctrl+C handling
proc controlCHandler() {.noconv.} =
when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057
setupForeignThreadGc()
running = false

setControlCHook(controlCHandler)

## Show logs on stdout until we get the user's logging choice
discard defaultChroniclesStream.output.open(stdout)

## Processing command line arguments
let conf = makeConfig()
waitFor loadBlocksFromBeaconChain(conf)

0 comments on commit 347e40e

Please sign in to comment.