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
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/libp2p/errors
|
|
|
|
|
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
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
suite "NetworkStore engine - 2 nodes":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
2022-01-10 15:32:56 +00:00
|
|
|
chunker1 = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
|
|
|
chunker2 = RandomChunker.new(Rng.instance(), size = 1024, chunkSize = 256)
|
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-05-19 02:29:15 +00:00
|
|
|
pendingBlocks1 = blocks2.mapIt( nodeCmps1.pendingBlocks.getWantHandle( it.cid ) )
|
|
|
|
pendingBlocks2 = blocks1.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())
|
|
|
|
|
|
|
|
test "Should exchange want lists 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:
|
|
|
|
peerCtx1.peerHave.mapIt( $it ).sorted(cmp[string]) ==
|
2022-05-12 21:52:03 +00:00
|
|
|
pendingBlocks2.mapIt( $it.read.cid ).sorted(cmp[string])
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
peerCtx2.peerHave.mapIt( $it ).sorted(cmp[string]) ==
|
2022-05-12 21:52:03 +00:00
|
|
|
pendingBlocks1.mapIt( $it.read.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,
|
|
|
|
wantType: WantType.wantBlock,
|
|
|
|
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-01-10 15:32:56 +00:00
|
|
|
let blocks = await allFinished(
|
2022-05-19 02:29:15 +00:00
|
|
|
blocks2.mapIt( nodeCmps1.networkStore.getBlock(it.cid) ))
|
2022-08-19 00:56:36 +00:00
|
|
|
check blocks.mapIt( it.read().tryGet() ) == blocks2
|
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-05-19 02:29:15 +00:00
|
|
|
check not await nodeCmps1.networkStore.getBlock(blk.cid)
|
2021-02-26 00:23:22 +00:00
|
|
|
.withTimeout(100.millis) # should expire
|
|
|
|
|
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
|
|
|
# delete on node1 cached blocks from node2
|
|
|
|
discard await allFinished(
|
|
|
|
blocks2.mapIt( nodeCmps1.networkStore.delBlock(it.cid) ))
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
let blocks = await allFinished(
|
2022-05-19 02:29:15 +00:00
|
|
|
blocks2.mapIt( nodeCmps1.networkStore.getBlock(it.cid) ))
|
|
|
|
|
2022-07-18 08:46:24 +00:00
|
|
|
let channel = !peerCtx1.paymentChannel
|
|
|
|
let 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
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
suite "NetworkStore - multiple nodes":
|
2022-05-12 21:52:03 +00:00
|
|
|
let
|
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = 4096, chunkSize = 256)
|
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-05-19 02:29:15 +00:00
|
|
|
test "Should receive haves 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
|
|
|
|
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)
|
|
|
|
await sleepAsync(1.seconds)
|
2021-05-10 12:08:40 +00:00
|
|
|
|
2022-05-12 21:52:03 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks1),
|
|
|
|
allFinished(pendingBlocks2))
|
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
let
|
|
|
|
peers = toSeq(engine.peers)
|
|
|
|
|
2021-05-10 12:08:40 +00:00
|
|
|
check:
|
2022-05-19 02:29:15 +00:00
|
|
|
peers[0].peerHave.mapIt($it).sorted(cmp[string]) ==
|
2022-05-12 21:52:03 +00:00
|
|
|
blocks[0..3].mapIt( $(it.cid) ).sorted(cmp[string])
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
peers[3].peerHave.mapIt($it).sorted(cmp[string]) ==
|
2022-05-12 21:52:03 +00:00
|
|
|
blocks[12..15].mapIt( $(it.cid) ).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
|
|
|
|
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]
|