From b54c3d958503abd00da311773b99a77e6bfa12d1 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 1 Feb 2021 11:11:30 +0100 Subject: [PATCH] Implement Ipfs API --- ipfs.nim | 51 +++++++++++++++++++++++++++++++---------- ipfs/ipfsobject.nim | 6 ++++- tests/ipfs/testIpfs.nim | 24 +++++++++---------- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/ipfs.nim b/ipfs.nim index 905bc651..9fd5b5a9 100644 --- a/ipfs.nim +++ b/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() diff --git a/ipfs/ipfsobject.nim b/ipfs/ipfsobject.nim index d3cadc3f..b4b46a06 100644 --- a/ipfs/ipfsobject.nim +++ b/ipfs/ipfsobject.nim @@ -1,4 +1,8 @@ -import pkg/libp2p +import pkg/libp2p/multihash +import pkg/libp2p/multicodec +import pkg/libp2p/cid + +export cid type IpfsObject* = object diff --git a/tests/ipfs/testIpfs.nim b/tests/ipfs/testIpfs.nim index 2ee920ba..aa5f3dfa 100644 --- a/tests/ipfs/testIpfs.nim +++ b/tests/ipfs/testIpfs.nim @@ -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()