2023-12-01 16:20:52 +00:00
|
|
|
# Fluffy
|
2024-01-25 10:04:09 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-07-09 11:34:16 +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.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[options, strutils, tables],
|
2024-02-28 17:31:45 +00:00
|
|
|
confutils,
|
|
|
|
confutils/std/net,
|
|
|
|
chronicles,
|
|
|
|
chronicles/topics_registry,
|
|
|
|
chronos,
|
|
|
|
metrics,
|
|
|
|
metrics/chronos_httpserver,
|
2024-05-30 12:54:03 +00:00
|
|
|
stew/byteutils,
|
|
|
|
results,
|
2021-09-03 08:57:19 +00:00
|
|
|
nimcrypto/[hash, sha2],
|
2024-10-04 21:21:26 +00:00
|
|
|
eth/[common/keys, net/nat],
|
2021-07-09 11:34:16 +00:00
|
|
|
eth/p2p/discoveryv5/[enr, node],
|
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
2021-12-13 08:06:29 +00:00
|
|
|
../common/common_utils,
|
2023-12-01 16:20:52 +00:00
|
|
|
../database/content_db,
|
2022-11-08 17:31:45 +00:00
|
|
|
../network/wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2022-08-23 14:28:10 +00:00
|
|
|
../network/history/[history_content, history_network]
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-09-29 11:58:55 +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-09-29 11:58:55 +00:00
|
|
|
|
2021-12-08 10:54:22 +00:00
|
|
|
defaultListenAddressDesc = $defaultListenAddress
|
|
|
|
defaultAdminListenAddressDesc = $defaultAdminListenAddress
|
2022-05-09 15:18:57 +00:00
|
|
|
# 100mb seems a bit smallish we may consider increasing defaults after some
|
|
|
|
# network measurements
|
|
|
|
defaultStorageSize* = uint32(1000 * 1000 * 100)
|
2022-07-11 14:29:16 +00:00
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
type
|
|
|
|
PortalCmd* = enum
|
|
|
|
noCommand
|
|
|
|
ping
|
2022-09-10 19:00:27 +00:00
|
|
|
findNodes
|
|
|
|
findContent
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-12-13 08:06:29 +00:00
|
|
|
PortalCliConf* = object
|
2021-07-09 11:34:16 +00:00
|
|
|
logLevel* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: LogLevel.DEBUG,
|
|
|
|
defaultValueDesc: $LogLevel.DEBUG,
|
|
|
|
desc: "Sets the log level",
|
|
|
|
name: "log-level"
|
|
|
|
.}: LogLevel
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
udpPort* {.defaultValue: 9009, desc: "UDP listening port", name: "udp-port".}:
|
|
|
|
uint16
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
listenAddress* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: defaultListenAddress,
|
|
|
|
defaultValueDesc: $defaultListenAddressDesc,
|
|
|
|
desc: "Listening address for the Discovery v5 traffic",
|
|
|
|
name: "listen-address"
|
|
|
|
.}: IpAddress
|
2021-07-09 11:34:16 +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.
|
|
|
|
bootstrapNodes* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
desc:
|
|
|
|
"ENR URI of node to bootstrap Discovery v5 and the Portal networks from. Argument may be repeated",
|
|
|
|
name: "bootstrap-node"
|
|
|
|
.}: seq[Record]
|
2021-12-13 08:06:29 +00:00
|
|
|
|
|
|
|
bootstrapNodesFile* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
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* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
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
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
enrAutoUpdate* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
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-07-09 11:34:16 +00:00
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
networkKey* {.
|
|
|
|
desc: "Private key (secp256k1) for the p2p network, hex encoded.",
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: PrivateKey.random(keys.newRng()[]),
|
|
|
|
defaultValueDesc: "random",
|
|
|
|
name: "network-key"
|
|
|
|
.}: PrivateKey
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
metricsEnabled* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: false, desc: "Enable the metrics server", name: "metrics"
|
|
|
|
.}: bool
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
metricsAddress* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: defaultAdminListenAddress,
|
|
|
|
defaultValueDesc: $defaultAdminListenAddressDesc,
|
|
|
|
desc: "Listening address of the metrics server",
|
|
|
|
name: "metrics-address"
|
|
|
|
.}: IpAddress
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
metricsPort* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: 8008,
|
|
|
|
desc: "Listening HTTP port of the metrics server",
|
|
|
|
name: "metrics-port"
|
|
|
|
.}: Port
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-12-13 13:12:51 +00:00
|
|
|
protocolId* {.
|
2024-06-18 07:32:57 +00:00
|
|
|
defaultValue: getProtocolId(PortalNetwork.mainnet, PortalSubnetwork.history),
|
2024-02-28 17:31:45 +00:00
|
|
|
desc: "Portal wire protocol id for the network to connect to",
|
|
|
|
name: "protocol-id"
|
|
|
|
.}: PortalProtocolId
|
2021-12-13 13:12:51 +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
|
|
|
|
storageSize* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
desc:
|
|
|
|
"Maximum amount (in bytes) of content which will be stored " &
|
|
|
|
"in local database.",
|
|
|
|
defaultValue: defaultStorageSize,
|
|
|
|
name: "storage-size"
|
|
|
|
.}: uint32
|
|
|
|
|
|
|
|
case cmd* {.command, defaultValue: noCommand.}: PortalCmd
|
2021-07-09 11:34:16 +00:00
|
|
|
of noCommand:
|
|
|
|
discard
|
|
|
|
of ping:
|
|
|
|
pingTarget* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
argument, desc: "ENR URI of the node to a send ping message", name: "node"
|
|
|
|
.}: Node
|
2022-09-10 19:00:27 +00:00
|
|
|
of findNodes:
|
2021-07-09 11:34:16 +00:00
|
|
|
distance* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: 255,
|
|
|
|
desc: "Distance parameter for the findNodes message",
|
|
|
|
name: "distance"
|
|
|
|
.}: uint16
|
2021-07-09 11:34:16 +00:00
|
|
|
# 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* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
argument, desc: "ENR URI of the node to send a findNodes message", name: "node"
|
|
|
|
.}: Node
|
2022-09-10 19:00:27 +00:00
|
|
|
of findContent:
|
2021-07-09 11:34:16 +00:00
|
|
|
findContentTarget* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
argument,
|
|
|
|
desc: "ENR URI of the node to send a findContent message",
|
|
|
|
name: "node"
|
|
|
|
.}: Node
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type enr.Record, p: string): T =
|
2024-06-27 15:59:08 +00:00
|
|
|
let res = enr.Record.fromURI(p)
|
|
|
|
if res.isErr:
|
|
|
|
raise newException(ValueError, "Invalid ENR: " & $res.error)
|
|
|
|
|
|
|
|
res.value
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type enr.Record, val: string): seq[string] =
|
2021-07-09 11:34:16 +00:00
|
|
|
return @[]
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type Node, p: string): T =
|
2024-06-27 15:59:08 +00:00
|
|
|
let res = enr.Record.fromURI(p)
|
|
|
|
if res.isErr:
|
|
|
|
raise newException(ValueError, "Invalid ENR: " & $res.error)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2024-06-27 15:59:08 +00:00
|
|
|
let n = Node.fromRecord(res.value)
|
|
|
|
if n.address.isNone():
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "ENR without address")
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2024-06-27 15:59:08 +00:00
|
|
|
n
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type Node, val: string): seq[string] =
|
2021-07-09 11:34:16 +00:00
|
|
|
return @[]
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type PrivateKey, p: string): T =
|
2021-07-09 11:34:16 +00:00
|
|
|
try:
|
2023-01-31 12:38:08 +00:00
|
|
|
result = PrivateKey.fromHex(p).tryGet()
|
2021-07-09 11:34:16 +00:00
|
|
|
except CatchableError:
|
2023-08-04 12:43:30 +00:00
|
|
|
raise newException(ValueError, "Invalid private key")
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type PrivateKey, val: string): seq[string] =
|
2021-07-09 11:34:16 +00:00
|
|
|
return @[]
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type PortalProtocolId, p: string): T =
|
2021-12-13 13:12:51 +00:00
|
|
|
try:
|
2023-01-31 12:38:08 +00:00
|
|
|
result = byteutils.hexToByteArray(p, 2)
|
2021-12-13 13:12:51 +00:00
|
|
|
except ValueError:
|
2024-02-28 17:31:45 +00:00
|
|
|
raise newException(ValueError, "Invalid protocol id, not a valid hex value")
|
2021-12-13 13:12:51 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type PortalProtocolId, val: string): seq[string] =
|
2021-12-13 13:12:51 +00:00
|
|
|
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)
|
|
|
|
|
2024-07-17 15:07:27 +00:00
|
|
|
proc testContentIdHandler(contentKey: ContentKeyByteList): results.Opt[ContentId] =
|
2022-01-06 08:06:05 +00:00
|
|
|
# Note: Returning a static content id here, as in practice this depends
|
|
|
|
# on the content key to content id derivation, which is different for the
|
|
|
|
# different content networks. And we want these tests to be independent from
|
|
|
|
# that.
|
|
|
|
let idHash = sha256.digest("test")
|
2022-11-08 17:31:45 +00:00
|
|
|
ok(readUintBE[256](idHash.data))
|
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!
|
2024-02-28 17:31:45 +00:00
|
|
|
(extIp, _, extUdpPort) =
|
|
|
|
setupAddress(config.nat, 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,
|
2024-02-28 17:31:45 +00:00
|
|
|
extIp,
|
2024-06-19 01:57:45 +00:00
|
|
|
Opt.none(Port),
|
2024-02-28 17:31:45 +00:00
|
|
|
extUdpPort,
|
2021-12-13 08:06:29 +00:00
|
|
|
bootstrapRecords = bootstrapRecords,
|
2024-02-28 17:31:45 +00:00
|
|
|
bindIp = bindIp,
|
|
|
|
bindPort = udpPort,
|
2021-12-13 08:06:29 +00:00
|
|
|
enrAutoUpdate = config.enrAutoUpdate,
|
2024-02-28 17:31:45 +00:00
|
|
|
rng = rng,
|
|
|
|
)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
d.open()
|
|
|
|
|
2022-01-14 15:07:14 +00:00
|
|
|
let
|
2024-09-05 16:31:55 +00:00
|
|
|
db = ContentDB.new(
|
|
|
|
"", config.storageSize, defaultRadiusConfig, d.localNode.id, inMemory = true
|
|
|
|
)
|
2022-08-17 07:32:06 +00:00
|
|
|
sm = StreamManager.new(d)
|
2023-09-04 10:21:01 +00:00
|
|
|
cq = newAsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
2022-08-17 07:32:06 +00:00
|
|
|
stream = sm.registerNewStream(cq)
|
2024-02-28 17:31:45 +00:00
|
|
|
portal = PortalProtocol.new(
|
|
|
|
d,
|
|
|
|
config.protocolId,
|
|
|
|
testContentIdHandler,
|
|
|
|
createGetHandler(db),
|
2024-09-09 15:52:11 +00:00
|
|
|
createStoreHandler(db, defaultRadiusConfig),
|
2024-09-05 16:31:55 +00:00
|
|
|
createRadiusHandler(db),
|
2024-02-28 17:31:45 +00:00
|
|
|
stream,
|
|
|
|
bootstrapRecords = bootstrapRecords,
|
|
|
|
)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
if config.metricsEnabled:
|
|
|
|
let
|
|
|
|
address = config.metricsAddress
|
|
|
|
port = config.metricsPort
|
|
|
|
url = "http://" & $address & ":" & $port & "/metrics"
|
2024-06-25 10:06:39 +00:00
|
|
|
|
|
|
|
server = MetricsHttpServerRef.new($address, port).valueOr:
|
|
|
|
error "Could not instantiate metrics HTTP server", url, error
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
info "Starting metrics HTTP server", url
|
2021-07-09 11:34:16 +00:00
|
|
|
try:
|
2024-06-25 10:06:39 +00:00
|
|
|
waitFor server.start()
|
|
|
|
except MetricsError as exc:
|
|
|
|
fatal "Could not start metrics HTTP server",
|
|
|
|
url, error_msg = exc.msg, error_name = exc.name
|
|
|
|
quit QuitFailure
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
case config.cmd
|
|
|
|
of ping:
|
|
|
|
let pong = waitFor portal.ping(config.pingTarget)
|
|
|
|
|
|
|
|
if pong.isOk():
|
|
|
|
echo pong.get()
|
|
|
|
else:
|
|
|
|
echo pong.error
|
2022-09-10 19:00:27 +00:00
|
|
|
of findNodes:
|
2022-03-19 07:54:42 +00:00
|
|
|
let distances = @[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():
|
2022-03-19 07:54:42 +00:00
|
|
|
for node in nodes.get():
|
|
|
|
echo $node.record & " - " & shortLog(node)
|
2021-07-09 11:34:16 +00:00
|
|
|
else:
|
|
|
|
echo nodes.error
|
2022-09-10 19:00:27 +00:00
|
|
|
of findContent:
|
2024-01-25 10:04:09 +00:00
|
|
|
# For now just some bogus bytes
|
2024-07-17 15:07:27 +00:00
|
|
|
let contentKey = ContentKeyByteList.init(@[1'u8])
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let foundContent = waitFor portal.findContent(config.findContentTarget, contentKey)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
if foundContent.isOk():
|
|
|
|
echo foundContent.get()
|
|
|
|
else:
|
|
|
|
echo foundContent.error
|
|
|
|
of noCommand:
|
|
|
|
d.start()
|
2021-07-30 19:19:03 +00:00
|
|
|
portal.start()
|
2022-09-10 19:00:27 +00:00
|
|
|
waitFor(discover(d))
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
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)
|