Repo for storing and retrieving Merkle DAGs
This commit is contained in:
parent
ecc4e50d28
commit
096fe0e40d
|
@ -0,0 +1,21 @@
|
|||
import std/tables
|
||||
import std/hashes
|
||||
import ./merkledag
|
||||
|
||||
export merkledag
|
||||
|
||||
type
|
||||
Repo* = ref object
|
||||
storage: Table[MultiHash, MerkleDag]
|
||||
|
||||
proc hash(multihash: MultiHash): Hash =
|
||||
hash($multihash)
|
||||
|
||||
proc store*(repo: Repo, dag: MerkleDag) =
|
||||
repo.storage[dag.rootHash] = dag
|
||||
|
||||
proc contains*(repo: Repo, hash: MultiHash): bool =
|
||||
repo.storage.hasKey(hash)
|
||||
|
||||
proc retrieve*(repo: Repo, hash: MultiHash): MerkleDag =
|
||||
repo.storage[hash]
|
|
@ -0,0 +1,22 @@
|
|||
import std/unittest
|
||||
import pkg/dagger/repo
|
||||
|
||||
suite "repo":
|
||||
|
||||
let dag = MerkleDag(data: @[1'u8, 2'u8, 3'u8])
|
||||
var repo: Repo
|
||||
|
||||
setup:
|
||||
repo = Repo()
|
||||
|
||||
test "stores Merkle DAGs":
|
||||
repo.store(dag)
|
||||
|
||||
test "retrieves Merkle DAGs by their root hash":
|
||||
repo.store(dag)
|
||||
check repo.retrieve(dag.rootHash) == dag
|
||||
|
||||
test "knows which hashes are stored":
|
||||
check repo.contains(dag.rootHash) == false
|
||||
repo.store(dag)
|
||||
check repo.contains(dag.rootHash) == true
|
|
@ -1,5 +1,6 @@
|
|||
import ./dagger/testMerkleDag
|
||||
import ./dagger/testChunking
|
||||
import ./dagger/testRepo
|
||||
import ./dagger/testDagger
|
||||
|
||||
{.warning[UnusedImport]: off.}
|
||||
|
|
Loading…
Reference in New Issue