diff --git a/dagger/blockstream.nim b/dagger/blockstream.nim deleted file mode 100644 index cb7601ce..00000000 --- a/dagger/blockstream.nim +++ /dev/null @@ -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 diff --git a/dagger/blocktype.nim b/dagger/blocktype.nim index c4e65afd..e48bce61 100644 --- a/dagger/blocktype.nim +++ b/dagger/blocktype.nim @@ -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] = [], diff --git a/dagger/chunkedblockstream.nim b/dagger/chunkedblockstream.nim deleted file mode 100644 index 49b6b6f5..00000000 --- a/dagger/chunkedblockstream.nim +++ /dev/null @@ -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) diff --git a/dagger/dataset.nim b/dagger/dataset.nim index 7656bc06..48a23c03 100644 --- a/dagger/dataset.nim +++ b/dagger/dataset.nim @@ -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() diff --git a/tests/dagger/dataset.nim b/tests/dagger/dataset.nim new file mode 100644 index 00000000..ab01ef03 --- /dev/null +++ b/tests/dagger/dataset.nim @@ -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) +