Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formalising the API #64

Merged
merged 6 commits into from
Mar 27, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 130 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,144 @@ npm install --save libp2p

## Usage

> **Disclaimer - We haven't solidified [libp2p interface](https://github.com/libp2p/interface-libp2p) yet, it might change at anytime.**

### Extending libp2p skeleton

libp2p becomes very simple and basically acts as a glue for every module that compose this library. Since it can be highly customized, it requires some setup. What we recommend is to have a libp2p build for the system you are developing taking into account in your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built and minified version that browsers can require).

### libp2p API
**Example:**

```JavaScript
// Creating a bundle that adds:
// transport: websockets + tcp
// stream-muxing: SPDY
// crypto-channel: secio
// discovery: multicast-dns

const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
const secio = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')

class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}

const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: [
spdy
],
crypto: [
secio
]
},
discovery: [
new MulticastDNS(peerInfo, 'your-identifier')
]
}

super(modules, peerInfo, peerBook, options)
}
}

// Now all the nodes you create, will have TCP, WebSockets, SPDY, SECIO and MulticastDNS support.
```

Defined by [interface-libp2p](https://github.com/libp2p/interface-libp2p)
### API

## Development
#### Create a Node - `new libp2p.Node([peerInfo, peerBook, options])`

## Linting
> Creates an instance of the libp2p.Node.

- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node. Optional.
- `peerBook`: instance of [PeerBook][] that contains the [PeerInfo][] of known peers. Optional.
- `options`: Object containing custom options for the bundle.

#### `libp2p.start(callback)`

> Start the libp2p Node.

`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case starting the node fails.

#### `libp2p.stop(callback)`

> Stop the libp2p Node.

`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.

#### `libp2p.dial(peer [, protocol, callback])`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to split this into dial and connect.

  • dial(peer, protocol, callback) all args required does what this does currently
  • connect(peer, callback) only ensures a connection is made to the peer, does not return a conn in the callback

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I'm thinking about it, not 100% convinced yet, but I can definitely see the potential of having a 'more clean way' for people to migrate from that swarm.connect that go-ipfs kind of standardised.

Copy link
Member Author

@daviddias daviddias Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second though, I like to keep it simple in libp2p land.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this make it simpler? It's just overloading a single method with even more functionality

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler as in "start fresh" and not have to take in a legacy 'connect'. I kind of like your idea, just not convinced about the dial and connect keyword: is it intuitive for users?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, on the other hand as long as dial creates a connection if it's missing I am not sure this is actually needed currently. Lets leave it as is for now, need to work through more code until am sure


> Dials to another peer in the network.

- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')

`callback` is a function with the following `function (err, conn) {}` signature, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.

#### `libp2p.hangUp(peer, callback)

> Closes an open connection with a peer, graciously.

- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]

`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.

#### `libp2p.handle(protocol, handlerFunc [, matchFunc])`

> Handle new protocol

- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
- `handlerFunc`: Function with signature `function (protocol, conn) {}`
- `matchFunc`: Function for matching on protocol (exact matching, semver, etc). Default to exact match.

#### `libp2p.unhandle(protocol)

> Stop handling protocol

- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')

#### `libp2p.on('peer', (peer) => {})`

> Peer has been discovered.

- `peer`: instance of [PeerInfo][]

#### `libp2p.isOn()`

> Check if libp2p is started

#### `libp2p.peerBook`

> PeerBook instance of the node

#### `libp2p.peerInfo`

> PeerInfo instance of the node

---------------------

`SOON™`

#### `libp2p.findPeers`

#### `libp2p.findProviders`

#### `libp2p.record.put`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be called dht.put and dht.get as they allow you to put arbitrary values not only records

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go-ipfs only allows (at least per js-ipfs-api tests) to put/get records, provider or IPNS.

It might make sense to just expose it as dht, so that we have a clean upgrade path for when we have the routing and record interfaces well established.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why that is, the underlying method do not have this restriction: https://github.com/libp2p/go-libp2p-kad-dht/blob/master/routing.go#L35

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dignifiedquire because there is no current value for IPFS in having people using the DHT as a generic key-value store.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said, don't feel the need to shoehorn this API methods as you develop the DHT, they aren't solidified yet.


#### `libp2p.record.get`

[PeerInfo]: https://github.com/libp2p/js-peer-info
[PeerId]: https://github.com/libp2p/js-peer-id
[PeerBook]: https://github.com/libp2p/js-peer-book
[multiaddr]: https://github.com/multiformats/js-multiaddr
[Connection]: https://github.com/libp2p/interface-connection

```sh
> npm run lint
```

### Packages

Expand Down