Repo for storing and retrieving Merkle DAGs

This commit is contained in:
Mark Spanbroek 2021-01-12 13:58:57 +01:00 committed by markspanbroek
parent ecc4e50d28
commit 096fe0e40d
3 changed files with 44 additions and 0 deletions

21
dagger/repo.nim Normal file
View File

@ -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]

22
tests/dagger/testRepo.nim Normal file
View File

@ -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

View File

@ -1,5 +1,6 @@
import ./dagger/testMerkleDag
import ./dagger/testChunking
import ./dagger/testRepo
import ./dagger/testDagger
{.warning[UnusedImport]: off.}