2022-01-10 15:32:56 +00:00
|
|
|
import std/os
|
|
|
|
import std/options
|
2022-08-24 12:15:59 +00:00
|
|
|
import std/math
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/blockexchange
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/node
|
|
|
|
import pkg/codex/manifest
|
|
|
|
import pkg/codex/discovery
|
|
|
|
import pkg/codex/blocktype as bt
|
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
|
2022-05-19 19:56:03 +00:00
|
|
|
node: CodexNodeRef
|
2022-05-19 02:29:15 +00:00
|
|
|
blockDiscovery: Discovery
|
|
|
|
peerStore: PeerCtxStore
|
|
|
|
pendingBlocks: PendingBlocksManager
|
|
|
|
discovery: DiscoveryEngine
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
proc fetch(T: type Manifest, chunker: Chunker): Future[Manifest] {.async.} =
|
|
|
|
# Collect blocks from Chunker into Manifest
|
|
|
|
var
|
|
|
|
manifest = Manifest.new().tryGet()
|
|
|
|
|
|
|
|
while (
|
|
|
|
let chunk = await chunker.getBytes();
|
|
|
|
chunk.len > 0):
|
|
|
|
|
|
|
|
let blk = bt.Block.new(chunk).tryGet()
|
|
|
|
(await localStore.putBlock(blk)).tryGet()
|
|
|
|
manifest.add(blk.cid)
|
|
|
|
|
|
|
|
return manifest
|
|
|
|
|
|
|
|
proc retrieve(cid: Cid): Future[seq[byte]] {.async.} =
|
|
|
|
# Retrieve an entire file contents by file Cid
|
|
|
|
let
|
|
|
|
oddChunkSize = math.trunc(BlockSize/1.359).int # Let's check that node.retrieve can correctly rechunk data
|
|
|
|
stream = (await node.retrieve(cid)).tryGet()
|
|
|
|
var
|
|
|
|
data: seq[byte]
|
|
|
|
|
|
|
|
while not stream.atEof:
|
|
|
|
var
|
|
|
|
buf = newSeq[byte](oddChunkSize)
|
|
|
|
res = await stream.readOnce(addr buf[0], oddChunkSize)
|
|
|
|
check res <= oddChunkSize
|
|
|
|
buf.setLen(res)
|
|
|
|
data &= buf
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
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-11-02 00:58:41 +00:00
|
|
|
blockDiscovery = Discovery.new(
|
|
|
|
switch.peerInfo.privateKey,
|
|
|
|
announceAddrs = @[MultiAddress.init("/ip4/127.0.0.1/tcp/0")
|
|
|
|
.expect("Should return multiaddress")])
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
discovery = DiscoveryEngine.new(localStore, peerStore, network, blockDiscovery, pendingBlocks)
|
|
|
|
engine = BlockExcEngine.new(localStore, wallet, network, discovery, peerStore, pendingBlocks)
|
2022-01-10 15:32:56 +00:00
|
|
|
store = NetworkStore.new(engine, localStore)
|
2022-08-09 04:29:06 +00:00
|
|
|
node = CodexNodeRef.new(switch, store, engine, nil, blockDiscovery) # TODO: pass `Erasure`
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
close(file)
|
|
|
|
await node.stop()
|
|
|
|
|
2022-07-28 17:44:59 +00:00
|
|
|
test "Fetch Manifest":
|
|
|
|
let
|
2022-08-24 12:15:59 +00:00
|
|
|
manifest = await Manifest.fetch(chunker)
|
|
|
|
|
2022-07-28 17:44:59 +00:00
|
|
|
manifestBlock = bt.Block.new(
|
|
|
|
manifest.encode().tryGet(),
|
|
|
|
codec = DagPBCodec
|
|
|
|
).tryGet()
|
|
|
|
|
|
|
|
(await localStore.putBlock(manifestBlock)).tryGet()
|
|
|
|
|
|
|
|
let
|
|
|
|
fetched = (await node.fetchManifest(manifestBlock.cid)).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
fetched.cid == manifest.cid
|
|
|
|
fetched.blocks == manifest.blocks
|
|
|
|
|
2022-07-29 20:04:12 +00:00
|
|
|
test "Block Batching":
|
|
|
|
let
|
2022-08-24 12:15:59 +00:00
|
|
|
manifest = await Manifest.fetch(chunker)
|
|
|
|
|
|
|
|
for batchSize in 1..12:
|
|
|
|
(await node.fetchBatched(
|
|
|
|
manifest,
|
|
|
|
batchSize = batchSize,
|
|
|
|
proc(blocks: seq[bt.Block]) {.gcsafe, async.} =
|
|
|
|
check blocks.len > 0 and blocks.len <= batchSize
|
|
|
|
)).tryGet()
|
2022-07-29 20:04:12 +00:00
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
test "Store and retrieve Data Stream":
|
2022-01-10 15:32:56 +00:00
|
|
|
let
|
|
|
|
stream = BufferStream.new()
|
|
|
|
storeFut = node.store(stream)
|
2022-10-06 20:01:25 +00:00
|
|
|
oddChunkSize = math.trunc(BlockSize/3.14).int # Let's check that node.store can correctly rechunk these odd chunks
|
2022-08-24 12:15:59 +00:00
|
|
|
oddChunker = FileChunker.new(file = file, chunkSize = oddChunkSize, pad = false) # TODO: doesn't work with pad=tue
|
2022-01-10 15:32:56 +00:00
|
|
|
var
|
2022-08-24 12:15:59 +00:00
|
|
|
original: seq[byte]
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while (
|
2022-08-24 12:15:59 +00:00
|
|
|
let chunk = await oddChunker.getBytes();
|
2022-01-10 15:32:56 +00:00
|
|
|
chunk.len > 0):
|
2022-08-24 12:15:59 +00:00
|
|
|
original &= chunk
|
2022-01-10 15:32:56 +00:00
|
|
|
await stream.pushData(chunk)
|
|
|
|
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:
|
2022-07-28 00:39:17 +00:00
|
|
|
(await localStore.hasBlock(manifestCid)).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-08-24 12:15:59 +00:00
|
|
|
let
|
2022-08-19 00:56:36 +00:00
|
|
|
manifestBlock = (await localStore.getBlock(manifestCid)).tryGet()
|
2022-07-29 20:04:12 +00:00
|
|
|
localManifest = Manifest.decode(manifestBlock).tryGet()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
let
|
2022-08-24 12:15:59 +00:00
|
|
|
data = await retrieve(manifestCid)
|
|
|
|
check:
|
|
|
|
data.len == localManifest.originalBytes
|
|
|
|
data.len == original.len
|
|
|
|
sha256.digest(data) == sha256.digest(original)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
(await localStore.putBlock(blk)).tryGet()
|
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
|