Skip to content

Commit

Permalink
fix: network agnostic loading
Browse files Browse the repository at this point in the history
  • Loading branch information
advaita-saha committed Jul 8, 2024
1 parent 2590542 commit 8fd48cd
Showing 1 changed file with 47 additions and 86 deletions.
133 changes: 47 additions & 86 deletions nimbus/nimbus_import.nim
Original file line number Diff line number Diff line change
Expand Up @@ -276,38 +276,57 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
boolFlag({PersistBlockFlag.NoFullValidation}, not conf.fullValidation) +
boolFlag(NoPersistBodies, not conf.storeBodies) +
boolFlag({PersistBlockFlag.NoPersistReceipts}, not conf.storeReceipts)
clConfig =
if conf.networkId == HoleskyNet:
getMetadataForNetwork("holesky")
elif conf.networkId == SepoliaNet:
getMetadataForNetwork("sepolia")
elif conf.networkId == MainNet:
getMetadataForNetwork("mainnet")
else:
error "Unsupported network", network = conf.networkId
quit(QuitFailure)
genesis_validators_root =
if conf.networkId == MainNet:
Eth2Digest.fromHex(
"0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"
) # Mainnet Validators Root
elif conf.networkId == HoleskyNet:
Eth2Digest.fromHex(
"0x9143aa7c615a7f7115e2b6aac319c03529df8242ae705fba9df39b79c59fa8b1"
) # Holesky Validators Root
elif conf.networkId == SepoliaNet:
Eth2Digest.fromHex(
"0xd8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078"
) # Sepolia Validators Root
else:
error "Unsupported network", network = conf.networkId
quit(QuitFailure)
blocks: seq[EthBlock]
clConfig: Eth2NetworkMetadata
genesis_validators_root: Eth2Digest
lastEra1Block: uint64
firstSlotAfterMerge: uint64

defer:
if csv != nil:
close(csv)

# Network Specific Configurations
# TODO: the merge block number could be fetched from the era1 file instead,
# specially if the accumulator is added to the chain metadata
if conf.networkId == MainNet:
doAssert isDir(conf.era1Dir.string), "Era1 directory not found"
clConfig = getMetadataForNetwork("mainnet")
genesis_validators_root = Eth2Digest.fromHex(
"0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"
) # Mainnet Validators Root
lastEra1Block = 15537393'u64 # Mainnet
firstSlotAfterMerge =
if isDir(conf.eraDir.string):
4700013'u64 # Mainnet
else:
notice "No eraDir found for Mainnet, block loading will stop after era1"
0'u64 # No eraDir for Mainnet
elif conf.networkId == SepoliaNet:
doAssert isDir(conf.era1Dir.string), "Era1 directory not found"
clConfig = getMetadataForNetwork("sepolia")
genesis_validators_root = Eth2Digest.fromHex(
"0xd8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078"
) # Sepolia Validators Root
lastEra1Block = 1450409'u64 # Sepolia
firstSlotAfterMerge =
if isDir(conf.eraDir.string):
115193'u64 # Sepolia
else:
notice "No eraDir found for Sepolia, block loading will stop after era1"
0'u64 # No eraDir for Sepolia
elif conf.networkId == HoleskyNet:
doAssert isDir(conf.eraDir.string), "Era directory not found"
clConfig = getMetadataForNetwork("holesky")
genesis_validators_root = Eth2Digest.fromHex(
"0x9143aa7c615a7f7115e2b6aac319c03529df8242ae705fba9df39b79c59fa8b1"
) # Holesky Validators Root
lastEra1Block = 0'u64
firstSlotAfterMerge = 0'u64
else:
error "Unsupported network", network = conf.networkId
quit(QuitFailure)

nec_import_block_number.set(start.int64)

template blockNumber(): uint64 =
Expand Down Expand Up @@ -395,24 +414,7 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
importedSlot -= 1
notice "Found the slot to start with", importedSlot

if isDir(conf.era1Dir.string):
doAssert conf.networkId == MainNet or conf.networkId == SepoliaNet,
"Only mainnet/sepolia era1 currently supported"

let
# TODO the merge block number could be fetched from the era1 file instead,
# specially if the accumulator is added to the chain metadata
lastEra1Block =
if conf.networkId == MainNet:
15537393'u64 # Mainnet
else:
1450409'u64 # Sepolia
firstSlotAfterMerge =
if conf.networkId == MainNet:
4700013'u64 # Mainnet
else:
115193'u64 # Sepolia

if isDir(conf.era1Dir.string) or isDir(conf.eraDir.string):
if start <= lastEra1Block:
notice "Importing era1 archive",
start, dataDir = conf.dataDir.string, era1Dir = conf.era1Dir.string
Expand Down Expand Up @@ -442,8 +444,6 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
process() # last chunk, if any

if start > lastEra1Block:
doAssert isDir(conf.eraDir.string), "Era directory not found"

notice "Importing era archive",
start, dataDir = conf.dataDir.string, eraDir = conf.eraDir.string

Expand All @@ -460,7 +460,7 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
eraDB, historical_roots.asSeq(), historical_summaries.asSeq()
)

if importedSlot < firstSlotAfterMerge:
if importedSlot < firstSlotAfterMerge and firstSlotAfterMerge != 0:
# if resuming import we do not update the slot
importedSlot = firstSlotAfterMerge

Expand All @@ -485,45 +485,6 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
if blocks.len > 0:
process()

if isDir(conf.eraDir.string) and conf.networkId == HoleskyNet:
let
eraDB = EraDB.new(clConfig.cfg, conf.eraDir.string, genesis_validators_root)
(historical_roots, historical_summaries, endSlot) = loadHistoricalRootsFromEra(
conf.eraDir.string, clConfig.cfg
).valueOr:
error "Error loading historical summaries", error
quit QuitFailure

# Load the last slot number
updateLastImportedSlot(
eraDB, historical_roots.asSeq(), historical_summaries.asSeq()
)

if importedSlot <= endSlot:
notice "Importing era archive HoleskyNet",
importedSlot, dataDir = conf.dataDir.string, eraDir = conf.eraDir.string

while running and imported < conf.maxBlocks and importedSlot <= endSlot:
let clblock = getBlockFromEra(
eraDB,
historical_roots.asSeq(),
historical_summaries.asSeq(),
Slot(importedSlot),
clConfig.cfg,
).valueOr:
importedSlot += 1
continue

blocks.add getEth1Block(clblock)
imported += 1

importedSlot += 1
if blocks.lenu64 mod conf.chunkSize == 0 and blocks.len > 0:
process()

if blocks.len > 0:
process()

for blocksFile in conf.blocksFile:
if isFile(string blocksFile):
# success or not, we quit after importing blocks
Expand Down

0 comments on commit 8fd48cd

Please sign in to comment.