Remove hashData usage on objects (#441)

* Remove hashData usage on objects

* Add hash func for NodeId to avoid using the one of stint
This commit is contained in:
Kim De Mey 2021-12-06 15:24:07 +01:00 committed by GitHub
parent 307b4e51b4
commit ae0920d40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -16,7 +16,7 @@ type
limit*: uint limit*: uint
ips: Table[ValidIpAddress, uint] ips: Table[ValidIpAddress, uint]
func hash(ip: ValidIpAddress): Hash = func hash*(ip: ValidIpAddress): Hash =
case ip.family case ip.family
of IpAddressFamily.IPv6: hash(ip.address_v6) of IpAddressFamily.IPv6: hash(ip.address_v6)
of IpAddressFamily.IPv4: hash(ip.address_v4) of IpAddressFamily.IPv4: hash(ip.address_v4)

View File

@ -10,7 +10,8 @@
import import
std/hashes, std/hashes,
nimcrypto, stint, chronos, stew/shims/net, chronicles, nimcrypto, stint, chronos, stew/shims/net, chronicles,
../../keys, ./enr ../../keys, ../../net/utils,
./enr
export stint export stint
@ -82,6 +83,9 @@ func `==`*(a, b: Node): bool =
(a.isNil and b.isNil) or (a.isNil and b.isNil) or
(not a.isNil and not b.isNil and a.pubkey == b.pubkey) (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 = proc random*(T: type NodeId, rng: var BrHmacDrbgContext): T =
var id: NodeId var id: NodeId
brHmacDrbgGenerate(addr rng, addr id, csize_t(sizeof(id))) brHmacDrbgGenerate(addr rng, addr id, csize_t(sizeof(id)))
@ -106,7 +110,8 @@ func shortLog*(id: NodeId): string =
chronicles.formatIt(NodeId): shortLog(it) chronicles.formatIt(NodeId): shortLog(it)
func hash*(a: Address): hashes.Hash = func hash*(a: Address): hashes.Hash =
hashData(unsafeAddr a, sizeof(a)) let res = a.ip.hash !& a.port.hash
!$res
func `$`*(a: Address): string = func `$`*(a: Address): string =
result.add($a.ip) result.add($a.ip)