Implement Ipfs API
This commit is contained in:
parent
a63000f602
commit
b54c3d9585
51
ipfs.nim
51
ipfs.nim
|
@ -1,22 +1,49 @@
|
|||
import pkg/chronos
|
||||
import pkg/libp2p/peerinfo
|
||||
import pkg/libp2p/multiaddress
|
||||
import ./ipfs/p2p/switch
|
||||
import ./ipfs/repo
|
||||
import ./ipfs/chunking
|
||||
import ./ipfs/bitswap
|
||||
|
||||
export peerinfo except IPFS
|
||||
export multiaddress except IPFS
|
||||
|
||||
type
|
||||
Ipfs* = ref object
|
||||
repo: Repo
|
||||
switch: Switch
|
||||
bitswap: Bitswap
|
||||
|
||||
proc create*(t: typedesc[Ipfs]): Ipfs =
|
||||
Ipfs()
|
||||
proc info*(ipfs: Ipfs): PeerInfo =
|
||||
ipfs.switch.peerInfo
|
||||
|
||||
proc listen*(peer: Ipfs, address: TransportAddress) =
|
||||
discard
|
||||
proc start*(_: type Ipfs, addresses: seq[MultiAddress]): Future[Ipfs] {.async.} =
|
||||
let repo = Repo()
|
||||
let switch = Switch.create()
|
||||
let bitswap = Bitswap.start(switch, repo)
|
||||
switch.peerInfo.addrs.add(addresses)
|
||||
discard await switch.start()
|
||||
result = Ipfs(repo: repo, switch: switch, bitswap: bitswap)
|
||||
|
||||
proc connect*(peer: Ipfs, address: TransportAddress) =
|
||||
discard
|
||||
proc start*(_: type Ipfs, address: MultiAddress): Future[Ipfs] {.async.} =
|
||||
result = await Ipfs.start(@[address])
|
||||
|
||||
proc add*(peer: Ipfs, input: File): Future[string] {.async.} =
|
||||
discard
|
||||
proc start*(_: type Ipfs): Future[Ipfs] {.async.} =
|
||||
result = await Ipfs.start(@[])
|
||||
|
||||
proc get*(peer: Ipfs, identifier: string, output: File) {.async.} =
|
||||
discard
|
||||
proc connect*(peer: Ipfs, info: PeerInfo) {.async.} =
|
||||
await peer.bitswap.connect(info)
|
||||
|
||||
proc stop*(peer: Ipfs) =
|
||||
discard
|
||||
proc add*(peer: Ipfs, input: File): Future[Cid] {.async.} =
|
||||
let obj = createObject(input)
|
||||
peer.repo.store(obj)
|
||||
result = obj.cid
|
||||
|
||||
proc get*(peer: Ipfs, identifier: Cid, output: File) {.async.} =
|
||||
let obj = await peer.bitswap.retrieve(identifier)
|
||||
if obj.isSome:
|
||||
obj.get().writeToFile(output)
|
||||
|
||||
proc stop*(peer: Ipfs) {.async.} =
|
||||
await peer.switch.stop()
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
import pkg/libp2p
|
||||
import pkg/libp2p/multihash
|
||||
import pkg/libp2p/multicodec
|
||||
import pkg/libp2p/cid
|
||||
|
||||
export cid
|
||||
|
||||
type
|
||||
IpfsObject* = object
|
||||
|
|
|
@ -5,16 +5,15 @@ import pkg/ipfs
|
|||
|
||||
suite "integration":
|
||||
|
||||
let address = initTAddress("127.0.0.1:48952")
|
||||
let address = MultiAddress.init("/ip4/127.0.0.1/tcp/48952").get()
|
||||
|
||||
var peer1, peer2: Ipfs
|
||||
var input, output: File
|
||||
|
||||
proc setupPeers =
|
||||
peer1 = Ipfs.create()
|
||||
peer2 = Ipfs.create()
|
||||
peer1.listen(address)
|
||||
peer2.connect(address)
|
||||
proc setupPeers {.async.} =
|
||||
peer1 = await Ipfs.start(address)
|
||||
peer2 = await Ipfs.start()
|
||||
await peer2.connect(peer1.info)
|
||||
|
||||
proc setupFiles =
|
||||
input = open("tests/input.txt", fmReadWrite)
|
||||
|
@ -22,9 +21,9 @@ suite "integration":
|
|||
input.write("foo")
|
||||
input.setFilePos(0)
|
||||
|
||||
proc teardownPeers =
|
||||
peer1.stop()
|
||||
peer2.stop()
|
||||
proc teardownPeers {.async.} =
|
||||
await peer1.stop()
|
||||
await peer2.stop()
|
||||
|
||||
proc teardownFiles =
|
||||
input.close()
|
||||
|
@ -33,11 +32,11 @@ suite "integration":
|
|||
removeFile("tests/output.txt")
|
||||
|
||||
setup:
|
||||
setupPeers()
|
||||
await setupPeers()
|
||||
setupFiles()
|
||||
|
||||
teardown:
|
||||
teardownPeers()
|
||||
await teardownPeers()
|
||||
teardownFiles()
|
||||
|
||||
test "file can be transferred from one peer to another":
|
||||
|
@ -46,5 +45,4 @@ suite "integration":
|
|||
|
||||
input.setFilePos(0)
|
||||
output.setFilePos(0)
|
||||
# TODO: enable this when all parts are in place:
|
||||
# check output.readAll() == input.readAll()
|
||||
check output.readAll() == input.readAll()
|
||||
|
|
Loading…
Reference in New Issue