reorg files and fix tests

This commit is contained in:
Dmitriy Ryajov 2021-08-27 11:45:15 -06:00
parent 1c05554f7c
commit 1834d298c9
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
5 changed files with 33 additions and 44 deletions

View File

@ -1,12 +0,0 @@
## Nim-Dagger
## Copyright (c) 2021 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import ./blockstream/[blockstream, chunkedblockstream]
export blockstream, chunkedblockstream

View File

@ -35,6 +35,17 @@ func new*(
data: @data)
func new*(
T: type Block,
data: openArray[byte] = [],
version = CIDv1,
hcodec = multiCodec("sha2-256"),
codec = multiCodec("raw")): ?!T =
let hash = MultiHash.digest($hcodec, data).get()
success Block(
cid: Cid.init(version, codec, hash).get(),
data: @data)
proc new*(
T: type Block,
cid: Cid,
data: openArray[byte] = [],

View File

@ -1,28 +0,0 @@
## Nim-Dagger
## Copyright (c) 2021 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
{.push raises: [Defect].}
import pkg/questionable
import pkg/questionable/results
import ./blockstream
import ./chunker
type
ChunkedBlockStreamRef* = ref object of BlockStreamRef
chunker*: Chunker
method nextBlock*(c: ChunkedBlockStreamRef): ?!Block =
let data: seq[byte] = c.chunker.getBytes()
if data.len > 0:
return success Block.new(data)
proc new*(T: type ChunkedBlockStreamRef, chunker: Chunker): T =
T(chunker: chunker)

View File

@ -26,10 +26,8 @@ proc hashBytes*(mh: MultiHash): seq[byte] =
mh.data.buffer[mh.dpos..(mh.dpos + mh.size - 1)]
proc hashBytes*(b: Block): seq[byte] =
without mh =? b.cid.mhash:
return
mh.hashBytes()
if mh =? b.cid.mhash:
mh.hashBytes()
method nextBlock*(d: BlockSetRef): ?!Block =
d.stream.nextBlock()

20
tests/dagger/dataset.nim Normal file
View File

@ -0,0 +1,20 @@
import std/sequtils
import pkg/chronos
import pkg/asynctest
import pkg/stew/results
import pkg/dagger/chunker
import pkg/dagger/merkletree
import pkg/stew/byteutils
import pkg/dagger/p2p/rng
import pkg/dagger/blocktype as bt
suite "Data set":
test "Make from Blocks":
let
chunker = newRandomChunker(Rng.instance(), size = 256*3, chunkSize = 256)
blocks = chunker.mapIt( bt.Block.new(it) )
let merkle = MerkleTreeRef.fromBlocks(blocks)