2024-01-15 10:45:04 -06:00
|
|
|
import std/os
|
|
|
|
|
import std/math
|
|
|
|
|
import std/importutils
|
|
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
|
import pkg/datastore
|
2024-06-21 00:46:06 +02:00
|
|
|
import pkg/datastore/typedds
|
2024-01-15 10:45:04 -06:00
|
|
|
import pkg/questionable/results
|
2025-02-12 23:26:26 +05:30
|
|
|
import pkg/taskpools
|
2024-01-15 10:45:04 -06:00
|
|
|
|
|
|
|
|
import pkg/codexdht/discv5/protocol as discv5
|
|
|
|
|
|
2026-02-19 15:59:15 +11:00
|
|
|
import pkg/storage/logutils
|
|
|
|
|
import pkg/storage/stores
|
|
|
|
|
import pkg/storage/clock
|
|
|
|
|
import pkg/storage/systemclock
|
|
|
|
|
import pkg/storage/blockexchange
|
|
|
|
|
import pkg/storage/chunker
|
|
|
|
|
import pkg/storage/manifest
|
|
|
|
|
import pkg/storage/discovery
|
|
|
|
|
import pkg/storage/blocktype as bt
|
|
|
|
|
|
|
|
|
|
import pkg/storage/node {.all.}
|
2024-01-15 10:45:04 -06:00
|
|
|
|
2024-01-29 21:03:51 +01:00
|
|
|
import ../../asynctest
|
2024-01-15 10:45:04 -06:00
|
|
|
import ../examples
|
|
|
|
|
import ../helpers
|
|
|
|
|
import ../helpers/mockclock
|
2025-02-11 16:00:05 -03:00
|
|
|
import ../slots/helpers
|
2024-01-15 10:45:04 -06:00
|
|
|
|
|
|
|
|
import ./helpers
|
|
|
|
|
|
2026-02-19 15:59:15 +11:00
|
|
|
privateAccess(StorageNodeRef) # enable access to private fields
|
2024-01-15 10:45:04 -06:00
|
|
|
|
|
|
|
|
asyncchecksuite "Test Node - Basic":
|
|
|
|
|
setupAndTearDown()
|
2026-01-15 19:13:14 +01:00
|
|
|
var taskPool: Taskpool
|
2024-01-15 10:45:04 -06:00
|
|
|
|
2024-08-26 15:18:59 +02:00
|
|
|
setup:
|
2026-01-15 19:13:14 +01:00
|
|
|
taskPool = Taskpool.new()
|
2024-08-26 15:18:59 +02:00
|
|
|
await node.start()
|
|
|
|
|
|
2026-01-15 19:13:14 +01:00
|
|
|
teardown:
|
|
|
|
|
taskPool.shutdown()
|
|
|
|
|
|
2024-01-15 10:45:04 -06:00
|
|
|
test "Fetch Manifest":
|
|
|
|
|
let
|
|
|
|
|
manifest = await storeDataGetManifest(localStore, chunker)
|
|
|
|
|
|
|
|
|
|
manifestBlock =
|
|
|
|
|
bt.Block.new(manifest.encode().tryGet(), codec = ManifestCodec).tryGet()
|
|
|
|
|
|
|
|
|
|
(await localStore.putBlock(manifestBlock)).tryGet()
|
|
|
|
|
|
|
|
|
|
let fetched = (await node.fetchManifest(manifestBlock.cid)).tryGet()
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
fetched == manifest
|
|
|
|
|
|
2026-02-18 14:44:16 +02:00
|
|
|
test "Fetch Dataset":
|
2024-01-15 10:45:04 -06:00
|
|
|
let manifest = await storeDataGetManifest(localStore, chunker)
|
|
|
|
|
|
2026-02-18 14:44:16 +02:00
|
|
|
# Fetch the dataset using the download manager
|
|
|
|
|
(await node.fetchDatasetAsync(manifest, fetchLocal = true)).tryGet()
|
|
|
|
|
|
|
|
|
|
# Verify all blocks are accessible from local store
|
|
|
|
|
for i in 0 ..< manifest.blocksCount:
|
|
|
|
|
let blk = (await localStore.getBlock(manifest.treeCid, i)).tryGet()
|
|
|
|
|
check blk.data[].len > 0
|
2025-03-31 06:57:55 +02:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
test "Should store Data Stream":
|
2024-01-15 10:45:04 -06:00
|
|
|
let
|
|
|
|
|
stream = BufferStream.new()
|
|
|
|
|
storeFut = node.store(stream)
|
|
|
|
|
# Let's check that node.store can correctly rechunk these odd chunks
|
2025-02-24 15:01:23 -06:00
|
|
|
oddChunker = FileChunker.new(file = file, chunkSize = 1024.NBytes, pad = false)
|
|
|
|
|
# don't pad, so `node.store` gets the correct size
|
2024-01-15 10:45:04 -06:00
|
|
|
|
|
|
|
|
var original: seq[byte]
|
|
|
|
|
try:
|
|
|
|
|
while (let chunk = await oddChunker.getBytes(); chunk.len > 0):
|
|
|
|
|
original &= chunk
|
|
|
|
|
await stream.pushData(chunk)
|
|
|
|
|
finally:
|
|
|
|
|
await stream.pushEof()
|
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
manifestCid = (await storeFut).tryGet()
|
|
|
|
|
manifestBlock = (await localStore.getBlock(manifestCid)).tryGet()
|
|
|
|
|
localManifest = Manifest.decode(manifestBlock).tryGet()
|
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
var data: seq[byte]
|
|
|
|
|
for i in 0 ..< localManifest.blocksCount:
|
|
|
|
|
let blk = (await localStore.getBlock(localManifest.treeCid, i)).tryGet()
|
2026-02-18 14:44:16 +02:00
|
|
|
data &= blk.data[]
|
2025-02-24 15:01:23 -06:00
|
|
|
|
|
|
|
|
data.setLen(localManifest.datasetSize.int) # truncate data to original size
|
2024-01-15 10:45:04 -06:00
|
|
|
check:
|
|
|
|
|
data.len == original.len
|
|
|
|
|
sha256.digest(data) == sha256.digest(original)
|
|
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
test "Should retrieve a Data Stream":
|
|
|
|
|
let
|
|
|
|
|
manifest = await storeDataGetManifest(localStore, chunker)
|
|
|
|
|
manifestBlk =
|
|
|
|
|
bt.Block.new(data = manifest.encode().tryGet, codec = ManifestCodec).tryGet()
|
|
|
|
|
|
|
|
|
|
(await localStore.putBlock(manifestBlk)).tryGet()
|
|
|
|
|
let data = await ((await node.retrieve(manifestBlk.cid)).tryGet()).drain()
|
|
|
|
|
|
|
|
|
|
var storedData: seq[byte]
|
|
|
|
|
for i in 0 ..< manifest.blocksCount:
|
|
|
|
|
let blk = (await localStore.getBlock(manifest.treeCid, i)).tryGet()
|
2026-02-18 14:44:16 +02:00
|
|
|
storedData &= blk.data[]
|
2025-02-24 15:01:23 -06:00
|
|
|
|
|
|
|
|
storedData.setLen(manifest.datasetSize.int) # truncate data to original size
|
|
|
|
|
check:
|
|
|
|
|
storedData == data
|
|
|
|
|
|
2024-01-15 10:45:04 -06:00
|
|
|
test "Retrieve One Block":
|
|
|
|
|
let
|
|
|
|
|
testString = "Block 1"
|
|
|
|
|
blk = bt.Block.new(testString.toBytes).tryGet()
|
|
|
|
|
|
|
|
|
|
(await localStore.putBlock(blk)).tryGet()
|
|
|
|
|
let stream = (await node.retrieve(blk.cid)).tryGet()
|
|
|
|
|
defer:
|
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
|
|
var data = newSeq[byte](testString.len)
|
|
|
|
|
await stream.readExactly(addr data[0], data.len)
|
|
|
|
|
check string.fromBytes(data) == testString
|
|
|
|
|
|
2025-02-11 16:00:05 -03:00
|
|
|
test "Should delete a single block":
|
|
|
|
|
let randomBlock = bt.Block.new("Random block".toBytes).tryGet()
|
|
|
|
|
(await localStore.putBlock(randomBlock)).tryGet()
|
|
|
|
|
check (await randomBlock.cid in localStore) == true
|
|
|
|
|
|
|
|
|
|
(await node.delete(randomBlock.cid)).tryGet()
|
|
|
|
|
check (await randomBlock.cid in localStore) == false
|
|
|
|
|
|
|
|
|
|
test "Should delete an entire dataset":
|
|
|
|
|
let
|
|
|
|
|
blocks = await makeRandomBlocks(datasetSize = 2048, blockSize = 256'nb)
|
|
|
|
|
manifest = await storeDataGetManifest(localStore, blocks)
|
|
|
|
|
manifestBlock = (await store.storeManifest(manifest)).tryGet()
|
|
|
|
|
manifestCid = manifestBlock.cid
|
|
|
|
|
|
|
|
|
|
check await manifestCid in localStore
|
|
|
|
|
for blk in blocks:
|
|
|
|
|
check await blk.cid in localStore
|
|
|
|
|
|
|
|
|
|
(await node.delete(manifestCid)).tryGet()
|
|
|
|
|
|
|
|
|
|
check not await manifestCid in localStore
|
|
|
|
|
for blk in blocks:
|
|
|
|
|
check not (await blk.cid in localStore)
|
2025-11-02 08:32:47 +04:00
|
|
|
|
|
|
|
|
test "Should return true when a cid is already in the local store":
|
|
|
|
|
let
|
|
|
|
|
blocks = await makeRandomBlocks(datasetSize = 1024, blockSize = 256'nb)
|
|
|
|
|
manifest = await storeDataGetManifest(localStore, blocks)
|
|
|
|
|
manifestBlock = (await store.storeManifest(manifest)).tryGet()
|
|
|
|
|
manifestCid = manifestBlock.cid
|
|
|
|
|
|
|
|
|
|
check (await node.hasLocalBlock(manifestCid)) == true
|
|
|
|
|
|
|
|
|
|
test "Should returns false when a cid is not in the local store":
|
|
|
|
|
let randomBlock = bt.Block.new("Random block".toBytes).tryGet()
|
|
|
|
|
|
|
|
|
|
check (await node.hasLocalBlock(randomBlock.cid)) == false
|