From ae0920d40dd5c6334113e73bb60657ab34113f51 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Mon, 6 Dec 2021 15:24:07 +0100 Subject: [PATCH] Remove hashData usage on objects (#441) * Remove hashData usage on objects * Add hash func for NodeId to avoid using the one of stint --- eth/net/utils.nim | 2 +- eth/p2p/discoveryv5/node.nim | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/eth/net/utils.nim b/eth/net/utils.nim index 1236dd9..ce496ae 100644 --- a/eth/net/utils.nim +++ b/eth/net/utils.nim @@ -16,7 +16,7 @@ type limit*: uint ips: Table[ValidIpAddress, uint] -func hash(ip: ValidIpAddress): Hash = +func hash*(ip: ValidIpAddress): Hash = case ip.family of IpAddressFamily.IPv6: hash(ip.address_v6) of IpAddressFamily.IPv4: hash(ip.address_v4) diff --git a/eth/p2p/discoveryv5/node.nim b/eth/p2p/discoveryv5/node.nim index d1ce2df..6efb637 100644 --- a/eth/p2p/discoveryv5/node.nim +++ b/eth/p2p/discoveryv5/node.nim @@ -10,7 +10,8 @@ import std/hashes, nimcrypto, stint, chronos, stew/shims/net, chronicles, - ../../keys, ./enr + ../../keys, ../../net/utils, + ./enr export stint @@ -82,6 +83,9 @@ func `==`*(a, b: Node): bool = (a.isNil and b.isNil) or (not a.isNil and not b.isNil and a.pubkey == b.pubkey) +func hash*(id: NodeId): Hash = + hash(id.toByteArrayBE) + proc random*(T: type NodeId, rng: var BrHmacDrbgContext): T = var id: NodeId brHmacDrbgGenerate(addr rng, addr id, csize_t(sizeof(id))) @@ -106,7 +110,8 @@ func shortLog*(id: NodeId): string = chronicles.formatIt(NodeId): shortLog(it) func hash*(a: Address): hashes.Hash = - hashData(unsafeAddr a, sizeof(a)) + let res = a.ip.hash !& a.port.hash + !$res func `$`*(a: Address): string = result.add($a.ip)