2021-04-28 14:20:05 +00:00
|
|
|
# nim-eth - Node Discovery Protocol v5
|
2023-05-10 13:50:04 +00:00
|
|
|
# Copyright (c) 2020-2023 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.
|
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2021-04-28 14:20:05 +00:00
|
|
|
|
2020-02-17 16:44:56 +00:00
|
|
|
import
|
2020-07-13 12:34:53 +00:00
|
|
|
std/[algorithm, times, sequtils, bitops, sets, options],
|
2022-06-17 20:45:37 +00:00
|
|
|
bearssl/rand,
|
|
|
|
stint, chronicles, metrics, chronos, stew/shims/net as stewNet,
|
2023-06-09 14:26:17 +00:00
|
|
|
../../net/utils, stew/endians2,
|
2021-04-06 11:33:24 +00:00
|
|
|
"."/[node, random2, enr]
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-06-23 14:11:58 +00:00
|
|
|
export options
|
|
|
|
|
2020-06-30 11:35:15 +00:00
|
|
|
declarePublicGauge routing_table_nodes,
|
|
|
|
"Discovery routing table nodes", labels = ["state"]
|
|
|
|
|
2019-12-16 19:38:45 +00:00
|
|
|
type
|
2023-05-10 13:50:04 +00:00
|
|
|
DistanceProc* =
|
|
|
|
proc(a, b: NodeId): NodeId {.raises: [], gcsafe, noSideEffect.}
|
|
|
|
LogDistanceProc* =
|
|
|
|
proc(a, b: NodeId): uint16 {.raises: [], gcsafe, noSideEffect.}
|
|
|
|
IdAtDistanceProc* =
|
|
|
|
proc (id: NodeId, dist: uint16): NodeId {.raises: [], gcsafe, noSideEffect.}
|
2021-09-02 12:00:36 +00:00
|
|
|
|
|
|
|
DistanceCalculator* = object
|
|
|
|
calculateDistance*: DistanceProc
|
|
|
|
calculateLogDistance*: LogDistanceProc
|
|
|
|
calculateIdAtDistance*: IdAtDistanceProc
|
|
|
|
|
2019-12-16 19:38:45 +00:00
|
|
|
RoutingTable* = object
|
2021-11-22 17:53:52 +00:00
|
|
|
localNode*: Node
|
|
|
|
buckets*: seq[KBucket]
|
2020-06-17 11:51:30 +00:00
|
|
|
bitsPerHop: int ## This value indicates how many bits (at minimum) you get
|
|
|
|
## closer to finding your target per query. Practically, it tells you also
|
|
|
|
## how often your "not in range" branch will split off. Setting this to 1
|
|
|
|
## is the basic, non accelerated version, which will never split off the
|
|
|
|
## not in range branch and which will result in log base2 n hops per lookup.
|
|
|
|
## Setting it higher will increase the amount of splitting on a not in range
|
|
|
|
## branch (thus holding more nodes with a better keyspace coverage) and this
|
|
|
|
## will result in an improvement of log base(2^b) n hops per lookup.
|
2020-11-26 17:20:15 +00:00
|
|
|
ipLimits: IpLimits ## IP limits for total routing table: all buckets and
|
|
|
|
## replacement caches.
|
2021-09-02 12:00:36 +00:00
|
|
|
distanceCalculator: DistanceCalculator
|
2022-06-17 20:45:37 +00:00
|
|
|
rng: ref HmacDrbgContext
|
2019-12-16 19:38:45 +00:00
|
|
|
|
|
|
|
KBucket = ref object
|
2020-06-05 20:56:23 +00:00
|
|
|
istart, iend: NodeId ## Range of NodeIds this KBucket covers. This is not a
|
|
|
|
## simple logarithmic distance as buckets can be split over a prefix that
|
2021-11-22 17:53:52 +00:00
|
|
|
## does not cover the `localNode` id.
|
|
|
|
nodes*: seq[Node] ## Node entries of the KBucket. Sorted according to last
|
2020-06-05 20:56:23 +00:00
|
|
|
## time seen. First entry (head) is considered the most recently seen node
|
|
|
|
## and the last entry (tail) is considered the least recently seen node.
|
|
|
|
## Here "seen" means a successful request-response. This can also not have
|
2022-11-16 16:44:00 +00:00
|
|
|
## occurred yet.
|
2020-06-05 20:56:23 +00:00
|
|
|
replacementCache: seq[Node] ## Nodes that could not be added to the `nodes`
|
2020-06-23 14:11:58 +00:00
|
|
|
## seq as it is full and without stale nodes. This is practically a small
|
|
|
|
## LRU cache.
|
2020-11-26 17:20:15 +00:00
|
|
|
ipLimits: IpLimits ## IP limits for bucket: node entries and replacement
|
|
|
|
## cache entries combined.
|
|
|
|
|
|
|
|
## The routing table IP limits are applied on both the total table, and on the
|
|
|
|
## individual buckets. In each case, the active node entries, but also the
|
|
|
|
## entries waiting in the replacement cache are accounted for. This way, the
|
|
|
|
## replacement cache can't get filled with nodes that then can't be added due
|
|
|
|
## to the limits that apply.
|
|
|
|
##
|
|
|
|
## As entries are not verified (=contacted) immediately before or on entry, it
|
|
|
|
## is possible that a malicious node could fill (poison) the routing table or
|
|
|
|
## a specific bucket with ENRs with IPs it does not control. The effect of
|
|
|
|
## this would be that a node that actually owns the IP could have a difficult
|
2022-11-16 16:44:00 +00:00
|
|
|
## time getting its ENR distributed in the DHT and as a consequence would
|
2020-11-26 17:20:15 +00:00
|
|
|
## not be reached from the outside as much (or at all). However, that node can
|
|
|
|
## still search and find nodes to connect to. So it would practically be a
|
|
|
|
## similar situation as a node that is not reachable behind the NAT because
|
|
|
|
## port mapping is not set up properly.
|
2022-11-16 16:44:00 +00:00
|
|
|
## There is the possibility to set the IP limit on verified (=contacted) nodes
|
2020-11-26 17:20:15 +00:00
|
|
|
## only, but that would allow for lookups to be done on a higher set of nodes
|
|
|
|
## owned by the same identity. This is a worse alternative.
|
|
|
|
## Next, doing lookups only on verified nodes would slow down discovery start
|
|
|
|
## up.
|
|
|
|
TableIpLimits* = object
|
|
|
|
tableIpLimit*: uint
|
|
|
|
bucketIpLimit*: uint
|
|
|
|
|
|
|
|
NodeStatus* = enum
|
|
|
|
Added
|
|
|
|
LocalNode
|
|
|
|
Existing
|
|
|
|
IpLimitReached
|
|
|
|
ReplacementAdded
|
|
|
|
ReplacementExisting
|
|
|
|
NoAddress
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2021-09-02 12:00:36 +00:00
|
|
|
# xor distance functions
|
2021-12-20 12:14:50 +00:00
|
|
|
func distance*(a, b: NodeId): UInt256 =
|
2020-04-14 09:08:08 +00:00
|
|
|
## Calculate the distance to a NodeId.
|
2021-09-02 12:00:36 +00:00
|
|
|
a xor b
|
|
|
|
|
2021-10-07 14:03:12 +00:00
|
|
|
func logDistance*(a, b: NodeId): uint16 =
|
2020-04-14 09:08:08 +00:00
|
|
|
## Calculate the logarithmic distance between two `NodeId`s.
|
|
|
|
##
|
|
|
|
## According the specification, this is the log base 2 of the distance. But it
|
|
|
|
## is rather the log base 2 of the distance + 1, as else the 0 value can not
|
|
|
|
## be used (e.g. by FindNode call to return peer its own ENR)
|
|
|
|
## For NodeId of 256 bits, range is 0-256.
|
2021-11-17 21:55:19 +00:00
|
|
|
let a = a.toBytesBE
|
|
|
|
let b = b.toBytesBE
|
2019-12-16 19:38:45 +00:00
|
|
|
var lz = 0
|
2021-11-17 21:55:19 +00:00
|
|
|
for i in 0..<a.len:
|
2019-12-16 19:38:45 +00:00
|
|
|
let x = a[i] xor b[i]
|
|
|
|
if x == 0:
|
2020-03-16 22:56:00 +00:00
|
|
|
lz += 8
|
2019-12-16 19:38:45 +00:00
|
|
|
else:
|
2020-03-16 22:56:00 +00:00
|
|
|
lz += bitops.countLeadingZeroBits(x)
|
|
|
|
break
|
2021-07-13 08:05:46 +00:00
|
|
|
return uint16(a.len * 8 - lz)
|
2021-10-07 14:03:12 +00:00
|
|
|
|
|
|
|
func idAtDistance*(id: NodeId, dist: uint16): NodeId =
|
|
|
|
## Calculate the "lowest" `NodeId` for given logarithmic distance.
|
|
|
|
## A logarithmic distance obviously covers a whole range of distances and thus
|
|
|
|
## potential `NodeId`s.
|
|
|
|
# xor the NodeId with 2^(d - 1) or one could say, calculate back the leading
|
|
|
|
# zeroes and xor those` with the id.
|
|
|
|
id xor (1.stuint(256) shl (dist.int - 1))
|
2021-09-02 12:00:36 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
BUCKET_SIZE* = 16 ## Maximum amount of nodes per bucket
|
|
|
|
REPLACEMENT_CACHE_SIZE* = 8 ## Maximum amount of nodes per replacement cache
|
|
|
|
## of a bucket
|
|
|
|
ID_SIZE = 256
|
|
|
|
DefaultBitsPerHop* = 5
|
|
|
|
DefaultBucketIpLimit* = 2'u
|
|
|
|
DefaultTableIpLimit* = 10'u
|
|
|
|
DefaultTableIpLimits* = TableIpLimits(tableIpLimit: DefaultTableIpLimit,
|
|
|
|
bucketIpLimit: DefaultBucketIpLimit)
|
2021-10-07 14:03:12 +00:00
|
|
|
XorDistanceCalculator* = DistanceCalculator(calculateDistance: distance,
|
|
|
|
calculateLogDistance: logDistance, calculateIdAtDistance: idAtDistance)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
func distance*(r: RoutingTable, a, b: NodeId): UInt256 =
|
2021-10-07 14:03:12 +00:00
|
|
|
r.distanceCalculator.calculateDistance(a, b)
|
|
|
|
|
|
|
|
func logDistance*(r: RoutingTable, a, b: NodeId): uint16 =
|
|
|
|
r.distanceCalculator.calculateLogDistance(a, b)
|
|
|
|
|
|
|
|
func idAtDistance*(r: RoutingTable, id: NodeId, dist: uint16): NodeId =
|
|
|
|
r.distanceCalculator.calculateIdAtDistance(id, dist)
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2021-09-07 09:56:16 +00:00
|
|
|
proc new(T: type KBucket, istart, iend: NodeId, bucketIpLimit: uint): T =
|
|
|
|
KBucket(
|
|
|
|
istart: istart,
|
|
|
|
iend: iend,
|
|
|
|
nodes: @[],
|
|
|
|
replacementCache: @[],
|
|
|
|
ipLimits: IpLimits(limit: bucketIpLimit))
|
2019-12-16 19:38:45 +00:00
|
|
|
|
|
|
|
proc midpoint(k: KBucket): NodeId =
|
|
|
|
k.istart + (k.iend - k.istart) div 2.u256
|
|
|
|
|
2021-05-11 07:59:58 +00:00
|
|
|
proc len(k: KBucket): int = k.nodes.len
|
2021-09-02 12:00:36 +00:00
|
|
|
|
2021-05-11 07:59:58 +00:00
|
|
|
proc tail(k: KBucket): Node = k.nodes[high(k.nodes)]
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-11-26 17:20:15 +00:00
|
|
|
proc ipLimitInc(r: var RoutingTable, b: KBucket, n: Node): bool =
|
|
|
|
## Check if the ip limits of the routing table and the bucket are reached for
|
|
|
|
## the specified `Node` its ip.
|
|
|
|
## When one of the ip limits is reached return false, else increment them and
|
|
|
|
## return true.
|
|
|
|
let ip = n.address.get().ip # Node from table should always have an address
|
|
|
|
# Check ip limit for bucket
|
|
|
|
if not b.ipLimits.inc(ip):
|
|
|
|
return false
|
|
|
|
# Check ip limit for routing table
|
|
|
|
if not r.ipLimits.inc(ip):
|
2021-12-20 12:14:50 +00:00
|
|
|
b.ipLimits.dec(ip)
|
2020-11-26 17:20:15 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc ipLimitDec(r: var RoutingTable, b: KBucket, n: Node) =
|
|
|
|
## Decrement the ip limits of the routing table and the bucket for the
|
|
|
|
## specified `Node` its ip.
|
|
|
|
let ip = n.address.get().ip # Node from table should always have an address
|
|
|
|
|
|
|
|
b.ipLimits.dec(ip)
|
|
|
|
r.ipLimits.dec(ip)
|
|
|
|
|
|
|
|
proc add(k: KBucket, n: Node) =
|
|
|
|
k.nodes.add(n)
|
|
|
|
routing_table_nodes.inc()
|
|
|
|
|
|
|
|
proc remove(k: KBucket, n: Node): bool =
|
2019-12-16 19:38:45 +00:00
|
|
|
let i = k.nodes.find(n)
|
2020-06-30 11:35:15 +00:00
|
|
|
if i != -1:
|
|
|
|
routing_table_nodes.dec()
|
2020-11-26 17:20:15 +00:00
|
|
|
if k.nodes[i].seen:
|
|
|
|
routing_table_nodes.dec(labelValues = ["seen"])
|
|
|
|
k.nodes.delete(i)
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
2019-12-16 19:38:45 +00:00
|
|
|
|
|
|
|
proc split(k: KBucket): tuple[lower, upper: KBucket] =
|
2020-07-12 15:25:18 +00:00
|
|
|
## Split the kbucket `k` at the median id.
|
2019-12-16 19:38:45 +00:00
|
|
|
let splitid = k.midpoint
|
2021-09-07 09:56:16 +00:00
|
|
|
result.lower = KBucket.new(k.istart, splitid, k.ipLimits.limit)
|
|
|
|
result.upper = KBucket.new(splitid + 1.u256, k.iend, k.ipLimits.limit)
|
2019-12-16 19:38:45 +00:00
|
|
|
for node in k.nodes:
|
|
|
|
let bucket = if node.id <= splitid: result.lower else: result.upper
|
2020-06-30 11:35:15 +00:00
|
|
|
bucket.nodes.add(node)
|
2021-09-07 09:56:16 +00:00
|
|
|
# Ip limits got reset because of the KBucket.new, so there is the need to
|
2020-11-26 17:20:15 +00:00
|
|
|
# increment again for each added node. It should however never fail as the
|
|
|
|
# previous bucket had the same limits.
|
|
|
|
doAssert(bucket.ipLimits.inc(node.address.get().ip),
|
|
|
|
"IpLimit increment should work as all buckets have the same limits")
|
|
|
|
|
2019-12-16 19:38:45 +00:00
|
|
|
for node in k.replacementCache:
|
|
|
|
let bucket = if node.id <= splitid: result.lower else: result.upper
|
|
|
|
bucket.replacementCache.add(node)
|
2020-11-26 17:20:15 +00:00
|
|
|
doAssert(bucket.ipLimits.inc(node.address.get().ip),
|
|
|
|
"IpLimit increment should work as all buckets have the same limits")
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2021-05-11 07:59:58 +00:00
|
|
|
proc inRange(k: KBucket, n: Node): bool =
|
2019-12-16 19:38:45 +00:00
|
|
|
k.istart <= n.id and n.id <= k.iend
|
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc contains(k: KBucket, n: Node): bool = n in k.nodes
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc binaryGetBucketForNode*(buckets: openArray[KBucket],
|
2020-06-24 10:29:59 +00:00
|
|
|
id: NodeId): KBucket =
|
|
|
|
## Given a list of ordered buckets, returns the bucket for a given `NodeId`.
|
|
|
|
## Returns nil if no bucket in range for given `id` is found.
|
2019-12-16 19:38:45 +00:00
|
|
|
let bucketPos = lowerBound(buckets, id) do(a: KBucket, b: NodeId) -> int:
|
|
|
|
cmp(a.iend, b)
|
2020-06-24 10:29:59 +00:00
|
|
|
|
|
|
|
# Prevent cases where `lowerBound` returns an out of range index e.g. at empty
|
2021-12-20 12:14:50 +00:00
|
|
|
# openArray, or when the id is out range for all buckets in the openArray.
|
2019-12-16 19:38:45 +00:00
|
|
|
if bucketPos < buckets.len:
|
|
|
|
let bucket = buckets[bucketPos]
|
|
|
|
if bucket.istart <= id and id <= bucket.iend:
|
|
|
|
result = bucket
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc computeSharedPrefixBits(nodes: openArray[NodeId]): int =
|
2019-12-16 19:38:45 +00:00
|
|
|
## Count the number of prefix bits shared by all nodes.
|
|
|
|
if nodes.len < 2:
|
|
|
|
return ID_SIZE
|
|
|
|
|
|
|
|
var mask = zero(UInt256)
|
|
|
|
let one = one(UInt256)
|
|
|
|
|
|
|
|
for i in 1 .. ID_SIZE:
|
|
|
|
mask = mask or (one shl (ID_SIZE - i))
|
2020-06-22 14:46:58 +00:00
|
|
|
let reference = nodes[0] and mask
|
2019-12-16 19:38:45 +00:00
|
|
|
for j in 1 .. nodes.high:
|
2020-06-22 14:46:58 +00:00
|
|
|
if (nodes[j] and mask) != reference: return i - 1
|
2019-12-16 19:38:45 +00:00
|
|
|
|
|
|
|
for n in nodes:
|
2020-06-22 14:46:58 +00:00
|
|
|
echo n.toHex()
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-07-12 15:25:18 +00:00
|
|
|
# Reaching this would mean that all node ids are equal.
|
2019-12-16 19:38:45 +00:00
|
|
|
doAssert(false, "Unable to calculate number of shared prefix bits")
|
|
|
|
|
2021-11-22 17:53:52 +00:00
|
|
|
proc init*(T: type RoutingTable, localNode: Node, bitsPerHop = DefaultBitsPerHop,
|
2022-06-17 20:45:37 +00:00
|
|
|
ipLimits = DefaultTableIpLimits, rng: ref HmacDrbgContext,
|
2021-09-07 09:56:16 +00:00
|
|
|
distanceCalculator = XorDistanceCalculator): T =
|
2020-07-12 15:25:18 +00:00
|
|
|
## Initialize the routing table for provided `Node` and bitsPerHop value.
|
|
|
|
## `bitsPerHop` is default set to 5 as recommended by original Kademlia paper.
|
2021-09-07 09:56:16 +00:00
|
|
|
RoutingTable(
|
2021-11-22 17:53:52 +00:00
|
|
|
localNode: localNode,
|
2021-12-20 12:14:50 +00:00
|
|
|
buckets: @[KBucket.new(0.u256, high(UInt256), ipLimits.bucketIpLimit)],
|
2021-09-07 09:56:16 +00:00
|
|
|
bitsPerHop: bitsPerHop,
|
|
|
|
ipLimits: IpLimits(limit: ipLimits.tableIpLimit),
|
|
|
|
distanceCalculator: distanceCalculator,
|
|
|
|
rng: rng)
|
2019-12-16 19:38:45 +00:00
|
|
|
|
|
|
|
proc splitBucket(r: var RoutingTable, index: int) =
|
|
|
|
let bucket = r.buckets[index]
|
|
|
|
let (a, b) = bucket.split()
|
|
|
|
r.buckets[index] = a
|
|
|
|
r.buckets.insert(b, index + 1)
|
|
|
|
|
|
|
|
proc bucketForNode(r: RoutingTable, id: NodeId): KBucket =
|
2020-06-24 10:29:59 +00:00
|
|
|
result = binaryGetBucketForNode(r.buckets, id)
|
|
|
|
doAssert(not result.isNil(),
|
|
|
|
"Routing table should always cover the full id space")
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-11-26 17:20:15 +00:00
|
|
|
proc addReplacement(r: var RoutingTable, k: KBucket, n: Node): NodeStatus =
|
|
|
|
## Add the node to the tail of the replacement cache of the KBucket.
|
|
|
|
##
|
|
|
|
## If the replacement cache is full, the oldest (first entry) node will be
|
|
|
|
## removed. If the node is already in the replacement cache, it will be moved
|
|
|
|
## to the tail.
|
|
|
|
## When the IP of the node has reached the IP limits for the bucket or the
|
|
|
|
## total routing table, the node will not be added to the replacement cache.
|
|
|
|
let nodeIdx = k.replacementCache.find(n)
|
|
|
|
if nodeIdx != -1:
|
|
|
|
if k.replacementCache[nodeIdx].record.seqNum <= n.record.seqNum:
|
|
|
|
# In case the record sequence number is higher or the same, the new node
|
|
|
|
# gets moved to the tail.
|
|
|
|
if k.replacementCache[nodeIdx].address.get().ip != n.address.get().ip:
|
|
|
|
if not ipLimitInc(r, k, n):
|
|
|
|
return IpLimitReached
|
|
|
|
ipLimitDec(r, k, k.replacementCache[nodeIdx])
|
|
|
|
k.replacementCache.delete(nodeIdx)
|
|
|
|
k.replacementCache.add(n)
|
|
|
|
return ReplacementExisting
|
|
|
|
elif not ipLimitInc(r, k, n):
|
|
|
|
return IpLimitReached
|
|
|
|
else:
|
|
|
|
doAssert(k.replacementCache.len <= REPLACEMENT_CACHE_SIZE)
|
|
|
|
|
|
|
|
if k.replacementCache.len == REPLACEMENT_CACHE_SIZE:
|
|
|
|
# Remove ip from limits for the to be deleted node.
|
|
|
|
ipLimitDec(r, k, k.replacementCache[0])
|
|
|
|
k.replacementCache.delete(0)
|
|
|
|
|
|
|
|
k.replacementCache.add(n)
|
|
|
|
return ReplacementAdded
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-11-26 17:20:15 +00:00
|
|
|
proc addNode*(r: var RoutingTable, n: Node): NodeStatus =
|
2020-06-23 14:11:58 +00:00
|
|
|
## Try to add the node to the routing table.
|
|
|
|
##
|
|
|
|
## First, an attempt will be done to add the node to the bucket in its range.
|
2022-11-16 16:44:00 +00:00
|
|
|
## If this fails, the bucket will be split if it is eligible for splitting.
|
2020-06-23 14:11:58 +00:00
|
|
|
## If so, a new attempt will be done to add the node. If not, the node will be
|
|
|
|
## added to the replacement cache.
|
2020-11-26 17:20:15 +00:00
|
|
|
##
|
|
|
|
## In case the node was already in the table, it will be updated if it has a
|
|
|
|
## newer record.
|
|
|
|
## When the IP of the node has reached the IP limits for the bucket or the
|
|
|
|
## total routing table, the node will not be added to the bucket, nor its
|
|
|
|
## replacement cache.
|
|
|
|
|
|
|
|
# Don't allow nodes without an address field in the ENR to be added.
|
|
|
|
# This could also be reworked by having another Node type that always has an
|
|
|
|
# address.
|
|
|
|
if n.address.isNone():
|
|
|
|
return NoAddress
|
|
|
|
|
2021-11-22 17:53:52 +00:00
|
|
|
if n == r.localNode:
|
2020-11-26 17:20:15 +00:00
|
|
|
return LocalNode
|
|
|
|
|
2019-12-16 19:38:45 +00:00
|
|
|
let bucket = r.bucketForNode(n.id)
|
2020-11-26 17:20:15 +00:00
|
|
|
|
|
|
|
## Check if the node is already present. If so, check if the record requires
|
|
|
|
## updating.
|
|
|
|
let nodeIdx = bucket.nodes.find(n)
|
|
|
|
if nodeIdx != -1:
|
|
|
|
if bucket.nodes[nodeIdx].record.seqNum < n.record.seqNum:
|
|
|
|
# In case of a newer record, it gets replaced.
|
|
|
|
if bucket.nodes[nodeIdx].address.get().ip != n.address.get().ip:
|
|
|
|
if not ipLimitInc(r, bucket, n):
|
|
|
|
return IpLimitReached
|
|
|
|
ipLimitDec(r, bucket, bucket.nodes[nodeIdx])
|
|
|
|
# Copy over the seen status, we trust here that after the ENR update the
|
|
|
|
# node will still be reachable, but it might not be the case.
|
|
|
|
n.seen = bucket.nodes[nodeIdx].seen
|
|
|
|
bucket.nodes[nodeIdx] = n
|
|
|
|
|
|
|
|
return Existing
|
|
|
|
|
|
|
|
# If the bucket has fewer than `BUCKET_SIZE` entries, it is inserted as the
|
|
|
|
# last entry of the bucket (least recently seen node). If the bucket is
|
|
|
|
# full, it might get split and adding is retried, else it is added as a
|
|
|
|
# replacement.
|
|
|
|
# Reasoning here is that adding nodes will happen for a big part from
|
|
|
|
# lookups, which do not necessarily return nodes that are (still) reachable.
|
|
|
|
# So, more trust is put in the own ordering by actually contacting peers and
|
|
|
|
# newly additions are added as least recently seen (in fact they have not been
|
|
|
|
# seen yet from our node its perspective).
|
|
|
|
# However, in discovery v5 a node can also be added after a incoming request
|
|
|
|
# if a handshake is done and an ENR is provided, and considering that this
|
|
|
|
# handshake needs to be done, it is more likely that this node is reachable.
|
|
|
|
# However, it is not certain and depending on different NAT mechanisms and
|
|
|
|
# timers it might still fail. For this reason we currently do not add a way to
|
|
|
|
# immediately add nodes to the most recently seen spot.
|
|
|
|
if bucket.len < BUCKET_SIZE:
|
|
|
|
if not ipLimitInc(r, bucket, n):
|
|
|
|
return IpLimitReached
|
|
|
|
|
|
|
|
bucket.add(n)
|
|
|
|
else:
|
|
|
|
# Bucket must be full, but lets see if it should be split the bucket.
|
|
|
|
|
2020-06-22 14:46:58 +00:00
|
|
|
# Calculate the prefix shared by all nodes in the bucket's range, not the
|
|
|
|
# ones actually in the bucket.
|
|
|
|
let depth = computeSharedPrefixBits(@[bucket.istart, bucket.iend])
|
2020-11-26 17:20:15 +00:00
|
|
|
# Split if the bucket has the local node in its range or if the depth is not
|
|
|
|
# congruent to 0 mod `bitsPerHop`
|
2021-11-22 17:53:52 +00:00
|
|
|
if bucket.inRange(r.localNode) or
|
2020-06-17 11:51:30 +00:00
|
|
|
(depth mod r.bitsPerHop != 0 and depth != ID_SIZE):
|
2019-12-16 19:38:45 +00:00
|
|
|
r.splitBucket(r.buckets.find(bucket))
|
2020-06-23 14:11:58 +00:00
|
|
|
return r.addNode(n) # retry adding
|
|
|
|
else:
|
|
|
|
# When bucket doesn't get split the node is added to the replacement cache
|
2020-11-26 17:20:15 +00:00
|
|
|
return r.addReplacement(bucket, n)
|
2020-06-23 14:11:58 +00:00
|
|
|
|
2020-11-26 17:20:15 +00:00
|
|
|
proc removeNode*(r: var RoutingTable, n: Node) =
|
|
|
|
## Remove the node `n` from the routing table.
|
|
|
|
let b = r.bucketForNode(n.id)
|
|
|
|
if b.remove(n):
|
|
|
|
ipLimitDec(r, b, n)
|
2020-06-23 14:11:58 +00:00
|
|
|
|
|
|
|
proc replaceNode*(r: var RoutingTable, n: Node) =
|
|
|
|
## Replace node `n` with last entry in the replacement cache. If there are
|
|
|
|
## no entries in the replacement cache, node `n` will simply be removed.
|
|
|
|
# TODO: Kademlia paper recommends here to not remove nodes if there are no
|
|
|
|
# replacements. However, that would require a bit more complexity in the
|
|
|
|
# revalidation as you don't want to try pinging that node all the time.
|
|
|
|
let b = r.bucketForNode(n.id)
|
2020-11-26 17:20:15 +00:00
|
|
|
if b.remove(n):
|
|
|
|
ipLimitDec(r, b, n)
|
2020-06-30 11:35:15 +00:00
|
|
|
|
2020-06-23 14:11:58 +00:00
|
|
|
if b.replacementCache.len > 0:
|
2020-11-26 17:20:15 +00:00
|
|
|
# Nodes in the replacement cache are already included in the ip limits.
|
|
|
|
b.add(b.replacementCache[high(b.replacementCache)])
|
2020-06-23 14:11:58 +00:00
|
|
|
b.replacementCache.delete(high(b.replacementCache))
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-04-20 11:50:22 +00:00
|
|
|
proc getNode*(r: RoutingTable, id: NodeId): Option[Node] =
|
2020-07-12 15:25:18 +00:00
|
|
|
## Get the `Node` with `id` as `NodeId` from the routing table.
|
|
|
|
## If no node with provided node id can be found,`none` is returned .
|
2019-12-23 17:21:11 +00:00
|
|
|
let b = r.bucketForNode(id)
|
2019-12-16 19:38:45 +00:00
|
|
|
for n in b.nodes:
|
|
|
|
if n.id == id:
|
2020-04-20 11:50:22 +00:00
|
|
|
return some(n)
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2019-12-23 17:21:11 +00:00
|
|
|
proc contains*(r: RoutingTable, n: Node): bool = n in r.bucketForNode(n.id)
|
2020-07-12 15:25:18 +00:00
|
|
|
# Check if the routing table contains node `n`.
|
2019-12-16 19:38:45 +00:00
|
|
|
|
|
|
|
proc bucketsByDistanceTo(r: RoutingTable, id: NodeId): seq[KBucket] =
|
2021-10-07 14:03:12 +00:00
|
|
|
sortedByIt(r.buckets, r.distance(it.midpoint, id))
|
2021-09-02 12:00:36 +00:00
|
|
|
|
|
|
|
proc nodesByDistanceTo(r: RoutingTable, k: KBucket, id: NodeId): seq[Node] =
|
2021-10-07 14:03:12 +00:00
|
|
|
sortedByIt(k.nodes, r.distance(it.id, id))
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-06-30 11:35:15 +00:00
|
|
|
proc neighbours*(r: RoutingTable, id: NodeId, k: int = BUCKET_SIZE,
|
|
|
|
seenOnly = false): seq[Node] =
|
2020-07-12 15:25:18 +00:00
|
|
|
## Return up to k neighbours of the given node id.
|
|
|
|
## When seenOnly is set to true, only nodes that have been contacted
|
|
|
|
## previously successfully will be selected.
|
2019-12-16 19:38:45 +00:00
|
|
|
result = newSeqOfCap[Node](k * 2)
|
2020-04-15 21:34:36 +00:00
|
|
|
block addNodes:
|
|
|
|
for bucket in r.bucketsByDistanceTo(id):
|
2021-09-02 12:00:36 +00:00
|
|
|
for n in r.nodesByDistanceTo(bucket, id):
|
2020-07-12 15:25:18 +00:00
|
|
|
# Only provide actively seen nodes when `seenOnly` set.
|
2020-06-30 11:35:15 +00:00
|
|
|
if not seenOnly or n.seen:
|
|
|
|
result.add(n)
|
|
|
|
if result.len == k * 2:
|
|
|
|
break addNodes
|
2020-04-15 21:34:36 +00:00
|
|
|
|
|
|
|
# TODO: is this sort still needed? Can we get nodes closer from the "next"
|
|
|
|
# bucket?
|
2021-10-07 14:03:12 +00:00
|
|
|
result = sortedByIt(result, r.distance(it.id, id))
|
2019-12-16 19:38:45 +00:00
|
|
|
if result.len > k:
|
|
|
|
result.setLen(k)
|
|
|
|
|
2021-07-13 08:05:46 +00:00
|
|
|
proc neighboursAtDistance*(r: RoutingTable, distance: uint16,
|
2020-06-30 11:35:15 +00:00
|
|
|
k: int = BUCKET_SIZE, seenOnly = false): seq[Node] =
|
2020-07-12 15:25:18 +00:00
|
|
|
## Return up to k neighbours at given logarithmic distance.
|
2021-11-22 17:53:52 +00:00
|
|
|
result = r.neighbours(r.idAtDistance(r.localNode.id, distance), k, seenOnly)
|
2020-04-15 21:34:36 +00:00
|
|
|
# This is a bit silly, first getting closest nodes then to only keep the ones
|
|
|
|
# that are exactly the requested distance.
|
2021-11-22 17:53:52 +00:00
|
|
|
keepIf(result, proc(n: Node): bool = r.logDistance(n.id, r.localNode.id) == distance)
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2021-07-13 08:05:46 +00:00
|
|
|
proc neighboursAtDistances*(r: RoutingTable, distances: seq[uint16],
|
2020-09-30 09:26:06 +00:00
|
|
|
k: int = BUCKET_SIZE, seenOnly = false): seq[Node] =
|
|
|
|
## Return up to k neighbours at given logarithmic distances.
|
|
|
|
# TODO: This will currently return nodes with neighbouring distances on the
|
|
|
|
# first one prioritize. It might end up not including all the node distances
|
|
|
|
# requested. Need to rework the logic here and not use the neighbours call.
|
|
|
|
if distances.len > 0:
|
2021-11-22 17:53:52 +00:00
|
|
|
result = r.neighbours(r.idAtDistance(r.localNode.id, distances[0]), k,
|
2020-09-30 09:26:06 +00:00
|
|
|
seenOnly)
|
|
|
|
# This is a bit silly, first getting closest nodes then to only keep the ones
|
|
|
|
# that are exactly the requested distances.
|
|
|
|
keepIf(result, proc(n: Node): bool =
|
2021-11-22 17:53:52 +00:00
|
|
|
distances.contains(r.logDistance(n.id, r.localNode.id)))
|
2020-09-30 09:26:06 +00:00
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc len*(r: RoutingTable): int =
|
2019-12-16 19:38:45 +00:00
|
|
|
for b in r.buckets: result += b.len
|
2019-12-23 17:21:11 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc moveRight[T](arr: var openArray[T], a, b: int) =
|
2019-12-23 17:21:11 +00:00
|
|
|
## In `arr` move elements in range [a, b] right by 1.
|
|
|
|
var t: T
|
2022-11-09 17:57:04 +00:00
|
|
|
shallowCopy(t, arr[b + 1])
|
2019-12-23 17:21:11 +00:00
|
|
|
for i in countdown(b, a):
|
2022-11-09 17:57:04 +00:00
|
|
|
shallowCopy(arr[i + 1], arr[i])
|
|
|
|
shallowCopy(arr[a], t)
|
2019-12-23 17:21:11 +00:00
|
|
|
|
|
|
|
proc setJustSeen*(r: RoutingTable, n: Node) =
|
2020-06-05 20:56:23 +00:00
|
|
|
## Move `n` to the head (most recently seen) of its bucket.
|
2020-06-24 10:29:59 +00:00
|
|
|
## If `n` is not in the routing table, do nothing.
|
2019-12-23 17:21:11 +00:00
|
|
|
let b = r.bucketForNode(n.id)
|
|
|
|
let idx = b.nodes.find(n)
|
2020-06-24 10:29:59 +00:00
|
|
|
if idx >= 0:
|
|
|
|
if idx != 0:
|
|
|
|
b.nodes.moveRight(0, idx - 1)
|
2019-12-23 17:21:11 +00:00
|
|
|
|
2020-06-30 11:35:15 +00:00
|
|
|
if not n.seen:
|
|
|
|
b.nodes[0].seen = true
|
|
|
|
routing_table_nodes.inc(labelValues = ["seen"])
|
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc nodeToRevalidate*(r: RoutingTable): Node =
|
2020-06-24 10:29:59 +00:00
|
|
|
## Return a node to revalidate. The least recently seen node from a random
|
|
|
|
## bucket is selected.
|
2019-12-23 17:21:11 +00:00
|
|
|
var buckets = r.buckets
|
2020-07-13 12:34:53 +00:00
|
|
|
r.rng[].shuffle(buckets)
|
2021-07-29 19:58:10 +00:00
|
|
|
# TODO: Should we prioritize less-recently-updated buckets instead? Could
|
|
|
|
# store a `now` Moment at setJustSeen or at revalidate per bucket.
|
2019-12-23 17:21:11 +00:00
|
|
|
for b in buckets:
|
|
|
|
if b.len > 0:
|
|
|
|
return b.nodes[^1]
|
|
|
|
|
2020-06-11 19:24:52 +00:00
|
|
|
proc randomNodes*(r: RoutingTable, maxAmount: int,
|
2022-11-09 17:57:04 +00:00
|
|
|
pred: proc(x: Node): bool {.gcsafe, noSideEffect.} = nil): seq[Node] =
|
2020-07-12 15:25:18 +00:00
|
|
|
## Get a `maxAmount` of random nodes from the routing table with the `pred`
|
|
|
|
## predicate function applied as filter on the nodes selected.
|
2020-06-11 19:24:52 +00:00
|
|
|
var maxAmount = maxAmount
|
2019-12-23 17:21:11 +00:00
|
|
|
let sz = r.len
|
2020-06-11 19:24:52 +00:00
|
|
|
if maxAmount > sz:
|
|
|
|
debug "Less peers in routing table than maximum requested",
|
|
|
|
requested = maxAmount, present = sz
|
|
|
|
maxAmount = sz
|
2019-12-23 17:21:11 +00:00
|
|
|
|
2020-06-11 19:24:52 +00:00
|
|
|
result = newSeqOfCap[Node](maxAmount)
|
2019-12-23 17:21:11 +00:00
|
|
|
var seen = initHashSet[Node]()
|
|
|
|
|
2020-06-05 20:56:23 +00:00
|
|
|
# This is a rather inefficient way of randomizing nodes from all buckets, but even if we
|
2019-12-23 17:21:11 +00:00
|
|
|
# iterate over all nodes in the routing table, the time it takes would still be
|
|
|
|
# insignificant compared to the time it takes for the network roundtrips when connecting
|
|
|
|
# to nodes.
|
2020-06-05 20:56:23 +00:00
|
|
|
# However, "time it takes" might not be relevant, as there might be no point
|
|
|
|
# in providing more `randomNodes` as the routing table might not have anything
|
|
|
|
# new to provide. And there is no way for the calling code to know this. So
|
|
|
|
# while it will take less total time compared to e.g. an (async)
|
|
|
|
# randomLookup, the time might be wasted as all nodes are possibly seen
|
|
|
|
# already.
|
2020-06-11 19:24:52 +00:00
|
|
|
while len(seen) < maxAmount:
|
2020-07-13 12:34:53 +00:00
|
|
|
let bucket = r.rng[].sample(r.buckets)
|
2019-12-23 17:21:11 +00:00
|
|
|
if bucket.nodes.len != 0:
|
2020-07-13 12:34:53 +00:00
|
|
|
let node = r.rng[].sample(bucket.nodes)
|
2019-12-23 17:21:11 +00:00
|
|
|
if node notin seen:
|
|
|
|
seen.incl(node)
|
2020-06-11 19:24:52 +00:00
|
|
|
if pred.isNil() or node.pred:
|
|
|
|
result.add(node)
|