Remove nim-eth dependency (#37)

This commit is contained in:
Tanguy 2022-04-08 12:13:14 +02:00 committed by GitHub
parent faf5cad9d4
commit f824c0a3cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 7 deletions

View File

@ -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",

View File

@ -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

View File

@ -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]

View File

@ -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