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/chronicles
|
|
|
|
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
|
2022-11-02 00:58:41 +00:00
|
|
|
import ./codex/utils/keyutils
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
export codex, conf, libp2p, chronos, chronicles
|
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",
|
|
|
|
secondarySources = proc (config: CodexConf, sources: auto) =
|
|
|
|
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
|
|
|
|
|
|
|
case config.cmd:
|
|
|
|
of StartUpCommand.noCommand:
|
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
if config.nat == ValidIpAddress.init(IPv4_any()):
|
|
|
|
error "`--nat` cannot be set to the any (`0.0.0.0`) address"
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
if config.nat == ValidIpAddress.init("127.0.0.1"):
|
2022-11-15 15:46:21 +00:00
|
|
|
warn "`--nat` is set to loopback, your node wont properly announce over the DHT"
|
2022-11-02 00:58:41 +00:00
|
|
|
|
2022-01-10 15:32:56 +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-04-14 10:49:03 +00:00
|
|
|
trace "Data dir initialized", dir = $config.dataDir
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2023-03-09 11:23:45 +00:00
|
|
|
if not(checkAndCreateDataDir((config.dataDir / "repo"))):
|
2022-01-10 15:32:56 +00:00
|
|
|
# We are unable to access/create data folder or data folder's
|
|
|
|
# permissions are insecure.
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
trace "Repo dir initialized", dir = config.dataDir / "repo"
|
|
|
|
|
2022-12-05 15:00:02 +00:00
|
|
|
var
|
|
|
|
state: CodexStatus
|
|
|
|
pendingFuts: seq[Future[void]]
|
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
let
|
|
|
|
keyPath =
|
2023-03-09 11:23:45 +00:00
|
|
|
if isAbsolute(config.netPrivKeyFile):
|
|
|
|
config.netPrivKeyFile
|
2022-11-02 00:58:41 +00:00
|
|
|
else:
|
2023-03-09 11:23:45 +00:00
|
|
|
config.dataDir / config.netPrivKeyFile
|
2022-11-02 00:58:41 +00:00
|
|
|
|
|
|
|
privateKey = setupKey(keyPath).expect("Should setup private key!")
|
|
|
|
server = CodexServer.new(config, privateKey)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
## Ctrl+C handling
|
|
|
|
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"
|
2022-12-05 15:00:02 +00:00
|
|
|
|
|
|
|
pendingFuts.add(server.stop())
|
|
|
|
state = CodexStatus.Stopping
|
|
|
|
|
|
|
|
notice "Stopping Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
setControlCHook(controlCHandler)
|
|
|
|
except Exception as exc: # TODO Exception
|
|
|
|
warn "Cannot set ctrl-c handler", msg = exc.msg
|
|
|
|
|
|
|
|
# equivalent SIGTERM handler
|
|
|
|
when defined(posix):
|
|
|
|
proc SIGTERMHandler(signal: cint) {.noconv.} =
|
|
|
|
notice "Shutting down after having received SIGTERM"
|
2022-12-05 15:00:02 +00:00
|
|
|
|
|
|
|
pendingFuts.add(server.stop())
|
|
|
|
state = CodexStatus.Stopping
|
|
|
|
|
|
|
|
notice "Stopping Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-11-02 17:40:28 +00:00
|
|
|
c_signal(ansi_c.SIGTERM, SIGTERMHandler)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-12-05 15:00:02 +00:00
|
|
|
pendingFuts.add(server.start())
|
|
|
|
|
|
|
|
state = CodexStatus.Running
|
|
|
|
while state == CodexStatus.Running:
|
|
|
|
# poll chronos
|
|
|
|
chronos.poll()
|
|
|
|
|
|
|
|
# wait fot futures to finish
|
|
|
|
let res = waitFor allFinished(pendingFuts)
|
|
|
|
state = CodexStatus.Stopped
|
|
|
|
|
|
|
|
if res.anyIt( it.failed ):
|
|
|
|
error "Codex didn't shutdown correctly"
|
|
|
|
quit QuitFailure
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
notice "Exited codex"
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
of StartUpCommand.initNode:
|
|
|
|
discard
|