2021-02-26 00:23:22 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/tables
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/rng
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/blockexchange
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
import ../helpers
|
2021-04-08 07:45:41 +00:00
|
|
|
import ../examples
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Network - Handlers":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
|
|
|
rng = Rng.instance()
|
2022-01-13 00:42:18 +00:00
|
|
|
seckey = PrivateKey.random(rng[]).tryGet()
|
2023-03-10 07:02:54 +00:00
|
|
|
peerId = PeerId.init(seckey.getPublicKey().tryGet()).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
var
|
2021-08-30 19:25:20 +00:00
|
|
|
network: BlockExcNetwork
|
2021-02-26 00:23:22 +00:00
|
|
|
networkPeer: NetworkPeer
|
|
|
|
buffer: BufferStream
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks: seq[bt.Block]
|
2021-02-26 00:23:22 +00:00
|
|
|
done: Future[void]
|
|
|
|
|
|
|
|
proc getConn(): Future[Connection] {.async.} =
|
|
|
|
return Connection(buffer)
|
|
|
|
|
|
|
|
setup:
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
done = newFuture[void]()
|
2021-10-29 19:30:52 +00:00
|
|
|
buffer = BufferStream.new()
|
2021-08-30 19:25:20 +00:00
|
|
|
network = BlockExcNetwork.new(
|
2021-02-26 00:23:22 +00:00
|
|
|
switch = newStandardSwitch(),
|
|
|
|
connProvider = getConn)
|
|
|
|
network.setupPeer(peerId)
|
|
|
|
networkPeer = network.peers[peerId]
|
|
|
|
discard await networkPeer.connect()
|
|
|
|
|
|
|
|
test "Want List handler":
|
2023-11-14 12:02:17 +00:00
|
|
|
proc wantListHandler(peer: PeerId, wantList: WantList) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
# check that we got the correct amount of entries
|
|
|
|
check wantList.entries.len == 4
|
|
|
|
|
|
|
|
for b in blocks:
|
2023-11-14 12:02:17 +00:00
|
|
|
check b.address in wantList.entries
|
|
|
|
let entry = wantList.entries[wantList.entries.find(b.address)]
|
2022-11-15 15:46:21 +00:00
|
|
|
check entry.wantType == WantType.WantHave
|
2021-02-26 00:23:22 +00:00
|
|
|
check entry.priority == 1
|
|
|
|
check entry.cancel == true
|
|
|
|
check entry.sendDontHave == true
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network.handlers.onWantList = wantListHandler
|
|
|
|
|
|
|
|
let wantList = makeWantList(
|
|
|
|
blocks.mapIt( it.cid ),
|
2022-11-15 15:46:21 +00:00
|
|
|
1, true, WantType.WantHave,
|
2021-02-26 00:23:22 +00:00
|
|
|
true, true)
|
|
|
|
|
|
|
|
let msg = Message(wantlist: wantList)
|
2023-03-10 07:02:54 +00:00
|
|
|
await buffer.pushData(lenPrefix(protobufEncode(msg)))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
|
|
|
test "Blocks Handler":
|
2023-11-14 12:02:17 +00:00
|
|
|
proc blocksDeliveryHandler(peer: PeerId, blocksDelivery: seq[BlockDelivery]) {.gcsafe, async.} =
|
|
|
|
check blocks == blocksDelivery.mapIt(it.blk)
|
2021-02-26 00:23:22 +00:00
|
|
|
done.complete()
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
network.handlers.onBlocksDelivery = blocksDeliveryHandler
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
let msg = Message(payload: blocks.mapIt(BlockDelivery(blk: it, address: it.address)))
|
2023-03-10 07:02:54 +00:00
|
|
|
await buffer.pushData(lenPrefix(protobufEncode(msg)))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
2021-04-08 07:43:13 +00:00
|
|
|
test "Presence Handler":
|
2022-01-10 15:32:56 +00:00
|
|
|
proc presenceHandler(
|
2023-03-10 07:02:54 +00:00
|
|
|
peer: PeerId,
|
2023-11-14 12:02:17 +00:00
|
|
|
presence: seq[BlockPresence]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
for b in blocks:
|
|
|
|
check:
|
2023-11-14 12:02:17 +00:00
|
|
|
b.address in presence
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network.handlers.onPresence = presenceHandler
|
|
|
|
|
|
|
|
let msg = Message(
|
|
|
|
blockPresences: blocks.mapIt(
|
|
|
|
BlockPresence(
|
2023-11-14 12:02:17 +00:00
|
|
|
address: it.address,
|
2022-11-15 15:46:21 +00:00
|
|
|
type: BlockPresenceType.Have
|
2021-02-26 00:23:22 +00:00
|
|
|
)))
|
2023-03-10 07:02:54 +00:00
|
|
|
await buffer.pushData(lenPrefix(protobufEncode(msg)))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
test "Handles account messages":
|
2021-05-10 14:21:47 +00:00
|
|
|
let account = Account(address: EthAddress.example)
|
2021-04-08 07:45:41 +00:00
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc handleAccount(peer: PeerId, received: Account) {.gcsafe, async.} =
|
2021-05-10 14:21:47 +00:00
|
|
|
check received == account
|
2021-04-08 07:45:41 +00:00
|
|
|
done.complete()
|
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
network.handlers.onAccount = handleAccount
|
2021-04-08 07:45:41 +00:00
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
let message = Message(account: AccountMessage.init(account))
|
2023-03-10 07:02:54 +00:00
|
|
|
await buffer.pushData(lenPrefix(protobufEncode(message)))
|
2021-04-08 07:45:41 +00:00
|
|
|
|
|
|
|
await done.wait(100.millis)
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
test "Handles payment messages":
|
2021-04-08 08:09:59 +00:00
|
|
|
let payment = SignedState.example
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc handlePayment(peer: PeerId, received: SignedState) {.gcsafe, async.} =
|
2021-04-08 08:09:59 +00:00
|
|
|
check received == payment
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network.handlers.onPayment = handlePayment
|
|
|
|
|
|
|
|
let message = Message(payment: StateChannelUpdate.init(payment))
|
2023-03-10 07:02:54 +00:00
|
|
|
await buffer.pushData(lenPrefix(protobufEncode(message)))
|
2021-04-08 08:09:59 +00:00
|
|
|
|
|
|
|
await done.wait(100.millis)
|
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Network - Senders":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
2022-01-10 15:32:56 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
switch1, switch2: Switch
|
2021-08-30 19:25:20 +00:00
|
|
|
network1, network2: BlockExcNetwork
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks: seq[bt.Block]
|
2021-02-26 00:23:22 +00:00
|
|
|
done: Future[void]
|
|
|
|
|
|
|
|
setup:
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
blocks.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
done = newFuture[void]()
|
|
|
|
switch1 = newStandardSwitch()
|
|
|
|
switch2 = newStandardSwitch()
|
2021-08-30 19:25:20 +00:00
|
|
|
network1 = BlockExcNetwork.new(
|
2021-04-08 12:51:06 +00:00
|
|
|
switch = switch1)
|
2021-02-26 00:23:22 +00:00
|
|
|
switch1.mount(network1)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
network2 = BlockExcNetwork.new(
|
2021-04-08 12:51:06 +00:00
|
|
|
switch = switch2)
|
2021-02-26 00:23:22 +00:00
|
|
|
switch2.mount(network2)
|
|
|
|
|
2022-11-02 17:40:28 +00:00
|
|
|
await switch1.start()
|
|
|
|
await switch2.start()
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
await switch1.connect(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
switch2.peerInfo.addrs)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await allFuturesThrowing(
|
|
|
|
switch1.stop(),
|
|
|
|
switch2.stop())
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
test "Send want list":
|
2023-11-14 12:02:17 +00:00
|
|
|
proc wantListHandler(peer: PeerId, wantList: WantList) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
# check that we got the correct amount of entries
|
|
|
|
check wantList.entries.len == 4
|
|
|
|
|
|
|
|
for b in blocks:
|
2023-11-14 12:02:17 +00:00
|
|
|
check b.address in wantList.entries
|
|
|
|
let entry = wantList.entries[wantList.entries.find(b.address)]
|
2022-11-15 15:46:21 +00:00
|
|
|
check entry.wantType == WantType.WantHave
|
2021-02-26 00:23:22 +00:00
|
|
|
check entry.priority == 1
|
|
|
|
check entry.cancel == true
|
|
|
|
check entry.sendDontHave == true
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network2.handlers.onWantList = wantListHandler
|
2022-07-29 16:19:34 +00:00
|
|
|
await network1.sendWantList(
|
2021-02-26 00:23:22 +00:00
|
|
|
switch2.peerInfo.peerId,
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks.mapIt( it.address ),
|
2022-11-15 15:46:21 +00:00
|
|
|
1, true, WantType.WantHave,
|
2021-02-26 00:23:22 +00:00
|
|
|
true, true)
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
test "send blocks":
|
2023-11-14 12:02:17 +00:00
|
|
|
proc blocksDeliveryHandler(peer: PeerId, blocksDelivery: seq[BlockDelivery]) {.gcsafe, async.} =
|
|
|
|
check blocks == blocksDelivery.mapIt(it.blk)
|
2021-02-26 00:23:22 +00:00
|
|
|
done.complete()
|
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
network2.handlers.onBlocksDelivery = blocksDeliveryHandler
|
|
|
|
await network1.sendBlocksDelivery(
|
2021-02-26 00:23:22 +00:00
|
|
|
switch2.peerInfo.peerId,
|
2023-11-14 12:02:17 +00:00
|
|
|
blocks.mapIt(BlockDelivery(blk: it, address: it.address)))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
test "send presence":
|
2022-01-10 15:32:56 +00:00
|
|
|
proc presenceHandler(
|
2023-03-10 07:02:54 +00:00
|
|
|
peer: PeerId,
|
2022-01-10 15:32:56 +00:00
|
|
|
precense: seq[BlockPresence]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
for b in blocks:
|
|
|
|
check:
|
2023-11-14 12:02:17 +00:00
|
|
|
b.address in precense
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network2.handlers.onPresence = presenceHandler
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
await network1.sendBlockPresence(
|
2021-02-26 00:23:22 +00:00
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
blocks.mapIt(
|
|
|
|
BlockPresence(
|
2023-11-14 12:02:17 +00:00
|
|
|
address: it.address,
|
2022-11-15 15:46:21 +00:00
|
|
|
type: BlockPresenceType.Have
|
2021-02-26 00:23:22 +00:00
|
|
|
)))
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
2021-04-08 08:20:35 +00:00
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
test "send account":
|
2021-05-10 14:21:47 +00:00
|
|
|
let account = Account(address: EthAddress.example)
|
2021-04-08 08:20:35 +00:00
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc handleAccount(peer: PeerId, received: Account) {.gcsafe, async.} =
|
2021-05-10 14:21:47 +00:00
|
|
|
check received == account
|
2021-04-08 08:20:35 +00:00
|
|
|
done.complete()
|
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
network2.handlers.onAccount = handleAccount
|
2021-04-08 08:20:35 +00:00
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
await network1.sendAccount(switch2.peerInfo.peerId, account)
|
2021-11-15 13:53:05 +00:00
|
|
|
await done.wait(500.millis)
|
2021-04-08 08:33:40 +00:00
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
test "send payment":
|
2021-04-08 08:33:40 +00:00
|
|
|
let payment = SignedState.example
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
proc handlePayment(peer: PeerId, received: SignedState) {.gcsafe, async.} =
|
2021-04-08 08:33:40 +00:00
|
|
|
check received == payment
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network2.handlers.onPayment = handlePayment
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
await network1.sendPayment(switch2.peerInfo.peerId, payment)
|
2021-11-15 13:53:05 +00:00
|
|
|
await done.wait(500.millis)
|
2022-07-29 16:19:34 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "Network - Test Limits":
|
2022-07-29 16:19:34 +00: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 17:40:28 +00:00
|
|
|
await switch1.start()
|
|
|
|
await switch2.start()
|
|
|
|
|
2022-07-29 16:19:34 +00:00
|
|
|
await switch1.connect(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
switch2.peerInfo.addrs)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await allFuturesThrowing(
|
|
|
|
switch1.stop(),
|
|
|
|
switch2.stop())
|
|
|
|
|
|
|
|
test "Concurrent Sends":
|
|
|
|
let account = Account(address: EthAddress.example)
|
|
|
|
network2.handlers.onAccount =
|
2023-03-10 07:02:54 +00:00
|
|
|
proc(peer: PeerId, received: Account) {.gcsafe, async.} =
|
2022-07-29 16:19:34 +00:00
|
|
|
check false
|
|
|
|
|
|
|
|
let fut = network1.send(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
Message(account: AccountMessage.init(account)))
|
|
|
|
|
|
|
|
await sleepAsync(100.millis)
|
|
|
|
check not fut.finished
|