Replace root hash by root content id
This commit is contained in:
parent
096fe0e40d
commit
dd18f50ec6
|
@ -1,10 +1,10 @@
|
||||||
import pkg/libp2p/multihash
|
import pkg/libp2p
|
||||||
|
|
||||||
export multihash
|
|
||||||
|
|
||||||
type
|
type
|
||||||
MerkleDag* = object
|
MerkleDag* = object
|
||||||
data*: seq[byte]
|
data*: seq[byte]
|
||||||
|
|
||||||
proc rootHash*(dag: MerkleDag): MultiHash =
|
proc rootId*(dag: MerkleDag): Cid =
|
||||||
MultiHash.digest("sha2-256", dag.data).get()
|
let codec = multiCodec("dag-pb")
|
||||||
|
let hash = MultiHash.digest("sha2-256", dag.data).get()
|
||||||
|
Cid.init(CIDv0, codec, hash).get()
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
import std/tables
|
import std/tables
|
||||||
import std/hashes
|
import std/hashes
|
||||||
|
import pkg/libp2p
|
||||||
import ./merkledag
|
import ./merkledag
|
||||||
|
|
||||||
export merkledag
|
export merkledag
|
||||||
|
|
||||||
type
|
type
|
||||||
Repo* = ref object
|
Repo* = ref object
|
||||||
storage: Table[MultiHash, MerkleDag]
|
storage: Table[Cid, MerkleDag]
|
||||||
|
|
||||||
proc hash(multihash: MultiHash): Hash =
|
proc hash(id: Cid): Hash =
|
||||||
hash($multihash)
|
hash($id)
|
||||||
|
|
||||||
proc store*(repo: Repo, dag: MerkleDag) =
|
proc store*(repo: Repo, dag: MerkleDag) =
|
||||||
repo.storage[dag.rootHash] = dag
|
repo.storage[dag.rootId] = dag
|
||||||
|
|
||||||
proc contains*(repo: Repo, hash: MultiHash): bool =
|
proc contains*(repo: Repo, id: Cid): bool =
|
||||||
repo.storage.hasKey(hash)
|
repo.storage.hasKey(id)
|
||||||
|
|
||||||
proc retrieve*(repo: Repo, hash: MultiHash): MerkleDag =
|
proc retrieve*(repo: Repo, id: Cid): MerkleDag =
|
||||||
repo.storage[hash]
|
repo.storage[id]
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import std/unittest
|
import std/unittest
|
||||||
|
import pkg/libp2p
|
||||||
import pkg/dagger/merkledag
|
import pkg/dagger/merkledag
|
||||||
|
|
||||||
suite "Merkle DAG":
|
suite "Merkle DAG":
|
||||||
|
|
||||||
test "has a root hash":
|
test "has a content id":
|
||||||
let dag1 = MerkleDag(data: @[1'u8, 2'u8, 3'u8])
|
let dag1 = MerkleDag(data: @[1'u8, 2'u8, 3'u8])
|
||||||
let dag2 = MerkleDag(data: @[4'u8, 5'u8, 6'u8])
|
let dag2 = MerkleDag(data: @[4'u8, 5'u8, 6'u8])
|
||||||
let dag3 = MerkleDag(data: @[4'u8, 5'u8, 6'u8])
|
let dag3 = MerkleDag(data: @[4'u8, 5'u8, 6'u8])
|
||||||
check dag1.rootHash != dag2.rootHash
|
check dag1.rootId != dag2.rootId
|
||||||
check dag2.rootHash == dag3.rootHash
|
check dag2.rootId == dag3.rootId
|
||||||
|
|
|
@ -12,11 +12,11 @@ suite "repo":
|
||||||
test "stores Merkle DAGs":
|
test "stores Merkle DAGs":
|
||||||
repo.store(dag)
|
repo.store(dag)
|
||||||
|
|
||||||
test "retrieves Merkle DAGs by their root hash":
|
test "retrieves Merkle DAGs by their root id":
|
||||||
repo.store(dag)
|
repo.store(dag)
|
||||||
check repo.retrieve(dag.rootHash) == dag
|
check repo.retrieve(dag.rootId) == dag
|
||||||
|
|
||||||
test "knows which hashes are stored":
|
test "knows which ids are stored":
|
||||||
check repo.contains(dag.rootHash) == false
|
check repo.contains(dag.rootId) == false
|
||||||
repo.store(dag)
|
repo.store(dag)
|
||||||
check repo.contains(dag.rootHash) == true
|
check repo.contains(dag.rootId) == true
|
||||||
|
|
Loading…
Reference in New Issue