2023-12-21 06:41:43 +00:00
|
|
|
import std/unittest
|
|
|
|
import std/sequtils
|
|
|
|
import std/tables
|
|
|
|
|
2024-10-28 15:52:21 +00:00
|
|
|
import pkg/chronos
|
2023-12-21 06:41:43 +00:00
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
import pkg/nimcrypto/sha2
|
|
|
|
import pkg/libp2p
|
|
|
|
|
2023-12-22 12:04:01 +00:00
|
|
|
import pkg/codex/codextypes
|
2023-12-21 06:41:43 +00:00
|
|
|
import pkg/codex/merkletree
|
2024-01-11 16:45:23 +00:00
|
|
|
import pkg/codex/utils/digest
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
import ./helpers
|
|
|
|
import ./generictreetests
|
|
|
|
|
|
|
|
# TODO: Generalize to other hashes
|
|
|
|
|
|
|
|
const
|
|
|
|
data =
|
2024-10-28 15:52:21 +00:00
|
|
|
@[
|
2023-12-21 06:41:43 +00:00
|
|
|
"00000000000000000000000000000001".toBytes,
|
|
|
|
"00000000000000000000000000000002".toBytes,
|
|
|
|
"00000000000000000000000000000003".toBytes,
|
|
|
|
"00000000000000000000000000000004".toBytes,
|
|
|
|
"00000000000000000000000000000005".toBytes,
|
|
|
|
"00000000000000000000000000000006".toBytes,
|
|
|
|
"00000000000000000000000000000007".toBytes,
|
|
|
|
"00000000000000000000000000000008".toBytes,
|
|
|
|
"00000000000000000000000000000009".toBytes,
|
|
|
|
"00000000000000000000000000000010".toBytes,
|
|
|
|
]
|
2023-12-22 12:04:01 +00:00
|
|
|
sha256 = Sha256HashCodec
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
suite "Test CodexTree":
|
2024-10-28 15:52:21 +00:00
|
|
|
proc CodexTreeInit(leaves: seq[MultiHash]): ?!CodexTree =
|
|
|
|
waitFor CodexTree.init(leaves)
|
|
|
|
|
|
|
|
proc CodexTreeInit(leaves: seq[Cid]): ?!CodexTree =
|
|
|
|
waitFor CodexTree.init(leaves)
|
|
|
|
|
|
|
|
proc CodexTreeInit(mcodec: MultiCodec, leaves: seq[ByteHash]): ?!CodexTree =
|
|
|
|
waitFor CodexTree.init(mcodec, leaves)
|
|
|
|
|
2023-12-21 06:41:43 +00:00
|
|
|
test "Cannot init tree without any multihash leaves":
|
|
|
|
check:
|
2024-10-28 15:52:21 +00:00
|
|
|
CodexTreeInit(leaves = newSeq[MultiHash]()).isErr
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
test "Cannot init tree without any cid leaves":
|
|
|
|
check:
|
2024-10-28 15:52:21 +00:00
|
|
|
CodexTreeInit(leaves = newSeq[Cid]()).isErr
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
test "Cannot init tree without any byte leaves":
|
|
|
|
check:
|
2024-10-28 15:52:21 +00:00
|
|
|
CodexTreeInit(sha256, leaves = newSeq[ByteHash]()).isErr
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
test "Should build tree from multihash leaves":
|
|
|
|
var
|
|
|
|
expectedLeaves = data.mapIt( MultiHash.digest($sha256, it).tryGet() )
|
|
|
|
|
2024-10-28 15:52:21 +00:00
|
|
|
var tree = CodexTreeInit(leaves = expectedLeaves)
|
2023-12-21 06:41:43 +00:00
|
|
|
check:
|
|
|
|
tree.isOk
|
2024-01-08 22:52:46 +00:00
|
|
|
tree.get().leaves == expectedLeaves.mapIt( it.digestBytes )
|
2023-12-21 06:41:43 +00:00
|
|
|
tree.get().mcodec == sha256
|
|
|
|
|
|
|
|
test "Should build tree from cid leaves":
|
|
|
|
var
|
|
|
|
expectedLeaves = data.mapIt(
|
|
|
|
Cid.init(
|
|
|
|
CidVersion.CIDv1,
|
|
|
|
BlockCodec,
|
|
|
|
MultiHash.digest($sha256, it).tryGet
|
|
|
|
).tryGet )
|
|
|
|
|
|
|
|
let
|
2024-10-28 15:52:21 +00:00
|
|
|
tree = CodexTreeInit(leaves = expectedLeaves)
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
tree.isOk
|
2024-01-08 22:52:46 +00:00
|
|
|
tree.get().leaves == expectedLeaves.mapIt( it.mhash.tryGet.digestBytes )
|
2023-12-21 06:41:43 +00:00
|
|
|
tree.get().mcodec == sha256
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
test "Should build from raw digestbytes (should not hash leaves)":
|
2023-12-21 06:41:43 +00:00
|
|
|
let
|
2024-10-28 15:52:21 +00:00
|
|
|
tree = CodexTreeInit(sha256, leaves = data).tryGet
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
tree.mcodec == sha256
|
|
|
|
tree.leaves == data
|
|
|
|
|
|
|
|
test "Should build from nodes":
|
|
|
|
let
|
2024-10-28 15:52:21 +00:00
|
|
|
tree = CodexTreeInit(sha256, leaves = data).tryGet
|
2023-12-21 06:41:43 +00:00
|
|
|
fromNodes = CodexTree.fromNodes(
|
|
|
|
nodes = toSeq(tree.nodes),
|
|
|
|
nleaves = tree.leavesCount).tryGet
|
|
|
|
|
|
|
|
check:
|
|
|
|
tree.mcodec == sha256
|
|
|
|
tree == fromNodes
|
|
|
|
|
|
|
|
let
|
2024-01-08 22:52:46 +00:00
|
|
|
mhash = sha256.mhash().tryGet
|
2023-12-21 06:41:43 +00:00
|
|
|
zero: seq[byte] = newSeq[byte](mhash.size)
|
|
|
|
compress = proc(x, y: seq[byte], key: ByteTreeKey): seq[byte] =
|
|
|
|
compress(x, y, key, mhash).tryGet
|
|
|
|
|
|
|
|
makeTree = proc(data: seq[seq[byte]]): CodexTree =
|
2024-10-28 15:52:21 +00:00
|
|
|
(waitFor CodexTree.init(sha256, leaves = data)).tryGet
|
2023-12-21 06:41:43 +00:00
|
|
|
|
|
|
|
testGenericTree(
|
|
|
|
"CodexTree",
|
|
|
|
@data,
|
|
|
|
zero,
|
|
|
|
compress,
|
|
|
|
makeTree)
|