diff --git a/.gitignore b/.gitignore index ca02be0..6f79490 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ NimBinaries .vscode/* *.exe crawler_data +dht diff --git a/codexcrawler/application.nim b/codexcrawler/application.nim index ba3f3a2..c4bb91a 100644 --- a/codexcrawler/application.nim +++ b/codexcrawler/application.nim @@ -72,7 +72,7 @@ proc initializeLists(app: Application): Future[?!void] {.async.} = return success() -proc initializeDht(app: Application): ?!void = +proc initializeDht(app: Application): Future[?!void] {.async.} = without dhtStore =? app.createDatastore("dht"), err: return failure(err) let keyPath = app.config.dataDir / "privatekey" @@ -86,8 +86,10 @@ proc initializeDht(app: Application): ?!void = bindPort = app.config.discPort, announceAddrs = announceAddresses, bootstrapNodes = app.config.bootNodes, - store = dhtStore + store = dhtStore, ) + + await app.dht.start() return success() proc initializeApp(app: Application): Future[?!void] {.async.} = @@ -95,7 +97,7 @@ proc initializeApp(app: Application): Future[?!void] {.async.} = error "Failed to initialize lists", err = err.msg return failure(err) - if err =? app.initializeDht().errorOption: + if err =? (await app.initializeDht()).errorOption: error "Failed to initialize DHT", err = err.msg return failure(err) @@ -103,6 +105,7 @@ proc initializeApp(app: Application): Future[?!void] {.async.} = proc stop*(app: Application) = app.status = ApplicationStatus.Stopping + waitFor app.dht.stop() proc run*(app: Application) = app.config = parseConfig() diff --git a/codexcrawler/config.nim b/codexcrawler/config.nim index 5185f4e..c1ad7f2 100644 --- a/codexcrawler/config.nim +++ b/codexcrawler/config.nim @@ -54,7 +54,7 @@ proc getBootNodeStrings(input: string): seq[string] = return getDefaultTestnetBootNodes() return input.split(";") -proc stringToSpr(uri: string): SignedPeerRecord = +proc stringToSpr(uri: string): SignedPeerRecord = var res: SignedPeerRecord try: if not res.fromURI(uri): diff --git a/codexcrawler/dht.nim b/codexcrawler/dht.nim index 37f30d2..ceb3753 100644 --- a/codexcrawler/dht.nim +++ b/codexcrawler/dht.nim @@ -86,8 +86,7 @@ proc new*( bootstrapNodes: openArray[SignedPeerRecord] = [], store: Datastore = SQLiteDatastore.new(Memory).expect("Should not fail!"), ): Dht = - var self = - Dht(key: key, peerId: PeerId.init(key).expect("Should construct PeerId")) + var self = Dht(key: key, peerId: PeerId.init(key).expect("Should construct PeerId")) self.updateAnnounceRecord(announceAddrs) diff --git a/codexcrawler/keyutils.nim b/codexcrawler/keyutils.nim index 7030e5a..3a446f2 100644 --- a/codexcrawler/keyutils.nim +++ b/codexcrawler/keyutils.nim @@ -44,8 +44,7 @@ proc checkSecureFile*(path: string): IoResult[bool] = else: ok (?getPermissionsSet(path) == {UserRead, UserWrite}) -type - KeyError* = object of CatchableError +type KeyError* = object of CatchableError proc setupKey*(path: string): ?!PrivateKey = if not path.fileAccessible({AccessFlags.Find}): @@ -60,9 +59,7 @@ proc setupKey*(path: string): ?!PrivateKey = info "Found a network private key" if not ?checkSecureFile(path).mapFailure(KeyError): warn "The network private key file is not safe, aborting" - return failure newException( - KeyError, "The network private key file is not safe" - ) + return failure newException(KeyError, "The network private key file is not safe") let kb = ?path.readAllBytes().mapFailure(KeyError) return PrivateKey.init(kb).mapFailure(KeyError) diff --git a/config.nims b/config.nims index 1a79753..d47ab5b 100644 --- a/config.nims +++ b/config.nims @@ -1,7 +1,11 @@ --define: metrics + # switch("define", "chronicles_runtime_filtering=true") +# Sets TRACE logging for everything except DHT switch("define", "chronicles_log_level=TRACE") +switch("define", "chronicles_disabled_topics:discv5") when (NimMajor, NimMinor) >= (2, 0): - --mm:refc + --mm: + refc