2024-06-10 11:47:35 +00:00
|
|
|
# Copyright (c) 2020-2024 Status Research & Development GmbH
|
2023-06-06 20:28:14 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2023-06-22 16:13:16 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2020-06-02 15:21:50 +00:00
|
|
|
import
|
2022-11-23 22:48:44 +00:00
|
|
|
std/[options, strutils, tables, sets],
|
2021-04-02 15:29:38 +00:00
|
|
|
confutils, confutils/std/net, chronicles, chronicles/topics_registry,
|
2022-11-23 22:48:44 +00:00
|
|
|
chronos, metrics, metrics/chronos_httpserver, stew/byteutils, stew/bitops2,
|
2023-09-22 10:32:43 +00:00
|
|
|
../eth/keys, ../eth/net/nat,
|
|
|
|
../eth/p2p/discoveryv5/[enr, node],
|
|
|
|
../eth/p2p/discoveryv5/protocol as discv5_protocol
|
2020-06-02 15:21:50 +00:00
|
|
|
|
2024-01-19 10:15:57 +00:00
|
|
|
const
|
|
|
|
defaultListenAddress* = (static parseIpAddress("0.0.0.0"))
|
|
|
|
defaultAdminListenAddress* = (static parseIpAddress("127.0.0.1"))
|
|
|
|
defaultListenAddressDesc = $defaultListenAddress
|
|
|
|
defaultAdminListenAddressDesc = $defaultAdminListenAddress
|
|
|
|
|
2020-06-02 15:21:50 +00:00
|
|
|
type
|
|
|
|
DiscoveryCmd* = enum
|
|
|
|
noCommand
|
|
|
|
ping
|
2022-02-03 14:51:08 +00:00
|
|
|
findNode
|
|
|
|
talkReq
|
2020-06-02 15:21:50 +00:00
|
|
|
|
|
|
|
DiscoveryConf* = object
|
|
|
|
logLevel* {.
|
|
|
|
defaultValue: LogLevel.DEBUG
|
2021-01-26 13:11:22 +00:00
|
|
|
desc: "Sets the log level"
|
2020-06-02 15:21:50 +00:00
|
|
|
name: "log-level" .}: LogLevel
|
|
|
|
|
|
|
|
udpPort* {.
|
|
|
|
defaultValue: 9009
|
2021-01-26 13:11:22 +00:00
|
|
|
desc: "UDP listening port"
|
2020-06-02 15:21:50 +00:00
|
|
|
name: "udp-port" .}: uint16
|
|
|
|
|
2021-01-07 09:15:48 +00:00
|
|
|
listenAddress* {.
|
2024-01-19 10:15:57 +00:00
|
|
|
defaultValue: defaultListenAddress
|
|
|
|
defaultValueDesc: $defaultListenAddressDesc
|
2021-01-07 09:15:48 +00:00
|
|
|
desc: "Listening address for the Discovery v5 traffic"
|
2023-11-10 06:28:21 +00:00
|
|
|
name: "listen-address" }: IpAddress
|
2021-01-07 09:15:48 +00:00
|
|
|
|
2022-11-23 22:48:44 +00:00
|
|
|
persistingFile* {.
|
|
|
|
defaultValue: "peerstore.csv",
|
|
|
|
desc: "File where the tool will keep the discovered records"
|
|
|
|
name: "persisting-file" .}: string
|
|
|
|
|
2020-06-02 15:21:50 +00:00
|
|
|
bootnodes* {.
|
2021-01-26 13:11:22 +00:00
|
|
|
desc: "ENR URI of node to bootstrap discovery with. Argument may be repeated"
|
2020-06-02 15:21:50 +00:00
|
|
|
name: "bootnode" .}: seq[enr.Record]
|
|
|
|
|
|
|
|
nat* {.
|
|
|
|
desc: "Specify method to use for determining public address. " &
|
2021-01-26 13:11:22 +00:00
|
|
|
"Must be one of: any, none, upnp, pmp, extip:<IP>"
|
2021-03-05 20:23:54 +00:00
|
|
|
defaultValue: NatConfig(hasExtIp: false, nat: NatAny)
|
|
|
|
name: "nat" .}: NatConfig
|
2020-06-02 15:21:50 +00:00
|
|
|
|
2021-01-26 13:11:22 +00:00
|
|
|
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
|
|
|
|
|
2020-06-30 11:35:15 +00:00
|
|
|
nodeKey* {.
|
2021-01-26 13:11:22 +00:00
|
|
|
desc: "P2P node private key as hex",
|
2020-07-12 15:25:18 +00:00
|
|
|
defaultValue: PrivateKey.random(keys.newRng()[])
|
2020-06-30 11:35:15 +00:00
|
|
|
name: "nodekey" .}: PrivateKey
|
|
|
|
|
|
|
|
metricsEnabled* {.
|
|
|
|
defaultValue: false
|
2021-01-26 13:11:22 +00:00
|
|
|
desc: "Enable the metrics server"
|
2020-06-30 11:35:15 +00:00
|
|
|
name: "metrics" .}: bool
|
|
|
|
|
|
|
|
metricsAddress* {.
|
2024-01-19 10:15:57 +00:00
|
|
|
defaultValue: defaultAdminListenAddress
|
|
|
|
defaultValueDesc: $defaultAdminListenAddressDesc
|
2021-01-26 13:11:22 +00:00
|
|
|
desc: "Listening address of the metrics server"
|
2024-01-19 10:15:57 +00:00
|
|
|
name: "metrics-address" .}: IpAddress
|
2020-06-30 11:35:15 +00:00
|
|
|
|
|
|
|
metricsPort* {.
|
|
|
|
defaultValue: 8008
|
2021-01-26 13:11:22 +00:00
|
|
|
desc: "Listening HTTP port of the metrics server"
|
2020-06-30 11:35:15 +00:00
|
|
|
name: "metrics-port" .}: Port
|
|
|
|
|
2020-06-02 15:21:50 +00:00
|
|
|
case cmd* {.
|
|
|
|
command
|
|
|
|
defaultValue: noCommand }: DiscoveryCmd
|
|
|
|
of noCommand:
|
|
|
|
discard
|
|
|
|
of ping:
|
|
|
|
pingTarget* {.
|
2020-06-03 12:17:50 +00:00
|
|
|
argument
|
2020-06-02 15:21:50 +00:00
|
|
|
desc: "ENR URI of the node to a send ping message"
|
|
|
|
name: "node" .}: Node
|
2022-02-03 14:51:08 +00:00
|
|
|
of findNode:
|
2020-06-02 15:21:50 +00:00
|
|
|
distance* {.
|
|
|
|
defaultValue: 255
|
|
|
|
desc: "Distance parameter for the findNode message"
|
2021-07-13 08:05:46 +00:00
|
|
|
name: "distance" .}: uint16
|
2020-06-03 12:17:50 +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
|
|
|
|
findNodeTarget* {.
|
|
|
|
argument
|
|
|
|
desc: "ENR URI of the node to send a findNode message"
|
|
|
|
name: "node" .}: Node
|
2022-02-03 14:51:08 +00:00
|
|
|
of talkReq:
|
|
|
|
talkReqTarget* {.
|
2020-11-13 11:45:39 +00:00
|
|
|
argument
|
2022-02-03 14:51:08 +00:00
|
|
|
desc: "ENR URI of the node to send a talkReq message"
|
2020-11-13 11:45:39 +00:00
|
|
|
name: "node" .}: Node
|
2020-06-02 15:21:50 +00:00
|
|
|
|
2023-06-06 20:28:14 +00:00
|
|
|
proc parseCmdArg*(T: type enr.Record, p: string): T {.raises: [ValueError].} =
|
2020-06-02 15:21:50 +00:00
|
|
|
if not fromURI(result, p):
|
2023-06-06 20:28:14 +00:00
|
|
|
raise newException(ValueError, "Invalid ENR")
|
2020-06-02 15:21:50 +00:00
|
|
|
|
2022-11-10 15:32:57 +00:00
|
|
|
proc completeCmdArg*(T: type enr.Record, val: string): seq[string] =
|
2020-06-02 15:21:50 +00:00
|
|
|
return @[]
|
|
|
|
|
2023-06-06 20:28:14 +00:00
|
|
|
proc parseCmdArg*(T: type Node, p: string): T {.raises: [ValueError].} =
|
2020-06-02 15:21:50 +00:00
|
|
|
var record: enr.Record
|
|
|
|
if not fromURI(record, p):
|
2023-06-06 20:28:14 +00:00
|
|
|
raise newException(ValueError, "Invalid ENR")
|
2020-06-02 15:21:50 +00:00
|
|
|
|
|
|
|
let n = newNode(record)
|
|
|
|
if n.isErr:
|
2023-06-06 20:28:14 +00:00
|
|
|
raise newException(ValueError, $n.error)
|
2020-06-02 15:21:50 +00:00
|
|
|
|
|
|
|
if n[].address.isNone():
|
2023-06-06 20:28:14 +00:00
|
|
|
raise newException(ValueError, "ENR without address")
|
2020-06-02 15:21:50 +00:00
|
|
|
|
|
|
|
n[]
|
|
|
|
|
2022-11-10 15:32:57 +00:00
|
|
|
proc completeCmdArg*(T: type Node, val: string): seq[string] =
|
2020-06-02 15:21:50 +00:00
|
|
|
return @[]
|
|
|
|
|
2023-06-06 20:28:14 +00:00
|
|
|
proc parseCmdArg*(T: type PrivateKey, p: string): T {.raises: [ValueError].} =
|
2020-06-30 11:35:15 +00:00
|
|
|
try:
|
2024-06-10 11:47:35 +00:00
|
|
|
result = PrivateKey.fromHex(p).tryGet()
|
2021-04-02 15:29:38 +00:00
|
|
|
except CatchableError:
|
2023-06-06 20:28:14 +00:00
|
|
|
raise newException(ValueError, "Invalid private key")
|
2020-06-30 11:35:15 +00:00
|
|
|
|
2022-11-10 15:32:57 +00:00
|
|
|
proc completeCmdArg*(T: type PrivateKey, val: string): seq[string] =
|
2020-06-30 11:35:15 +00:00
|
|
|
return @[]
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc discover(d: discv5_protocol.Protocol, psFile: string) {.async: (raises: [CancelledError]).} =
|
2022-11-23 22:48:44 +00:00
|
|
|
info "Starting peer-discovery in Ethereum - persisting peers at: ", psFile
|
|
|
|
|
|
|
|
var ethNodes: HashSet[seq[byte]]
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
let ps =
|
|
|
|
try:
|
|
|
|
open(psFile, fmWrite)
|
|
|
|
except IOError as e:
|
|
|
|
fatal "Failed to open file for writing", file = psFile, error = e.msg
|
|
|
|
quit QuitFailure
|
2022-11-23 22:48:44 +00:00
|
|
|
defer: ps.close()
|
2024-06-10 11:47:35 +00:00
|
|
|
try:
|
|
|
|
ps.writeLine("pubkey,node_id,fork_digest,ip:port,attnets,attnets_number")
|
|
|
|
except IOError as e:
|
|
|
|
fatal "Failed to write to file", file = psFile, error = e.msg
|
|
|
|
quit QuitFailure
|
2023-01-27 17:40:40 +00:00
|
|
|
|
2021-02-02 21:47:21 +00:00
|
|
|
while true:
|
2022-11-23 22:48:44 +00:00
|
|
|
let iTime = now(chronos.Moment)
|
2021-02-02 21:47:21 +00:00
|
|
|
let discovered = await d.queryRandom()
|
2022-11-23 22:48:44 +00:00
|
|
|
let qDuration = now(chronos.Moment) - iTime
|
|
|
|
info "Lookup finished", query_time = qDuration.secs, new_nodes = discovered.len, tot_peers=len(ethNodes)
|
2023-01-27 17:40:40 +00:00
|
|
|
|
2022-11-23 22:48:44 +00:00
|
|
|
for dNode in discovered:
|
2023-01-27 17:40:40 +00:00
|
|
|
let eth2 = dNode.record.tryGet("eth2", seq[byte])
|
2022-11-23 22:48:44 +00:00
|
|
|
let pubkey = dNode.record.tryGet("secp256k1", seq[byte])
|
|
|
|
let attnets = dNode.record.tryGet("attnets", seq[byte])
|
|
|
|
if eth2.isNone or attnets.isNone or pubkey.isNone: continue
|
|
|
|
|
|
|
|
if pubkey.get() in ethNodes: continue
|
|
|
|
ethNodes.incl(pubkey.get())
|
2023-01-27 17:40:40 +00:00
|
|
|
|
2022-11-23 22:48:44 +00:00
|
|
|
let forkDigest = eth2.get()
|
|
|
|
|
|
|
|
var bits = 0
|
|
|
|
for byt in attnets.get():
|
2023-01-27 17:40:40 +00:00
|
|
|
bits.inc(countOnes(byt.uint))
|
2022-11-23 22:48:44 +00:00
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
let str = "$#,$#,$#,$#,$#,$#"
|
|
|
|
let newLine =
|
|
|
|
try:
|
|
|
|
str % [pubkey.get().toHex, dNode.id.toHex, forkDigest[0..3].toHex, $dNode.address.get(), attnets.get().toHex, $bits]
|
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert e.msg
|
|
|
|
try:
|
|
|
|
ps.writeLine(newLine)
|
|
|
|
except IOError as e:
|
|
|
|
fatal "Failed to write to file", file = psFile, error = e.msg
|
|
|
|
quit QuitFailure
|
2024-01-22 16:08:08 +00:00
|
|
|
await sleepAsync(1.seconds) # 1 sec of delay
|
2023-01-27 17:40:40 +00:00
|
|
|
|
2021-02-02 21:47:21 +00:00
|
|
|
|
2023-06-22 16:13:16 +00:00
|
|
|
proc run(config: DiscoveryConf) {.raises: [CatchableError].} =
|
2020-06-02 15:21:50 +00:00
|
|
|
let
|
2021-03-02 16:13:29 +00:00
|
|
|
bindIp = config.listenAddress
|
|
|
|
udpPort = Port(config.udpPort)
|
|
|
|
# TODO: allow for no TCP port mapping!
|
|
|
|
(extIp, _, extUdpPort) = setupAddress(config.nat,
|
|
|
|
config.listenAddress, udpPort, udpPort, "dcli")
|
|
|
|
|
|
|
|
let d = newProtocol(config.nodeKey,
|
|
|
|
extIp, none(Port), extUdpPort,
|
|
|
|
bootstrapRecords = config.bootnodes,
|
|
|
|
bindIp = bindIp, bindPort = udpPort,
|
|
|
|
enrAutoUpdate = config.enrAutoUpdate)
|
2020-06-02 15:21:50 +00:00
|
|
|
|
|
|
|
d.open()
|
|
|
|
|
2021-04-02 15:29:38 +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
|
2020-06-30 11:35:15 +00:00
|
|
|
|
2020-06-02 15:21:50 +00:00
|
|
|
case config.cmd
|
|
|
|
of ping:
|
|
|
|
let pong = waitFor d.ping(config.pingTarget)
|
|
|
|
if pong.isOk():
|
|
|
|
echo pong[]
|
|
|
|
else:
|
|
|
|
echo "No Pong message returned"
|
2022-02-03 14:51:08 +00:00
|
|
|
of findNode:
|
2020-11-13 11:33:07 +00:00
|
|
|
let nodes = waitFor d.findNode(config.findNodeTarget, @[config.distance])
|
2020-06-02 15:21:50 +00:00
|
|
|
if nodes.isOk():
|
|
|
|
echo "Received valid records:"
|
|
|
|
for node in nodes[]:
|
2020-10-23 14:41:44 +00:00
|
|
|
echo $node.record & " - " & shortLog(node)
|
2020-06-02 15:21:50 +00:00
|
|
|
else:
|
|
|
|
echo "No Nodes message returned"
|
2022-02-03 14:51:08 +00:00
|
|
|
of talkReq:
|
|
|
|
let talkresp = waitFor d.talkReq(config.talkReqTarget, @[], @[])
|
2020-11-13 11:45:39 +00:00
|
|
|
if talkresp.isOk():
|
|
|
|
echo talkresp[]
|
|
|
|
else:
|
|
|
|
echo "No Talk Response message returned"
|
2020-06-02 15:21:50 +00:00
|
|
|
of noCommand:
|
|
|
|
d.start()
|
2022-11-23 22:48:44 +00:00
|
|
|
waitFor(discover(d, config.persistingFile))
|
2020-06-02 15:21:50 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2023-06-22 16:13:16 +00:00
|
|
|
{.pop.}
|
2020-06-02 15:21:50 +00:00
|
|
|
let config = DiscoveryConf.load()
|
2023-06-22 16:13:16 +00:00
|
|
|
{.push raises: [].}
|
2020-06-02 15:21:50 +00:00
|
|
|
|
2021-04-02 15:29:38 +00:00
|
|
|
setLogLevel(config.logLevel)
|
2020-06-02 15:21:50 +00:00
|
|
|
|
|
|
|
run(config)
|