2023-11-23 10:44:57 +00:00
|
|
|
# Fluffy
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2021-2023 Status Research & Development GmbH
|
2021-06-16 19:18:45 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
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,
|
2023-08-04 11:10:09 +00:00
|
|
|
eth/keys, eth/p2p/discoveryv5/[enr, node, routing_table],
|
2022-05-12 16:04:37 +00:00
|
|
|
json_rpc/rpcproxy,
|
2022-12-27 14:57:10 +00:00
|
|
|
nimcrypto/hash,
|
|
|
|
stew/byteutils,
|
2023-08-04 11:10:09 +00:00
|
|
|
eth/net/nat, # must be late (compilation annoyance)
|
2023-04-19 15:01:01 +00:00
|
|
|
./logging,
|
2022-05-12 16:04:37 +00:00
|
|
|
./network/wire/portal_protocol_config
|
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
|
2023-11-10 18:38:11 +00:00
|
|
|
defaultListenAddress* = (static parseIpAddress("0.0.0.0"))
|
|
|
|
defaultAdminListenAddress* = (static parseIpAddress("127.0.0.1"))
|
2021-12-08 10:54:22 +00:00
|
|
|
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)
|
2023-11-10 16:16:15 +00:00
|
|
|
defaultStorageCapacity* = 2000'u32 # 2 GB default
|
|
|
|
defaultStorageCapacityDesc* = $defaultStorageCapacity
|
2021-09-29 11:58:55 +00:00
|
|
|
|
2023-10-20 12:30:21 +00:00
|
|
|
defaultTableIpLimitDesc* =
|
|
|
|
$defaultPortalProtocolConfig.tableIpLimits.tableIpLimit
|
|
|
|
defaultBucketIpLimitDesc* =
|
|
|
|
$defaultPortalProtocolConfig.tableIpLimits.bucketIpLimit
|
|
|
|
defaultBitsPerHopDesc* =
|
|
|
|
$defaultPortalProtocolConfig.bitsPerHop
|
2023-01-02 15:37:22 +00:00
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
type
|
2022-12-27 14:57:10 +00:00
|
|
|
TrustedDigest* = MDigest[32 * 8]
|
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
PortalCmd* = enum
|
|
|
|
noCommand
|
|
|
|
|
2022-03-17 17:39:24 +00:00
|
|
|
PortalNetwork* = enum
|
|
|
|
none
|
|
|
|
testnet0
|
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
PortalConf* = object
|
|
|
|
logLevel* {.
|
2023-04-19 15:01:01 +00:00
|
|
|
desc: "Sets the log level for process and topics (e.g. \"DEBUG; TRACE:discv5,portal_wire; REQUIRED:none; DISABLED:none\")"
|
|
|
|
defaultValue: "INFO"
|
|
|
|
name: "log-level" .}: string
|
|
|
|
|
|
|
|
logStdout* {.
|
|
|
|
hidden
|
|
|
|
desc: "Specifies what kind of logs should be written to stdout (auto, colors, nocolors, json)"
|
|
|
|
defaultValueDesc: "auto"
|
|
|
|
defaultValue: StdoutLogKind.Auto
|
|
|
|
name: "log-format" .}: StdoutLogKind
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
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"
|
2023-11-10 18:38:11 +00:00
|
|
|
name: "listen-address" .}: IpAddress
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2022-03-17 17:39:24 +00:00
|
|
|
portalNetwork* {.
|
|
|
|
desc:
|
2023-01-02 15:37:22 +00:00
|
|
|
"Select which Portal network to join. This will set the " &
|
|
|
|
"network specific bootstrap nodes automatically"
|
2023-06-28 21:53:27 +00:00
|
|
|
defaultValue: PortalNetwork.testnet0
|
2022-03-17 17:39:24 +00:00
|
|
|
name: "network" }: PortalNetwork
|
|
|
|
|
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-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
|
|
|
|
2022-02-17 13:13:39 +00:00
|
|
|
networkKeyFile* {.
|
|
|
|
desc: "Source of network (secp256k1) private key file"
|
|
|
|
defaultValue: config.dataDir / "netkey",
|
|
|
|
name: "netkey-file" }: string
|
|
|
|
|
|
|
|
networkKey* {.
|
|
|
|
hidden
|
|
|
|
desc: "Private key (secp256k1) for the p2p network, hex encoded.",
|
|
|
|
defaultValue: none(PrivateKey)
|
|
|
|
defaultValueDesc: "none"
|
|
|
|
name: "netkey-unsafe" .}: Option[PrivateKey]
|
|
|
|
|
2022-08-01 19:00:21 +00:00
|
|
|
accumulatorFile* {.
|
|
|
|
desc:
|
2022-10-17 18:38:51 +00:00
|
|
|
"Get the master accumulator snapshot from a file containing an " &
|
|
|
|
"pre-build SSZ encoded master accumulator."
|
2022-08-01 19:00:21 +00:00
|
|
|
defaultValue: none(InputFile)
|
|
|
|
defaultValueDesc: "none"
|
|
|
|
name: "accumulator-file" .}: Option[InputFile]
|
|
|
|
|
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"
|
2023-11-10 18:38:11 +00:00
|
|
|
name: "metrics-address" .}: IpAddress
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
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
|
2023-11-10 18:38:11 +00:00
|
|
|
name: "rpc-address" .}: IpAddress
|
2021-06-16 19:18:45 +00:00
|
|
|
|
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
|
2023-01-02 15:37:22 +00:00
|
|
|
desc: "URI of eth client where to proxy unimplemented JSON-RPC methods to"
|
2021-08-05 06:14:25 +00:00
|
|
|
name: "proxy-uri" .}: ClientConfig
|
2021-07-07 12:13:27 +00:00
|
|
|
|
2022-01-18 08:01:22 +00:00
|
|
|
tableIpLimit* {.
|
2023-10-20 12:30:21 +00:00
|
|
|
hidden
|
2023-01-02 15:37:22 +00:00
|
|
|
desc: "Maximum amount of nodes with the same IP in the routing table. " &
|
|
|
|
"This option is currently required as many nodes are running from " &
|
|
|
|
"the same machines. The option will be removed/adjusted in the future"
|
2023-10-20 12:30:21 +00:00
|
|
|
defaultValue: defaultPortalProtocolConfig.tableIpLimits.tableIpLimit
|
2023-01-02 15:37:22 +00:00
|
|
|
defaultValueDesc: $defaultTableIpLimitDesc
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "table-ip-limit" .}: uint
|
|
|
|
|
|
|
|
bucketIpLimit* {.
|
2023-10-20 12:30:21 +00:00
|
|
|
hidden
|
2023-01-02 15:37:22 +00:00
|
|
|
desc: "Maximum amount of nodes with the same IP in the routing table's buckets. " &
|
|
|
|
"This option is currently required as many nodes are running from " &
|
|
|
|
"the same machines. The option will be removed/adjusted in the future"
|
2023-10-20 12:30:21 +00:00
|
|
|
defaultValue: defaultPortalProtocolConfig.tableIpLimits.bucketIpLimit
|
2023-01-02 15:37:22 +00:00
|
|
|
defaultValueDesc: $defaultBucketIpLimitDesc
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "bucket-ip-limit" .}: uint
|
|
|
|
|
|
|
|
bitsPerHop* {.
|
|
|
|
hidden
|
|
|
|
desc: "Kademlia's b variable, increase for less hops per lookup"
|
2023-10-20 12:30:21 +00:00
|
|
|
defaultValue: defaultPortalProtocolConfig.bitsPerHop
|
|
|
|
defaultValueDesc: $defaultBitsPerHopDesc
|
2022-01-18 08:01:22 +00:00
|
|
|
name: "bits-per-hop" .}: int
|
|
|
|
|
2022-05-12 16:04:37 +00:00
|
|
|
radiusConfig* {.
|
2022-05-23 21:23:24 +00:00
|
|
|
desc: "Radius configuration for a fluffy node. Radius can be either `dynamic` " &
|
|
|
|
"where the node adjusts the radius based on `storage-size` option, " &
|
|
|
|
"or `static:<logRadius>` where the node has a hardcoded logarithmic radius value. " &
|
|
|
|
"Warning: `static:<logRadius>` disables `storage-size` limits and " &
|
|
|
|
"makes the node store a fraction of the network based on set radius."
|
2022-05-12 16:04:37 +00:00
|
|
|
defaultValue: defaultRadiusConfig
|
2022-05-23 21:23:24 +00:00
|
|
|
defaultValueDesc: $defaultRadiusConfigDesc
|
|
|
|
name: "radius" .}: RadiusConfig
|
2022-05-12 16:04:37 +00:00
|
|
|
|
2022-05-09 15:18:57 +00:00
|
|
|
# TODO maybe it is worth defining minimal storage size and throw error if
|
|
|
|
# value provided is smaller than minimum
|
2023-11-10 16:16:15 +00:00
|
|
|
storageCapacityMB* {.
|
|
|
|
desc: "Maximum amount (in megabytes) of content which will be stored " &
|
2022-05-23 21:23:24 +00:00
|
|
|
"in the local database."
|
2023-11-10 16:16:15 +00:00
|
|
|
defaultValue: defaultStorageCapacity
|
|
|
|
defaultValueDesc: $defaultStorageCapacityDesc
|
|
|
|
name: "storage-capacity" .}: uint64
|
2022-05-09 15:18:57 +00:00
|
|
|
|
2022-12-27 14:57:10 +00:00
|
|
|
trustedBlockRoot* {.
|
|
|
|
desc: "Recent trusted finalized block root to initialize the consensus light client from. " &
|
|
|
|
"If not provided by the user, portal light client will be disabled."
|
|
|
|
defaultValue: none(TrustedDigest)
|
|
|
|
name: "trusted-block-root" .}: Option[TrustedDigest]
|
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
forcePrune* {.
|
|
|
|
hidden
|
|
|
|
desc: "Force the pruning of the database. This should be used when the " &
|
|
|
|
"database is decreased in size, e.g. when a lower static radius " &
|
2023-11-23 10:44:57 +00:00
|
|
|
"or a lower storage capacity is set."
|
2023-11-21 15:16:15 +00:00
|
|
|
defaultValue: false
|
|
|
|
name: "force-prune" .}: bool
|
|
|
|
|
2023-08-30 08:01:00 +00:00
|
|
|
disablePoke* {.
|
|
|
|
hidden
|
|
|
|
desc: "Disable POKE functionality for gossip mechanisms testing"
|
|
|
|
defaultValue: defaultDisablePoke
|
|
|
|
defaultValueDesc: $defaultDisablePoke
|
|
|
|
name: "disable-poke" .}: bool
|
|
|
|
|
2021-06-16 19:18:45 +00:00
|
|
|
case cmd* {.
|
|
|
|
command
|
|
|
|
defaultValue: noCommand .}: PortalCmd
|
|
|
|
of noCommand:
|
|
|
|
discard
|
|
|
|
|
2022-12-27 14:57:10 +00:00
|
|
|
func parseCmdArg*(T: type TrustedDigest, input: string): T
|
2023-05-10 16:04:35 +00:00
|
|
|
{.raises: [ValueError].} =
|
2022-12-27 14:57:10 +00:00
|
|
|
TrustedDigest.fromHex(input)
|
|
|
|
|
|
|
|
func completeCmdArg*(T: type TrustedDigest, input: string): seq[string] =
|
|
|
|
return @[]
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type enr.Record, p: string): T
|
2023-08-04 12:43:30 +00:00
|
|
|
{.raises: [ValueError].} =
|
2021-06-16 19:18:45 +00:00
|
|
|
if not fromURI(result, p):
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "Invalid ENR")
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type enr.Record, val: string): seq[string] =
|
2021-06-16 19:18:45 +00:00
|
|
|
return @[]
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type Node, p: string): T
|
2023-08-04 12:43:30 +00:00
|
|
|
{.raises: [ValueError].} =
|
2021-06-16 19:18:45 +00:00
|
|
|
var record: enr.Record
|
|
|
|
if not fromURI(record, p):
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "Invalid ENR")
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
let n = newNode(record)
|
|
|
|
if n.isErr:
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, $n.error)
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
if n[].address.isNone():
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "ENR without address")
|
2021-06-16 19:18:45 +00:00
|
|
|
|
|
|
|
n[]
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type Node, val: string): seq[string] =
|
2021-06-16 19:18:45 +00:00
|
|
|
return @[]
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type PrivateKey, p: string): T
|
2023-08-04 12:43:30 +00:00
|
|
|
{.raises: [ValueError].} =
|
2021-06-16 19:18:45 +00:00
|
|
|
try:
|
2023-01-31 12:38:08 +00:00
|
|
|
result = PrivateKey.fromHex(p).tryGet()
|
2021-06-16 19:18:45 +00:00
|
|
|
except CatchableError:
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "Invalid private key")
|
2021-06-16 19:18:45 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type PrivateKey, val: string): seq[string] =
|
2021-06-16 19:18:45 +00:00
|
|
|
return @[]
|
2021-08-05 06:14:25 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type ClientConfig, p: string): T
|
2023-08-04 12:43:30 +00:00
|
|
|
{.raises: [ValueError].} =
|
2021-08-05 06:14:25 +00:00
|
|
|
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(
|
2023-08-04 12:43:30 +00:00
|
|
|
ValueError, "Proxy uri should have defined scheme (http/https/ws/wss)"
|
2021-08-05 06:14:25 +00:00
|
|
|
)
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type ClientConfig, val: string): seq[string] =
|
2021-08-05 06:14:25 +00:00
|
|
|
return @[]
|
2023-04-19 15:01:01 +00:00
|
|
|
|
|
|
|
chronicles.formatIt(InputDir): $it
|
|
|
|
chronicles.formatIt(OutDir): $it
|
|
|
|
chronicles.formatIt(InputFile): $it
|