2026-04-25 03:37:42 +03:00
|
|
|
import std/[sequtils, tables]
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
|
|
2026-02-19 15:59:15 +11:00
|
|
|
import pkg/storage/rng
|
|
|
|
|
import pkg/storage/chunker
|
|
|
|
|
import pkg/storage/blocktype as bt
|
|
|
|
|
import pkg/storage/blockexchange
|
2026-04-25 03:37:42 +03:00
|
|
|
import pkg/storage/blockexchange/protocol/wantblocks
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2024-01-29 21:03:51 +01:00
|
|
|
import ../../asynctest
|
2021-04-08 09:45:41 +02:00
|
|
|
import ../examples
|
2024-01-29 21:03:51 +01:00
|
|
|
import ../helpers
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2023-06-22 12:01:21 -06:00
|
|
|
asyncchecksuite "Network - Handlers":
|
2021-02-25 18:23:22 -06:00
|
|
|
let
|
|
|
|
|
rng = Rng.instance()
|
2022-01-12 18:42:18 -06:00
|
|
|
seckey = PrivateKey.random(rng[]).tryGet()
|
2023-03-10 08:02:54 +01:00
|
|
|
peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet()
|
2022-01-10 09:32:56 -06:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
var
|
2021-08-30 13:25:20 -06:00
|
|
|
network: BlockExcNetwork
|
2021-02-25 18:23:22 -06:00
|
|
|
networkPeer: NetworkPeer
|
|
|
|
|
buffer: BufferStream
|
2022-01-10 09:32:56 -06:00
|
|
|
blocks: seq[bt.Block]
|
2021-02-25 18:23:22 -06:00
|
|
|
done: Future[void]
|
|
|
|
|
|
2025-03-13 08:33:15 -06:00
|
|
|
proc getConn(): Future[Connection] {.async: (raises: [CancelledError]).} =
|
2021-02-25 18:23:22 -06:00
|
|
|
return Connection(buffer)
|
|
|
|
|
|
|
|
|
|
setup:
|
2022-01-10 09:32:56 -06:00
|
|
|
while true:
|
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
|
if chunk.len <= 0:
|
|
|
|
|
break
|
|
|
|
|
|
2022-03-18 13:50:53 -06:00
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 09:32:56 -06:00
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
done = newFuture[void]()
|
2021-10-29 13:30:52 -06:00
|
|
|
buffer = BufferStream.new()
|
2021-08-30 13:25:20 -06:00
|
|
|
network = BlockExcNetwork.new(switch = newStandardSwitch(), connProvider = getConn)
|
2025-11-13 07:47:02 +02:00
|
|
|
await network.handlePeerJoined(peerId)
|
2021-02-25 18:23:22 -06:00
|
|
|
networkPeer = network.peers[peerId]
|
|
|
|
|
discard await networkPeer.connect()
|
|
|
|
|
|
|
|
|
|
test "Want List handler":
|
2026-04-25 03:37:42 +03:00
|
|
|
let treeCid = Cid.example
|
|
|
|
|
|
2025-03-13 08:33:15 -06:00
|
|
|
proc wantListHandler(peer: PeerId, wantList: WantList) {.async: (raises: []).} =
|
2021-02-25 18:23:22 -06:00
|
|
|
check wantList.entries.len == 4
|
|
|
|
|
|
2026-04-25 03:37:42 +03:00
|
|
|
for entry in wantList.entries:
|
|
|
|
|
check entry.address.treeCid == treeCid
|
2022-11-15 09:46:21 -06:00
|
|
|
check entry.wantType == WantType.WantHave
|
2021-02-25 18:23:22 -06:00
|
|
|
check entry.priority == 1
|
|
|
|
|
check entry.cancel == true
|
|
|
|
|
check entry.sendDontHave == true
|
|
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
|
|
network.handlers.onWantList = wantListHandler
|
|
|
|
|
|
|
|
|
|
let wantList =
|
2026-04-25 03:37:42 +03:00
|
|
|
makeWantList(treeCid, blocks.len, 1, true, WantType.WantHave, true, true)
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
let msg = Message(wantlist: wantList)
|
2026-04-25 03:37:42 +03:00
|
|
|
await buffer.pushData(frameProtobufMessage(protobufEncode(msg)))
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
|
2021-04-08 09:43:13 +02:00
|
|
|
test "Presence Handler":
|
2026-04-25 03:37:42 +03:00
|
|
|
let
|
|
|
|
|
treeCid = Cid.example
|
|
|
|
|
addresses = (0 ..< blocks.len).mapIt(BlockAddress(treeCid: treeCid, index: it))
|
|
|
|
|
|
2025-03-13 08:33:15 -06:00
|
|
|
proc presenceHandler(
|
|
|
|
|
peer: PeerId, presence: seq[BlockPresence]
|
|
|
|
|
) {.async: (raises: []).} =
|
2026-04-25 03:37:42 +03:00
|
|
|
check presence.len == blocks.len
|
|
|
|
|
for p in presence:
|
|
|
|
|
check p.address.treeCid == treeCid
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
|
|
network.handlers.onPresence = presenceHandler
|
|
|
|
|
|
|
|
|
|
let msg = Message(
|
2022-11-15 09:46:21 -06:00
|
|
|
blockPresences:
|
2026-04-25 03:37:42 +03:00
|
|
|
addresses.mapIt(BlockPresence(address: it, kind: BlockPresenceType.HaveRange))
|
2021-02-25 18:23:22 -06:00
|
|
|
)
|
2026-04-25 03:37:42 +03:00
|
|
|
await buffer.pushData(frameProtobufMessage(protobufEncode(msg)))
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
|
2023-06-22 12:01:21 -06:00
|
|
|
asyncchecksuite "Network - Senders":
|
2022-01-10 09:32:56 -06:00
|
|
|
let chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
var
|
|
|
|
|
switch1, switch2: Switch
|
2021-08-30 13:25:20 -06:00
|
|
|
network1, network2: BlockExcNetwork
|
2022-01-10 09:32:56 -06:00
|
|
|
blocks: seq[bt.Block]
|
2021-02-25 18:23:22 -06:00
|
|
|
done: Future[void]
|
|
|
|
|
|
|
|
|
|
setup:
|
2022-01-10 09:32:56 -06:00
|
|
|
while true:
|
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
|
if chunk.len <= 0:
|
|
|
|
|
break
|
|
|
|
|
|
2022-03-18 13:50:53 -06:00
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 09:32:56 -06:00
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
done = newFuture[void]()
|
|
|
|
|
switch1 = newStandardSwitch()
|
|
|
|
|
switch2 = newStandardSwitch()
|
2021-08-30 13:25:20 -06:00
|
|
|
network1 = BlockExcNetwork.new(switch = switch1)
|
2021-02-25 18:23:22 -06:00
|
|
|
switch1.mount(network1)
|
|
|
|
|
|
2021-08-30 13:25:20 -06:00
|
|
|
network2 = BlockExcNetwork.new(switch = switch2)
|
2021-02-25 18:23:22 -06:00
|
|
|
switch2.mount(network2)
|
|
|
|
|
|
2022-11-02 12:40:28 -05:00
|
|
|
await switch1.start()
|
|
|
|
|
await switch2.start()
|
|
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
await switch1.connect(switch2.peerInfo.peerId, switch2.peerInfo.addrs)
|
|
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
|
await allFuturesThrowing(switch1.stop(), switch2.stop())
|
|
|
|
|
|
2022-07-29 10:19:34 -06:00
|
|
|
test "Send want list":
|
2026-04-25 03:37:42 +03:00
|
|
|
let
|
|
|
|
|
treeCid = Cid.example
|
|
|
|
|
addresses = (0 ..< blocks.len).mapIt(BlockAddress(treeCid: treeCid, index: it))
|
|
|
|
|
|
2025-03-13 08:33:15 -06:00
|
|
|
proc wantListHandler(peer: PeerId, wantList: WantList) {.async: (raises: []).} =
|
2021-02-25 18:23:22 -06:00
|
|
|
check wantList.entries.len == 4
|
|
|
|
|
|
2026-04-25 03:37:42 +03:00
|
|
|
for entry in wantList.entries:
|
|
|
|
|
check entry.address.treeCid == treeCid
|
2022-11-15 09:46:21 -06:00
|
|
|
check entry.wantType == WantType.WantHave
|
2021-02-25 18:23:22 -06:00
|
|
|
check entry.priority == 1
|
|
|
|
|
check entry.cancel == true
|
|
|
|
|
check entry.sendDontHave == true
|
|
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
|
|
network2.handlers.onWantList = wantListHandler
|
2022-07-29 10:19:34 -06:00
|
|
|
await network1.sendWantList(
|
2026-04-25 03:37:42 +03:00
|
|
|
switch2.peerInfo.peerId, addresses, 1, true, WantType.WantHave, true, true
|
2025-01-21 21:54:46 +01:00
|
|
|
)
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
|
2022-07-29 10:19:34 -06:00
|
|
|
test "send presence":
|
2026-04-25 03:37:42 +03:00
|
|
|
let
|
|
|
|
|
treeCid = Cid.example
|
|
|
|
|
addresses = (0 ..< blocks.len).mapIt(BlockAddress(treeCid: treeCid, index: it))
|
|
|
|
|
|
2025-03-13 08:33:15 -06:00
|
|
|
proc presenceHandler(
|
|
|
|
|
peer: PeerId, precense: seq[BlockPresence]
|
|
|
|
|
) {.async: (raises: []).} =
|
2026-04-25 03:37:42 +03:00
|
|
|
check precense.len == blocks.len
|
|
|
|
|
for p in precense:
|
|
|
|
|
check p.address.treeCid == treeCid
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
|
|
network2.handlers.onPresence = presenceHandler
|
|
|
|
|
|
2022-07-29 10:19:34 -06:00
|
|
|
await network1.sendBlockPresence(
|
2021-02-25 18:23:22 -06:00
|
|
|
switch2.peerInfo.peerId,
|
2026-04-25 03:37:42 +03:00
|
|
|
addresses.mapIt(BlockPresence(address: it, kind: BlockPresenceType.HaveRange)),
|
2021-02-25 18:23:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
2021-04-08 10:20:35 +02:00
|
|
|
|
2023-06-22 12:01:21 -06:00
|
|
|
asyncchecksuite "Network - Test Limits":
|
2022-07-29 10:19:34 -06:00
|
|
|
var
|
|
|
|
|
switch1, switch2: Switch
|
|
|
|
|
network1, network2: BlockExcNetwork
|
|
|
|
|
done: Future[void]
|
|
|
|
|
|
|
|
|
|
setup:
|
|
|
|
|
done = newFuture[void]()
|
|
|
|
|
switch1 = newStandardSwitch()
|
|
|
|
|
switch2 = newStandardSwitch()
|
|
|
|
|
|
|
|
|
|
network1 = BlockExcNetwork.new(switch = switch1, maxInflight = 0)
|
|
|
|
|
switch1.mount(network1)
|
|
|
|
|
|
|
|
|
|
network2 = BlockExcNetwork.new(switch = switch2)
|
|
|
|
|
switch2.mount(network2)
|
|
|
|
|
|
2022-11-02 12:40:28 -05:00
|
|
|
await switch1.start()
|
|
|
|
|
await switch2.start()
|
|
|
|
|
|
2022-07-29 10:19:34 -06:00
|
|
|
await switch1.connect(switch2.peerInfo.peerId, switch2.peerInfo.addrs)
|
|
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
|
await allFuturesThrowing(switch1.stop(), switch2.stop())
|
|
|
|
|
|
|
|
|
|
test "Concurrent Sends":
|
2026-01-17 06:03:54 +11:00
|
|
|
network2.handlers.onPresence = proc(
|
|
|
|
|
peer: PeerId, presence: seq[BlockPresence]
|
|
|
|
|
): Future[void] {.async: (raises: []).} =
|
2022-07-29 10:19:34 -06:00
|
|
|
check false
|
|
|
|
|
|
2026-01-17 06:03:54 +11:00
|
|
|
let fut = network1.send(switch2.peerInfo.peerId, Message())
|
2022-07-29 10:19:34 -06:00
|
|
|
|
|
|
|
|
await sleepAsync(100.millis)
|
|
|
|
|
check not fut.finished
|