2023-10-11 08:58:45 +02:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
std/[options, sequtils, tables],
|
|
|
|
|
testutils/unittests,
|
|
|
|
|
chronos,
|
|
|
|
|
chronicles,
|
|
|
|
|
libp2p/switch,
|
|
|
|
|
libp2p/peerId,
|
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
|
libp2p/multistream,
|
|
|
|
|
libp2p/muxers/muxer,
|
|
|
|
|
eth/keys,
|
|
|
|
|
eth/p2p/discoveryv5/enr
|
|
|
|
|
import
|
2026-02-13 11:23:21 +01:00
|
|
|
waku/[
|
|
|
|
|
waku_node,
|
|
|
|
|
waku_core/topics,
|
|
|
|
|
waku_core,
|
|
|
|
|
node/peer_manager,
|
|
|
|
|
discovery/waku_discv5,
|
|
|
|
|
waku_metadata,
|
|
|
|
|
waku_relay/protocol,
|
|
|
|
|
],
|
2023-10-11 08:58:45 +02:00
|
|
|
./testlib/wakucore,
|
|
|
|
|
./testlib/wakunode
|
|
|
|
|
|
|
|
|
|
procSuite "Waku Metadata Protocol":
|
|
|
|
|
asyncTest "request() returns the supported metadata of the peer":
|
2024-06-05 15:32:35 +02:00
|
|
|
let clusterId = 10.uint16
|
2023-10-11 08:58:45 +02:00
|
|
|
let
|
2024-03-16 00:08:47 +01:00
|
|
|
node1 = newTestWakuNode(
|
|
|
|
|
generateSecp256k1Key(),
|
|
|
|
|
parseIpAddress("0.0.0.0"),
|
|
|
|
|
Port(0),
|
|
|
|
|
clusterId = clusterId,
|
|
|
|
|
)
|
|
|
|
|
node2 = newTestWakuNode(
|
|
|
|
|
generateSecp256k1Key(),
|
|
|
|
|
parseIpAddress("0.0.0.0"),
|
|
|
|
|
Port(0),
|
|
|
|
|
clusterId = clusterId,
|
|
|
|
|
)
|
2023-10-11 08:58:45 +02:00
|
|
|
|
2026-02-13 11:23:21 +01:00
|
|
|
# Mount metadata protocol on both nodes before starting
|
|
|
|
|
discard node1.mountMetadata(clusterId, @[])
|
|
|
|
|
discard node2.mountMetadata(clusterId, @[])
|
|
|
|
|
|
|
|
|
|
# Mount relay so metadata can track subscriptions
|
|
|
|
|
discard await node1.mountRelay()
|
|
|
|
|
discard await node2.mountRelay()
|
|
|
|
|
|
2023-10-11 08:58:45 +02:00
|
|
|
# Start nodes
|
|
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
|
2026-02-13 11:23:21 +01:00
|
|
|
# Subscribe to topics on node1 - relay will track these and metadata will report them
|
|
|
|
|
let noOpHandler: WakuRelayHandler = proc(
|
|
|
|
|
pubsubTopic: PubsubTopic, message: WakuMessage
|
|
|
|
|
): Future[void] {.async.} =
|
|
|
|
|
discard
|
|
|
|
|
|
|
|
|
|
node1.wakuRelay.subscribe("/waku/2/rs/10/7", noOpHandler)
|
|
|
|
|
node1.wakuRelay.subscribe("/waku/2/rs/10/6", noOpHandler)
|
2023-10-30 16:58:15 -04:00
|
|
|
|
2023-10-11 08:58:45 +02:00
|
|
|
# Create connection
|
2024-03-16 00:08:47 +01:00
|
|
|
let connOpt = await node2.peerManager.dialPeer(
|
|
|
|
|
node1.switch.peerInfo.toRemotePeerInfo(), WakuMetadataCodec
|
|
|
|
|
)
|
2023-10-11 08:58:45 +02:00
|
|
|
require:
|
2026-02-13 11:23:21 +01:00
|
|
|
connOpt.isSome()
|
2023-10-11 08:58:45 +02:00
|
|
|
|
|
|
|
|
# Request metadata
|
|
|
|
|
let response1 = await node2.wakuMetadata.request(connOpt.get())
|
|
|
|
|
|
|
|
|
|
# Check the response or dont even continue
|
|
|
|
|
require:
|
2026-02-13 11:23:21 +01:00
|
|
|
response1.isOk()
|
2023-10-11 08:58:45 +02:00
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
response1.get().clusterId.get() == clusterId
|
2023-10-30 16:58:15 -04:00
|
|
|
response1.get().shards == @[uint32(6), uint32(7)]
|
2026-02-13 11:23:21 +01:00
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop()])
|
|
|
|
|
|
|
|
|
|
asyncTest "Metadata reports configured shards before relay subscription":
|
|
|
|
|
## Given: Node with configured shards but no relay subscriptions yet
|
|
|
|
|
let
|
|
|
|
|
clusterId = 10.uint16
|
|
|
|
|
configuredShards = @[uint16(0), uint16(1)]
|
|
|
|
|
|
|
|
|
|
let node1 = newTestWakuNode(
|
|
|
|
|
generateSecp256k1Key(),
|
|
|
|
|
parseIpAddress("0.0.0.0"),
|
|
|
|
|
Port(0),
|
|
|
|
|
clusterId = clusterId,
|
|
|
|
|
subscribeShards = configuredShards,
|
|
|
|
|
)
|
|
|
|
|
let node2 = newTestWakuNode(
|
|
|
|
|
generateSecp256k1Key(), parseIpAddress("0.0.0.0"), Port(0), clusterId = clusterId
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Mount metadata with configured shards on node1
|
|
|
|
|
discard node1.mountMetadata(clusterId, configuredShards)
|
|
|
|
|
# Mount metadata on node2 so it can make requests
|
|
|
|
|
discard node2.mountMetadata(clusterId, @[])
|
|
|
|
|
|
|
|
|
|
# Start nodes (relay is NOT mounted yet on node1)
|
|
|
|
|
await allFutures([node1.start(), node2.start()])
|
|
|
|
|
|
|
|
|
|
## When: Node2 requests metadata from Node1 before relay is active
|
|
|
|
|
let connOpt = await node2.peerManager.dialPeer(
|
|
|
|
|
node1.switch.peerInfo.toRemotePeerInfo(), WakuMetadataCodec
|
|
|
|
|
)
|
|
|
|
|
require:
|
|
|
|
|
connOpt.isSome
|
|
|
|
|
|
|
|
|
|
let response = await node2.wakuMetadata.request(connOpt.get())
|
|
|
|
|
|
|
|
|
|
## Then: Response contains configured shards even without relay subscriptions
|
|
|
|
|
require:
|
|
|
|
|
response.isOk()
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
response.get().clusterId.get() == clusterId
|
|
|
|
|
response.get().shards == @[uint32(0), uint32(1)]
|
|
|
|
|
|
|
|
|
|
await allFutures([node1.stop(), node2.stop()])
|