2023-12-01 16:20:52 +00:00
|
|
|
# Fluffy
|
2024-01-12 21:06:19 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-07-09 11:34:16 +00:00
|
|
|
# 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
|
2022-06-24 13:35:31 +00:00
|
|
|
std/[algorithm, sequtils],
|
2024-02-28 17:31:45 +00:00
|
|
|
chronos,
|
|
|
|
testutils/unittests,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2024-02-28 17:31:45 +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-11-08 17:31:45 +00:00
|
|
|
../network/wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2023-12-01 16:20:52 +00:00
|
|
|
../database/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
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
proc toContentId(contentKey: ByteList): results.Opt[ContentId] =
|
2022-05-16 11:17:42 +00:00
|
|
|
# Note: Returning sha256 digest as content id here. This content key to
|
|
|
|
# content id derivation is different for the different content networks
|
|
|
|
# and their content types.
|
2022-04-06 11:47:23 +00:00
|
|
|
let idHash = sha256.digest(contentKey.asSeq())
|
2022-11-08 17:31:45 +00:00
|
|
|
ok(readUintBE[256](idHash.data))
|
2022-04-06 11:47:23 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
proc initPortalProtocol(
|
2022-07-04 07:38:02 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2022-05-16 11:17:42 +00:00
|
|
|
privKey: PrivateKey,
|
|
|
|
address: Address,
|
2024-02-28 17:31:45 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
): PortalProtocol =
|
2022-05-16 11:17:42 +00:00
|
|
|
let
|
|
|
|
d = initDiscoveryNode(rng, privKey, address, bootstrapRecords)
|
|
|
|
db = ContentDB.new("", uint32.high, inMemory = true)
|
2022-08-17 07:32:06 +00:00
|
|
|
manager = StreamManager.new(d)
|
2023-09-04 10:21:01 +00:00
|
|
|
q = newAsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
2022-08-17 07:32:06 +00:00
|
|
|
stream = manager.registerNewStream(q)
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
proto = PortalProtocol.new(
|
2024-02-28 17:31:45 +00:00
|
|
|
d,
|
|
|
|
protocolId,
|
|
|
|
toContentId,
|
|
|
|
createGetHandler(db),
|
|
|
|
stream,
|
|
|
|
bootstrapRecords = bootstrapRecords,
|
|
|
|
)
|
2022-05-16 11:17:42 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
proto.dbPut = createStoreHandler(db, defaultRadiusConfig, proto)
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
return proto
|
|
|
|
|
|
|
|
proc stopPortalProtocol(proto: PortalProtocol) {.async.} =
|
|
|
|
proto.stop()
|
|
|
|
await proto.baseProtocol.closeWait()
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc defaultTestSetup(rng: ref HmacDrbgContext): (PortalProtocol, PortalProtocol) =
|
2021-08-20 14:02:29 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
proto1 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
proto2 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20303))
|
2021-08-20 14:02:29 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
(proto1, proto2)
|
2021-08-20 14:02:29 +00:00
|
|
|
|
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":
|
2022-05-16 11:17:42 +00:00
|
|
|
let (proto1, proto2) = defaultTestSetup(rng)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
let pong = await proto1.ping(proto2.localNode)
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let customPayload = ByteList(SSZ.encode(CustomPayload(dataRadius: UInt256.high())))
|
2021-10-13 19:35:54 +00:00
|
|
|
|
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
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await proto1.stopPortalProtocol()
|
|
|
|
await proto2.stopPortalProtocol()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-12-08 10:54:22 +00:00
|
|
|
asyncTest "FindNodes/Nodes":
|
2022-05-16 11:17:42 +00:00
|
|
|
let (proto1, proto2) = defaultTestSetup(rng)
|
2022-01-06 08:06:05 +00:00
|
|
|
|
2021-07-09 11:34:16 +00:00
|
|
|
block: # Find itself
|
2024-02-28 17:31:45 +00:00
|
|
|
let nodes =
|
|
|
|
await proto1.findNodesImpl(proto2.localNode, List[uint16, 256](@[0'u16]))
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
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.
|
2024-02-28 17:31:45 +00:00
|
|
|
let nodes = await proto1.findNodesImpl(proto2.localNode, List[uint16, 256](@[]))
|
2021-07-09 11:34:16 +00:00
|
|
|
|
|
|
|
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.
|
2022-05-16 11:17:42 +00:00
|
|
|
check (await proto1.baseProtocol.ping(proto2.localNode)).isOk()
|
|
|
|
check (await proto2.baseProtocol.ping(proto1.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.
|
2022-05-16 11:17:42 +00:00
|
|
|
proto2.start()
|
2021-07-30 19:19:03 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
let distance = logDistance(proto1.localNode.id, proto2.localNode.id)
|
2024-02-28 17:31:45 +00:00
|
|
|
let nodes =
|
|
|
|
await proto1.findNodesImpl(proto2.localNode, List[uint16, 256](@[distance]))
|
2021-07-13 13:15:33 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
nodes.isOk()
|
|
|
|
nodes.get().total == 1'u8
|
|
|
|
nodes.get().enrs.len() == 1
|
2022-01-06 08:06:05 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await proto1.stopPortalProtocol()
|
|
|
|
await proto2.stopPortalProtocol()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-10-13 19:35:54 +00:00
|
|
|
asyncTest "FindContent/Content - send enrs":
|
2022-05-16 11:17:42 +00:00
|
|
|
let (proto1, proto2) = defaultTestSetup(rng)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
# ping in one direction to add, ping in the other to update as seen.
|
2022-05-16 11:17:42 +00:00
|
|
|
check (await proto1.baseProtocol.ping(proto2.localNode)).isOk()
|
|
|
|
check (await proto2.baseProtocol.ping(proto1.localNode)).isOk()
|
2021-09-23 12:26:41 +00:00
|
|
|
|
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-05-16 11:17:42 +00:00
|
|
|
let content = await proto1.findContentImpl(proto2.localNode, contentKey)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
2021-10-13 19:35:54 +00:00
|
|
|
content.isOk()
|
|
|
|
content.get().enrs.len() == 1
|
2021-09-23 12:26:41 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await proto1.stopPortalProtocol()
|
|
|
|
await proto2.stopPortalProtocol()
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
asyncTest "Offer/Accept":
|
2022-05-16 11:17:42 +00:00
|
|
|
let (proto1, proto2) = defaultTestSetup(rng)
|
|
|
|
let contentKeys = ContentKeysList(@[ByteList(@[byte 0x01, 0x02, 0x03])])
|
2021-09-23 12:26:41 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let accept = await proto1.offerImpl(proto2.baseProtocol.localNode, contentKeys)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
accept.isOk()
|
|
|
|
accept.get().connectionId.len == 2
|
|
|
|
accept.get().contentKeys.len == contentKeys.len
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await proto1.stopPortalProtocol()
|
|
|
|
await proto2.stopPortalProtocol()
|
2021-09-23 12:26:41 +00:00
|
|
|
|
2022-06-24 13:35:31 +00:00
|
|
|
asyncTest "Offer/Accept/Stream":
|
|
|
|
let (proto1, proto2) = defaultTestSetup(rng)
|
2023-08-24 16:19:29 +00:00
|
|
|
var content: seq[ContentKV]
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< contentKeysLimit:
|
|
|
|
let contentKV =
|
|
|
|
ContentKV(contentKey: ByteList(@[byte i]), content: repeat(byte i, 5000))
|
2023-08-24 16:19:29 +00:00
|
|
|
content.add(contentKV)
|
2022-06-24 13:35:31 +00:00
|
|
|
|
|
|
|
let res = await proto1.offer(proto2.baseProtocol.localNode, content)
|
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
check res.isOk()
|
|
|
|
|
2023-09-04 10:21:01 +00:00
|
|
|
let (srcNodeId, contentKeys, contentItems) =
|
2024-02-28 17:31:45 +00:00
|
|
|
await proto2.stream.contentQueue.popFirst()
|
2022-07-11 14:29:16 +00:00
|
|
|
|
|
|
|
check contentItems.len() == content.len()
|
2022-06-24 13:35:31 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
for i, contentItem in contentItems:
|
2023-08-24 16:19:29 +00:00
|
|
|
let contentKV = content[i]
|
2022-06-24 13:35:31 +00:00
|
|
|
check:
|
2023-08-24 16:19:29 +00:00
|
|
|
contentItem == contentKV.content
|
|
|
|
contentKeys[i] == contentKV.contentKey
|
2022-06-24 13:35:31 +00:00
|
|
|
|
|
|
|
await proto1.stopPortalProtocol()
|
|
|
|
await proto2.stopPortalProtocol()
|
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
asyncTest "Correctly mark node as seen after request":
|
2022-05-16 11:17:42 +00:00
|
|
|
let (proto1, proto2) = defaultTestSetup(rng)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let initialNeighbours = proto1.neighbours(proto1.localNode.id, seenOnly = false)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(initialNeighbours) == 0
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
discard proto1.addNode(proto2.localNode)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let allNeighboursAfterAdd = proto1.neighbours(proto1.localNode.id, seenOnly = false)
|
|
|
|
let seenNeighboursAfterAdd = proto1.neighbours(proto1.localNode.id, seenOnly = true)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
len(allNeighboursAfterAdd) == 1
|
|
|
|
len(seenNeighboursAfterAdd) == 0
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
let pong = await proto1.ping(proto2.localNode)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let allNeighboursAfterPing =
|
|
|
|
proto1.neighbours(proto1.localNode.id, seenOnly = false)
|
|
|
|
let seenNeighboursAfterPing =
|
|
|
|
proto1.neighbours(proto1.localNode.id, seenOnly = true)
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
pong.isOk()
|
|
|
|
len(allNeighboursAfterPing) == 1
|
|
|
|
len(seenNeighboursAfterPing) == 1
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await proto1.stopPortalProtocol()
|
|
|
|
await proto2.stopPortalProtocol()
|
2021-09-23 12:26:41 +00:00
|
|
|
|
|
|
|
asyncTest "Lookup nodes":
|
2022-05-16 11:17:42 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
node1 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
node3 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20304))
|
2021-09-07 12:22:25 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
# Make node1 know about node2, and node2 about node3
|
|
|
|
# node1 will then do a lookup for node3
|
|
|
|
check node1.addNode(node2.localNode) == Added
|
|
|
|
check node2.addNode(node3.localNode) == Added
|
2021-09-07 12:22:25 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
check (await node2.ping(node3.localNode)).isOk()
|
2021-09-07 12:22:25 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
let lookupResult = await node1.lookup(node3.localNode.id)
|
2021-09-07 12:22:25 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
check:
|
|
|
|
# Result should contain node3 as it is in the routing table of node2
|
|
|
|
lookupResult.contains(node3.localNode)
|
2021-09-07 12:22:25 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await node1.stopPortalProtocol()
|
|
|
|
await node2.stopPortalProtocol()
|
|
|
|
await node3.stopPortalProtocol()
|
2021-09-07 12:22:25 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
asyncTest "Lookup content - nodes interested":
|
2022-04-06 11:47:23 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
node1 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20303))
|
|
|
|
node3 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20304))
|
2022-04-06 11:47:23 +00:00
|
|
|
|
|
|
|
content = @[byte 1, 2]
|
|
|
|
contentList = List[byte, 2048].init(content)
|
|
|
|
contentId = readUintBE[256](sha256.digest(content).data)
|
2022-04-11 17:42:38 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
# Store the content on node3
|
2022-11-08 17:31:45 +00:00
|
|
|
node3.storeContent(contentList, contentId, content)
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
# Make node1 know about node2, and node2 about node3
|
|
|
|
check node1.addNode(node2.localNode) == Added
|
|
|
|
check node2.addNode(node3.localNode) == Added
|
2022-04-06 11:47:23 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
# node1 needs to know the radius of the nodes to determine if they are
|
|
|
|
# interested in content, so a ping is done.
|
|
|
|
check (await node1.ping(node2.localNode)).isOk()
|
|
|
|
check (await node2.ping(node3.localNode)).isOk()
|
2022-04-06 11:47:23 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
let lookupResult = await node1.contentLookup(contentList, contentId)
|
2022-04-06 11:47:23 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
lookupResult.isSome()
|
|
|
|
|
|
|
|
let res = lookupResult.unsafeGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
res.content == content
|
|
|
|
res.nodesInterestedInContent.contains(node2.localNode)
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await node1.stopPortalProtocol()
|
|
|
|
await node2.stopPortalProtocol()
|
|
|
|
await node3.stopPortalProtocol()
|
2022-04-06 11:47:23 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
asyncTest "Valid Bootstrap Node":
|
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
node1 = initPortalProtocol(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initPortalProtocol(
|
|
|
|
rng,
|
|
|
|
PrivateKey.random(rng[]),
|
|
|
|
localAddress(20303),
|
|
|
|
bootstrapRecords = [node1.localNode.record],
|
|
|
|
)
|
2022-01-06 08:06:05 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
node1.start()
|
|
|
|
node2.start()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
check node2.neighbours(node2.localNode.id).len == 1
|
2021-07-13 13:15:33 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
await node1.stopPortalProtocol()
|
|
|
|
await node2.stopPortalProtocol()
|
2021-07-09 11:34:16 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
asyncTest "Invalid Bootstrap Node":
|
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
node1 = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20302))
|
|
|
|
node2 = initPortalProtocol(
|
|
|
|
rng,
|
|
|
|
PrivateKey.random(rng[]),
|
|
|
|
localAddress(20303),
|
|
|
|
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
|
2022-05-16 11:17:42 +00:00
|
|
|
node2.seedTable()
|
|
|
|
check node2.neighbours(node2.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
|
2022-05-16 11:17:42 +00:00
|
|
|
await node2.revalidateNode(node1.localNode)
|
2021-09-22 09:28:04 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
check node2.neighbours(node2.localNode.id).len == 0
|
2021-09-22 09:28:04 +00:00
|
|
|
|
2021-09-23 12:26:41 +00:00
|
|
|
await node1.closeWait()
|
2022-05-16 11:17:42 +00:00
|
|
|
await node2.stopPortalProtocol()
|
2022-05-12 16:04:37 +00:00
|
|
|
|
|
|
|
asyncTest "Adjusting radius after hitting full database":
|
2023-11-16 14:27:30 +00:00
|
|
|
# TODO: This test is extremely breakable when changing
|
|
|
|
# `contentDeletionFraction` and/or the used test values.
|
|
|
|
# Need to rework either this test, or the pruning mechanism, or probably
|
|
|
|
# both.
|
2022-05-12 16:04:37 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
node1 = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20303))
|
2022-05-12 16:04:37 +00:00
|
|
|
|
2023-11-16 14:27:30 +00:00
|
|
|
dbLimit = 400_000'u32
|
2022-05-12 16:04:37 +00:00
|
|
|
db = ContentDB.new("", dbLimit, inMemory = true)
|
2022-08-17 07:32:06 +00:00
|
|
|
m = StreamManager.new(node1)
|
2023-09-04 10:21:01 +00:00
|
|
|
q = newAsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
2022-08-17 07:32:06 +00:00
|
|
|
stream = m.registerNewStream(q)
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proto1 =
|
|
|
|
PortalProtocol.new(node1, protocolId, toContentId, createGetHandler(db), stream)
|
2022-11-08 17:31:45 +00:00
|
|
|
|
|
|
|
proto1.dbPut = createStoreHandler(db, defaultRadiusConfig, proto1)
|
2022-05-12 16:04:37 +00:00
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
let item = genByteSeq(10_000)
|
2022-05-12 16:04:37 +00:00
|
|
|
var distances: seq[UInt256] = @[]
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< 40:
|
2022-11-08 17:31:45 +00:00
|
|
|
proto1.storeContent(ByteList.init(@[uint8(i)]), u256(i), item)
|
2022-05-12 16:04:37 +00:00
|
|
|
distances.add(u256(i) xor proto1.localNode.id)
|
2022-05-16 11:17:42 +00:00
|
|
|
|
2022-05-12 16:04:37 +00:00
|
|
|
distances.sort(order = SortOrder.Descending)
|
|
|
|
|
2022-05-16 11:17:42 +00:00
|
|
|
# With the selected db limit of 100_000 bytes and added elements of 10_000
|
|
|
|
# bytes each, the two furthest elements should be prined, i.e index 0 and 1.
|
|
|
|
# Index 2 should be still be in database and its distance should be <=
|
|
|
|
# updated radius
|
2022-05-12 16:04:37 +00:00
|
|
|
check:
|
|
|
|
db.get((distances[0] xor proto1.localNode.id)).isNone()
|
|
|
|
db.get((distances[1] xor proto1.localNode.id)).isNone()
|
2023-11-16 14:27:30 +00:00
|
|
|
db.get((distances[2] xor proto1.localNode.id)).isNone()
|
|
|
|
db.get((distances[3] xor proto1.localNode.id)).isSome()
|
2022-05-16 11:17:42 +00:00
|
|
|
# The radius has been updated and is lower than the maximum start value.
|
2022-05-12 16:04:37 +00:00
|
|
|
proto1.dataRadius < UInt256.high
|
2022-05-16 11:17:42 +00:00
|
|
|
# Yet higher than or equal to the furthest non deleted element.
|
2023-11-16 14:27:30 +00:00
|
|
|
proto1.dataRadius >= distances[3]
|
2022-05-12 16:04:37 +00:00
|
|
|
|
|
|
|
proto1.stop()
|
|
|
|
await node1.closeWait()
|