Small improvements and cleanup

This commit is contained in:
kdeme 2021-01-22 11:00:25 +01:00 committed by zah
parent e43ee6ef9c
commit d33d27c2ee
4 changed files with 9 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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