2021-06-16 19:18:45 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2021-09-28 17:58:41 +00:00
|
|
|
std/os,
|
2021-08-05 06:14:25 +00:00
|
|
|
uri, confutils, confutils/std/net, chronicles,
|
|
|
|
eth/keys, eth/net/nat, eth/p2p/discoveryv5/[enr, node],
|
|
|
|
json_rpc/rpcproxy
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2021-09-29 11:58:55 +00:00
|
|
|
proc defaultDataDir*(): string =
|
|
|
|
let dataDir = when defined(windows):
|
|
|
|
"AppData" / "Roaming" / "Fluffy"
|
|
|
|
elif defined(macosx):
|
|
|
|
"Library" / "Application Support" / "Fluffy"
|
|
|
|
else:
|
|
|
|
".cache" / "fluffy"
|
|
|
|
|
|
|
|
getHomeDir() / dataDir
|
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
const
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultListenAddress* = (static ValidIpAddress.init("0.0.0.0"))
|
|
|
|
defaultAdminListenAddress* = (static ValidIpAddress.init("127.0.0.1"))
|
|
|
|
defaultProxyAddress* = (static "http://127.0.0.1:8546")
|
|
|
|
defaultClientConfig* = getHttpClientConfig(defaultProxyAddress)
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultListenAddressDesc = $defaultListenAddress
|
|
|
|
defaultAdminListenAddressDesc = $defaultAdminListenAddress
|
|
|
|
defaultDataDirDesc = defaultDataDir()
|
|
|
|
defaultClientConfigDesc = $(defaultClientConfig.httpUri)
|
2021-09-29 11:58:55 +00:00
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
type
|
|
|
|
PortalCmd* = enum
|
|
|
|
noCommand
|
2022-01-18 14:08:02 +00:00
|
|
|
populateHistoryDb
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
PortalConf* = object
|
|
|
|
logLevel* {.
|
|
|
|
defaultValue: LogLevel.DEBUG
|
2021-09-29 11:58:55 +00:00
|
|
|
defaultValueDesc: $LogLevel.DEBUG
|
2021-06-16 19:18:45 +00:00
|
|
|
desc: "Sets the log level"
|
|
|
|
name: "log-level" .}: LogLevel
|
|
|
|
|
|
|
|
udpPort* {.
|
|
|
|
defaultValue: 9009
|
|
|
|
desc: "UDP listening port"
|
|
|
|
name: "udp-port" .}: uint16
|
|
|
|
|
|
|
|
listenAddress* {.
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultValue: defaultListenAddress
|
|
|
|
defaultValueDesc: $defaultListenAddressDesc
|
2021-06-16 19:18:45 +00:00
|
|
|
desc: "Listening address for the Discovery v5 traffic"
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "listen-address" .}: ValidIpAddress
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2021-12-13 08:06:29 +00:00
|
|
|
# Note: This will add bootstrap nodes for both Discovery v5 network and each
|
|
|
|
# enabled Portal network. No distinction is made on bootstrap nodes per
|
|
|
|
# specific network.
|
2021-11-24 11:12:25 +00:00
|
|
|
bootstrapNodes* {.
|
2021-12-13 08:06:29 +00:00
|
|
|
desc: "ENR URI of node to bootstrap Discovery v5 and the Portal networks from. Argument may be repeated"
|
2021-11-24 11:12:25 +00:00
|
|
|
name: "bootstrap-node" .}: seq[Record]
|
|
|
|
|
|
|
|
bootstrapNodesFile* {.
|
2021-12-13 08:06:29 +00:00
|
|
|
desc: "Specifies a line-delimited file of ENR URIs to bootstrap Discovery v5 and Portal networks from"
|
2021-11-24 11:12:25 +00:00
|
|
|
defaultValue: ""
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "bootstrap-file" .}: InputFile
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
nat* {.
|
|
|
|
desc: "Specify method to use for determining public address. " &
|
|
|
|
"Must be one of: any, none, upnp, pmp, extip:<IP>"
|
|
|
|
defaultValue: NatConfig(hasExtIp: false, nat: NatAny)
|
2021-09-29 11:58:55 +00:00
|
|
|
defaultValueDesc: "any"
|
2021-06-16 19:18:45 +00:00
|
|
|
name: "nat" .}: NatConfig
|
|
|
|
|
|
|
|
enrAutoUpdate* {.
|
|
|
|
defaultValue: false
|
|
|
|
desc: "Discovery can automatically update its ENR with the IP address " &
|
|
|
|
"and UDP port as seen by other nodes it communicates with. " &
|
|
|
|
"This option allows to enable/disable this functionality"
|
|
|
|
name: "enr-auto-update" .}: bool
|
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
networkKey* {.
|
|
|
|
desc: "Private key (secp256k1) for the p2p network, hex encoded. Safer keyfile support to be added.",
|
2021-06-16 19:18:45 +00:00
|
|
|
defaultValue: PrivateKey.random(keys.newRng()[])
|
2021-09-29 11:58:55 +00:00
|
|
|
defaultValueDesc: "random"
|
2021-12-13 13:12:51 +00:00
|
|
|
name: "network-key-unsafe" .}: PrivateKey
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2021-09-28 17:58:41 +00:00
|
|
|
dataDir* {.
|
|
|
|
desc: "The directory where fluffy will store the content data"
|
2021-09-29 11:58:55 +00:00
|
|
|
defaultValue: defaultDataDir()
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultValueDesc: $defaultDataDirDesc
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "data-dir" .}: OutDir
|
2021-09-28 17:58:41 +00:00
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
metricsEnabled* {.
|
|
|
|
defaultValue: false
|
|
|
|
desc: "Enable the metrics server"
|
|
|
|
name: "metrics" .}: bool
|
|
|
|
|
|
|
|
metricsAddress* {.
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultValue: defaultAdminListenAddress
|
|
|
|
defaultValueDesc: $defaultAdminListenAddressDesc
|
2021-06-16 19:18:45 +00:00
|
|
|
desc: "Listening address of the metrics server"
|
|
|
|
name: "metrics-address" .}: ValidIpAddress
|
|
|
|
|
|
|
|
metricsPort* {.
|
|
|
|
defaultValue: 8008
|
|
|
|
desc: "Listening HTTP port of the metrics server"
|
|
|
|
name: "metrics-port" .}: Port
|
|
|
|
|
|
|
|
rpcEnabled* {.
|
|
|
|
desc: "Enable the JSON-RPC server"
|
|
|
|
defaultValue: false
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "rpc" .}: bool
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
rpcPort* {.
|
2021-12-13 13:12:51 +00:00
|
|
|
desc: "HTTP port for the JSON-RPC server"
|
2021-06-16 19:18:45 +00:00
|
|
|
defaultValue: 8545
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "rpc-port" .}: Port
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
rpcAddress* {.
|
|
|
|
desc: "Listening address of the RPC server"
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultValue: defaultAdminListenAddress
|
|
|
|
defaultValueDesc: $defaultAdminListenAddressDesc
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "rpc-address" .}: ValidIpAddress
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2021-07-02 05:30:48 +00:00
|
|
|
bridgeUri* {.
|
|
|
|
defaultValue: none(string)
|
2021-09-29 11:58:55 +00:00
|
|
|
defaultValueDesc: ""
|
2021-07-02 05:30:48 +00:00
|
|
|
desc: "if provided, enables getting data from bridge node"
|
|
|
|
name: "bridge-client-uri" .}: Option[string]
|
|
|
|
|
2021-07-07 12:13:27 +00:00
|
|
|
# it makes little sense to have default value here in final release, but until then
|
|
|
|
# it would be troublesome to add some fake uri param every time
|
|
|
|
proxyUri* {.
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultValue: defaultClientConfig
|
|
|
|
defaultValueDesc: $defaultClientConfigDesc
|
2021-09-29 11:58:55 +00:00
|
|
|
desc: "URI of eth client where to proxy unimplemented rpc methods to"
|
2021-08-05 06:14:25 +00:00
|
|
|
name: "proxy-uri" .}: ClientConfig
|
2021-07-07 12:13:27 +00:00
|
|
|
|
2022-02-15 12:11:27 +00:00
|
|
|
logRadius* {.
|
|
|
|
desc: "Hardcoded (logarithmic) radius for each Portal network. This is " &
|
|
|
|
"a temporary development option which will be replaced in the " &
|
|
|
|
"future by e.g. a storage size limit"
|
|
|
|
defaultValue: 256
|
|
|
|
name: "radius" .}: uint16
|
|
|
|
|
2022-01-18 08:01:22 +00:00
|
|
|
tableIpLimit* {.
|
|
|
|
hidden
|
|
|
|
desc: "Maximum amount of nodes with the same IP in the routing tables"
|
|
|
|
defaultValue: DefaultTableIpLimit
|
|
|
|
name: "table-ip-limit" .}: uint
|
|
|
|
|
|
|
|
bucketIpLimit* {.
|
|
|
|
hidden
|
|
|
|
desc: "Maximum amount of nodes with the same IP in the routing tables buckets"
|
|
|
|
defaultValue: DefaultBucketIpLimit
|
|
|
|
name: "bucket-ip-limit" .}: uint
|
|
|
|
|
|
|
|
bitsPerHop* {.
|
|
|
|
hidden
|
|
|
|
desc: "Kademlia's b variable, increase for less hops per lookup"
|
|
|
|
defaultValue: DefaultBitsPerHop
|
|
|
|
name: "bits-per-hop" .}: int
|
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
case cmd* {.
|
|
|
|
command
|
|
|
|
defaultValue: noCommand .}: PortalCmd
|
|
|
|
of noCommand:
|
|
|
|
discard
|
2022-01-18 14:08:02 +00:00
|
|
|
of populateHistoryDb:
|
|
|
|
# Note: we could use the existing data dir here, but it would require
|
|
|
|
# also to properly store the network key and default use the one available
|
|
|
|
dbDir* {.
|
|
|
|
desc: "The directory of the fluffy content database"
|
|
|
|
defaultValue: ""
|
|
|
|
name: "db-dir" }: OutDir
|
|
|
|
dataFile* {.
|
|
|
|
desc: "Specify a json file with a map of k:v pairs representing BlockHash : Rlp encoded block"
|
|
|
|
defaultValue: ""
|
|
|
|
name: "data-file" }: InputFile
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
proc parseCmdArg*(T: type enr.Record, p: TaintedString): T
|
|
|
|
{.raises: [Defect, ConfigurationError].} =
|
|
|
|
if not fromURI(result, p):
|
|
|
|
raise newException(ConfigurationError, "Invalid ENR")
|
|
|
|
|
|
|
|
proc completeCmdArg*(T: type enr.Record, val: TaintedString): seq[string] =
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type Node, p: TaintedString): T
|
|
|
|
{.raises: [Defect, ConfigurationError].} =
|
|
|
|
var record: enr.Record
|
|
|
|
if not fromURI(record, p):
|
|
|
|
raise newException(ConfigurationError, "Invalid ENR")
|
|
|
|
|
|
|
|
let n = newNode(record)
|
|
|
|
if n.isErr:
|
|
|
|
raise newException(ConfigurationError, $n.error)
|
|
|
|
|
|
|
|
if n[].address.isNone():
|
|
|
|
raise newException(ConfigurationError, "ENR without address")
|
|
|
|
|
|
|
|
n[]
|
|
|
|
|
|
|
|
proc completeCmdArg*(T: type Node, val: TaintedString): seq[string] =
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type PrivateKey, p: TaintedString): T
|
|
|
|
{.raises: [Defect, ConfigurationError].} =
|
|
|
|
try:
|
|
|
|
result = PrivateKey.fromHex(string(p)).tryGet()
|
|
|
|
except CatchableError:
|
|
|
|
raise newException(ConfigurationError, "Invalid private key")
|
|
|
|
|
|
|
|
proc completeCmdArg*(T: type PrivateKey, val: TaintedString): seq[string] =
|
|
|
|
return @[]
|
2021-08-05 06:14:25 +00:00
|
|
|
|
2022-01-18 08:01:22 +00:00
|
|
|
proc parseCmdArg*(T: type ClientConfig, p: TaintedString): T
|
2021-08-05 06:14:25 +00:00
|
|
|
{.raises: [Defect, ConfigurationError].} =
|
|
|
|
let uri = parseUri(p)
|
|
|
|
if (uri.scheme == "http" or uri.scheme == "https"):
|
|
|
|
getHttpClientConfig(p)
|
|
|
|
elif (uri.scheme == "ws" or uri.scheme == "wss"):
|
|
|
|
getWebSocketClientConfig(p)
|
|
|
|
else:
|
|
|
|
raise newException(
|
|
|
|
ConfigurationError, "Proxy uri should have defined scheme (http/https/ws/wss)"
|
|
|
|
)
|
|
|
|
|
|
|
|
proc completeCmdArg*(T: type ClientConfig, val: TaintedString): seq[string] =
|
|
|
|
return @[]
|