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
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
import pkg/dagger/rng
|
|
|
|
import pkg/dagger/stores
|
|
|
|
import pkg/dagger/blockexchange
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/dagger/chunker
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/dagger/discovery
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/dagger/blocktype as bt
|
|
|
|
|
|
|
|
import ../helpers
|
2021-04-08 12:27:49 +00:00
|
|
|
import ../examples
|
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
|
|
|
|
switch1, switch2: Switch
|
2021-04-19 14:37:38 +00:00
|
|
|
wallet1, wallet2: WalletRef
|
2021-04-08 12:27:49 +00:00
|
|
|
pricing1, pricing2: Pricing
|
2021-08-30 19:25:20 +00:00
|
|
|
network1, network2: BlockExcNetwork
|
2022-01-10 15:32:56 +00:00
|
|
|
blockexc1, blockexc2: NetworkStore
|
2021-02-26 00:23:22 +00:00
|
|
|
peerId1, peerId2: PeerID
|
2021-08-30 19:25:20 +00:00
|
|
|
peerCtx1, peerCtx2: BlockExcPeerCtx
|
2022-01-10 15:32:56 +00:00
|
|
|
blocks1, blocks2: seq[bt.Block]
|
|
|
|
engine1, engine2: BlockExcEngine
|
|
|
|
localStore1, localStore2: BlockStore
|
2022-04-13 16:32:35 +00:00
|
|
|
discovery1, discovery2: Discovery
|
2022-04-20 03:46:44 +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
|
|
|
|
|
|
|
switch1 = newStandardSwitch()
|
|
|
|
switch2 = newStandardSwitch()
|
2021-04-19 14:37:38 +00:00
|
|
|
wallet1 = WalletRef.example
|
|
|
|
wallet2 = WalletRef.example
|
2021-04-08 12:27:49 +00:00
|
|
|
pricing1 = Pricing.example
|
|
|
|
pricing2 = Pricing.example
|
2022-01-10 15:32:56 +00:00
|
|
|
await switch1.start()
|
|
|
|
await switch2.start()
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
peerId1 = switch1.peerInfo.peerId
|
|
|
|
peerId2 = switch2.peerInfo.peerId
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
localStore1 = CacheStore.new(blocks1.mapIt( it ))
|
2022-04-13 16:32:35 +00:00
|
|
|
discovery1 = Discovery.new(switch1.peerInfo, Port(0))
|
2021-08-30 19:25:20 +00:00
|
|
|
network1 = BlockExcNetwork.new(switch = switch1)
|
2022-04-13 16:32:35 +00:00
|
|
|
engine1 = BlockExcEngine.new(localStore1, wallet1, network1, discovery1)
|
2022-01-10 15:32:56 +00:00
|
|
|
blockexc1 = NetworkStore.new(engine1, localStore1)
|
2021-02-26 00:23:22 +00:00
|
|
|
switch1.mount(network1)
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
localStore2 = CacheStore.new(blocks2.mapIt( it ))
|
2022-04-13 16:32:35 +00:00
|
|
|
discovery2 = Discovery.new(switch2.peerInfo, Port(0))
|
2021-08-30 19:25:20 +00:00
|
|
|
network2 = BlockExcNetwork.new(switch = switch2)
|
2022-04-13 16:32:35 +00:00
|
|
|
engine2 = BlockExcEngine.new(localStore2, wallet2, network2, discovery2)
|
2022-01-10 15:32:56 +00:00
|
|
|
blockexc2 = NetworkStore.new(engine2, localStore2)
|
2021-02-26 00:23:22 +00:00
|
|
|
switch2.mount(network2)
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
2022-01-10 15:32:56 +00:00
|
|
|
engine1.start(),
|
|
|
|
engine2.start(),
|
2021-02-26 00:23:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# initialize our want lists
|
2022-04-20 03:46:44 +00:00
|
|
|
pendingBlocks1 = blocks2.mapIt( blockexc1.engine.pendingBlocks.getWantHandle( it.cid ) )
|
|
|
|
pendingBlocks2 = blocks1.mapIt( blockexc2.engine.pendingBlocks.getWantHandle( it.cid ) )
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-04-19 14:47:47 +00:00
|
|
|
pricing1.address = wallet1.address
|
|
|
|
pricing2.address = wallet2.address
|
2021-08-30 19:25:20 +00:00
|
|
|
blockexc1.engine.pricing = pricing1.some
|
|
|
|
blockexc2.engine.pricing = pricing2.some
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
await switch1.connect(
|
|
|
|
switch2.peerInfo.peerId,
|
|
|
|
switch2.peerInfo.addrs)
|
|
|
|
|
2022-04-20 03:46:44 +00:00
|
|
|
await sleepAsync(1.seconds) # give some time to exchange lists
|
2021-08-30 19:25:20 +00:00
|
|
|
peerCtx2 = blockexc1.engine.getPeerCtx(peerId2)
|
|
|
|
peerCtx1 = blockexc2.engine.getPeerCtx(peerId1)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
teardown:
|
|
|
|
await allFuturesThrowing(
|
2022-01-10 15:32:56 +00:00
|
|
|
engine1.stop(),
|
|
|
|
engine2.stop(),
|
2021-02-26 00:23:22 +00:00
|
|
|
switch1.stop(),
|
|
|
|
switch2.stop())
|
|
|
|
|
|
|
|
test "should exchange want lists on connect":
|
|
|
|
check not isNil(peerCtx1)
|
|
|
|
check not isNil(peerCtx2)
|
|
|
|
|
2022-04-20 03:46:44 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks1))
|
|
|
|
|
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks2))
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
check:
|
|
|
|
peerCtx1.peerHave.mapIt( $it ).sorted(cmp[string]) ==
|
2022-04-20 03:46:44 +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-04-20 03:46:44 +00:00
|
|
|
pendingBlocks1.mapIt( $it.read.cid ).sorted(cmp[string])
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-05-10 14:21:47 +00:00
|
|
|
test "exchanges accounts on connect":
|
|
|
|
check peerCtx1.account.?address == pricing1.address.some
|
|
|
|
check peerCtx2.account.?address == pricing2.address.some
|
2021-04-08 12:27:49 +00:00
|
|
|
|
2021-02-26 00:23:22 +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-01-10 15:32:56 +00:00
|
|
|
check await blockexc2.engine.localStore.putBlock(blk)
|
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-01-10 15:32:56 +00:00
|
|
|
check blockexc2
|
|
|
|
.engine
|
|
|
|
.taskQueue
|
|
|
|
.pushOrUpdateNoWait(peerCtx1).isOk
|
2021-02-26 00:23:22 +00:00
|
|
|
await sleepAsync(100.millis)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
check blockexc1.engine.localStore.hasBlock(blk.cid)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
test "should get blocks from remote":
|
2022-01-10 15:32:56 +00:00
|
|
|
let blocks = await allFinished(
|
|
|
|
blocks2.mapIt( blockexc1.getBlock(it.cid) ))
|
|
|
|
check blocks.mapIt( !it.read ) == blocks2
|
2021-02-26 00:23:22 +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-01-10 15:32:56 +00:00
|
|
|
check not await blockexc1.getBlock(blk.cid)
|
2021-02-26 00:23:22 +00:00
|
|
|
.withTimeout(100.millis) # should expire
|
|
|
|
|
|
|
|
# first put the required block in the local store
|
2022-01-10 15:32:56 +00:00
|
|
|
check await blockexc2.engine.localStore.putBlock(blk)
|
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-01-10 15:32:56 +00:00
|
|
|
check await blockexc2.putBlock(blk)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
# should succeed retrieving block from remote
|
|
|
|
check await blockexc1.getBlock(blk.cid)
|
|
|
|
.withTimeout(100.millis) # should succede
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-04-19 14:47:47 +00:00
|
|
|
test "receives payments for blocks that were sent":
|
2022-01-10 15:32:56 +00:00
|
|
|
let blocks = await allFinished(
|
|
|
|
blocks2.mapIt( blockexc1.getBlock(it.cid) ))
|
2021-04-19 14:47:47 +00:00
|
|
|
await sleepAsync(100.millis)
|
|
|
|
let channel = !peerCtx1.paymentChannel
|
2021-04-22 08:11:24 +00:00
|
|
|
check wallet2.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-04-20 03:46:44 +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-01-10 15:32:56 +00:00
|
|
|
blockexc: seq[NetworkStore]
|
|
|
|
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)
|
2021-08-30 19:25:20 +00:00
|
|
|
blockexc.add(e.blockexc)
|
2022-01-10 15:32:56 +00:00
|
|
|
await e.blockexc.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 = @[]
|
|
|
|
blockexc = @[]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
test "should receive haves for own want list":
|
|
|
|
let
|
2021-08-30 19:25:20 +00:00
|
|
|
downloader = blockexc[4]
|
2021-02-26 00:23:22 +00:00
|
|
|
engine = downloader.engine
|
|
|
|
|
|
|
|
# Add blocks from 1st peer to want list
|
2022-04-20 03:46:44 +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-01-10 15:32:56 +00:00
|
|
|
await allFutures(
|
|
|
|
blocks[0..3].mapIt( blockexc[0].engine.localStore.putBlock(it) ))
|
|
|
|
await allFutures(
|
|
|
|
blocks[4..7].mapIt( blockexc[1].engine.localStore.putBlock(it) ))
|
|
|
|
await allFutures(
|
|
|
|
blocks[8..11].mapIt( blockexc[2].engine.localStore.putBlock(it) ))
|
|
|
|
await allFutures(
|
|
|
|
blocks[12..15].mapIt( blockexc[3].engine.localStore.putBlock(it) ))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
await connectNodes(switch)
|
|
|
|
await sleepAsync(1.seconds)
|
2021-05-10 12:08:40 +00:00
|
|
|
|
2022-04-20 03:46:44 +00:00
|
|
|
await allFuturesThrowing(
|
|
|
|
allFinished(pendingBlocks1),
|
|
|
|
allFinished(pendingBlocks2))
|
|
|
|
|
2021-05-10 12:08:40 +00:00
|
|
|
check:
|
|
|
|
engine.peers[0].peerHave.mapIt($it).sorted(cmp[string]) ==
|
2022-04-20 03:46:44 +00:00
|
|
|
blocks[0..3].mapIt( $(it.cid) ).sorted(cmp[string])
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2021-05-10 12:08:40 +00:00
|
|
|
engine.peers[3].peerHave.mapIt($it).sorted(cmp[string]) ==
|
2022-04-20 03:46:44 +00:00
|
|
|
blocks[12..15].mapIt( $(it.cid) ).sorted(cmp[string])
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
test "should exchange blocks with multiple nodes":
|
|
|
|
let
|
2021-08-30 19:25:20 +00:00
|
|
|
downloader = blockexc[4]
|
2021-02-26 00:23:22 +00:00
|
|
|
engine = downloader.engine
|
|
|
|
|
|
|
|
# Add blocks from 1st peer to want list
|
2022-04-20 03:46:44 +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-01-10 15:32:56 +00:00
|
|
|
await allFutures(
|
|
|
|
blocks[0..3].mapIt( blockexc[0].engine.localStore.putBlock(it) ))
|
|
|
|
await allFutures(
|
|
|
|
blocks[4..7].mapIt( blockexc[1].engine.localStore.putBlock(it) ))
|
|
|
|
await allFutures(
|
|
|
|
blocks[8..11].mapIt( blockexc[2].engine.localStore.putBlock(it) ))
|
|
|
|
await allFutures(
|
|
|
|
blocks[12..15].mapIt( blockexc[3].engine.localStore.putBlock(it) ))
|
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-04-20 03:46:44 +00:00
|
|
|
allFinished(pendingBlocks1),
|
|
|
|
allFinished(pendingBlocks2))
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-04-20 03:46:44 +00:00
|
|
|
check pendingBlocks1.mapIt( it.read ) == blocks[0..3]
|
|
|
|
check pendingBlocks2.mapIt( it.read ) == blocks[12..15]
|