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
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
blocks1.add(bt.Block.init(chunk))
|
|
|
|
|
|
|
|
while true:
|
|
|
|
let chunk = await chunker2.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
blocks2.add(bt.Block.init(chunk))
|
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-01-10 15:32:56 +00:00
|
|
|
localStore1 = MemoryStore.new(blocks1.mapIt( it ))
|
2021-08-30 19:25:20 +00:00
|
|
|
network1 = BlockExcNetwork.new(switch = switch1)
|
2022-01-10 15:32:56 +00:00
|
|
|
engine1 = BlockExcEngine.new(localStore1, wallet1, network1)
|
|
|
|
blockexc1 = NetworkStore.new(engine1, localStore1)
|
2021-02-26 00:23:22 +00:00
|
|
|
switch1.mount(network1)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
localStore2 = MemoryStore.new(blocks2.mapIt( it ))
|
2021-08-30 19:25:20 +00:00
|
|
|
network2 = BlockExcNetwork.new(switch = switch2)
|
2022-01-10 15:32:56 +00:00
|
|
|
engine2 = BlockExcEngine.new(localStore2, wallet2, network2)
|
|
|
|
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
|
2021-08-30 19:25:20 +00:00
|
|
|
blockexc1.engine.wantList = blocks2.mapIt( it.cid )
|
|
|
|
blockexc2.engine.wantList = blocks1.mapIt( 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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
check:
|
|
|
|
peerCtx1.peerHave.mapIt( $it ).sorted(cmp[string]) ==
|
2021-08-30 19:25:20 +00:00
|
|
|
blockexc2.engine.wantList.mapIt( $it ).sorted(cmp[string])
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
peerCtx2.peerHave.mapIt( $it ).sorted(cmp[string]) ==
|
2021-08-30 19:25:20 +00:00
|
|
|
blockexc1.engine.wantList.mapIt( $it ).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-01-10 15:32:56 +00:00
|
|
|
let blk = bt.Block.init("Block 1".toBytes)
|
|
|
|
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-01-10 15:32:56 +00:00
|
|
|
let blk = bt.Block.init("Block 1".toBytes)
|
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":
|
2021-02-26 00:23:22 +00:00
|
|
|
let
|
2022-01-10 15:32:56 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
blocks.add(bt.Block.init(chunk))
|
|
|
|
|
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
|
|
|
|
engine.wantList &= blocks[0..3].mapIt( it.cid )
|
|
|
|
engine.wantList &= blocks[12..15].mapIt( it.cid )
|
|
|
|
|
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
|
|
|
|
|
|
|
check:
|
|
|
|
engine.peers[0].peerHave.mapIt($it).sorted(cmp[string]) ==
|
|
|
|
blocks[0..3].mapIt( it.cid ).mapIt($it).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]) ==
|
|
|
|
blocks[12..15].mapIt( it.cid ).mapIt($it).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
|
|
|
|
engine.wantList &= blocks[0..3].mapIt( it.cid )
|
|
|
|
engine.wantList &= blocks[12..15].mapIt( it.cid )
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
let wantListBlocks = await allFinished(
|
|
|
|
blocks[0..3].mapIt( downloader.getBlock(it.cid) ))
|
|
|
|
check wantListBlocks.mapIt( !it.read ) == blocks[0..3]
|