2021-02-26 00:23:22 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/algorithm
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/stew/byteutils
|
2021-08-30 19:25:20 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/rng
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/blockexchange
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/discovery
|
|
|
|
import pkg/codex/blocktype as bt
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-11-02 17:40:28 +00:00
|
|
|
import ../../examples
|
2022-05-19 22:28:53 +00:00
|
|
|
import ../../helpers
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "NetworkStore engine - 2 nodes":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker1 = RandomChunker.new(Rng.instance(), size = 2048, chunkSize = 256'nb)
|
|
|
|
chunker2 = RandomChunker.new(Rng.instance(), size = 2048, chunkSize = 256'nb)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
var
|
2022-05-19 02:29:15 +00:00
|
|
|
nodeCmps1, nodeCmps2: NodesComponents
|
2021-08-30 19:25:20 +00:00
|
|
|
peerCtx1, peerCtx2: BlockExcPeerCtx
|
2022-05-19 02:29:15 +00:00
|
|
|
pricing1, pricing2: Pricing
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks1, blocks2: seq[bt.Block]
|
2022-05-12 21:52:03 +00:00
|
|
|
pendingBlocks1, pendingBlocks2: seq[Future[bt.Block]]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
setup:
|
2022-01-10 15:32:56 +00:00
|
|
|
while true:
|
|
|
|
let chunk = await chunker1.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
blocks1.add(bt.Block.new(chunk).tryGet())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
while true:
|
|
|
|
let chunk = await chunker2.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
blocks2.add(bt.Block.new(chunk).tryGet())
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
nodeCmps1 = generateNodes(1, blocks1)[0]
|
|
|
|
nodeCmps2 = generateNodes(1, blocks2)[0]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
2022-05-19 02:29:15 +00:00
|
|
|
nodeCmps1.switch.start(),
|
|
|
|
nodeCmps1.blockDiscovery.start(),
|
|
|
|
nodeCmps1.engine.start(),
|
|
|
|
nodeCmps2.switch.start(),
|
|
|
|
nodeCmps2.blockDiscovery.start(),
|
|
|
|
nodeCmps2.engine.start())
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
# initialize our want lists
|
2022-11-15 15:46:21 +00:00
|
|
|
pendingBlocks1 = blocks2[0..3].mapIt(
|
|
|
|
nodeCmps1.pendingBlocks.getWantHandle( it.cid )
|
|
|
|
)
|
|
|
|
|
|
|
|
pendingBlocks2 = blocks1[0..3].mapIt(
|
|
|
|
nodeCmps2.pendingBlocks.getWantHandle( it.cid )
|
|
|
|
)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-11-02 17:40:28 +00:00
|
|
|
pricing1 = Pricing.example()
|
|
|
|
pricing2 = Pricing.example()
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
pricing1.address = nodeCmps1.wallet.address
|
|
|
|
pricing2.address = nodeCmps2.wallet.address
|
|
|
|
nodeCmps1.engine.pricing = pricing1.some
|
|
|
|
nodeCmps2.engine.pricing = pricing2.some
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
await nodeCmps1.switch.connect(
|
|
|
|
nodeCmps2.switch.peerInfo.peerId,
|
|
|
|
nodeCmps2.switch.peerInfo.addrs)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
await sleepAsync(1.seconds) # give some time to exchange lists
|
2022-05-19 02:29:15 +00:00
|
|
|
peerCtx2 = nodeCmps1.peerStore.get(nodeCmps2.switch.peerInfo.peerId)
|
|
|
|
peerCtx1 = nodeCmps2.peerStore.get(nodeCmps1.switch.peerInfo.peerId)
|
|
|
|
|
|
|
|
check isNil(peerCtx1).not
|
|
|
|
check isNil(peerCtx2).not
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
teardown:
|
|
|
|
await allFuturesThrowing(
|
2022-05-19 02:29:15 +00:00
|
|
|
nodeCmps1.blockDiscovery.stop(),
|
|
|
|
nodeCmps1.engine.stop(),
|
|
|
|
nodeCmps1.switch.stop(),
|
|
|
|
nodeCmps2.blockDiscovery.stop(),
|
|
|
|
nodeCmps2.engine.stop(),
|
|
|
|
nodeCmps2.switch.stop())
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
test "Should exchange blocks on connect":
|
2022-05-12 21:52:03 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks1))
|
2022-05-19 02:29:15 +00:00
|
|
|
.wait(10.seconds)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks2))
|
2022-05-19 02:29:15 +00:00
|
|
|
.wait(10.seconds)
|
2022-05-12 21:52:03 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
check:
|
2022-11-15 15:46:21 +00:00
|
|
|
(await allFinished(
|
|
|
|
blocks1[0..3].mapIt(
|
|
|
|
nodeCmps2.localStore.getBlock( it.cid ) )))
|
|
|
|
.filterIt( it.completed and it.read.isOk )
|
|
|
|
.mapIt( $it.read.get.cid ).sorted(cmp[string]) ==
|
|
|
|
blocks1[0..3].mapIt( $it.cid ).sorted(cmp[string])
|
|
|
|
|
|
|
|
(await allFinished(
|
|
|
|
blocks2[0..3].mapIt(
|
|
|
|
nodeCmps1.localStore.getBlock( it.cid ) )))
|
|
|
|
.filterIt( it.completed and it.read.isOk )
|
|
|
|
.mapIt( $it.read.get.cid ).sorted(cmp[string]) ==
|
|
|
|
blocks2[0..3].mapIt( $it.cid ).sorted(cmp[string])
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should exchanges accounts on connect":
|
2021-05-10 14:21:47 +00:00
|
|
|
check peerCtx1.account.?address == pricing1.address.some
|
|
|
|
check peerCtx2.account.?address == pricing2.address.some
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should send want-have for block":
|
2022-03-18 19:50:53 +00:00
|
|
|
let blk = bt.Block.new("Block 1".toBytes).tryGet()
|
2022-07-28 00:39:17 +00:00
|
|
|
(await nodeCmps2.localStore.putBlock(blk)).tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
let entry = Entry(
|
|
|
|
`block`: blk.cid.data.buffer,
|
|
|
|
priority: 1,
|
|
|
|
cancel: false,
|
2022-11-15 15:46:21 +00:00
|
|
|
wantType: WantType.WantBlock,
|
2021-02-26 00:23:22 +00:00
|
|
|
sendDontHave: false)
|
|
|
|
|
|
|
|
peerCtx1.peerWants.add(entry)
|
2022-05-19 02:29:15 +00:00
|
|
|
check nodeCmps2
|
2022-01-10 15:32:56 +00:00
|
|
|
.engine
|
|
|
|
.taskQueue
|
|
|
|
.pushOrUpdateNoWait(peerCtx1).isOk
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
check eventually (await nodeCmps1.localStore.hasBlock(blk.cid)).tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should get blocks from remote":
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
blocks = await allFinished(
|
|
|
|
blocks2[4..7].mapIt(
|
|
|
|
nodeCmps1.networkStore.getBlock(it.cid)
|
|
|
|
))
|
|
|
|
|
|
|
|
check blocks.mapIt( it.read().tryGet() ) == blocks2[4..7]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Remote should send blocks when available":
|
2022-03-18 19:50:53 +00:00
|
|
|
let blk = bt.Block.new("Block 1".toBytes).tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
# should fail retrieving block from remote
|
2022-11-15 15:46:21 +00:00
|
|
|
check not await blk.cid in nodeCmps1.networkStore
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
# second trigger blockexc to resolve any pending requests
|
2021-02-26 00:23:22 +00:00
|
|
|
# for the block
|
2022-07-28 00:39:17 +00:00
|
|
|
(await nodeCmps2.networkStore.putBlock(blk)).tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
# should succeed retrieving block from remote
|
2022-05-19 02:29:15 +00:00
|
|
|
check await nodeCmps1.networkStore.getBlock(blk.cid)
|
|
|
|
.withTimeout(100.millis) # should succeed
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should receive payments for blocks that were sent":
|
2022-11-02 17:40:28 +00:00
|
|
|
discard await allFinished(
|
2022-11-15 15:46:21 +00:00
|
|
|
blocks2[4..7].mapIt(
|
|
|
|
nodeCmps2.networkStore.putBlock(it)
|
|
|
|
))
|
|
|
|
|
2023-03-09 11:23:45 +00:00
|
|
|
discard await allFinished(
|
|
|
|
blocks2[4..7].mapIt(
|
|
|
|
nodeCmps1.networkStore.getBlock(it.cid)
|
|
|
|
))
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
let
|
|
|
|
channel = !peerCtx1.paymentChannel
|
|
|
|
wallet = nodeCmps2.wallet
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2022-07-18 08:46:24 +00:00
|
|
|
check eventually wallet.balance(channel, Asset) > 0
|
2021-04-19 14:47:47 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
asyncchecksuite "NetworkStore - multiple nodes":
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
2023-07-06 23:23:27 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = 4096, chunkSize = 256'nb)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
switch: seq[Switch]
|
2022-05-19 02:29:15 +00:00
|
|
|
networkStore: seq[NetworkStore]
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks: seq[bt.Block]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
for e in generateNodes(5):
|
|
|
|
switch.add(e.switch)
|
2022-05-19 02:29:15 +00:00
|
|
|
networkStore.add(e.networkStore)
|
|
|
|
await e.engine.start()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
switch.mapIt( it.start() )
|
|
|
|
)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
teardown:
|
|
|
|
await allFuturesThrowing(
|
|
|
|
switch.mapIt( it.stop() )
|
|
|
|
)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
switch = @[]
|
2022-05-19 02:29:15 +00:00
|
|
|
networkStore = @[]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
test "Should receive blocks for own want list":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
2022-05-19 02:29:15 +00:00
|
|
|
downloader = networkStore[4]
|
2021-02-26 00:23:22 +00:00
|
|
|
engine = downloader.engine
|
|
|
|
|
|
|
|
# Add blocks from 1st peer to want list
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
2022-11-15 15:46:21 +00:00
|
|
|
downloadCids =
|
|
|
|
blocks[0..3].mapIt(
|
|
|
|
it.cid
|
|
|
|
) &
|
|
|
|
blocks[12..15].mapIt(
|
|
|
|
it.cid
|
|
|
|
)
|
|
|
|
|
|
|
|
pendingBlocks = downloadCids.mapIt(
|
|
|
|
engine.pendingBlocks.getWantHandle( it )
|
|
|
|
)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
for i in 0..15:
|
|
|
|
(await networkStore[i div 4].engine.localStore.putBlock(blocks[i])).tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await connectNodes(switch)
|
|
|
|
await sleepAsync(1.seconds)
|
2021-05-10 12:08:40 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
await allFuturesThrowing(
|
2022-11-15 15:46:21 +00:00
|
|
|
allFinished(pendingBlocks))
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2021-05-10 12:08:40 +00:00
|
|
|
check:
|
2022-11-15 15:46:21 +00:00
|
|
|
(await allFinished(
|
|
|
|
downloadCids.mapIt(
|
|
|
|
downloader.localStore.getBlock( it ) )))
|
|
|
|
.filterIt( it.completed and it.read.isOk )
|
|
|
|
.mapIt( $it.read.get.cid ).sorted(cmp[string]) ==
|
|
|
|
downloadCids.mapIt( $it ).sorted(cmp[string])
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
test "Should exchange blocks with multiple nodes":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
2022-05-19 02:29:15 +00:00
|
|
|
downloader = networkStore[4]
|
2021-02-26 00:23:22 +00:00
|
|
|
engine = downloader.engine
|
|
|
|
|
|
|
|
# Add blocks from 1st peer to want list
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
2022-11-15 15:46:21 +00:00
|
|
|
pendingBlocks1 = blocks[0..3].mapIt(
|
|
|
|
engine.pendingBlocks.getWantHandle( it.cid )
|
|
|
|
)
|
|
|
|
pendingBlocks2 = blocks[12..15].mapIt(
|
|
|
|
engine.pendingBlocks.getWantHandle( it.cid )
|
|
|
|
)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
for i in 0..15:
|
|
|
|
(await networkStore[i div 4].engine.localStore.putBlock(blocks[i])).tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await connectNodes(switch)
|
2022-01-10 15:32:56 +00:00
|
|
|
await sleepAsync(1.seconds)
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
await allFuturesThrowing(
|
2022-05-12 21:52:03 +00:00
|
|
|
allFinished(pendingBlocks1),
|
|
|
|
allFinished(pendingBlocks2))
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
check pendingBlocks1.mapIt( it.read ) == blocks[0..3]
|
|
|
|
check pendingBlocks2.mapIt( it.read ) == blocks[12..15]
|