2020-06-17 11:51:30 +00:00
|
|
|
import
|
2020-07-07 08:56:26 +00:00
|
|
|
testutils/unittests, stew/shims/net, bearssl,
|
2020-06-17 11:51:30 +00:00
|
|
|
eth/[keys, rlp, trie/db],
|
|
|
|
eth/p2p/discoveryv5/[discovery_db, enr, node, types, routing_table, encoding],
|
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol
|
|
|
|
|
|
|
|
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] = [],
|
|
|
|
localEnrFields: openarray[FieldPair] = []):
|
|
|
|
discv5_protocol.Protocol =
|
|
|
|
var db = DiscoveryDB.init(newMemoryDB())
|
|
|
|
result = newProtocol(privKey, db,
|
|
|
|
some(address.ip),
|
|
|
|
address.port, address.port,
|
|
|
|
bootstrapRecords = bootstrapRecords,
|
2020-07-07 08:56:26 +00:00
|
|
|
localEnrFields = localEnrFields, 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
|
|
|
|
|
|
|
|
# Creating a random packet with specific nodeid each time
|
2020-07-07 08:56:26 +00:00
|
|
|
proc randomPacket*(rng: var BrHmacDrbgContext, tag: PacketTag): seq[byte] =
|
2020-06-17 11:51:30 +00:00
|
|
|
var
|
|
|
|
authTag: AuthTag
|
|
|
|
msg: array[44, byte]
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
brHmacDrbgGenerate(rng, authTag)
|
|
|
|
brHmacDrbgGenerate(rng, msg)
|
2020-06-17 11:51:30 +00:00
|
|
|
result.add(tag)
|
|
|
|
result.add(rlp.encode(authTag))
|
|
|
|
result.add(msg)
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
proc generateNode*(privKey: PrivateKey, port: int = 20302,
|
2020-06-17 11:51:30 +00:00
|
|
|
localEnrFields: openarray[FieldPair] = []): Node =
|
|
|
|
let port = Port(port)
|
|
|
|
let enr = enr.Record.init(1, privKey, some(ValidIpAddress.init("127.0.0.1")),
|
|
|
|
port, port, localEnrFields).expect("Properly intialized private key")
|
|
|
|
result = newNode(enr).expect("Properly initialized node")
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
proc nodeAtDistance*(n: Node, rng: var BrHmacDrbgContext, d: uint32): Node =
|
2020-06-17 11:51:30 +00:00
|
|
|
while true:
|
2020-07-07 08:56:26 +00:00
|
|
|
let node = generateNode(PrivateKey.random(rng))
|
2020-06-17 11:51:30 +00:00
|
|
|
if logDist(n.id, node.id) == d:
|
|
|
|
return node
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
proc nodesAtDistance*(
|
|
|
|
n: Node, rng: var BrHmacDrbgContext, d: uint32, amount: int): seq[Node] =
|
2020-06-17 11:51:30 +00:00
|
|
|
for i in 0..<amount:
|
2020-07-07 08:56:26 +00:00
|
|
|
result.add(nodeAtDistance(n, rng, d))
|
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)
|