2021-07-09 11:34:16 +00:00
|
|
|
# Nimbus - Portal Network
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2021-10-22 19:36:53 +00:00
|
|
|
chronos, testutils/unittests, stew/shims/net,
|
2021-09-03 08:57:19 +00:00
|
|
|
eth/keys, eth/p2p/discoveryv5/routing_table, nimcrypto/[hash, sha2],
|
2021-07-09 11:34:16 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
2022-01-14 15:07:14 +00:00
|
|
|
../network/wire/[portal_protocol, portal_stream],
|
2022-01-06 08:06:05 +00:00
|
|
|
../content_db,
|
2021-07-15 13:12:33 +00:00
|
|
|
./test_helpers
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-10-20 20:31:05 +00:00
|
|
|
const protocolId = [byte 0x50, 0x00]
|
2021-09-22 15:07:14 +00:00
|
|
|
|
2021-08-20 14:02:29 +00:00
|
|
|
type Default2NodeTest = ref object
|
|
|
|
node1: discv5_protocol.Protocol
|
|
|
|
node2: discv5_protocol.Protocol
|
|
|
|
proto1: PortalProtocol
|
|
|
|
proto2: PortalProtocol
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
proc testHandler(contentKey: ByteList): Option[ContentId] =
|
|
|
|
# Note: Returning a static content id here, as in practice this depends
|
2021-09-24 09:22:07 +00:00
|
|
|
# on the content key to content id derivation, which is different for the
|
|
|
|
# different content networks. And we want these tests to be independent from
|
2022-01-06 08:06:05 +00:00
|
|
|
# that.
|
|
|
|
let idHash = sha256.digest("test")
|
|
|
|
some(readUintBE[256](idHash.data))
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-08-20 14:02:29 +00:00
|
|
|
proc defaultTestCase(rng: ref BrHmacDrbgContext): Default2NodeTest =
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
db1 = ContentDB.new("", inMemory = true)
|
|
|
|
db2 = ContentDB.new("", inMemory = true)
|
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
proto1 = PortalProtocol.new(node1, protocolId, db1, testHandler)
|
|
|
|
proto2 = PortalProtocol.new(node2, protocolId, db2, testHandler)
|
2021-08-20 14:02:29 +00:00
|
|
|
|
|
|
|
Default2NodeTest(node1: node1, node2: node2, proto1: proto1, proto2: proto2)
|
|
|
|
|
|
|
|
proc stopTest(test: Default2NodeTest) {.async.} =
|
|
|
|
test.proto1.stop()
|
|
|
|
test.proto2.stop()
|
|
|
|
await test.node1.closeWait()
|
|
|
|
await test.node2.closeWait()
|
|
|
|
|
2021-09-22 15:07:14 +00:00
|
|
|
procSuite "Portal Wire Protocol Tests":
|
2021-07-09 11:34:16 +00:00
|
|
|
let rng = newRng()
|
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
asyncTest "Ping/Pong":
|
2021-08-20 14:02:29 +00:00
|
|
|
let test = defaultTestCase(rng)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-09-24 09:22:07 +00:00
|
|
|
let pong = await test.proto1.ping(test.proto2.localNode)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-10-13 19:35:54 +00:00
|
|
|
let customPayload = ByteList(SSZ.encode(CustomPayload(dataRadius: UInt256.high())))
|
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
check:
|
|
|
|
pong.isOk()
|
|
|
|
pong.get().enrSeq == 1'u64
|
2021-10-13 19:35:54 +00:00
|
|
|
pong.get().customPayload == customPayload
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-08-20 14:02:29 +00:00
|
|
|
await test.stopTest()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-12-08 10:54:22 +00:00
|
|
|
asyncTest "FindNodes/Nodes":
|
2021-08-20 14:02:29 +00:00
|
|
|
let test = defaultTestCase(rng)
|
2022-01-06 08:06:05 +00:00
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
block: # Find itself
|
2022-03-19 07:54:42 +00:00
|
|
|
let nodes = await test.proto1.findNodesImpl(test.proto2.localNode,
|
2021-07-09 11:34:16 +00:00
|
|
|
List[uint16, 256](@[0'u16]))
|
|
|
|
|
|
|
|
check:
|
|
|
|
nodes.isOk()
|
|
|
|
nodes.get().total == 1'u8
|
|
|
|
nodes.get().enrs.len() == 1
|
|
|
|
|
2021-07-30 19:19:03 +00:00
|
|
|
block: # Find nothing: this should result in nothing as we haven't started
|
|
|
|
# the seeding of the portal protocol routing table yet.
|
2022-03-19 07:54:42 +00:00
|
|
|
let nodes = await test.proto1.findNodesImpl(test.proto2.localNode,
|
2021-07-09 11:34:16 +00:00
|
|
|
List[uint16, 256](@[]))
|
|
|
|
|
|
|
|
check:
|
|
|
|
nodes.isOk()
|
|
|
|
nodes.get().total == 1'u8
|
|
|
|
nodes.get().enrs.len() == 0
|
|
|
|
|
|
|
|
block: # Find for distance
|
2021-07-30 19:19:03 +00:00
|
|
|
# ping in one direction to add, ping in the other to update as seen,
|
|
|
|
# adding the node in the discovery v5 routing table. Could also launch
|
|
|
|
# with bootstrap node instead.
|
2021-08-20 14:02:29 +00:00
|
|
|
check (await test.node1.ping(test.node2.localNode)).isOk()
|
|
|
|
check (await test.node2.ping(test.node1.localNode)).isOk()
|
2021-07-13 13:15:33 +00:00
|
|
|
|
2021-07-30 19:19:03 +00:00
|
|
|
# Start the portal protocol to seed nodes from the discoveryv5 routing
|
|
|
|
# table.
|
2021-08-20 14:02:29 +00:00
|
|
|
test.proto2.start()
|
2021-07-30 19:19:03 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
let distance = logDistance(test.node1.localNode.id, test.node2.localNode.id)
|
2022-03-19 07:54:42 +00:00
|
|
|
let nodes = await test.proto1.findNodesImpl(test.proto2.localNode,
|
2021-07-13 13:15:33 +00:00
|
|
|
List[uint16, 256](@[distance]))
|
|
|
|
|
|
|
|
check:
|
|
|
|
nodes.isOk()
|
|
|
|
nodes.get().total == 1'u8
|
|
|
|
nodes.get().enrs.len() == 1
|
2022-01-06 08:06:05 +00:00
|
|
|
|
2021-08-20 14:02:29 +00:00
|
|
|
await test.stopTest()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-10-13 19:35:54 +00:00
|
|
|
asyncTest "FindContent/Content - send enrs":
|
2021-09-23 12:26:41 +00:00
|
|
|
let test = defaultTestCase(rng)
|
|
|
|
|
|
|
|
# ping in one direction to add, ping in the other to update as seen.
|
|
|
|
check (await test.node1.ping(test.node2.localNode)).isOk()
|
|
|
|
check (await test.node2.ping(test.node1.localNode)).isOk()
|
|
|
|
|
|
|
|
# Start the portal protocol to seed nodes from the discoveryv5 routing
|
|
|
|
# table.
|
|
|
|
test.proto2.start()
|
|
|
|
|
2021-10-22 15:49:23 +00:00
|
|
|
let contentKey = ByteList.init(@[1'u8])
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
# content does not exist so this should provide us with the closest nodes
|
|
|
|
# to the content, which is the only node in the routing table.
|
2022-01-14 15:07:14 +00:00
|
|
|
let content = await test.proto1.findContentImpl(test.proto2.localNode,
|
2021-09-23 12:26:41 +00:00
|
|
|
contentKey)
|
|
|
|
|
|
|
|
check:
|
2021-10-13 19:35:54 +00:00
|
|
|
content.isOk()
|
|
|
|
content.get().enrs.len() == 1
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
await test.stopTest()
|
|
|
|
|
|
|
|
asyncTest "Offer/Accept":
|
|
|
|
let test = defaultTestCase(rng)
|
|
|
|
let contentKeys = ContentKeysList(List(@[ByteList(@[byte 0x01, 0x02, 0x03])]))
|
|
|
|
|
2022-01-14 15:07:14 +00:00
|
|
|
let accept = await test.proto1.offerImpl(
|
2021-09-23 12:26:41 +00:00
|
|
|
test.proto2.baseProtocol.localNode, contentKeys)
|
|
|
|
|
|
|
|
check:
|
|
|
|
accept.isOk()
|
|
|
|
accept.get().connectionId.len == 2
|
|
|
|
accept.get().contentKeys.len == contentKeys.len
|
|
|
|
|
|
|
|
await test.stopTest()
|
|
|
|
|
|
|
|
asyncTest "Correctly mark node as seen after request":
|
|
|
|
let test = defaultTestCase(rng)
|
|
|
|
|
2021-09-24 09:22:07 +00:00
|
|
|
let initialNeighbours = test.proto1.neighbours(test.proto1.localNode.id,
|
|
|
|
seenOnly = false)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(initialNeighbours) == 0
|
|
|
|
|
|
|
|
discard test.proto1.addNode(test.proto2.baseProtocol.localNode)
|
|
|
|
|
2021-09-24 09:22:07 +00:00
|
|
|
let allNeighboursAfterAdd = test.proto1.neighbours(
|
|
|
|
test.proto1.localNode.id, seenOnly = false)
|
|
|
|
let seenNeighboursAfterAdd = test.proto1.neighbours(
|
|
|
|
test.proto1.localNode.id, seenOnly = true)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(allNeighboursAfterAdd) == 1
|
|
|
|
len(seenNeighboursAfterAdd) == 0
|
|
|
|
|
|
|
|
let pong = await test.proto1.ping(test.proto2.baseProtocol.localNode)
|
|
|
|
|
2021-09-24 09:22:07 +00:00
|
|
|
let allNeighboursAfterPing = test.proto1.neighbours(
|
|
|
|
test.proto1.localNode.id, seenOnly = false)
|
|
|
|
let seenNeighboursAfterPing = test.proto1.neighbours(
|
|
|
|
test.proto1.localNode.id, seenOnly = true)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
pong.isOk()
|
|
|
|
len(allNeighboursAfterPing) == 1
|
|
|
|
len(seenNeighboursAfterPing) == 1
|
|
|
|
|
|
|
|
await test.stopTest()
|
|
|
|
|
|
|
|
asyncTest "Lookup nodes":
|
2021-09-07 12:22:25 +00:00
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
node3 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20304))
|
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
db1 = ContentDB.new("", inMemory = true)
|
|
|
|
db2 = ContentDB.new("", inMemory = true)
|
|
|
|
db3 = ContentDB.new("", inMemory = true)
|
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
proto1 = PortalProtocol.new(node1, protocolId, db1, testHandler)
|
|
|
|
proto2 = PortalProtocol.new(node2, protocolId, db2, testHandler)
|
|
|
|
proto3 = PortalProtocol.new(node3, protocolId, db3, testHandler)
|
2021-09-07 12:22:25 +00:00
|
|
|
|
|
|
|
# Node1 knows about Node2, and Node2 knows about Node3 which hold all content
|
|
|
|
check proto1.addNode(node2.localNode) == Added
|
|
|
|
check proto2.addNode(node3.localNode) == Added
|
|
|
|
|
|
|
|
check (await proto2.ping(node3.localNode)).isOk()
|
|
|
|
|
|
|
|
let lookuResult = await proto1.lookup(node3.localNode.id)
|
|
|
|
|
|
|
|
check:
|
|
|
|
# returned result should contain node3 as it is in node2 routing table
|
|
|
|
lookuResult.contains(node3.localNode)
|
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
await node3.closeWait()
|
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
asyncTest "Valid Bootstrap Node":
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
2021-09-07 12:22:25 +00:00
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
db1 = ContentDB.new("", inMemory = true)
|
|
|
|
db2 = ContentDB.new("", inMemory = true)
|
|
|
|
|
2022-02-11 13:43:10 +00:00
|
|
|
proto1 = PortalProtocol.new(node1, protocolId, db1, testHandler)
|
|
|
|
proto2 = PortalProtocol.new(node2, protocolId, db2, testHandler,
|
2021-09-23 12:26:41 +00:00
|
|
|
bootstrapRecords = [node1.localNode.record])
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
proto1.start()
|
|
|
|
proto2.start()
|
2021-07-13 13:15:33 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
check proto2.neighbours(proto2.localNode.id).len == 1
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
proto1.stop()
|
|
|
|
proto2.stop()
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
asyncTest "Invalid Bootstrap Node":
|
|
|
|
let
|
|
|
|
node1 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initDiscoveryNode(
|
|
|
|
rng, PrivateKey.random(rng[]), localAddress(20303))
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2022-01-06 08:06:05 +00:00
|
|
|
db = ContentDB.new("", inMemory = true)
|
2021-09-23 12:26:41 +00:00
|
|
|
# No portal protocol for node1, hence an invalid bootstrap node
|
2022-02-11 13:43:10 +00:00
|
|
|
proto2 = PortalProtocol.new(node2, protocolId, db, testHandler,
|
2021-09-23 12:26:41 +00:00
|
|
|
bootstrapRecords = [node1.localNode.record])
|
2021-08-20 14:02:29 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
# seedTable to add node1 to the routing table
|
|
|
|
proto2.seedTable()
|
|
|
|
check proto2.neighbours(proto2.localNode.id).len == 1
|
2021-09-22 09:28:04 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
# This should fail and drop node1 from the routing table
|
|
|
|
await proto2.revalidateNode(node1.localNode)
|
2021-09-22 09:28:04 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
check proto2.neighbours(proto2.localNode.id).len == 0
|
2021-09-22 09:28:04 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
proto2.stop()
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|