Change MerkleDag to IpfsObject to better match IPFS API

This commit is contained in:
Mark Spanbroek 2021-01-14 14:21:07 +01:00 committed by markspanbroek
parent 5a36e26e7c
commit dc06106ae1
8 changed files with 43 additions and 43 deletions

View File

@ -1,10 +1,10 @@
import ./merkledag
import ./ipfsobject
export merkledag
export ipfsobject
proc createChunks*(file: File): MerkleDag =
proc createObject*(file: File): IpfsObject =
let contents = file.readAll()
MerkleDag(data: cast[seq[byte]](contents))
IpfsObject(data: cast[seq[byte]](contents))
proc assembleChunks*(dag: MerkleDag, output: File) =
output.write(cast[string](dag.data))
proc writeToFile*(obj: IpfsObject, output: File) =
output.write(cast[string](obj.data))

View File

@ -1,10 +1,10 @@
import pkg/libp2p
type
MerkleDag* = object
IpfsObject* = object
data*: seq[byte]
proc rootId*(dag: MerkleDag): Cid =
proc cid*(obj: IpfsObject): Cid =
let codec = multiCodec("dag-pb")
let hash = MultiHash.digest("sha2-256", dag.data).get()
let hash = MultiHash.digest("sha2-256", obj.data).get()
Cid.init(CIDv0, codec, hash).get()

View File

@ -1,22 +1,22 @@
import std/tables
import std/hashes
import pkg/libp2p
import ./merkledag
import ./ipfsobject
export merkledag
export ipfsobject
type
Repo* = ref object
storage: Table[Cid, MerkleDag]
storage: Table[Cid, IpfsObject]
proc hash(id: Cid): Hash =
hash($id)
proc store*(repo: Repo, dag: MerkleDag) =
repo.storage[dag.rootId] = dag
proc store*(repo: Repo, obj: IpfsObject) =
repo.storage[obj.cid] = obj
proc contains*(repo: Repo, id: Cid): bool =
repo.storage.hasKey(id)
proc retrieve*(repo: Repo, id: Cid): MerkleDag =
proc retrieve*(repo: Repo, id: Cid): IpfsObject =
repo.storage[id]

View File

@ -18,12 +18,12 @@ suite "chunking":
removeFile("tests/input.txt")
removeFile("tests/output.txt")
test "creates a Merkle DAG from a file":
check createChunks(input) != MerkleDag.default
test "creates an IPFS object from a file":
check createObject(input) != IpfsObject.default
test "creates a file from a Merkle DAG":
let dag = createChunks(input)
assembleChunks(dag, output)
test "writes an IPFS object to a file":
let obj = createObject(input)
writeToFile(obj, output)
input.setFilePos(0)
output.setFilePos(0)

View File

@ -1,12 +0,0 @@
import std/unittest
import pkg/libp2p
import pkg/ipfs/merkledag
suite "Merkle DAG":
test "has a content id":
let dag1 = MerkleDag(data: @[1'u8, 2'u8, 3'u8])
let dag2 = MerkleDag(data: @[4'u8, 5'u8, 6'u8])
let dag3 = MerkleDag(data: @[4'u8, 5'u8, 6'u8])
check dag1.rootId != dag2.rootId
check dag2.rootId == dag3.rootId

12
tests/ipfs/testObject.nim Normal file
View File

@ -0,0 +1,12 @@
import std/unittest
import pkg/libp2p
import pkg/ipfs/ipfsobject
suite "IPFS Object":
test "has a content id":
let dag1 = IpfsObject(data: @[1'u8, 2'u8, 3'u8])
let dag2 = IpfsObject(data: @[4'u8, 5'u8, 6'u8])
let dag3 = IpfsObject(data: @[4'u8, 5'u8, 6'u8])
check dag1.cid != dag2.cid
check dag2.cid == dag3.cid

View File

@ -3,20 +3,20 @@ import pkg/ipfs/repo
suite "repo":
let dag = MerkleDag(data: @[1'u8, 2'u8, 3'u8])
let obj = IpfsObject(data: @[1'u8, 2'u8, 3'u8])
var repo: Repo
setup:
repo = Repo()
test "stores Merkle DAGs":
repo.store(dag)
test "stores IPFS objects":
repo.store(obj)
test "retrieves Merkle DAGs by their root id":
repo.store(dag)
check repo.retrieve(dag.rootId) == dag
test "retrieves IPFS objects by their content id":
repo.store(obj)
check repo.retrieve(obj.cid) == obj
test "knows which ids are stored":
check repo.contains(dag.rootId) == false
repo.store(dag)
check repo.contains(dag.rootId) == true
test "knows which content ids are stored":
check repo.contains(obj.cid) == false
repo.store(obj)
check repo.contains(obj.cid) == true

View File

@ -1,4 +1,4 @@
import ./ipfs/testMerkleDag
import ./ipfs/testObject
import ./ipfs/testChunking
import ./ipfs/testRepo
import ./ipfs/testIpfs