From d33d27c2ee685ec2bec37f96886251a6cf810ec4 Mon Sep 17 00:00:00 2001 From: kdeme Date: Fri, 22 Jan 2021 11:00:25 +0100 Subject: [PATCH] Small improvements and cleanup --- eth/p2p/discoveryv5/ip_vote.nim | 6 +++--- eth/p2p/discoveryv5/node.nim | 3 +++ eth/p2p/discoveryv5/protocol.nim | 6 +++--- eth/p2p/discoveryv5/types.nim | 12 ------------ 4 files changed, 9 insertions(+), 18 deletions(-) diff --git a/eth/p2p/discoveryv5/ip_vote.nim b/eth/p2p/discoveryv5/ip_vote.nim index 2c8cd43..d2e6802 100644 --- a/eth/p2p/discoveryv5/ip_vote.nim +++ b/eth/p2p/discoveryv5/ip_vote.nim @@ -30,7 +30,7 @@ const IpVoteTimeout = 5.minutes ## Duration until a vote expires type IpVote* = object - votes: Table[NodeId, (Address, chronos.Moment)] + votes: Table[NodeId, tuple[address: Address, expiry: chronos.Moment]] threshold: uint ## Minimum threshold to allow for a majority to count func init*(T: type IpVote, threshold: uint = 10): T = @@ -59,10 +59,10 @@ proc majority*(ipvote: var IpVote): Option[Address] = pruneList: seq[NodeId] ipCount: CountTable[Address] for k, v in ipvote.votes: - if now > v[1]: + if now > v.expiry: pruneList.add(k) else: - ipCount.inc(v[0]) + ipCount.inc(v.address) for id in pruneList: ipvote.votes.del(id) diff --git a/eth/p2p/discoveryv5/node.nim b/eth/p2p/discoveryv5/node.nim index 98d7402..a8aa506 100644 --- a/eth/p2p/discoveryv5/node.nim +++ b/eth/p2p/discoveryv5/node.nim @@ -73,6 +73,9 @@ proc random*(T: type NodeId, rng: var BrHmacDrbgContext): T = id +func toBytes*(id: NodeId): array[32, byte] = + id.toByteArrayBE() + func `$`*(id: NodeId): string = id.toHex() diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index 65ee610..860234f 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -75,7 +75,7 @@ import std/[tables, sets, options, math, sequtils, algorithm], stew/shims/net as stewNet, json_serialization/std/net, - stew/endians2, chronicles, chronos, stint, bearssl, metrics, + stew/[endians2, arrayops], chronicles, chronos, stint, bearssl, metrics, eth/[rlp, keys, async_utils], types, encoding, node, routing_table, enr, random2, sessions, ip_vote @@ -866,12 +866,12 @@ proc revalidateNode*(d: Protocol, n: Node) # Get IP and port from pong message and add it to the ip votes if res.ip.len == 4: var ip: array[4, byte] - copyMem(addr ip, unsafeAddr res.ip[0], sizeof(ip)) + discard copyFrom(ip, res.ip) let a = Address(ip: ipv4(ip), port: Port(res.port)) d.ipVote.insert(n.id, a); elif res.ip.len == 16: var ip: array[16, byte] - copyMem(addr ip, unsafeAddr res.ip[0], sizeof(ip)) + discard copyFrom(ip, res.ip) let a = Address(ip: ipv6(ip), port: Port(res.port)) d.ipVote.insert(n.id, a); else: diff --git a/eth/p2p/discoveryv5/types.nim b/eth/p2p/discoveryv5/types.nim index 9e6c9bb..e48e159 100644 --- a/eth/p2p/discoveryv5/types.nim +++ b/eth/p2p/discoveryv5/types.nim @@ -1,6 +1,5 @@ import std/hashes, - stint, eth/rlp, enr, node {.push raises: [Defect].} @@ -117,17 +116,6 @@ proc append*(writer: var RlpWriter, value: RequestId) = proc hash*(reqId: RequestId): Hash = hash(reqId.id) -proc toBytes*(id: NodeId): array[32, byte] {.inline.} = - id.toByteArrayBE() - -proc hash*(id: NodeId): Hash {.inline.} = - result = hashData(unsafeAddr id, sizeof(id)) - -# TODO: To make this work I think we also need to implement `==` due to case -# fields in object -proc hash*(address: Address): Hash {.inline.} = - hashData(unsafeAddr address, sizeof(address)) - proc hash*(key: HandshakeKey): Hash = result = key.nodeId.hash !& key.address.hash result = !$result