Implement Ipfs API

This commit is contained in:
Mark Spanbroek 2021-02-01 11:11:30 +01:00 committed by markspanbroek
parent a63000f602
commit b54c3d9585
3 changed files with 55 additions and 26 deletions

View File

@ -1,22 +1,49 @@
import pkg/chronos 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 type
Ipfs* = ref object Ipfs* = ref object
repo: Repo
switch: Switch
bitswap: Bitswap
proc create*(t: typedesc[Ipfs]): Ipfs = proc info*(ipfs: Ipfs): PeerInfo =
Ipfs() ipfs.switch.peerInfo
proc listen*(peer: Ipfs, address: TransportAddress) = proc start*(_: type Ipfs, addresses: seq[MultiAddress]): Future[Ipfs] {.async.} =
discard 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) = proc start*(_: type Ipfs, address: MultiAddress): Future[Ipfs] {.async.} =
discard result = await Ipfs.start(@[address])
proc add*(peer: Ipfs, input: File): Future[string] {.async.} = proc start*(_: type Ipfs): Future[Ipfs] {.async.} =
discard result = await Ipfs.start(@[])
proc get*(peer: Ipfs, identifier: string, output: File) {.async.} = proc connect*(peer: Ipfs, info: PeerInfo) {.async.} =
discard await peer.bitswap.connect(info)
proc stop*(peer: Ipfs) = proc add*(peer: Ipfs, input: File): Future[Cid] {.async.} =
discard 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()

View File

@ -1,4 +1,8 @@
import pkg/libp2p import pkg/libp2p/multihash
import pkg/libp2p/multicodec
import pkg/libp2p/cid
export cid
type type
IpfsObject* = object IpfsObject* = object

View File

@ -5,16 +5,15 @@ import pkg/ipfs
suite "integration": 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 peer1, peer2: Ipfs
var input, output: File var input, output: File
proc setupPeers = proc setupPeers {.async.} =
peer1 = Ipfs.create() peer1 = await Ipfs.start(address)
peer2 = Ipfs.create() peer2 = await Ipfs.start()
peer1.listen(address) await peer2.connect(peer1.info)
peer2.connect(address)
proc setupFiles = proc setupFiles =
input = open("tests/input.txt", fmReadWrite) input = open("tests/input.txt", fmReadWrite)
@ -22,9 +21,9 @@ suite "integration":
input.write("foo") input.write("foo")
input.setFilePos(0) input.setFilePos(0)
proc teardownPeers = proc teardownPeers {.async.} =
peer1.stop() await peer1.stop()
peer2.stop() await peer2.stop()
proc teardownFiles = proc teardownFiles =
input.close() input.close()
@ -33,11 +32,11 @@ suite "integration":
removeFile("tests/output.txt") removeFile("tests/output.txt")
setup: setup:
setupPeers() await setupPeers()
setupFiles() setupFiles()
teardown: teardown:
teardownPeers() await teardownPeers()
teardownFiles() teardownFiles()
test "file can be transferred from one peer to another": test "file can be transferred from one peer to another":
@ -46,5 +45,4 @@ suite "integration":
input.setFilePos(0) input.setFilePos(0)
output.setFilePos(0) output.setFilePos(0)
# TODO: enable this when all parts are in place: check output.readAll() == input.readAll()
# check output.readAll() == input.readAll()