2022-11-23 16:11:38 +00:00
|
|
|
# nimbus_verified_proxy
|
2023-01-31 12:38:08 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-08-30 05:40:03 +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: [].}
|
2022-08-30 05:40:03 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/os,
|
2023-08-04 11:10:09 +00:00
|
|
|
json_rpc/rpcproxy, # must be early (compilation annoyance)
|
2022-08-30 05:40:03 +00:00
|
|
|
json_serialization/std/net,
|
2023-06-04 06:00:50 +00:00
|
|
|
beacon_chain/conf_light_client,
|
2023-08-04 11:10:09 +00:00
|
|
|
beacon_chain/conf
|
2022-08-30 05:40:03 +00:00
|
|
|
|
|
|
|
export net, conf
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
proc defaultVerifiedProxyDataDir*(): string =
|
2022-08-30 05:40:03 +00:00
|
|
|
let dataDir = when defined(windows):
|
2022-11-23 16:11:38 +00:00
|
|
|
"AppData" / "Roaming" / "NimbusVerifiedProxy"
|
2022-08-30 05:40:03 +00:00
|
|
|
elif defined(macosx):
|
2022-11-23 16:11:38 +00:00
|
|
|
"Library" / "Application Support" / "NimbusVerifiedProxy"
|
2022-08-30 05:40:03 +00:00
|
|
|
else:
|
2022-11-23 16:11:38 +00:00
|
|
|
".cache" / "nimbus-verified-proxy"
|
2022-08-30 05:40:03 +00:00
|
|
|
|
|
|
|
getHomeDir() / dataDir
|
|
|
|
|
|
|
|
const
|
2022-11-24 13:41:00 +00:00
|
|
|
defaultDataVerifiedProxyDirDesc* = defaultVerifiedProxyDataDir()
|
2022-08-30 05:40:03 +00:00
|
|
|
|
2022-09-24 11:57:27 +00:00
|
|
|
type
|
|
|
|
Web3UrlKind* = enum
|
|
|
|
HttpUrl, WsUrl
|
|
|
|
|
2023-03-28 13:50:23 +00:00
|
|
|
Web3Url* = object
|
2022-09-24 11:57:27 +00:00
|
|
|
kind*: Web3UrlKind
|
|
|
|
web3Url*: string
|
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
type VerifiedProxyConf* = object
|
2022-08-30 05:40:03 +00:00
|
|
|
# Config
|
|
|
|
configFile* {.
|
|
|
|
desc: "Loads the configuration from a TOML file"
|
|
|
|
name: "config-file" .}: Option[InputFile]
|
|
|
|
|
|
|
|
# Logging
|
|
|
|
logLevel* {.
|
|
|
|
desc: "Sets the log level"
|
|
|
|
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
|
|
|
|
|
|
|
|
# Storage
|
|
|
|
dataDir* {.
|
2022-11-24 13:41:00 +00:00
|
|
|
desc: "The directory where nimbus_verified_proxy will store all data"
|
|
|
|
defaultValue: defaultVerifiedProxyDataDir()
|
|
|
|
defaultValueDesc: $defaultDataVerifiedProxyDirDesc
|
2022-08-30 05:40:03 +00:00
|
|
|
abbr: "d"
|
|
|
|
name: "data-dir" .}: OutDir
|
|
|
|
|
|
|
|
# Network
|
|
|
|
eth2Network* {.
|
|
|
|
desc: "The Eth2 network to join"
|
|
|
|
defaultValueDesc: "mainnet"
|
|
|
|
name: "network" .}: Option[string]
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
# Consensus light sync
|
|
|
|
# No default - Needs to be provided by the user
|
|
|
|
trustedBlockRoot* {.
|
|
|
|
desc: "Recent trusted finalized block root to initialize the consensus light client from"
|
|
|
|
name: "trusted-block-root" .}: Eth2Digest
|
|
|
|
|
|
|
|
# (Untrusted) web3 provider
|
|
|
|
# No default - Needs to be provided by the user
|
|
|
|
web3url* {.
|
|
|
|
desc: "URL of the web3 data provider"
|
2023-03-28 13:50:23 +00:00
|
|
|
name: "web3-url" .}: Web3Url
|
2022-11-24 13:41:00 +00:00
|
|
|
|
|
|
|
# Local JSON-RPC server
|
|
|
|
rpcAddress* {.
|
|
|
|
desc: "Listening address of the JSON-RPC server"
|
|
|
|
defaultValue: defaultAdminListenAddress
|
|
|
|
defaultValueDesc: $defaultAdminListenAddressDesc
|
2023-11-10 18:38:11 +00:00
|
|
|
name: "rpc-address" .}: IpAddress
|
2022-11-24 13:41:00 +00:00
|
|
|
|
|
|
|
rpcPort* {.
|
|
|
|
desc: "Listening port of the JSON-RPC server"
|
|
|
|
defaultValue: 8545
|
|
|
|
name: "rpc-port" .}: Port
|
|
|
|
|
2022-08-30 05:40:03 +00:00
|
|
|
# Libp2p
|
|
|
|
bootstrapNodes* {.
|
|
|
|
desc: "Specifies one or more bootstrap nodes to use when connecting to the network"
|
|
|
|
abbr: "b"
|
|
|
|
name: "bootstrap-node" .}: seq[string]
|
|
|
|
|
|
|
|
bootstrapNodesFile* {.
|
|
|
|
desc: "Specifies a line-delimited file of bootstrap Ethereum network addresses"
|
|
|
|
defaultValue: ""
|
|
|
|
name: "bootstrap-file" .}: InputFile
|
|
|
|
|
|
|
|
listenAddress* {.
|
|
|
|
desc: "Listening address for the Ethereum LibP2P and Discovery v5 traffic"
|
|
|
|
defaultValue: defaultListenAddress
|
|
|
|
defaultValueDesc: $defaultListenAddressDesc
|
2023-11-10 18:38:11 +00:00
|
|
|
name: "listen-address" .}: IpAddress
|
2022-08-30 05:40:03 +00:00
|
|
|
|
|
|
|
tcpPort* {.
|
|
|
|
desc: "Listening TCP port for Ethereum LibP2P traffic"
|
|
|
|
defaultValue: defaultEth2TcpPort
|
|
|
|
defaultValueDesc: $defaultEth2TcpPortDesc
|
|
|
|
name: "tcp-port" .}: Port
|
|
|
|
|
|
|
|
udpPort* {.
|
|
|
|
desc: "Listening UDP port for node discovery"
|
|
|
|
defaultValue: defaultEth2TcpPort
|
|
|
|
defaultValueDesc: $defaultEth2TcpPortDesc
|
|
|
|
name: "udp-port" .}: Port
|
|
|
|
|
2022-11-24 13:41:00 +00:00
|
|
|
# TODO: Select a lower amount of peers.
|
2022-08-30 05:40:03 +00:00
|
|
|
maxPeers* {.
|
|
|
|
desc: "The target number of peers to connect to"
|
2022-11-24 13:41:00 +00:00
|
|
|
defaultValue: 160 # 5 (fanout) * 64 (subnets) / 2 (subs) for a healthy mesh
|
2022-08-30 05:40:03 +00:00
|
|
|
name: "max-peers" .}: int
|
|
|
|
|
|
|
|
hardMaxPeers* {.
|
|
|
|
desc: "The maximum number of peers to connect to. Defaults to maxPeers * 1.5"
|
|
|
|
name: "hard-max-peers" .}: Option[int]
|
|
|
|
|
|
|
|
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)
|
|
|
|
defaultValueDesc: "any"
|
|
|
|
name: "nat" .}: NatConfig
|
|
|
|
|
|
|
|
enrAutoUpdate* {.
|
|
|
|
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"
|
|
|
|
defaultValue: false
|
|
|
|
name: "enr-auto-update" .}: bool
|
|
|
|
|
|
|
|
agentString* {.
|
|
|
|
defaultValue: "nimbus",
|
2022-11-24 13:41:00 +00:00
|
|
|
desc: "Node agent string which is used as identifier in the LibP2P network"
|
2022-08-30 05:40:03 +00:00
|
|
|
name: "agent-string" .}: string
|
|
|
|
|
|
|
|
discv5Enabled* {.
|
|
|
|
desc: "Enable Discovery v5"
|
|
|
|
defaultValue: true
|
|
|
|
name: "discv5" .}: bool
|
|
|
|
|
|
|
|
directPeers* {.
|
|
|
|
desc: "The list of priviledged, secure and known peers to connect and" &
|
|
|
|
"maintain the connection to, this requires a not random netkey-file." &
|
|
|
|
"In the complete multiaddress format like:" &
|
|
|
|
"/ip4/<address>/tcp/<port>/p2p/<peerId-public-key>." &
|
2022-11-24 13:41:00 +00:00
|
|
|
"Peering agreements are established out of band and must be reciprocal"
|
2022-08-30 05:40:03 +00:00
|
|
|
name: "direct-peer" .}: seq[string]
|
|
|
|
|
|
|
|
|
2023-03-28 13:50:23 +00:00
|
|
|
proc parseCmdArg*(
|
2023-08-04 12:43:30 +00:00
|
|
|
T: type Web3Url, p: string): T {.raises: [ValueError].} =
|
2023-03-28 13:50:23 +00:00
|
|
|
let
|
|
|
|
url = parseUri(p)
|
|
|
|
normalizedScheme = url.scheme.toLowerAscii()
|
|
|
|
|
2022-08-30 05:40:03 +00:00
|
|
|
if (normalizedScheme == "http" or normalizedScheme == "https"):
|
2023-03-28 13:50:23 +00:00
|
|
|
Web3Url(kind: HttpUrl, web3Url: p)
|
2022-08-30 05:40:03 +00:00
|
|
|
elif (normalizedScheme == "ws" or normalizedScheme == "wss"):
|
2023-03-28 13:50:23 +00:00
|
|
|
Web3Url(kind: WsUrl, web3Url: p)
|
2022-08-30 05:40:03 +00:00
|
|
|
else:
|
|
|
|
raise newException(
|
2023-08-04 12:43:30 +00:00
|
|
|
ValueError, "Web3 url should have defined scheme (http/https/ws/wss)"
|
2022-08-30 05:40:03 +00:00
|
|
|
)
|
|
|
|
|
2023-03-28 13:50:23 +00:00
|
|
|
proc completeCmdArg*(T: type Web3Url, val: string): seq[string] =
|
2022-08-30 05:40:03 +00:00
|
|
|
return @[]
|
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
func asLightClientConf*(pc: VerifiedProxyConf): LightClientConf =
|
2022-08-30 05:40:03 +00:00
|
|
|
return LightClientConf(
|
|
|
|
configFile: pc.configFile,
|
|
|
|
logLevel: pc.logLevel,
|
|
|
|
logStdout: pc.logStdout,
|
2022-11-24 13:41:00 +00:00
|
|
|
logFile: none(OutFile),
|
2022-08-30 05:40:03 +00:00
|
|
|
dataDir: pc.dataDir,
|
|
|
|
eth2Network: pc.eth2Network,
|
|
|
|
bootstrapNodes: pc.bootstrapNodes,
|
|
|
|
bootstrapNodesFile: pc.bootstrapNodesFile,
|
|
|
|
listenAddress: pc.listenAddress,
|
|
|
|
tcpPort: pc.tcpPort,
|
|
|
|
udpPort: pc.udpPort,
|
|
|
|
maxPeers: pc.maxPeers,
|
|
|
|
hardMaxPeers: pc.hardMaxPeers,
|
|
|
|
nat: pc.nat,
|
|
|
|
enrAutoUpdate: pc.enrAutoUpdate,
|
|
|
|
agentString: pc.agentString,
|
|
|
|
discv5Enabled: pc.discv5Enabled,
|
|
|
|
directPeers: pc.directPeers,
|
|
|
|
trustedBlockRoot: pc.trustedBlockRoot,
|
|
|
|
web3Urls: @[],
|
2023-03-09 23:40:55 +00:00
|
|
|
jwtSecret: none(InputFile),
|
2022-08-30 05:40:03 +00:00
|
|
|
stopAtEpoch: 0
|
|
|
|
)
|
2022-09-24 11:57:27 +00:00
|
|
|
|
2022-11-23 16:11:38 +00:00
|
|
|
# TODO: Cannot use ClientConfig in VerifiedProxyConf due to the fact that
|
2022-09-24 11:57:27 +00:00
|
|
|
# it contain `set[TLSFlags]` which does not have proper toml serialization
|
2023-03-28 13:50:23 +00:00
|
|
|
func asClientConfig*(url: Web3Url): ClientConfig =
|
2022-09-24 11:57:27 +00:00
|
|
|
case url.kind
|
|
|
|
of HttpUrl:
|
|
|
|
getHttpClientConfig(url.web3Url)
|
|
|
|
of WsUrl:
|
|
|
|
getWebSocketClientConfig(url.web3Url, flags = {})
|