From 9f590a22e84be00fc682ca7027edf85e28143646 Mon Sep 17 00:00:00 2001 From: andri lim Date: Tue, 8 Jan 2019 18:29:56 +0700 Subject: [PATCH] fix persist tool --- nimbus/config.nim | 17 ++++++++++------- premix/persist.nim | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/nimbus/config.nim b/nimbus/config.nim index a9fc7a496..7270740aa 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -222,7 +222,7 @@ proc processList(v: string, o: var seq[string]) = if len(n) > 0: o.add(n) -proc processInteger(v: string, o: var int): ConfigStatus = +proc processInteger*(v: string, o: var int): ConfigStatus = ## Convert string to integer. try: o = parseInt(v) @@ -512,6 +512,14 @@ template checkArgument(a, b, c, e: untyped) = result = res break +proc getDefaultDataDir*(): string = + when defined(windows): + "AppData" / "Roaming" / "Nimbus" / "DB" + elif defined(macosx): + "Library" / "Application Support" / "Nimbus" / "DB" + else: + ".cache" / "nimbus" / "db" + proc initConfiguration(): NimbusConfiguration = ## Allocates and initializes `NimbusConfiguration` with default values result = new NimbusConfiguration @@ -527,12 +535,7 @@ proc initConfiguration(): NimbusConfiguration = result.net.discPort = 30303'u16 result.net.ident = NimbusIdent - const dataDir = when defined(windows): - "AppData" / "Roaming" / "Nimbus" / "DB" - elif defined(macosx): - "Library" / "Application Support" / "Nimbus" / "DB" - else: - ".cache" / "nimbus" / "db" + const dataDir = getDefaultDataDir() result.dataDir = getHomeDir() / dataDir result.prune = PruneMode.Full diff --git a/premix/persist.nim b/premix/persist.nim index fc93979fc..6ad6d0a4f 100644 --- a/premix/persist.nim +++ b/premix/persist.nim @@ -2,12 +2,12 @@ import eth_common, stint, byteutils, nimcrypto, - chronicles, rlp, downloader + chronicles, rlp, downloader, configuration import eth_trie/[hexary, db, defs], ../nimbus/db/[storage_types, db_chain, select_backend], - ../nimbus/[genesis, utils, config], + ../nimbus/[genesis, utils], ../nimbus/p2p/chain const @@ -39,8 +39,9 @@ proc main() = let chainDB = newBaseChainDB(trieDB, false) # move head to block number ... - #var parentBlock = requestBlock(49438.u256) - #chainDB.setHead(parentBlock.header) + if conf.head != 0.u256: + var parentBlock = requestBlock(conf.head) + chainDB.setHead(parentBlock.header) if canonicalHeadHashKey().toOpenArray notin trieDB: persistToDb(db): @@ -51,16 +52,16 @@ proc main() = var blockNumber = head.blockNumber + 1 var chain = newChain(chainDB) - const - numBlocksToCommit = 128 - numBlocksToDownload = 20000 + let numBlocksToCommit = conf.numCommits var headers = newSeqOfCap[BlockHeader](numBlocksToCommit) var bodies = newSeqOfCap[BlockBody](numBlocksToCommit) var one = 1.u256 var numBlocks = 0 - for _ in 0 ..< numBlocksToDownload: + var counter = 0 + + while true: info "REQUEST HEADER", blockNumber=blockNumber var thisBlock = requestBlock(blockNumber) @@ -71,13 +72,30 @@ proc main() = if numBlocks == numBlocksToCommit: persistToDb(db): - discard chain.persistBlocks(headers, bodies) + if chain.persistBlocks(headers, bodies) != ValidationResult.OK: + break numBlocks = 0 headers.setLen(0) bodies.setLen(0) + inc counter + if conf.maxBlocks != 0 and counter >= conf.maxBlocks: + break + if numBlocks > 0: persistToDb(db): discard chain.persistBlocks(headers, bodies) -main() +when isMainModule: + var message: string + + ## Processing command line arguments + if processArguments(message) != Success: + echo message + quit(QuitFailure) + else: + if len(message) > 0: + echo message + quit(QuitSuccess) + + main()