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
|
|
|
|
2023-02-15 19:40:21 +00:00
|
|
|
import pkg/chronos
|
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-12-03 00:00:55 +00:00
|
|
|
import ./stores
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2023-03-08 15:04:54 +00:00
|
|
|
export DefaultCacheSizeMiB, net, DefaultQuotaBytes, DefaultBlockTtl, DefaultBlockMaintenanceInterval, DefaultNumberOfBlocksToMaintainPerInterval
|
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"
|
|
|
|
|
2023-03-14 22:32:15 +00:00
|
|
|
RepoKind* = enum
|
|
|
|
repoFS = "fs"
|
|
|
|
repoSQLite = "sqlite"
|
|
|
|
|
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-12-03 00:00:55 +00:00
|
|
|
# TODO: change this once we integrate nat support
|
2022-11-02 00:58:41 +00:00
|
|
|
nat* {.
|
|
|
|
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"
|
2022-11-15 19:16:02 +00:00
|
|
|
abbr: "e"
|
2022-11-02 00:58:41 +00:00
|
|
|
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-11-15 19:16:02 +00:00
|
|
|
defaultValue: 8090.Port
|
2022-04-13 16:32:35 +00:00
|
|
|
defaultValueDesc: "8090"
|
2022-11-15 19:16:02 +00:00
|
|
|
abbr: "u"
|
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
|
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
apiBindAddress* {.
|
|
|
|
desc: "The REST API bind address"
|
|
|
|
defaultValue: "127.0.0.1"
|
|
|
|
name: "api-bindaddr"
|
|
|
|
}: string
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
apiPort* {.
|
|
|
|
desc: "The REST Api port",
|
|
|
|
defaultValue: 8080
|
|
|
|
defaultValueDesc: "8080"
|
|
|
|
name: "api-port"
|
|
|
|
abbr: "p" }: int
|
|
|
|
|
2023-03-14 22:32:15 +00:00
|
|
|
repoKind* {.
|
|
|
|
desc: "backend for main repo store (fs, sqlite)"
|
|
|
|
defaultValueDesc: "fs"
|
|
|
|
defaultValue: repoFS
|
|
|
|
name: "repo-kind" }: RepoKind
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
storageQuota* {.
|
|
|
|
desc: "The size of the total storage quota dedicated to the node"
|
|
|
|
defaultValue: DefaultQuotaBytes
|
|
|
|
defaultValueDesc: $DefaultQuotaBytes
|
|
|
|
name: "storage-quota"
|
|
|
|
abbr: "q" }: Natural
|
|
|
|
|
2023-03-08 15:04:54 +00:00
|
|
|
blockTtlSeconds* {.
|
2022-12-03 00:00:55 +00:00
|
|
|
desc: "Default block timeout in seconds - 0 disables the ttl"
|
2023-03-08 15:04:54 +00:00
|
|
|
defaultValue: DefaultBlockTtl.seconds
|
|
|
|
defaultValueDesc: $DefaultBlockTtl
|
2022-12-03 00:00:55 +00:00
|
|
|
name: "block-ttl"
|
2023-03-08 15:04:54 +00:00
|
|
|
abbr: "t" }: int
|
|
|
|
|
|
|
|
blockMaintenanceIntervalSeconds* {.
|
|
|
|
desc: "Time interval in seconds - determines frequency of block maintenance cycle: how often blocks are checked for expiration and cleanup."
|
|
|
|
defaultValue: DefaultBlockMaintenanceInterval.seconds
|
|
|
|
defaultValueDesc: $DefaultBlockMaintenanceInterval
|
|
|
|
name: "block-mi" }: int
|
|
|
|
|
|
|
|
blockMaintenanceNumberOfBlocks* {.
|
|
|
|
desc: "Number of blocks to check every maintenance cycle."
|
|
|
|
defaultValue: DefaultNumberOfBlocksToMaintainPerInterval
|
|
|
|
defaultValueDesc: $DefaultNumberOfBlocksToMaintainPerInterval
|
|
|
|
name: "block-mn" }: int
|
2022-12-03 00:00:55 +00:00
|
|
|
|
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
|
|
|
|
2023-04-19 13:06:00 +00:00
|
|
|
validator* {.
|
|
|
|
desc: "Enables validator, requires an Ethereum node"
|
|
|
|
defaultValue: false
|
|
|
|
name: "validator"
|
|
|
|
.}: bool
|
|
|
|
|
|
|
|
validatorMaxSlots* {.
|
|
|
|
desc: "Maximum number of slots that the validator monitors"
|
|
|
|
defaultValue: 1000
|
|
|
|
name: "validator-max-slots"
|
|
|
|
.}: int
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
of initNode:
|
|
|
|
discard
|
|
|
|
|
2022-04-14 16:20:01 +00:00
|
|
|
EthAddress* = ethers.Address
|
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
proc getCodexVersion(): string =
|
|
|
|
let tag = strip(staticExec("git tag"))
|
|
|
|
if tag.isEmptyOrWhitespace:
|
|
|
|
return "untagged build"
|
|
|
|
return tag
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
proc getCodexRevision(): string =
|
|
|
|
strip(staticExec("git rev-parse --short HEAD"))[0..5]
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
proc getNimBanner(): string =
|
|
|
|
staticExec("nim --version | grep Version")
|
|
|
|
|
|
|
|
const
|
|
|
|
codexVersion* = getCodexVersion()
|
|
|
|
codexRevision* = getCodexRevision()
|
|
|
|
nimBanner* = getNimBanner()
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
codexFullVersion* =
|
2023-03-08 11:45:55 +00:00
|
|
|
"Codex version: " & codexVersion & "\p" &
|
|
|
|
"Codex revision: " & codexRevision & "\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
|
|
|
|
|
2023-03-09 11:23:45 +00:00
|
|
|
func parseCmdArg*(T: type MultiAddress, input: string): T
|
2022-01-10 15:32:56 +00:00
|
|
|
{.raises: [ValueError, LPError, Defect].} =
|
|
|
|
MultiAddress.init($input).tryGet()
|
2022-04-12 13:21:07 +00:00
|
|
|
|
2023-03-09 11:23:45 +00:00
|
|
|
proc parseCmdArg*(T: type SignedPeerRecord, uri: string): T =
|
2022-04-13 16:32:35 +00:00
|
|
|
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
|
|
|
|
|
2023-03-09 11:23:45 +00:00
|
|
|
func parseCmdArg*(T: type EthAddress, address: string): T =
|
2022-04-14 16:20:01 +00:00
|
|
|
EthAddress.init($address).get()
|
|
|
|
|
|
|
|
# no idea why confutils needs this:
|
2023-03-09 11:23:45 +00:00
|
|
|
proc completeCmdArg*(T: type EthAddress; val: string): seq[string] =
|
2022-04-14 16:20:01 +00:00
|
|
|
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
|