rename dataset to blockset

This commit is contained in:
Dmitriy Ryajov 2021-08-27 13:48:23 -06:00
parent cfa6161952
commit baa5fd78a0
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4

View File

@ -9,25 +9,24 @@
{.push raises: [Defect].}
import std/sequtils
import pkg/libp2p
import pkg/stew/byteutils
import pkg/questionable
import pkg/questionable/results
import ./blockstream
export blockstream
type
BlockSetRef* = ref object of BlockStreamRef
stream*: BlockStreamRef
hcodec*: MultiCodec
proc hashBytes*(mh: MultiHash): seq[byte] =
mh.data.buffer[mh.dpos..(mh.dpos + mh.size - 1)]
proc hashBytes*(b: Block): seq[byte] =
if mh =? b.cid.mhash:
mh.hashBytes()
return mh.hashBytes()
method nextBlock*(d: BlockSetRef): ?!Block =
d.stream.nextBlock()
@ -35,12 +34,11 @@ method nextBlock*(d: BlockSetRef): ?!Block =
proc treeHash*(d: BlockSetRef): ?!MultiHash =
var
stack: seq[seq[byte]]
codec: MultiCodec = InvalidMultiCodec
while true:
let (blk1, blk2) = (d.nextBlock().option, d.nextBlock().option)
if blk1.isNone and blk2.isNone and stack.len == 1:
let res = MultiHash.init($codec, stack[0])
let res = MultiHash.digest($d.hcodec, stack[0])
if mh =? res:
return success mh
@ -49,14 +47,16 @@ proc treeHash*(d: BlockSetRef): ?!MultiHash =
if blk1.isSome: stack.add((!blk1).hashBytes())
if blk2.isSome: stack.add((!blk2).hashBytes())
codec = if codec == InvalidMultiCodec: (!blk1).cid.mcodec else: codec
while (stack.len mod 2) == 0:
while stack.len > 1:
let (b1, b2) = (stack.pop(), stack.pop())
let res = MultiHash.init($codec, b1 & b2)
let res = MultiHash.digest($d.hcodec, b1 & b2)
if mh =? res:
stack.add(mh.hashBytes())
else:
return failure($res.error)
proc new*(T: type BlockSetRef, blockStream: BlockStreamref) =
discard
func new*(
T: type BlockSetRef,
stream: BlockStreamRef,
hcodec: MultiCodec = multiCodec("sha2-256")): T =
T(stream: stream, hcodec: hcodec)