diff --git a/ipfs/chunking.nim b/ipfs/chunking.nim index 4b15a48f..59e601d8 100644 --- a/ipfs/chunking.nim +++ b/ipfs/chunking.nim @@ -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)) diff --git a/ipfs/merkledag.nim b/ipfs/ipfsobject.nim similarity index 50% rename from ipfs/merkledag.nim rename to ipfs/ipfsobject.nim index d79878d2..d3cadc3f 100644 --- a/ipfs/merkledag.nim +++ b/ipfs/ipfsobject.nim @@ -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() diff --git a/ipfs/repo.nim b/ipfs/repo.nim index 7f0c9d95..c82f87c8 100644 --- a/ipfs/repo.nim +++ b/ipfs/repo.nim @@ -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] diff --git a/tests/ipfs/testChunking.nim b/tests/ipfs/testChunking.nim index 0567974c..56e48461 100644 --- a/tests/ipfs/testChunking.nim +++ b/tests/ipfs/testChunking.nim @@ -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) diff --git a/tests/ipfs/testMerkleDag.nim b/tests/ipfs/testMerkleDag.nim deleted file mode 100644 index 1abceea5..00000000 --- a/tests/ipfs/testMerkleDag.nim +++ /dev/null @@ -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 diff --git a/tests/ipfs/testObject.nim b/tests/ipfs/testObject.nim new file mode 100644 index 00000000..551cfcd5 --- /dev/null +++ b/tests/ipfs/testObject.nim @@ -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 diff --git a/tests/ipfs/testRepo.nim b/tests/ipfs/testRepo.nim index 5c3550b1..b54f5cef 100644 --- a/tests/ipfs/testRepo.nim +++ b/tests/ipfs/testRepo.nim @@ -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 diff --git a/tests/testAll.nim b/tests/testAll.nim index aaee2396..6059f90f 100644 --- a/tests/testAll.nim +++ b/tests/testAll.nim @@ -1,4 +1,4 @@ -import ./ipfs/testMerkleDag +import ./ipfs/testObject import ./ipfs/testChunking import ./ipfs/testRepo import ./ipfs/testIpfs