2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-01-10 15:32:56 +00:00
|
|
|
## Copyright (c) 2021 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 pkg/chronos
|
2023-05-02 13:06:34 +00:00
|
|
|
import pkg/questionable
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/confutils
|
2023-05-02 13:06:34 +00:00
|
|
|
import pkg/confutils/defs
|
|
|
|
import pkg/confutils/std/net
|
|
|
|
import pkg/confutils/toml/defs as confTomlDefs
|
|
|
|
import pkg/confutils/toml/std/net as confTomlNet
|
|
|
|
import pkg/confutils/toml/std/uri as confTomlUri
|
|
|
|
import pkg/toml_serialization
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/libp2p
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import ./codex/conf
|
|
|
|
import ./codex/codex
|
feat: create logging proxy (#663)
* implement a logging proxy
The logging proxy:
- prevents the need to import chronicles (as well as export except toJson),
- prevents the need to override `writeValue` or use or import nim-json-seralization elsewhere in the codebase, allowing for sole use of utils/json for de/serialization,
- and handles json formatting correctly in chronicles json sinks
* Rename logging -> logutils to avoid ambiguity with common names
* clean up
* add setProperty for JsonRecord, remove nim-json-serialization conflict
* Allow specifying textlines and json format separately
Not specifying a LogFormat will apply the formatting to both textlines and json sinks.
Specifying a LogFormat will apply the formatting to only that sink.
* remove unneeded usages of std/json
We only need to import utils/json instead of std/json
* move serialization from rest/json to utils/json so it can be shared
* fix NoColors ambiguity
Was causing unit tests to fail on Windows.
* Remove nre usage to fix Windows error
Windows was erroring with `could not load: pcre64.dll`. Instead of fixing that error, remove the pcre usage :)
* Add logutils module doc
* Shorten logutils.formatIt for `NBytes`
Both json and textlines formatIt were not needed, and could be combined into one formatIt
* remove debug integration test config
debug output and logformat of json for integration test logs
* Use ## module doc to support docgen
* bump nim-poseidon2 to export fromBytes
Before the changes in this branch, fromBytes was likely being resolved by nim-stew, or other dependency. With the changes in this branch, that dependency was removed and fromBytes could no longer be resolved. By exporting fromBytes from nim-poseidon, the correct resolution is now happening.
* fixes to get compiling after rebasing master
* Add support for Result types being logged using formatIt
2024-01-23 07:35:03 +00:00
|
|
|
import ./codex/logutils
|
2023-07-06 23:23:27 +00:00
|
|
|
import ./codex/units
|
2022-11-02 00:58:41 +00:00
|
|
|
import ./codex/utils/keyutils
|
2024-03-12 09:57:13 +00:00
|
|
|
import ./codex/codextypes
|
2022-01-10 15:32:56 +00:00
|
|
|
|
feat: create logging proxy (#663)
* implement a logging proxy
The logging proxy:
- prevents the need to import chronicles (as well as export except toJson),
- prevents the need to override `writeValue` or use or import nim-json-seralization elsewhere in the codebase, allowing for sole use of utils/json for de/serialization,
- and handles json formatting correctly in chronicles json sinks
* Rename logging -> logutils to avoid ambiguity with common names
* clean up
* add setProperty for JsonRecord, remove nim-json-serialization conflict
* Allow specifying textlines and json format separately
Not specifying a LogFormat will apply the formatting to both textlines and json sinks.
Specifying a LogFormat will apply the formatting to only that sink.
* remove unneeded usages of std/json
We only need to import utils/json instead of std/json
* move serialization from rest/json to utils/json so it can be shared
* fix NoColors ambiguity
Was causing unit tests to fail on Windows.
* Remove nre usage to fix Windows error
Windows was erroring with `could not load: pcre64.dll`. Instead of fixing that error, remove the pcre usage :)
* Add logutils module doc
* Shorten logutils.formatIt for `NBytes`
Both json and textlines formatIt were not needed, and could be combined into one formatIt
* remove debug integration test config
debug output and logformat of json for integration test logs
* Use ## module doc to support docgen
* bump nim-poseidon2 to export fromBytes
Before the changes in this branch, fromBytes was likely being resolved by nim-stew, or other dependency. With the changes in this branch, that dependency was removed and fromBytes could no longer be resolved. By exporting fromBytes from nim-poseidon, the correct resolution is now happening.
* fixes to get compiling after rebasing master
* Add support for Result types being logged using formatIt
2024-01-23 07:35:03 +00:00
|
|
|
export codex, conf, libp2p, chronos, logutils
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2022-12-05 15:00:02 +00:00
|
|
|
import std/sequtils
|
2022-01-10 15:32:56 +00:00
|
|
|
import std/os
|
|
|
|
import pkg/confutils/defs
|
2022-05-19 19:56:03 +00:00
|
|
|
import ./codex/utils/fileutils
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-11-01 20:05:40 +00:00
|
|
|
logScope:
|
|
|
|
topics = "codex"
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
when defined(posix):
|
|
|
|
import system/ansi_c
|
|
|
|
|
2022-12-05 15:00:02 +00:00
|
|
|
type
|
|
|
|
CodexStatus {.pure.} = enum
|
|
|
|
Stopped,
|
|
|
|
Stopping,
|
|
|
|
Running
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
let config = CodexConf.load(
|
2023-05-02 13:06:34 +00:00
|
|
|
version = codexFullVersion,
|
|
|
|
envVarsPrefix = "codex",
|
2024-12-18 15:36:55 +00:00
|
|
|
secondarySources = proc (config: CodexConf, sources: auto) {.gcsafe, raises: [ConfigurationError].} =
|
2023-05-02 13:06:34 +00:00
|
|
|
if configFile =? config.configFile:
|
|
|
|
sources.addConfigFile(Toml, configFile)
|
2022-04-14 10:49:03 +00:00
|
|
|
)
|
2022-04-12 13:21:07 +00:00
|
|
|
config.setupLogging()
|
2022-04-14 10:49:03 +00:00
|
|
|
config.setupMetrics()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-12-18 16:17:31 +00:00
|
|
|
if config.nat == IPv4_any():
|
2024-03-12 09:57:13 +00:00
|
|
|
error "`--nat` cannot be set to the any (`0.0.0.0`) address"
|
|
|
|
quit QuitFailure
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-12-18 16:17:31 +00:00
|
|
|
if config.nat == static parseIpAddress("127.0.0.1"):
|
2024-03-12 09:57:13 +00:00
|
|
|
warn "`--nat` is set to loopback, your node wont properly announce over the DHT"
|
2022-11-02 00:58:41 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
if not(checkAndCreateDataDir((config.dataDir).string)):
|
|
|
|
# We are unable to access/create data folder or data folder's
|
|
|
|
# permissions are insecure.
|
|
|
|
quit QuitFailure
|
2022-11-02 00:58:41 +00:00
|
|
|
|
2024-09-23 14:37:17 +00:00
|
|
|
if config.prover() and not(checkAndCreateDataDir((config.circuitDir).string)):
|
|
|
|
quit QuitFailure
|
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
trace "Data dir initialized", dir = $config.dataDir
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
if not(checkAndCreateDataDir((config.dataDir / "repo"))):
|
|
|
|
# We are unable to access/create data folder or data folder's
|
|
|
|
# permissions are insecure.
|
|
|
|
quit QuitFailure
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
trace "Repo dir initialized", dir = config.dataDir / "repo"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
var
|
|
|
|
state: CodexStatus
|
|
|
|
shutdown: Future[void]
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
let
|
|
|
|
keyPath =
|
|
|
|
if isAbsolute(config.netPrivKeyFile):
|
|
|
|
config.netPrivKeyFile
|
|
|
|
else:
|
|
|
|
config.dataDir / config.netPrivKeyFile
|
2022-12-05 15:00:02 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
privateKey = setupKey(keyPath).expect("Should setup private key!")
|
|
|
|
server = try:
|
|
|
|
CodexServer.new(config, privateKey)
|
|
|
|
except Exception as exc:
|
|
|
|
error "Failed to start Codex", msg = exc.msg
|
|
|
|
quit QuitFailure
|
2022-11-02 00:58:41 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
## Ctrl+C handling
|
|
|
|
proc doShutdown() =
|
|
|
|
shutdown = server.stop()
|
|
|
|
state = CodexStatus.Stopping
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
notice "Stopping Codex"
|
2024-03-11 12:31:17 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
proc controlCHandler() {.noconv.} =
|
|
|
|
when defined(windows):
|
|
|
|
# workaround for https://github.com/nim-lang/Nim/issues/4057
|
|
|
|
try:
|
|
|
|
setupForeignThreadGc()
|
|
|
|
except Exception as exc: raiseAssert exc.msg # shouldn't happen
|
|
|
|
notice "Shutting down after having received SIGINT"
|
2024-03-11 12:31:17 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
doShutdown()
|
2022-12-05 15:00:02 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
try:
|
|
|
|
setControlCHook(controlCHandler)
|
|
|
|
except Exception as exc: # TODO Exception
|
|
|
|
warn "Cannot set ctrl-c handler", msg = exc.msg
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
# equivalent SIGTERM handler
|
|
|
|
when defined(posix):
|
|
|
|
proc SIGTERMHandler(signal: cint) {.noconv.} =
|
|
|
|
notice "Shutting down after having received SIGTERM"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
doShutdown()
|
2022-12-05 15:00:02 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
c_signal(ansi_c.SIGTERM, SIGTERMHandler)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
try:
|
|
|
|
waitFor server.start()
|
|
|
|
except CatchableError as error:
|
|
|
|
error "Codex failed to start", error = error.msg
|
|
|
|
# XXX ideally we'd like to issue a stop instead of quitting cold turkey,
|
|
|
|
# but this would mean we'd have to fix the implementation of all
|
|
|
|
# services so they won't crash if we attempt to stop them before they
|
|
|
|
# had a chance to start (currently you'll get a SISGSEV if you try to).
|
|
|
|
quit QuitFailure
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
state = CodexStatus.Running
|
|
|
|
while state == CodexStatus.Running:
|
2024-03-11 12:31:17 +00:00
|
|
|
try:
|
2022-12-05 15:00:02 +00:00
|
|
|
# poll chronos
|
|
|
|
chronos.poll()
|
2024-03-12 09:57:13 +00:00
|
|
|
except Exception as exc:
|
|
|
|
error "Unhandled exception in async proc, aborting", msg = exc.msg
|
2022-12-05 15:00:02 +00:00
|
|
|
quit QuitFailure
|
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
try:
|
|
|
|
# signal handlers guarantee that the shutdown Future will
|
|
|
|
# be assigned before state switches to Stopping
|
|
|
|
waitFor shutdown
|
|
|
|
except CatchableError as error:
|
|
|
|
error "Codex didn't shutdown correctly", error = error.msg
|
|
|
|
quit QuitFailure
|
2022-11-15 15:46:21 +00:00
|
|
|
|
2024-03-12 09:57:13 +00:00
|
|
|
notice "Exited codex"
|