diff --git a/libp2pdht.nimble b/libp2pdht.nimble index 80f43ca..54cd671 100644 --- a/libp2pdht.nimble +++ b/libp2pdht.nimble @@ -13,7 +13,6 @@ requires "nim >= 1.2.0", "bearssl >= 0.1.5 & < 0.2.0", "chronicles >= 0.10.2 & < 0.11.0", "chronos >= 3.0.11 & < 3.1.0", - "eth >= 1.0.0 & < 1.1.0", # to be removed in https://github.com/status-im/nim-libp2p-dht/issues/2 "libp2p#c7504d2446717a48a79c8b15e0f21bbfc84957ba", "metrics", "protobufserialization >= 0.2.0 & < 0.3.0", diff --git a/libp2pdht/private/eth/p2p/discoveryv5/node.nim b/libp2pdht/private/eth/p2p/discoveryv5/node.nim index aa9654c..0ee7327 100644 --- a/libp2pdht/private/eth/p2p/discoveryv5/node.nim +++ b/libp2pdht/private/eth/p2p/discoveryv5/node.nim @@ -15,7 +15,6 @@ import nimcrypto, stew/shims/net, stint, - eth/net/utils, ./crypto, ./spr @@ -123,6 +122,11 @@ func shortLog*(id: NodeId): string = result.add(sid[i]) chronicles.formatIt(NodeId): shortLog(it) +func hash*(ip: ValidIpAddress): Hash = + case ip.family + of IpAddressFamily.IPv6: hash(ip.address_v6) + of IpAddressFamily.IPv4: hash(ip.address_v4) + func hash*(a: Address): hashes.Hash = let res = a.ip.hash !& a.port.hash !$res diff --git a/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim b/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim index 31e8eb0..fbd0ef3 100644 --- a/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim +++ b/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim @@ -76,8 +76,8 @@ import std/[tables, sets, options, math, sequtils, algorithm], stew/shims/net as stewNet, json_serialization/std/net, - stew/[base64, endians2, results], chronicles, chronos, chronos/timer, stint, bearssl, - metrics, eth/async_utils, + stew/[base64, endians2, results], chronicles, chronicles/chronos_tools, chronos, chronos/timer, stint, bearssl, + metrics, libp2p/[crypto/crypto, routing_record], "."/[transport, messages, messages_encoding, node, routing_table, spr, random2, ip_vote, nodes_verification] diff --git a/libp2pdht/private/eth/p2p/discoveryv5/routing_table.nim b/libp2pdht/private/eth/p2p/discoveryv5/routing_table.nim index d5542d2..eb80210 100644 --- a/libp2pdht/private/eth/p2p/discoveryv5/routing_table.nim +++ b/libp2pdht/private/eth/p2p/discoveryv5/routing_table.nim @@ -8,9 +8,8 @@ {.push raises: [Defect].} import - std/[algorithm, times, sequtils, bitops, sets, options], + std/[algorithm, times, sequtils, bitops, sets, options, tables], stint, chronicles, metrics, bearssl, chronos, stew/shims/net as stewNet, - eth/net/utils, "."/[node, random2, spr] export options @@ -28,6 +27,11 @@ type calculateLogDistance*: LogDistanceProc calculateIdAtDistance*: IdAtDistanceProc + IpLimits* = object + limit*: uint + ips: Table[ValidIpAddress, uint] + + RoutingTable* = object localNode*: Node buckets*: seq[KBucket] @@ -92,7 +96,21 @@ type ReplacementExisting NoAddress -# xor distance functions +func inc*(ipLimits: var IpLimits, ip: ValidIpAddress): bool = + let val = ipLimits.ips.getOrDefault(ip, 0) + if val < ipLimits.limit: + ipLimits.ips[ip] = val + 1 + true + else: + false + +func dec*(ipLimits: var IpLimits, ip: ValidIpAddress) = + let val = ipLimits.ips.getOrDefault(ip, 0) + if val == 1: + ipLimits.ips.del(ip) + elif val > 1: + ipLimits.ips[ip] = val - 1 + func distance*(a, b: NodeId): UInt256 = ## Calculate the distance to a NodeId. a xor b