2022-01-10 15:32:56 +00:00
|
|
|
import std/os
|
|
|
|
import std/options
|
|
|
|
|
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
|
|
|
|
import pkg/nitro
|
|
|
|
import pkg/libp2p
|
|
|
|
|
|
|
|
import pkg/dagger/stores
|
|
|
|
import pkg/dagger/blockexchange
|
|
|
|
import pkg/dagger/chunker
|
|
|
|
import pkg/dagger/node
|
|
|
|
import pkg/dagger/manifest
|
|
|
|
import pkg/dagger/blocktype as bt
|
|
|
|
|
|
|
|
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
|
|
|
|
localStore: MemoryStore
|
|
|
|
engine: BlockExcEngine
|
|
|
|
store: NetworkStore
|
|
|
|
node: DaggerNodeRef
|
|
|
|
|
|
|
|
setup:
|
|
|
|
file = open(path.splitFile().dir /../ "fixtures" / "test.jpg")
|
|
|
|
chunker = FileChunker.new(file = file)
|
|
|
|
switch = newStandardSwitch()
|
|
|
|
wallet = WalletRef.new(EthPrivateKey.random())
|
|
|
|
network = BlockExcNetwork.new(switch)
|
|
|
|
localStore = MemoryStore.new()
|
|
|
|
engine = BlockExcEngine.new(localStore, wallet, network)
|
|
|
|
store = NetworkStore.new(engine, localStore)
|
|
|
|
node = DaggerNodeRef.new(switch, store, engine)
|
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
close(file)
|
|
|
|
await node.stop()
|
|
|
|
|
|
|
|
test "Store Data Stream":
|
|
|
|
let
|
|
|
|
stream = BufferStream.new()
|
|
|
|
storeFut = node.store(stream)
|
|
|
|
|
|
|
|
var
|
2022-01-13 00:26:52 +00:00
|
|
|
manifest = BlocksManifest.init().tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
await stream.pushData(chunk)
|
2022-01-13 00:26:52 +00:00
|
|
|
manifest.put(bt.Block.init(chunk).tryGet().cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
finally:
|
|
|
|
await stream.pushEof()
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
let
|
2022-01-13 00:26:52 +00:00
|
|
|
manifestCid = (await storeFut).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
manifestCid in localStore
|
|
|
|
|
|
|
|
var
|
2022-01-13 00:26:52 +00:00
|
|
|
manifestBlock = (await localStore.getBlock(manifestCid)).tryGet()
|
|
|
|
localManifest = BlocksManifest.init(manifestBlock).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
manifest.len == localManifest.len
|
|
|
|
manifest.cid == localManifest.cid
|
|
|
|
|
|
|
|
test "Retrieve Data Stream":
|
|
|
|
var
|
2022-01-13 00:26:52 +00:00
|
|
|
manifest = BlocksManifest.init().tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
original: seq[byte]
|
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
|
|
|
let
|
2022-01-13 00:26:52 +00:00
|
|
|
blk = bt.Block.init(chunk).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
original &= chunk
|
|
|
|
check await localStore.putBlock(blk)
|
|
|
|
manifest.put(blk.cid)
|
|
|
|
|
|
|
|
let
|
|
|
|
manifestBlock = bt.Block.init(
|
2022-01-13 00:26:52 +00:00
|
|
|
manifest.encode().tryGet(),
|
|
|
|
codec = ManifestCodec).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
check await localStore.putBlock(manifestBlock)
|
|
|
|
|
|
|
|
let stream = BufferStream.new()
|
|
|
|
check (await node.retrieve(stream, manifestBlock.cid)).isOk
|
|
|
|
|
|
|
|
var data: seq[byte]
|
|
|
|
while true:
|
|
|
|
var
|
|
|
|
buf = newSeq[byte](FileChunkSize)
|
|
|
|
res = await stream.readOnce(addr buf[0], buf.len)
|
|
|
|
|
|
|
|
if res <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
buf.setLen(res)
|
|
|
|
data &= buf
|
|
|
|
|
|
|
|
check data == original
|
|
|
|
|
|
|
|
test "Retrieve One Block":
|
|
|
|
let
|
|
|
|
testString = "Block 1"
|
2022-01-13 00:26:52 +00:00
|
|
|
blk = bt.Block.init(testString.toBytes).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
stream = BufferStream.new()
|
|
|
|
|
|
|
|
check (await localStore.putBlock(blk))
|
|
|
|
check (await node.retrieve(stream, blk.cid)).isOk
|
|
|
|
|
|
|
|
var data = newSeq[byte](testString.len)
|
|
|
|
await stream.readExactly(addr data[0], data.len)
|
|
|
|
check string.fromBytes(data) == testString
|