2020-11-13 11:33:07 +00:00
|
|
|
# nim-eth - Node Discovery Protocol v5
|
2024-01-24 08:09:11 +00:00
|
|
|
# Copyright (c) 2020-2024 Status Research & Development GmbH
|
2021-04-28 14:20:05 +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.
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
## Node Discovery Protocol v5
|
|
|
|
##
|
|
|
|
## Node discovery protocol implementation as per specification:
|
|
|
|
## https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md
|
|
|
|
##
|
|
|
|
## This node discovery protocol implementation uses the same underlying
|
|
|
|
## implementation of routing table as is also used for the discovery v4
|
|
|
|
## implementation, which is the same or similar as the one described in the
|
|
|
|
## original Kademlia paper:
|
|
|
|
## https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf
|
|
|
|
##
|
|
|
|
## This might not be the most optimal implementation for the node discovery
|
|
|
|
## protocol v5. Why?
|
|
|
|
##
|
|
|
|
## The Kademlia paper describes an implementation that starts off from one
|
|
|
|
## k-bucket, and keeps splitting the bucket as more nodes are discovered and
|
|
|
|
## added. The bucket splits only on the part of the binary tree where our own
|
|
|
|
## node its id belongs too (same prefix). Resulting eventually in a k-bucket per
|
|
|
|
## logarithmic distance (log base2 distance). Well, not really, as nodes with
|
|
|
|
## ids in the closer distance ranges will never be found. And because of this an
|
|
|
|
## optimisation is done where buckets will also split sometimes even if the
|
|
|
|
## nodes own id does not have the same prefix (this is to avoid creating highly
|
|
|
|
## unbalanced branches which would require longer lookups).
|
|
|
|
##
|
|
|
|
## Now, some implementations take a more simplified approach. They just create
|
|
|
|
## directly a bucket for each possible logarithmic distance (e.g. here 1->256).
|
|
|
|
## Some implementations also don't create buckets with logarithmic distance
|
|
|
|
## lower than a certain value (e.g. only 1/15th of the highest buckets),
|
|
|
|
## because the closer to the node (the lower the distance), the less chance
|
|
|
|
## there is to still find nodes.
|
|
|
|
##
|
|
|
|
## The discovery protocol v4 its `FindNode` call will request the k closest
|
|
|
|
## nodes. As does original Kademlia. This effectively puts the work at the node
|
|
|
|
## that gets the request. This node will have to check its buckets and gather
|
|
|
|
## the closest. Some implementations go over all the nodes in all the buckets
|
|
|
|
## for this (e.g. go-ethereum discovery v4). However, in our bucket splitting
|
|
|
|
## approach, this search is improved.
|
|
|
|
##
|
|
|
|
## In the discovery protocol v5 the `FindNode` call is changed and now the
|
|
|
|
## logarithmic distance is passed as parameter instead of the NodeId. And only
|
|
|
|
## nodes that match that logarithmic distance are allowed to be returned.
|
|
|
|
## This change was made to not put the trust at the requested node for selecting
|
|
|
|
## the closest nodes. To counter a possible (mistaken) difference in
|
|
|
|
## implementation, but more importantly for security reasons. See also:
|
|
|
|
## https://github.com/ethereum/devp2p/blob/master/discv5/discv5-rationale.md#115-guard-against-kademlia-implementation-flaws
|
|
|
|
##
|
|
|
|
## The result is that in an implementation which just stores buckets per
|
2022-04-22 14:46:47 +00:00
|
|
|
## logarithmic distance, it simply needs to return the right bucket. In this
|
2020-11-13 11:33:07 +00:00
|
|
|
## split-bucket implementation, this cannot be done as such and thus the closest
|
|
|
|
## neighbours search is still done. And to do this, a reverse calculation of an
|
|
|
|
## id at given logarithmic distance is needed (which is why there is the
|
|
|
|
## `idAtDistance` proc). Next, nodes with invalid distances need to be filtered
|
|
|
|
## out to be compliant to the specification. This can most likely get further
|
2022-04-22 14:46:47 +00:00
|
|
|
## optimised, but if it would turn out to be an issue, it is probably easier to
|
|
|
|
## switch away from the split-bucket approach. The main benefit that the split
|
|
|
|
## bucket approach has is improved lookups (less hops due to no unbalanced
|
|
|
|
## branches), but lookup functionality of Kademlia is not something that is
|
|
|
|
## typically used in discv5. It is mostly used as a secure mechanism to find &
|
|
|
|
## select peers.
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2022-04-22 14:46:47 +00:00
|
|
|
## This `FindNode` change in discovery v5 could also have an effect on the
|
2020-11-13 11:33:07 +00:00
|
|
|
## efficiency of the network. Work will be moved from the receiver of
|
2022-04-22 14:46:47 +00:00
|
|
|
## `FindNodes` to the requester. But this could also mean more network traffic,
|
|
|
|
## as less nodes may potentially be passed around per `FindNode` call, and thus
|
|
|
|
## more requests may be needed for a lookup (adding bandwidth and latency).
|
|
|
|
## For this reason Discovery v5.1 has added the possibility to send a `FindNode`
|
|
|
|
## request with multiple distances specified. This implementation will
|
|
|
|
## underneath still use the neighbours search, specifically for the first
|
|
|
|
## distance provided. This means that if distances with wide gaps are provided,
|
|
|
|
## it could be that only nodes matching the first distance are returned.
|
|
|
|
## When distance 0 is provided in the requested list of distances, only the own
|
|
|
|
## ENR will be returned.
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2021-04-28 14:20:05 +00:00
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
import
|
2024-06-18 16:09:27 +00:00
|
|
|
std/[tables, sets, math, sequtils, algorithm],
|
2024-01-24 08:09:11 +00:00
|
|
|
json_serialization/std/net,
|
2024-05-30 12:46:57 +00:00
|
|
|
results, chronicles, chronos, stint, metrics,
|
2024-09-29 08:52:19 +00:00
|
|
|
../../rlp,
|
|
|
|
../../common/keys,
|
2022-10-10 10:13:20 +00:00
|
|
|
"."/[messages_encoding, encoding, node, routing_table, enr, random2, sessions,
|
|
|
|
ip_vote, nodes_verification]
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2022-05-02 14:49:19 +00:00
|
|
|
export
|
2024-09-19 15:53:11 +00:00
|
|
|
results, node, enr, encoding.maxDiscv5PacketSize, encoding.maxDiscv5TalkRespPayload
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2021-01-29 14:05:18 +00:00
|
|
|
declareCounter discovery_message_requests_outgoing,
|
2021-01-13 20:44:17 +00:00
|
|
|
"Discovery protocol outgoing message requests", labels = ["response"]
|
2021-01-29 14:05:18 +00:00
|
|
|
declareCounter discovery_message_requests_incoming,
|
2021-01-13 20:44:17 +00:00
|
|
|
"Discovery protocol incoming message requests", labels = ["response"]
|
2021-01-29 14:05:18 +00:00
|
|
|
declareCounter discovery_unsolicited_messages,
|
2021-01-13 20:44:17 +00:00
|
|
|
"Discovery protocol unsolicited or timed-out messages"
|
2021-01-29 14:05:18 +00:00
|
|
|
declareCounter discovery_enr_auto_update,
|
|
|
|
"Amount of discovery IP:port address ENR auto updates"
|
2020-12-15 23:07:49 +00:00
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
logScope:
|
2022-12-06 13:54:03 +00:00
|
|
|
topics = "eth p2p discv5"
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
alpha = 3 ## Kademlia concurrency factor
|
2020-12-17 16:06:42 +00:00
|
|
|
lookupRequestLimit = 3 ## Amount of distances requested in a single Findnode
|
|
|
|
## message for a lookup or query
|
|
|
|
findNodeResultLimit = 16 ## Maximum amount of ENRs in the total Nodes messages
|
|
|
|
## that will be processed
|
|
|
|
maxNodesPerMessage = 3 ## Maximum amount of ENRs per individual Nodes message
|
2021-01-05 14:03:38 +00:00
|
|
|
refreshInterval = 5.minutes ## Interval of launching a random query to
|
|
|
|
## refresh the routing table.
|
2020-11-13 11:33:07 +00:00
|
|
|
revalidateMax = 10000 ## Revalidation of a peer is done between 0 and this
|
|
|
|
## value in milliseconds
|
2021-01-26 13:11:22 +00:00
|
|
|
ipMajorityInterval = 5.minutes ## Interval for checking the latest IP:Port
|
|
|
|
## majority and updating this when ENR auto update is set.
|
2021-01-07 09:15:48 +00:00
|
|
|
initialLookups = 1 ## Amount of lookups done when populating the routing table
|
2022-08-05 13:19:16 +00:00
|
|
|
defaultHandshakeTimeout* = 2.seconds ## timeout for the reply on the
|
2020-11-13 11:33:07 +00:00
|
|
|
## whoareyou message
|
2022-08-05 13:19:16 +00:00
|
|
|
defaultResponseTimeout* = 4.seconds ## timeout for the response of a request-response
|
2020-11-13 11:33:07 +00:00
|
|
|
## call
|
|
|
|
|
|
|
|
type
|
2024-04-24 11:38:52 +00:00
|
|
|
OptAddress* = object
|
|
|
|
ip*: Opt[IpAddress]
|
|
|
|
port*: Port
|
|
|
|
|
2022-02-02 14:29:45 +00:00
|
|
|
DiscoveryConfig* = object
|
|
|
|
tableIpLimits*: TableIpLimits
|
|
|
|
bitsPerHop*: int
|
2022-08-05 13:19:16 +00:00
|
|
|
handshakeTimeout: Duration
|
|
|
|
responseTimeout: Duration
|
2022-02-02 14:29:45 +00:00
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
Protocol* = ref object
|
|
|
|
transp: DatagramTransport
|
|
|
|
localNode*: Node
|
|
|
|
privateKey: PrivateKey
|
2024-04-24 11:38:52 +00:00
|
|
|
bindAddress: OptAddress ## UDP binding address
|
2020-11-13 11:33:07 +00:00
|
|
|
pendingRequests: Table[AESGCMNonce, PendingRequest]
|
2021-11-22 17:53:52 +00:00
|
|
|
routingTable*: RoutingTable
|
2020-11-13 11:33:07 +00:00
|
|
|
codec*: Codec
|
2024-06-18 16:09:27 +00:00
|
|
|
awaitedMessages: Table[(NodeId, RequestId), Future[Opt[Message]]]
|
2021-01-05 14:03:38 +00:00
|
|
|
refreshLoop: Future[void]
|
2020-11-13 11:33:07 +00:00
|
|
|
revalidateLoop: Future[void]
|
2021-01-26 13:11:22 +00:00
|
|
|
ipMajorityLoop: Future[void]
|
2021-01-05 14:03:38 +00:00
|
|
|
lastLookup: chronos.Moment
|
2020-11-13 11:33:07 +00:00
|
|
|
bootstrapRecords*: seq[Record]
|
2021-01-21 16:21:36 +00:00
|
|
|
ipVote: IpVote
|
2021-01-26 13:11:22 +00:00
|
|
|
enrAutoUpdate: bool
|
2021-05-31 09:15:54 +00:00
|
|
|
talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of
|
|
|
|
# overkill here, use sequence
|
2022-08-05 13:19:16 +00:00
|
|
|
handshakeTimeout: Duration
|
|
|
|
responseTimeout: Duration
|
2022-06-17 20:45:37 +00:00
|
|
|
rng*: ref HmacDrbgContext
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
PendingRequest = object
|
|
|
|
node: Node
|
|
|
|
message: seq[byte]
|
|
|
|
|
2022-04-22 14:46:47 +00:00
|
|
|
TalkProtocolHandler* = proc(
|
|
|
|
p: TalkProtocol, request: seq[byte],
|
2023-08-30 15:44:05 +00:00
|
|
|
fromId: NodeId, fromUdpAddress: Address,
|
|
|
|
node: Opt[Node]): seq[byte]
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [].}
|
2021-05-20 07:49:46 +00:00
|
|
|
|
2021-05-31 09:15:54 +00:00
|
|
|
TalkProtocol* = ref object of RootObj
|
|
|
|
protocolHandler*: TalkProtocolHandler
|
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
DiscResult*[T] = Result[T, cstring]
|
|
|
|
|
2022-02-02 14:29:45 +00:00
|
|
|
const
|
|
|
|
defaultDiscoveryConfig* = DiscoveryConfig(
|
|
|
|
tableIpLimits: DefaultTableIpLimits,
|
2022-08-05 13:19:16 +00:00
|
|
|
bitsPerHop: DefaultBitsPerHop,
|
|
|
|
handshakeTimeout: defaultHandshakeTimeout,
|
|
|
|
responseTimeout: defaultResponseTimeout
|
|
|
|
)
|
2022-02-02 14:29:45 +00:00
|
|
|
|
2024-06-18 16:09:27 +00:00
|
|
|
chronicles.formatIt(Opt[Port]): $it
|
|
|
|
chronicles.formatIt(Opt[IpAddress]): $it
|
2022-11-10 09:52:24 +00:00
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
proc addNode*(d: Protocol, node: Node): bool =
|
|
|
|
## Add `Node` to discovery routing table.
|
|
|
|
##
|
2020-11-26 17:20:15 +00:00
|
|
|
## Returns true only when `Node` was added as a new entry to a bucket in the
|
|
|
|
## routing table.
|
|
|
|
if d.routingTable.addNode(node) == Added:
|
2020-11-13 11:33:07 +00:00
|
|
|
return true
|
2020-11-26 17:20:15 +00:00
|
|
|
else:
|
|
|
|
return false
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
proc addNode*(d: Protocol, r: Record): bool =
|
|
|
|
## Add `Node` from a `Record` to discovery routing table.
|
|
|
|
##
|
|
|
|
## Returns false only if no valid `Node` can be created from the `Record` or
|
|
|
|
## on the conditions of `addNode` from a `Node`.
|
2024-06-27 14:18:21 +00:00
|
|
|
d.addNode(Node.fromRecord(r))
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2024-06-27 14:18:21 +00:00
|
|
|
proc addNode*(d: Protocol, uri: string): bool =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Add `Node` from a ENR URI to discovery routing table.
|
|
|
|
##
|
|
|
|
## Returns false if no valid ENR URI, or on the conditions of `addNode` from
|
|
|
|
## an `Record`.
|
2024-06-27 14:18:21 +00:00
|
|
|
let r = enr.Record.fromURI(uri).valueOr:
|
|
|
|
return false
|
|
|
|
|
|
|
|
d.addNode(r)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2023-08-01 06:05:22 +00:00
|
|
|
func getNode*(d: Protocol, id: NodeId): Opt[Node] =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Get the node with id from the routing table.
|
|
|
|
d.routingTable.getNode(id)
|
|
|
|
|
|
|
|
proc randomNodes*(d: Protocol, maxAmount: int): seq[Node] =
|
|
|
|
## Get a `maxAmount` of random nodes from the local routing table.
|
|
|
|
d.routingTable.randomNodes(maxAmount)
|
|
|
|
|
|
|
|
proc randomNodes*(d: Protocol, maxAmount: int,
|
2023-08-01 06:05:22 +00:00
|
|
|
pred: proc(x: Node): bool {.raises: [], gcsafe, noSideEffect.}): seq[Node] =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Get a `maxAmount` of random nodes from the local routing table with the
|
|
|
|
## `pred` predicate function applied as filter on the nodes selected.
|
|
|
|
d.routingTable.randomNodes(maxAmount, pred)
|
|
|
|
|
|
|
|
proc randomNodes*(d: Protocol, maxAmount: int,
|
|
|
|
enrField: (string, seq[byte])): seq[Node] =
|
|
|
|
## Get a `maxAmount` of random nodes from the local routing table. The
|
|
|
|
## the nodes selected are filtered by provided `enrField`.
|
|
|
|
d.randomNodes(maxAmount, proc(x: Node): bool = x.record.contains(enrField))
|
|
|
|
|
2023-08-01 06:05:22 +00:00
|
|
|
func neighbours*(d: Protocol, id: NodeId, k: int = BUCKET_SIZE,
|
2021-07-13 08:05:46 +00:00
|
|
|
seenOnly = false): seq[Node] =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Return up to k neighbours (closest node ids) of the given node id.
|
2021-07-13 08:05:46 +00:00
|
|
|
d.routingTable.neighbours(id, k, seenOnly)
|
|
|
|
|
2023-08-01 06:05:22 +00:00
|
|
|
func neighboursAtDistances*(d: Protocol, distances: seq[uint16],
|
2021-07-13 08:05:46 +00:00
|
|
|
k: int = BUCKET_SIZE, seenOnly = false): seq[Node] =
|
|
|
|
## Return up to k neighbours (closest node ids) at given distances.
|
|
|
|
d.routingTable.neighboursAtDistances(distances, k, seenOnly)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2023-08-01 06:05:22 +00:00
|
|
|
func nodesDiscovered*(d: Protocol): int = d.routingTable.len
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
func privKey*(d: Protocol): lent PrivateKey =
|
|
|
|
d.privateKey
|
|
|
|
|
|
|
|
func getRecord*(d: Protocol): Record =
|
|
|
|
## Get the ENR of the local node.
|
|
|
|
d.localNode.record
|
|
|
|
|
2023-08-01 06:05:22 +00:00
|
|
|
func updateRecord*(
|
2021-12-20 12:14:50 +00:00
|
|
|
d: Protocol, enrFields: openArray[(string, seq[byte])]): DiscResult[void] =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Update the ENR of the local node with provided `enrFields` k:v pairs.
|
|
|
|
let fields = mapIt(enrFields, toFieldPair(it[0], it[1]))
|
2024-06-24 12:57:39 +00:00
|
|
|
d.localNode.record.update(d.privateKey, extraFields = fields)
|
2020-11-13 11:33:07 +00:00
|
|
|
# TODO: Would it make sense to actively ping ("broadcast") to all the peers
|
|
|
|
# we stored a handshake with in order to get that ENR updated?
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc sendTo(d: Protocol, a: Address, data: seq[byte]): Future[void] {.async: (raises: []).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
let ta = initTAddress(a.ip, a.port)
|
2024-01-12 16:35:44 +00:00
|
|
|
try:
|
|
|
|
await d.transp.sendTo(ta, data)
|
|
|
|
except CatchableError as e:
|
|
|
|
# Could be `TransportUseClosedError` in case the transport is already
|
|
|
|
# closed, or could be `TransportOsError` in case of a socket error.
|
|
|
|
# In the latter case this would probably mostly occur if the network
|
|
|
|
# interface underneath gets disconnected or similar.
|
|
|
|
# It could also be an "Operation not permitted" error, which would
|
|
|
|
# indicate a firewall restriction kicking in.
|
|
|
|
# TODO: Should this kind of error be propagated upwards? Probably, but
|
|
|
|
# it should not stop the process as that would reset the discovery
|
|
|
|
# progress in case there is even a small window of no connection.
|
|
|
|
# One case that needs this error available upwards is when revalidating
|
|
|
|
# nodes. Else the revalidation might end up clearing the routing tabl
|
|
|
|
# because of ping failures due to own network connection failure.
|
2024-04-24 11:38:52 +00:00
|
|
|
warn "Discovery send failed", msg = e.msg, address = $ta
|
2024-01-12 16:35:44 +00:00
|
|
|
|
|
|
|
proc send*(d: Protocol, a: Address, data: seq[byte]) =
|
|
|
|
asyncSpawn sendTo(d, a, data)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
proc send(d: Protocol, n: Node, data: seq[byte]) =
|
|
|
|
doAssert(n.address.isSome())
|
|
|
|
d.send(n.address.get(), data)
|
|
|
|
|
|
|
|
proc sendNodes(d: Protocol, toId: NodeId, toAddr: Address, reqId: RequestId,
|
2021-12-20 12:14:50 +00:00
|
|
|
nodes: openArray[Node]) =
|
2020-11-13 11:33:07 +00:00
|
|
|
proc sendNodes(d: Protocol, toId: NodeId, toAddr: Address,
|
|
|
|
message: NodesMessage, reqId: RequestId) {.nimcall.} =
|
|
|
|
let (data, _) = encodeMessagePacket(d.rng[], d.codec, toId, toAddr,
|
|
|
|
encodeMessage(message, reqId))
|
|
|
|
|
|
|
|
trace "Respond message packet", dstId = toId, address = toAddr,
|
|
|
|
kind = MessageKind.nodes
|
|
|
|
d.send(toAddr, data)
|
|
|
|
|
|
|
|
if nodes.len == 0:
|
|
|
|
# In case of 0 nodes, a reply is still needed
|
|
|
|
d.sendNodes(toId, toAddr, NodesMessage(total: 1, enrs: @[]), reqId)
|
|
|
|
return
|
|
|
|
|
|
|
|
var message: NodesMessage
|
|
|
|
# TODO: Do the total calculation based on the max UDP packet size we want to
|
|
|
|
# send and the ENR size of all (max 16) nodes.
|
|
|
|
# Which UDP packet size to take? 1280? 576?
|
|
|
|
message.total = ceil(nodes.len / maxNodesPerMessage).uint32
|
|
|
|
|
|
|
|
for i in 0 ..< nodes.len:
|
|
|
|
message.enrs.add(nodes[i].record)
|
|
|
|
if message.enrs.len == maxNodesPerMessage:
|
|
|
|
d.sendNodes(toId, toAddr, message, reqId)
|
|
|
|
message.enrs.setLen(0)
|
|
|
|
|
|
|
|
if message.enrs.len != 0:
|
|
|
|
d.sendNodes(toId, toAddr, message, reqId)
|
|
|
|
|
|
|
|
proc handlePing(d: Protocol, fromId: NodeId, fromAddr: Address,
|
|
|
|
ping: PingMessage, reqId: RequestId) =
|
2021-01-25 16:26:18 +00:00
|
|
|
let pong = PongMessage(enrSeq: d.localNode.record.seqNum, ip: fromAddr.ip,
|
|
|
|
port: fromAddr.port.uint16)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
let (data, _) = encodeMessagePacket(d.rng[], d.codec, fromId, fromAddr,
|
|
|
|
encodeMessage(pong, reqId))
|
|
|
|
|
|
|
|
trace "Respond message packet", dstId = fromId, address = fromAddr,
|
|
|
|
kind = MessageKind.pong
|
|
|
|
d.send(fromAddr, data)
|
|
|
|
|
|
|
|
proc handleFindNode(d: Protocol, fromId: NodeId, fromAddr: Address,
|
|
|
|
fn: FindNodeMessage, reqId: RequestId) =
|
|
|
|
if fn.distances.len == 0:
|
|
|
|
d.sendNodes(fromId, fromAddr, reqId, [])
|
|
|
|
elif fn.distances.contains(0):
|
|
|
|
# A request for our own record.
|
|
|
|
# It would be a weird request if there are more distances next to 0
|
|
|
|
# requested, so in this case lets just pass only our own. TODO: OK?
|
|
|
|
d.sendNodes(fromId, fromAddr, reqId, [d.localNode])
|
|
|
|
else:
|
|
|
|
# TODO: Still deduplicate also?
|
2021-07-13 08:05:46 +00:00
|
|
|
if fn.distances.all(proc (x: uint16): bool = return x <= 256):
|
2020-11-13 11:33:07 +00:00
|
|
|
d.sendNodes(fromId, fromAddr, reqId,
|
|
|
|
d.routingTable.neighboursAtDistances(fn.distances, seenOnly = true))
|
|
|
|
else:
|
|
|
|
# At least one invalid distance, but the polite node we are, still respond
|
|
|
|
# with empty nodes.
|
|
|
|
d.sendNodes(fromId, fromAddr, reqId, [])
|
|
|
|
|
|
|
|
proc handleTalkReq(d: Protocol, fromId: NodeId, fromAddr: Address,
|
2023-08-30 15:44:05 +00:00
|
|
|
talkreq: TalkReqMessage, reqId: RequestId, node: Opt[Node]) =
|
2021-05-31 09:15:54 +00:00
|
|
|
let talkProtocol = d.talkProtocols.getOrDefault(talkreq.protocol)
|
2021-05-20 07:49:46 +00:00
|
|
|
|
|
|
|
let talkresp =
|
2021-05-31 09:15:54 +00:00
|
|
|
if talkProtocol.isNil() or talkProtocol.protocolHandler.isNil():
|
2021-05-20 07:49:46 +00:00
|
|
|
# Protocol identifier that is not registered and thus not supported. An
|
|
|
|
# empty response is send as per specification.
|
|
|
|
TalkRespMessage(response: @[])
|
|
|
|
else:
|
2021-05-31 09:15:54 +00:00
|
|
|
TalkRespMessage(response: talkProtocol.protocolHandler(talkProtocol,
|
2023-08-30 15:44:05 +00:00
|
|
|
talkreq.request, fromId, fromAddr, node))
|
2020-11-13 11:33:07 +00:00
|
|
|
let (data, _) = encodeMessagePacket(d.rng[], d.codec, fromId, fromAddr,
|
|
|
|
encodeMessage(talkresp, reqId))
|
|
|
|
|
|
|
|
trace "Respond message packet", dstId = fromId, address = fromAddr,
|
|
|
|
kind = MessageKind.talkresp
|
|
|
|
d.send(fromAddr, data)
|
|
|
|
|
2023-08-30 15:44:05 +00:00
|
|
|
proc handleMessage(
|
|
|
|
d: Protocol, srcId: NodeId, fromAddr: Address,
|
|
|
|
message: Message, node: Opt[Node] = Opt.none(Node)) =
|
2020-11-13 11:33:07 +00:00
|
|
|
case message.kind
|
|
|
|
of ping:
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_message_requests_incoming.inc()
|
2020-11-13 11:33:07 +00:00
|
|
|
d.handlePing(srcId, fromAddr, message.ping, message.reqId)
|
2022-02-03 14:51:08 +00:00
|
|
|
of findNode:
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_message_requests_incoming.inc()
|
2022-02-03 14:51:08 +00:00
|
|
|
d.handleFindNode(srcId, fromAddr, message.findNode, message.reqId)
|
|
|
|
of talkReq:
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_message_requests_incoming.inc()
|
2023-08-30 15:44:05 +00:00
|
|
|
d.handleTalkReq(srcId, fromAddr, message.talkReq, message.reqId, node)
|
2022-02-03 14:51:08 +00:00
|
|
|
of regTopic, topicQuery:
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_message_requests_incoming.inc()
|
|
|
|
discovery_message_requests_incoming.inc(labelValues = ["no_response"])
|
2020-11-13 11:33:07 +00:00
|
|
|
trace "Received unimplemented message kind", kind = message.kind,
|
|
|
|
origin = fromAddr
|
|
|
|
else:
|
2024-06-18 16:09:27 +00:00
|
|
|
var waiter: Future[Opt[Message]]
|
2020-11-13 11:33:07 +00:00
|
|
|
if d.awaitedMessages.take((srcId, message.reqId), waiter):
|
2024-06-18 16:09:27 +00:00
|
|
|
waiter.complete(Opt.some(message))
|
2020-11-13 11:33:07 +00:00
|
|
|
else:
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_unsolicited_messages.inc()
|
2020-11-13 11:33:07 +00:00
|
|
|
trace "Timed out or unrequested message", kind = message.kind,
|
|
|
|
origin = fromAddr
|
|
|
|
|
2023-08-01 06:05:22 +00:00
|
|
|
func registerTalkProtocol*(d: Protocol, protocolId: seq[byte],
|
2021-05-31 09:15:54 +00:00
|
|
|
protocol: TalkProtocol): DiscResult[void] =
|
2021-05-20 07:49:46 +00:00
|
|
|
# Currently allow only for one handler per talk protocol.
|
2021-05-31 09:15:54 +00:00
|
|
|
if d.talkProtocols.hasKeyOrPut(protocolId, protocol):
|
2021-05-20 07:49:46 +00:00
|
|
|
err("Protocol identifier already registered")
|
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
proc sendWhoareyou(d: Protocol, toId: NodeId, a: Address,
|
2023-06-26 09:30:33 +00:00
|
|
|
requestNonce: AESGCMNonce, node: Opt[Node]) =
|
2021-12-20 12:14:50 +00:00
|
|
|
let key = HandshakeKey(nodeId: toId, address: a)
|
2020-11-13 11:33:07 +00:00
|
|
|
if not d.codec.hasHandshake(key):
|
|
|
|
let
|
2024-06-18 16:09:27 +00:00
|
|
|
recordSeq =
|
|
|
|
if node.isSome():
|
|
|
|
node.get().record.seqNum
|
|
|
|
else:
|
|
|
|
0
|
|
|
|
pubkey = node.map(proc(node: Node): PublicKey = node.pubkey)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
let data = encodeWhoareyouPacket(d.rng[], d.codec, toId, a, requestNonce,
|
|
|
|
recordSeq, pubkey)
|
2022-08-05 13:19:16 +00:00
|
|
|
sleepAsync(d.handshakeTimeout).addCallback() do(data: pointer):
|
2020-11-13 11:33:07 +00:00
|
|
|
# TODO: should we still provide cancellation in case handshake completes
|
|
|
|
# correctly?
|
|
|
|
d.codec.handshakes.del(key)
|
|
|
|
|
|
|
|
trace "Send whoareyou", dstId = toId, address = a
|
|
|
|
d.send(a, data)
|
|
|
|
else:
|
|
|
|
debug "Node with this id already has ongoing handshake, ignoring packet"
|
|
|
|
|
2021-03-25 17:12:11 +00:00
|
|
|
proc receive*(d: Protocol, a: Address, packet: openArray[byte]) =
|
2020-11-13 11:33:07 +00:00
|
|
|
let decoded = d.codec.decodePacket(a, packet)
|
|
|
|
if decoded.isOk:
|
|
|
|
let packet = decoded[]
|
|
|
|
case packet.flag
|
|
|
|
of OrdinaryMessage:
|
|
|
|
if packet.messageOpt.isSome():
|
|
|
|
let message = packet.messageOpt.get()
|
|
|
|
trace "Received message packet", srcId = packet.srcId, address = a,
|
|
|
|
kind = message.kind
|
|
|
|
d.handleMessage(packet.srcId, a, message)
|
|
|
|
else:
|
|
|
|
trace "Not decryptable message packet received",
|
|
|
|
srcId = packet.srcId, address = a
|
|
|
|
d.sendWhoareyou(packet.srcId, a, packet.requestNonce,
|
|
|
|
d.getNode(packet.srcId))
|
|
|
|
|
|
|
|
of Flag.Whoareyou:
|
|
|
|
trace "Received whoareyou packet", address = a
|
|
|
|
var pr: PendingRequest
|
|
|
|
if d.pendingRequests.take(packet.whoareyou.requestNonce, pr):
|
|
|
|
let toNode = pr.node
|
|
|
|
# This is a node we previously contacted and thus must have an address.
|
|
|
|
doAssert(toNode.address.isSome())
|
|
|
|
let address = toNode.address.get()
|
|
|
|
let data = encodeHandshakePacket(d.rng[], d.codec, toNode.id,
|
|
|
|
address, pr.message, packet.whoareyou, toNode.pubkey)
|
|
|
|
|
|
|
|
trace "Send handshake message packet", dstId = toNode.id, address
|
|
|
|
d.send(toNode, data)
|
|
|
|
else:
|
|
|
|
debug "Timed out or unrequested whoareyou packet", address = a
|
|
|
|
of HandshakeMessage:
|
|
|
|
trace "Received handshake message packet", srcId = packet.srcIdHs,
|
|
|
|
address = a, kind = packet.message.kind
|
2023-08-30 15:44:05 +00:00
|
|
|
d.handleMessage(packet.srcIdHs, a, packet.message, packet.node)
|
2020-11-13 11:33:07 +00:00
|
|
|
# For a handshake message it is possible that we received an newer ENR.
|
|
|
|
# In that case we can add/update it to the routing table.
|
|
|
|
if packet.node.isSome():
|
|
|
|
let node = packet.node.get()
|
2021-11-22 18:45:38 +00:00
|
|
|
# Lets not add nodes without correct IP in the ENR to the routing table.
|
|
|
|
# The ENR could contain bogus IPs and although they would get removed
|
|
|
|
# on the next revalidation, one could spam these as the handshake
|
|
|
|
# message occurs on (first) incoming messages.
|
2020-11-13 11:33:07 +00:00
|
|
|
if node.address.isSome() and a == node.address.get():
|
2020-11-26 17:20:15 +00:00
|
|
|
if d.addNode(node):
|
|
|
|
trace "Added new node to routing table after handshake", node
|
2020-11-13 11:33:07 +00:00
|
|
|
else:
|
2020-11-26 17:20:15 +00:00
|
|
|
trace "Packet decoding error", error = decoded.error, address = a
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
proc processClient(transp: DatagramTransport, raddr: TransportAddress):
|
2024-01-22 16:08:08 +00:00
|
|
|
Future[void] {.async: (raises: []).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
let proto = getUserData[Protocol](transp)
|
|
|
|
|
|
|
|
# TODO: should we use `peekMessage()` to avoid allocation?
|
|
|
|
let buf = try: transp.getMessage()
|
2024-01-12 08:49:06 +00:00
|
|
|
except TransportError as e:
|
2020-11-13 11:33:07 +00:00
|
|
|
# This is likely to be local network connection issues.
|
|
|
|
warn "Transport getMessage", exception = e.name, msg = e.msg
|
|
|
|
return
|
|
|
|
|
2024-04-24 11:38:52 +00:00
|
|
|
proto.receive(Address(ip: raddr.toIpAddress(), port: raddr.port), buf)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
proc replaceNode(d: Protocol, n: Node) =
|
|
|
|
if n.record notin d.bootstrapRecords:
|
|
|
|
d.routingTable.replaceNode(n)
|
|
|
|
else:
|
|
|
|
# For now we never remove bootstrap nodes. It might make sense to actually
|
|
|
|
# do so and to retry them only in case we drop to a really low amount of
|
|
|
|
# peers in the routing table.
|
|
|
|
debug "Message request to bootstrap node failed", enr = toURI(n.record)
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
# TODO: This could be improved to do the clean-up immediately in case a non
|
2020-11-13 11:33:07 +00:00
|
|
|
# whoareyou response does arrive, but we would need to store the AuthTag
|
|
|
|
# somewhere
|
|
|
|
proc registerRequest(d: Protocol, n: Node, message: seq[byte],
|
2021-03-25 17:12:11 +00:00
|
|
|
nonce: AESGCMNonce) =
|
2020-11-13 11:33:07 +00:00
|
|
|
let request = PendingRequest(node: n, message: message)
|
|
|
|
if not d.pendingRequests.hasKeyOrPut(nonce, request):
|
2022-08-05 13:19:16 +00:00
|
|
|
sleepAsync(d.responseTimeout).addCallback() do(data: pointer):
|
2020-11-13 11:33:07 +00:00
|
|
|
d.pendingRequests.del(nonce)
|
|
|
|
|
|
|
|
proc waitMessage(d: Protocol, fromNode: Node, reqId: RequestId):
|
2024-06-18 16:09:27 +00:00
|
|
|
Future[Opt[Message]] {.async: (raw: true, raises: [CancelledError]).} =
|
|
|
|
let retFuture = Future[Opt[Message]].Raising([CancelledError]).init("discv5.waitMessage")
|
2020-11-13 11:33:07 +00:00
|
|
|
let key = (fromNode.id, reqId)
|
2022-08-05 13:19:16 +00:00
|
|
|
sleepAsync(d.responseTimeout).addCallback() do(data: pointer):
|
2020-11-13 11:33:07 +00:00
|
|
|
d.awaitedMessages.del(key)
|
2024-06-13 10:11:25 +00:00
|
|
|
if not retFuture.finished:
|
2024-06-18 16:09:27 +00:00
|
|
|
retFuture.complete(Opt.none(Message))
|
2024-06-13 10:11:25 +00:00
|
|
|
d.awaitedMessages[key] = retFuture
|
|
|
|
retFuture
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
proc waitNodes(d: Protocol, fromNode: Node, reqId: RequestId):
|
2024-06-10 11:47:35 +00:00
|
|
|
Future[DiscResult[seq[Record]]] {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Wait for one or more nodes replies.
|
|
|
|
##
|
|
|
|
## The first reply will hold the total number of replies expected, and based
|
|
|
|
## on that, more replies will be awaited.
|
|
|
|
## If one reply is lost here (timed out), others are ignored too.
|
|
|
|
## Same counts for out of order receival.
|
|
|
|
var op = await d.waitMessage(fromNode, reqId)
|
2021-06-09 12:55:00 +00:00
|
|
|
if op.isSome:
|
|
|
|
if op.get.kind == nodes:
|
|
|
|
var res = op.get.nodes.enrs
|
|
|
|
let total = op.get.nodes.total
|
|
|
|
for i in 1 ..< total:
|
|
|
|
op = await d.waitMessage(fromNode, reqId)
|
|
|
|
if op.isSome and op.get.kind == nodes:
|
|
|
|
res.add(op.get.nodes.enrs)
|
|
|
|
else:
|
|
|
|
# No error on this as we received some nodes.
|
|
|
|
break
|
|
|
|
return ok(res)
|
|
|
|
else:
|
|
|
|
discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"])
|
|
|
|
return err("Invalid response to find node message")
|
2020-11-13 11:33:07 +00:00
|
|
|
else:
|
2021-06-09 12:55:00 +00:00
|
|
|
discovery_message_requests_outgoing.inc(labelValues = ["no_response"])
|
2020-11-13 11:33:07 +00:00
|
|
|
return err("Nodes message not received in time")
|
|
|
|
|
|
|
|
proc sendMessage*[T: SomeMessage](d: Protocol, toNode: Node, m: T):
|
2021-03-25 17:12:11 +00:00
|
|
|
RequestId =
|
2020-11-13 11:33:07 +00:00
|
|
|
doAssert(toNode.address.isSome())
|
|
|
|
let
|
|
|
|
address = toNode.address.get()
|
|
|
|
reqId = RequestId.init(d.rng[])
|
|
|
|
message = encodeMessage(m, reqId)
|
|
|
|
|
|
|
|
let (data, nonce) = encodeMessagePacket(d.rng[], d.codec, toNode.id,
|
|
|
|
address, message)
|
|
|
|
|
|
|
|
d.registerRequest(toNode, message, nonce)
|
|
|
|
trace "Send message packet", dstId = toNode.id, address, kind = messageKind(T)
|
|
|
|
d.send(toNode, data)
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_message_requests_outgoing.inc()
|
2020-11-13 11:33:07 +00:00
|
|
|
return reqId
|
|
|
|
|
|
|
|
proc ping*(d: Protocol, toNode: Node):
|
2024-06-10 11:47:35 +00:00
|
|
|
Future[DiscResult[PongMessage]] {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Send a discovery ping message.
|
|
|
|
##
|
|
|
|
## Returns the received pong message or an error.
|
|
|
|
let reqId = d.sendMessage(toNode,
|
|
|
|
PingMessage(enrSeq: d.localNode.record.seqNum))
|
|
|
|
let resp = await d.waitMessage(toNode, reqId)
|
|
|
|
|
2021-06-09 12:55:00 +00:00
|
|
|
if resp.isSome():
|
|
|
|
if resp.get().kind == pong:
|
|
|
|
d.routingTable.setJustSeen(toNode)
|
|
|
|
return ok(resp.get().pong)
|
|
|
|
else:
|
|
|
|
d.replaceNode(toNode)
|
|
|
|
discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"])
|
|
|
|
return err("Invalid response to ping message")
|
2020-11-13 11:33:07 +00:00
|
|
|
else:
|
|
|
|
d.replaceNode(toNode)
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_message_requests_outgoing.inc(labelValues = ["no_response"])
|
2020-11-13 11:33:07 +00:00
|
|
|
return err("Pong message not received in time")
|
|
|
|
|
2021-07-13 08:05:46 +00:00
|
|
|
proc findNode*(d: Protocol, toNode: Node, distances: seq[uint16]):
|
2024-06-10 11:47:35 +00:00
|
|
|
Future[DiscResult[seq[Node]]] {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Send a discovery findNode message.
|
|
|
|
##
|
|
|
|
## Returns the received nodes or an error.
|
|
|
|
## Received ENRs are already validated and converted to `Node`.
|
|
|
|
let reqId = d.sendMessage(toNode, FindNodeMessage(distances: distances))
|
|
|
|
let nodes = await d.waitNodes(toNode, reqId)
|
|
|
|
|
|
|
|
if nodes.isOk:
|
2021-09-13 12:20:26 +00:00
|
|
|
let res = verifyNodesRecords(nodes.get(), toNode, findNodeResultLimit, distances)
|
2020-11-13 11:33:07 +00:00
|
|
|
d.routingTable.setJustSeen(toNode)
|
|
|
|
return ok(res)
|
|
|
|
else:
|
|
|
|
d.replaceNode(toNode)
|
|
|
|
return err(nodes.error)
|
|
|
|
|
2022-02-03 14:51:08 +00:00
|
|
|
proc talkReq*(d: Protocol, toNode: Node, protocol, request: seq[byte]):
|
2024-06-10 11:47:35 +00:00
|
|
|
Future[DiscResult[seq[byte]]] {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Send a discovery talkreq message.
|
|
|
|
##
|
|
|
|
## Returns the received talkresp message or an error.
|
|
|
|
let reqId = d.sendMessage(toNode,
|
|
|
|
TalkReqMessage(protocol: protocol, request: request))
|
|
|
|
let resp = await d.waitMessage(toNode, reqId)
|
|
|
|
|
2021-06-09 12:55:00 +00:00
|
|
|
if resp.isSome():
|
2022-02-03 14:51:08 +00:00
|
|
|
if resp.get().kind == talkResp:
|
2021-06-09 12:55:00 +00:00
|
|
|
d.routingTable.setJustSeen(toNode)
|
2022-02-03 14:51:08 +00:00
|
|
|
return ok(resp.get().talkResp.response)
|
2021-06-09 12:55:00 +00:00
|
|
|
else:
|
|
|
|
d.replaceNode(toNode)
|
|
|
|
discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"])
|
|
|
|
return err("Invalid response to talk request message")
|
2020-11-13 11:33:07 +00:00
|
|
|
else:
|
|
|
|
d.replaceNode(toNode)
|
2021-01-13 20:44:17 +00:00
|
|
|
discovery_message_requests_outgoing.inc(labelValues = ["no_response"])
|
2020-11-13 11:33:07 +00:00
|
|
|
return err("Talk response message not received in time")
|
|
|
|
|
2023-08-01 06:05:22 +00:00
|
|
|
func lookupDistances*(target, dest: NodeId): seq[uint16] =
|
2021-10-07 14:03:12 +00:00
|
|
|
let td = logDistance(target, dest)
|
2021-09-10 15:26:22 +00:00
|
|
|
let tdAsInt = int(td)
|
2020-11-13 11:33:07 +00:00
|
|
|
result.add(td)
|
2021-09-10 15:26:22 +00:00
|
|
|
var i = 1
|
2020-11-13 11:33:07 +00:00
|
|
|
while result.len < lookupRequestLimit:
|
2022-11-15 11:36:16 +00:00
|
|
|
if tdAsInt + i <= 256:
|
2021-09-10 15:26:22 +00:00
|
|
|
result.add(td + uint16(i))
|
|
|
|
if tdAsInt - i > 0:
|
|
|
|
result.add(td - uint16(i))
|
2020-11-13 11:33:07 +00:00
|
|
|
inc i
|
|
|
|
|
|
|
|
proc lookupWorker(d: Protocol, destNode: Node, target: NodeId):
|
2024-06-10 11:47:35 +00:00
|
|
|
Future[seq[Node]] {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
let dists = lookupDistances(target, destNode.id)
|
|
|
|
|
2022-02-03 14:51:08 +00:00
|
|
|
# Instead of doing max `lookupRequestLimit` findNode requests, make use
|
2020-12-17 14:42:04 +00:00
|
|
|
# of the discv5.1 functionality to request nodes for multiple distances.
|
|
|
|
let r = await d.findNode(destNode, dists)
|
|
|
|
if r.isOk:
|
|
|
|
result.add(r[])
|
|
|
|
|
|
|
|
# Attempt to add all nodes discovered
|
|
|
|
for n in result:
|
|
|
|
discard d.addNode(n)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc lookup*(d: Protocol, target: NodeId): Future[seq[Node]] {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Perform a lookup for the given target, return the closest n nodes to the
|
|
|
|
## target. Maximum value for n is `BUCKET_SIZE`.
|
2020-12-15 12:24:57 +00:00
|
|
|
# `closestNodes` holds the k closest nodes to target found, sorted by distance
|
|
|
|
# Unvalidated nodes are used for requests as a form of validation.
|
|
|
|
var closestNodes = d.routingTable.neighbours(target, BUCKET_SIZE,
|
|
|
|
seenOnly = false)
|
|
|
|
|
2024-06-10 19:19:25 +00:00
|
|
|
var asked, seen = HashSet[NodeId]()
|
2020-12-15 12:24:57 +00:00
|
|
|
asked.incl(d.localNode.id) # No need to ask our own node
|
|
|
|
seen.incl(d.localNode.id) # No need to discover our own node
|
|
|
|
for node in closestNodes:
|
2020-11-13 11:33:07 +00:00
|
|
|
seen.incl(node.id)
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
var pendingQueries = newSeqOfCap[Future[seq[Node]].Raising([CancelledError])](alpha)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
while true:
|
|
|
|
var i = 0
|
2020-12-15 12:24:57 +00:00
|
|
|
# Doing `alpha` amount of requests at once as long as closer non queried
|
|
|
|
# nodes are discovered.
|
|
|
|
while i < closestNodes.len and pendingQueries.len < alpha:
|
|
|
|
let n = closestNodes[i]
|
2020-11-13 11:33:07 +00:00
|
|
|
if not asked.containsOrIncl(n.id):
|
|
|
|
pendingQueries.add(d.lookupWorker(n, target))
|
|
|
|
inc i
|
|
|
|
|
|
|
|
trace "discv5 pending queries", total = pendingQueries.len
|
|
|
|
|
|
|
|
if pendingQueries.len == 0:
|
|
|
|
break
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
let query =
|
|
|
|
try:
|
|
|
|
await one(pendingQueries)
|
|
|
|
except ValueError:
|
|
|
|
raiseAssert("pendingQueries should not have been empty")
|
|
|
|
|
2020-12-15 11:41:53 +00:00
|
|
|
trace "Got discv5 lookup query response"
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2020-12-15 11:41:53 +00:00
|
|
|
let index = pendingQueries.find(query)
|
|
|
|
if index != -1:
|
|
|
|
pendingQueries.del(index)
|
|
|
|
else:
|
2020-12-15 23:07:49 +00:00
|
|
|
error "Resulting query should have been in the pending queries"
|
2020-12-15 11:41:53 +00:00
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
# future.read is possible here but await is recommended (avoids also FuturePendingError)
|
|
|
|
let nodes = await query
|
2020-11-13 11:33:07 +00:00
|
|
|
for n in nodes:
|
|
|
|
if not seen.containsOrIncl(n.id):
|
2020-12-15 12:24:57 +00:00
|
|
|
# If it wasn't seen before, insert node while remaining sorted
|
|
|
|
closestNodes.insert(n, closestNodes.lowerBound(n,
|
|
|
|
proc(x: Node, n: Node): int =
|
2021-10-07 14:03:12 +00:00
|
|
|
cmp(distance(x.id, target), distance(n.id, target))
|
2020-12-15 12:24:57 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
if closestNodes.len > BUCKET_SIZE:
|
|
|
|
closestNodes.del(closestNodes.high())
|
|
|
|
|
2021-01-05 14:03:38 +00:00
|
|
|
d.lastLookup = now(chronos.Moment)
|
2020-12-15 12:24:57 +00:00
|
|
|
return closestNodes
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2020-12-17 14:22:48 +00:00
|
|
|
proc query*(d: Protocol, target: NodeId, k = BUCKET_SIZE): Future[seq[Node]]
|
2024-06-10 11:47:35 +00:00
|
|
|
{.async: (raises: [CancelledError]).} =
|
2020-12-17 14:22:48 +00:00
|
|
|
## Query k nodes for the given target, returns all nodes found, including the
|
|
|
|
## nodes queried.
|
|
|
|
##
|
|
|
|
## This will take k nodes from the routing table closest to target and
|
|
|
|
## query them for nodes closest to target. If there are less than k nodes in
|
|
|
|
## the routing table, nodes returned by the first queries will be used.
|
|
|
|
var queryBuffer = d.routingTable.neighbours(target, k, seenOnly = false)
|
|
|
|
|
2024-06-10 19:19:25 +00:00
|
|
|
var asked, seen = HashSet[NodeId]()
|
2020-12-17 14:22:48 +00:00
|
|
|
asked.incl(d.localNode.id) # No need to ask our own node
|
|
|
|
seen.incl(d.localNode.id) # No need to discover our own node
|
|
|
|
for node in queryBuffer:
|
|
|
|
seen.incl(node.id)
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
var pendingQueries = newSeqOfCap[Future[seq[Node]].Raising([CancelledError])](alpha)
|
2020-12-17 14:22:48 +00:00
|
|
|
|
|
|
|
while true:
|
|
|
|
var i = 0
|
|
|
|
while i < min(queryBuffer.len, k) and pendingQueries.len < alpha:
|
|
|
|
let n = queryBuffer[i]
|
|
|
|
if not asked.containsOrIncl(n.id):
|
|
|
|
pendingQueries.add(d.lookupWorker(n, target))
|
|
|
|
inc i
|
|
|
|
|
|
|
|
trace "discv5 pending queries", total = pendingQueries.len
|
|
|
|
|
|
|
|
if pendingQueries.len == 0:
|
|
|
|
break
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
let query =
|
|
|
|
try:
|
|
|
|
await one(pendingQueries)
|
|
|
|
except ValueError:
|
|
|
|
raiseAssert("pendingQueries should not have been empty")
|
|
|
|
|
2020-12-17 14:22:48 +00:00
|
|
|
trace "Got discv5 lookup query response"
|
|
|
|
|
|
|
|
let index = pendingQueries.find(query)
|
|
|
|
if index != -1:
|
|
|
|
pendingQueries.del(index)
|
|
|
|
else:
|
|
|
|
error "Resulting query should have been in the pending queries"
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
# future.read is possible here but await is recommended (avoids also FuturePendingError)
|
|
|
|
let nodes = await query
|
2020-12-17 14:22:48 +00:00
|
|
|
for n in nodes:
|
|
|
|
if not seen.containsOrIncl(n.id):
|
|
|
|
queryBuffer.add(n)
|
|
|
|
|
2021-01-05 14:03:38 +00:00
|
|
|
d.lastLookup = now(chronos.Moment)
|
2020-12-17 14:22:48 +00:00
|
|
|
return queryBuffer
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc queryRandom*(d: Protocol): Future[seq[Node]] {.async: (raw: true, raises: [CancelledError]).} =
|
2020-12-17 14:22:48 +00:00
|
|
|
## Perform a query for a random target, return all nodes discovered.
|
2021-11-22 22:14:37 +00:00
|
|
|
d.query(NodeId.random(d.rng[]))
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2021-01-05 14:03:38 +00:00
|
|
|
proc queryRandom*(d: Protocol, enrField: (string, seq[byte])):
|
2024-06-10 11:47:35 +00:00
|
|
|
Future[seq[Node]] {.async: (raises: [CancelledError]).} =
|
2021-01-05 14:03:38 +00:00
|
|
|
## Perform a query for a random target, return all nodes discovered which
|
|
|
|
## contain enrField.
|
|
|
|
let nodes = await d.queryRandom()
|
|
|
|
var filtered: seq[Node]
|
|
|
|
for n in nodes:
|
|
|
|
if n.record.contains(enrField):
|
|
|
|
filtered.add(n)
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc resolve*(d: Protocol, id: NodeId): Future[Opt[Node]] {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
## Resolve a `Node` based on provided `NodeId`.
|
|
|
|
##
|
|
|
|
## This will first look in the own routing table. If the node is known, it
|
|
|
|
## will try to contact if for newer information. If node is not known or it
|
|
|
|
## does not reply, a lookup is done to see if it can find a (newer) record of
|
|
|
|
## the node on the network.
|
2021-12-02 10:10:26 +00:00
|
|
|
if id == d.localNode.id:
|
2023-06-26 09:30:33 +00:00
|
|
|
return Opt.some(d.localNode)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
let node = d.getNode(id)
|
|
|
|
if node.isSome():
|
2021-07-13 08:05:46 +00:00
|
|
|
let request = await d.findNode(node.get(), @[0'u16])
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
# TODO: Handle failures better. E.g. stop on different failures than timeout
|
|
|
|
if request.isOk() and request[].len > 0:
|
2023-06-26 09:30:33 +00:00
|
|
|
return Opt.some(request[][0])
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
let discovered = await d.lookup(id)
|
|
|
|
for n in discovered:
|
|
|
|
if n.id == id:
|
|
|
|
if node.isSome() and node.get().record.seqNum >= n.record.seqNum:
|
|
|
|
return node
|
|
|
|
else:
|
2023-06-26 09:30:33 +00:00
|
|
|
return Opt.some(n)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
return node
|
|
|
|
|
2021-01-07 09:15:48 +00:00
|
|
|
proc seedTable*(d: Protocol) =
|
|
|
|
## Seed the table with known nodes.
|
|
|
|
for record in d.bootstrapRecords:
|
|
|
|
if d.addNode(record):
|
|
|
|
debug "Added bootstrap node", uri = toURI(record)
|
|
|
|
else:
|
|
|
|
debug "Bootstrap node could not be added", uri = toURI(record)
|
|
|
|
|
|
|
|
# TODO:
|
|
|
|
# Persistent stored nodes could be added to seed from here
|
|
|
|
# See: https://github.com/status-im/nim-eth/issues/189
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc populateTable*(d: Protocol) {.async: (raises: [CancelledError]).} =
|
2021-01-07 09:15:48 +00:00
|
|
|
## Do a set of initial lookups to quickly populate the table.
|
|
|
|
# start with a self target query (neighbour nodes)
|
|
|
|
let selfQuery = await d.query(d.localNode.id)
|
|
|
|
trace "Discovered nodes in self target query", nodes = selfQuery.len
|
|
|
|
|
|
|
|
# `initialLookups` random queries
|
|
|
|
for i in 0..<initialLookups:
|
|
|
|
let randomQuery = await d.queryRandom()
|
|
|
|
trace "Discovered nodes in random target query", nodes = randomQuery.len
|
|
|
|
|
|
|
|
debug "Total nodes in routing table after populate",
|
|
|
|
total = d.routingTable.len()
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc revalidateNode*(d: Protocol, n: Node) {.async: (raises: [CancelledError]).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
let pong = await d.ping(n)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
if pong.isOk():
|
2021-01-21 16:21:36 +00:00
|
|
|
let res = pong.get()
|
|
|
|
if res.enrSeq > n.record.seqNum:
|
2020-11-13 11:33:07 +00:00
|
|
|
# Request new ENR
|
2021-07-13 08:05:46 +00:00
|
|
|
let nodes = await d.findNode(n, @[0'u16])
|
2020-11-13 11:33:07 +00:00
|
|
|
if nodes.isOk() and nodes[].len > 0:
|
|
|
|
discard d.addNode(nodes[][0])
|
|
|
|
|
2021-01-21 16:21:36 +00:00
|
|
|
# Get IP and port from pong message and add it to the ip votes
|
2023-11-10 06:28:21 +00:00
|
|
|
let a = Address(ip: res.ip, port: Port(res.port))
|
2021-01-25 16:26:18 +00:00
|
|
|
d.ipVote.insert(n.id, a)
|
2021-01-21 16:21:36 +00:00
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc revalidateLoop(d: Protocol) {.async: (raises: []).} =
|
2021-01-05 14:03:38 +00:00
|
|
|
## Loop which revalidates the nodes in the routing table by sending the ping
|
|
|
|
## message.
|
2020-11-13 11:33:07 +00:00
|
|
|
try:
|
|
|
|
while true:
|
|
|
|
await sleepAsync(milliseconds(d.rng[].rand(revalidateMax)))
|
|
|
|
let n = d.routingTable.nodeToRevalidate()
|
|
|
|
if not n.isNil:
|
2022-10-07 15:20:03 +00:00
|
|
|
asyncSpawn d.revalidateNode(n)
|
2020-11-13 11:33:07 +00:00
|
|
|
except CancelledError:
|
|
|
|
trace "revalidateLoop canceled"
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc refreshLoop(d: Protocol) {.async: (raises: []).} =
|
2021-01-05 14:03:38 +00:00
|
|
|
## Loop that refreshes the routing table by starting a random query in case
|
|
|
|
## no queries were done since `refreshInterval` or more.
|
2021-01-21 16:21:36 +00:00
|
|
|
## It also refreshes the majority address voted for via pong responses.
|
2020-11-13 11:33:07 +00:00
|
|
|
try:
|
2021-01-07 09:15:48 +00:00
|
|
|
await d.populateTable()
|
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
while true:
|
2021-01-05 14:03:38 +00:00
|
|
|
let currentTime = now(chronos.Moment)
|
|
|
|
if currentTime > (d.lastLookup + refreshInterval):
|
|
|
|
let randomQuery = await d.queryRandom()
|
|
|
|
trace "Discovered nodes in random target query", nodes = randomQuery.len
|
|
|
|
debug "Total nodes in discv5 routing table", total = d.routingTable.len()
|
|
|
|
|
|
|
|
await sleepAsync(refreshInterval)
|
2020-11-13 11:33:07 +00:00
|
|
|
except CancelledError:
|
2021-01-05 14:03:38 +00:00
|
|
|
trace "refreshLoop canceled"
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2023-11-10 06:28:21 +00:00
|
|
|
proc updateExternalIp*(d: Protocol, extIp: IpAddress, udpPort: Port): bool =
|
2022-03-30 10:52:45 +00:00
|
|
|
var success = false
|
|
|
|
let
|
|
|
|
previous = d.localNode.address
|
|
|
|
res = d.localNode.update(d.privateKey,
|
2024-06-18 16:09:27 +00:00
|
|
|
ip = Opt.some(extIp), udpPort = Opt.some(udpPort))
|
2022-03-30 10:52:45 +00:00
|
|
|
|
|
|
|
if res.isErr:
|
|
|
|
warn "Failed updating ENR with newly discovered external address",
|
|
|
|
previous, newExtIp = extIp, newUdpPort = udpPort, error = res.error
|
|
|
|
else:
|
|
|
|
success = true
|
|
|
|
info "Updated ENR with newly discovered external address",
|
|
|
|
previous, newExtIp = extIp, newUdpPort = udpPort, uri = toURI(d.localNode.record)
|
|
|
|
return success
|
|
|
|
|
2024-06-10 11:47:35 +00:00
|
|
|
proc ipMajorityLoop(d: Protocol) {.async: (raises: []).} =
|
2021-01-29 14:05:18 +00:00
|
|
|
## When `enrAutoUpdate` is enabled, the IP:port combination returned
|
|
|
|
## by the majority will be used to update the local ENR.
|
|
|
|
## This should be safe as long as the routing table is not overwhelmed by
|
|
|
|
## malicious nodes trying to provide invalid addresses.
|
|
|
|
## Why is that?
|
|
|
|
## - Only one vote per NodeId is counted, and they are removed over time.
|
|
|
|
## - IP:port values are provided through the pong message. The local node
|
|
|
|
## initiates this by first sending a ping message. Unsolicited pong messages
|
|
|
|
## are ignored.
|
|
|
|
## - At interval pings are send to the least recently contacted node (tail of
|
|
|
|
## bucket) from a random bucket from the routing table.
|
|
|
|
## - Only messages that our node initiates (ping, findnode, talkreq) and that
|
|
|
|
## successfully get a response move a node to the head of the bucket.
|
|
|
|
## Additionally, findNode requests have typically a randomness to it, as they
|
|
|
|
## usually come from a query for random NodeId.
|
|
|
|
## - Currently, when a peer fails the respond, it gets replaced. It doesn't
|
|
|
|
## remain at the tail of the bucket.
|
|
|
|
## - There are IP limits on the buckets and the whole routing table.
|
2021-01-26 13:11:22 +00:00
|
|
|
try:
|
|
|
|
while true:
|
|
|
|
let majority = d.ipVote.majority()
|
|
|
|
if majority.isSome():
|
|
|
|
if d.localNode.address != majority:
|
|
|
|
let address = majority.get()
|
|
|
|
let previous = d.localNode.address
|
|
|
|
if d.enrAutoUpdate:
|
2022-03-30 10:52:45 +00:00
|
|
|
let success = d.updateExternalIp(address.ip, address.port)
|
|
|
|
if success:
|
2021-01-29 14:05:18 +00:00
|
|
|
discovery_enr_auto_update.inc()
|
2021-01-26 13:11:22 +00:00
|
|
|
else:
|
|
|
|
warn "Discovered new external address but ENR auto update is off",
|
|
|
|
majority, previous
|
|
|
|
else:
|
|
|
|
debug "Discovered external address matches current address", majority,
|
|
|
|
current = d.localNode.address
|
|
|
|
|
|
|
|
await sleepAsync(ipMajorityInterval)
|
|
|
|
except CancelledError:
|
|
|
|
trace "ipMajorityLoop canceled"
|
|
|
|
|
2022-02-02 14:29:45 +00:00
|
|
|
func init*(
|
|
|
|
T: type DiscoveryConfig,
|
|
|
|
tableIpLimit: uint,
|
|
|
|
bucketIpLimit: uint,
|
2022-08-05 13:19:16 +00:00
|
|
|
bitsPerHop: int,
|
|
|
|
handshakeTimeout: Duration,
|
|
|
|
responseTimeout: Duration
|
|
|
|
): T =
|
2022-02-02 14:29:45 +00:00
|
|
|
|
|
|
|
DiscoveryConfig(
|
|
|
|
tableIpLimits: TableIpLimits(
|
|
|
|
tableIpLimit: tableIpLimit,
|
|
|
|
bucketIpLimit: bucketIpLimit),
|
2022-08-05 13:19:16 +00:00
|
|
|
bitsPerHop: bitsPerHop,
|
|
|
|
handshakeTimeout: handshakeTimeout,
|
|
|
|
responseTimeout: responseTimeout
|
|
|
|
)
|
|
|
|
|
|
|
|
func init*(
|
|
|
|
T: type DiscoveryConfig,
|
|
|
|
tableIpLimit: uint,
|
|
|
|
bucketIpLimit: uint,
|
|
|
|
bitsPerHop: int): T =
|
|
|
|
|
|
|
|
DiscoveryConfig.init(
|
|
|
|
tableIpLimit,
|
|
|
|
bucketIpLimit,
|
|
|
|
bitsPerHop,
|
|
|
|
defaultHandshakeTimeout,
|
|
|
|
defaultResponseTimeout
|
2022-02-02 14:29:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc newProtocol*(
|
|
|
|
privKey: PrivateKey,
|
2024-06-18 16:09:27 +00:00
|
|
|
enrIp: Opt[IpAddress],
|
|
|
|
enrTcpPort, enrUdpPort: Opt[Port],
|
2022-02-02 14:29:45 +00:00
|
|
|
localEnrFields: openArray[(string, seq[byte])] = [],
|
|
|
|
bootstrapRecords: openArray[Record] = [],
|
2024-06-18 16:09:27 +00:00
|
|
|
previousRecord = Opt.none(enr.Record),
|
2022-02-02 14:29:45 +00:00
|
|
|
bindPort: Port,
|
|
|
|
bindIp = IPv4_any(),
|
|
|
|
enrAutoUpdate = false,
|
|
|
|
config = defaultDiscoveryConfig,
|
|
|
|
rng = newRng()):
|
|
|
|
Protocol =
|
2020-11-13 11:33:07 +00:00
|
|
|
# TODO: Tried adding bindPort = udpPort as parameter but that gave
|
|
|
|
# "Error: internal error: environment misses: udpPort" in nim-beacon-chain.
|
|
|
|
# Anyhow, nim-beacon-chain would also require some changes to support port
|
|
|
|
# remapping through NAT and this API is also subject to change once we
|
|
|
|
# introduce support for ipv4 + ipv6 binding/listening.
|
2022-11-10 09:52:24 +00:00
|
|
|
let customEnrFields = mapIt(localEnrFields, toFieldPair(it[0], it[1]))
|
2020-11-13 11:33:07 +00:00
|
|
|
# TODO:
|
|
|
|
# - Defect as is now or return a result for enr errors?
|
|
|
|
# - In case incorrect key, allow for new enr based on new key (new node id)?
|
|
|
|
var record: Record
|
|
|
|
if previousRecord.isSome():
|
|
|
|
record = previousRecord.get()
|
2024-06-24 12:57:39 +00:00
|
|
|
# TODO: this is faulty in case the intent is to remove a field with
|
|
|
|
# opt.none
|
2021-03-02 16:13:29 +00:00
|
|
|
record.update(privKey, enrIp, enrTcpPort, enrUdpPort,
|
2022-11-10 09:52:24 +00:00
|
|
|
customEnrFields).expect("Record within size limits and correct key")
|
2020-11-13 11:33:07 +00:00
|
|
|
else:
|
2021-03-02 16:13:29 +00:00
|
|
|
record = enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort,
|
2022-11-10 09:52:24 +00:00
|
|
|
customEnrFields).expect("Record within size limits")
|
2022-02-18 15:42:37 +00:00
|
|
|
|
2022-11-10 09:52:24 +00:00
|
|
|
info "Discovery ENR initialized", enrAutoUpdate, seqNum = record.seqNum,
|
|
|
|
ip = enrIp, tcpPort = enrTcpPort, udpPort = enrUdpPort,
|
|
|
|
customEnrFields, uri = toURI(record)
|
2021-03-02 16:13:29 +00:00
|
|
|
if enrIp.isNone():
|
2021-11-29 21:13:08 +00:00
|
|
|
if enrAutoUpdate:
|
|
|
|
notice "No external IP provided for the ENR, this node will not be " &
|
|
|
|
"discoverable until the ENR is updated with the discovered external IP address"
|
|
|
|
else:
|
|
|
|
warn "No external IP provided for the ENR, this node will not be discoverable"
|
2021-03-02 16:13:29 +00:00
|
|
|
|
2024-06-27 14:18:21 +00:00
|
|
|
let node = Node.fromRecord(record)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
|
|
|
# TODO Consider whether this should be a Defect
|
|
|
|
doAssert rng != nil, "RNG initialization failed"
|
|
|
|
|
2021-09-07 09:56:16 +00:00
|
|
|
Protocol(
|
2020-11-13 11:33:07 +00:00
|
|
|
privateKey: privKey,
|
|
|
|
localNode: node,
|
2024-04-24 11:38:52 +00:00
|
|
|
bindAddress: OptAddress(ip: Opt.some(bindIp), port: bindPort),
|
|
|
|
codec: Codec(localNode: node, privKey: privKey,
|
|
|
|
sessions: Sessions.init(256)),
|
|
|
|
bootstrapRecords: @bootstrapRecords,
|
|
|
|
ipVote: IpVote.init(),
|
|
|
|
enrAutoUpdate: enrAutoUpdate,
|
|
|
|
routingTable: RoutingTable.init(
|
|
|
|
node, config.bitsPerHop, config.tableIpLimits, rng),
|
|
|
|
handshakeTimeout: config.handshakeTimeout,
|
|
|
|
responseTimeout: config.responseTimeout,
|
|
|
|
rng: rng)
|
|
|
|
|
|
|
|
proc newProtocol*(
|
|
|
|
privKey: PrivateKey,
|
2024-06-18 16:09:27 +00:00
|
|
|
enrIp: Opt[IpAddress],
|
|
|
|
enrTcpPort, enrUdpPort: Opt[Port],
|
2024-04-24 11:38:52 +00:00
|
|
|
localEnrFields: openArray[(string, seq[byte])] = [],
|
|
|
|
bootstrapRecords: openArray[Record] = [],
|
2024-06-18 16:09:27 +00:00
|
|
|
previousRecord = Opt.none(enr.Record),
|
2024-04-24 11:38:52 +00:00
|
|
|
bindPort: Port,
|
|
|
|
bindIp: Opt[IpAddress],
|
|
|
|
enrAutoUpdate = false,
|
|
|
|
config = defaultDiscoveryConfig,
|
|
|
|
rng = newRng()): Protocol =
|
|
|
|
let
|
|
|
|
customEnrFields = mapIt(localEnrFields, toFieldPair(it[0], it[1]))
|
|
|
|
record =
|
|
|
|
if previousRecord.isSome():
|
|
|
|
var res = previousRecord.get()
|
2024-06-24 12:57:39 +00:00
|
|
|
# TODO: this is faulty in case the intent is to remove a field with
|
|
|
|
# opt.none
|
2024-04-24 11:38:52 +00:00
|
|
|
res.update(privKey, enrIp, enrTcpPort, enrUdpPort,
|
|
|
|
customEnrFields).expect("Record within size limits and correct key")
|
|
|
|
res
|
|
|
|
else:
|
|
|
|
enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort,
|
|
|
|
customEnrFields).expect("Record within size limits")
|
|
|
|
|
|
|
|
info "Discovery ENR initialized", enrAutoUpdate, seqNum = record.seqNum,
|
|
|
|
ip = enrIp, tcpPort = enrTcpPort, udpPort = enrUdpPort,
|
|
|
|
customEnrFields, uri = toURI(record)
|
|
|
|
|
|
|
|
if enrIp.isNone():
|
|
|
|
if enrAutoUpdate:
|
|
|
|
notice "No external IP provided for the ENR, this node will not be " &
|
|
|
|
"discoverable until the ENR is updated with the discovered " &
|
|
|
|
"external IP address"
|
|
|
|
else:
|
|
|
|
warn "No external IP provided for the ENR, this node will not be " &
|
|
|
|
"discoverable"
|
|
|
|
|
2024-06-27 14:18:21 +00:00
|
|
|
let node = Node.fromRecord(record)
|
2024-04-24 11:38:52 +00:00
|
|
|
|
|
|
|
doAssert not(isNil(rng)), "RNG initialization failed"
|
|
|
|
|
|
|
|
Protocol(
|
|
|
|
privateKey: privKey,
|
|
|
|
localNode: node,
|
|
|
|
bindAddress: OptAddress(ip: bindIp, port: bindPort),
|
2020-11-13 11:33:07 +00:00
|
|
|
codec: Codec(localNode: node, privKey: privKey,
|
|
|
|
sessions: Sessions.init(256)),
|
|
|
|
bootstrapRecords: @bootstrapRecords,
|
2021-01-21 16:21:36 +00:00
|
|
|
ipVote: IpVote.init(),
|
2021-01-26 13:11:22 +00:00
|
|
|
enrAutoUpdate: enrAutoUpdate,
|
2022-02-02 14:29:45 +00:00
|
|
|
routingTable: RoutingTable.init(
|
|
|
|
node, config.bitsPerHop, config.tableIpLimits, rng),
|
2022-08-05 13:19:16 +00:00
|
|
|
handshakeTimeout: config.handshakeTimeout,
|
|
|
|
responseTimeout: config.responseTimeout,
|
2020-11-13 11:33:07 +00:00
|
|
|
rng: rng)
|
|
|
|
|
2024-04-24 11:38:52 +00:00
|
|
|
proc `$`*(a: OptAddress): string =
|
|
|
|
if a.ip.isNone():
|
|
|
|
"*:" & $a.port
|
|
|
|
else:
|
|
|
|
$a.ip.get() & ":" & $a.port
|
|
|
|
|
|
|
|
chronicles.formatIt(OptAddress): $it
|
|
|
|
|
2021-09-28 22:43:00 +00:00
|
|
|
template listeningAddress*(p: Protocol): Address =
|
2024-04-24 11:38:52 +00:00
|
|
|
if p.bindAddress.ip.isNone():
|
|
|
|
let ta = getAutoAddress(p.bindAddress.port)
|
|
|
|
Address(ta.toIpAddress(), ta.port)
|
|
|
|
else:
|
|
|
|
Address(p.bindAddress.ip.get(), p.bindAddress.port)
|
2021-09-28 22:43:00 +00:00
|
|
|
|
2024-04-24 11:38:52 +00:00
|
|
|
proc open*(d: Protocol) {.raises: [TransportOsError].} =
|
2020-11-13 11:33:07 +00:00
|
|
|
info "Starting discovery node", node = d.localNode,
|
2021-03-02 16:13:29 +00:00
|
|
|
bindAddress = d.bindAddress
|
2020-11-26 17:19:13 +00:00
|
|
|
|
2020-11-13 11:33:07 +00:00
|
|
|
# TODO allow binding to specific IP / IPv6 / etc
|
2024-04-24 11:38:52 +00:00
|
|
|
d.transp =
|
|
|
|
newDatagramTransport(processClient, udata = d,
|
|
|
|
localPort = d.bindAddress.port,
|
|
|
|
local = d.bindAddress.ip)
|
2021-01-07 09:15:48 +00:00
|
|
|
d.seedTable()
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2021-03-25 17:12:11 +00:00
|
|
|
proc start*(d: Protocol) =
|
2021-01-05 14:03:38 +00:00
|
|
|
d.refreshLoop = refreshLoop(d)
|
2020-11-13 11:33:07 +00:00
|
|
|
d.revalidateLoop = revalidateLoop(d)
|
2021-01-26 13:11:22 +00:00
|
|
|
d.ipMajorityLoop = ipMajorityLoop(d)
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2024-01-22 16:08:08 +00:00
|
|
|
proc closeWait*(d: Protocol) {.async: (raises: []).} =
|
2020-11-13 11:33:07 +00:00
|
|
|
doAssert(not d.transp.closed)
|
|
|
|
|
|
|
|
debug "Closing discovery node", node = d.localNode
|
2024-01-22 16:08:08 +00:00
|
|
|
var futures: seq[Future[void]]
|
2020-11-13 11:33:07 +00:00
|
|
|
if not d.revalidateLoop.isNil:
|
2024-01-22 16:08:08 +00:00
|
|
|
futures.add(d.revalidateLoop.cancelAndWait())
|
2021-01-05 14:03:38 +00:00
|
|
|
if not d.refreshLoop.isNil:
|
2024-01-22 16:08:08 +00:00
|
|
|
futures.add(d.refreshLoop.cancelAndWait())
|
2021-01-26 13:11:22 +00:00
|
|
|
if not d.ipMajorityLoop.isNil:
|
2024-01-22 16:08:08 +00:00
|
|
|
futures.add(d.ipMajorityLoop.cancelAndWait())
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2024-01-22 16:08:08 +00:00
|
|
|
await noCancel(allFutures(futures))
|
|
|
|
await noCancel(d.transp.closeWait())
|
2020-11-13 11:33:07 +00:00
|
|
|
|
2024-01-22 16:08:08 +00:00
|
|
|
proc close*(d: Protocol) {.deprecated: "Please use closeWait() instead".} =
|
|
|
|
asyncSpawn d.closeWait()
|