2020-06-17 11:51:30 +00:00
|
|
|
import
|
2020-11-26 17:20:15 +00:00
|
|
|
stew/shims/net, bearssl, chronos,
|
2021-04-06 11:33:24 +00:00
|
|
|
../../eth/keys,
|
|
|
|
../../eth/p2p/discoveryv5/[enr, node, routing_table],
|
|
|
|
../../eth/p2p/discoveryv5/protocol as discv5_protocol
|
2020-06-17 11:51:30 +00:00
|
|
|
|
2020-11-26 17:20:15 +00:00
|
|
|
export net
|
|
|
|
|
2020-06-17 11:51:30 +00:00
|
|
|
proc localAddress*(port: int): Address =
|
|
|
|
Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port))
|
|
|
|
|
2020-07-07 21:39:32 +00:00
|
|
|
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey,
|
|
|
|
address: Address,
|
2020-06-17 11:51:30 +00:00
|
|
|
bootstrapRecords: openarray[Record] = [],
|
2020-07-08 11:13:29 +00:00
|
|
|
localEnrFields: openarray[(string, seq[byte])] = [],
|
|
|
|
previousRecord = none[enr.Record]()):
|
2020-06-17 11:51:30 +00:00
|
|
|
discv5_protocol.Protocol =
|
2020-11-26 17:20:15 +00:00
|
|
|
# set bucketIpLimit to allow bucket split
|
|
|
|
let tableIpLimits = TableIpLimits(tableIpLimit: 1000, bucketIpLimit: 24)
|
|
|
|
|
2020-09-10 12:49:48 +00:00
|
|
|
result = newProtocol(privKey,
|
2020-06-17 11:51:30 +00:00
|
|
|
some(address.ip),
|
2021-03-02 16:13:29 +00:00
|
|
|
some(address.port), some(address.port),
|
|
|
|
bindPort = address.port,
|
2020-06-17 11:51:30 +00:00
|
|
|
bootstrapRecords = bootstrapRecords,
|
2020-07-08 11:13:29 +00:00
|
|
|
localEnrFields = localEnrFields,
|
2020-11-26 17:20:15 +00:00
|
|
|
previousRecord = previousRecord,
|
|
|
|
tableIpLimits = tableIpLimits,
|
|
|
|
rng = rng)
|
2020-06-17 11:51:30 +00:00
|
|
|
|
|
|
|
result.open()
|
|
|
|
|
|
|
|
proc nodeIdInNodes*(id: NodeId, nodes: openarray[Node]): bool =
|
|
|
|
for n in nodes:
|
|
|
|
if id == n.id: return true
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
proc generateNode*(privKey: PrivateKey, port: int = 20302,
|
2020-11-26 17:20:15 +00:00
|
|
|
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1"),
|
2020-06-17 11:51:30 +00:00
|
|
|
localEnrFields: openarray[FieldPair] = []): Node =
|
|
|
|
let port = Port(port)
|
2020-11-26 17:20:15 +00:00
|
|
|
let enr = enr.Record.init(1, privKey, some(ip),
|
2021-01-26 13:11:22 +00:00
|
|
|
some(port), some(port), localEnrFields).expect("Properly intialized private key")
|
2020-06-17 11:51:30 +00:00
|
|
|
result = newNode(enr).expect("Properly initialized node")
|
|
|
|
|
2020-11-26 17:20:15 +00:00
|
|
|
proc nodeAndPrivKeyAtDistance*(n: Node, rng: var BrHmacDrbgContext, d: uint32,
|
|
|
|
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): (Node, PrivateKey) =
|
2020-06-17 11:51:30 +00:00
|
|
|
while true:
|
2020-11-26 17:20:15 +00:00
|
|
|
let pk = PrivateKey.random(rng)
|
|
|
|
let node = generateNode(pk, ip = ip)
|
2020-06-17 11:51:30 +00:00
|
|
|
if logDist(n.id, node.id) == d:
|
2020-11-26 17:20:15 +00:00
|
|
|
return (node, pk)
|
|
|
|
|
|
|
|
proc nodeAtDistance*(n: Node, rng: var BrHmacDrbgContext, d: uint32,
|
|
|
|
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node =
|
|
|
|
let (node, _) = n.nodeAndPrivKeyAtDistance(rng, d, ip)
|
|
|
|
node
|
2020-06-17 11:51:30 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
proc nodesAtDistance*(
|
2020-11-26 17:20:15 +00:00
|
|
|
n: Node, rng: var BrHmacDrbgContext, d: uint32, amount: int,
|
|
|
|
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] =
|
|
|
|
for i in 0..<amount:
|
|
|
|
result.add(nodeAtDistance(n, rng, d, ip))
|
|
|
|
|
|
|
|
proc nodesAtDistanceUniqueIp*(
|
|
|
|
n: Node, rng: var BrHmacDrbgContext, d: uint32, amount: int,
|
|
|
|
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] =
|
|
|
|
var ta = initTAddress(ip, Port(0))
|
2020-06-17 11:51:30 +00:00
|
|
|
for i in 0..<amount:
|
2020-11-26 17:20:15 +00:00
|
|
|
ta.inc()
|
|
|
|
result.add(nodeAtDistance(n, rng, d, ValidIpAddress.init(ta.address())))
|
2020-06-30 11:35:15 +00:00
|
|
|
|
|
|
|
proc addSeenNode*(d: discv5_protocol.Protocol, n: Node): bool =
|
|
|
|
# Add it as a seen node, warning: for testing convenience only!
|
|
|
|
n.seen = true
|
|
|
|
d.addNode(n)
|