mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-05-18 09:19:43 +00:00
adding basic test for treehash algo
This commit is contained in:
parent
ad63969f20
commit
9ffd64ebed
@ -2,9 +2,22 @@ import pkg/libp2p/varint
|
|||||||
|
|
||||||
import pkg/dagger/chunker
|
import pkg/dagger/chunker
|
||||||
import pkg/dagger/blocktype
|
import pkg/dagger/blocktype
|
||||||
|
import pkg/dagger/blockstream
|
||||||
|
|
||||||
|
import pkg/questionable
|
||||||
|
import pkg/questionable/results
|
||||||
|
|
||||||
export chunker
|
export chunker
|
||||||
|
|
||||||
|
type
|
||||||
|
TestStreamProc* = proc(): ?!Block {.raises: [Defect].}
|
||||||
|
|
||||||
|
TestStream* = ref object of BlockStreamRef
|
||||||
|
handler*: TestStreamProc
|
||||||
|
|
||||||
|
method nextBlock*(b: TestStream): ?!Block =
|
||||||
|
b.handler()
|
||||||
|
|
||||||
proc lenPrefix*(msg: openArray[byte]): seq[byte] =
|
proc lenPrefix*(msg: openArray[byte]): seq[byte] =
|
||||||
## Write `msg` with a varint-encoded length prefix
|
## Write `msg` with a varint-encoded length prefix
|
||||||
##
|
##
|
||||||
|
|||||||
@ -12,17 +12,41 @@ import pkg/dagger/blocktype as bt
|
|||||||
import pkg/dagger/blockstream
|
import pkg/dagger/blockstream
|
||||||
import pkg/dagger/blockset
|
import pkg/dagger/blockset
|
||||||
|
|
||||||
|
import ./helpers
|
||||||
|
|
||||||
suite "Data set":
|
suite "Data set":
|
||||||
test "Make from Blocks":
|
test "Should produce valid tree hash checksum":
|
||||||
let
|
let
|
||||||
blockStream = ChunkedBlockStreamRef.new(
|
blocks = @[
|
||||||
newRandomChunker(Rng.instance(), size = 256 * 7, chunkSize = 256))
|
!Block.new("Block 1".toBytes),
|
||||||
|
!Block.new("Block 2".toBytes),
|
||||||
|
!Block.new("Block 3".toBytes),
|
||||||
|
!Block.new("Block 4".toBytes),
|
||||||
|
!Block.new("Block 5".toBytes),
|
||||||
|
!Block.new("Block 6".toBytes),
|
||||||
|
!Block.new("Block 7".toBytes),
|
||||||
|
]
|
||||||
|
|
||||||
|
checksum = @[byte(43), 2, 105, 202, 45, 227,
|
||||||
|
178, 211, 83, 246, 56, 250, 210,
|
||||||
|
160, 210, 98, 123, 87, 139, 157,
|
||||||
|
188, 221, 252, 255, 17, 11, 79,
|
||||||
|
85, 220, 161, 238, 108]
|
||||||
|
|
||||||
|
var idx = 0
|
||||||
|
proc nextBlockHandler(): ?!Block =
|
||||||
|
let blk = if idx < blocks.len: blocks[idx] else: return
|
||||||
|
idx.inc()
|
||||||
|
return success blk
|
||||||
|
|
||||||
|
let
|
||||||
|
blockStream = TestStream(handler: nextBlockHandler)
|
||||||
blockSet = BlockSetRef.new(stream = blockStream)
|
blockSet = BlockSetRef.new(stream = blockStream)
|
||||||
|
|
||||||
let res = blockSet.treeHash()
|
let res = blockSet.treeHash()
|
||||||
check res.isOK
|
check res.isOK
|
||||||
if h =? res:
|
if h =? res:
|
||||||
echo h.hashBytes()
|
check h.hashBytes() == checksum
|
||||||
return
|
return
|
||||||
|
|
||||||
echo res.error.msg
|
check false
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user