mirror of
https://github.com/status-im/nim-dagger.git
synced 2025-01-12 07:34:08 +00:00
2fb39ca4a3
* use PeerInfo in event handlers * use CidV1 and raw multicodec as default * add block stream abstraction * raises defect * adding dataset abstraction * move blockstream into own dir * reorg files and fix tests * rename dataset to blockset * wip * wip * adding basic test for treehash algo * run blockset tests along with with the rest * remove obsolete contents * fix chunker tests * rename bitswap and move to stores * rename bitwsap to blockexc and move to stores * moare project structure reorg
54 lines
1.2 KiB
Nim
54 lines
1.2 KiB
Nim
## 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/libp2p
|
|
import pkg/questionable
|
|
import pkg/questionable/results
|
|
import pkg/stew/byteutils
|
|
|
|
type
|
|
Block* = object of RootObj
|
|
cid*: Cid
|
|
data*: seq[byte]
|
|
|
|
proc `$`*(b: Block): string =
|
|
result &= "cid: " & $b.cid
|
|
result &= "\ndata: " & string.fromBytes(b.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)
|
|
|
|
func new*(
|
|
T: type Block,
|
|
cid: Cid,
|
|
data: openArray[byte] = [],
|
|
verify: bool = false): ?!T =
|
|
let res = Block.new(
|
|
data,
|
|
cid.cidver,
|
|
cid.mhash.get().mcodec,
|
|
cid.mcodec
|
|
)
|
|
|
|
if b =? res:
|
|
if verify and cid != b.cid:
|
|
return failure("The suplied Cid doesn't match the data!")
|
|
|
|
res
|