use default hash codec
This commit is contained in:
parent
ff17551e32
commit
8f0102f0eb
|
@ -18,7 +18,6 @@ import std/sugar
|
||||||
import std/algorithm
|
import std/algorithm
|
||||||
import std/tables
|
import std/tables
|
||||||
|
|
||||||
import pkg/chronicles
|
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
import pkg/questionable/results
|
import pkg/questionable/results
|
||||||
import pkg/libp2p/[cid, multicodec, multihash]
|
import pkg/libp2p/[cid, multicodec, multihash]
|
||||||
|
@ -36,6 +35,10 @@ export merkletree
|
||||||
logScope:
|
logScope:
|
||||||
topics = "codex merkletree"
|
topics = "codex merkletree"
|
||||||
|
|
||||||
|
const
|
||||||
|
DatasetRootCodec* = multiCodec("codex-root") # TODO: move to blocktype
|
||||||
|
BlockCodec* = multiCodec("raw") # TODO: fix multicodec to `codex-block` and move to blocktype
|
||||||
|
|
||||||
type
|
type
|
||||||
ByteTreeKey* {.pure.} = enum
|
ByteTreeKey* {.pure.} = enum
|
||||||
KeyNone = 0x0.byte
|
KeyNone = 0x0.byte
|
||||||
|
@ -135,11 +138,9 @@ func getLeafCid*(
|
||||||
|
|
||||||
let
|
let
|
||||||
leaf = self.leaves[i]
|
leaf = self.leaves[i]
|
||||||
|
mhash = ? MultiHash.init($self.mcodec, leaf).mapFailure
|
||||||
|
|
||||||
Cid.init(
|
Cid.init(version, dataCodec, mhash).mapFailure
|
||||||
CidVersion.CIDv1,
|
|
||||||
dataCodec,
|
|
||||||
? MultiHash.init(self.mcodec, self.root).mapFailure).mapFailure
|
|
||||||
|
|
||||||
proc `==`*(a, b: CodexMerkleTree): bool =
|
proc `==`*(a, b: CodexMerkleTree): bool =
|
||||||
(a.mcodec == b.mcodec) and
|
(a.mcodec == b.mcodec) and
|
||||||
|
@ -152,6 +153,15 @@ proc `==`*(a, b: CodexMerkleProof): bool =
|
||||||
(a.path == b.path) and
|
(a.path == b.path) and
|
||||||
(a.index == b.index)
|
(a.index == b.index)
|
||||||
|
|
||||||
|
proc `$`*(self: CodexMerkleTree): string =
|
||||||
|
"CodexMerkleTree(" & $self.mcodec & ", " & $self.leavesCount & ")"
|
||||||
|
|
||||||
|
proc `$`*(self: CodexMerkleProof): string =
|
||||||
|
"CodexMerkleProof(" &
|
||||||
|
$self.mcodec & ", " &
|
||||||
|
$self.nleaves & ", " &
|
||||||
|
$self.index & ")"
|
||||||
|
|
||||||
func compress*(
|
func compress*(
|
||||||
x, y: openArray[byte],
|
x, y: openArray[byte],
|
||||||
key: ByteTreeKey,
|
key: ByteTreeKey,
|
||||||
|
@ -165,7 +175,7 @@ func compress*(
|
||||||
|
|
||||||
func init*(
|
func init*(
|
||||||
_: type CodexMerkleTree,
|
_: type CodexMerkleTree,
|
||||||
mcodec: MultiCodec,
|
mcodec: MultiCodec = multiCodec("sha2-256"),
|
||||||
leaves: openArray[ByteHash]): ?!CodexMerkleTree =
|
leaves: openArray[ByteHash]): ?!CodexMerkleTree =
|
||||||
|
|
||||||
if leaves.len == 0:
|
if leaves.len == 0:
|
||||||
|
@ -213,7 +223,7 @@ func init*(
|
||||||
|
|
||||||
proc fromNodes*(
|
proc fromNodes*(
|
||||||
_: type CodexMerkleTree,
|
_: type CodexMerkleTree,
|
||||||
mcodec: MultiCodec,
|
mcodec: MultiCodec = multiCodec("sha2-256"),
|
||||||
nodes: openArray[ByteHash],
|
nodes: openArray[ByteHash],
|
||||||
nleaves: int): ?!CodexMerkleTree =
|
nleaves: int): ?!CodexMerkleTree =
|
||||||
|
|
||||||
|
@ -248,7 +258,7 @@ proc fromNodes*(
|
||||||
|
|
||||||
func init*(
|
func init*(
|
||||||
_: type CodexMerkleProof,
|
_: type CodexMerkleProof,
|
||||||
mcodec: MultiCodec,
|
mcodec: MultiCodec = multiCodec("sha2-256"),
|
||||||
index: int,
|
index: int,
|
||||||
nleaves: int,
|
nleaves: int,
|
||||||
nodes: openArray[ByteHash]): ?!CodexMerkleProof =
|
nodes: openArray[ByteHash]): ?!CodexMerkleProof =
|
||||||
|
|
Loading…
Reference in New Issue