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.
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import std/os
|
2022-04-12 13:21:07 +00:00
|
|
|
import std/terminal
|
2022-01-10 15:32:56 +00:00
|
|
|
import std/options
|
2022-04-14 10:49:03 +00:00
|
|
|
import std/strutils
|
2022-04-12 13:21:07 +00:00
|
|
|
import std/typetraits
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import pkg/chronicles
|
2022-11-01 20:05:40 +00:00
|
|
|
import pkg/chronicles/helpers
|
2022-04-12 13:21:07 +00:00
|
|
|
import pkg/chronicles/topics_registry
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/confutils/defs
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/confutils/std/net
|
2022-04-14 10:49:03 +00:00
|
|
|
import pkg/metrics
|
|
|
|
import pkg/metrics/chronos_httpserver
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/stew/shims/net as stewnet
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/libp2p
|
2022-04-14 16:20:01 +00:00
|
|
|
import pkg/ethers
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
import ./discovery
|
2022-03-02 16:30:42 +00:00
|
|
|
import ./stores/cachestore
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
export DefaultCacheSizeMiB, net
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
StartUpCommand* {.pure.} = enum
|
|
|
|
noCommand,
|
|
|
|
initNode
|
|
|
|
|
2022-04-12 13:21:07 +00:00
|
|
|
LogKind* = enum
|
|
|
|
Auto = "auto"
|
|
|
|
Colors = "colors"
|
|
|
|
NoColors = "nocolors"
|
|
|
|
Json = "json"
|
|
|
|
None = "none"
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
CodexConf* = object
|
2022-01-10 15:32:56 +00:00
|
|
|
logLevel* {.
|
2022-11-01 20:05:40 +00:00
|
|
|
defaultValue: "INFO"
|
2022-04-12 13:21:07 +00:00
|
|
|
desc: "Sets the log level",
|
2022-11-01 20:05:40 +00:00
|
|
|
name: "log-level" }: string
|
2022-04-12 13:21:07 +00:00
|
|
|
|
|
|
|
logFormat* {.
|
|
|
|
hidden
|
|
|
|
desc: "Specifies what kind of logs should be written to stdout (auto, colors, nocolors, json)"
|
|
|
|
defaultValueDesc: "auto"
|
|
|
|
defaultValue: LogKind.Auto
|
|
|
|
name: "log-format" }: LogKind
|
|
|
|
|
2022-04-14 10:49:03 +00:00
|
|
|
metricsEnabled* {.
|
|
|
|
desc: "Enable the metrics server"
|
|
|
|
defaultValue: false
|
|
|
|
name: "metrics" }: bool
|
|
|
|
|
|
|
|
metricsAddress* {.
|
|
|
|
desc: "Listening address of the metrics server"
|
|
|
|
defaultValue: ValidIpAddress.init("127.0.0.1")
|
|
|
|
defaultValueDesc: "127.0.0.1"
|
|
|
|
name: "metrics-address" }: ValidIpAddress
|
|
|
|
|
|
|
|
metricsPort* {.
|
|
|
|
desc: "Listening HTTP port of the metrics server"
|
|
|
|
defaultValue: 8008
|
|
|
|
name: "metrics-port" }: Port
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
dataDir* {.
|
2022-05-19 19:56:03 +00:00
|
|
|
desc: "The directory where codex will store configuration and data."
|
2022-01-10 15:32:56 +00:00
|
|
|
defaultValue: defaultDataDir()
|
|
|
|
defaultValueDesc: ""
|
|
|
|
abbr: "d"
|
|
|
|
name: "data-dir" }: OutDir
|
|
|
|
|
|
|
|
case cmd* {.
|
|
|
|
command
|
|
|
|
defaultValue: noCommand }: StartUpCommand
|
|
|
|
|
|
|
|
of noCommand:
|
2022-10-27 13:44:56 +00:00
|
|
|
listenAddrs* {.
|
|
|
|
desc: "Multi Addresses to listen on"
|
|
|
|
defaultValue: @[
|
|
|
|
MultiAddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
.expect("Should init multiaddress")]
|
|
|
|
defaultValueDesc: "/ip4/0.0.0.0/tcp/0"
|
2022-04-13 16:32:35 +00:00
|
|
|
abbr: "i"
|
2022-10-27 13:44:56 +00:00
|
|
|
name: "listen-addrs" }: seq[MultiAddress]
|
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
nat* {.
|
|
|
|
# TODO: change this once we integrate nat support
|
|
|
|
desc: "IP Addresses to announce behind a NAT"
|
|
|
|
defaultValue: ValidIpAddress.init("127.0.0.1")
|
|
|
|
defaultValueDesc: "127.0.0.1"
|
2022-10-27 13:44:56 +00:00
|
|
|
abbr: "a"
|
2022-11-02 00:58:41 +00:00
|
|
|
name: "nat" }: ValidIpAddress
|
|
|
|
|
|
|
|
discoveryIp* {.
|
|
|
|
desc: "Discovery listen address"
|
|
|
|
defaultValue: ValidIpAddress.init(IPv4_any())
|
|
|
|
defaultValueDesc: "0.0.0.0"
|
|
|
|
name: "disc-ip" }: ValidIpAddress
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
discoveryPort* {.
|
2022-11-02 00:58:41 +00:00
|
|
|
desc: "Discovery (UDP) port"
|
2022-04-13 16:32:35 +00:00
|
|
|
defaultValue: Port(8090)
|
|
|
|
defaultValueDesc: "8090"
|
2022-11-02 00:58:41 +00:00
|
|
|
name: "disc-port" }: Port
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
netPrivKeyFile* {.
|
2022-11-02 00:58:41 +00:00
|
|
|
desc: "Source of network (secp256k1) private key file path or name"
|
|
|
|
defaultValue: "key"
|
2022-04-13 16:32:35 +00:00
|
|
|
name: "net-privkey" }: string
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
bootstrapNodes* {.
|
|
|
|
desc: "Specifies one or more bootstrap nodes to use when connecting to the network."
|
|
|
|
abbr: "b"
|
2022-04-14 10:49:03 +00:00
|
|
|
name: "bootstrap-node" }: seq[SignedPeerRecord]
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
maxPeers* {.
|
|
|
|
desc: "The maximum number of peers to connect to"
|
|
|
|
defaultValue: 160
|
|
|
|
name: "max-peers" }: int
|
|
|
|
|
|
|
|
agentString* {.
|
2022-05-19 19:56:03 +00:00
|
|
|
defaultValue: "Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
desc: "Node agent string which is used as identifier in network"
|
|
|
|
name: "agent-string" }: string
|
|
|
|
|
|
|
|
apiPort* {.
|
|
|
|
desc: "The REST Api port",
|
|
|
|
defaultValue: 8080
|
|
|
|
defaultValueDesc: "8080"
|
|
|
|
name: "api-port"
|
|
|
|
abbr: "p" }: int
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
cacheSize* {.
|
2022-11-15 15:46:21 +00:00
|
|
|
desc: "The size in MiB of the block cache, 0 disables the cache - might help on slow hardrives"
|
|
|
|
defaultValue: 0
|
|
|
|
defaultValueDesc: "0"
|
2022-03-02 16:30:42 +00:00
|
|
|
name: "cache-size"
|
|
|
|
abbr: "c" }: Natural
|
|
|
|
|
2022-08-09 04:29:06 +00:00
|
|
|
persistence* {.
|
|
|
|
desc: "Enables persistence mechanism, requires an Ethereum node"
|
|
|
|
defaultValue: false
|
|
|
|
name: "persistence"
|
|
|
|
.}: bool
|
|
|
|
|
2022-04-14 16:20:01 +00:00
|
|
|
ethProvider* {.
|
|
|
|
desc: "The URL of the JSON-RPC API of the Ethereum node"
|
|
|
|
defaultValue: "ws://localhost:8545"
|
|
|
|
name: "eth-provider"
|
|
|
|
.}: string
|
|
|
|
|
|
|
|
ethAccount* {.
|
|
|
|
desc: "The Ethereum account that is used for storage contracts"
|
2022-08-09 04:29:06 +00:00
|
|
|
defaultValue: EthAddress.none
|
2022-04-14 16:20:01 +00:00
|
|
|
name: "eth-account"
|
2022-08-09 04:29:06 +00:00
|
|
|
.}: Option[EthAddress]
|
2022-04-14 16:20:01 +00:00
|
|
|
|
2022-04-25 13:12:37 +00:00
|
|
|
ethDeployment* {.
|
|
|
|
desc: "The json file describing the contract deployment"
|
2022-08-09 04:29:06 +00:00
|
|
|
defaultValue: string.none
|
2022-04-25 13:12:37 +00:00
|
|
|
name: "eth-deployment"
|
2022-08-09 04:29:06 +00:00
|
|
|
.}: Option[string]
|
2022-04-25 13:12:37 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
of initNode:
|
|
|
|
discard
|
|
|
|
|
2022-04-14 16:20:01 +00:00
|
|
|
EthAddress* = ethers.Address
|
|
|
|
|
2022-04-14 10:49:03 +00:00
|
|
|
const
|
|
|
|
gitRevision* = strip(staticExec("git rev-parse --short HEAD"))[0..5]
|
|
|
|
|
|
|
|
nimBanner* = staticExec("nim --version | grep Version")
|
|
|
|
|
|
|
|
#TODO add versionMajor, Minor & Fix when we switch to semver
|
2022-05-19 19:56:03 +00:00
|
|
|
codexVersion* = gitRevision
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
codexFullVersion* =
|
|
|
|
"Codex build " & codexVersion & "\p" &
|
2022-04-14 10:49:03 +00:00
|
|
|
nimBanner
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc defaultDataDir*(): string =
|
|
|
|
let dataDir = when defined(windows):
|
2022-05-19 19:56:03 +00:00
|
|
|
"AppData" / "Roaming" / "Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
elif defined(macosx):
|
2022-05-19 19:56:03 +00:00
|
|
|
"Library" / "Application Support" / "Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
else:
|
2022-05-19 19:56:03 +00:00
|
|
|
".cache" / "codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
getHomeDir() / dataDir
|
|
|
|
|
|
|
|
func parseCmdArg*(T: type MultiAddress, input: TaintedString): T
|
|
|
|
{.raises: [ValueError, LPError, Defect].} =
|
|
|
|
MultiAddress.init($input).tryGet()
|
2022-04-12 13:21:07 +00:00
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
proc parseCmdArg*(T: type SignedPeerRecord, uri: TaintedString): T =
|
|
|
|
var res: SignedPeerRecord
|
|
|
|
try:
|
|
|
|
if not res.fromURI(uri):
|
|
|
|
warn "Invalid SignedPeerRecord uri", uri=uri
|
|
|
|
quit QuitFailure
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "Invalid SignedPeerRecord uri", uri=uri, error=exc.msg
|
|
|
|
quit QuitFailure
|
|
|
|
res
|
|
|
|
|
2022-04-14 16:20:01 +00:00
|
|
|
func parseCmdArg*(T: type EthAddress, address: TaintedString): T =
|
|
|
|
EthAddress.init($address).get()
|
|
|
|
|
|
|
|
# no idea why confutils needs this:
|
|
|
|
proc completeCmdArg*(T: type EthAddress; val: TaintedString): seq[string] =
|
|
|
|
discard
|
|
|
|
|
2022-04-12 13:21:07 +00:00
|
|
|
# silly chronicles, colors is a compile-time property
|
|
|
|
proc stripAnsi(v: string): string =
|
|
|
|
var
|
|
|
|
res = newStringOfCap(v.len)
|
|
|
|
i: int
|
|
|
|
|
|
|
|
while i < v.len:
|
|
|
|
let c = v[i]
|
|
|
|
if c == '\x1b':
|
|
|
|
var
|
|
|
|
x = i + 1
|
|
|
|
found = false
|
|
|
|
|
|
|
|
while x < v.len: # look for [..m
|
|
|
|
let c2 = v[x]
|
|
|
|
if x == i + 1:
|
|
|
|
if c2 != '[':
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if c2 in {'0'..'9'} + {';'}:
|
|
|
|
discard # keep looking
|
|
|
|
elif c2 == 'm':
|
|
|
|
i = x + 1
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
inc x
|
|
|
|
|
|
|
|
if found: # skip adding c
|
|
|
|
continue
|
|
|
|
res.add c
|
|
|
|
inc i
|
|
|
|
|
|
|
|
res
|
|
|
|
|
2022-11-01 20:05:40 +00:00
|
|
|
proc updateLogLevel*(logLevel: string) {.raises: [Defect, ValueError].} =
|
|
|
|
# Updates log levels (without clearing old ones)
|
|
|
|
let directives = logLevel.split(";")
|
|
|
|
try:
|
|
|
|
setLogLevel(parseEnum[LogLevel](directives[0]))
|
|
|
|
except ValueError:
|
|
|
|
raise (ref ValueError)(msg: "Please specify one of TRACE, DEBUG, INFO, NOTICE, WARN, ERROR or FATAL")
|
|
|
|
|
|
|
|
if directives.len > 1:
|
|
|
|
for topicName, settings in parseTopicDirectives(directives[1..^1]):
|
|
|
|
if not setTopicState(topicName, settings.state, settings.logLevel):
|
|
|
|
warn "Unrecognized logging topic", topic = topicName
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc setupLogging*(conf: CodexConf) =
|
2022-04-12 13:21:07 +00:00
|
|
|
when defaultChroniclesStream.outputs.type.arity != 2:
|
|
|
|
warn "Logging configuration options not enabled in the current build"
|
|
|
|
else:
|
|
|
|
proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard
|
|
|
|
proc writeAndFlush(f: File, msg: LogOutputStr) =
|
|
|
|
try:
|
|
|
|
f.write(msg)
|
|
|
|
f.flushFile()
|
|
|
|
except IOError as err:
|
|
|
|
logLoggingFailure(cstring(msg), err)
|
|
|
|
|
|
|
|
proc stdoutFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
writeAndFlush(stdout, msg)
|
|
|
|
|
|
|
|
proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
writeAndFlush(stdout, stripAnsi(msg))
|
|
|
|
|
|
|
|
defaultChroniclesStream.outputs[1].writer = noOutput
|
|
|
|
|
|
|
|
defaultChroniclesStream.outputs[0].writer =
|
|
|
|
case conf.logFormat:
|
|
|
|
of LogKind.Auto:
|
|
|
|
if isatty(stdout):
|
|
|
|
stdoutFlush
|
|
|
|
else:
|
|
|
|
noColorsFlush
|
|
|
|
of LogKind.Colors: stdoutFlush
|
|
|
|
of LogKind.NoColors: noColorsFlush
|
|
|
|
of LogKind.Json:
|
|
|
|
defaultChroniclesStream.outputs[1].writer = stdoutFlush
|
|
|
|
noOutput
|
|
|
|
of LogKind.None:
|
|
|
|
noOutput
|
|
|
|
|
2022-11-01 20:05:40 +00:00
|
|
|
try:
|
|
|
|
updateLogLevel(conf.logLevel)
|
|
|
|
except ValueError as err:
|
|
|
|
try:
|
|
|
|
stderr.write "Invalid value for --log-level. " & err.msg & "\n"
|
|
|
|
except IOError:
|
|
|
|
echo "Invalid value for --log-level. " & err.msg
|
|
|
|
quit QuitFailure
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc setupMetrics*(config: CodexConf) =
|
2022-04-14 10:49:03 +00:00
|
|
|
if config.metricsEnabled:
|
|
|
|
let metricsAddress = config.metricsAddress
|
|
|
|
notice "Starting metrics HTTP server",
|
|
|
|
url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics"
|
|
|
|
try:
|
|
|
|
startMetricsHttpServer($metricsAddress, config.metricsPort)
|
|
|
|
except CatchableError as exc:
|
|
|
|
raiseAssert exc.msg
|
|
|
|
except Exception as exc:
|
|
|
|
raiseAssert exc.msg # TODO fix metrics
|