mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-04 06:23:06 +00:00
Change MerkleDag to IpfsObject to better match IPFS API
This commit is contained in:
parent
5a36e26e7c
commit
dc06106ae1
@ -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()
|
let contents = file.readAll()
|
||||||
MerkleDag(data: cast[seq[byte]](contents))
|
IpfsObject(data: cast[seq[byte]](contents))
|
||||||
|
|
||||||
proc assembleChunks*(dag: MerkleDag, output: File) =
|
proc writeToFile*(obj: IpfsObject, output: File) =
|
||||||
output.write(cast[string](dag.data))
|
output.write(cast[string](obj.data))
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import pkg/libp2p
|
import pkg/libp2p
|
||||||
|
|
||||||
type
|
type
|
||||||
MerkleDag* = object
|
IpfsObject* = object
|
||||||
data*: seq[byte]
|
data*: seq[byte]
|
||||||
|
|
||||||
proc rootId*(dag: MerkleDag): Cid =
|
proc cid*(obj: IpfsObject): Cid =
|
||||||
let codec = multiCodec("dag-pb")
|
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()
|
Cid.init(CIDv0, codec, hash).get()
|
||||||
@ -1,22 +1,22 @@
|
|||||||
import std/tables
|
import std/tables
|
||||||
import std/hashes
|
import std/hashes
|
||||||
import pkg/libp2p
|
import pkg/libp2p
|
||||||
import ./merkledag
|
import ./ipfsobject
|
||||||
|
|
||||||
export merkledag
|
export ipfsobject
|
||||||
|
|
||||||
type
|
type
|
||||||
Repo* = ref object
|
Repo* = ref object
|
||||||
storage: Table[Cid, MerkleDag]
|
storage: Table[Cid, IpfsObject]
|
||||||
|
|
||||||
proc hash(id: Cid): Hash =
|
proc hash(id: Cid): Hash =
|
||||||
hash($id)
|
hash($id)
|
||||||
|
|
||||||
proc store*(repo: Repo, dag: MerkleDag) =
|
proc store*(repo: Repo, obj: IpfsObject) =
|
||||||
repo.storage[dag.rootId] = dag
|
repo.storage[obj.cid] = obj
|
||||||
|
|
||||||
proc contains*(repo: Repo, id: Cid): bool =
|
proc contains*(repo: Repo, id: Cid): bool =
|
||||||
repo.storage.hasKey(id)
|
repo.storage.hasKey(id)
|
||||||
|
|
||||||
proc retrieve*(repo: Repo, id: Cid): MerkleDag =
|
proc retrieve*(repo: Repo, id: Cid): IpfsObject =
|
||||||
repo.storage[id]
|
repo.storage[id]
|
||||||
|
|||||||
@ -18,12 +18,12 @@ suite "chunking":
|
|||||||
removeFile("tests/input.txt")
|
removeFile("tests/input.txt")
|
||||||
removeFile("tests/output.txt")
|
removeFile("tests/output.txt")
|
||||||
|
|
||||||
test "creates a Merkle DAG from a file":
|
test "creates an IPFS object from a file":
|
||||||
check createChunks(input) != MerkleDag.default
|
check createObject(input) != IpfsObject.default
|
||||||
|
|
||||||
test "creates a file from a Merkle DAG":
|
test "writes an IPFS object to a file":
|
||||||
let dag = createChunks(input)
|
let obj = createObject(input)
|
||||||
assembleChunks(dag, output)
|
writeToFile(obj, output)
|
||||||
|
|
||||||
input.setFilePos(0)
|
input.setFilePos(0)
|
||||||
output.setFilePos(0)
|
output.setFilePos(0)
|
||||||
|
|||||||
@ -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
12
tests/ipfs/testObject.nim
Normal 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
|
||||||
@ -3,20 +3,20 @@ import pkg/ipfs/repo
|
|||||||
|
|
||||||
suite "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
|
var repo: Repo
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
repo = Repo()
|
repo = Repo()
|
||||||
|
|
||||||
test "stores Merkle DAGs":
|
test "stores IPFS objects":
|
||||||
repo.store(dag)
|
repo.store(obj)
|
||||||
|
|
||||||
test "retrieves Merkle DAGs by their root id":
|
test "retrieves IPFS objects by their content id":
|
||||||
repo.store(dag)
|
repo.store(obj)
|
||||||
check repo.retrieve(dag.rootId) == dag
|
check repo.retrieve(obj.cid) == obj
|
||||||
|
|
||||||
test "knows which ids are stored":
|
test "knows which content ids are stored":
|
||||||
check repo.contains(dag.rootId) == false
|
check repo.contains(obj.cid) == false
|
||||||
repo.store(dag)
|
repo.store(obj)
|
||||||
check repo.contains(dag.rootId) == true
|
check repo.contains(obj.cid) == true
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import ./ipfs/testMerkleDag
|
import ./ipfs/testObject
|
||||||
import ./ipfs/testChunking
|
import ./ipfs/testChunking
|
||||||
import ./ipfs/testRepo
|
import ./ipfs/testRepo
|
||||||
import ./ipfs/testIpfs
|
import ./ipfs/testIpfs
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user