2021-07-09 11:34:16 +00:00
|
|
|
# Nimbus - Portal Network
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[options, strutils, tables],
|
|
|
|
confutils, confutils/std/net, chronicles, chronicles/topics_registry,
|
|
|
|
chronos, metrics, metrics/chronos_httpserver, stew/byteutils,
|
2021-09-03 08:57:19 +00:00
|
|
|
nimcrypto/[hash, sha2],
|
2021-07-09 11:34:16 +00:00
|
|
|
eth/[keys, net/nat],
|
|
|
|
eth/p2p/discoveryv5/[enr, node],
|
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
2021-12-13 08:06:29 +00:00
|
|
|
../common/common_utils,
|
2021-09-24 15:18:54 +00:00
|
|
|
../network/wire/[messages, portal_protocol],
|
2021-12-13 13:12:51 +00:00
|
|
|
../network/state/[state_content, state_network]
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-09-29 11:58:55 +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"))
|
2021-09-29 11:58:55 +00:00
|
|
|
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultListenAddressDesc = $defaultListenAddress
|
|
|
|
defaultAdminListenAddressDesc = $defaultAdminListenAddress
|
2021-09-29 11:58:55 +00:00
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
type
|
|
|
|
PortalCmd* = enum
|
|
|
|
noCommand
|
|
|
|
ping
|
2021-12-08 10:54:22 +00:00
|
|
|
findnodes
|
2021-07-09 11:34:16 +00:00
|
|
|
findcontent
|
|
|
|
|
2021-12-13 08:06:29 +00:00
|
|
|
PortalCliConf* = object
|
2021-07-09 11:34:16 +00:00
|
|
|
logLevel* {.
|
|
|
|
defaultValue: LogLevel.DEBUG
|
2021-09-29 11:58:55 +00:00
|
|
|
defaultValueDesc: $LogLevel.DEBUG
|
2021-07-09 11:34:16 +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-07-09 11:34:16 +00:00
|
|
|
desc: "Listening address for the Discovery v5 traffic"
|
|
|
|
name: "listen-address" }: ValidIpAddress
|
|
|
|
|
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.
|
|
|
|
bootstrapNodes* {.
|
|
|
|
desc: "ENR URI of node to bootstrap Discovery v5 and the Portal networks from. Argument may be repeated"
|
|
|
|
name: "bootstrap-node" .}: seq[Record]
|
|
|
|
|
|
|
|
bootstrapNodesFile* {.
|
|
|
|
desc: "Specifies a line-delimited file of ENR URIs to bootstrap Discovery v5 and Portal networks from"
|
|
|
|
defaultValue: ""
|
|
|
|
name: "bootstrap-file" }: InputFile
|
2021-07-09 11:34:16 +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-07-09 11:34:16 +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.",
|
2021-07-09 11:34:16 +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" .}: PrivateKey
|
2021-07-09 11:34:16 +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-07-09 11:34:16 +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
|
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
protocolId* {.
|
|
|
|
defaultValue: stateProtocolId
|
|
|
|
desc: "Portal wire protocol id for the network to connect to"
|
|
|
|
name: "protocol-id" .}: PortalProtocolId
|
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
case cmd* {.
|
|
|
|
command
|
|
|
|
defaultValue: noCommand }: PortalCmd
|
|
|
|
of noCommand:
|
|
|
|
discard
|
|
|
|
of ping:
|
|
|
|
pingTarget* {.
|
|
|
|
argument
|
|
|
|
desc: "ENR URI of the node to a send ping message"
|
|
|
|
name: "node" .}: Node
|
2021-12-08 10:54:22 +00:00
|
|
|
of findnodes:
|
2021-07-09 11:34:16 +00:00
|
|
|
distance* {.
|
|
|
|
defaultValue: 255
|
2021-12-08 10:54:22 +00:00
|
|
|
desc: "Distance parameter for the findNodes message"
|
2021-07-09 11:34:16 +00:00
|
|
|
name: "distance" .}: uint16
|
|
|
|
# TODO: Order here matters as else the help message does not show all the
|
|
|
|
# information, see: https://github.com/status-im/nim-confutils/issues/15
|
2021-12-08 10:54:22 +00:00
|
|
|
findNodesTarget* {.
|
2021-07-09 11:34:16 +00:00
|
|
|
argument
|
2021-12-08 10:54:22 +00:00
|
|
|
desc: "ENR URI of the node to send a findNodes message"
|
2021-07-09 11:34:16 +00:00
|
|
|
name: "node" .}: Node
|
|
|
|
of findcontent:
|
|
|
|
findContentTarget* {.
|
|
|
|
argument
|
|
|
|
desc: "ENR URI of the node to send a findContent message"
|
|
|
|
name: "node" .}: Node
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type enr.Record, p: TaintedString): T =
|
|
|
|
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 =
|
|
|
|
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 =
|
|
|
|
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-12-13 13:12:51 +00:00
|
|
|
proc parseCmdArg*(T: type PortalProtocolId, p: TaintedString): T =
|
|
|
|
try:
|
|
|
|
result = byteutils.hexToByteArray(string(p), 2)
|
|
|
|
except ValueError:
|
|
|
|
raise newException(ConfigurationError,
|
|
|
|
"Invalid protocol id, not a valid hex value")
|
|
|
|
|
|
|
|
proc completeCmdArg*(T: type PortalProtocolId, val: TaintedString): seq[string] =
|
|
|
|
return @[]
|
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
proc discover(d: discv5_protocol.Protocol) {.async.} =
|
|
|
|
while true:
|
|
|
|
let discovered = await d.queryRandom()
|
|
|
|
info "Lookup finished", nodes = discovered.len
|
|
|
|
await sleepAsync(30.seconds)
|
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
proc testHandler(contentKey: ByteList): ContentResult =
|
2021-09-24 09:22:07 +00:00
|
|
|
# Note: We don't incorperate storage in this tool so we always return
|
|
|
|
# missing content. For now we are using the state network derivation but it
|
|
|
|
# could be selective based on the network the tool is used for.
|
2021-11-17 16:11:17 +00:00
|
|
|
let contentId = toContentId(contentKey)
|
|
|
|
if contentId.isSome():
|
|
|
|
ContentResult(kind: ContentMissing, contentId: contentId.get())
|
|
|
|
else:
|
|
|
|
ContentResult(kind: ContentKeyValidationFailure,
|
|
|
|
error: "Failed decoding content key")
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-12-13 08:06:29 +00:00
|
|
|
proc run(config: PortalCliConf) =
|
2021-07-09 11:34:16 +00:00
|
|
|
let
|
|
|
|
rng = newRng()
|
|
|
|
bindIp = config.listenAddress
|
|
|
|
udpPort = Port(config.udpPort)
|
|
|
|
# TODO: allow for no TCP port mapping!
|
|
|
|
(extIp, _, extUdpPort) = setupAddress(config.nat,
|
2021-12-13 13:12:51 +00:00
|
|
|
config.listenAddress, udpPort, udpPort, "portalcli")
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-12-13 08:06:29 +00:00
|
|
|
var bootstrapRecords: seq[Record]
|
|
|
|
loadBootstrapFile(string config.bootstrapNodesFile, bootstrapRecords)
|
|
|
|
bootstrapRecords.add(config.bootstrapNodes)
|
|
|
|
|
|
|
|
let d = newProtocol(
|
2021-12-13 13:12:51 +00:00
|
|
|
config.networkKey,
|
2021-12-13 08:06:29 +00:00
|
|
|
extIp, none(Port), extUdpPort,
|
|
|
|
bootstrapRecords = bootstrapRecords,
|
|
|
|
bindIp = bindIp, bindPort = udpPort,
|
|
|
|
enrAutoUpdate = config.enrAutoUpdate,
|
|
|
|
rng = rng)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
d.open()
|
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
let portal = PortalProtocol.new(d, config.protocolId, testHandler,
|
2021-12-13 08:06:29 +00:00
|
|
|
bootstrapRecords = bootstrapRecords)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
if config.metricsEnabled:
|
|
|
|
let
|
|
|
|
address = config.metricsAddress
|
|
|
|
port = config.metricsPort
|
|
|
|
notice "Starting metrics HTTP server",
|
|
|
|
url = "http://" & $address & ":" & $port & "/metrics"
|
|
|
|
try:
|
|
|
|
chronos_httpserver.startMetricsHttpServer($address, port)
|
|
|
|
except CatchableError as exc: raise exc
|
|
|
|
except Exception as exc: raiseAssert exc.msg # TODO fix metrics
|
|
|
|
|
|
|
|
case config.cmd
|
|
|
|
of ping:
|
|
|
|
let pong = waitFor portal.ping(config.pingTarget)
|
|
|
|
|
|
|
|
if pong.isOk():
|
|
|
|
echo pong.get()
|
|
|
|
else:
|
|
|
|
echo pong.error
|
2021-12-08 10:54:22 +00:00
|
|
|
of findnodes:
|
2021-07-09 11:34:16 +00:00
|
|
|
let distances = List[uint16, 256](@[config.distance])
|
2021-12-08 10:54:22 +00:00
|
|
|
let nodes = waitFor portal.findNodes(config.findNodesTarget, distances)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
if nodes.isOk():
|
|
|
|
echo nodes.get()
|
|
|
|
else:
|
|
|
|
echo nodes.error
|
|
|
|
of findcontent:
|
|
|
|
proc random(T: type UInt256, rng: var BrHmacDrbgContext): T =
|
|
|
|
var key: UInt256
|
|
|
|
brHmacDrbgGenerate(addr rng, addr key, csize_t(sizeof(key)))
|
|
|
|
|
|
|
|
key
|
|
|
|
|
2021-09-03 08:57:19 +00:00
|
|
|
# For now just some random bytes
|
2021-10-22 15:49:23 +00:00
|
|
|
let contentKey = ByteList.init(@[1'u8])
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
let foundContent = waitFor portal.findContent(config.findContentTarget,
|
|
|
|
contentKey)
|
|
|
|
|
|
|
|
if foundContent.isOk():
|
|
|
|
echo foundContent.get()
|
|
|
|
else:
|
|
|
|
echo foundContent.error
|
|
|
|
|
|
|
|
of noCommand:
|
|
|
|
d.start()
|
2021-07-30 19:19:03 +00:00
|
|
|
portal.start()
|
2021-07-09 11:34:16 +00:00
|
|
|
waitfor(discover(d))
|
|
|
|
|
|
|
|
when isMainModule:
|
2021-12-13 08:06:29 +00:00
|
|
|
let config = PortalCliConf.load()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
setLogLevel(config.logLevel)
|
|
|
|
|
|
|
|
run(config)
|