2021-02-26 00:23:22 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/tables
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/libp2p/errors
|
2021-08-30 19:25:20 +00:00
|
|
|
import questionable
|
|
|
|
import questionable/results
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/protobuf_serialization
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
import pkg/dagger/rng
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/dagger/chunker
|
|
|
|
import pkg/dagger/blocktype as bt
|
2021-08-30 19:25:20 +00:00
|
|
|
import pkg/dagger/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
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
suite "NetworkStore network":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
|
|
|
rng = Rng.instance()
|
|
|
|
seckey = PrivateKey.random(rng[]).tryGet()
|
2021-10-29 19:30:52 +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
|
|
|
|
|
|
|
|
blocks.add(bt.Block.init(chunk))
|
|
|
|
|
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":
|
2022-01-10 15:32:56 +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:
|
|
|
|
check b.cid in wantList.entries
|
|
|
|
let entry = wantList.entries[wantList.entries.find(b.cid)]
|
|
|
|
check entry.wantType == WantType.wantHave
|
|
|
|
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 ),
|
|
|
|
1, true, WantType.wantHave,
|
|
|
|
true, true)
|
|
|
|
|
|
|
|
let msg = Message(wantlist: wantList)
|
|
|
|
await buffer.pushData(lenPrefix(Protobuf.encode(msg)))
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
|
|
|
test "Blocks Handler":
|
2022-01-10 15:32:56 +00:00
|
|
|
proc blocksHandler(peer: PeerID, blks: seq[bt.Block]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
check blks == blocks
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network.handlers.onBlocks = blocksHandler
|
|
|
|
|
|
|
|
let msg = Message(payload: makeBlocks(blocks))
|
|
|
|
await buffer.pushData(lenPrefix(Protobuf.encode(msg)))
|
|
|
|
|
|
|
|
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(
|
|
|
|
peer: PeerID,
|
|
|
|
precense: seq[BlockPresence]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
for b in blocks:
|
|
|
|
check:
|
|
|
|
b.cid in precense
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network.handlers.onPresence = presenceHandler
|
|
|
|
|
|
|
|
let msg = Message(
|
|
|
|
blockPresences: blocks.mapIt(
|
|
|
|
BlockPresence(
|
|
|
|
cid: it.cid.data.buffer,
|
|
|
|
type: BlockPresenceType.presenceHave
|
|
|
|
)))
|
|
|
|
await buffer.pushData(lenPrefix(Protobuf.encode(msg)))
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
test "handles account messages":
|
|
|
|
let account = Account(address: EthAddress.example)
|
2021-04-08 07:45:41 +00:00
|
|
|
|
2022-01-10 15:32:56 +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))
|
2021-04-08 07:45:41 +00:00
|
|
|
await buffer.pushData(lenPrefix(Protobuf.encode(message)))
|
|
|
|
|
|
|
|
await done.wait(100.millis)
|
|
|
|
|
2021-04-08 08:09:59 +00:00
|
|
|
test "handles payment messages":
|
|
|
|
let payment = SignedState.example
|
|
|
|
|
2022-01-10 15:32:56 +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))
|
|
|
|
await buffer.pushData(lenPrefix(Protobuf.encode(message)))
|
|
|
|
|
|
|
|
await done.wait(100.millis)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
suite "NetworkStore Network - e2e":
|
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
|
|
|
|
|
|
|
|
blocks.add(bt.Block.init(chunk))
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
done = newFuture[void]()
|
|
|
|
switch1 = newStandardSwitch()
|
|
|
|
switch2 = newStandardSwitch()
|
2022-01-10 15:32:56 +00:00
|
|
|
await switch1.start()
|
|
|
|
await switch2.start()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
await switch1.connect(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
switch2.peerInfo.addrs)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await allFuturesThrowing(
|
|
|
|
switch1.stop(),
|
|
|
|
switch2.stop())
|
|
|
|
|
|
|
|
test "broadcast want list":
|
2022-01-10 15:32:56 +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:
|
|
|
|
check b.cid in wantList.entries
|
|
|
|
let entry = wantList.entries[wantList.entries.find(b.cid)]
|
|
|
|
check entry.wantType == WantType.wantHave
|
|
|
|
check entry.priority == 1
|
|
|
|
check entry.cancel == true
|
|
|
|
check entry.sendDontHave == true
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network2.handlers.onWantList = wantListHandler
|
|
|
|
network1.broadcastWantList(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
blocks.mapIt( it.cid ),
|
|
|
|
1, true, WantType.wantHave,
|
|
|
|
true, true)
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
|
|
|
test "broadcast blocks":
|
2022-01-10 15:32:56 +00:00
|
|
|
proc blocksHandler(peer: PeerID, blks: seq[bt.Block]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
check blks == blocks
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network2.handlers.onBlocks = blocksHandler
|
|
|
|
network1.broadcastBlocks(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
blocks)
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
|
|
|
|
|
|
|
test "broadcast precense":
|
2022-01-10 15:32:56 +00:00
|
|
|
proc presenceHandler(
|
|
|
|
peer: PeerID,
|
|
|
|
precense: seq[BlockPresence]) {.gcsafe, async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
for b in blocks:
|
|
|
|
check:
|
|
|
|
b.cid in precense
|
|
|
|
|
|
|
|
done.complete()
|
|
|
|
|
|
|
|
network2.handlers.onPresence = presenceHandler
|
|
|
|
|
|
|
|
network1.broadcastBlockPresence(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
blocks.mapIt(
|
|
|
|
BlockPresence(
|
|
|
|
cid: it.cid.data.buffer,
|
|
|
|
type: BlockPresenceType.presenceHave
|
|
|
|
)))
|
|
|
|
|
|
|
|
await done.wait(500.millis)
|
2021-04-08 08:20:35 +00:00
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
test "broadcasts account":
|
|
|
|
let account = Account(address: EthAddress.example)
|
2021-04-08 08:20:35 +00:00
|
|
|
|
2022-01-10 15:32:56 +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
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
network1.broadcastAccount(switch2.peerInfo.peerId, account)
|
2021-04-08 08:20:35 +00:00
|
|
|
|
2021-11-15 13:53:05 +00:00
|
|
|
await done.wait(500.millis)
|
2021-04-08 08:33:40 +00:00
|
|
|
|
|
|
|
test "broadcasts payment":
|
|
|
|
let payment = SignedState.example
|
|
|
|
|
2022-01-10 15:32:56 +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
|
|
|
|
|
|
|
|
network1.broadcastPayment(switch2.peerInfo.peerId, payment)
|
|
|
|
|
2021-11-15 13:53:05 +00:00
|
|
|
await done.wait(500.millis)
|