2022-02-15 13:00:39 +00:00
|
|
|
#
|
|
|
|
# Ethereum P2P
|
|
|
|
# (c) Copyright 2018
|
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# See the file "LICENSE", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
#
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-03-25 04:00:57 +00:00
|
|
|
std/[options, sequtils],
|
|
|
|
asynctest,
|
2022-11-02 16:21:05 +00:00
|
|
|
bearssl/rand,
|
2022-02-15 14:02:25 +00:00
|
|
|
chronicles,
|
2022-03-25 04:00:57 +00:00
|
|
|
chronos,
|
|
|
|
nimcrypto,
|
|
|
|
libp2p/crypto/[crypto, secp],
|
|
|
|
libp2p/[multiaddress, multicodec, multihash, routing_record, signed_envelope],
|
|
|
|
libp2pdht/dht,
|
|
|
|
libp2pdht/discv5/crypto as dhtcrypto,
|
2022-02-24 11:56:11 +00:00
|
|
|
libp2pdht/discv5/protocol as discv5_protocol,
|
2022-03-25 04:00:57 +00:00
|
|
|
stew/byteutils,
|
|
|
|
test_helper
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-03-10 06:02:24 +00:00
|
|
|
proc bootstrapNodes(
|
|
|
|
nodecount: int,
|
2022-03-16 20:32:21 +00:00
|
|
|
bootnodes: seq[SignedPeerRecord],
|
2022-03-25 04:00:57 +00:00
|
|
|
rng = newRng(),
|
2022-03-16 21:14:01 +00:00
|
|
|
delay: int = 0
|
2022-03-25 04:00:57 +00:00
|
|
|
) : Future[seq[(discv5_protocol.Protocol, PrivateKey)]] {.async.} =
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-03-16 21:14:01 +00:00
|
|
|
debug "---- STARTING BOOSTRAPS ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
for i in 0..<nodecount:
|
2022-03-25 04:00:57 +00:00
|
|
|
let privKey = PrivateKey.example(rng)
|
2022-03-10 06:02:24 +00:00
|
|
|
let node = initDiscoveryNode(rng, privKey, localAddress(20302 + i), bootnodes)
|
2022-10-01 16:05:03 +00:00
|
|
|
await node.start()
|
2022-03-10 06:02:24 +00:00
|
|
|
result.add((node, privKey))
|
2022-03-16 21:14:01 +00:00
|
|
|
if delay > 0:
|
|
|
|
await sleepAsync(chronos.milliseconds(delay))
|
|
|
|
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-03-10 06:02:24 +00:00
|
|
|
#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,
|
2022-03-25 04:00:57 +00:00
|
|
|
rng = newRng(),
|
2022-03-18 10:01:14 +00:00
|
|
|
delay: int = 0
|
2022-03-25 04:00:57 +00:00
|
|
|
) : Future[seq[(discv5_protocol.Protocol, PrivateKey)]] {.async.} =
|
2022-02-15 13:00:39 +00:00
|
|
|
|
|
|
|
let
|
2022-03-25 04:00:57 +00:00
|
|
|
bootNodeKey = PrivateKey.fromHex(
|
|
|
|
"a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a617")
|
|
|
|
.expect("Valid private key hex")
|
2022-02-15 13:00:39 +00:00
|
|
|
bootNodeAddr = localAddress(20301)
|
2022-03-10 06:02:24 +00:00
|
|
|
bootNode = initDiscoveryNode(rng, bootNodeKey, bootNodeAddr, @[]) # just a shortcut for new and open
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-15 14:02:25 +00:00
|
|
|
#waitFor bootNode.bootstrap() # immediate, since no bootnodes are defined above
|
2022-02-24 11:56:11 +00:00
|
|
|
|
2022-03-16 20:32:21 +00:00
|
|
|
var res = await bootstrapNodes(nodecount - 1,
|
2022-03-10 06:02:24 +00:00
|
|
|
@[bootnode.localNode.record],
|
2022-03-18 10:01:14 +00:00
|
|
|
rng,
|
|
|
|
delay)
|
2022-03-10 06:02:24 +00:00
|
|
|
res.insert((bootNode, bootNodeKey), 0)
|
|
|
|
return res
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
|
|
|
|
# suite "Providers Tests":
|
2022-02-15 13:00:39 +00:00
|
|
|
suite "Providers Tests: node alone":
|
2022-02-24 11:56:11 +00:00
|
|
|
var
|
|
|
|
rng: ref HmacDrbgContext
|
2022-03-25 04:00:57 +00:00
|
|
|
nodes: seq[(discv5_protocol.Protocol, PrivateKey)]
|
2022-02-24 11:56:11 +00:00
|
|
|
targetId: NodeId
|
2022-03-10 06:02:24 +00:00
|
|
|
node0: discv5_protocol.Protocol
|
2022-03-25 04:00:57 +00:00
|
|
|
privKey0: PrivateKey
|
2022-03-07 04:13:03 +00:00
|
|
|
signedPeerRec0: SignedPeerRecord
|
|
|
|
peerRec0: PeerRecord
|
2022-02-24 11:56:11 +00:00
|
|
|
|
2022-02-25 01:27:43 +00:00
|
|
|
setupAll:
|
2022-03-25 04:00:57 +00:00
|
|
|
rng = newRng()
|
2022-03-16 20:32:21 +00:00
|
|
|
nodes = await bootstrapNetwork(nodecount=1)
|
2022-03-25 04:00:57 +00:00
|
|
|
targetId = NodeId.example(rng)
|
|
|
|
(node0, privKey0) = nodes[0]
|
2022-03-07 04:13:03 +00:00
|
|
|
signedPeerRec0 = privKey0.toSignedPeerRecord
|
|
|
|
peerRec0 = signedPeerRec0.data
|
2022-02-24 11:56:11 +00:00
|
|
|
|
2022-02-25 01:27:43 +00:00
|
|
|
teardownAll:
|
2022-03-07 04:13:03 +00:00
|
|
|
for (n, _) in nodes:
|
2022-03-10 06:02:24 +00:00
|
|
|
await n.closeWait()
|
2022-02-24 11:56:11 +00:00
|
|
|
await sleepAsync(chronos.seconds(3))
|
|
|
|
|
|
|
|
|
|
|
|
test "Node in isolation should store":
|
|
|
|
debug "---- ADDING PROVIDERS ---", nodes = nodes.len
|
2022-03-07 04:13:03 +00:00
|
|
|
let addedTo = await node0.addProvider(targetId, signedPeerRec0)
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "Provider added to: ", addedTo
|
|
|
|
|
|
|
|
debug "---- STARTING CHECKS ---"
|
|
|
|
check (addedTo.len == 1)
|
2022-03-10 06:02:24 +00:00
|
|
|
check (addedTo[0].id == node0.localNode.id)
|
2022-09-29 23:49:55 +00:00
|
|
|
check ((await node0.getProvidersLocal(targetId))[0].data.peerId == peerRec0.peerId)
|
2022-02-24 11:56:11 +00:00
|
|
|
|
|
|
|
test "Node in isolation should retrieve":
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING PROVIDERS LOOKUP ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
let providersRes = await node0.getProviders(targetId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING CHECKS ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
check providersRes.isOk
|
|
|
|
let providers = providersRes.get
|
|
|
|
debug "Providers:", providers
|
2022-03-07 04:13:03 +00:00
|
|
|
check (providers.len > 0 and providers[0].data.peerId == peerRec0.peerId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
test "Should not retrieve bogus":
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-03-25 04:00:57 +00:00
|
|
|
let bogusId = NodeId.example(rng)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING PROVIDERS LOOKUP ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
let providersRes = await node0.getProviders(bogusId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING CHECKS ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
check providersRes.isOk
|
|
|
|
let providers = providersRes.get
|
|
|
|
debug "Providers:", providers
|
2022-02-24 11:56:11 +00:00
|
|
|
check (providers.len == 0)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
suite "Providers Tests: two nodes":
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
var
|
|
|
|
rng: ref HmacDrbgContext
|
2022-03-25 04:00:57 +00:00
|
|
|
nodes: seq[(discv5_protocol.Protocol, PrivateKey)]
|
2022-02-24 11:56:11 +00:00
|
|
|
targetId: NodeId
|
2022-03-10 06:02:24 +00:00
|
|
|
node0: discv5_protocol.Protocol
|
2022-03-25 04:00:57 +00:00
|
|
|
privKey0: PrivateKey
|
2022-03-07 04:13:03 +00:00
|
|
|
signedPeerRec0: SignedPeerRecord
|
|
|
|
peerRec0: PeerRecord
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-25 01:27:43 +00:00
|
|
|
setupAll:
|
2022-03-25 04:00:57 +00:00
|
|
|
rng = newRng()
|
2022-03-16 20:32:21 +00:00
|
|
|
nodes = await bootstrapNetwork(nodecount=3)
|
2022-03-25 04:00:57 +00:00
|
|
|
targetId = NodeId.example(rng)
|
|
|
|
(node0, privKey0) = nodes[0]
|
2022-03-07 04:13:03 +00:00
|
|
|
signedPeerRec0 = privKey0.toSignedPeerRecord
|
|
|
|
peerRec0 = signedPeerRec0.data
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-25 01:27:43 +00:00
|
|
|
teardownAll:
|
2022-03-07 04:13:03 +00:00
|
|
|
for (n, _) in nodes:
|
2022-03-10 06:02:24 +00:00
|
|
|
await n.closeWait()
|
2022-02-15 13:00:39 +00:00
|
|
|
await sleepAsync(chronos.seconds(3))
|
|
|
|
|
2022-03-07 04:13:03 +00:00
|
|
|
test "2 nodes, store and retrieve from same":
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- ADDING PROVIDERS ---"
|
2022-03-07 04:13:03 +00:00
|
|
|
let addedTo = await node0.addProvider(targetId, signedPeerRec0)
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "Provider added to: ", addedTo
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING PROVIDERS LOOKUP ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
let providersRes = await node0.getProviders(targetId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING CHECKS ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
check providersRes.isOk
|
|
|
|
let providers = providersRes.get
|
|
|
|
debug "Providers:", providers
|
2022-03-07 04:13:03 +00:00
|
|
|
check (providers.len == 1 and providers[0].data.peerId == peerRec0.peerId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-03-07 04:13:03 +00:00
|
|
|
test "2 nodes, retrieve from other":
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING PROVIDERS LOOKUP ---"
|
2022-03-07 04:13:03 +00:00
|
|
|
let (node1, _) = nodes[1]
|
2022-03-10 06:02:24 +00:00
|
|
|
let providersRes = await node1.getProviders(targetId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING CHECKS ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
let providers = providersRes.get
|
|
|
|
debug "Providers:", providers
|
2022-03-07 04:13:03 +00:00
|
|
|
check (providers.len == 1 and providers[0].data.peerId == peerRec0.peerId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
suite "Providers Tests: 20 nodes":
|
|
|
|
|
|
|
|
var
|
|
|
|
rng: ref HmacDrbgContext
|
2022-03-25 04:00:57 +00:00
|
|
|
nodes: seq[(discv5_protocol.Protocol, PrivateKey)]
|
2022-02-24 11:56:11 +00:00
|
|
|
targetId: NodeId
|
2022-03-10 06:02:24 +00:00
|
|
|
node0: discv5_protocol.Protocol
|
2022-03-25 04:00:57 +00:00
|
|
|
privKey0: PrivateKey
|
2022-03-07 04:13:03 +00:00
|
|
|
signedPeerRec0: SignedPeerRecord
|
|
|
|
peerRec0: PeerRecord
|
2022-02-24 11:56:11 +00:00
|
|
|
|
2022-02-25 01:27:43 +00:00
|
|
|
setupAll:
|
2022-03-25 04:00:57 +00:00
|
|
|
rng = newRng()
|
2022-03-16 20:32:21 +00:00
|
|
|
nodes = await bootstrapNetwork(nodecount=20)
|
2022-03-25 04:00:57 +00:00
|
|
|
targetId = NodeId.example(rng)
|
|
|
|
(node0, privKey0) = nodes[0]
|
2022-03-07 04:13:03 +00:00
|
|
|
signedPeerRec0 = privKey0.toSignedPeerRecord
|
|
|
|
peerRec0 = signedPeerRec0.data
|
2022-02-24 11:56:11 +00:00
|
|
|
|
2022-02-16 10:28:07 +00:00
|
|
|
await sleepAsync(chronos.seconds(15))
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-25 01:27:43 +00:00
|
|
|
teardownAll:
|
2022-03-07 04:13:03 +00:00
|
|
|
for (n, _) in nodes: # if last test is enabled, we need nodes[1..^1] here
|
2022-03-10 06:02:24 +00:00
|
|
|
await n.closeWait()
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-03-07 04:13:03 +00:00
|
|
|
test "20 nodes, store and retrieve from same":
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- ADDING PROVIDERS ---"
|
2022-03-07 04:13:03 +00:00
|
|
|
let addedTo = await node0.addProvider(targetId, signedPeerRec0)
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "Provider added to: ", addedTo
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING PROVIDERS LOOKUP ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
let providersRes = await node0.getProviders(targetId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING CHECKS ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
let providers = providersRes.get
|
|
|
|
debug "Providers:", providers
|
2022-03-07 04:13:03 +00:00
|
|
|
check (providers.len == 1 and providers[0].data.peerId == peerRec0.peerId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-03-07 04:13:03 +00:00
|
|
|
test "20 nodes, retrieve from other":
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING PROVIDERS LOOKUP ---"
|
2022-03-07 04:13:03 +00:00
|
|
|
let (node19, _) = nodes[^2]
|
2022-03-10 06:02:24 +00:00
|
|
|
let providersRes = await node19.getProviders(targetId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-02-24 11:56:11 +00:00
|
|
|
debug "---- STARTING CHECKS ---"
|
2022-03-10 06:02:24 +00:00
|
|
|
let providers = providersRes.get
|
|
|
|
debug "Providers:", providers
|
2022-03-07 04:13:03 +00:00
|
|
|
check (providers.len == 1 and providers[0].data.peerId == peerRec0.peerId)
|
2022-02-15 13:00:39 +00:00
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
test "20 nodes, retrieve after bootnodes dies":
|
2022-03-16 21:25:12 +00:00
|
|
|
debug "---- KILLING BOOTSTRAP NODE ---"
|
|
|
|
let (node0, _) = nodes[0]
|
|
|
|
let (node18, _) = nodes[^2]
|
|
|
|
await node0.closeWait()
|
|
|
|
nodes.del(0)
|
2022-02-24 11:56:11 +00:00
|
|
|
|
2022-03-16 21:25:12 +00:00
|
|
|
debug "---- STARTING PROVIDERS LOOKUP ---"
|
|
|
|
let providersRes = await node18.getProviders(targetId)
|
2022-02-24 11:56:11 +00:00
|
|
|
|
2022-03-16 21:25:12 +00:00
|
|
|
debug "---- STARTING CHECKS ---"
|
|
|
|
let providers = providersRes.get
|
|
|
|
debug "Providers:", providers
|
|
|
|
check (providers.len == 1 and providers[0].data.peerId == peerRec0.peerId)
|