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 2, 2020
1 parent d3923da commit b23fc7a
Show file tree
Hide file tree
Showing 30 changed files with 1,456 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
114 changes: 111 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,118 @@
# 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, multiaddrs, [ttl])`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| namespace | `string` | namespace to register |
| multiaddrs | `Array<Multiaddr>` | [`Multiaddrs`][multiaddr] to announce |
| 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, multiaddrs)
```

### 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, multiaddrs)
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, multiaddrs: Array<Multiaddr>, ns: string, ttl: number }>` | Async Iterable registrations |

#### Example

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

for await (const reg of rendezvous.discover(namespace)) {
console.log(reg.id, reg.multiaddrs, 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.

76 changes: 51 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,66 @@
{
"name": "libp2p-rendezvous",
"version": "0.0.0",
"description": "A javascript implementation of the rendezvous protocol for libp2p",
"leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>",
"main": "index.js",
"scripts": {
"test": "aegir test"
"description": "Javascript implementation of the rendezvous protocol for libp2p",
"leadMaintainer": "Vasco Santos <santos.vasco10@gmail.com>",
"main": "src/index.js",
"files": [
"dist",
"src"
],
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p-rendezvous.git"
},
"keywords": [
"libp2p",
"rendezvous",
"protocol",
"discovery"
],
"author": "Maciej Krüger <mkg20001@gmail.com>",
"license": "MIT",
"dependencies": {
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"protons": "^1.0.1",
"pull-protocol-buffers": "^0.1.2"
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-rendezvous/issues"
},
"devDependencies": {
"aegir": "^13.1.0",
"libp2p": "^0.20.2",
"libp2p-mplex": "^0.7.0",
"libp2p-secio": "^0.10.0",
"libp2p-spdy": "^0.12.1",
"libp2p-tcp": "^0.12.0"
"homepage": "https://libp2p.io",
"license": "MIT",
"engines": {
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mkg20001/libp2p-rendezvous.git"
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "nyc --reporter=text --reporter=lcov npm test"
},
"bugs": {
"url": "https://github.com/mkg20001/libp2p-rendezvous/issues"
"dependencies": {
"debug": "^4.1.1",
"err-code": "^2.0.3",
"it-buffer": "^0.1.2",
"it-length-prefixed": "^3.0.1",
"it-pipe": "^1.1.0",
"libp2p-interfaces": "^0.3.0",
"multiaddr": "^7.5.0",
"peer-id": "^0.13.13",
"protons": "^1.2.0",
"streaming-iterables": "^4.1.2"
},
"homepage": "https://github.com/mkg20001/libp2p-rendezvous#readme"
"devDependencies": {
"aegir": "^23.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"libp2p": "^0.28.3",
"libp2p-mplex": "^0.9.5",
"libp2p-noise": "^1.1.2",
"libp2p-websockets": "^0.13.6",
"p-times": "^3.0.0",
"p-wait-for": "^3.1.0",
"sinon": "^9.0.2"
}
}
5 changes: 5 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

exports.PROTOCOL_MULTICODEC = '/rendezvous/1.0.0'
exports.MAX_NS_LENGTH = 255 // TODO: spec this
exports.MAX_LIMIT = 1000 // TODO: spec this
8 changes: 8 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

exports.codes = {
INVALID_NAMESPACE: 'ERR_INVALID_NAMESPACE',
INVALID_TTL: 'ERR_INVALID_TTL',
INVALID_MULTIADDRS: 'ERR_INVALID_MULTIADDRS',
NO_CONNECTED_RENDEZVOUS_SERVERS: 'ERR_NO_CONNECTED_RENDEZVOUS_SERVERS'
}
Loading

0 comments on commit b23fc7a

Please sign in to comment.