partial tests (AddProvider)

This commit is contained in:
Csaba Kiraly 2022-02-15 15:02:25 +01:00
parent 43a27ed997
commit be5d911cda
1 changed files with 29 additions and 30 deletions

View File

@ -13,43 +13,42 @@ import
std/sequtils, std/sequtils,
chronos, stew/byteutils, nimcrypto, testutils/unittests, chronos, stew/byteutils, nimcrypto, testutils/unittests,
eth/keys, eth/keys,
eth/p2p/enode, ../../eth/p2p/providers,
../../dht/[discovery, kademlia, node], chronicles,
chronicles ../../eth/p2p/discoveryv5/protocol as discv5_protocol,
./discv5_test_helper,
libp2p/routing_record
proc localAddress(port: int): Address = proc initProvidersNode*(
let port = Port(port) rng: ref BrHmacDrbgContext,
result = Address(udpPort: port, tcpPort: port, privKey: keys.PrivateKey,
ip: parseIpAddress("127.0.0.1")) address: Address,
bootstrapRecords: openArray[Record] = []):
ProvidersProtocol =
proc initDiscoveryNode(privKey: PrivateKey, address: Address, let d = initDiscoveryNode(rng, privKey, address, bootstrapRecords)
bootnodes: seq[ENode]): DiscoveryProtocol = ProvidersProtocol(discovery: d)
let node = newDiscoveryProtocol(privKey, address, bootnodes)
node.open()
return node proc bootstrapNodes(nodecount: int, bootnodes: openArray[Record], rng = keys.newRng()) : seq[ProvidersProtocol] =
proc bootstrapNodes(nodecount: int, bootnodes: seq[ENode], rng = keys.newRng()) : Future[seq[DiscoveryProtocol]] {.async.} =
for i in 0..<nodecount: for i in 0..<nodecount:
let node = initDiscoveryNode(PrivateKey.random(rng[]), localAddress(20302 + i), bootnodes) let node = initProvidersNode(rng, keys.PrivateKey.random(rng[]), localAddress(20302 + i), bootnodes)
result.add(node) result.add(node)
debug "---- STARTING BOOSTRAPS ---" debug "---- STARTING BOOSTRAPS ---"
await allFutures(result.mapIt(it.bootstrap())) # this waits for bootstrap based on bootENode, which includes bonding with all its ping pongs #await allFutures(result.mapIt(it.bootstrap())) # this waits for bootstrap based on bootENode, which includes bonding with all its ping pongs
proc bootstrapNetwork(nodecount: int, rng = keys.newRng()) : Future[seq[DiscoveryProtocol]] {.async.} = proc bootstrapNetwork(nodecount: int, rng = keys.newRng()) : seq[ProvidersProtocol] =
let let
bootNodeKey = PrivateKey.fromHex( bootNodeKey = keys.PrivateKey.fromHex(
"a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")[] "a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")[]
bootNodeAddr = localAddress(20301) bootNodeAddr = localAddress(20301)
bootENode = ENode(pubkey: bootNodeKey.toPublicKey(), address: bootNodeAddr) bootNode = initProvidersNode(rng, bootNodeKey, bootNodeAddr, @[]) # just a shortcut for new and open
bootNode = initDiscoveryNode(bootNodeKey, bootNodeAddr, @[]) # just a shortcut for new and open
waitFor bootNode.bootstrap() # immediate, since no bootnodes are defined above #waitFor bootNode.bootstrap() # immediate, since no bootnodes are defined above
result = await bootstrapNodes(nodecount - 1, @[bootENode], rng = rng) result = bootstrapNodes(nodecount - 1, @[bootnode.discovery.localNode.record], rng = rng)
result.insert(bootNode, 0) result.insert(bootNode, 0)
@ -58,13 +57,13 @@ suite "Providers Tests: node alone":
asyncTest "node alone": asyncTest "node alone":
let let
rng = keys.newRng() rng = keys.newRng()
nodes = await bootstrapNetwork(nodecount=1) nodes = bootstrapNetwork(nodecount=1)
targetId = toNodeId(PrivateKey.random(rng[]).toPublicKey) targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey)
asyncTest "Node in isolation should store": asyncTest "Node in isolation should store":
debug "---- ADDING PROVIDERS ---" debug "---- ADDING PROVIDERS ---"
let addedTo = await nodes[0].addProvider(targetId) let addedTo = await nodes[0].addProvider(targetId, PeerRecord())
debug "Provider added to: ", addedTo debug "Provider added to: ", addedTo
debug "---- STARTING CHECKS ---" debug "---- STARTING CHECKS ---"
@ -93,19 +92,19 @@ suite "Providers Tests: node alone":
check (providers.len == 0) check (providers.len == 0)
for n in nodes: for n in nodes:
n.close() n.discovery.close()
await sleepAsync(chronos.seconds(3)) await sleepAsync(chronos.seconds(3))
asyncTest "Providers Tests: two nodes": asyncTest "Providers Tests: two nodes":
let let
rng = keys.newRng() rng = keys.newRng()
nodes = await bootstrapNetwork(nodecount=2) nodes = bootstrapNetwork(nodecount=2)
targetId = toNodeId(PrivateKey.random(rng[]).toPublicKey) targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey)
asyncTest "2 nodes, store and retieve from same": asyncTest "2 nodes, store and retieve from same":
debug "---- ADDING PROVIDERS ---" debug "---- ADDING PROVIDERS ---"
let addedTo = await nodes[0].addProvider(targetId) let addedTo = await nodes[0].addProvider(targetId, PeerRecord())
debug "Provider added to: ", addedTo debug "Provider added to: ", addedTo
debug "---- STARTING PROVIDERS LOOKUP ---" debug "---- STARTING PROVIDERS LOOKUP ---"
@ -124,7 +123,7 @@ suite "Providers Tests: node alone":
check (providers.len == 1 and providers[0].id == nodes[0].thisNode.id) check (providers.len == 1 and providers[0].id == nodes[0].thisNode.id)
for n in nodes: for n in nodes:
n.close() n.discovery.close()
await sleepAsync(chronos.seconds(3)) await sleepAsync(chronos.seconds(3))
asyncTest "Providers Tests: 20 nodes": asyncTest "Providers Tests: 20 nodes":