2022-01-10 15:32:56 +00:00
|
|
|
import std/os
|
|
|
|
import std/options
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
2022-03-14 16:06:36 +00:00
|
|
|
import pkg/chronicles
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/stew/byteutils
|
|
|
|
|
|
|
|
import pkg/nitro
|
|
|
|
import pkg/libp2p
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/libp2pdht/discv5/protocol as discv5
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import pkg/dagger/stores
|
|
|
|
import pkg/dagger/blockexchange
|
|
|
|
import pkg/dagger/chunker
|
|
|
|
import pkg/dagger/node
|
2022-03-14 16:06:36 +00:00
|
|
|
import pkg/dagger/manifest
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/dagger/discovery
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/dagger/blocktype as bt
|
2022-04-14 10:48:10 +00:00
|
|
|
import pkg/dagger/contracts
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import ./helpers
|
|
|
|
|
|
|
|
suite "Test Node":
|
|
|
|
let
|
|
|
|
(path, _, _) = instantiationInfo(-2, fullPaths = true) # get this file's name
|
|
|
|
|
|
|
|
var
|
|
|
|
file: File
|
|
|
|
chunker: Chunker
|
|
|
|
switch: Switch
|
|
|
|
wallet: WalletRef
|
|
|
|
network: BlockExcNetwork
|
2022-03-02 16:30:42 +00:00
|
|
|
localStore: CacheStore
|
2022-01-10 15:32:56 +00:00
|
|
|
engine: BlockExcEngine
|
|
|
|
store: NetworkStore
|
|
|
|
node: DaggerNodeRef
|
2022-04-13 16:32:35 +00:00
|
|
|
discovery: Discovery
|
2022-04-25 13:12:37 +00:00
|
|
|
contracts: ?ContractInteractions
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
file = open(path.splitFile().dir /../ "fixtures" / "test.jpg")
|
2022-03-30 02:43:35 +00:00
|
|
|
chunker = FileChunker.new(file = file, chunkSize = BlockSize)
|
2022-01-10 15:32:56 +00:00
|
|
|
switch = newStandardSwitch()
|
|
|
|
wallet = WalletRef.new(EthPrivateKey.random())
|
|
|
|
network = BlockExcNetwork.new(switch)
|
2022-03-02 16:30:42 +00:00
|
|
|
localStore = CacheStore.new()
|
2022-04-13 16:32:35 +00:00
|
|
|
discovery = Discovery.new(switch.peerInfo, Port(0))
|
|
|
|
engine = BlockExcEngine.new(localStore, wallet, network, discovery)
|
2022-01-10 15:32:56 +00:00
|
|
|
store = NetworkStore.new(engine, localStore)
|
2022-04-14 10:48:10 +00:00
|
|
|
contracts = ContractInteractions.new()
|
|
|
|
node = DaggerNodeRef.new(switch, store, engine, nil, discovery, contracts) # TODO: pass `Erasure`
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
close(file)
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
test "Store Data Stream":
|
|
|
|
let
|
|
|
|
stream = BufferStream.new()
|
|
|
|
storeFut = node.store(stream)
|
|
|
|
|
|
|
|
var
|
2022-03-17 13:56:46 +00:00
|
|
|
manifest = Manifest.new().tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
await stream.pushData(chunk)
|
2022-03-18 19:50:53 +00:00
|
|
|
manifest.add(bt.Block.new(chunk).tryGet().cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
finally:
|
|
|
|
await stream.pushEof()
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
let
|
2022-01-13 00:42:18 +00:00
|
|
|
manifestCid = (await storeFut).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
manifestCid in localStore
|
|
|
|
|
|
|
|
var
|
2022-01-13 00:42:18 +00:00
|
|
|
manifestBlock = (await localStore.getBlock(manifestCid)).tryGet()
|
2022-03-14 16:06:36 +00:00
|
|
|
localManifest = Manifest.decode(manifestBlock.data).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
manifest.len == localManifest.len
|
|
|
|
manifest.cid == localManifest.cid
|
|
|
|
|
|
|
|
test "Retrieve Data Stream":
|
|
|
|
var
|
2022-03-17 13:56:46 +00:00
|
|
|
manifest = Manifest.new().tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
original: seq[byte]
|
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
|
|
|
let
|
2022-03-18 19:50:53 +00:00
|
|
|
blk = bt.Block.new(chunk).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
original &= chunk
|
|
|
|
check await localStore.putBlock(blk)
|
2022-03-14 16:06:36 +00:00
|
|
|
manifest.add(blk.cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
let
|
2022-03-18 19:50:53 +00:00
|
|
|
manifestBlock = bt.Block.new(
|
2022-01-13 00:42:18 +00:00
|
|
|
manifest.encode().tryGet(),
|
2022-03-14 16:06:36 +00:00
|
|
|
codec = DagPBCodec)
|
|
|
|
.tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
check await localStore.putBlock(manifestBlock)
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
let stream = (await node.retrieve(manifestBlock.cid)).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
var data: seq[byte]
|
2022-03-30 02:43:35 +00:00
|
|
|
while not stream.atEof:
|
2022-01-10 15:32:56 +00:00
|
|
|
var
|
2022-03-15 18:47:31 +00:00
|
|
|
buf = newSeq[byte](BlockSize)
|
2022-03-30 02:43:35 +00:00
|
|
|
res = await stream.readOnce(addr buf[0], BlockSize div 2)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
buf.setLen(res)
|
2022-03-30 02:43:35 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
data &= buf
|
|
|
|
|
|
|
|
check data == original
|
|
|
|
|
|
|
|
test "Retrieve One Block":
|
|
|
|
let
|
|
|
|
testString = "Block 1"
|
2022-03-18 19:50:53 +00:00
|
|
|
blk = bt.Block.new(testString.toBytes).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
check (await localStore.putBlock(blk))
|
2022-03-30 02:43:35 +00:00
|
|
|
let stream = (await node.retrieve(blk.cid)).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
var data = newSeq[byte](testString.len)
|
|
|
|
await stream.readExactly(addr data[0], data.len)
|
|
|
|
check string.fromBytes(data) == testString
|