Skip to content

Commit

Permalink
feat: rendezvous protocol full implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 9, 2020
1 parent d3923da commit b8bf044
Show file tree
Hide file tree
Showing 31 changed files with 1,509 additions and 754 deletions.
57 changes: 57 additions & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

const Libp2p = require('libp2p')
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
const Peers = require('./test/fixtures/peers')
const PeerId = require('peer-id')
const WebSockets = require('libp2p-websockets')
const Muxer = require('libp2p-mplex')
const { NOISE: Crypto } = require('libp2p-noise')

const Rendezvous = require('.')

let libp2p, rendezvous

const before = async () => {
// Use the last peer
const peerId = await PeerId.createFromJSON(Peers[Peers.length - 1])

libp2p = new Libp2p({
addresses: {
listen: [MULTIADDRS_WEBSOCKETS[0]]
},
peerId,
modules: {
transport: [WebSockets],
streamMuxer: [Muxer],
connEncryption: [Crypto]
},
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: false
}
}
}
})

await libp2p.start()

// rendezvous = new Rendezvous({ libp2p })
// await rendezvous.start()
}

const after = async () => {
// await rendezvous.stop()
await libp2p.stop()
}

module.exports = {
bundlesize: { maxSize: '100kB' },
hooks: {
pre: before,
post: after
}
}
61 changes: 36 additions & 25 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
sudo: false
language: node_js
cache: npm
stages:
- check
- test
- cov

matrix:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8
node_js:
- '10'
- '12'

os:
- linux
- osx
- windows

script:
- npm run lint
- npm run test
- npm run coverage
script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov

jobs:
include:
- stage: check
script:
- npx aegir dep-check
- npm run lint

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- stage: test
name: chrome
addons:
chrome: stable
script:
- npx aegir test -t browser -t webworker

after_success:
- npm run coverage-publish
- stage: test
name: firefox
addons:
firefox: latest
script:
- npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless

addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
notifications:
email: false
47 changes: 47 additions & 0 deletions LIBP2P.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Rendezvous Protocol in js-libp2p

The rendezvous protocol can be used in different contexts across libp2p. For using it, the libp2p network needs to have well known libp2p nodes acting as rendezvous servers. These nodes will have an extra role in the network. They will collect and maintain a list of registrations per rendezvous namespace. Other peers in the network will act as rendezvous clients and will register themselves on given namespaces by messaging a rendezvous server node. Taking into account these registrations, a rendezvous client is able to discover other peers in a given namespace by querying a server.

## Usage

`js-libp2p` supports the usage of the rendezvous protocol through its configuration. It allows to enable the rendezvous protocol, as well as its server mode, enable automatic peer discover and to specify the topics to register from startup.

The rendezvous comes with a discovery service that enables libp2p to automatically discover other peers in the provided namespaces and eventually connect to them.
**TODO: it should be compliant with the peer-discovery interface and configured as any other discovery service instead!!**

You can configure it through libp2p as follows:

```js
const Libp2p = require('libp2p')

const node = await Libp2p.create({
// ... required configurations
rendezvous: {
enabled: true,
namespaces: ['/namespace/1', '/namespace/2'],
discovery: {
enabled: true,
interval: 1000
},
server: {
enabled: true
}
}
})
```

While `js-libp2p` supports the rendezvous protocol out of the box, it also provides a rendezvous API that users can interact with. This API should allow users to register new rendezvous namespaces, unregister from previously registered namespaces and to manually discover other peers.

## Libp2p Flow

When a libp2p node with the rendezvous protocol enabled starts, it should start by connecting to a rendezvous server and ask for nodes in the given namespaces. The rendezvous server can be added to the bootstrap nodes or manually dialed. An example of a namespace could be a relay namespace, so that undiable nodes can register themselves as reachable through that relay.

If the discovery service is disabled, the rendezvous API should allow users to discover peers registered on provided namespaces.

When a libp2p node running the rendezvous protocol is going to stop, it should unregister from all the namespaces previously registered.

In the event of a rendezvous client getting connected to a second rendezvous server, it should propagate its registrations to it. The rendezvous server should clean its registrations for a peer when it is not connected with it anymore.

## Other notes:

After a query is made, who is responsible for determining if we need more records? (cookie reuse)
113 changes: 110 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,117 @@
# libp2p-rendezvous
# js-libp2p-rendezvous

A javascript implementation of the rendezvous protocol for libp2p
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)

> Javascript implementation of the rendezvous protocol for libp2p
## Overview

Libp2p rendezvous is a lightweight mechanism for generalized peer discovery. It can be used for bootstrap purposes, real time peer discovery, application specific routing, and so on. Any node implementing the rendezvous protocol can act as a rendezvous point, allowing the discovery of relevant peers in a decentralized fashion.

See https://github.com/libp2p/specs/tree/master/rendezvous for more details

## Lead Maintainer

[Vasco Santos](https://github.com/vasco-santos).

See https://github.com/libp2p/specs/pull/44 for more details
## API

### rendezvous.register

Registers the peer in a given namespace.

`rendezvous.register(namespace, [ttl])`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| namespace | `string` | namespace to register |
| ttl | `number` | registration ttl in ms (default: `7200e3` and minimum `120`) |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<number>` | Remaining ttl value |

#### Example

```js
// ...
const ttl = await rendezvous.register(namespace)
```

### rendezvous.unregister

Unregisters the peer from a given namespace.

`rendezvous.unregister(namespace)`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| namespace | `string` | namespace to unregister |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<void>` | Operation resolved |

#### Example

```js
// ...
await rendezvous.register(namespace)
await rendezvous.unregister(namespace)
```

### rendezvous.discover

Discovers peers registered under a given namespace.

`rendezvous.discover(namespace, [limit], [cookie])`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| namespace | `string` | namespace to discover |
| limit | `number` | limit of peers to discover |
| cookie | `Buffer` | |

#### Returns

| Type | Description |
|------|-------------|
| `AsyncIterable<{ id: PeerId, signedPeerRecord: Envelope, ns: string, ttl: number }>` | Async Iterable registrations |

#### Example

```js
// ...
await rendezvous.register(namespace)

for await (const reg of rendezvous.discover(namespace)) {
console.log(reg.id, reg.signedPeerRecord, reg.ns, reg.ttl)
}
```

## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/issues)!

This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/contributing.md)

## License

MIT - Protocol Labs 2020

[multiaddr]: https://github.com/multiformats/js-multiaddr
28 changes: 0 additions & 28 deletions appveyor.yml

This file was deleted.

Loading

0 comments on commit b8bf044

Please sign in to comment.