Compare commits

...

38 Commits

Author SHA1 Message Date
ddca720b2a chore: travis cache 2019-11-15 16:03:29 +01:00
1ca2d87287 chore: address review 2019-11-15 15:48:14 +01:00
340edf53e3 chore: address review 2019-11-15 15:15:12 +01:00
4cc9736485 chore: use topology interface 2019-11-14 19:13:45 +01:00
cdfd02306f chore: address review 2019-11-14 15:34:35 +01:00
dcd127b7a5 refactor: pubsub subsystem 2019-11-14 15:34:35 +01:00
751bc00f1b chore: address review 2019-11-14 15:33:12 +01:00
ec5319a50e chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
2019-11-14 15:33:12 +01:00
dca4727112 feat: peer-store v0 2019-11-14 15:33:12 +01:00
695d71e44f chore: update it-length-prefixed (#476)
fix: decode.fromReader usage
2019-11-13 16:38:07 +01:00
0fc6668321 refactor: async identify and identify push (#473)
* chore: add missing dep

* feat: import from identify push branch

https://github.com/libp2p/js-libp2p-identify/tree/feat/identify-push

* feat: add the connection to stream handlers

* refactor: identify to async/await

* chore: fix lint

* test: add identify tests

* refactor: add identify to the dialer flow

* feat: connect identify to the registrar

* fix: resolve review feedback

* fix: perform identify push when our protocols change
2019-11-07 12:11:50 +01:00
d7c38d3fec feat: registrar (#471)
* feat: peer-store v0

* feat: registrar

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* chore: address review

* chore: support multiple conns

* chore: address review

* fix: no remote peer from topology on disconnect
2019-11-06 15:47:11 +01:00
53bcd2d745 feat: peer store (#470)
* feat: peer-store v0

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
2019-11-06 15:11:13 +01:00
fba058370a refactor: crypto and pnet (#469)
* feat: add initial plaintext 2 module

* refactor: initial refactor of pnet

* chore: fix lint

* fix: update plaintext api usage

* test: use plaintext for test crypto

* chore: update deps

test: update dialer suite scope

* feat: add connection protection to the upgrader

* refactor: cleanup and lint fix

* chore: remove unncessary transforms

* chore: temporarily disable bundlesize

* chore: add missing dep

* fix: use it-handshake to prevent overreading

* chore(fix): PR feedback updates

* chore: apply suggestions from code review

Co-Authored-By: Vasco Santos <vasco.santos@moxy.studio>
2019-11-04 14:05:58 +01:00
a23d4d23cb refactor(async): add dialer and upgrader (#462)
* chore(deps): update connection and multistream

* feat: add basic dial support for addresses and peers

* test: automatically require all node test files

* fix: dont catch and log in the wrong place

* test: add direct spec test

fix: improve dial error consistency

* feat: add dial timeouts and concurrency

Queue timeouts will result in aborts of the dials

* chore: fix linting

* test: verify dialer defaults

* feat: add initial upgrader

* fix: add more test coverage and fix bugs

* feat: libp2p creates the upgrader

* feat: hook up handle to the upgrader

* feat: hook up the dialer to libp2p

test: add node dialer libp2p tests

* feat: add connection listeners to upgrader

* feat: emit connect and disconnect events

* chore: use libp2p-interfaces

* fix: address review feedback

* fix: correct import

* refactor: dedupe connection creation code
2019-10-21 16:53:58 +02:00
6ecc9b80c3 docs: add stream wrapping example (#466)
* docs: add duplex wrapping example

docs: add iterable types from @alanshaw's gist

* docs(fix): add feedback fix

Co-Authored-By: Vasco Santos <vasco.santos@moxy.studio>

* docs: clean up based on feedback
2019-10-21 13:36:05 +02:00
cd97abfcc3 refactor(async): update transports subsystem (#461)
* test: remove all tests for a clean slate

The refactor will require a large number of updates to the tests. In order
to ensure we have done a decent deduplication, and have a cleaner suite of tests
we've removed all tests. This will also allow us to more easily see tests
for the refactored systems.

We have a record of the latest test suites in master, so we are not losing any history.

* chore: update tcp and websockets
* chore: remove other transports until they are converted
* chore: use mafmt and multiaddr async versions
* chore: add and fix dependencies
* chore: clean up travis file
* feat: add new transport manager
* docs: add constructor jsdocs
* refactor(config): check that transports exist
This also removes the other logic, it can be added when those subsystems are refactored

* chore(deps): use async peer-id and peer-info
* feat: wire up the transport manager with libp2p
* chore: remove superstruct dep
2019-10-02 13:31:28 +02:00
c1da30bc74 Add streaming iterables guide (#459)
* docs: add streaming iterables guide placeholder

* chore: move peer discovery readme to doc fold:wqer

* docs: add link to async refactor issue
2019-09-30 18:20:25 +02:00
35aa45ce92 chore: update required libp2p-crypto version (#457)
So as to only pull in libp2p-crypto version with updated `node-forge` dep
2019-09-25 12:12:36 +02:00
b11c6fc7e9 chore: release version v0.26.2 2019-09-24 14:10:58 +02:00
ebedd3510b chore: update contributors 2019-09-24 14:10:57 +02:00
ae6af20e8e fix: pubsub promisify (#456)
* fix: allow pubsub sub/unsub via promises

* chore: fix linting errors
2019-09-24 14:02:07 +02:00
2a80618740 docs: add bridged chats (#454) 2019-09-23 11:39:47 +02:00
5b1bd389f8 docs: update packages table (#265)
* docs: update packages table

* docs: removed not implemented packages
2019-09-03 10:29:03 +02:00
3e31c2d0df docs: remove dead link from readme (#450) 2019-08-27 17:07:21 +02:00
8079c2078b chore: release version v0.26.1 2019-08-21 19:51:51 +02:00
80cf0777b5 chore: update contributors 2019-08-21 19:51:50 +02:00
60b0cbc179 fix: reject rather than throw in get peer info (#410)
The get peer info util consolidation from #400 exposed an issue
with how bad values are being handled. Throwing the error can cause
issues when promises are being used. Rejecting resolves this.
I added a test case to validate the change.
2019-08-21 19:08:56 +02:00
3eef695bc0 fix: improve config defaults (#409)
This removes defaults from superstruct and instead uses
mergeOptions to deeply set the defaults on configuration.
This ensures that defaults are properly set.

This is a step toward removing superstruct altogether, #406,
but it is still being used for basic type validation.
2019-08-21 16:44:30 +02:00
b3deb356f1 fix: reference files directly to avoid npm install failures (#408) 2019-08-21 10:23:06 +02:00
299cfefa01 chore: remove webrtcsupport 2019-08-20 17:34:55 +02:00
aa95ab9928 fix: avoid using superstruct interface 2019-08-20 17:34:55 +02:00
b0f124b5ff fix: pubsub configuration (#404)
* fix: add pubsub default config (#401)

License: MIT
Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net>

* docs: add default pubsub config to README (#401)

License: MIT
Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net>

* fix: pass config to provided PubSub (#401)

License: MIT
Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net>

* docs: adapt pubsub/example for new config (#401)

License: MIT
Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net>

* Update examples/pubsub/README.md

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* test: add pubsub config tests (#401)

License: MIT
Signed-off-by: Matthias Knopp <matthias-knopp@gmx.net>
2019-08-19 17:06:08 +02:00
b294301456 refactor: add core modules to libp2p (#400)
* refactor: add js-libp2p-connection-manager to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Pedro Teixeira <i@pgte.me>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>

* test(conn-mgr): only run in node

* refactor: add js-libp2p-identify to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com>
Co-authored-by: Hugo Dias <hugomrdias@gmail.com>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Maciej Krüger <mkg20001@gmail.com>
Co-authored-by: Richard Littauer <richard.littauer@gmail.com>
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
Co-authored-by: Yusef Napora <yusef@protocol.ai>
Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>

* refactor: add libp2p-pnet to repo

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>

* refactor: add libp2p-ping to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Francisco Baio Dias <xicombd@gmail.com>
Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com>
Co-authored-by: Hugo Dias <mail@hugodias.me>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: João Antunes <j.goncalo.antunes@gmail.com>
Co-authored-by: Richard Littauer <richard.littauer@gmail.com>
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>

* refactor: add libp2p-circuit to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com>
Co-authored-by: Hugo Dias <mail@hugodias.me>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Maciej Krüger <mkg20001@gmail.com>
Co-authored-by: Oli Evans <oli@tableflip.io>
Co-authored-by: Pedro Teixeira <i@pgte.me>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
Co-authored-by: Victor Bjelkholm <victorbjelkholm@gmail.com>
Co-authored-by: Yusef Napora <yusef@napora.org>
Co-authored-by: dirkmc <dirk@mccormick.cx>

* test(switch): avoid using instanceof

* chore(switch): update bignumber dep

* refactor(circuit): clean up tests

* refactor(switch): consolidate get peer utils

* test(identify): do deep checks of addresses

* test(identify): bump timeout for identify test

* test(switch): tidy up limit dialer test

* refactor(switch): remove redundant circuit tests

* chore: add coverage script

* refactor(circuit): consolidate get peer info

* docs: reference original repositories in each sub readme

* docs: fix comment

* refactor: clean up sub package.json files and readmes
2019-08-16 17:30:03 +02:00
d92306f222 docs: fix contributing readme (#403) 2019-08-13 12:10:59 +02:00
fd738f9d51 refactor: add js-libp2p-switch to the libp2p codebase (#388)
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
Co-authored-by: Alan Shaw <alan@tableflip.io>
Co-authored-by: Arnaud <arnaud.valensi@gmail.com>
Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: David Dias <mail@daviddias.me>
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
Co-authored-by: Francisco Baio Dias <xicombd@gmail.com>
Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com>
Co-authored-by: Haad <haadcode@users.noreply.github.com>
Co-authored-by: Hugo Dias <mail@hugodias.me>
Co-authored-by: Hugo Dias <hugomrdias@gmail.com>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Kevin Kwok <antimatter15@gmail.com>
Co-authored-by: Kobi Gurkan <kobigurk@gmail.com>
Co-authored-by: Maciej Krüger <mkg20001@gmail.com>
Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
Co-authored-by: Michael Fakhry <fakhrimichael@live.com>
Co-authored-by: Oli Evans <oli@tableflip.io>
Co-authored-by: Pau Ramon Revilla <masylum@gmail.com>
Co-authored-by: Pedro Teixeira <i@pgte.me>
Co-authored-by: Pius Nyakoojo <piusnyakoojo@gmail.com>
Co-authored-by: Richard Littauer <richard.littauer@gmail.com>
Co-authored-by: Sid Harder <sideharder@gmail.com>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
Co-authored-by: harrshasri <35241544+harrshasri@users.noreply.github.com>
Co-authored-by: kumavis <kumavis@users.noreply.github.com>
Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>
2019-08-08 19:01:16 +02:00
d788433b43 chore: release version v0.26.0 2019-08-07 20:48:42 +02:00
d5a977b227 chore: update contributors 2019-08-07 20:48:41 +02:00
141 changed files with 10895 additions and 5542 deletions

View File

@ -1,59 +1,38 @@
'use strict'
const pull = require('pull-stream')
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
const sigServer = require('libp2p-webrtc-star/src/sig-server')
const Node = require('./test/utils/bundle-nodejs.js')
const {
getPeerRelay,
WRTC_RENDEZVOUS_MULTIADDR,
WS_RENDEZVOUS_MULTIADDR
} = require('./test/utils/constants')
let wrtcRendezvous
let wsRendezvous
let node
let peerInfo
const Libp2p = require('./src')
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
const Peers = require('./test/fixtures/peers')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const WebSockets = require('libp2p-websockets')
const Muxer = require('libp2p-mplex')
const Crypto = require('./src/insecure/plaintext')
const pipe = require('it-pipe')
let libp2p
const before = async () => {
[wrtcRendezvous, wsRendezvous, peerInfo] = await Promise.all([
sigServer.start({
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
}),
WebSocketStarRendezvous.start({
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
refreshPeerListIntervalMS: 1000,
strictMultiaddr: false,
cryptoChallenge: true
}),
getPeerRelay()
])
// Use the last peer
const peerId = await PeerId.createFromJSON(Peers[Peers.length - 1])
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(MULTIADDRS_WEBSOCKETS[0])
node = new Node({
libp2p = new Libp2p({
peerInfo,
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
modules: {
transport: [WebSockets],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})
// Add the echo protocol
libp2p.handle('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
await node.start()
await libp2p.start()
}
const after = () => {
return Promise.all([
wrtcRendezvous.stop(),
wsRendezvous.stop(),
node.stop()
])
const after = async () => {
await libp2p.stop()
}
module.exports = {

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ logs
*.log
coverage
.nyc_output
# Runtime data
pids

View File

@ -20,7 +20,7 @@ jobs:
include:
- stage: check
script:
- npx aegir build --bundlesize
# - npx aegir build --bundlesize
- npx aegir dep-check -- -i wrtc -i electron-webrtc
- npm run lint
@ -29,16 +29,14 @@ jobs:
addons:
chrome: stable
script:
- npx aegir test -t browser
- npx aegir test -t webworker
- npx aegir test -t browser -t webworker
- stage: test
name: firefox
addons:
firefox: latest
script:
- npx aegir test -t browser -- --browsers FirefoxHeadless
- npx aegir test -t webworker -- --browsers FirefoxHeadless
- npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
notifications:
email: false

View File

@ -1,3 +1,32 @@
<a name="0.26.2"></a>
## [0.26.2](https://github.com/libp2p/js-libp2p/compare/v0.26.1...v0.26.2) (2019-09-24)
### Bug Fixes
* pubsub promisify ([#456](https://github.com/libp2p/js-libp2p/issues/456)) ([ae6af20](https://github.com/libp2p/js-libp2p/commit/ae6af20))
<a name="0.26.1"></a>
## [0.26.1](https://github.com/libp2p/js-libp2p/compare/v0.26.0...v0.26.1) (2019-08-21)
### Bug Fixes
* avoid using superstruct interface ([aa95ab9](https://github.com/libp2p/js-libp2p/commit/aa95ab9))
* improve config defaults ([#409](https://github.com/libp2p/js-libp2p/issues/409)) ([3eef695](https://github.com/libp2p/js-libp2p/commit/3eef695)), closes [#406](https://github.com/libp2p/js-libp2p/issues/406)
* pubsub configuration ([#404](https://github.com/libp2p/js-libp2p/issues/404)) ([b0f124b](https://github.com/libp2p/js-libp2p/commit/b0f124b)), closes [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401) [#401](https://github.com/libp2p/js-libp2p/issues/401)
* reference files directly to avoid npm install failures ([#408](https://github.com/libp2p/js-libp2p/issues/408)) ([b3deb35](https://github.com/libp2p/js-libp2p/commit/b3deb35))
* reject rather than throw in get peer info ([#410](https://github.com/libp2p/js-libp2p/issues/410)) ([60b0cbc](https://github.com/libp2p/js-libp2p/commit/60b0cbc)), closes [#400](https://github.com/libp2p/js-libp2p/issues/400)
<a name="0.26.0"></a>
# [0.26.0](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.3...v0.26.0) (2019-08-07)
<a name="0.26.0-rc.3"></a>
# [0.26.0-rc.3](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.2...v0.26.0-rc.3) (2019-08-06)

View File

@ -1,8 +1,8 @@
# Contributing guidelines
libp2p as a project, including js-libp2p and all of its modules, follows the [standard IPFS Community contributing guidelines](https://github.com/ipfs/community/blob/master/contribution-guidelines.md).
libp2p as a project, including js-libp2p and all of its modules, follows the [standard IPFS Community contributing guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md).
We also adhere to the [IPFS JavaScript Community contributing guidelines](https://github.com/ipfs/community/blob/master/js-code-guidelines.md) which provide additional information of how to collaborate and contribute in the JavaScript implementation of libp2p.
We also adhere to the [IPFS JavaScript Community contributing guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md) which provide additional information of how to collaborate and contribute in the JavaScript implementation of libp2p.
We appreciate your time and attention for going over these. Please open an issue on [ipfs/community](https://github.com/ipfs/community) if you have any question.

View File

@ -8,8 +8,9 @@
<a href="http://ipn.io"><img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" /></a>
<a href="http://libp2p.io/"><img src="https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square" /></a>
<a href="http://webchat.freenode.net/?channels=%23libp2p"><img src="https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square" /></a>
<a href="https://riot.permaweb.io/#/room/#libp2p:permaweb.io"><img src="https://img.shields.io/badge/matrix-%23libp2p%3Apermaweb.io-blue.svg?style=flat-square" /> </a>
<a href="https://discord.gg/66KBrm2"><img src="https://img.shields.io/discord/475789330380488707?color=blueviolet&label=discord&style=flat-square" /></a>
<a href="https://discuss.libp2p.io"><img src="https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg" /></a>
<a href="https://waffle.io/libp2p/libp2p"><img src="https://img.shields.io/badge/pm-waffle-yellow.svg?style=flat-square" /></a>
</p>
<p align="center">
@ -29,9 +30,7 @@
We've come a long way, but this project is still in Alpha, lots of development is happening, API might change, beware of the Dragons 🐉..
**Want to get started?** Check our [examples folder](/examples). You can check the development status at the [Waffle Board](https://waffle.io/libp2p/js-libp2p).
[![Throughput Graph](https://graphs.waffle.io/libp2p/js-libp2p/throughput.svg)](https://waffle.io/libp2p/js-libp2p/metrics/throughput)
**Want to get started?** Check our [examples folder](/examples).
[**`Weekly Core Dev Calls`**](https://github.com/ipfs/pm/issues/650)
@ -192,7 +191,10 @@ class Node extends Libp2p {
}
},
pubsub: {
enabled: true
enabled: true,
emitSelf: true, // whether the node should emit to self on publish, in the event of the topic being subscribed
signMessages: true, // if messages should be signed
strictSigning: true // if message signing should be required
}
}
}
@ -209,22 +211,18 @@ class Node extends Libp2p {
**IMPORTANT NOTE**: All the methods listed in the API section that take a callback are also now Promisified. Libp2p is migrating away from callbacks to async/await, and in a future release (that will be announced in advance), callback support will be removed entirely. You can follow progress of the async/await endeavor at https://github.com/ipfs/js-ipfs/issues/1670.
#### Create a Node - `Libp2p.createLibp2p(options, callback)`
#### Create a Node - `Libp2p.create(options)`
> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead
```js
const { createLibp2p } = require('libp2p')
createLibp2p(options, (err, libp2p) => {
if (err) throw err
libp2p.start((err) => {
if (err) throw err
})
})
const { create } = require('libp2p')
const libp2p = await create(options)
await libp2p.start()
```
- `options`: Object of libp2p configuration options
- `callback`: Function with signature `function (Error, Libp2p) {}`
#### Create a Node alternative - `new Libp2p(options)`
@ -334,7 +332,7 @@ Required keys in the `options` object:
> Peer has been discovered.
If `autoDial` is `true`, applications should **not** attempt to connect to the peer
unless they are performing a specific action. See [peer discovery and auto dial](./PEER_DISCOVERY.md) for more information.
unless they are performing a specific action. See [peer discovery and auto dial](./doc/PEER_DISCOVERY.md) for more information.
- `peer`: instance of [PeerInfo][]
@ -568,14 +566,11 @@ List of packages currently in existence for libp2p
| **Transport** |
| [`interface-transport`](//github.com/libp2p/interface-transport) | [![npm](https://img.shields.io/npm/v/interface-transport.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/interface-transport/releases) | [![Deps](https://david-dm.org/libp2p/interface-transport.svg?style=flat-square)](https://david-dm.org/libp2p/interface-transport) | [![Travis CI](https://travis-ci.com/libp2p/interface-transport.svg?branch=master)](https://travis-ci.com/libp2p/interface-transport) | [![codecov](https://codecov.io/gh/libp2p/interface-transport/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/interface-transport) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-tcp`](//github.com/libp2p/js-libp2p-tcp) | [![npm](https://img.shields.io/npm/v/libp2p-tcp.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-tcp/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-tcp) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-tcp.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-tcp) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-tcp/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-tcp) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-udp`](//github.com/libp2p/js-libp2p-udp) | [![npm](https://img.shields.io/npm/v/libp2p-udp.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-udp/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-udp.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-udp) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-udp.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-udp) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-udp/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-udp) | N/A |
| [`libp2p-udt`](//github.com/libp2p/js-libp2p-udt) | [![npm](https://img.shields.io/npm/v/libp2p-udt.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-udt/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-udt.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-udt) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-udt.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-udt) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-udt/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-udt) | N/A |
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [![npm](https://img.shields.io/npm/v/libp2p-utp.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-utp/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-utp.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-utp) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-utp.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-utp) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-utp/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-utp) | N/A |
| [`libp2p-webrtc-direct`](//github.com/libp2p/js-libp2p-webrtc-direct) | [![npm](https://img.shields.io/npm/v/libp2p-webrtc-direct.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-webrtc-direct/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-webrtc-direct.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-webrtc-direct) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-webrtc-direct.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-webrtc-direct) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [![npm](https://img.shields.io/npm/v/libp2p-webrtc-star.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-webrtc-star.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-webrtc-star.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [![npm](https://img.shields.io/npm/v/libp2p-websockets.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-websockets/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-websockets.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websockets) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-websockets.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-websockets) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-websockets/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-websockets) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [![npm](https://img.shields.io/npm/v/libp2p-websocket-star.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-websocket-star/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-websocket-star.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-websocket-star.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-websocket-star/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-websocket-star-rendezvous`](//github.com/libp2p/js-libp2p-websocket-star-rendezvous) | [![npm](https://img.shields.io/npm/v/libp2p-websocket-star-rendezvous.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-websocket-star-rendezvous/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-websocket-star-rendezvous.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-websocket-star-rendezvous) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-websocket-star-rendezvous/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-websocket-star-rendezvous) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| **Crypto Channels** |
| [`libp2p-secio`](//github.com/libp2p/js-libp2p-secio) | [![npm](https://img.shields.io/npm/v/libp2p-secio.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-secio/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-secio.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-secio) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-secio.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-secio) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-secio/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-secio) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
| **Stream Muxers** |
@ -590,13 +585,6 @@ List of packages currently in existence for libp2p
| [`libp2p-rendezvous`](//github.com/libp2p/js-libp2p-rendezvous) | [![npm](https://img.shields.io/npm/v/libp2p-rendezvous.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-rendezvous/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-rendezvous.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-rendezvous) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-rendezvous.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-rendezvous) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-rendezvous/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-rendezvous) | N/A |
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [![npm](https://img.shields.io/npm/v/libp2p-webrtc-star.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-webrtc-star.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-webrtc-star.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [![npm](https://img.shields.io/npm/v/libp2p-websocket-star.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-websocket-star/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-websocket-star.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-websocket-star.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-websocket-star/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| **NAT Traversal** |
| [`libp2p-circuit`](//github.com/libp2p/js-libp2p-circuit) | [![npm](https://img.shields.io/npm/v/libp2p-circuit.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-circuit/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-circuit.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-circuit) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-circuit.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-circuit) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-circuit/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-circuit) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-nat-mngr`](//github.com/libp2p/js-libp2p-nat-mngr) | [![npm](https://img.shields.io/npm/v/libp2p-nat-mngr.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-nat-mngr/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-nat-mngr.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-nat-mngr) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-nat-mngr.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-nat-mngr) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-nat-mngr/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-nat-mngr) | N/A |
| **Data Types** |
| [`peer-book`](//github.com/libp2p/js-peer-book) | [![npm](https://img.shields.io/npm/v/peer-book.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-peer-book/releases) | [![Deps](https://david-dm.org/libp2p/js-peer-book.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-book) | [![Travis CI](https://travis-ci.com/libp2p/js-peer-book.svg?branch=master)](https://travis-ci.com/libp2p/js-peer-book) | [![codecov](https://codecov.io/gh/libp2p/js-peer-book/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-peer-book) | [Pedro Teixeira](mailto:i@pgte.me) |
| [`peer-id`](//github.com/libp2p/js-peer-id) | [![npm](https://img.shields.io/npm/v/peer-id.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-peer-id/releases) | [![Deps](https://david-dm.org/libp2p/js-peer-id.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-id) | [![Travis CI](https://travis-ci.com/libp2p/js-peer-id.svg?branch=master)](https://travis-ci.com/libp2p/js-peer-id) | [![codecov](https://codecov.io/gh/libp2p/js-peer-id/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-peer-id) | [Pedro Teixeira](mailto:i@pgte.me) |
| [`peer-info`](//github.com/libp2p/js-peer-info) | [![npm](https://img.shields.io/npm/v/peer-info.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-peer-info/releases) | [![Deps](https://david-dm.org/libp2p/js-peer-info.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-info) | [![Travis CI](https://travis-ci.com/libp2p/js-peer-info.svg?branch=master)](https://travis-ci.com/libp2p/js-peer-info) | [![codecov](https://codecov.io/gh/libp2p/js-peer-info/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-peer-info) | [Pedro Teixeira](mailto:i@pgte.me) |
| **Content Routing** |
| [`interface-content-routing`](//github.com/libp2p/interface-content-routing) | [![npm](https://img.shields.io/npm/v/interface-content-routing.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/interface-content-routing/releases) | [![Deps](https://david-dm.org/libp2p/interface-content-routing.svg?style=flat-square)](https://david-dm.org/libp2p/interface-content-routing) | [![Travis CI](https://travis-ci.com/libp2p/interface-content-routing.svg?branch=master)](https://travis-ci.com/libp2p/interface-content-routing) | [![codecov](https://codecov.io/gh/libp2p/interface-content-routing/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/interface-content-routing) | N/A |
| [`libp2p-delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [![npm](https://img.shields.io/npm/v/libp2p-delegated-content-routing.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-delegated-content-routing.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-delegated-content-routing) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
@ -605,22 +593,15 @@ List of packages currently in existence for libp2p
| [`interface-peer-routing`](//github.com/libp2p/interface-peer-routing) | [![npm](https://img.shields.io/npm/v/interface-peer-routing.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/interface-peer-routing/releases) | [![Deps](https://david-dm.org/libp2p/interface-peer-routing.svg?style=flat-square)](https://david-dm.org/libp2p/interface-peer-routing) | [![Travis CI](https://travis-ci.com/libp2p/interface-peer-routing.svg?branch=master)](https://travis-ci.com/libp2p/interface-peer-routing) | [![codecov](https://codecov.io/gh/libp2p/interface-peer-routing/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/interface-peer-routing) | N/A |
| [`libp2p-delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing) | [![npm](https://img.shields.io/npm/v/libp2p-delegated-peer-routing.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-delegated-peer-routing/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-delegated-peer-routing.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-delegated-peer-routing) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [![npm](https://img.shields.io/npm/v/libp2p-kad-dht.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-kad-dht/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-kad-dht.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-kad-dht.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-kad-dht/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| **Record Store** |
| [`interface-record-store`](//github.com/libp2p/interface-record-store) | [![npm](https://img.shields.io/npm/v/interface-record-store.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/interface-record-store/releases) | [![Deps](https://david-dm.org/libp2p/interface-record-store.svg?style=flat-square)](https://david-dm.org/libp2p/interface-record-store) | [![Travis CI](https://travis-ci.com/libp2p/interface-record-store.svg?branch=master)](https://travis-ci.com/libp2p/interface-record-store) | [![codecov](https://codecov.io/gh/libp2p/interface-record-store/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/interface-record-store) | N/A |
| [`libp2p-record`](//github.com/libp2p/js-libp2p-record) | [![npm](https://img.shields.io/npm/v/libp2p-record.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-record/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-record.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-record) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-record.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-record) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-record/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-record) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| **Generics** |
| [`libp2p-connection-manager`](//github.com/libp2p/js-libp2p-connection-manager) | [![npm](https://img.shields.io/npm/v/libp2p-connection-manager.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-connection-manager/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-connection-manager.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-connection-manager) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-connection-manager.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-connection-manager) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-connection-manager/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-connection-manager) | N/A |
| **Utilities** |
| [`libp2p-crypto`](//github.com/libp2p/js-libp2p-crypto) | [![npm](https://img.shields.io/npm/v/libp2p-crypto.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-crypto/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-crypto.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-crypto) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-crypto.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-crypto) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-crypto/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-crypto) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
| [`libp2p-crypto-secp256k1`](//github.com/libp2p/js-libp2p-crypto-secp256k1) | [![npm](https://img.shields.io/npm/v/libp2p-crypto-secp256k1.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-crypto-secp256k1/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-crypto-secp256k1.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-crypto-secp256k1) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
| [`libp2p-switch`](//github.com/libp2p/js-libp2p-switch) | [![npm](https://img.shields.io/npm/v/libp2p-switch.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-switch/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-switch.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-switch) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-switch.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-switch) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-switch/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-switch) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| **Data Types** |
| [`peer-book`](//github.com/libp2p/js-peer-book) | [![npm](https://img.shields.io/npm/v/peer-book.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-peer-book/releases) | [![Deps](https://david-dm.org/libp2p/js-peer-book.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-book) | [![Travis CI](https://travis-ci.com/libp2p/js-peer-book.svg?branch=master)](https://travis-ci.com/libp2p/js-peer-book) | [![codecov](https://codecov.io/gh/libp2p/js-peer-book/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-peer-book) | [Pedro Teixeira](mailto:i@pgte.me) |
| [`peer-id`](//github.com/libp2p/js-peer-id) | [![npm](https://img.shields.io/npm/v/peer-id.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-peer-id/releases) | [![Deps](https://david-dm.org/libp2p/js-peer-id.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-id) | [![Travis CI](https://travis-ci.com/libp2p/js-peer-id.svg?branch=master)](https://travis-ci.com/libp2p/js-peer-id) | [![codecov](https://codecov.io/gh/libp2p/js-peer-id/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-peer-id) | [Pedro Teixeira](mailto:i@pgte.me) |
| [`peer-info`](//github.com/libp2p/js-peer-info) | [![npm](https://img.shields.io/npm/v/peer-info.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-peer-info/releases) | [![Deps](https://david-dm.org/libp2p/js-peer-info.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-info) | [![Travis CI](https://travis-ci.com/libp2p/js-peer-info.svg?branch=master)](https://travis-ci.com/libp2p/js-peer-info) | [![codecov](https://codecov.io/gh/libp2p/js-peer-info/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-peer-info) | [Pedro Teixeira](mailto:i@pgte.me) |
| **Extensions** |
| [`libp2p-floodsub`](//github.com/libp2p/js-libp2p-floodsub) | [![npm](https://img.shields.io/npm/v/libp2p-floodsub.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-floodsub/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-floodsub.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-floodsub) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-floodsub.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-floodsub) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-floodsub/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-floodsub) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| [`libp2p-identify`](//github.com/libp2p/js-libp2p-identify) | [![npm](https://img.shields.io/npm/v/libp2p-identify.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-identify/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-identify.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-identify) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-identify.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-identify) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-identify/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-identify) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-keychain`](//github.com/libp2p/js-libp2p-keychain) | [![npm](https://img.shields.io/npm/v/libp2p-keychain.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-keychain/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-keychain.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-keychain) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-keychain.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-keychain) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-keychain/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-keychain) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| [`libp2p-ping`](//github.com/libp2p/js-libp2p-ping) | [![npm](https://img.shields.io/npm/v/libp2p-ping.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-ping/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-ping.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-ping) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-ping.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-ping) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-ping/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-ping) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-pnet`](//github.com/libp2p/js-libp2p-pnet) | [![npm](https://img.shields.io/npm/v/libp2p-pnet.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-pnet/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-pnet.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-pnet) | [![Travis CI](https://travis-ci.com/libp2p/js-libp2p-pnet.svg?branch=master)](https://travis-ci.com/libp2p/js-libp2p-pnet) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-pnet/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-pnet) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| **Utilities** |
| [`p2pcat`](//github.com/libp2p/js-p2pcat) | [![npm](https://img.shields.io/npm/v/p2pcat.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-p2pcat/releases) | [![Deps](https://david-dm.org/libp2p/js-p2pcat.svg?style=flat-square)](https://david-dm.org/libp2p/js-p2pcat) | [![Travis CI](https://travis-ci.com/libp2p/js-p2pcat.svg?branch=master)](https://travis-ci.com/libp2p/js-p2pcat) | [![codecov](https://codecov.io/gh/libp2p/js-p2pcat/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-p2pcat) | N/A |
## Contribute

164
doc/STREAMING_ITERABLES.md Normal file
View File

@ -0,0 +1,164 @@
# Iterable Streams
> This document is a guide on how to use Iterable Streams in Libp2p. As a part of the [refactor away from callbacks](https://github.com/ipfs/js-ipfs/issues/1670), we have also moved to using Iterable Streams instead of [pull-streams](https://pull-stream.github.io/). If there are missing usage guides you feel should be added, please submit a PR!
## Table of Contents
- [Iterable Streams](#iterable-streams)
- [Table of Contents](#table-of-contents)
- [Usage Guide](#usage-guide)
- [Transforming Bidirectional Data](#transforming-bidirectional-data)
- [Iterable Stream Types](#iterable-stream-types)
- [Source](#source)
- [Sink](#sink)
- [Transform](#transform)
- [Duplex](#duplex)
- [Iterable Modules](#iterable-modules)
## Usage Guide
### Transforming Bidirectional Data
Sometimes you may need to wrap an existing duplex stream in order to perform incoming and outgoing [transforms](#transform) on data. This type of wrapping is commonly used in stream encryption/decryption. Using [it-pair][it-pair] and [it-pipe][it-pipe], we can do this rather easily, given an existing [duplex iterable](#duplex).
```js
const duplexPair = require('it-pair/duplex')
const pipe = require('it-pipe')
// Wrapper is what we will write and read from
// This gives us two duplex iterables that are internally connected
const [internal, external] = duplexPair()
// Now we can pipe our wrapper to the existing duplex iterable
pipe(
external, // The external half of the pair interacts with the existing duplex
outgoingTransform, // A transform iterable to send data through (ie: encrypting)
existingDuplex, // The original duplex iterable we are wrapping
incomingTransform, // A transform iterable to read data through (ie: decrypting)
external
)
// We can now read and write from the other half of our pair
pipe(
['some data'],
internal, // The internal half of the pair is what we will interact with to read/write data
async (source) => {
for await (const chunk of source) {
console.log('Data: %s', chunk.toString())
// > Data: some data
}
}
)
```
## Iterable Stream Types
These types are pulled from [@alanshaw's gist](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9) on streaming iterables.
### Source
A "source" is something that can be consumed. It is an iterable object.
```js
const ints = {
[Symbol.asyncIterator] () {
let i = 0
return {
async next () {
return { done: false, value: i++ }
}
}
}
}
// or, more succinctly using a generator and for/await:
const ints = (async function * () {
let i = 0
while (true) yield i++
})()
```
### Sink
A "sink" is something that consumes (or drains) a source. It is a function that takes a source and iterates over it. It optionally returns a value.
```js
const logger = async source => {
const it = source[Symbol.asyncIterator]()
while (true) {
const { done, value } = await it.next()
if (done) break
console.log(value) // prints 0, 1, 2, 3...
}
}
// or, more succinctly using a generator and for/await:
const logger = async source => {
for await (const chunk of source) {
console.log(chunk) // prints 0, 1, 2, 3...
}
}
```
### Transform
A "transform" is both a sink _and_ a source where the values it consumes and the values that can be consumed from it are connected in some way. It is a function that takes a source and returns a source.
```js
const doubler = source => {
return {
[Symbol.asyncIterator] () {
const it = source[Symbol.asyncIterator]()
return {
async next () {
const { done, value } = await it.next()
if (done) return { done }
return { done, value: value * 2 }
}
return () {
return it.return && it.return()
}
}
}
}
}
// or, more succinctly using a generator and for/await:
const doubler = source => (async function * () {
for await (const chunk of source) {
yield chunk * 2
}
})()
```
### Duplex
A "duplex" is similar to a transform but the values it consumes are not necessarily connected to the values that can be consumed from it. It is an object with two properties, `sink` and `source`.
```js
const duplex = {
sink: async source => {/* ... */},
source: { [Symbol.asyncIterator] () {/* ... */} }
}
```
## Iterable Modules
- [it-handshake][it-handshake] Handshakes for binary protocols with iterable streams.
- [it-length-prefixed][it-length-prefixed] Streaming length prefixed buffers with async iterables.
- [it-pair][it-pair] Paired streams that are internally connected.
- [it-pipe][it-pipe] Create a pipeline of iterables. Works with duplex streams.
- [it-pushable][it-pushable] An iterable that you can push values into.
- [it-reader][it-reader] Read an exact number of bytes from a binary, async iterable.
- [streaming-iterables][streaming-iterables] A Swiss army knife for async iterables.
[it-handshake]: https://github.com/jacobheun/it-handshake
[it-length-prefixed]: https://github.com/alanshaw/it-length-prefixed
[it-pair]: https://github.com/alanshaw/it-pair
[it-pipe]: https://github.com/alanshaw/it-pipe
[it-pushable]: https://github.com/alanshaw/it-pushable
[it-reader]: https://github.com/alanshaw/it-reader
[streaming-iterables]: https://github.com/bustle/streaming-iterables

1
examples/pnet-ipfs/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tmp/

View File

@ -0,0 +1,29 @@
# Private Networking with IPFS
This example shows how to set up a private network of IPFS nodes.
## Setup
Install dependencies:
```
npm install
```
## Run
Running the example will cause two nodes with the same swarm key to be started and exchange basic information.
```
node index.js
```
### Using different keys
This example includes `TASK` comments that can be used to try the example with different swarm keys. This will
allow you to see how nodes will fail to connect if they are on different private networks and try to connect to
one another.
To change the swarm key of one of the nodes, look through `index.js` for comments starting with `TASK` to indicate
where lines are that pertain to changing the swarm key of node 2.
### Exploring the repos
Once you've run the example you can take a look at the repos in the `./tmp` directory to see how they differ, including
the swarm keys. You should see a `swarm.key` file in each of the repos and when the nodes are on the same private network
this contents of the `swarm.key` files should be the same.

145
examples/pnet-ipfs/index.js Normal file
View File

@ -0,0 +1,145 @@
/* eslint no-console: ["off"] */
'use strict'
const IPFS = require('ipfs')
const assert = require('assert').strict
const writeKey = require('libp2p-pnet').generate
const path = require('path')
const fs = require('fs')
const privateLibp2pBundle = require('./libp2p-bundle')
const { mkdirp } = require('./utils')
// Create two separate repo paths so we can run two nodes and check their output
const repo1 = path.resolve('./tmp', 'repo1', '.ipfs')
const repo2 = path.resolve('./tmp', 'repo2', '.ipfs')
mkdirp(repo1)
mkdirp(repo2)
// Create a buffer and write the swarm key to it
const swarmKey = Buffer.alloc(95)
writeKey(swarmKey)
// This key is for the `TASK` mentioned in the writeFileSync calls below
const otherSwarmKey = Buffer.alloc(95)
writeKey(otherSwarmKey)
// Add the swarm key to both repos
const swarmKey1Path = path.resolve(repo1, 'swarm.key')
const swarmKey2Path = path.resolve(repo2, 'swarm.key')
fs.writeFileSync(swarmKey1Path, swarmKey)
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
fs.writeFileSync(swarmKey2Path, swarmKey)
// fs.writeFileSync(swarmKey2Path, otherSwarmKey)
// Create the first ipfs node
const node1 = new IPFS({
repo: repo1,
libp2p: privateLibp2pBundle(swarmKey1Path),
config: {
Addresses: {
// Set the swarm address so we dont get port collision on the nodes
Swarm: ['/ip4/0.0.0.0/tcp/9101']
}
}
})
// Create the second ipfs node
const node2 = new IPFS({
repo: repo2,
libp2p: privateLibp2pBundle(swarmKey2Path),
config: {
Addresses: {
// Set the swarm address so we dont get port collision on the nodes
Swarm: ['/ip4/0.0.0.0/tcp/9102']
}
}
})
console.log('auto starting the nodes...')
// `nodesStarted` keeps track of how many of our nodes have started
let nodesStarted = 0
/**
* Calls `connectAndTalk` when both nodes have started
* @returns {void}
*/
const didStartHandler = () => {
if (++nodesStarted === 2) {
// If both nodes are up, start talking
connectAndTalk()
}
}
/**
* Exits the process when all started nodes have stopped
* @returns {void}
*/
const didStopHandler = () => {
if (--nodesStarted < 1) {
console.log('all nodes stopped, exiting.')
process.exit(0)
}
}
/**
* Stops the running nodes
* @param {Error} err An optional error to log to the console
* @returns {void}
*/
const doStop = (err) => {
if (err) {
console.error(err)
}
console.log('Shutting down...')
node1.stop()
node2.stop()
}
/**
* Connects the IPFS nodes and transfers data between them
* @returns {void}
*/
const connectAndTalk = async () => {
console.log('connecting the nodes...')
const node2Id = await node2.id()
const dataToAdd = Buffer.from('Hello, private friend!')
// Connect the nodes
// This will error when different private keys are used
try {
await node1.swarm.connect(node2Id.addresses[0])
} catch (err) {
return doStop(err)
}
console.log('the nodes are connected, let\'s add some data')
// Add some data to node 1
let addedCID
try {
addedCID = await node1.files.add(dataToAdd)
} catch (err) {
return doStop(err)
}
console.log(`added ${addedCID[0].path} to the node1`)
// Retrieve the data from node 2
let cattedData
try {
cattedData = await node2.files.cat(addedCID[0].path)
} catch (err) {
return doStop(err)
}
assert.deepEqual(cattedData.toString(), dataToAdd.toString(), 'Should have equal data')
console.log(`successfully retrieved "${dataToAdd.toString()}" from node2`)
doStop()
}
// Wait for the nodes to boot
node1.once('start', didStartHandler)
node2.once('start', didStartHandler)
// Listen for the nodes stopping so we can cleanup
node1.once('stop', didStopHandler)
node2.once('stop', didStopHandler)

View File

@ -0,0 +1,60 @@
'use strict'
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const fs = require('fs')
const Protector = require('libp2p-pnet')
/**
* Options for the libp2p bundle
* @typedef {Object} libp2pBundle~options
* @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node
* @property {PeerBook} peerBook - The PeerBook of the IPFS node
* @property {Object} config - The config of the IPFS node
* @property {Object} options - The options given to the IPFS node
*/
/**
* privateLibp2pBundle returns a libp2p bundle function that will use the swarm
* key at the given `swarmKeyPath` to create the Protector
*
* @param {string} swarmKeyPath The path to our swarm key
* @returns {libp2pBundle} Returns a libp2pBundle function for use in IPFS creation
*/
const privateLibp2pBundle = (swarmKeyPath) => {
/**
* This is the bundle we will use to create our fully customized libp2p bundle.
*
* @param {libp2pBundle~options} opts The options to use when generating the libp2p node
* @returns {Libp2p} Our new libp2p node
*/
const libp2pBundle = (opts) => {
// Set convenience variables to clearly showcase some of the useful things that are available
const peerInfo = opts.peerInfo
const peerBook = opts.peerBook
// Build and return our libp2p node
return new Libp2p({
peerInfo,
peerBook,
modules: {
transport: [TCP], // We're only using the TCP transport for this example
streamMuxer: [MPLEX], // We're only using mplex muxing
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
// care about node identity, and only the presence of private keys
connEncryption: [SECIO],
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
// being left in for explicit readability.
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
peerDiscovery: [],
connProtector: new Protector(fs.readFileSync(swarmKeyPath))
}
})
}
return libp2pBundle
}
module.exports = privateLibp2pBundle

View File

@ -0,0 +1,21 @@
{
"name": "pnet-ipfs-example",
"version": "1.0.0",
"description": "An example of private networking with IPFS",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ipfs": "~0.32.3",
"libp2p": "~0.23.1",
"libp2p-mplex": "~0.8.2",
"libp2p-pnet": "../../",
"libp2p-secio": "~0.10.0",
"libp2p-tcp": "~0.13.0"
}
}

View File

@ -0,0 +1,28 @@
'use strict'
const fs = require('fs')
const path = require('path')
/**
* mkdirp recursively creates needed folders for the given dir path
* @param {string} dir
* @returns {string} The path that was created
*/
module.exports.mkdirp = (dir) => {
return path
.resolve(dir)
.split(path.sep)
.reduce((acc, cur) => {
const currentPath = path.normalize(acc + path.sep + cur)
try {
fs.statSync(currentPath)
} catch (e) {
if (e.code === 'ENOENT') {
fs.mkdirSync(currentPath)
} else {
throw e
}
}
return currentPath
}, '')
}

View File

@ -11,6 +11,7 @@ const Gossipsub = require('libp2p-gossipsub')
const defaultsDeep = require('@nodeutils/defaults-deep')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
class MyBundle extends libp2p {
constructor (_options) {
@ -28,6 +29,10 @@ class MyBundle extends libp2p {
interval: 2000,
enabled: true
}
},
pubsub: {
enabled: true,
emitSelf: true
}
}
}
@ -63,19 +68,36 @@ parallel([
node1.once('peer:connect', (peer) => {
console.log('connected to %s', peer.id.toB58String())
// Subscribe to the topic 'news'
node1.pubsub.subscribe('news',
(msg) => console.log(msg.from, msg.data.toString()),
() => {
series([
// node1 subscribes to "news"
(cb) => node1.pubsub.subscribe(
'news',
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
cb
),
(cb) => setTimeout(cb, 500),
// node2 subscribes to "news"
(cb) => node2.pubsub.subscribe(
'news',
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
cb
),
(cb) => setTimeout(cb, 500),
// node2 publishes "news" every second
(cb) => {
setInterval(() => {
// Publish the message on topic 'news'
node2.pubsub.publish(
'news',
Buffer.from('Bird bird bird, bird is the word!'),
() => {}
)
}, 1000)
(err) => {
if (err) { throw err }
}
)
}, 1000)
cb()
},
], (err) => {
if (err) { throw err }
})
})
})

View File

@ -1,6 +1,6 @@
# Publish Subscribe
Publish Subscribe is also included on the stack. Currently, we have on PubSub implementation which we ship by default [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub), with many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub).
Publish Subscribe is also included on the stack. Currently, we have two PubSub implementation available [libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub) and [libp2p-gossipsub](https://github.com/ChainSafe/gossipsub-js), with many more being researched at [research-pubsub](https://github.com/libp2p/research-pubsub).
We've seen many interesting use cases appear with this, here are some highlights:
@ -12,26 +12,43 @@ We've seen many interesting use cases appear with this, here are some highlights
For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js).
Using PubSub is super simple, all you have to do is start a libp2p node with `EXPERIMENTAL.pubsub` set to true.
Using PubSub is super simple, you only need to provide the implementation of your choice and you are ready to go. No need for extra configuration.
```JavaScript
node1.once('peer:connect', (peer) => {
console.log('connected to %s', peer.id.toB58String())
// Subscribe to the topic 'news'
node1.pubsub.subscribe('news',
(msg) => console.log(msg.from, msg.data.toString()),
() => {
series([
// node1 subscribes to "news"
(cb) => node1.pubsub.subscribe(
'news',
(msg) => console.log(`node1 received: ${msg.data.toString()}`),
cb
),
(cb) => setTimeout(cb, 500),
// node2 subscribes to "news"
(cb) => node2.pubsub.subscribe(
'news',
(msg) => console.log(`node2 received: ${msg.data.toString()}`),
cb
),
(cb) => setTimeout(cb, 500),
// node2 publishes "news" every second
(cb) => {
setInterval(() => {
// Publish the message on topic 'news'
node2.pubsub.publish(
'news',
Buffer.from('Bird bird bird, bird is the word!'),
() => {}
)
}, 1000)
(err) => {
if (err) { throw err }
}
)
}, 1000)
cb()
},
], (err) => {
if (err) { throw err }
})
})
```
@ -40,11 +57,29 @@ The output of the program should look like:
```
> node 1.js
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
node2 received: Bird bird bird, bird is the word!
node1 received: Bird bird bird, bird is the word!
node2 received: Bird bird bird, bird is the word!
node1 received: Bird bird bird, bird is the word!
```
You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages.
```JavaScript
const defaults = {
config: {
peerDiscovery: {
mdns: {
interval: 2000,
enabled: true
}
},
pubsub: {
enabled: true,
emitSelf: false
}
}
}
```
## 2. Future work

View File

@ -3,7 +3,7 @@
"Package",
"Version",
"Deps",
"CI/Travis",
"CI",
"Coverage",
"Lead Maintainer"
],
@ -18,14 +18,11 @@
"Transport",
["libp2p/interface-transport", "interface-transport"],
["libp2p/js-libp2p-tcp", "libp2p-tcp"],
["libp2p/js-libp2p-udp", "libp2p-udp"],
["libp2p/js-libp2p-udt", "libp2p-udt"],
["libp2p/js-libp2p-utp", "libp2p-utp"],
["libp2p/js-libp2p-webrtc-direct", "libp2p-webrtc-direct"],
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
["libp2p/js-libp2p-websockets", "libp2p-websockets"],
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
["libp2p/js-libp2p-websocket-star-rendezvous", "libp2p-websocket-star-rendezvous"],
"Crypto Channels",
["libp2p/js-libp2p-secio", "libp2p-secio"],
@ -44,15 +41,6 @@
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
"NAT Traversal",
["libp2p/js-libp2p-circuit", "libp2p-circuit"],
["libp2p/js-libp2p-nat-mngr", "libp2p-nat-mngr"],
"Data Types",
["libp2p/js-peer-book", "peer-book"],
["libp2p/js-peer-id", "peer-id"],
["libp2p/js-peer-info", "peer-info"],
"Content Routing",
["libp2p/interface-content-routing", "interface-content-routing"],
["libp2p/js-libp2p-delegated-content-routing", "libp2p-delegated-content-routing"],
@ -63,24 +51,16 @@
["libp2p/js-libp2p-delegated-peer-routing", "libp2p-delegated-peer-routing"],
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
"Record Store",
["libp2p/interface-record-store", "interface-record-store"],
["libp2p/js-libp2p-record", "libp2p-record"],
"Generics",
["libp2p/js-libp2p-connection-manager", "libp2p-connection-manager"],
"Utilities",
["libp2p/js-libp2p-crypto", "libp2p-crypto"],
["libp2p/js-libp2p-crypto-secp256k1", "libp2p-crypto-secp256k1"],
["libp2p/js-libp2p-switch", "libp2p-switch"],
"Data Types",
["libp2p/js-peer-book", "peer-book"],
["libp2p/js-peer-id", "peer-id"],
["libp2p/js-peer-info", "peer-info"],
"Extensions",
["libp2p/js-libp2p-floodsub", "libp2p-floodsub"],
["libp2p/js-libp2p-identify", "libp2p-identify"],
["libp2p/js-libp2p-keychain", "libp2p-keychain"],
["libp2p/js-libp2p-ping", "libp2p-ping"],
["libp2p/js-libp2p-pnet", "libp2p-pnet"],
"Utilities",
["libp2p/js-p2pcat", "p2pcat"]
["libp2p/js-libp2p-floodsub", "libp2p-floodsub"]
]
}

View File

@ -1,6 +1,6 @@
{
"name": "libp2p",
"version": "0.26.0-rc.3",
"version": "0.26.2",
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
@ -16,7 +16,8 @@
"test:browser": "aegir test -t browser",
"release": "aegir release -t node -t browser",
"release-minor": "aegir release --type minor -t node -t browser",
"release-major": "aegir release --type major -t node -t browser"
"release-major": "aegir release --type major -t node -t browser",
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
},
"repository": {
"type": "git",
@ -35,63 +36,84 @@
},
"homepage": "https://libp2p.io",
"license": "MIT",
"browser": {
"./test/utils/bundle-nodejs": "./test/utils/bundle-browser"
},
"engines": {
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
"dependencies": {
"abort-controller": "^3.0.0",
"async": "^2.6.2",
"bignumber.js": "^9.0.0",
"class-is": "^1.1.0",
"debug": "^4.1.1",
"err-code": "^1.1.2",
"fsm-event": "^2.1.0",
"libp2p-connection-manager": "^0.1.0",
"libp2p-ping": "^0.8.5",
"libp2p-switch": "^0.43.0",
"libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7",
"multiaddr": "^6.1.0",
"hashlru": "^2.3.0",
"it-handshake": "^1.0.1",
"it-length-prefixed": "^3.0.0",
"it-pipe": "^1.1.0",
"it-protocol-buffers": "^0.2.0",
"latency-monitor": "~0.2.1",
"libp2p-crypto": "^0.17.1",
"libp2p-interfaces": "^0.1.5",
"mafmt": "^7.0.0",
"merge-options": "^1.0.1",
"moving-average": "^1.0.0",
"multiaddr": "^7.1.0",
"multistream-select": "^0.15.0",
"once": "^1.4.0",
"peer-book": "^0.9.1",
"peer-id": "^0.12.2",
"peer-info": "^0.15.1",
"p-map": "^3.0.0",
"p-queue": "^6.1.1",
"p-settle": "^3.1.0",
"peer-id": "^0.13.3",
"peer-info": "^0.17.0",
"promisify-es6": "^1.0.3",
"superstruct": "^0.6.0"
"protons": "^1.0.1",
"pull-cat": "^1.1.11",
"pull-handshake": "^1.1.4",
"pull-stream": "^3.6.9",
"retimer": "^2.0.0",
"xsalsa20": "^1.0.2"
},
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"abortable-iterator": "^2.1.0",
"aegir": "^20.0.0",
"chai": "^4.2.0",
"chai-checkmark": "^1.0.1",
"cids": "^0.7.1",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"electron-webrtc": "^0.3.0",
"glob": "^7.1.4",
"interface-datastore": "^0.6.0",
"it-pair": "^1.0.0",
"libp2p-bootstrap": "^0.9.7",
"libp2p-circuit": "^0.3.7",
"libp2p-delegated-content-routing": "^0.2.2",
"libp2p-delegated-peer-routing": "^0.2.2",
"libp2p-floodsub": "~0.17.0",
"libp2p-gossipsub": "~0.0.4",
"libp2p-floodsub": "^0.19.0",
"libp2p-gossipsub": "ChainSafe/gossipsub-js#beta/async",
"libp2p-kad-dht": "^0.15.3",
"libp2p-mdns": "^0.12.3",
"libp2p-mplex": "^0.8.4",
"libp2p-mplex": "^0.9.1",
"libp2p-pnet": "~0.1.0",
"libp2p-secio": "^0.11.1",
"libp2p-spdy": "^0.13.2",
"libp2p-tcp": "^0.13.0",
"libp2p-webrtc-star": "^0.16.1",
"libp2p-websocket-star": "~0.10.2",
"libp2p-websocket-star-rendezvous": "~0.4.1",
"libp2p-tcp": "^0.14.1",
"libp2p-websockets": "^0.13.1",
"lodash.times": "^4.3.2",
"merge-options": "^1.0.1",
"nock": "^10.0.6",
"p-defer": "^3.0.0",
"p-wait-for": "^3.1.0",
"portfinder": "^1.0.20",
"pull-goodbye": "0.0.2",
"pull-length-prefixed": "^1.3.3",
"pull-mplex": "^0.1.2",
"pull-pair": "^1.1.0",
"pull-protocol-buffers": "~0.1.2",
"pull-serializer": "^0.3.2",
"pull-stream": "^3.6.12",
"sinon": "^7.2.7",
"streaming-iterables": "^4.1.0",
"wrtc": "^0.4.1"
},
"contributors": [
@ -107,6 +129,7 @@
"Diogo Silva <fsdiogo@gmail.com>",
"Dmitriy Ryajov <dryajov@gmail.com>",
"Elven <mon.samuel@qq.com>",
"Fei Liu <liu.feiwood@gmail.com>",
"Florian-Merle <florian.david.merle@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Giovanni T. Parra <fiatjaf@gmail.com>",
@ -138,10 +161,12 @@
"Volker Mische <volker.mische@gmail.com>",
"Yusef Napora <yusef@napora.org>",
"Zane Starr <zcstarr@gmail.com>",
"a1300 <a1300@users.noreply.github.com>",
"ebinks <elizabethjbinks@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>",
"isan_rivkin <isanrivkin@gmail.com>",
"mayerwin <mayerwin@users.noreply.github.com>",
"swedneck <40505480+swedneck@users.noreply.github.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
]
}

View File

@ -0,0 +1,128 @@
EDIT: This document is outdated and here only for historical purposes
NOTE: This document is structured in an `if-then/else[if]-then` manner, each line is a precondition for following lines with a higher number of indentation
Example:
- if there are apples
- eat them
- if not, check for pears
- then eat them
- if not, check for cherries
- then eat them
Or,
- if there are apples
- eat them
- if not
- check for pears
- then eat them
- if not
- check for cherries
- then eat them
In order to minimize nesting, the first example is preferred
# Relay flow
## Relay transport (dialer/listener)
- ### Dial over a relay
- See if there is a relay that's already connected to the destination peer, if not
- Ask all the peer's known relays to dial the destination peer until an active relay (one that can dial on behalf of other peers), or a relay that may have recently acquired a connection to the destination peer is successful.
- If successful
- Write the `/ipfs/relay/circuit/1.0.0` header to the relay, followed by the destination address
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmDest`.
- If no relays could connect, fail the same way a regular transport would
- Once the connection has been established, the swarm should treat it as a regular connection,
- i.e. muxing, encrypt, etc should all be performed on the relayed connection
- ### Listen for relayed connections
- Peer mounts the `/ipfs/relay/circuit/1.0.0` proto and listens for relayed connections
- A connection arrives
- read the address of the source peer from the incoming connection stream
- if valid, create a PeerInfo object for that peer and add the incoming address to its multiaddresses list
- pass the connection to `protocolMuxer(swarm.protocols, conn)` to have it go through the regular muxing/encryption flow
- ### Relay discovery and static relay addresses in swarm config
- #### Relay address in swarm config
- A peer has relay addresses in its swarm config section
- On node startup, connect to the relays in swarm config
- if successful add address to swarms PeerInfo's multiaddresses
- `identify` should take care of announcing that the peer is reachable over the listed relays
- #### Passive relay discovery
- A peer that can dial over `/ipfs/relay/circuit/1.0.0` listens for the `peer-mux-established` swarm event, every time a new muxed connection arrives, it checks if the incoming peer is a relay. (How would this work? Some way of discovering if its a relay is required.)
- *Useful in cases when the peer/node doesn't know of any relays on startup and also, to learn of as many additional relays in the network as possible*
- *Useful during startup, when connecting to bootstrap nodes. It allows us to implicitly learn if its a relay without having to explicitly add `/p2p-circuit` addresses to the bootstrap list*
- *Also useful if the relay communicates its capabilities upon connecting to it, as to avoid additional unnecessary requests/queries. I.e. if it supports weather its able to forward connections and weather it supports the `ls` or other commands.*
- *Should it be possible to disable passive relay discovery?*
- This could be useful when the peer wants to be reachable **only** over the listed relays
- If the incoming peer is a relay, send an `ls` and record its peers
## Relay Nodes
- ### Passive relay node
- *A passive relay does not explicitly dial into any requested peer, only those that it's swarm already has connections to.*
- When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
- check its swarm's peerbook(?) see if its a known peer, if it is
- use the swarms existing connection and
- send the multistream header and the source peer address to the dest peer
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
- circuit the source and dest connections
- if couldn't dial, or the connection/stream to the dest peer closed prematurelly
- close the src stream
- ### Active relay node
- *An active relay node can dial other peers even if its swarm doesnt know about those peers*
- When the relay gets a request, read the the destination peer's multiaddr from the connection stream and if its a valid address and peer id
- use the swarm to dial to the dest node
- send the multistream header and the source peer address to the dest peer
- e.g. `/ipfs/relay/circuit/1.0.0\n/p2p-circuit/ipfs/QmSource`
- circuit the source and dest connections
- if couldn't dial, or the connection/stream to the dest peer closed prematurely
- close the src stream
- ### `ls` command
- *A relay node can allow the peers known to it's swarm to be listed*
- *this should be possible to enable/disable from the config*
- when a relay gets the `ls` request
- if enabled, get its swarm's peerbook's known peers and return their ids and multiaddrs
- e.g `[{id: /ipfs/QmPeerId, addrs: ['ma1', 'ma2', 'ma3']}, ...]`
- if disabled, respond with `na`
## Relay Implementation notes
- ### Relay transport
- Currently I've implemented the dialer and listener parts of the relay as a transport, meaning that it *tries* to implement the `interface-transport` interface as closely as possible. This seems to work pretty well and it's makes the dialer/listener parts really easy to plug in into the swarm. I think this is the cleanest solution.
- ### `circuit-relay`
- This is implemented as a separate piece (not a transport), and it can be enabled/disabled with a config. The transport listener however, will do the initial parsing of the incoming header and figure out weather it's a connection that's needs to be handled by the circuit-relay, or its a connection that is being relayed from a circuit-relay.
## Relay swarm integration
- The relay transport is mounted explicitly by calling the `swarm.connection.relay(config.relay)` from libp2p
- Swarm will register the dialer and listener using the swarm `transport.add` and `transport.listen` methods
- ### Listener
- the listener registers itself as a multistream handler on the `/ipfs/relay/circuit/1.0.0` proto
- if `circuit-relay` is enabled, the listener will delegate connections to it if appropriate
- when the listener receives a connection, it will read the multiaddr and determine if its a connection that needs to be relayed, or its a connection that is being relayed
- ### Dialer
- When the swarm attempts to dial to a peer, it will filter the protocols that the peer can be reached on
- *The relay will be used in two cases*
- If the peer has an explicit relay address that it can be reached on
- no other transport is available
- The relay will attempt to dial the peer over that relay
- If no explicit relay address is provided
- no other transport is available
- A generic circuit address will be added to the peers multiaddr list
- i.e. `/p2p-circuit/ipfs/QmDest`
- If another transport is available, then use that instead of the relay

159
src/circuit/README.md Normal file
View File

@ -0,0 +1,159 @@
# js-libp2p-circuit
> Node.js implementation of the Circuit module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) interface for dial/listen.
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-circuit.
`libp2p-circuit` implements the circuit-relay mechanism that allows nodes that don't speak the same protocol to communicate using a third _relay_ node.
This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
### Why?
`circuit-relaying` uses additional nodes in order to transfer traffic between two otherwise unreachable nodes. This allows nodes that don't speak the same protocols or are running in limited environments, e.g. browsers and IoT devices, to communicate, which would otherwise be impossible given the fact that for example browsers don't have any socket support and as such cannot be directly dialed.
The use of circuit-relaying is not limited to routing traffic between browser nodes, other uses include:
- routing traffic between private nets and circumventing NAT layers
- route mangling for better privacy (matreshka/shallot dialing).
It's also possible to use it for clients that implement exotic transports such as devices that only have bluetooth radios to be reachable over bluetooth enabled relays and become full p2p nodes.
### libp2p-circuit and IPFS
Prior to `libp2p-circuit` there was a rift in the IPFS network, were IPFS nodes could only access content from nodes that speak the same protocol, for example TCP only nodes could only dial to other TCP only nodes, same for any other protocol combination. In practice, this limitation was most visible in JS-IPFS browser nodes, since they can only dial out but not be dialed in over WebRTC or WebSockets, hence any content that the browser node held was not reachable by the rest of the network even through it was announced on the DHT. Non browser IPFS nodes would usually speak more than one protocol such as TCP, WebSockets and/or WebRTC, this made the problem less severe outside of the browser. `libp2p-circuit` solves this problem completely, as long as there are `relay nodes` capable of routing traffic between those nodes their content should be available to the rest of the IPFS network.
## Table of Contents
- [js-libp2p-circuit](#js-libp2p-circuit)
- [Why?](#why)
- [libp2p-circuit and IPFS](#libp2p-circuit-and-ipfs)
- [Table of Contents](#table-of-contents)
- [Usage](#usage)
- [Example](#example)
- [Create dialer/listener](#create-dialerlistener)
- [Create `relay`](#create-relay)
- [This module uses `pull-streams`](#this-module-uses-pull-streams)
- [Converting `pull-streams` to Node.js Streams](#converting-pull-streams-to-nodejs-streams)
- [API](#api)
- [Implementation rational](#implementation-rational)
## Usage
### Example
#### Create dialer/listener
```js
const Circuit = require('libp2p-circuit')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const mh1 = multiaddr('/p2p-circuit/ipfs/QmHash') // dial /ipfs/QmHash over any circuit
const circuit = new Circuit(swarmInstance, options) // pass swarm instance and options
const listener = circuit.createListener(mh1, (connection) => {
console.log('new connection opened')
pull(
pull.values(['hello']),
socket
)
})
listener.listen(() => {
console.log('listening')
pull(
circuit.dial(mh1),
pull.log,
pull.onEnd(() => {
circuit.close()
})
)
})
```
Outputs:
```sh
listening
new connection opened
hello
```
#### Create `relay`
```js
const Relay = require('libp2p-circuit').Relay
const relay = new Relay(options)
relay.mount(swarmInstance) // start relaying traffic
```
### This module uses `pull-streams`
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
You can learn more about pull-streams at:
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
- [pull-streams documentation](https://pull-stream.github.io/)
#### Converting `pull-streams` to Node.js Streams
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/dominictarr/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
```js
const pullToStream = require('pull-stream-to-stream')
const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
```
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
## API
[![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)](https://github.com/libp2p/interface-transport)
`libp2p-circuit` accepts Circuit addresses for both IPFS and non IPFS encapsulated addresses, i.e:
`/p2p-circuit/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
Both for dialing and listening.
### Implementation rational
This module is not a transport, however it implements `interface-transport` interface in order to allow circuit to be plugged with `libp2p-swarm`. The rational behind it is that, `libp2p-circuit` has a dial and listen flow, which fits nicely with other transports, moreover, it requires the _raw_ connection to be encrypted and muxed just as a regular transport's connection does. All in all, `interface-transport` ended up being the correct level of abstraction for circuit, as well as allowed us to reuse existing integration points in `libp2p-swarm` and `libp2p` without adding any ad-hoc logic. All parts of `interface-transport` are used, including `.getAddr` which returns a list of `/p2p-circuit` addresses that circuit is currently listening.
```
libp2p libp2p-circuit (transport)
+-------------------------------------------------+ +--------------------------+
| +---------------------------------+ | | |
| | | | | +------------------+ |
| | | | circuit-relay listens for the HOP | | | |
| | libp2p-swarm <------------------------------------------------| circuit-relay | |
| | | | message to handle incomming relay | | | |
| | | | requests from other nodes | +------------------+ |
| +---------------------------------+ | | |
| ^ ^ ^ ^ ^ ^ | | +------------------+ |
| | | | | | | | | | +-------------+ | |
| | | | | | | | dialer uses libp2p-swarm to dial | | | | | |
| | | | +----------------------------------------------------------------------> dialer | | |
| | | transports | | to a circuit-relay node using the | | | | | |
| | | | | | | HOP message | | +-------------+ | |
| | | | | | | | | | |
| v v | v v | | | | |
|+------------------|----------------------------+| | | +-------------+ | |
|| | | | | || | | | | | |
||libp2p-tcp |libp2p-ws | .... |libp2p-circuit || listener handles STOP messages from| | | listener | | |
|| | +--------------------------------------------------------------------------> | | |
|| | | |plugs in just || circuit-relay nodes | | +-------------+ | |
|| | | |as any other || | | | |
|| | | |transport || | +------------------+ |
|+-----------------------------------------------+| | |
+-------------------------------------------------+ +--------------------------+
```

126
src/circuit/circuit.js Normal file
View File

@ -0,0 +1,126 @@
'use strict'
const mafmt = require('mafmt')
const multiaddr = require('multiaddr')
const CircuitDialer = require('./circuit/dialer')
const utilsFactory = require('./circuit/utils')
const debug = require('debug')
const log = debug('libp2p:circuit:transportdialer')
log.err = debug('libp2p:circuit:error:transportdialer')
const createListener = require('./listener')
class Circuit {
static get tag () {
return 'Circuit'
}
/**
* Creates an instance of Dialer.
*
* @param {Swarm} swarm - the swarm
* @param {any} options - config options
*
* @memberOf Dialer
*/
constructor (swarm, options) {
this.options = options || {}
this.swarm = swarm
this.dialer = null
this.utils = utilsFactory(swarm)
this.peerInfo = this.swarm._peerInfo
this.relays = this.filter(this.peerInfo.multiaddrs.toArray())
// if no explicit relays, add a default relay addr
if (this.relays.length === 0) {
this.peerInfo
.multiaddrs
.add(`/p2p-circuit/ipfs/${this.peerInfo.id.toB58String()}`)
}
this.dialer = new CircuitDialer(swarm, options)
this.swarm.on('peer-mux-established', (peerInfo) => {
this.dialer.canHop(peerInfo)
})
this.swarm.on('peer-mux-closed', (peerInfo) => {
this.dialer.relayPeers.delete(peerInfo.id.toB58String())
})
}
/**
* Dial the relays in the Addresses.Swarm config
*
* @param {Array} relays
* @return {void}
*/
_dialSwarmRelays () {
// if we have relay addresses in swarm config, then dial those relays
this.relays.forEach((relay) => {
const relaySegments = relay
.toString()
.split('/p2p-circuit')
.filter(segment => segment.length)
relaySegments.forEach((relaySegment) => {
const ma = this.utils.peerInfoFromMa(multiaddr(relaySegment))
this.dialer._dialRelay(ma)
})
})
}
/**
* Dial a peer over a relay
*
* @param {multiaddr} ma - the multiaddr of the peer to dial
* @param {Object} options - dial options
* @param {Function} cb - a callback called once dialed
* @returns {Connection} - the connection
*
* @memberOf Dialer
*/
dial (ma, options, cb) {
return this.dialer.dial(ma, options, cb)
}
/**
* Create a listener
*
* @param {any} options
* @param {Function} handler
* @return {listener}
*/
createListener (options, handler) {
if (typeof options === 'function') {
handler = options
options = this.options || {}
}
const listener = createListener(this.swarm, options, handler)
listener.on('listen', this._dialSwarmRelays.bind(this))
return listener
}
/**
* Filter check for all multiaddresses
* that this transport can dial on
*
* @param {any} multiaddrs
* @returns {Array<multiaddr>}
*
* @memberOf Dialer
*/
filter (multiaddrs) {
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
return multiaddrs.filter((ma) => {
return mafmt.Circuit.matches(ma)
})
}
}
module.exports = Circuit

View File

@ -0,0 +1,275 @@
'use strict'
const once = require('once')
const PeerId = require('peer-id')
const waterfall = require('async/waterfall')
const setImmediate = require('async/setImmediate')
const multiaddr = require('multiaddr')
const { Connection } = require('libp2p-interfaces/src/connection')
const utilsFactory = require('./utils')
const StreamHandler = require('./stream-handler')
const debug = require('debug')
const log = debug('libp2p:circuit:dialer')
log.err = debug('libp2p:circuit:error:dialer')
const multicodec = require('../multicodec')
const proto = require('../protocol')
class Dialer {
/**
* Creates an instance of Dialer.
* @param {Swarm} swarm - the swarm
* @param {any} options - config options
*
* @memberOf Dialer
*/
constructor (swarm, options) {
this.swarm = swarm
this.relayPeers = new Map()
this.relayConns = new Map()
this.options = options
this.utils = utilsFactory(swarm)
}
/**
* Helper that returns a relay connection
*
* @param {*} relay
* @param {*} callback
* @returns {Function} - callback
*/
_dialRelayHelper (relay, callback) {
if (this.relayConns.has(relay.id.toB58String())) {
return callback(null, this.relayConns.get(relay.id.toB58String()))
}
return this._dialRelay(relay, callback)
}
/**
* Dial a peer over a relay
*
* @param {multiaddr} ma - the multiaddr of the peer to dial
* @param {Function} cb - a callback called once dialed
* @returns {Connection} - the connection
*
*/
dial (ma, cb) {
cb = cb || (() => { })
const strMa = ma.toString()
if (!strMa.includes('/p2p-circuit')) {
log.err('invalid circuit address')
return cb(new Error('invalid circuit address'))
}
const addr = strMa.split('p2p-circuit') // extract relay address if any
const relay = addr[0] === '/' ? null : multiaddr(addr[0])
const peer = multiaddr(addr[1] || addr[0])
const dstConn = new Connection()
setImmediate(
this._dialPeer.bind(this),
peer,
relay,
(err, conn) => {
if (err) {
log.err(err)
return cb(err)
}
dstConn.setInnerConn(conn)
cb(null, dstConn)
})
return dstConn
}
/**
* Does the peer support the HOP protocol
*
* @param {PeerInfo} peer
* @param {Function} callback
* @returns {void}
*/
canHop (peer, callback) {
callback = once(callback || (() => { }))
this._dialRelayHelper(peer, (err, conn) => {
if (err) {
return callback(err)
}
const sh = new StreamHandler(conn)
waterfall([
(cb) => sh.write(proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.CAN_HOP
}), cb),
(cb) => sh.read(cb)
], (err, msg) => {
if (err) {
return callback(err)
}
const response = proto.CircuitRelay.decode(msg)
if (response.code !== proto.CircuitRelay.Status.SUCCESS) {
const err = new Error(`HOP not supported, skipping - ${this.utils.getB58String(peer)}`)
log(err)
return callback(err)
}
log('HOP supported adding as relay - %s', this.utils.getB58String(peer))
this.relayPeers.set(this.utils.getB58String(peer), peer)
sh.close()
callback()
})
})
}
/**
* Dial the destination peer over a relay
*
* @param {multiaddr} dstMa
* @param {Connection|PeerInfo} relay
* @param {Function} cb
* @return {Function|void}
* @private
*/
_dialPeer (dstMa, relay, cb) {
if (typeof relay === 'function') {
cb = relay
relay = null
}
if (!cb) {
cb = () => {}
}
dstMa = multiaddr(dstMa)
// if no relay provided, dial on all available relays until one succeeds
if (!relay) {
const relays = Array.from(this.relayPeers.values())
const next = (nextRelay) => {
if (!nextRelay) {
const err = 'no relay peers were found or all relays failed to dial'
log.err(err)
return cb(err)
}
return this._negotiateRelay(
nextRelay,
dstMa,
(err, conn) => {
if (err) {
log.err(err)
return next(relays.shift())
}
cb(null, conn)
})
}
next(relays.shift())
} else {
return this._negotiateRelay(
relay,
dstMa,
(err, conn) => {
if (err) {
log.err('An error has occurred negotiating the relay connection', err)
return cb(err)
}
return cb(null, conn)
})
}
}
/**
* Negotiate the relay connection
*
* @param {Multiaddr|PeerInfo|Connection} relay - the Connection or PeerInfo of the relay
* @param {multiaddr} dstMa - the multiaddr of the peer to relay the connection for
* @param {Function} callback - a callback which gets the negotiated relay connection
* @returns {void}
* @private
*
* @memberOf Dialer
*/
_negotiateRelay (relay, dstMa, callback) {
dstMa = multiaddr(dstMa)
relay = this.utils.peerInfoFromMa(relay)
const srcMas = this.swarm._peerInfo.multiaddrs.toArray()
this._dialRelayHelper(relay, (err, conn) => {
if (err) {
log.err(err)
return callback(err)
}
const sh = new StreamHandler(conn)
waterfall([
(cb) => {
log('negotiating relay for peer %s', dstMa.getPeerId())
let dstPeerId
try {
dstPeerId = PeerId.createFromB58String(dstMa.getPeerId()).id
} catch (err) {
return cb(err)
}
sh.write(
proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.HOP,
srcPeer: {
id: this.swarm._peerInfo.id.id,
addrs: srcMas.map((addr) => addr.buffer)
},
dstPeer: {
id: dstPeerId,
addrs: [dstMa.buffer]
}
}), cb)
},
(cb) => sh.read(cb)
], (err, msg) => {
if (err) {
return callback(err)
}
const message = proto.CircuitRelay.decode(msg)
if (message.type !== proto.CircuitRelay.Type.STATUS) {
return callback(new Error('Got invalid message type - ' +
`expected ${proto.CircuitRelay.Type.STATUS} got ${message.type}`))
}
if (message.code !== proto.CircuitRelay.Status.SUCCESS) {
return callback(new Error(`Got ${message.code} error code trying to dial over relay`))
}
callback(null, new Connection(sh.rest()))
})
})
}
/**
* Dial a relay peer by its PeerInfo
*
* @param {PeerInfo} peer - the PeerInfo of the relay peer
* @param {Function} cb - a callback with the connection to the relay peer
* @returns {void}
* @private
*/
_dialRelay (peer, cb) {
cb = once(cb || (() => { }))
this.swarm.dial(
peer,
multicodec.relay,
once((err, conn) => {
if (err) {
log.err(err)
return cb(err)
}
cb(null, conn)
}))
}
}
module.exports = Dialer

283
src/circuit/circuit/hop.js Normal file
View File

@ -0,0 +1,283 @@
'use strict'
const pull = require('pull-stream/pull')
const debug = require('debug')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const EE = require('events').EventEmitter
const once = require('once')
const utilsFactory = require('./utils')
const StreamHandler = require('./stream-handler')
const proto = require('../protocol').CircuitRelay
const multiaddr = require('multiaddr')
const series = require('async/series')
const waterfall = require('async/waterfall')
const setImmediate = require('async/setImmediate')
const multicodec = require('./../multicodec')
const log = debug('libp2p:circuit:relay')
log.err = debug('libp2p:circuit:error:relay')
class Hop extends EE {
/**
* Construct a Circuit object
*
* This class will handle incoming circuit connections and
* either start a relay or hand the relayed connection to
* the swarm
*
* @param {Swarm} swarm
* @param {Object} options
*/
constructor (swarm, options) {
super()
this.swarm = swarm
this.peerInfo = this.swarm._peerInfo
this.utils = utilsFactory(swarm)
this.config = options || { active: false, enabled: false }
this.active = this.config.active
}
/**
* Handle the relay message
*
* @param {CircuitRelay} message
* @param {StreamHandler} sh
* @returns {*}
*/
handle (message, sh) {
if (!this.config.enabled) {
this.utils.writeResponse(
sh,
proto.Status.HOP_CANT_SPEAK_RELAY)
return sh.close()
}
// check if message is `CAN_HOP`
if (message.type === proto.Type.CAN_HOP) {
this.utils.writeResponse(
sh,
proto.Status.SUCCESS)
return sh.close()
}
// This is a relay request - validate and create a circuit
let srcPeerId = null
let dstPeerId = null
try {
srcPeerId = PeerId.createFromBytes(message.srcPeer.id).toB58String()
dstPeerId = PeerId.createFromBytes(message.dstPeer.id).toB58String()
} catch (err) {
log.err(err)
if (!srcPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_SRC_MULTIADDR_INVALID)
return sh.close()
}
if (!dstPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_DST_MULTIADDR_INVALID)
return sh.close()
}
}
if (srcPeerId === dstPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_CANT_RELAY_TO_SELF)
return sh.close()
}
if (!message.dstPeer.addrs.length) {
// TODO: use encapsulate here
const addr = multiaddr(`/p2p-circuit/ipfs/${dstPeerId}`).buffer
message.dstPeer.addrs.push(addr)
}
log('trying to establish a circuit: %s <-> %s', srcPeerId, dstPeerId)
const noPeer = () => {
// log.err(err)
this.utils.writeResponse(
sh,
proto.Status.HOP_NO_CONN_TO_DST)
return sh.close()
}
const isConnected = (cb) => {
let dstPeer
try {
dstPeer = this.swarm._peerBook.get(dstPeerId)
if (!dstPeer.isConnected() && !this.active) {
const err = new Error(`No Connection to peer ${dstPeerId}`)
noPeer(err)
return cb(err)
}
} catch (err) {
if (!this.active) {
noPeer(err)
return cb(err)
}
}
cb()
}
series([
(cb) => this.utils.validateAddrs(message, sh, proto.Type.HOP, cb),
(cb) => isConnected(cb),
(cb) => this._circuit(sh, message, cb)
], (err) => {
if (err) {
log.err(err)
sh.close()
return setImmediate(() => this.emit('circuit:error', err))
}
setImmediate(() => this.emit('circuit:success'))
})
}
/**
* Connect to STOP
*
* @param {PeerInfo} peer
* @param {StreamHandler} srcSh
* @param {function} callback
* @returns {void}
*/
_connectToStop (peer, srcSh, callback) {
this._dialPeer(peer, (err, dstConn) => {
if (err) {
this.utils.writeResponse(
srcSh,
proto.Status.HOP_CANT_DIAL_DST)
log.err(err)
return callback(err)
}
return this.utils.writeResponse(
srcSh,
proto.Status.SUCCESS,
(err) => {
if (err) {
log.err(err)
return callback(err)
}
return callback(null, dstConn)
})
})
}
/**
* Negotiate STOP
*
* @param {StreamHandler} dstSh
* @param {StreamHandler} srcSh
* @param {CircuitRelay} message
* @param {function} callback
* @returns {void}
*/
_negotiateStop (dstSh, srcSh, message, callback) {
const stopMsg = Object.assign({}, message, {
type: proto.Type.STOP // change the message type
})
dstSh.write(proto.encode(stopMsg),
(err) => {
if (err) {
this.utils.writeResponse(
srcSh,
proto.Status.HOP_CANT_OPEN_DST_STREAM)
log.err(err)
return callback(err)
}
// read response from STOP
dstSh.read((err, msg) => {
if (err) {
log.err(err)
return callback(err)
}
const message = proto.decode(msg)
if (message.code !== proto.Status.SUCCESS) {
return callback(new Error('Unable to create circuit!'))
}
return callback(null, msg)
})
})
}
/**
* Attempt to make a circuit from A <-> R <-> B where R is this relay
*
* @param {StreamHandler} srcSh - the source stream handler
* @param {CircuitRelay} message - the message with the src and dst entries
* @param {Function} callback - callback to signal success or failure
* @returns {void}
* @private
*/
_circuit (srcSh, message, callback) {
let dstSh = null
waterfall([
(cb) => this._connectToStop(message.dstPeer, srcSh, cb),
(_dstConn, cb) => {
dstSh = new StreamHandler(_dstConn)
this._negotiateStop(dstSh, srcSh, message, cb)
}
], (err) => {
if (err) {
// close/end the source stream if there was an error
if (srcSh) {
srcSh.close()
}
if (dstSh) {
dstSh.close()
}
return callback(err)
}
const src = srcSh.rest()
const dst = dstSh.rest()
const srcIdStr = PeerId.createFromBytes(message.srcPeer.id).toB58String()
const dstIdStr = PeerId.createFromBytes(message.dstPeer.id).toB58String()
// circuit the src and dst streams
pull(
src,
dst,
src
)
log('circuit %s <-> %s established', srcIdStr, dstIdStr)
callback()
})
}
/**
* Dial the dest peer and create a circuit
*
* @param {Multiaddr} dstPeer
* @param {Function} callback
* @returns {void}
* @private
*/
_dialPeer (dstPeer, callback) {
const peerInfo = new PeerInfo(PeerId.createFromBytes(dstPeer.id))
dstPeer.addrs.forEach((a) => peerInfo.multiaddrs.add(a))
this.swarm.dial(peerInfo, multicodec.relay, once((err, conn) => {
if (err) {
log.err(err)
return callback(err)
}
callback(null, conn)
}))
}
}
module.exports = Hop

View File

@ -0,0 +1,56 @@
'use strict'
const setImmediate = require('async/setImmediate')
const EE = require('events').EventEmitter
const { Connection } = require('libp2p-interfaces/src/connection')
const utilsFactory = require('./utils')
const PeerInfo = require('peer-info')
const proto = require('../protocol').CircuitRelay
const series = require('async/series')
const debug = require('debug')
const log = debug('libp2p:circuit:stop')
log.err = debug('libp2p:circuit:error:stop')
class Stop extends EE {
constructor (swarm) {
super()
this.swarm = swarm
this.utils = utilsFactory(swarm)
}
/**
* Handle the incoming STOP message
*
* @param {{}} msg - the parsed protobuf message
* @param {StreamHandler} sh - the stream handler wrapped connection
* @param {Function} callback - callback
* @returns {undefined}
*/
handle (msg, sh, callback) {
callback = callback || (() => {})
series([
(cb) => this.utils.validateAddrs(msg, sh, proto.Type.STOP, cb),
(cb) => this.utils.writeResponse(sh, proto.Status.Success, cb)
], (err) => {
if (err) {
// we don't return the error here,
// since multistream select don't expect one
callback()
return log(err)
}
const peerInfo = new PeerInfo(this.utils.peerIdFromId(msg.srcPeer.id))
msg.srcPeer.addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
const newConn = new Connection(sh.rest())
newConn.setPeerInfo(peerInfo)
setImmediate(() => this.emit('connection', newConn))
callback(newConn)
})
}
}
module.exports = Stop

View File

@ -0,0 +1,140 @@
'use strict'
const values = require('pull-stream/sources/values')
const collect = require('pull-stream/sinks/collect')
const empty = require('pull-stream/sources/empty')
const pull = require('pull-stream/pull')
const lp = require('pull-length-prefixed')
const handshake = require('pull-handshake')
const debug = require('debug')
const log = debug('libp2p:circuit:stream-handler')
log.err = debug('libp2p:circuit:error:stream-handler')
class StreamHandler {
/**
* Create a stream handler for connection
*
* @param {Connection} conn - connection to read/write
* @param {Function|undefined} cb - handshake callback called on error
* @param {Number} timeout - handshake timeout
* @param {Number} maxLength - max bytes length of message
*/
constructor (conn, cb, timeout, maxLength) {
this.conn = conn
this.stream = null
this.shake = null
this.timeout = cb || 1000 * 60
this.maxLength = maxLength || 4096
if (typeof cb === 'function') {
this.timeout = timeout || 1000 * 60
}
this.stream = handshake({ timeout: this.timeout }, cb)
this.shake = this.stream.handshake
pull(this.stream, conn, this.stream)
}
isValid () {
return this.conn && this.shake && this.stream
}
/**
* Read and decode message
*
* @param {Function} cb
* @returns {void|Function}
*/
read (cb) {
if (!this.isValid()) {
return cb(new Error('handler is not in a valid state'))
}
lp.decodeFromReader(
this.shake,
{ maxLength: this.maxLength },
(err, msg) => {
if (err) {
log.err(err)
// this.shake.abort(err)
return cb(err)
}
return cb(null, msg)
})
}
/**
* Encode and write array of buffers
*
* @param {Buffer[]} msg
* @param {Function} [cb]
* @returns {Function}
*/
write (msg, cb) {
cb = cb || (() => {})
if (!this.isValid()) {
return cb(new Error('handler is not in a valid state'))
}
pull(
values([msg]),
lp.encode(),
collect((err, encoded) => {
if (err) {
log.err(err)
this.shake.abort(err)
return cb(err)
}
encoded.forEach((e) => this.shake.write(e))
cb()
})
)
}
/**
* Get the raw Connection
*
* @returns {null|Connection|*}
*/
getRawConn () {
return this.conn
}
/**
* Return the handshake rest stream and invalidate handler
*
* @return {*|{source, sink}}
*/
rest () {
const rest = this.shake.rest()
this.conn = null
this.stream = null
this.shake = null
return rest
}
/**
* Close the stream
*
* @returns {undefined}
*/
close () {
if (!this.isValid()) {
return
}
// close stream
pull(
empty(),
this.rest()
)
}
}
module.exports = StreamHandler

View File

@ -0,0 +1,118 @@
'use strict'
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const proto = require('../protocol')
const { getPeerInfo } = require('../../get-peer-info')
module.exports = function (swarm) {
/**
* Get b58 string from multiaddr or peerinfo
*
* @param {Multiaddr|PeerInfo} peer
* @return {*}
*/
function getB58String (peer) {
let b58Id = null
if (multiaddr.isMultiaddr(peer)) {
const relayMa = multiaddr(peer)
b58Id = relayMa.getPeerId()
} else if (PeerInfo.isPeerInfo(peer)) {
b58Id = peer.id.toB58String()
}
return b58Id
}
/**
* Helper to make a peer info from a multiaddrs
*
* @param {Multiaddr|PeerInfo|PeerId} peer
* @return {PeerInfo}
* @private
*/
function peerInfoFromMa (peer) {
return getPeerInfo(peer, swarm._peerBook)
}
/**
* Checks if peer has an existing connection
*
* @param {String} peerId
* @param {Swarm} swarm
* @return {Boolean}
*/
function isPeerConnected (peerId) {
return swarm.muxedConns[peerId] || swarm.conns[peerId]
}
/**
* Write a response
*
* @param {StreamHandler} streamHandler
* @param {CircuitRelay.Status} status
* @param {Function} cb
* @returns {*}
*/
function writeResponse (streamHandler, status, cb) {
cb = cb || (() => {})
streamHandler.write(proto.CircuitRelay.encode({
type: proto.CircuitRelay.Type.STATUS,
code: status
}))
return cb()
}
/**
* Validate incomming HOP/STOP message
*
* @param {CircuitRelay} msg
* @param {StreamHandler} streamHandler
* @param {CircuitRelay.Type} type
* @returns {*}
* @param {Function} cb
*/
function validateAddrs (msg, streamHandler, type, cb) {
try {
msg.dstPeer.addrs.forEach((addr) => {
return multiaddr(addr)
})
} catch (err) {
writeResponse(streamHandler, type === proto.CircuitRelay.Type.HOP
? proto.CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
: proto.CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID)
return cb(err)
}
try {
msg.srcPeer.addrs.forEach((addr) => {
return multiaddr(addr)
})
} catch (err) {
writeResponse(streamHandler, type === proto.CircuitRelay.Type.HOP
? proto.CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
: proto.CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID)
return cb(err)
}
return cb(null)
}
function peerIdFromId (id) {
if (typeof id === 'string') {
return PeerId.createFromB58String(id)
}
return PeerId.createFromBytes(id)
}
return {
getB58String,
peerInfoFromMa,
isPeerConnected,
validateAddrs,
writeResponse,
peerIdFromId
}
}

3
src/circuit/index.js Normal file
View File

@ -0,0 +1,3 @@
'use strict'
module.exports = require('./circuit')

149
src/circuit/listener.js Normal file
View File

@ -0,0 +1,149 @@
'use strict'
const setImmediate = require('async/setImmediate')
const multicodec = require('./multicodec')
const EE = require('events').EventEmitter
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const Stop = require('./circuit/stop')
const Hop = require('./circuit/hop')
const proto = require('./protocol')
const utilsFactory = require('./circuit/utils')
const StreamHandler = require('./circuit/stream-handler')
const debug = require('debug')
const log = debug('libp2p:circuit:listener')
log.err = debug('libp2p:circuit:error:listener')
module.exports = (swarm, options, connHandler) => {
const listener = new EE()
const utils = utilsFactory(swarm)
listener.stopHandler = new Stop(swarm)
listener.stopHandler.on('connection', (conn) => listener.emit('connection', conn))
listener.hopHandler = new Hop(swarm, options.hop)
/**
* Add swarm handler and listen for incoming connections
*
* @param {Multiaddr} ma
* @param {Function} callback
* @return {void}
*/
listener.listen = (ma, callback) => {
callback = callback || (() => {})
swarm.handle(multicodec.relay, (_, conn) => {
const sh = new StreamHandler(conn)
sh.read((err, msg) => {
if (err) {
log.err(err)
return
}
let request = null
try {
request = proto.CircuitRelay.decode(msg)
} catch (err) {
return utils.writeResponse(
sh,
proto.CircuitRelay.Status.MALFORMED_MESSAGE)
}
switch (request.type) {
case proto.CircuitRelay.Type.CAN_HOP:
case proto.CircuitRelay.Type.HOP: {
return listener.hopHandler.handle(request, sh)
}
case proto.CircuitRelay.Type.STOP: {
return listener.stopHandler.handle(request, sh, connHandler)
}
default: {
utils.writeResponse(
sh,
proto.CircuitRelay.Status.INVALID_MSG_TYPE)
return sh.close()
}
}
})
})
setImmediate(() => listener.emit('listen'))
callback()
}
/**
* Remove swarm listener
*
* @param {Function} cb
* @return {void}
*/
listener.close = (cb) => {
swarm.unhandle(multicodec.relay)
setImmediate(() => listener.emit('close'))
cb()
}
/**
* Get fixed up multiaddrs
*
* NOTE: This method will grab the peers multiaddrs and expand them such that:
*
* a) If it's an existing /p2p-circuit address for a specific relay i.e.
* `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
* address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
* `QmPeer` is this peers id
* b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
* addr, such when dialing over a relay with this address, it will create the circuit using
* the encapsulated transport address. This is useful when for example, a peer should only
* be dialed over TCP rather than any other transport
*
* @param {Function} callback
* @return {void}
*/
listener.getAddrs = (callback) => {
let addrs = swarm._peerInfo.multiaddrs.toArray()
// get all the explicit relay addrs excluding self
const p2pAddrs = addrs.filter((addr) => {
return mafmt.Circuit.matches(addr) &&
!addr.toString().includes(swarm._peerInfo.id.toB58String())
})
// use the explicit relays instead of any relay
if (p2pAddrs.length) {
addrs = p2pAddrs
}
const listenAddrs = []
addrs.forEach((addr) => {
const peerMa = `/p2p-circuit/ipfs/${swarm._peerInfo.id.toB58String()}`
if (addr.toString() === peerMa) {
listenAddrs.push(multiaddr(peerMa))
return
}
if (!mafmt.Circuit.matches(addr)) {
if (addr.getPeerId()) {
// by default we're reachable over any relay
listenAddrs.push(multiaddr('/p2p-circuit').encapsulate(addr))
} else {
const ma = `${addr}/ipfs/${swarm._peerInfo.id.toB58String()}`
listenAddrs.push(multiaddr('/p2p-circuit').encapsulate(ma))
}
} else {
listenAddrs.push(addr.encapsulate(`/ipfs/${swarm._peerInfo.id.toB58String()}`))
}
})
callback(null, listenAddrs)
}
return listener
}

View File

@ -0,0 +1,5 @@
'use strict'
module.exports = {
relay: '/libp2p/circuit/relay/0.1.0'
}

View File

@ -0,0 +1,44 @@
'use strict'
const protobuf = require('protons')
module.exports = protobuf(`
message CircuitRelay {
enum Status {
SUCCESS = 100;
HOP_SRC_ADDR_TOO_LONG = 220;
HOP_DST_ADDR_TOO_LONG = 221;
HOP_SRC_MULTIADDR_INVALID = 250;
HOP_DST_MULTIADDR_INVALID = 251;
HOP_NO_CONN_TO_DST = 260;
HOP_CANT_DIAL_DST = 261;
HOP_CANT_OPEN_DST_STREAM = 262;
HOP_CANT_SPEAK_RELAY = 270;
HOP_CANT_RELAY_TO_SELF = 280;
STOP_SRC_ADDR_TOO_LONG = 320;
STOP_DST_ADDR_TOO_LONG = 321;
STOP_SRC_MULTIADDR_INVALID = 350;
STOP_DST_MULTIADDR_INVALID = 351;
STOP_RELAY_REFUSED = 390;
MALFORMED_MESSAGE = 400;
}
enum Type { // RPC identifier, either HOP, STOP or STATUS
HOP = 1;
STOP = 2;
STATUS = 3;
CAN_HOP = 4;
}
message Peer {
required bytes id = 1; // peer id
repeated bytes addrs = 2; // peer's known addresses
}
optional Type type = 1; // Type of the message
optional Peer srcPeer = 2; // srcPeer and dstPeer are used when Type is HOP or STATUS
optional Peer dstPeer = 3;
optional Status code = 4; // Status code, used when Type is STATUS
}
`)

View File

@ -1,56 +1,13 @@
'use strict'
const { struct, superstruct } = require('superstruct')
const { optional, list } = struct
const mergeOptions = require('merge-options')
// Define custom types
const s = superstruct()
const transport = s.union([
s.interface({
createListener: 'function',
dial: 'function'
}),
'function'
])
const modulesSchema = s({
connEncryption: optional(list([s('object|function')])),
// this is hacky to simulate optional because interface doesnt work correctly with it
// change to optional when fixed upstream
connProtector: s.union(['undefined', s.interface({ protect: 'function' })]),
contentRouting: optional(list(['object'])),
dht: optional(s('null|function|object')),
pubsub: optional(s('null|function|object')),
peerDiscovery: optional(list([s('object|function')])),
peerRouting: optional(list(['object'])),
streamMuxer: optional(list([s('object|function')])),
transport: s.intersection([[transport], s.interface({
length (v) {
return v > 0 ? true : 'ERROR_EMPTY'
}
})])
})
const configSchema = s({
peerDiscovery: s('object', {
autoDial: true
}),
relay: s({
enabled: 'boolean',
hop: optional(s({
enabled: 'boolean',
active: 'boolean'
}, {
// HOP defaults
enabled: false,
active: false
}))
}, {
// Relay defaults
enabled: true
}),
// DHT config
dht: s('object?', {
// DHT defaults
const DefaultConfig = {
connectionManager: {
minPeers: 25
},
config: {
dht: {
enabled: false,
kBucketSize: 20,
randomWalk: {
@ -59,42 +16,30 @@ const configSchema = s({
interval: 300e3,
timeout: 10e3
}
}),
// Pubsub config
pubsub: s('object?', {
// Pubsub defaults
enabled: true
})
}, {})
const optionsSchema = s({
switch: 'object?',
connectionManager: s('object', {
minPeers: 25
}),
datastore: 'object?',
peerInfo: 'object',
peerBook: 'object?',
modules: modulesSchema,
config: configSchema
})
},
peerDiscovery: {
autoDial: true
},
pubsub: {
enabled: true,
emitSelf: true,
signMessages: true,
strictSigning: true
},
relay: {
enabled: true,
hop: {
enabled: false,
active: false
}
}
}
}
module.exports.validate = (opts) => {
const [error, options] = optionsSchema.validate(opts)
opts = mergeOptions(DefaultConfig, opts)
// Improve errors throwed, reduce stack by throwing here and add reason to the message
if (error) {
throw new Error(`${error.message}${error.reason ? ' - ' + error.reason : ''}`)
} else {
// Throw when dht is enabled but no dht module provided
if (options.config.dht.enabled) {
s('function|object')(options.modules.dht)
}
}
if (opts.modules.transport.length < 1) throw new Error("'options.modules.transport' must contain at least 1 transport")
if (options.config.peerDiscovery.autoDial === undefined) {
options.config.peerDiscovery.autoDial = true
}
return options
return opts
}

View File

@ -0,0 +1,99 @@
# libp2p-connection-manager
> JavaScript connection manager for libp2p
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-connection-manager.
## Table of Contents
- [Install](#install)
- [npm](#npm)
- [Use in Node.js, a browser with browserify, webpack or any other bundler](##use-in-nodejs-or-in-the-browser-with-browserify-webpack-or-any-other-bundler)
- [Usage](#usage)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)
## API
A connection manager manages the peers you're connected to. The application provides one or more limits that will trigger the disconnection of peers. These limits can be any of the following:
* number of connected peers
* maximum bandwidth (sent / received or both)
* maximum event loop delay
The connection manager will disconnect peers (starting from the less important peers) until all the measures are withing the stated limits.
A connection manager first disconnects the peers with the least value. By default all peers have the same importance (1), but the application can define otherwise. Once a peer disconnects the connection manager discards the peer importance. (If necessary, the application should redefine the peer state if the peer is again connected).
### Create a ConnectionManager
```js
const libp2p = // …
const options = {}
const connManager = new ConnManager(libp2p, options)
```
Options is an optional object with the following key-value pairs:
* **`maxPeers`**: number identifying the maximum number of peers the current peer is willing to be connected to before is starts disconnecting. Defaults to `Infinity`
* **`maxPeersPerProtocol`**: Object with key-value pairs, where a key is the protocol tag (case-insensitive) and the value is a number, representing the maximum number of peers allowing to connect for each protocol. Defaults to `{}`.
* **`minPeers`**: number identifying the number of peers below which this node will not activate preemptive disconnections. Defaults to `0`.
* **`maxData`**: sets the maximum data — in bytes per second - (sent and received) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`maxSentData`**: sets the maximum sent data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`maxReceivedData`**: sets the maximum received data — in bytes per second - this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`maxEventLoopDelay`**: sets the maximum event loop delay (measured in miliseconds) this node is willing to endure before it starts disconnecting peers. Defaults to `Infinity`.
* **`pollInterval`**: sets the poll interval (in miliseconds) for assessing the current state and determining if this peer needs to force a disconnect. Defaults to `2000` (2 seconds).
* **`movingAverageInterval`**: the interval used to calculate moving averages (in miliseconds). Defaults to `60000` (1 minute).
* **`defaultPeerValue`**: number between 0 and 1. Defaults to 1.
### `connManager.start()`
Starts the connection manager.
### `connManager.stop()`
Stops the connection manager.
### `connManager.setPeerValue(peerId, value)`
Sets the peer value for a given peer id. This is used to sort peers (in reverse order of value) to determine which to disconnect from first.
Arguments:
* peerId: B58-encoded string or [`peer-id`](https://github.com/libp2p/js-peer-id) instance.
* value: a number between 0 and 1, which represents a scale of how valuable this given peer id is to the application.
### `connManager.peers()`
Returns the peers this connection manager is connected to.
Returns an array of [PeerInfo](https://github.com/libp2p/js-peer-info).
### `connManager.emit('limit:exceeded', limitName, measured)`
Emitted when a limit is exceeded. Limit names can be:
* `maxPeers`
* `minPeers`
* `maxData`
* `maxSentData`
* `maxReceivedData`
* `maxEventLoopDelay`
* a protocol tag string (lower-cased)
### `connManager.emit('disconnect:preemptive', peerId)`
Emitted when a peer is about to be preemptively disconnected.
### `connManager.emit('disconnected', peerId)`
Emitted when a peer is disconnected (preemptively or note). If this peer reconnects, you will need to reset it's value, since the connection manager does not remember it.
### `connManager.emit('connected', peerId: String)`
Emitted when a peer connects. This is a good event to set the peer value, so you can get some control over who gets banned once a maximum number of peers is reached.

View File

@ -0,0 +1,218 @@
'use strict'
const EventEmitter = require('events')
const LatencyMonitor = require('latency-monitor').default
const debug = require('debug')('libp2p:connection-manager')
const defaultOptions = {
maxPeers: Infinity,
minPeers: 0,
maxData: Infinity,
maxSentData: Infinity,
maxReceivedData: Infinity,
maxEventLoopDelay: Infinity,
pollInterval: 2000,
movingAverageInterval: 60000,
defaultPeerValue: 1
}
class ConnectionManager extends EventEmitter {
constructor (libp2p, options) {
super()
this._libp2p = libp2p
this._options = Object.assign({}, defaultOptions, options)
this._options.maxPeersPerProtocol = fixMaxPeersPerProtocol(this._options.maxPeersPerProtocol)
debug('options: %j', this._options)
this._stats = libp2p.stats
if (options && !this._stats) {
throw new Error('No libp2p.stats')
}
this._peerValues = new Map()
this._peers = new Map()
this._peerProtocols = new Map()
this._peerCountPerProtocol = new Map()
this._onStatsUpdate = this._onStatsUpdate.bind(this)
this._onPeerConnect = this._onPeerConnect.bind(this)
this._onPeerDisconnect = this._onPeerDisconnect.bind(this)
if (this._libp2p.isStarted()) {
this._onceStarted()
} else {
this._libp2p.once('start', this._onceStarted.bind(this))
}
}
start () {
this._stats.on('update', this._onStatsUpdate)
this._libp2p.on('connection:start', this._onPeerConnect)
this._libp2p.on('connection:end', this._onPeerDisconnect)
// latency monitor
this._latencyMonitor = new LatencyMonitor({
dataEmitIntervalMs: this._options.pollInterval
})
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
this._latencyMonitor.on('data', this._onLatencyMeasure)
}
stop () {
this._stats.removeListener('update', this._onStatsUpdate)
this._libp2p.removeListener('connection:start', this._onPeerConnect)
this._libp2p.removeListener('connection:end', this._onPeerDisconnect)
this._latencyMonitor.removeListener('data', this._onLatencyMeasure)
}
setPeerValue (peerId, value) {
if (value < 0 || value > 1) {
throw new Error('value should be a number between 0 and 1')
}
if (peerId.toB58String) {
peerId = peerId.toB58String()
}
this._peerValues.set(peerId, value)
}
_onceStarted () {
this._peerId = this._libp2p.peerInfo.id.toB58String()
}
_onStatsUpdate () {
const movingAvgs = this._stats.global.movingAverages
const received = movingAvgs.dataReceived[this._options.movingAverageInterval].movingAverage()
this._checkLimit('maxReceivedData', received)
const sent = movingAvgs.dataSent[this._options.movingAverageInterval].movingAverage()
this._checkLimit('maxSentData', sent)
const total = received + sent
this._checkLimit('maxData', total)
debug('stats update', total)
}
_onPeerConnect (peerInfo) {
const peerId = peerInfo.id.toB58String()
debug('%s: connected to %s', this._peerId, peerId)
this._peerValues.set(peerId, this._options.defaultPeerValue)
this._peers.set(peerId, peerInfo)
this.emit('connected', peerId)
this._checkLimit('maxPeers', this._peers.size)
protocolsFromPeerInfo(peerInfo).forEach((protocolTag) => {
const protocol = this._peerCountPerProtocol[protocolTag]
if (!protocol) {
this._peerCountPerProtocol[protocolTag] = 0
}
this._peerCountPerProtocol[protocolTag]++
let peerProtocols = this._peerProtocols[peerId]
if (!peerProtocols) {
peerProtocols = this._peerProtocols[peerId] = new Set()
}
peerProtocols.add(protocolTag)
this._checkProtocolMaxPeersLimit(protocolTag, this._peerCountPerProtocol[protocolTag])
})
}
_onPeerDisconnect (peerInfo) {
const peerId = peerInfo.id.toB58String()
debug('%s: disconnected from %s', this._peerId, peerId)
this._peerValues.delete(peerId)
this._peers.delete(peerId)
const peerProtocols = this._peerProtocols[peerId]
if (peerProtocols) {
Array.from(peerProtocols).forEach((protocolTag) => {
const peerCountForProtocol = this._peerCountPerProtocol[protocolTag]
if (peerCountForProtocol) {
this._peerCountPerProtocol[protocolTag]--
}
})
}
this.emit('disconnected', peerId)
}
_onLatencyMeasure (summary) {
this._checkLimit('maxEventLoopDelay', summary.avgMs)
}
_checkLimit (name, value) {
const limit = this._options[name]
debug('checking limit of %s. current value: %d of %d', name, value, limit)
if (value > limit) {
debug('%s: limit exceeded: %s, %d', this._peerId, name, value)
this.emit('limit:exceeded', name, value)
this._maybeDisconnectOne()
}
}
_checkProtocolMaxPeersLimit (protocolTag, value) {
debug('checking protocol limit. current value of %s is %d', protocolTag, value)
const limit = this._options.maxPeersPerProtocol[protocolTag]
if (value > limit) {
debug('%s: protocol max peers limit exceeded: %s, %d', this._peerId, protocolTag, value)
this.emit('limit:exceeded', protocolTag, value)
this._maybeDisconnectOne()
}
}
_maybeDisconnectOne () {
if (this._options.minPeers < this._peerValues.size) {
const peerValues = Array.from(this._peerValues).sort(byPeerValue)
debug('%s: sorted peer values: %j', this._peerId, peerValues)
const disconnectPeer = peerValues[0]
if (disconnectPeer) {
const peerId = disconnectPeer[0]
debug('%s: lowest value peer is %s', this._peerId, peerId)
debug('%s: forcing disconnection from %j', this._peerId, peerId)
this._disconnectPeer(peerId)
}
}
}
_disconnectPeer (peerId) {
debug('preemptively disconnecting peer', peerId)
this.emit('%s: disconnect:preemptive', this._peerId, peerId)
const peer = this._peers.get(peerId)
this._libp2p.hangUp(peer, (err) => {
if (err) {
this.emit('error', err)
}
})
}
}
module.exports = ConnectionManager
function byPeerValue (peerValueEntryA, peerValueEntryB) {
return peerValueEntryA[1] - peerValueEntryB[1]
}
function fixMaxPeersPerProtocol (maxPeersPerProtocol) {
if (!maxPeersPerProtocol) {
maxPeersPerProtocol = {}
}
Object.keys(maxPeersPerProtocol).forEach((transportTag) => {
const max = maxPeersPerProtocol[transportTag]
delete maxPeersPerProtocol[transportTag]
maxPeersPerProtocol[transportTag.toLowerCase()] = max
})
return maxPeersPerProtocol
}
function protocolsFromPeerInfo (peerInfo) {
const protocolTags = new Set()
peerInfo.multiaddrs.forEach((multiaddr) => {
multiaddr.protos().map(protocolToProtocolTag).forEach((protocolTag) => {
protocolTags.add(protocolTag)
})
})
return Array.from(protocolTags)
}
function protocolToProtocolTag (protocol) {
return protocol.name.toLowerCase()
}

12
src/constants.js Normal file
View File

@ -0,0 +1,12 @@
'use strict'
module.exports = {
DENY_TTL: 5 * 60 * 1e3, // How long before an errored peer can be dialed again
DENY_ATTEMPTS: 5, // Num of unsuccessful dials before a peer is permanently denied
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
MAX_COLD_CALLS: 50, // How many dials w/o protocols that can be queued
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials
QUARTER_HOUR: 15 * 60e3,
PRIORITY_HIGH: 10,
PRIORITY_LOW: 20
}

127
src/dialer.js Normal file
View File

@ -0,0 +1,127 @@
'use strict'
const nextTick = require('async/nextTick')
const multiaddr = require('multiaddr')
const errCode = require('err-code')
const { default: PQueue } = require('p-queue')
const AbortController = require('abort-controller')
const debug = require('debug')
const log = debug('libp2p:dialer')
log.error = debug('libp2p:dialer:error')
const { codes } = require('./errors')
const {
MAX_PARALLEL_DIALS,
DIAL_TIMEOUT
} = require('./constants')
class Dialer {
/**
* @constructor
* @param {object} options
* @param {TransportManager} options.transportManager
* @param {number} options.concurrency Number of max concurrent dials. Defaults to `MAX_PARALLEL_DIALS`
* @param {number} options.timeout How long a dial attempt is allowed to take. Defaults to `DIAL_TIMEOUT`
*/
constructor ({
transportManager,
concurrency = MAX_PARALLEL_DIALS,
timeout = DIAL_TIMEOUT
}) {
this.transportManager = transportManager
this.concurrency = concurrency
this.timeout = timeout
this.queue = new PQueue({ concurrency, timeout, throwOnTimeout: true })
/**
* @property {IdentifyService}
*/
this._identifyService = null
}
set identifyService (service) {
this._identifyService = service
}
/**
* @type {IdentifyService}
*/
get identifyService () {
return this._identifyService
}
/**
* Connects to a given `Multiaddr`. `addr` should include the id of the peer being
* dialed, it will be used for encryption verification.
*
* @async
* @param {Multiaddr} addr The address to dial
* @param {object} [options]
* @param {AbortSignal} [options.signal] An AbortController signal
* @returns {Promise<Connection>}
*/
async connectToMultiaddr (addr, options = {}) {
addr = multiaddr(addr)
let conn
let controller
if (!options.signal) {
controller = new AbortController()
options.signal = controller.signal
}
try {
conn = await this.queue.add(() => this.transportManager.dial(addr, options))
} catch (err) {
if (err.name === 'TimeoutError') {
controller.abort()
err.code = codes.ERR_TIMEOUT
}
log.error('Error dialing address %s,', addr, err)
throw err
}
// Perform a delayed Identify handshake
if (this.identifyService) {
nextTick(async () => {
try {
await this.identifyService.identify(conn, conn.remotePeer)
// TODO: Update the PeerStore with the information from identify
} catch (err) {
log.error(err)
}
})
}
return conn
}
/**
* Connects to a given `PeerInfo` by dialing all of its known addresses.
* The dial to the first address that is successfully able to upgrade a connection
* will be used.
*
* @async
* @param {PeerInfo} peerInfo The remote peer to dial
* @param {object} [options]
* @param {AbortSignal} [options.signal] An AbortController signal
* @returns {Promise<Connection>}
*/
async connectToPeer (peerInfo, options = {}) {
const addrs = peerInfo.multiaddrs.toArray()
for (const addr of addrs) {
try {
return await this.connectToMultiaddr(addr, options)
} catch (_) {
// The error is already logged, just move to the next addr
continue
}
}
const err = errCode(new Error('Could not dial peer, all addresses failed'), codes.ERR_CONNECTION_FAILED)
log.error(err)
throw err
}
}
module.exports = Dialer

View File

@ -8,6 +8,19 @@ exports.messages = {
exports.codes = {
DHT_DISABLED: 'ERR_DHT_DISABLED',
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF'
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
ERR_INVALID_PEER: 'ERR_INVALID_PEER',
ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE',
ERR_TIMEOUT: 'ERR_TIMEOUT',
ERR_TRANSPORT_UNAVAILABLE: 'ERR_TRANSPORT_UNAVAILABLE',
ERR_TRANSPORT_DIAL_FAILED: 'ERR_TRANSPORT_DIAL_FAILED',
ERR_UNSUPPORTED_PROTOCOL: 'ERR_UNSUPPORTED_PROTOCOL'
}

View File

@ -4,64 +4,73 @@ const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const errCode = require('err-code')
const promisify = require('promisify-es6')
module.exports = (node) => {
/*
* Helper method to check the data type of peer and convert it to PeerInfo
/**
* Converts the given `peer` to a `PeerInfo` instance.
* The `PeerStore` will be checked for the resulting peer, and
* the peer will be updated in the `PeerStore`.
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer
* @param {PeerStore} peerStore
* @returns {PeerInfo}
*/
return promisify(function (peer, callback) {
let p
// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
p = peer
// Multiaddr instance or Multiaddr String
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
function getPeerInfo (peer, peerStore) {
if (typeof peer === 'string') {
try {
peer = multiaddr(peer)
}
let addr
if (multiaddr.isMultiaddr(peer)) {
addr = peer
try {
peer = PeerId.createFromB58String(peer.getPeerId())
} catch (err) {
return callback(
errCode(err, 'ERR_INVALID_MULTIADDR')
)
}
}
const peerIdB58Str = peer.getPeerId()
if (!peerIdB58Str) {
return callback(
errCode(
new Error('peer multiaddr instance or string must include peerId'),
throw errCode(
new Error(`${peer} is not a valid peer type`),
'ERR_INVALID_MULTIADDR'
)
)
}
}
try {
p = node.peerBook.get(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
if (PeerId.isPeerId(peer)) {
peer = new PeerInfo(peer)
}
p.multiaddrs.add(peer)
// PeerId
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
p = node.peerBook.get(peerIdB58Str)
} catch (err) {
return node.peerRouting.findPeer(peer, callback)
addr && peer.multiaddrs.add(addr)
return peerStore ? peerStore.put(peer) : peer
}
} else {
return callback(
errCode(
new Error(`${p} is not a valid peer type`),
/**
* If `getPeerInfo` does not return a peer with multiaddrs,
* the `libp2p` PeerRouter will be used to attempt to find the peer.
*
* @async
* @param {PeerInfo|PeerId|Multiaddr|string} peer
* @param {Libp2p} libp2p
* @returns {Promise<PeerInfo>}
*/
function getPeerInfoRemote (peer, libp2p) {
let peerInfo
try {
peerInfo = getPeerInfo(peer, libp2p.peerStore)
} catch (err) {
return Promise.reject(errCode(
new Error(`${peer} is not a valid peer type`),
'ERR_INVALID_PEER_TYPE'
)
)
))
}
callback(null, p)
})
// If we don't have an address for the peer, attempt to find it
if (peerInfo.multiaddrs.size < 1) {
return libp2p.peerRouting.findPeer(peerInfo.id)
}
return Promise.resolve(peerInfo)
}
module.exports = {
getPeerInfoRemote,
getPeerInfo
}

13
src/identify/README.md Normal file
View File

@ -0,0 +1,13 @@
# js-libp2p-identify
> libp2p Identify Protocol
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-identify.
## Description
Identify is a STUN protocol, used by libp2p in order to broadcast and learn about the `ip:port` pairs a specific peer is available through and to know when a new stream muxer is established, so a conn can be reused.
## How does it work
The spec for Identify and Identify Push is at [libp2p/specs](https://github.com/libp2p/specs/tree/master/identify).

6
src/identify/consts.js Normal file
View File

@ -0,0 +1,6 @@
'use strict'
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
module.exports.AGENT_VERSION = 'js-libp2p/0.1.0'
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0'
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0'

299
src/identify/index.js Normal file
View File

@ -0,0 +1,299 @@
'use strict'
const debug = require('debug')
const pb = require('it-protocol-buffers')
const lp = require('it-length-prefixed')
const pipe = require('it-pipe')
const { collect, take } = require('streaming-iterables')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const { toBuffer } = require('../util')
const Message = require('./message')
const log = debug('libp2p:identify')
log.error = debug('libp2p:identify:error')
const {
MULTICODEC_IDENTIFY,
MULTICODEC_IDENTIFY_PUSH,
AGENT_VERSION,
PROTOCOL_VERSION
} = require('./consts')
const errCode = require('err-code')
const { codes } = require('../errors')
class IdentifyService {
/**
* Replaces the multiaddrs on the given `peerInfo`,
* with the provided `multiaddrs`
* @param {PeerInfo} peerInfo
* @param {Array<Multiaddr>|Array<Buffer>} multiaddrs
*/
static updatePeerAddresses (peerInfo, multiaddrs) {
if (multiaddrs && multiaddrs.length > 0) {
peerInfo.multiaddrs.clear()
multiaddrs.forEach(ma => {
try {
peerInfo.multiaddrs.add(ma)
} catch (err) {
log.error('could not add multiaddr', err)
}
})
}
}
/**
* Replaces the protocols on the given `peerInfo`,
* with the provided `protocols`
* @static
* @param {PeerInfo} peerInfo
* @param {Array<string>} protocols
*/
static updatePeerProtocols (peerInfo, protocols) {
if (protocols && protocols.length > 0) {
peerInfo.protocols.clear()
protocols.forEach(proto => peerInfo.protocols.add(proto))
}
}
/**
* Takes the `addr` and converts it to a Multiaddr if possible
* @param {Buffer|String} addr
* @returns {Multiaddr|null}
*/
static getCleanMultiaddr (addr) {
if (addr && addr.length > 0) {
try {
return multiaddr(addr)
} catch (_) {
return null
}
}
return null
}
/**
* @constructor
* @param {object} options
* @param {Registrar} options.registrar
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
* @param {PeerInfo} options.peerInfo The peer running the identify service
*/
constructor (options) {
/**
* @property {Registrar}
*/
this.registrar = options.registrar
/**
* @property {PeerInfo}
*/
this.peerInfo = options.peerInfo
this._protocols = options.protocols
this.handleMessage = this.handleMessage.bind(this)
}
/**
* Send an Identify Push update to the list of connections
* @param {Array<Connection>} connections
* @returns {Promise<void>}
*/
push (connections) {
const pushes = connections.map(async connection => {
try {
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)
await pipe(
[{
listenAddrs: this.peerInfo.multiaddrs.toArray().map((ma) => ma.buffer),
protocols: Array.from(this._protocols.keys())
}],
pb.encode(Message),
stream
)
} catch (err) {
// Just log errors
log.error('could not push identify update to peer', err)
}
})
return Promise.all(pushes)
}
/**
* Calls `push` for all peers in the `peerStore` that are connected
* @param {PeerStore} peerStore
*/
pushToPeerStore (peerStore) {
const connections = []
let connection
for (const peer of peerStore.peers.values()) {
if (peer.protocols.has(MULTICODEC_IDENTIFY_PUSH) && (connection = this.registrar.getConnection(peer))) {
connections.push(connection)
}
}
this.push(connections)
}
/**
* Requests the `Identify` message from peer associated with the given `connection`.
* If the identified peer does not match the `PeerId` associated with the connection,
* an error will be thrown.
*
* @async
* @param {Connection} connection
* @param {PeerID} expectedPeer The PeerId the identify response should match
* @returns {Promise<void>}
*/
async identify (connection, expectedPeer) {
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY)
const [data] = await pipe(
stream,
lp.decode(),
take(1),
toBuffer,
collect
)
if (!data) {
throw errCode(new Error('No data could be retrieved'), codes.ERR_CONNECTION_ENDED)
}
let message
try {
message = Message.decode(data)
} catch (err) {
throw errCode(err, codes.ERR_INVALID_MESSAGE)
}
let {
publicKey,
listenAddrs,
protocols,
observedAddr
} = message
const id = await PeerId.createFromPubKey(publicKey)
const peerInfo = new PeerInfo(id)
if (expectedPeer && expectedPeer.toB58String() !== id.toB58String()) {
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
}
// Get the observedAddr if there is one
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
// Copy the listenAddrs and protocols
IdentifyService.updatePeerAddresses(peerInfo, listenAddrs)
IdentifyService.updatePeerProtocols(peerInfo, protocols)
this.registrar.peerStore.update(peerInfo)
// TODO: Track our observed address so that we can score it
log('received observed address of %s', observedAddr)
}
/**
* A handler to register with Libp2p to process identify messages.
*
* @param {object} options
* @param {String} options.protocol
* @param {*} options.stream
* @param {Connection} options.connection
* @returns {Promise<void>}
*/
handleMessage ({ connection, stream, protocol }) {
switch (protocol) {
case MULTICODEC_IDENTIFY:
return this._handleIdentify({ connection, stream })
case MULTICODEC_IDENTIFY_PUSH:
return this._handlePush({ connection, stream })
default:
log.error('cannot handle unknown protocol %s', protocol)
}
}
/**
* Sends the `Identify` response to the requesting peer over the
* given `connection`
* @private
* @param {object} options
* @param {*} options.stream
* @param {Connection} options.connection
*/
_handleIdentify ({ connection, stream }) {
let publicKey = Buffer.alloc(0)
if (this.peerInfo.id.pubKey) {
publicKey = this.peerInfo.id.pubKey.bytes
}
const message = Message.encode({
protocolVersion: PROTOCOL_VERSION,
agentVersion: AGENT_VERSION,
publicKey,
listenAddrs: this.peerInfo.multiaddrs.toArray().map((ma) => ma.buffer),
observedAddr: connection.remoteAddr.buffer,
protocols: Array.from(this._protocols.keys())
})
pipe(
[message],
lp.encode(),
stream
)
}
/**
* Reads the Identify Push message from the given `connection`
* @private
* @param {object} options
* @param {*} options.stream
* @param {Connection} options.connection
*/
async _handlePush ({ connection, stream }) {
const [data] = await pipe(
stream,
lp.decode(),
take(1),
toBuffer,
collect
)
let message
try {
message = Message.decode(data)
} catch (err) {
return log.error('received invalid message', err)
}
// Update the listen addresses
const peerInfo = new PeerInfo(connection.remotePeer)
try {
IdentifyService.updatePeerAddresses(peerInfo, message.listenAddrs)
} catch (err) {
return log.error('received invalid listen addrs', err)
}
// Update the protocols
IdentifyService.updatePeerProtocols(peerInfo, message.protocols)
// Update the peer in the PeerStore
this.registrar.peerStore.update(peerInfo)
}
}
module.exports.IdentifyService = IdentifyService
/**
* The protocols the IdentifyService supports
* @property multicodecs
*/
module.exports.multicodecs = {
IDENTIFY: MULTICODEC_IDENTIFY,
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
}
module.exports.Message = Message

30
src/identify/message.js Normal file
View File

@ -0,0 +1,30 @@
'use strict'
const protons = require('protons')
const schema = `
message Identify {
// protocolVersion determines compatibility between peers
optional string protocolVersion = 5; // e.g. ipfs/1.0.0
// agentVersion is like a UserAgent string in browsers, or client version in bittorrent
// includes the client name and client.
optional string agentVersion = 6; // e.g. go-ipfs/0.1.0
// publicKey is this node's public key (which also gives its node.ID)
// - may not need to be sent, as secure channel implies it has been sent.
// - then again, if we change / disable secure channel, may still want it.
optional bytes publicKey = 1;
// listenAddrs are the multiaddrs the sender node listens for open connections on
repeated bytes listenAddrs = 2;
// oservedAddr is the multiaddr of the remote endpoint that the sender node perceives
// this is useful information to convey to the other side, as it helps the remote endpoint
// determine whether its connection to the local peer goes through NAT.
optional bytes observedAddr = 4;
repeated string protocols = 3;
}
`
module.exports = protons(schema).Identify

View File

@ -1,7 +1,7 @@
'use strict'
const FSM = require('fsm-event')
const EventEmitter = require('events').EventEmitter
const { EventEmitter } = require('events')
const debug = require('debug')
const log = debug('libp2p')
log.error = debug('libp2p:error')
@ -9,26 +9,31 @@ const errCode = require('err-code')
const promisify = require('promisify-es6')
const each = require('async/each')
const series = require('async/series')
const parallel = require('async/parallel')
const nextTick = require('async/nextTick')
const PeerBook = require('peer-book')
const PeerInfo = require('peer-info')
const Switch = require('libp2p-switch')
const Ping = require('libp2p-ping')
const WebSockets = require('libp2p-websockets')
const ConnectionManager = require('libp2p-connection-manager')
const multiaddr = require('multiaddr')
const Switch = require('./switch')
const Ping = require('./ping')
const { emitFirst } = require('./util')
const peerRouting = require('./peer-routing')
const contentRouting = require('./content-routing')
const dht = require('./dht')
const pubsub = require('./pubsub')
const getPeerInfo = require('./get-peer-info')
const validateConfig = require('./config').validate
const { getPeerInfo, getPeerInfoRemote } = require('./get-peer-info')
const { validate: validateConfig } = require('./config')
const { codes } = require('./errors')
const Dialer = require('./dialer')
const TransportManager = require('./transport-manager')
const Upgrader = require('./upgrader')
const PeerStore = require('./peer-store')
const Registrar = require('./registrar')
const {
IdentifyService,
multicodecs: IDENTIFY_PROTOCOLS
} = require('./identify')
const notStarted = (action, state) => {
return errCode(
new Error(`libp2p cannot ${action} when not started; state is ${state}`),
@ -53,61 +58,81 @@ class Libp2p extends EventEmitter {
this.datastore = this._options.datastore
this.peerInfo = this._options.peerInfo
this.peerBook = this._options.peerBook || new PeerBook()
this.peerStore = new PeerStore()
this._modules = this._options.modules
this._config = this._options.config
this._transport = [] // Transport instances/references
this._discovery = [] // Discovery service instances/references
this.peerStore = new PeerStore()
// create the switch, and listen for errors
this._switch = new Switch(this.peerInfo, this.peerBook, this._options.switch)
this._switch.on('error', (...args) => this.emit('error', ...args))
this._switch = new Switch(this.peerInfo, this.peerStore, this._options.switch)
this.stats = this._switch.stats
this.connectionManager = new ConnectionManager(this, this._options.connectionManager)
// Setup the Upgrader
this.upgrader = new Upgrader({
localPeer: this.peerInfo.id,
onConnection: (connection) => {
const peerInfo = getPeerInfo(connection.remotePeer)
// Attach stream multiplexers
if (this._modules.streamMuxer) {
const muxers = this._modules.streamMuxer
muxers.forEach((muxer) => this._switch.connection.addStreamMuxer(muxer))
// If muxer exists
// we can use Identify
this._switch.connection.reuse()
// we can use Relay for listening/dialing
this._switch.connection.enableCircuitRelay(this._config.relay)
// Received incomming dial and muxer upgrade happened,
// reuse this muxed connection
this._switch.on('peer-mux-established', (peerInfo) => {
this.peerStore.put(peerInfo)
this.registrar.onConnect(peerInfo, connection)
this.emit('peer:connect', peerInfo)
})
},
onConnectionEnd: (connection) => {
const peerInfo = getPeerInfo(connection.remotePeer)
this._switch.on('peer-mux-closed', (peerInfo) => {
this.registrar.onDisconnect(peerInfo, connection)
this.emit('peer:disconnect', peerInfo)
})
}
// Events for anytime connections are created/removed
this._switch.on('connection:start', (peerInfo) => {
this.emit('connection:start', peerInfo)
})
this._switch.on('connection:end', (peerInfo) => {
this.emit('connection:end', peerInfo)
// Create the Registrar
this.registrar = new Registrar({ peerStore: this.peerStore })
this.handle = this.handle.bind(this)
this.registrar.handle = this.handle
// Setup the transport manager
this.transportManager = new TransportManager({
libp2p: this,
upgrader: this.upgrader
})
this._modules.transport.forEach((Transport) => {
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
})
// Attach crypto channels
if (this._modules.connEncryption) {
const cryptos = this._modules.connEncryption
cryptos.forEach((crypto) => {
this._switch.connection.crypto(crypto.tag, crypto.encrypt)
this.upgrader.cryptos.set(crypto.protocol, crypto)
})
}
this.dialer = new Dialer({
transportManager: this.transportManager
})
// Attach stream multiplexers
if (this._modules.streamMuxer) {
const muxers = this._modules.streamMuxer
muxers.forEach((muxer) => {
this.upgrader.muxers.set(muxer.multicodec, muxer)
})
// Add the identify service since we can multiplex
this.dialer.identifyService = new IdentifyService({
registrar: this.registrar,
peerInfo: this.peerInfo,
protocols: this.upgrader.protocols
})
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.dialer.identifyService.handleMessage)
}
// Attach private network protector
if (this._modules.connProtector) {
this._switch.protector = this._modules.connProtector
this.upgrader.protector = this._modules.connProtector
} else if (process.env.LIBP2P_FORCE_PNET) {
throw new Error('Private network is enforced, but no protector was provided')
}
@ -123,8 +148,8 @@ class Libp2p extends EventEmitter {
}
// start pubsub
if (this._modules.pubsub && this._config.pubsub.enabled !== false) {
this.pubsub = pubsub(this, this._modules.pubsub)
if (this._modules.pubsub) {
this.pubsub = pubsub(this, this._modules.pubsub, this._config.pubsub)
}
// Attach remaining APIs
@ -133,15 +158,14 @@ class Libp2p extends EventEmitter {
this.contentRouting = contentRouting(this)
this.dht = dht(this)
this._getPeerInfo = getPeerInfo(this)
// Mount default protocols
Ping.mount(this._switch)
this.state = new FSM('STOPPED', {
STOPPED: {
start: 'STARTING',
stop: 'STOPPED'
stop: 'STOPPED',
done: 'STOPPED'
},
STARTING: {
done: 'STARTED',
@ -163,7 +187,6 @@ class Libp2p extends EventEmitter {
})
this.state.on('STOPPING', () => {
log('libp2p is stopping')
this._onStopping()
})
this.state.on('STARTED', () => {
log('libp2p has started')
@ -180,16 +203,16 @@ class Libp2p extends EventEmitter {
// Once we start, emit and dial any peers we may have already discovered
this.state.on('STARTED', () => {
this.peerBook.getAllArray().forEach((peerInfo) => {
for (const peerInfo of this.peerStore.peers) {
this.emit('peer:discovery', peerInfo)
this._maybeConnect(peerInfo)
})
}
})
this._peerDiscovered = this._peerDiscovered.bind(this)
// promisify all instance methods
;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
;['start', 'hangUp', 'ping'].forEach(method => {
this[method] = promisify(this[method], { context: this })
})
}
@ -222,13 +245,23 @@ class Libp2p extends EventEmitter {
/**
* Stop the libp2p node by closing its listeners and open connections
*
* @param {function(Error)} callback
* @async
* @returns {void}
*/
stop (callback = () => {}) {
emitFirst(this, ['error', 'stop'], callback)
async stop () {
this.state('stop')
try {
this.pubsub && await this.pubsub.stop()
await this.transportManager.close()
await this._switch.stop()
} catch (err) {
if (err) {
log.error(err)
this.emit('error', err)
}
}
this.state('done')
}
isStarted () {
@ -237,67 +270,52 @@ class Libp2p extends EventEmitter {
/**
* Dials to the provided peer. If successful, the `PeerInfo` of the
* peer will be added to the nodes `PeerBook`
* peer will be added to the nodes `peerStore`
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
* @param {function(Error)} callback
* @returns {void}
* @param {object} options
* @param {AbortSignal} [options.signal]
* @returns {Promise<Connection>}
*/
dial (peer, callback) {
this.dialProtocol(peer, null, callback)
dial (peer, options) {
return this.dialProtocol(peer, null, options)
}
/**
* Dials to the provided peer and handshakes with the given protocol.
* If successful, the `PeerInfo` of the peer will be added to the nodes `PeerBook`,
* If successful, the `PeerInfo` of the peer will be added to the nodes `peerStore`,
* and the `Connection` will be sent in the callback
*
* @async
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
* @param {string} protocol
* @param {function(Error, Connection)} callback
* @returns {void}
* @param {string[]|string} protocols
* @param {object} options
* @param {AbortSignal} [options.signal]
* @returns {Promise<Connection|*>}
*/
dialProtocol (peer, protocol, callback) {
if (!this.isStarted()) {
return callback(notStarted('dial', this.state._state))
async dialProtocol (peer, protocols, options) {
let connection
if (multiaddr.isMultiaddr(peer)) {
connection = await this.dialer.connectToMultiaddr(peer, options)
} else {
peer = await getPeerInfoRemote(peer, this)
connection = await this.dialer.connectToPeer(peer, options)
}
if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
const peerInfo = getPeerInfo(connection.remotePeer)
// If a protocol was provided, create a new stream
if (protocols) {
const stream = await connection.newStream(protocols)
peerInfo.protocols.add(stream.protocol)
this.peerStore.put(peerInfo)
return stream
}
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this._switch.dial(peerInfo, protocol, callback)
})
}
/**
* Similar to `dial` and `dialProtocol`, but the callback will contain a
* Connection State Machine.
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
* @param {string} protocol
* @param {function(Error, ConnectionFSM)} callback
* @returns {void}
*/
dialFSM (peer, protocol, callback) {
if (!this.isStarted()) {
return callback(notStarted('dial', this.state._state))
}
if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this._switch.dialFSM(peerInfo, protocol, callback)
})
this.peerStore.put(peerInfo)
return connection
}
/**
@ -308,11 +326,10 @@ class Libp2p extends EventEmitter {
* @returns {void}
*/
hangUp (peer, callback) {
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
getPeerInfoRemote(peer, this)
.then(peerInfo => {
this._switch.hangUp(peerInfo, callback)
})
}, callback)
}
/**
@ -327,161 +344,67 @@ class Libp2p extends EventEmitter {
return callback(notStarted('ping', this.state._state))
}
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
getPeerInfoRemote(peer, this)
.then(peerInfo => {
callback(null, new Ping(this._switch, peerInfo))
}, callback)
}
/**
* Registers the `handler` for each protocol
* @param {string[]|string} protocols
* @param {function({ connection:*, stream:*, protocol:string })} handler
*/
handle (protocols, handler) {
protocols = Array.isArray(protocols) ? protocols : [protocols]
protocols.forEach(protocol => {
this.upgrader.protocols.set(protocol, handler)
})
this.dialer.identifyService.pushToPeerStore(this.peerStore)
}
handle (protocol, handlerFunc, matchFunc) {
this._switch.handle(protocol, handlerFunc, matchFunc)
/**
* Removes the handler for each protocol. The protocol
* will no longer be supported on streams.
* @param {string[]|string} protocols
*/
unhandle (protocols) {
protocols = Array.isArray(protocols) ? protocols : [protocols]
protocols.forEach(protocol => {
this.upgrader.protocols.delete(protocol)
})
this.dialer.identifyService.pushToPeerStore(this.peerStore)
}
unhandle (protocol) {
this._switch.unhandle(protocol)
}
_onStarting () {
async _onStarting () {
if (!this._modules.transport) {
this.emit('error', new Error('no transports were present'))
return this.state('abort')
}
let ws
// so that we can have webrtc-star addrs without adding manually the id
const maOld = []
const maNew = []
this.peerInfo.multiaddrs.toArray().forEach((ma) => {
if (!ma.getPeerId()) {
maOld.push(ma)
maNew.push(ma.encapsulate('/p2p/' + this.peerInfo.id.toB58String()))
}
})
this.peerInfo.multiaddrs.replace(maOld, maNew)
const multiaddrs = this.peerInfo.multiaddrs.toArray()
this._modules.transport.forEach((Transport) => {
let t
// Start parallel tasks
const tasks = [
this.transportManager.listen(multiaddrs)
]
if (typeof Transport === 'function') {
t = new Transport({ libp2p: this })
} else {
t = Transport
if (this._config.pubsub.enabled) {
this.pubsub && this.pubsub.start()
}
if (t.filter(multiaddrs).length > 0) {
this._switch.transport.add(t.tag || t[Symbol.toStringTag], t)
} else if (WebSockets.isWebSockets(t)) {
// TODO find a cleaner way to signal that a transport is always used
// for dialing, even if no listener
ws = t
}
this._transport.push(t)
})
series([
(cb) => {
this.connectionManager.start()
this._switch.start(cb)
},
(cb) => {
if (ws) {
// always add dialing on websockets
this._switch.transport.add(ws.tag || ws.constructor.name, ws)
}
// detect which multiaddrs we don't have a transport for and remove them
const multiaddrs = this.peerInfo.multiaddrs.toArray()
multiaddrs.forEach((multiaddr) => {
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
!this._transport.find((transport) => transport.filter(multiaddr).length > 0)) {
this.peerInfo.multiaddrs.delete(multiaddr)
}
})
cb()
},
(cb) => {
if (this._dht) {
this._dht.start(() => {
this._dht.on('peer', this._peerDiscovered)
cb()
})
} else {
cb()
}
},
(cb) => {
if (this.pubsub) {
return this.pubsub.start(cb)
}
cb()
},
// Peer Discovery
(cb) => {
if (this._modules.peerDiscovery) {
this._setupPeerDiscovery(cb)
} else {
cb()
}
}
], (err) => {
if (err) {
try {
await Promise.all(tasks)
} catch (err) {
log.error(err)
this.emit('error', err)
return this.state('stop')
}
this.state('done')
})
}
_onStopping () {
series([
(cb) => {
// stop all discoveries before continuing with shutdown
parallel(
this._discovery.map((d) => {
d.removeListener('peer', this._peerDiscovered)
return (_cb) => d.stop((err) => {
log.error('an error occurred stopping the discovery service', err)
_cb()
})
}),
cb
)
},
(cb) => {
if (this.pubsub) {
return this.pubsub.stop(cb)
}
cb()
},
(cb) => {
if (this._dht) {
this._dht.removeListener('peer', this._peerDiscovered)
return this._dht.stop(cb)
}
cb()
},
(cb) => {
this.connectionManager.stop()
this._switch.stop(cb)
},
(cb) => {
// Ensures idempotent restarts, ignore any errors
// from removeAll, they're not useful at this point
this._switch.transport.removeAll(() => cb())
}
], (err) => {
if (err) {
log.error(err)
this.emit('error', err)
}
// libp2p has started
this.state('done')
})
}
/**
@ -489,12 +412,6 @@ class Libp2p extends EventEmitter {
* the `peer:discovery` event. If auto dial is enabled for libp2p
* and the current connection count is under the low watermark, the
* peer will be dialed.
*
* TODO: If `peerBook.put` becomes centralized, https://github.com/libp2p/js-libp2p/issues/345,
* it would be ideal if only new peers were emitted. Currently, with
* other modules adding peers to the `PeerBook` we have no way of knowing
* if a peer is new or not, so it has to be emitted.
*
* @private
* @param {PeerInfo} peerInfo
*/
@ -503,7 +420,7 @@ class Libp2p extends EventEmitter {
log.error(new Error(codes.ERR_DISCOVERED_SELF))
return
}
peerInfo = this.peerBook.put(peerInfo)
peerInfo = this.peerStore.put(peerInfo)
if (!this.isStarted()) return
@ -574,16 +491,15 @@ module.exports = Libp2p
* Like `new Libp2p(options)` except it will create a `PeerInfo`
* instance if one is not provided in options.
* @param {object} options Libp2p configuration options
* @param {function(Error, Libp2p)} callback
* @returns {void}
* @returns {Libp2p}
*/
module.exports.createLibp2p = promisify((options, callback) => {
module.exports.create = async (options = {}) => {
if (options.peerInfo) {
return nextTick(callback, null, new Libp2p(options))
return new Libp2p(options)
}
PeerInfo.create((err, peerInfo) => {
if (err) return callback(err)
const peerInfo = await PeerInfo.create()
options.peerInfo = peerInfo
callback(null, new Libp2p(options))
})
})
return new Libp2p(options)
}

67
src/insecure/plaintext.js Normal file
View File

@ -0,0 +1,67 @@
'use strict'
const handshake = require('it-handshake')
const lp = require('it-length-prefixed')
const PeerId = require('peer-id')
const debug = require('debug')
const log = debug('libp2p:plaintext')
log.error = debug('libp2p:plaintext:error')
const { UnexpectedPeerError, InvalidCryptoExchangeError } = require('libp2p-interfaces/src/crypto/errors')
const { Exchange, KeyType } = require('./proto')
const protocol = '/plaintext/2.0.0'
function lpEncodeExchange (exchange) {
const pb = Exchange.encode(exchange)
return lp.encode.single(pb)
}
async function encrypt (localId, conn, remoteId) {
const shake = handshake(conn)
// Encode the public key and write it to the remote peer
shake.write(lpEncodeExchange({
id: localId.toBytes(),
pubkey: {
Type: KeyType.RSA, // TODO: dont hard code
Data: localId.marshalPubKey()
}
}))
log('write pubkey exchange to peer %j', remoteId)
// Get the Exchange message
const response = (await lp.decode.fromReader(shake.reader).next()).value
const id = Exchange.decode(response.slice())
log('read pubkey exchange from peer %j', remoteId)
let peerId
try {
peerId = await PeerId.createFromPubKey(id.pubkey.Data)
} catch (err) {
log.error(err)
throw new InvalidCryptoExchangeError('Remote did not provide its public key')
}
if (remoteId && !peerId.isEqual(remoteId)) {
throw new UnexpectedPeerError()
}
log('plaintext key exchange completed successfully with peer %j', peerId)
shake.rest()
return {
conn: shake.stream,
remotePeer: peerId
}
}
module.exports = {
protocol,
secureInbound: (localId, conn, remoteId) => {
return encrypt(localId, conn, remoteId)
},
secureOutbound: (localId, conn, remoteId) => {
return encrypt(localId, conn, remoteId)
}
}

22
src/insecure/proto.js Normal file
View File

@ -0,0 +1,22 @@
'use strict'
const protobuf = require('protons')
module.exports = protobuf(`
message Exchange {
optional bytes id = 1;
optional PublicKey pubkey = 2;
}
enum KeyType {
RSA = 0;
Ed25519 = 1;
Secp256k1 = 2;
ECDSA = 3;
}
message PublicKey {
required KeyType Type = 1;
required bytes Data = 2;
}
`)

3
src/peer-store/README.md Normal file
View File

@ -0,0 +1,3 @@
# Peerstore
WIP

190
src/peer-store/index.js Normal file
View File

@ -0,0 +1,190 @@
'use strict'
const assert = require('assert')
const debug = require('debug')
const log = debug('libp2p:peer-store')
log.error = debug('libp2p:peer-store:error')
const { EventEmitter } = require('events')
const PeerInfo = require('peer-info')
/**
* Responsible for managing known peers, as well as their addresses and metadata
* @fires PeerStore#peer Emitted when a peer is connected to this node
* @fires PeerStore#change:protocols
* @fires PeerStore#change:multiaddrs
*/
class PeerStore extends EventEmitter {
constructor () {
super()
/**
* Map of peers
*
* @type {Map<string, PeerInfo>}
*/
this.peers = new Map()
// TODO: Track ourselves. We should split `peerInfo` up into its pieces so we get better
// control and observability. This will be the initial step for removing PeerInfo
// https://github.com/libp2p/go-libp2p-core/blob/master/peerstore/peerstore.go
// this.addressBook = new Map()
// this.protoBook = new Map()
}
/**
* Stores the peerInfo of a new peer.
* If already exist, its info is updated.
* @param {PeerInfo} peerInfo
*/
put (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
// Already know the peer?
if (this.peers.has(peerInfo.id.toB58String())) {
this.update(peerInfo)
} else {
this.add(peerInfo)
// Emit the new peer found
this.emit('peer', peerInfo)
}
}
/**
* Add a new peer to the store.
* @param {PeerInfo} peerInfo
*/
add (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
// Create new instance and add values to it
const newPeerInfo = new PeerInfo(peerInfo.id)
peerInfo.multiaddrs.forEach((ma) => newPeerInfo.multiaddrs.add(ma))
peerInfo.protocols.forEach((p) => newPeerInfo.protocols.add(p))
const connectedMa = peerInfo.isConnected()
connectedMa && newPeerInfo.connect(connectedMa)
const peerProxy = new Proxy(newPeerInfo, {
set: (obj, prop, value) => {
if (prop === 'multiaddrs') {
this.emit('change:multiaddrs', {
peerInfo: obj,
multiaddrs: value.toArray()
})
} else if (prop === 'protocols') {
this.emit('change:protocols', {
peerInfo: obj,
protocols: Array.from(value)
})
}
return Reflect.set(...arguments)
}
})
this.peers.set(peerInfo.id.toB58String(), peerProxy)
}
/**
* Updates an already known peer.
* @param {PeerInfo} peerInfo
*/
update (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
const id = peerInfo.id.toB58String()
const recorded = this.peers.get(id)
// pass active connection state
const ma = peerInfo.isConnected()
if (ma) {
recorded.connect(ma)
}
// Verify new multiaddrs
// TODO: better track added and removed multiaddrs
const multiaddrsIntersection = [
...recorded.multiaddrs.toArray()
].filter((m) => peerInfo.multiaddrs.has(m))
if (multiaddrsIntersection.length !== peerInfo.multiaddrs.size ||
multiaddrsIntersection.length !== recorded.multiaddrs.size) {
// recorded.multiaddrs = peerInfo.multiaddrs
recorded.multiaddrs.clear()
for (const ma of peerInfo.multiaddrs.toArray()) {
recorded.multiaddrs.add(ma)
}
this.emit('change:multiaddrs', {
peerInfo: peerInfo,
multiaddrs: recorded.multiaddrs.toArray()
})
}
// Update protocols
// TODO: better track added and removed protocols
const protocolsIntersection = new Set(
[...recorded.protocols].filter((p) => peerInfo.protocols.has(p))
)
if (protocolsIntersection.size !== peerInfo.protocols.size ||
protocolsIntersection.size !== recorded.protocols.size) {
recorded.protocols.clear()
for (const protocol of peerInfo.protocols) {
recorded.protocols.add(protocol)
}
this.emit('change:protocols', {
peerInfo: peerInfo,
protocols: Array.from(recorded.protocols)
})
}
// Add the public key if missing
if (!recorded.id.pubKey && peerInfo.id.pubKey) {
recorded.id.pubKey = peerInfo.id.pubKey
}
}
/**
* Get the info to the given id.
* @param {string} peerId b58str id
* @returns {PeerInfo}
*/
get (peerId) {
const peerInfo = this.peers.get(peerId)
if (peerInfo) {
return peerInfo
}
return undefined
}
/**
* Removes the Peer with the matching `peerId` from the PeerStore
* @param {string} peerId b58str id
* @returns {boolean} true if found and removed
*/
remove (peerId) {
return this.peers.delete(peerId)
}
/**
* Completely replaces the existing peers metadata with the given `peerInfo`
* @param {PeerInfo} peerInfo
* @returns {void}
*/
replace (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
this.remove(peerInfo.id.toB58String())
this.add(peerInfo)
}
}
module.exports = PeerStore

23
src/ping/README.md Normal file
View File

@ -0,0 +1,23 @@
libp2p-ping JavaScript Implementation
=====================================
> IPFS ping protocol JavaScript implementation
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-ping.
## Usage
```javascript
var Ping = require('libp2p-ping')
Ping.mount(swarm) // Enable this peer to echo Ping requests
var p = new Ping(swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object
p.on('ping', function (time) {
console.log(time + 'ms')
p.stop() // stop sending pings
})
p.start()
```

6
src/ping/constants.js Normal file
View File

@ -0,0 +1,6 @@
'use strict'
module.exports = {
PROTOCOL: '/ipfs/ping/1.0.0',
PING_LENGTH: 32
}

50
src/ping/handler.js Normal file
View File

@ -0,0 +1,50 @@
'use strict'
const pull = require('pull-stream/pull')
const handshake = require('pull-handshake')
const constants = require('./constants')
const PROTOCOL = constants.PROTOCOL
const PING_LENGTH = constants.PING_LENGTH
const debug = require('debug')
const log = debug('libp2p-ping')
log.error = debug('libp2p-ping:error')
function mount (swarm) {
swarm.handle(PROTOCOL, (protocol, conn) => {
const stream = handshake({ timeout: 0 })
const shake = stream.handshake
// receive and echo back
function next () {
shake.read(PING_LENGTH, (err, buf) => {
if (err === true) {
// stream closed
return
}
if (err) {
return log.error(err)
}
shake.write(buf)
return next()
})
}
pull(
conn,
stream,
conn
)
next()
})
}
function unmount (swarm) {
swarm.unhandle(PROTOCOL)
}
exports = module.exports
exports.mount = mount
exports.unmount = unmount

7
src/ping/index.js Normal file
View File

@ -0,0 +1,7 @@
'use strict'
const handler = require('./handler')
exports = module.exports = require('./ping')
exports.mount = handler.mount
exports.unmount = handler.unmount

83
src/ping/ping.js Normal file
View File

@ -0,0 +1,83 @@
'use strict'
const EventEmitter = require('events').EventEmitter
const pull = require('pull-stream/pull')
const empty = require('pull-stream/sources/empty')
const handshake = require('pull-handshake')
const constants = require('./constants')
const util = require('./util')
const rnd = util.rnd
const debug = require('debug')
const log = debug('libp2p-ping')
log.error = debug('libp2p-ping:error')
const PROTOCOL = constants.PROTOCOL
const PING_LENGTH = constants.PING_LENGTH
class Ping extends EventEmitter {
constructor (swarm, peer) {
super()
this._stopped = false
this.peer = peer
this.swarm = swarm
}
start () {
log('dialing %s to %s', PROTOCOL, this.peer.id.toB58String())
this.swarm.dial(this.peer, PROTOCOL, (err, conn) => {
if (err) {
return this.emit('error', err)
}
const stream = handshake({ timeout: 0 })
this.shake = stream.handshake
pull(
stream,
conn,
stream
)
// write and wait to see ping back
const self = this
function next () {
const start = new Date()
const buf = rnd(PING_LENGTH)
self.shake.write(buf)
self.shake.read(PING_LENGTH, (err, bufBack) => {
const end = new Date()
if (err || !buf.equals(bufBack)) {
const err = new Error('Received wrong ping ack')
return self.emit('error', err)
}
self.emit('ping', end - start)
if (self._stopped) {
return
}
next()
})
}
next()
})
}
stop () {
if (this._stopped || !this.shake) {
return
}
this._stopped = true
pull(
empty(),
this.shake.rest()
)
}
}
module.exports = Ping

13
src/ping/util.js Normal file
View File

@ -0,0 +1,13 @@
'use strict'
const crypto = require('libp2p-crypto')
const constants = require('./constants')
exports = module.exports
exports.rnd = (length) => {
if (!length) {
length = constants.PING_LENGTH
}
return crypto.randomBytes(length)
}

67
src/pnet/README.md Normal file
View File

@ -0,0 +1,67 @@
js-libp2p-pnet
==================
> Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-pnet.
## Table of Contents
- [Usage](#usage)
- [Examples](#examples)
- [Private Shared Keys (PSK)](#private-shared-keys)
- [PSK Generation](#psk-generation)
- [Contribute](#contribute)
- [License](#license)
## Usage
```js
const Protector = require('libp2p-pnet')
const protector = new Protector(swarmKeyBuffer)
const privateConnection = protector.protect(myPublicConnection, (err) => { })
```
### Examples
[Private Networks with IPFS](./examples/pnet-ipfs)
### Private Shared Keys
Private Shared Keys are expected to be in the following format:
```
/key/swarm/psk/1.0.0/
/base16/
dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
```
### PSK Generation
A utility method has been created to generate a key for your private network. You can
use one of the methods below to generate your key.
#### From libp2p-pnet
If you have libp2p-pnet locally, you can run the following from the projects root.
```sh
node ./key-generator.js > swarm.key
```
#### From a module using libp2p
If you have a module locally that depends on libp2p-pnet, you can run the following from
that project, assuming the node_modules are installed.
```sh
node -e "require('libp2p-pnet').generate(process.stdout)" > swarm.key
```
#### Programmatically
```js
const writeKey = require('libp2p-pnet').generate
const swarmKey = Buffer.alloc(95)
writeKey(swarmKey)
fs.writeFileSync('swarm.key', swarmKey)
```

78
src/pnet/crypto.js Normal file
View File

@ -0,0 +1,78 @@
'use strict'
const debug = require('debug')
const Errors = require('./errors')
const xsalsa20 = require('xsalsa20')
const KEY_LENGTH = require('./key-generator').KEY_LENGTH
const log = debug('libp2p:pnet')
log.trace = debug('libp2p:pnet:trace')
log.error = debug('libp2p:pnet:err')
/**
* Creates a stream iterable to encrypt messages in a private network
*
* @param {Buffer} nonce The nonce to use in encryption
* @param {Buffer} psk The private shared key to use in encryption
* @returns {*} a through iterable
*/
module.exports.createBoxStream = (nonce, psk) => {
const xor = xsalsa20(nonce, psk)
return (source) => (async function * () {
for await (const chunk of source) {
yield Buffer.from(xor.update(chunk.slice()))
}
})()
}
/**
* Creates a stream iterable to decrypt messages in a private network
*
* @param {Buffer} nonce The nonce of the remote peer
* @param {Buffer} psk The private shared key to use in decryption
* @returns {*} a through iterable
*/
module.exports.createUnboxStream = (nonce, psk) => {
return (source) => (async function * () {
const xor = xsalsa20(nonce, psk)
log.trace('Decryption enabled')
for await (const chunk of source) {
yield Buffer.from(xor.update(chunk.slice()))
}
})()
}
/**
* Decode the version 1 psk from the given Buffer
*
* @param {Buffer} pskBuffer
* @throws {INVALID_PSK}
* @returns {Object} The PSK metadata (tag, codecName, psk)
*/
module.exports.decodeV1PSK = (pskBuffer) => {
try {
// This should pull from multibase/multicodec to allow for
// more encoding flexibility. Ideally we'd consume the codecs
// from the buffer line by line to evaluate the next line
// programmatically instead of making assumptions about the
// encodings of each line.
const metadata = pskBuffer.toString().split(/(?:\r\n|\r|\n)/g)
const pskTag = metadata.shift()
const codec = metadata.shift()
const psk = Buffer.from(metadata.shift(), 'hex')
if (psk.byteLength !== KEY_LENGTH) {
throw new Error(Errors.INVALID_PSK)
}
return {
tag: pskTag,
codecName: codec,
psk: psk
}
} catch (err) {
log.error(err)
throw new Error(Errors.INVALID_PSK)
}
}

7
src/pnet/errors.js Normal file
View File

@ -0,0 +1,7 @@
'use strict'
module.exports.INVALID_PEER = 'Not a valid peer connection'
module.exports.INVALID_PSK = 'Your private shared key is invalid'
module.exports.NO_LOCAL_ID = 'No local private key provided'
module.exports.NO_HANDSHAKE_CONNECTION = 'No connection for the handshake provided'
module.exports.STREAM_ENDED = 'Stream ended prematurely'

75
src/pnet/index.js Normal file
View File

@ -0,0 +1,75 @@
'use strict'
const pipe = require('it-pipe')
const assert = require('assert')
const duplexPair = require('it-pair/duplex')
const crypto = require('libp2p-crypto')
const Errors = require('./errors')
const {
createBoxStream,
createUnboxStream,
decodeV1PSK
} = require('./crypto')
const handshake = require('it-handshake')
const { NONCE_LENGTH } = require('./key-generator')
const debug = require('debug')
const log = debug('libp2p:pnet')
log.err = debug('libp2p:pnet:err')
/**
* Takes a Private Shared Key (psk) and provides a `protect` method
* for wrapping existing connections in a private encryption stream
*/
class Protector {
/**
* @param {Buffer} keyBuffer The private shared key buffer
* @constructor
*/
constructor (keyBuffer) {
const decodedPSK = decodeV1PSK(keyBuffer)
this.psk = decodedPSK.psk
this.tag = decodedPSK.tag
}
/**
* Takes a given Connection and creates a private encryption stream
* between its two peers from the PSK the Protector instance was
* created with.
*
* @param {Connection} connection The connection to protect
* @returns {*} A protected duplex iterable
*/
async protect (connection) {
assert(connection, Errors.NO_HANDSHAKE_CONNECTION)
// Exchange nonces
log('protecting the connection')
const localNonce = crypto.randomBytes(NONCE_LENGTH)
const shake = handshake(connection)
shake.write(localNonce)
const result = await shake.reader.next(NONCE_LENGTH)
const remoteNonce = result.value.slice()
shake.rest()
// Create the boxing/unboxing pipe
log('exchanged nonces')
const [internal, external] = duplexPair()
pipe(
external,
// Encrypt all outbound traffic
createBoxStream(localNonce, this.psk),
shake.stream,
// Decrypt all inbound traffic
createUnboxStream(remoteNonce, this.psk),
external
)
return internal
}
}
module.exports = Protector
module.exports.errors = Errors
module.exports.generate = require('./key-generator')

22
src/pnet/key-generator.js Normal file
View File

@ -0,0 +1,22 @@
'use strict'
const crypto = require('crypto')
const KEY_LENGTH = 32
/**
* Generates a PSK that can be used in a libp2p-pnet private network
* @param {Writer} writer An object containing a `write` method
* @returns {void}
*/
function generate (writer) {
const psk = crypto.randomBytes(KEY_LENGTH).toString('hex')
writer.write('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
}
module.exports = generate
module.exports.NONCE_LENGTH = 24
module.exports.KEY_LENGTH = KEY_LENGTH
if (require.main === module) {
generate(process.stdout)
}

View File

@ -1,84 +1,38 @@
'use strict'
const nextTick = require('async/nextTick')
const { messages, codes } = require('./errors')
const promisify = require('promisify-es6')
const errCode = require('err-code')
const { messages, codes } = require('./errors')
module.exports = (node, Pubsub) => {
const pubsub = new Pubsub(node, { emitSelf: true })
module.exports = (node, Pubsub, config) => {
const pubsub = new Pubsub(node.peerInfo, node.registrar, config)
return {
/**
* Subscribe the given handler to a pubsub topic
*
* @param {string} topic
* @param {function} handler The handler to subscribe
* @param {object|null} [options]
* @param {function} [callback] An optional callback
*
* @returns {Promise|void} A promise is returned if no callback is provided
*
* @example <caption>Subscribe a handler to a topic</caption>
*
* // `null` must be passed for options until subscribe is no longer using promisify
* const handler = (message) => { }
* await libp2p.subscribe(topic, handler, null)
*
* @example <caption>Use a callback instead of the Promise api</caption>
*
* // `options` may be passed or omitted when supplying a callback
* const handler = (message) => { }
* libp2p.subscribe(topic, handler, callback)
* @returns {void}
*/
subscribe: promisify((topic, handler, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
subscribe: (topic, handler) => {
if (!node.isStarted() && !pubsub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
}
function subscribe (cb) {
if (pubsub.listenerCount(topic) === 0) {
pubsub.subscribe(topic)
}
pubsub.on(topic, handler)
nextTick(cb)
}
subscribe(callback)
}),
},
/**
* Unsubscribes from a pubsub topic
*
* @param {string} topic
* @param {function|null} handler The handler to unsubscribe from
* @param {function} [callback] An optional callback
*
* @returns {Promise|void} A promise is returned if no callback is provided
*
* @example <caption>Unsubscribe a topic for all handlers</caption>
*
* // `null` must be passed until unsubscribe is no longer using promisify
* await libp2p.unsubscribe(topic, null)
*
* @example <caption>Unsubscribe a topic for 1 handler</caption>
*
* await libp2p.unsubscribe(topic, handler)
*
* @example <caption>Use a callback instead of the Promise api</caption>
*
* libp2p.unsubscribe(topic, handler, callback)
* @param {function} [handler] The handler to unsubscribe from
*/
unsubscribe: promisify((topic, handler, callback) => {
unsubscribe: (topic, handler) => {
if (!node.isStarted() && !pubsub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
}
if (!handler) {
@ -90,61 +44,61 @@ module.exports = (node, Pubsub) => {
if (pubsub.listenerCount(topic) === 0) {
pubsub.unsubscribe(topic)
}
},
if (typeof callback === 'function') {
return nextTick(() => callback())
}
return Promise.resolve()
}),
publish: promisify((topic, data, callback) => {
/**
* Publish messages to the given topics.
* @param {Array<string>|string} topic
* @param {Buffer} data
* @returns {Promise<void>}
*/
publish: (topic, data) => {
if (!node.isStarted() && !pubsub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
}
try {
data = Buffer.from(data)
} catch (err) {
return nextTick(callback, errCode(new Error('data must be convertible to a Buffer'), 'ERR_DATA_IS_NOT_VALID'))
throw errCode(new Error('data must be convertible to a Buffer'), 'ERR_DATA_IS_NOT_VALID')
}
pubsub.publish(topic, data, callback)
}),
return pubsub.publish(topic, data)
},
ls: promisify((callback) => {
/**
* Get a list of topics the node is subscribed to.
* @returns {Array<String>} topics
*/
getTopics: () => {
if (!node.isStarted() && !pubsub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
}
const subscriptions = Array.from(pubsub.subscriptions)
return pubsub.getTopics()
},
nextTick(() => callback(null, subscriptions))
}),
peers: promisify((topic, callback) => {
/**
* Get a list of the peer-ids that are subscribed to one topic.
* @param {string} topic
* @returns {Array<string>}
*/
getPeersSubscribed: (topic) => {
if (!node.isStarted() && !pubsub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
}
if (typeof topic === 'function') {
callback = topic
topic = null
}
const peers = Array.from(pubsub.peers.values())
.filter((peer) => topic ? peer.topics.has(topic) : true)
.map((peer) => peer.info.id.toB58String())
nextTick(() => callback(null, peers))
}),
return pubsub.getPeersSubscribed(topic)
},
setMaxListeners (n) {
return pubsub.setMaxListeners(n)
},
start: promisify((cb) => pubsub.start(cb)),
_pubsub: pubsub,
stop: promisify((cb) => pubsub.stop(cb))
start: () => pubsub.start(),
stop: () => pubsub.stop()
}
}

138
src/registrar.js Normal file
View File

@ -0,0 +1,138 @@
'use strict'
const assert = require('assert')
const debug = require('debug')
const log = debug('libp2p:peer-store')
log.error = debug('libp2p:peer-store:error')
const Topology = require('libp2p-interfaces/src/topology')
const { Connection } = require('libp2p-interfaces/src/connection')
const PeerInfo = require('peer-info')
/**
* Responsible for notifying registered protocols of events in the network.
*/
class Registrar {
/**
* @param {Object} props
* @param {PeerStore} props.peerStore
* @constructor
*/
constructor ({ peerStore }) {
this.peerStore = peerStore
/**
* Map of connections per peer
* TODO: this should be handled by connectionManager
* @type {Map<string, Array<conn>>}
*/
this.connections = new Map()
/**
* Map of topologies
*
* @type {Map<string, object>}
*/
this.topologies = new Map()
this._handle = undefined
}
get handle () {
return this._handle
}
set handle (handle) {
this._handle = handle
}
/**
* Add a new connected peer to the record
* TODO: this should live in the ConnectionManager
* @param {PeerInfo} peerInfo
* @param {Connection} conn
* @returns {void}
*/
onConnect (peerInfo, conn) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection')
const id = peerInfo.id.toB58String()
const storedConn = this.connections.get(id)
if (storedConn) {
storedConn.push(conn)
} else {
this.connections.set(id, [conn])
}
}
/**
* Remove a disconnected peer from the record
* TODO: this should live in the ConnectionManager
* @param {PeerInfo} peerInfo
* @param {Connection} connection
* @param {Error} [error]
* @returns {void}
*/
onDisconnect (peerInfo, connection, error) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
const id = peerInfo.id.toB58String()
let storedConn = this.connections.get(id)
if (storedConn && storedConn.length > 1) {
storedConn = storedConn.filter((conn) => conn.id === connection.id)
} else if (storedConn) {
for (const [, topology] of this.topologies) {
topology.disconnect(peerInfo, error)
}
this.connections.delete(peerInfo.id.toB58String())
}
}
/**
* Get a connection with a peer.
* @param {PeerInfo} peerInfo
* @returns {Connection}
*/
getConnection (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
// TODO: what should we return
return this.connections.get(peerInfo.id.toB58String())[0]
}
/**
* Register handlers for a set of multicodecs given
* @param {Topology} topology protocol topology
* @return {string} registrar identifier
*/
register (topology) {
assert(
Topology.isTopology(topology),
'topology must be an instance of interfaces/topology')
// Create topology
const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
this.topologies.set(id, topology)
// Set registrar
topology.registrar = this
return id
}
/**
* Unregister topology.
* @param {string} id registrar identifier
* @return {boolean} unregistered successfully
*/
unregister (id) {
return this.topologies.delete(id)
}
}
module.exports = Registrar

423
src/switch/README.md Normal file
View File

@ -0,0 +1,423 @@
libp2p-switch JavaScript implementation
======================================
> libp2p-switch is a dialer machine, it leverages the multiple libp2p transports, stream muxers, crypto channels and other connection upgrades to dial to peers in the libp2p network. It also supports Protocol Multiplexing through a multicodec and multistream-select handshake.
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-switch.
## Table of Contents
- [Install](#install)
- [Usage](#usage)
- [Create a libp2p switch](#create-a-libp2p-switch)
- [API](#api)
- [`switch.connection`](#switchconnection)
- [`switch.dial(peer, protocol, callback)`](#switchdialpeer-protocol-callback)
- [`switch.dialFSM(peer, protocol, callback)`](#switchdialfsmpeer-protocol-callback)
- [`switch.handle(protocol, handlerFunc, matchFunc)`](#switchhandleprotocol-handlerfunc-matchfunc)
- [`switch.hangUp(peer, callback)`](#switchhanguppeer-callback)
- [`switch.start(callback)`](#switchstartcallback)
- [`switch.stop(callback)`](#switchstopcallback)
- [`switch.stats`](#stats-api)
- [`switch.unhandle(protocol)`](#switchunhandleprotocol)
- [Internal Transports API](#internal-transports-api)
- [Design Notes](#design-notes)
- [Multitransport](#multitransport)
- [Connection upgrades](#connection-upgrades)
- [Identify](#identify)
- [Notes](#notes)
- [Contribute](#contribute)
- [License](#license)
## Install
```bash
> npm install libp2p-switch --save
```
## Usage
### Create a libp2p Switch
```JavaScript
const switch = require('libp2p-switch')
const sw = new switch(peerInfo , peerBook [, options])
```
If defined, `options` should be an object with the following keys and respective values:
- `denyTTL`: - number of ms a peer should not be dialable to after it errors. Each successive deny will increase the TTL from the base value. Defaults to 5 minutes
- `denyAttempts`: - number of times a peer can be denied before they are permanently denied. Defaults to 5.
- `maxParallelDials`: - number of concurrent dials the switch should allow. Defaults to `100`
- `maxColdCalls`: - number of queued cold calls that are allowed. Defaults to `50`
- `dialTimeout`: - number of ms a dial to a peer should be allowed to run. Defaults to `30000` (30 seconds)
- `stats`: an object with the following keys and respective values:
- `maxOldPeersRetention`: maximum old peers retention. For when peers disconnect and keeping the stats around in case they reconnect. Defaults to `100`.
- `computeThrottleMaxQueueSize`: maximum queue size to perform stats computation throttling. Defaults to `1000`.
- `computeThrottleTimeout`: Throttle timeout, in miliseconds. Defaults to `2000`,
- `movingAverageIntervals`: Array containin the intervals, in miliseconds, for which moving averages are calculated. Defaults to:
```js
[
60 * 1000, // 1 minute
5 * 60 * 1000, // 5 minutes
15 * 60 * 1000 // 15 minutes
]
```
### Private Networks
libp2p-switch supports private networking. In order to enabled private networks, the `switch.protector` must be
set and must contain a `protect` method. You can see an example of this in the [private network
tests]([./test/pnet.node.js]).
## API
- peerInfo is a [PeerInfo](https://github.com/libp2p/js-peer-info) object that has the peer information.
- peerBook is a [PeerBook](https://github.com/libp2p/js-peer-book) object that stores all the known peers.
### `switch.connection`
##### `switch.connection.addUpgrade()`
A connection upgrade must be able to receive and return something that implements the [interface-connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) specification.
> **WIP**
##### `switch.connection.addStreamMuxer(muxer)`
Upgrading a connection to use a stream muxer is still considered an upgrade, but a special case since once this connection is applied, the returned obj will implement the [interface-stream-muxer](https://github.com/libp2p/interface-stream-muxer) spec.
- `muxer`
##### `switch.connection.reuse()`
Enable the identify protocol.
##### `switch.connection.crypto([tag, encrypt])`
Enable a specified crypto protocol. By default no encryption is used, aka `plaintext`. If called with no arguments it resets to use `plaintext`.
You can use for example [libp2p-secio](https://github.com/libp2p/js-libp2p-secio) like this
```js
const secio = require('libp2p-secio')
switch.connection.crypto(secio.tag, secio.encrypt)
```
##### `switch.connection.enableCircuitRelay(options, callback)`
Enable circuit relaying.
- `options`
- enabled - activates relay dialing and listening functionality
- hop - an object with two properties
- enabled - enables circuit relaying
- active - is it an active or passive relay (default false)
- `callback`
### `switch.dial(peer, protocol, callback)`
dial uses the best transport (whatever works first, in the future we can have some criteria), and jump starts the connection until the point where we have to negotiate the protocol. If a muxer is available, then drop the muxer onto that connection. Good to warm up connections or to check for connectivity. If we have already a muxer for that peerInfo, then do nothing.
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `protocol`
- `callback`
### `switch.dialFSM(peer, protocol, callback)`
works like dial, but calls back with a [Connection State Machine](#connection-state-machine)
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0') to be used
- `callback`: Function with signature `function (err, connFSM) {}` where `connFSM` is a [Connection State Machine](#connection-state-machine)
#### Connection State Machine
Connection state machines emit a number of events that can be used to determine the current state of the connection
and to received the underlying connection that can be used to transfer data.
### `switch.dialer.connect(peer, options, callback)`
a low priority dial to the provided peer. Calls to `dial` and `dialFSM` will take priority. This should be used when an application only wishes to establish connections to new peers, such as during peer discovery when there is a low peer count. Currently, anything greater than the HIGH_PRIORITY (10) will be placed into the cold call queue, and anything less than or equal to the HIGH_PRIORITY will be added to the normal queue.
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `options`: Optional
- `options.priority`: Number of the priority of the dial, defaults to 20.
- `options.useFSM`: Boolean of whether or not to callback with a [Connection State Machine](#connection-state-machine)
- `callback`: Function with signature `function (err, connFSM) {}` where `connFSM` is a [Connection State Machine](#connection-state-machine)
##### Events
- `error`: emitted whenever a fatal error occurs with the connection; the error will be emitted.
- `error:upgrade_failed`: emitted whenever the connection fails to upgrade with a muxer, this is not fatal.
- `error:connection_attempt_failed`: emitted whenever a dial attempt fails for a given transport. An array of errors is emitted.
- `connection`: emitted whenever a useable connection has been established; the underlying [Connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) will be emitted.
- `close`: emitted when the connection has closed.
### `switch.handle(protocol, handlerFunc, matchFunc)`
Handle a new protocol.
- `protocol`
- `handlerFunc` - function called when we receive a dial on `protocol. Signature must be `function (protocol, conn) {}`
- `matchFunc` - matchFunc for multistream-select
### `switch.hangUp(peer, callback)`
Hang up the muxed connection we have with the peer.
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `callback`
### `switch.on('error', (err) => {})`
Emitted when the switch encounters an error.
- `err`: instance of [Error][]
### `switch.on('peer-mux-established', (peer) => {})`
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just established a muxed connection with.
### `switch.on('peer-mux-closed', (peer) => {})`
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection with.
### `switch.on('connection:start', (peer) => {})`
This will be triggered anytime a new connection is created.
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just started a connection with.
### `switch.on('connection:end', (peer) => {})`
This will be triggered anytime an existing connection, regardless of state, is removed from the switch's internal connection tracking.
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a connection with.
### `switch.on('start', () => {})`
Emitted when the switch has successfully started.
### `switch.on('stop', () => {})`
Emitted when the switch has successfully stopped.
### `switch.start(callback)`
Start listening on all added transports that are available on the current `peerInfo`.
### `switch.stop(callback)`
Close all the listeners and muxers.
- `callback`
### Stats API
##### `switch.stats.emit('update')`
Every time any stat value changes, this object emits an `update` event.
#### Global stats
##### `switch.stats.global.snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `switch.stats.global.movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-transport stats
##### `switch.stats.transports()`
Returns an array containing the tags (string) for each observed transport.
##### `switch.stats.forTransport(transportTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `switch.stats.forTransport(transportTag).movingAverages`
Returns an object containing the following keys:
dataSent
dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-protocol stats
##### `switch.stats.protocols()`
Returns an array containing the tags (string) for each observed protocol.
##### `switch.stats.forProtocol(protocolTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `switch.stats.forProtocol(protocolTag).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-peer stats
##### `switch.stats.peers()`
Returns an array containing the peerIDs (B58-encoded string) for each observed peer.
##### `switch.stats.forPeer(peerId:String).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `switch.stats.forPeer(peerId:String).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Stats update interval
Stats are not updated in real-time. Instead, measurements are buffered and stats are updated at an interval. The maximum interval can be defined through the `Switch` constructor option `stats.computeThrottleTimeout`, defined in miliseconds.
### `switch.unhandle(protocol)`
Unhandle a protocol.
- `protocol`
### Internal Transports API
##### `switch.transport.add(key, transport, options)`
libp2p-switch expects transports that implement [interface-transport](https://github.com/libp2p/interface-transport). For example [libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp).
- `key` - the transport identifier.
- `transport` -
- `options` -
##### `switch.transport.dial(key, multiaddrs, callback)`
Dial to a peer on a specific transport.
- `key`
- `multiaddrs`
- `callback`
##### `switch.transport.listen(key, options, handler, callback)`
Set a transport to start listening mode.
- `key`
- `options`
- `handler`
- `callback`
##### `switch.transport.close(key, callback)`
Close the listeners of a given transport.
- `key`
- `callback`
## Design Notes
### Multitransport
libp2p is designed to support multiple transports at the same time. While peers are identified by their ID (which are generated from their public keys), the addresses of each pair may vary, depending the device where they are being run or the network in which they are accessible through.
In order for a transport to be supported, it has to follow the [interface-transport](https://github.com/libp2p/interface-transport) spec.
### Connection upgrades
Each connection in libp2p follows the [interface-connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) spec. This design decision enables libp2p to have upgradable transports.
We think of `upgrade` as a very important notion when we are talking about connections, we can see mechanisms like: stream multiplexing, congestion control, encrypted channels, multipath, simulcast, etc, as `upgrades` to a connection. A connection can be a simple and with no guarantees, drop a packet on the network with a destination thing, a transport in the other hand can be a connection and or a set of different upgrades that are mounted on top of each other, giving extra functionality to that connection and therefore `upgrading` it.
Types of upgrades to a connection:
- encrypted channel (with TLS for e.g)
- congestion flow (some transports don't have it by default)
- multipath (open several connections and abstract it as a single connection)
- simulcast (still really thinking this one through, it might be interesting to send a packet through different connections under some hard network circumstances)
- stream-muxer - this a special case, because once we upgrade a connection to a stream-muxer, we can open more streams (multiplex them) on a single stream, also enabling us to reuse the underlying dialed transport
We also want to enable flexibility when it comes to upgrading a connection, for example, we might that all dialed transports pass through the encrypted channel upgrade, but not the congestion flow, specially when a transport might have already some underlying properties (UDP vs TCP vs WebRTC vs every other transport protocol)
### Identify
Identify is a protocol that switchs mounts on top of itself, to identify the connections between any two peers. E.g:
- a) peer A dials a conn to peer B
- b) that conn gets upgraded to a stream multiplexer that both peers agree
- c) peer B executes de identify protocol
- d) peer B now can open streams to peer A, knowing which is the
identity of peer A
In addition to this, we also share the "observed addresses" by the other peer, which is extremely useful information for different kinds of network topologies.
### Notes
To avoid the confusion between connection, stream, transport, and other names that represent an abstraction of data flow between two points, we use terms as:
- connection - something that implements the transversal expectations of a stream between two peers, including the benefits of using a stream plus having a way to do half duplex, full duplex
- transport - something that as a dial/listen interface and return objs that implement a connection interface
### This module uses `pull-streams`
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
You can learn more about pull-streams at:
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
- [pull-streams documentation](https://pull-stream.github.io/)
#### Converting `pull-streams` to Node.js Streams
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
```js
const pullToStream = require('pull-stream-to-stream')
const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
```
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.

View File

@ -0,0 +1,126 @@
'use strict'
const EventEmitter = require('events').EventEmitter
const debug = require('debug')
const withIs = require('class-is')
class BaseConnection extends EventEmitter {
constructor ({ _switch, name }) {
super()
this.switch = _switch
this.ourPeerInfo = this.switch._peerInfo
this.log = debug(`libp2p:conn:${name}`)
this.log.error = debug(`libp2p:conn:${name}:error`)
}
/**
* Puts the state into its disconnecting flow
*
* @param {Error} err Will be emitted if provided
* @returns {void}
*/
close (err) {
if (this._state._state === 'DISCONNECTING') return
this.log('closing connection to %s', this.theirB58Id)
if (err && this._events.error) {
this.emit('error', err)
}
this._state('disconnect')
}
emit (eventName, ...args) {
if (eventName === 'error' && !this._events.error) {
this.log.error(...args)
} else {
super.emit(eventName, ...args)
}
}
/**
* Gets the current state of the connection
*
* @returns {string} The current state of the connection
*/
getState () {
return this._state._state
}
/**
* Puts the state into encrypting mode
*
* @returns {void}
*/
encrypt () {
this._state('encrypt')
}
/**
* Puts the state into privatizing mode
*
* @returns {void}
*/
protect () {
this._state('privatize')
}
/**
* Puts the state into muxing mode
*
* @returns {void}
*/
upgrade () {
this._state('upgrade')
}
/**
* Event handler for disconnected.
*
* @fires BaseConnection#close
* @returns {void}
*/
_onDisconnected () {
this.switch.connection.remove(this)
this.log('disconnected from %s', this.theirB58Id)
this.emit('close')
this.removeAllListeners()
}
/**
* Event handler for privatized
*
* @fires BaseConnection#private
* @returns {void}
*/
_onPrivatized () {
this.emit('private', this.conn)
}
/**
* Wraps this.conn with the Switch.protector for private connections
*
* @private
* @fires ConnectionFSM#error
* @returns {void}
*/
_onPrivatizing () {
if (!this.switch.protector) {
return this._state('done')
}
this.conn = this.switch.protector.protect(this.conn, (err) => {
if (err) {
return this.close(err)
}
this.log('successfully privatized conn to %s', this.theirB58Id)
this.conn.setPeerInfo(this.theirPeerInfo)
this._state('done')
})
}
}
module.exports = withIs(BaseConnection, {
className: 'BaseConnection',
symbolName: 'libp2p-switch/BaseConnection'
})

View File

@ -0,0 +1,47 @@
'use strict'
const debug = require('debug')
const IncomingConnection = require('./incoming')
const observeConn = require('../observe-connection')
function listener (_switch) {
const log = debug('libp2p:switch:listener')
/**
* Takes a transport key and returns a connection handler function
*
* @param {string} transportKey The key of the transport to handle connections for
* @param {function} handler A custom handler to use
* @returns {function(Connection)} A connection handler function
*/
return function (transportKey, handler) {
/**
* Takes a base connection and manages listening behavior
*
* @param {Connection} conn The connection to manage
* @returns {void}
*/
return function (conn) {
log('received incoming connection for transport %s', transportKey)
conn.getPeerInfo((_, peerInfo) => {
// Add a transport level observer, if needed
const connection = transportKey ? observeConn(transportKey, null, conn, _switch.observer) : conn
const connFSM = new IncomingConnection({ connection, _switch, transportKey, peerInfo })
connFSM.once('error', (err) => log(err))
connFSM.once('private', (_conn) => {
// Use the custom handler, if it was provided
if (handler) {
return handler(_conn)
}
connFSM.encrypt()
})
connFSM.once('encrypted', () => connFSM.upgrade())
connFSM.protect()
})
}
}
}
module.exports = listener

View File

@ -0,0 +1,115 @@
'use strict'
const FSM = require('fsm-event')
const multistream = require('multistream-select')
const withIs = require('class-is')
const BaseConnection = require('./base')
class IncomingConnectionFSM extends BaseConnection {
constructor ({ connection, _switch, transportKey, peerInfo }) {
super({
_switch,
name: `inc:${_switch._peerInfo.id.toB58String().slice(0, 8)}`
})
this.conn = connection
this.theirPeerInfo = peerInfo || null
this.theirB58Id = this.theirPeerInfo ? this.theirPeerInfo.id.toB58String() : null
this.ourPeerInfo = this.switch._peerInfo
this.transportKey = transportKey
this.protocolMuxer = this.switch.protocolMuxer(this.transportKey)
this.msListener = new multistream.Listener()
this._state = FSM('DIALED', {
DISCONNECTED: {
disconnect: 'DISCONNECTED'
},
DIALED: { // Base connection to peer established
privatize: 'PRIVATIZING',
encrypt: 'ENCRYPTING'
},
PRIVATIZING: { // Protecting the base connection
done: 'PRIVATIZED',
disconnect: 'DISCONNECTING'
},
PRIVATIZED: { // Base connection is protected
encrypt: 'ENCRYPTING'
},
ENCRYPTING: { // Encrypting the base connection
done: 'ENCRYPTED',
disconnect: 'DISCONNECTING'
},
ENCRYPTED: { // Upgrading could not happen, the connection is encrypted and waiting
upgrade: 'UPGRADING',
disconnect: 'DISCONNECTING'
},
UPGRADING: { // Attempting to upgrade the connection with muxers
done: 'MUXED'
},
MUXED: {
disconnect: 'DISCONNECTING'
},
DISCONNECTING: { // Shutting down the connection
done: 'DISCONNECTED'
}
})
this._state.on('DISCONNECTED', () => this._onDisconnected())
this._state.on('PRIVATIZING', () => this._onPrivatizing())
this._state.on('PRIVATIZED', () => this._onPrivatized())
this._state.on('ENCRYPTING', () => this._onEncrypting())
this._state.on('ENCRYPTED', () => {
this.log('successfully encrypted connection to %s', this.theirB58Id || 'unknown peer')
this.emit('encrypted', this.conn)
})
this._state.on('UPGRADING', () => this._onUpgrading())
this._state.on('MUXED', () => {
this.log('successfully muxed connection to %s', this.theirB58Id || 'unknown peer')
this.emit('muxed', this.conn)
})
this._state.on('DISCONNECTING', () => {
this._state('done')
})
}
/**
* Attempts to encrypt `this.conn` with the Switch's crypto.
*
* @private
* @fires IncomingConnectionFSM#error
* @returns {void}
*/
_onEncrypting () {
this.log('encrypting connection via %s', this.switch.crypto.tag)
this.msListener.addHandler(this.switch.crypto.tag, (protocol, _conn) => {
this.conn = this.switch.crypto.encrypt(this.ourPeerInfo.id, _conn, undefined, (err) => {
if (err) {
return this.close(err)
}
this.conn.getPeerInfo((_, peerInfo) => {
this.theirPeerInfo = peerInfo
this._state('done')
})
})
}, null)
// Start handling the connection
this.msListener.handle(this.conn, (err) => {
if (err) {
this.emit('crypto handshaking failed', err)
}
})
}
_onUpgrading () {
this.log('adding the protocol muxer to the connection')
this.protocolMuxer(this.conn, this.msListener)
this._state('done')
}
}
module.exports = withIs(IncomingConnectionFSM, {
className: 'IncomingConnectionFSM',
symbolName: 'libp2p-switch/IncomingConnectionFSM'
})

View File

@ -0,0 +1,498 @@
'use strict'
const FSM = require('fsm-event')
const Circuit = require('../../circuit')
const multistream = require('multistream-select')
const withIs = require('class-is')
const BaseConnection = require('./base')
const parallel = require('async/parallel')
const nextTick = require('async/nextTick')
const identify = require('../../identify')
const errCode = require('err-code')
const { msHandle, msSelect, identifyDialer } = require('../utils')
const observeConnection = require('../observe-connection')
const {
CONNECTION_FAILED,
DIAL_SELF,
INVALID_STATE_TRANSITION,
NO_TRANSPORTS_REGISTERED,
maybeUnexpectedEnd
} = require('../errors')
/**
* @typedef {Object} ConnectionOptions
* @property {Switch} _switch Our switch instance
* @property {PeerInfo} peerInfo The PeerInfo of the peer to dial
* @property {Muxer} muxer Optional - A muxed connection
* @property {Connection} conn Optional - The base connection
* @property {string} type Optional - identify the connection as incoming or outgoing. Defaults to out.
*/
/**
* ConnectionFSM handles the complex logic of managing a connection
* between peers. ConnectionFSM is internally composed of a state machine
* to help improve the usability and debuggability of connections. The
* state machine also helps to improve the ability to handle dial backoff,
* coalescing dials and dial locks.
*/
class ConnectionFSM extends BaseConnection {
/**
* @param {ConnectionOptions} connectionOptions
* @constructor
*/
constructor ({ _switch, peerInfo, muxer, conn, type = 'out' }) {
super({
_switch,
name: `${type}:${_switch._peerInfo.id.toB58String().slice(0, 8)}`
})
this.theirPeerInfo = peerInfo
this.theirB58Id = this.theirPeerInfo.id.toB58String()
this.conn = conn // The base connection
this.muxer = muxer // The upgraded/muxed connection
let startState = 'DISCONNECTED'
if (this.muxer) {
startState = 'MUXED'
}
this._state = FSM(startState, {
DISCONNECTED: { // No active connections exist for the peer
dial: 'DIALING',
disconnect: 'DISCONNECTED',
done: 'DISCONNECTED'
},
DIALING: { // Creating an initial connection
abort: 'ABORTED',
// emit events for different transport dials?
done: 'DIALED',
error: 'ERRORED',
disconnect: 'DISCONNECTING'
},
DIALED: { // Base connection to peer established
encrypt: 'ENCRYPTING',
privatize: 'PRIVATIZING'
},
PRIVATIZING: { // Protecting the base connection
done: 'PRIVATIZED',
abort: 'ABORTED',
disconnect: 'DISCONNECTING'
},
PRIVATIZED: { // Base connection is protected
encrypt: 'ENCRYPTING'
},
ENCRYPTING: { // Encrypting the base connection
done: 'ENCRYPTED',
error: 'ERRORED',
disconnect: 'DISCONNECTING'
},
ENCRYPTED: { // Upgrading could not happen, the connection is encrypted and waiting
upgrade: 'UPGRADING',
disconnect: 'DISCONNECTING'
},
UPGRADING: { // Attempting to upgrade the connection with muxers
stop: 'CONNECTED', // If we cannot mux, stop upgrading
done: 'MUXED',
error: 'ERRORED',
disconnect: 'DISCONNECTING'
},
MUXED: {
disconnect: 'DISCONNECTING'
},
CONNECTED: { // A non muxed connection is established
disconnect: 'DISCONNECTING'
},
DISCONNECTING: { // Shutting down the connection
done: 'DISCONNECTED',
disconnect: 'DISCONNECTING'
},
ABORTED: { }, // A severe event occurred
ERRORED: { // An error occurred, but future dials may be allowed
disconnect: 'DISCONNECTING' // There could be multiple options here, but this is a likely action
}
})
this._state.on('DISCONNECTED', () => this._onDisconnected())
this._state.on('DIALING', () => this._onDialing())
this._state.on('DIALED', () => this._onDialed())
this._state.on('PRIVATIZING', () => this._onPrivatizing())
this._state.on('PRIVATIZED', () => this._onPrivatized())
this._state.on('ENCRYPTING', () => this._onEncrypting())
this._state.on('ENCRYPTED', () => {
this.log('successfully encrypted connection to %s', this.theirB58Id)
this.emit('encrypted', this.conn)
})
this._state.on('UPGRADING', () => this._onUpgrading())
this._state.on('MUXED', () => {
this.log('successfully muxed connection to %s', this.theirB58Id)
delete this.switch.conns[this.theirB58Id]
this.emit('muxed', this.muxer)
})
this._state.on('CONNECTED', () => {
this.log('unmuxed connection opened to %s', this.theirB58Id)
this.emit('unmuxed', this.conn)
})
this._state.on('DISCONNECTING', () => this._onDisconnecting())
this._state.on('ABORTED', () => this._onAborted())
this._state.on('ERRORED', () => this._onErrored())
this._state.on('error', (err) => this._onStateError(err))
}
/**
* Puts the state into dialing mode
*
* @fires ConnectionFSM#Error May emit a DIAL_SELF error
* @returns {void}
*/
dial () {
if (this.theirB58Id === this.ourPeerInfo.id.toB58String()) {
return this.emit('error', DIAL_SELF())
} else if (this.getState() === 'DIALING') {
return this.log('attempted to dial while already dialing, ignoring')
}
this._state('dial')
}
/**
* Initiates a handshake for the given protocol
*
* @param {string} protocol The protocol to negotiate
* @param {function(Error, Connection)} callback
* @returns {void}
*/
shake (protocol, callback) {
// If there is no protocol set yet, don't perform the handshake
if (!protocol) {
return callback(null, null)
}
if (this.muxer && this.muxer.newStream) {
return this.muxer.newStream((err, stream) => {
if (err) {
return callback(err, null)
}
this.log('created new stream to %s', this.theirB58Id)
this._protocolHandshake(protocol, stream, callback)
})
}
this._protocolHandshake(protocol, this.conn, callback)
}
/**
* Puts the state into muxing mode
*
* @returns {void}
*/
upgrade () {
this._state('upgrade')
}
/**
* Event handler for dialing. Transitions state when successful.
*
* @private
* @fires ConnectionFSM#error
* @returns {void}
*/
_onDialing () {
this.log('dialing %s', this.theirB58Id)
if (!this.switch.hasTransports()) {
return this.close(NO_TRANSPORTS_REGISTERED())
}
const tKeys = this.switch.availableTransports(this.theirPeerInfo)
const circuitEnabled = Boolean(this.switch.transports[Circuit.tag])
if (circuitEnabled && !tKeys.includes(Circuit.tag)) {
tKeys.push(Circuit.tag)
}
const nextTransport = (key) => {
const transport = key
if (!transport) {
if (!circuitEnabled) {
return this.close(
CONNECTION_FAILED(`Circuit not enabled and all transports failed to dial peer ${this.theirB58Id}!`)
)
}
return this.close(
CONNECTION_FAILED(`No available transports to dial peer ${this.theirB58Id}!`)
)
}
if (transport === Circuit.tag) {
this.theirPeerInfo.multiaddrs.add(`/p2p-circuit/p2p/${this.theirB58Id}`)
}
this.log('dialing transport %s', transport)
this.switch.transport.dial(transport, this.theirPeerInfo, (errors, _conn) => {
if (errors) {
this.emit('error:connection_attempt_failed', errors)
this.log(errors)
return nextTransport(tKeys.shift())
}
this.conn = observeConnection(transport, null, _conn, this.switch.observer)
this._state('done')
})
}
nextTransport(tKeys.shift())
}
/**
* Once a connection has been successfully dialed, the connection
* will be privatized or encrypted depending on the presence of the
* Switch.protector.
*
* @returns {void}
*/
_onDialed () {
this.log('successfully dialed %s', this.theirB58Id)
this.emit('connected', this.conn)
}
/**
* Event handler for disconnecting. Handles any needed cleanup
*
* @returns {void}
*/
_onDisconnecting () {
this.log('disconnecting from %s', this.theirB58Id, Boolean(this.muxer))
delete this.switch.conns[this.theirB58Id]
const tasks = []
// Clean up stored connections
if (this.muxer) {
tasks.push((cb) => {
this.muxer.end(() => {
delete this.muxer
cb()
})
})
}
// If we have the base connection, abort it
// Ignore abort errors, since we're closing
if (this.conn) {
try {
this.conn.source.abort()
} catch (_) { }
delete this.conn
}
parallel(tasks, () => {
this._state('done')
})
}
/**
* Attempts to encrypt `this.conn` with the Switch's crypto.
*
* @private
* @fires ConnectionFSM#error
* @returns {void}
*/
_onEncrypting () {
const msDialer = new multistream.Dialer()
msDialer.handle(this.conn, (err) => {
if (err) {
return this.close(maybeUnexpectedEnd(err))
}
this.log('selecting crypto %s to %s', this.switch.crypto.tag, this.theirB58Id)
msDialer.select(this.switch.crypto.tag, (err, _conn) => {
if (err) {
return this.close(maybeUnexpectedEnd(err))
}
const observedConn = observeConnection(null, this.switch.crypto.tag, _conn, this.switch.observer)
const encryptedConn = this.switch.crypto.encrypt(this.ourPeerInfo.id, observedConn, this.theirPeerInfo.id, (err) => {
if (err) {
return this.close(err)
}
this.conn = encryptedConn
this.conn.setPeerInfo(this.theirPeerInfo)
this._state('done')
})
})
})
}
/**
* Iterates over each Muxer on the Switch and attempts to upgrade
* the given `connection`. Successful muxed connections will be stored
* on the Switch.muxedConns with `b58Id` as their key for future reference.
*
* @private
* @returns {void}
*/
_onUpgrading () {
const muxers = Object.keys(this.switch.muxers)
this.log('upgrading connection to %s', this.theirB58Id)
if (muxers.length === 0) {
return this._state('stop')
}
const msDialer = new multistream.Dialer()
msDialer.handle(this.conn, (err) => {
if (err) {
return this._didUpgrade(err)
}
// 1. try to handshake in one of the muxers available
// 2. if succeeds
// - add the muxedConn to the list of muxedConns
// - add incomming new streams to connHandler
const nextMuxer = (key) => {
this.log('selecting %s', key)
msDialer.select(key, (err, _conn) => {
if (err) {
if (muxers.length === 0) {
return this._didUpgrade(err)
}
return nextMuxer(muxers.shift())
}
// observe muxed connections
const conn = observeConnection(null, key, _conn, this.switch.observer)
this.muxer = this.switch.muxers[key].dialer(conn)
this.muxer.once('close', () => {
this.close()
})
// For incoming streams, in case identify is on
this.muxer.on('stream', (conn) => {
this.log('new stream created via muxer to %s', this.theirB58Id)
conn.setPeerInfo(this.theirPeerInfo)
this.switch.protocolMuxer(null)(conn)
})
this._didUpgrade(null)
// Run identify on the connection
if (this.switch.identify) {
this._identify((err, results) => {
if (err) {
return this.close(err)
}
this.theirPeerInfo = this.switch._peerBook.put(results.peerInfo)
})
}
})
}
nextMuxer(muxers.shift())
})
}
/**
* Runs the identify protocol on the connection
* @private
* @param {function(error, { PeerInfo })} callback
* @returns {void}
*/
_identify (callback) {
if (!this.muxer) {
return nextTick(callback, errCode('The connection was already closed', 'ERR_CONNECTION_CLOSED'))
}
this.muxer.newStream(async (err, conn) => {
if (err) return callback(err)
const ms = new multistream.Dialer()
let results
try {
await msHandle(ms, conn)
const msConn = await msSelect(ms, identify.multicodec)
results = await identifyDialer(msConn, this.theirPeerInfo)
} catch (err) {
return callback(err)
}
callback(null, results)
})
}
/**
* Analyses the given error, if it exists, to determine where the state machine
* needs to go.
*
* @param {Error} err
* @returns {void}
*/
_didUpgrade (err) {
if (err) {
this.log('Error upgrading connection:', err)
this.switch.conns[this.theirB58Id] = this
this.emit('error:upgrade_failed', err)
// Cant upgrade, hold the encrypted connection
return this._state('stop')
}
// move the state machine forward
this._state('done')
}
/**
* Performs the protocol handshake for the given protocol
* over the given connection. The resulting error or connection
* will be returned via the callback.
*
* @private
* @param {string} protocol
* @param {Connection} connection
* @param {function(Error, Connection)} callback
* @returns {void}
*/
_protocolHandshake (protocol, connection, callback) {
const msDialer = new multistream.Dialer()
msDialer.handle(connection, (err) => {
if (err) {
return callback(err, null)
}
msDialer.select(protocol, (err, _conn) => {
if (err) {
this.log('could not perform protocol handshake:', err)
return callback(err, null)
}
const conn = observeConnection(null, protocol, _conn, this.switch.observer)
this.log('successfully performed handshake of %s to %s', protocol, this.theirB58Id)
this.emit('connection', conn)
callback(null, conn)
})
})
}
/**
* Event handler for state transition errors
*
* @param {Error} err
* @returns {void}
*/
_onStateError (err) {
this.emit('error', INVALID_STATE_TRANSITION(err))
this.log(err)
}
}
module.exports = withIs(ConnectionFSM, {
className: 'ConnectionFSM',
symbolName: 'libp2p-switch/ConnectionFSM'
})

View File

@ -0,0 +1,289 @@
'use strict'
const identify = require('../../identify')
const multistream = require('multistream-select')
const debug = require('debug')
const log = debug('libp2p:switch:conn-manager')
const once = require('once')
const ConnectionFSM = require('../connection')
const { msHandle, msSelect, identifyDialer } = require('../utils')
const Circuit = require('../../circuit')
const plaintext = require('../plaintext')
/**
* Contains methods for binding handlers to the Switch
* in order to better manage its connections.
*/
class ConnectionManager {
constructor (_switch) {
this.switch = _switch
this.connections = {}
}
/**
* Adds the connection for tracking if it's not already added
* @private
* @param {ConnectionFSM} connection
* @returns {void}
*/
add (connection) {
this.connections[connection.theirB58Id] = this.connections[connection.theirB58Id] || []
// Only add it if it's not there
if (!this.get(connection)) {
this.connections[connection.theirB58Id].push(connection)
this.switch.emit('connection:start', connection.theirPeerInfo)
if (connection.getState() === 'MUXED') {
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
// Clear the denylist of the peer
this.switch.dialer.clearDenylist(connection.theirPeerInfo)
} else {
connection.once('muxed', () => {
this.switch.emit('peer-mux-established', connection.theirPeerInfo)
// Clear the denylist of the peer
this.switch.dialer.clearDenylist(connection.theirPeerInfo)
})
}
}
}
/**
* Gets the connection from the list if it exists
* @private
* @param {ConnectionFSM} connection
* @returns {ConnectionFSM|null} The found connection or null
*/
get (connection) {
if (!this.connections[connection.theirB58Id]) return null
for (let i = 0; i < this.connections[connection.theirB58Id].length; i++) {
if (this.connections[connection.theirB58Id][i] === connection) {
return this.connections[connection.theirB58Id][i]
}
}
return null
}
/**
* Gets a connection associated with the given peer
* @private
* @param {string} peerId The peers id
* @returns {ConnectionFSM|null} The found connection or null
*/
getOne (peerId) {
if (this.connections[peerId]) {
// Only return muxed connections
for (var i = 0; i < this.connections[peerId].length; i++) {
if (this.connections[peerId][i].getState() === 'MUXED') {
return this.connections[peerId][i]
}
}
}
return null
}
/**
* Removes the connection from tracking
* @private
* @param {ConnectionFSM} connection The connection to remove
* @returns {void}
*/
remove (connection) {
// No record of the peer, disconnect it
if (!this.connections[connection.theirB58Id]) {
if (connection.theirPeerInfo) {
connection.theirPeerInfo.disconnect()
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
}
return
}
for (let i = 0; i < this.connections[connection.theirB58Id].length; i++) {
if (this.connections[connection.theirB58Id][i] === connection) {
this.connections[connection.theirB58Id].splice(i, 1)
break
}
}
// The peer is fully disconnected
if (this.connections[connection.theirB58Id].length === 0) {
delete this.connections[connection.theirB58Id]
connection.theirPeerInfo.disconnect()
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
}
// A tracked connection was closed, let the world know
this.switch.emit('connection:end', connection.theirPeerInfo)
}
/**
* Returns all connections being tracked
* @private
* @returns {ConnectionFSM[]}
*/
getAll () {
let connections = []
for (const conns of Object.values(this.connections)) {
connections = [...connections, ...conns]
}
return connections
}
/**
* Returns all connections being tracked for a given peer id
* @private
* @param {string} peerId Stringified peer id
* @returns {ConnectionFSM[]}
*/
getAllById (peerId) {
return this.connections[peerId] || []
}
/**
* Adds a listener for the given `muxer` and creates a handler for it
* leveraging the Switch.protocolMuxer handler factory
*
* @param {Muxer} muxer
* @returns {void}
*/
addStreamMuxer (muxer) {
// for dialing
this.switch.muxers[muxer.multicodec] = muxer
// for listening
this.switch.handle(muxer.multicodec, (protocol, conn) => {
const muxedConn = muxer.listener(conn)
muxedConn.on('stream', this.switch.protocolMuxer(null))
// If identify is enabled
// 1. overload getPeerInfo
// 2. call getPeerInfo
// 3. add this conn to the pool
if (this.switch.identify) {
// Get the peer info from the crypto exchange
conn.getPeerInfo((err, cryptoPI) => {
if (err || !cryptoPI) {
log('crypto peerInfo wasnt found')
}
// overload peerInfo to use Identify instead
conn.getPeerInfo = async (callback) => {
const conn = muxedConn.newStream()
const ms = new multistream.Dialer()
callback = once(callback)
let results
try {
await msHandle(ms, conn)
const msConn = await msSelect(ms, identify.multicodec)
results = await identifyDialer(msConn, cryptoPI)
} catch (err) {
return muxedConn.end(() => {
callback(err, null)
})
}
const { peerInfo } = results
if (peerInfo) {
conn.setPeerInfo(peerInfo)
}
callback(null, peerInfo)
}
conn.getPeerInfo((err, peerInfo) => {
/* eslint no-warning-comments: off */
if (err) {
return log('identify not successful')
}
const b58Str = peerInfo.id.toB58String()
peerInfo = this.switch._peerBook.put(peerInfo)
const connection = new ConnectionFSM({
_switch: this.switch,
peerInfo,
muxer: muxedConn,
conn: conn,
type: 'inc'
})
this.switch.connection.add(connection)
// Only update if it's not already connected
if (!peerInfo.isConnected()) {
if (peerInfo.multiaddrs.size > 0) {
// with incomming conn and through identify, going to pick one
// of the available multiaddrs from the other peer as the one
// I'm connected to as we really can't be sure at the moment
// TODO add this consideration to the connection abstraction!
peerInfo.connect(peerInfo.multiaddrs.toArray()[0])
} else {
// for the case of websockets in the browser, where peers have
// no addr, use just their IPFS id
peerInfo.connect(`/ipfs/${b58Str}`)
}
}
muxedConn.once('close', () => {
connection.close()
})
})
})
}
return conn
})
}
/**
* Adds the `encrypt` handler for the given `tag` and also sets the
* Switch's crypto to passed `encrypt` function
*
* @param {String} tag
* @param {function(PeerID, Connection, PeerId, Callback)} encrypt
* @returns {void}
*/
crypto (tag, encrypt) {
if (!tag && !encrypt) {
tag = plaintext.tag
encrypt = plaintext.encrypt
}
this.switch.crypto = { tag, encrypt }
}
/**
* If config.enabled is true, a Circuit relay will be added to the
* available Switch transports.
*
* @param {any} config
* @returns {void}
*/
enableCircuitRelay (config) {
config = config || {}
if (config.enabled) {
if (!config.hop) {
Object.assign(config, { hop: { enabled: false, active: false } })
}
this.switch.transport.add(Circuit.tag, new Circuit(this.switch, config))
}
}
/**
* Sets identify to true on the Switch and performs handshakes
* for libp2p-identify leveraging the Switch's muxer.
*
* @returns {void}
*/
reuse () {
this.switch.identify = true
this.switch.handle(identify.multicodec, (protocol, conn) => {
identify.listener(conn, this.switch._peerInfo)
})
}
}
module.exports = ConnectionManager

12
src/switch/constants.js Normal file
View File

@ -0,0 +1,12 @@
'use strict'
module.exports = {
DENY_TTL: 5 * 60 * 1e3, // How long before an errored peer can be dialed again
DENY_ATTEMPTS: 5, // Num of unsuccessful dials before a peer is permanently denied
DIAL_TIMEOUT: 30e3, // How long in ms a dial attempt is allowed to take
MAX_COLD_CALLS: 50, // How many dials w/o protocols that can be queued
MAX_PARALLEL_DIALS: 100, // Maximum allowed concurrent dials
QUARTER_HOUR: 15 * 60e3,
PRIORITY_HIGH: 10,
PRIORITY_LOW: 20
}

119
src/switch/dialer/index.js Normal file
View File

@ -0,0 +1,119 @@
'use strict'
const DialQueueManager = require('./queueManager')
const { getPeerInfo } = require('../../get-peer-info')
const {
DENY_ATTEMPTS,
DENY_TTL,
MAX_COLD_CALLS,
MAX_PARALLEL_DIALS,
PRIORITY_HIGH,
PRIORITY_LOW
} = require('../constants')
module.exports = function (_switch) {
const dialQueueManager = new DialQueueManager(_switch)
_switch.state.on('STARTED:enter', start)
_switch.state.on('STOPPING:enter', stop)
/**
* @param {DialRequest} dialRequest
* @returns {void}
*/
function _dial ({ peerInfo, protocol, options, callback }) {
if (typeof protocol === 'function') {
callback = protocol
protocol = null
}
try {
peerInfo = getPeerInfo(peerInfo, _switch._peerBook)
} catch (err) {
return callback(err)
}
// Add it to the queue, it will automatically get executed
dialQueueManager.add({ peerInfo, protocol, options, callback })
}
/**
* Starts the `DialQueueManager`
*
* @param {function} callback
*/
function start (callback) {
dialQueueManager.start()
callback()
}
/**
* Aborts all dials that are queued. This should
* only be used when the Switch is being stopped
*
* @param {function} callback
*/
function stop (callback) {
dialQueueManager.stop()
callback()
}
/**
* Clears the denylist for a given peer
* @param {PeerInfo} peerInfo
*/
function clearDenylist (peerInfo) {
dialQueueManager.clearDenylist(peerInfo)
}
/**
* Attempts to establish a connection to the given `peerInfo` at
* a lower priority than a standard dial.
* @param {PeerInfo} peerInfo
* @param {object} options
* @param {boolean} options.useFSM Whether or not to return a `ConnectionFSM`. Defaults to false.
* @param {number} options.priority Lowest priority goes first. Defaults to 20.
* @param {function(Error, Connection)} callback
*/
function connect (peerInfo, options, callback) {
if (typeof options === 'function') {
callback = options
options = null
}
options = { useFSM: false, priority: PRIORITY_LOW, ...options }
_dial({ peerInfo, protocol: null, options, callback })
}
/**
* Adds the dial request to the queue for the given `peerInfo`
* The request will be added with a high priority (10).
* @param {PeerInfo} peerInfo
* @param {string} protocol
* @param {function(Error, Connection)} callback
*/
function dial (peerInfo, protocol, callback) {
_dial({ peerInfo, protocol, options: { useFSM: false, priority: PRIORITY_HIGH }, callback })
}
/**
* Behaves like dial, except it calls back with a ConnectionFSM
*
* @param {PeerInfo} peerInfo
* @param {string} protocol
* @param {function(Error, ConnectionFSM)} callback
*/
function dialFSM (peerInfo, protocol, callback) {
_dial({ peerInfo, protocol, options: { useFSM: true, priority: PRIORITY_HIGH }, callback })
}
return {
connect,
dial,
dialFSM,
clearDenylist,
DENY_ATTEMPTS: isNaN(_switch._options.denyAttempts) ? DENY_ATTEMPTS : _switch._options.denyAttempts,
DENY_TTL: isNaN(_switch._options.denyTTL) ? DENY_TTL : _switch._options.denyTTL,
MAX_COLD_CALLS: isNaN(_switch._options.maxColdCalls) ? MAX_COLD_CALLS : _switch._options.maxColdCalls,
MAX_PARALLEL_DIALS: isNaN(_switch._options.maxParallelDials) ? MAX_PARALLEL_DIALS : _switch._options.maxParallelDials
}
}

281
src/switch/dialer/queue.js Normal file
View File

@ -0,0 +1,281 @@
'use strict'
const ConnectionFSM = require('../connection')
const { DIAL_ABORTED, ERR_DENIED } = require('../errors')
const nextTick = require('async/nextTick')
const once = require('once')
const debug = require('debug')
const log = debug('libp2p:switch:dial')
log.error = debug('libp2p:switch:dial:error')
/**
* Components required to execute a dial
* @typedef {Object} DialRequest
* @property {PeerInfo} peerInfo - The peer to dial to
* @property {string} [protocol] - The protocol to create a stream for
* @property {object} options
* @property {boolean} options.useFSM - If `callback` should return a ConnectionFSM
* @property {number} options.priority - The priority of the dial
* @property {function(Error, Connection|ConnectionFSM)} callback
*/
/**
* @typedef {Object} NewConnection
* @property {ConnectionFSM} connectionFSM
* @property {boolean} didCreate
*/
/**
* Attempts to create a new connection or stream (when muxed),
* via negotiation of the given `protocol`. If no `protocol` is
* provided, no action will be taken and `callback` will be called
* immediately with no error or values.
*
* @param {object} options
* @param {string} options.protocol
* @param {ConnectionFSM} options.connection
* @param {function(Error, Connection)} options.callback
* @returns {void}
*/
function createConnectionWithProtocol ({ protocol, connection, callback }) {
if (!protocol) {
return callback()
}
connection.shake(protocol, (err, conn) => {
if (!conn) {
return callback(err)
}
conn.setPeerInfo(connection.theirPeerInfo)
callback(null, conn)
})
}
/**
* A convenience array wrapper for controlling
* a per peer queue
*
* @returns {Queue}
*/
class Queue {
/**
* @constructor
* @param {string} peerId
* @param {Switch} _switch
* @param {function(string)} onStopped Called when the queue stops
*/
constructor (peerId, _switch, onStopped) {
this.id = peerId
this.switch = _switch
this._queue = []
this.denylisted = null
this.denylistCount = 0
this.isRunning = false
this.onStopped = onStopped
}
get length () {
return this._queue.length
}
/**
* Adds the dial request to the queue. The queue is not automatically started
* @param {string} protocol
* @param {boolean} useFSM If callback should use a ConnectionFSM instead
* @param {function(Error, Connection)} callback
* @returns {void}
*/
add (protocol, useFSM, callback) {
if (!this.isDialAllowed()) {
return nextTick(callback, ERR_DENIED())
}
this._queue.push({ protocol, useFSM, callback })
}
/**
* Determines whether or not dialing is currently allowed
* @returns {boolean}
*/
isDialAllowed () {
if (this.denylisted) {
// If the deny ttl has passed, reset it
if (Date.now() > this.denylisted) {
this.denylisted = null
return true
}
// Dial is not allowed
return false
}
return true
}
/**
* Starts the queue. If the queue was started `true` will be returned.
* If the queue was already running `false` is returned.
* @returns {boolean}
*/
start () {
if (!this.isRunning) {
log('starting dial queue to %s', this.id)
this.isRunning = true
this._run()
return true
}
return false
}
/**
* Stops the queue
*/
stop () {
if (this.isRunning) {
log('stopping dial queue to %s', this.id)
this.isRunning = false
this.onStopped(this.id)
}
}
/**
* Stops the queue and errors the callback for each dial request
*/
abort () {
while (this.length > 0) {
const dial = this._queue.shift()
dial.callback(DIAL_ABORTED())
}
this.stop()
}
/**
* Marks the queue as denylisted. The queue will be immediately aborted.
* @returns {void}
*/
denylist () {
this.denylistCount++
if (this.denylistCount >= this.switch.dialer.DENY_ATTEMPTS) {
this.denylisted = Infinity
return
}
let ttl = this.switch.dialer.DENY_TTL * Math.pow(this.denylistCount, 3)
const minTTL = ttl * 0.9
const maxTTL = ttl * 1.1
// Add a random jitter of 20% to the ttl
ttl = Math.floor(Math.random() * (maxTTL - minTTL) + minTTL)
this.denylisted = Date.now() + ttl
this.abort()
}
/**
* Attempts to find a muxed connection for the given peer. If one
* isn't found, a new one will be created.
*
* Returns an array containing two items. The ConnectionFSM and wether
* or not the ConnectionFSM was just created. The latter can be used
* to determine dialing needs.
*
* @private
* @param {PeerInfo} peerInfo
* @returns {NewConnection}
*/
_getOrCreateConnection (peerInfo) {
let connectionFSM = this.switch.connection.getOne(this.id)
let didCreate = false
if (!connectionFSM) {
connectionFSM = new ConnectionFSM({
_switch: this.switch,
peerInfo,
muxer: null,
conn: null
})
this.switch.connection.add(connectionFSM)
// Add control events and start the dialer
connectionFSM.once('connected', () => connectionFSM.protect())
connectionFSM.once('private', () => connectionFSM.encrypt())
connectionFSM.once('encrypted', () => connectionFSM.upgrade())
didCreate = true
}
return { connectionFSM, didCreate }
}
/**
* Executes the next dial in the queue for the given peer
* @private
* @returns {void}
*/
_run () {
// If we have no items in the queue or we're stopped, exit
if (this.length < 1 || !this.isRunning) {
log('stopping the queue for %s', this.id)
return this.stop()
}
const next = once(() => {
log('starting next dial to %s', this.id)
this._run()
})
const peerInfo = this.switch._peerBook.get(this.id)
const queuedDial = this._queue.shift()
const { connectionFSM, didCreate } = this._getOrCreateConnection(peerInfo)
// If the dial expects a ConnectionFSM, we can provide that back now
if (queuedDial.useFSM) {
nextTick(queuedDial.callback, null, connectionFSM)
}
// If we can handshake protocols, get a new stream and call run again
if (['MUXED', 'CONNECTED'].includes(connectionFSM.getState())) {
queuedDial.connection = connectionFSM
createConnectionWithProtocol(queuedDial)
next()
return
}
// If we error, error the queued dial
// In the future, it may be desired to error the other queued dials,
// depending on the error.
connectionFSM.once('error', (err) => {
queuedDial.callback(err)
// Dont denylist peers we have identified and that we are connected to
if (peerInfo.protocols.size > 0 && peerInfo.isConnected()) {
return
}
this.denylist()
})
connectionFSM.once('close', () => {
next()
})
// If we're not muxed yet, add listeners
connectionFSM.once('muxed', () => {
this.denylistCount = 0 // reset denylisting on good connections
queuedDial.connection = connectionFSM
createConnectionWithProtocol(queuedDial)
next()
})
connectionFSM.once('unmuxed', () => {
this.denylistCount = 0
queuedDial.connection = connectionFSM
createConnectionWithProtocol(queuedDial)
next()
})
// If we have a new connection, start dialing
if (didCreate) {
connectionFSM.dial()
}
}
}
module.exports = Queue

View File

@ -0,0 +1,220 @@
'use strict'
const once = require('once')
const Queue = require('./queue')
const { DIAL_ABORTED } = require('../errors')
const nextTick = require('async/nextTick')
const retimer = require('retimer')
const { QUARTER_HOUR, PRIORITY_HIGH } = require('../constants')
const debug = require('debug')
const log = debug('libp2p:switch:dial:manager')
const noop = () => {}
class DialQueueManager {
/**
* @constructor
* @param {Switch} _switch
*/
constructor (_switch) {
this._queue = new Set()
this._coldCallQueue = new Set()
this._dialingQueues = new Set()
this._queues = {}
this.switch = _switch
this._cleanInterval = retimer(this._clean.bind(this), QUARTER_HOUR)
this.start()
}
/**
* Runs through all queues, aborts and removes them if they
* are no longer valid. A queue that is denylisted indefinitely,
* is considered no longer valid.
* @private
*/
_clean () {
const queues = Object.values(this._queues)
queues.forEach(dialQueue => {
// Clear if the queue has reached max denylist
if (dialQueue.denylisted === Infinity) {
dialQueue.abort()
delete this._queues[dialQueue.id]
return
}
// Keep track of denylisted queues
if (dialQueue.denylisted) return
// Clear if peer is no longer active
// To avoid reallocating memory, dont delete queues of
// connected peers, as these are highly likely to leverage the
// queues in the immediate term
if (!dialQueue.isRunning && dialQueue.length < 1) {
let isConnected = false
try {
const peerInfo = this.switch._peerBook.get(dialQueue.id)
isConnected = Boolean(peerInfo.isConnected())
} catch (_) {
// If we get an error, that means the peerbook doesnt have the peer
}
if (!isConnected) {
dialQueue.abort()
delete this._queues[dialQueue.id]
}
}
})
this._cleanInterval.reschedule(QUARTER_HOUR)
}
/**
* Allows the `DialQueueManager` to execute dials
*/
start () {
this.isRunning = true
}
/**
* Iterates over all items in the DialerQueue
* and executes there callback with an error.
*
* This causes the entire DialerQueue to be drained
*/
stop () {
this.isRunning = false
// Clear the general queue
this._queue.clear()
// Clear the cold call queue
this._coldCallQueue.clear()
this._cleanInterval.clear()
// Abort the individual peer queues
const queues = Object.values(this._queues)
queues.forEach(dialQueue => {
dialQueue.abort()
delete this._queues[dialQueue.id]
})
}
/**
* Adds the `dialRequest` to the queue and ensures queue is running
*
* @param {DialRequest} dialRequest
* @returns {void}
*/
add ({ peerInfo, protocol, options, callback }) {
callback = callback ? once(callback) : noop
// Add the dial to its respective queue
const targetQueue = this.getQueue(peerInfo)
// Cold Call
if (options.priority > PRIORITY_HIGH) {
// If we have too many cold calls, abort the dial immediately
if (this._coldCallQueue.size >= this.switch.dialer.MAX_COLD_CALLS) {
return nextTick(callback, DIAL_ABORTED())
}
if (this._queue.has(targetQueue.id)) {
return nextTick(callback, DIAL_ABORTED())
}
}
targetQueue.add(protocol, options.useFSM, callback)
// If we're already connected to the peer, start the queue now
// While it might cause queues to go over the max parallel amount,
// it avoids denying peers we're already connected to
if (peerInfo.isConnected()) {
targetQueue.start()
return
}
// If dialing is not allowed, abort
if (!targetQueue.isDialAllowed()) {
return
}
// Add the id to its respective queue set if the queue isn't running
if (!targetQueue.isRunning) {
if (options.priority <= PRIORITY_HIGH) {
this._queue.add(targetQueue.id)
this._coldCallQueue.delete(targetQueue.id)
// Only add it to the cold queue if it's not in the normal queue
} else {
this._coldCallQueue.add(targetQueue.id)
}
}
this.run()
}
/**
* Will execute up to `MAX_PARALLEL_DIALS` dials
*/
run () {
if (!this.isRunning) return
if (this._dialingQueues.size < this.switch.dialer.MAX_PARALLEL_DIALS) {
let nextQueue = { done: true }
// Check the queue first and fall back to the cold call queue
if (this._queue.size > 0) {
nextQueue = this._queue.values().next()
this._queue.delete(nextQueue.value)
} else if (this._coldCallQueue.size > 0) {
nextQueue = this._coldCallQueue.values().next()
this._coldCallQueue.delete(nextQueue.value)
}
if (nextQueue.done) {
return
}
const targetQueue = this._queues[nextQueue.value]
if (!targetQueue) {
log('missing queue %s, maybe it was aborted?', nextQueue.value)
return
}
this._dialingQueues.add(targetQueue.id)
targetQueue.start()
}
}
/**
* Will remove the `peerInfo` from the dial denylist
* @param {PeerInfo} peerInfo
*/
clearDenylist (peerInfo) {
const queue = this.getQueue(peerInfo)
queue.denylisted = null
queue.denylistCount = 0
}
/**
* A handler for when dialing queues stop. This will trigger
* `run()` in order to keep the queue processing.
* @private
* @param {string} id peer id of the queue that stopped
*/
_onQueueStopped (id) {
this._dialingQueues.delete(id)
this.run()
}
/**
* Returns the `Queue` for the given `peerInfo`
* @param {PeerInfo} peerInfo
* @returns {Queue}
*/
getQueue (peerInfo) {
const id = peerInfo.id.toB58String()
this._queues[id] = this._queues[id] || new Queue(id, this.switch, this._onQueueStopped.bind(this))
return this._queues[id]
}
}
module.exports = DialQueueManager

20
src/switch/errors.js Normal file
View File

@ -0,0 +1,20 @@
'use strict'
const errCode = require('err-code')
module.exports = {
CONNECTION_FAILED: (err) => errCode(err, 'CONNECTION_FAILED'),
DIAL_ABORTED: () => errCode('Dial was aborted', 'DIAL_ABORTED'),
ERR_DENIED: () => errCode('Dial is currently denied for this peer', 'ERR_DENIED'),
DIAL_SELF: () => errCode('A node cannot dial itself', 'DIAL_SELF'),
INVALID_STATE_TRANSITION: (err) => errCode(err, 'INVALID_STATE_TRANSITION'),
NO_TRANSPORTS_REGISTERED: () => errCode('No transports registered, dial not possible', 'NO_TRANSPORTS_REGISTERED'),
PROTECTOR_REQUIRED: () => errCode('No protector provided with private network enforced', 'PROTECTOR_REQUIRED'),
UNEXPECTED_END: () => errCode('Unexpected end of input from reader.', 'UNEXPECTED_END'),
maybeUnexpectedEnd: (err) => {
if (err === true) {
return module.exports.UNEXPECTED_END()
}
return err
}
}

274
src/switch/index.js Normal file
View File

@ -0,0 +1,274 @@
'use strict'
const FSM = require('fsm-event')
const EventEmitter = require('events').EventEmitter
const each = require('async/each')
const eachSeries = require('async/eachSeries')
const series = require('async/series')
const Circuit = require('../circuit')
const TransportManager = require('./transport')
const ConnectionManager = require('./connection/manager')
const { getPeerInfo } = require('../get-peer-info')
const getDialer = require('./dialer')
const connectionHandler = require('./connection/handler')
const ProtocolMuxer = require('./protocol-muxer')
const plaintext = require('./plaintext')
const Observer = require('./observer')
const Stats = require('./stats')
const assert = require('assert')
const Errors = require('./errors')
const debug = require('debug')
const log = debug('libp2p:switch')
log.error = debug('libp2p:switch:error')
/**
* @fires Switch#stop Triggered when the switch has stopped
* @fires Switch#start Triggered when the switch has started
* @fires Switch#error Triggered whenever an error occurs
*/
class Switch extends EventEmitter {
constructor (peerInfo, peerBook, options) {
super()
assert(peerInfo, 'You must provide a `peerInfo`')
assert(peerBook, 'You must provide a `peerBook`')
this._peerInfo = peerInfo
this._peerBook = peerBook
this._options = options || {}
this.setMaxListeners(Infinity)
// transports --
// { key: transport }; e.g { tcp: <tcp> }
this.transports = {}
// connections --
// { peerIdB58: { conn: <conn> }}
this.conns = {}
// { protocol: handler }
this.protocols = {}
// { muxerCodec: <muxer> } e.g { '/spdy/0.3.1': spdy }
this.muxers = {}
// is the Identify protocol enabled?
this.identify = false
// Crypto details
this.crypto = plaintext
this.protector = this._options.protector || null
this.transport = new TransportManager(this)
this.connection = new ConnectionManager(this)
this.observer = Observer(this)
this.stats = Stats(this.observer, this._options.stats)
this.protocolMuxer = ProtocolMuxer(this.protocols, this.observer)
// All purpose connection handler for managing incoming connections
this._connectionHandler = connectionHandler(this)
// Setup the internal state
this.state = new FSM('STOPPED', {
STOPPED: {
start: 'STARTING',
stop: 'STOPPING' // ensures that any transports that were manually started are stopped
},
STARTING: {
done: 'STARTED',
stop: 'STOPPING'
},
STARTED: {
stop: 'STOPPING',
start: 'STARTED'
},
STOPPING: {
stop: 'STOPPING',
done: 'STOPPED'
}
})
this.state.on('STARTING', () => {
log('The switch is starting')
this._onStarting()
})
this.state.on('STOPPING', () => {
log('The switch is stopping')
this._onStopping()
})
this.state.on('STARTED', () => {
log('The switch has started')
this.emit('start')
})
this.state.on('STOPPED', () => {
log('The switch has stopped')
this.emit('stop')
})
this.state.on('error', (err) => {
log.error(err)
this.emit('error', err)
})
// higher level (public) API
this.dialer = getDialer(this)
this.dial = this.dialer.dial
this.dialFSM = this.dialer.dialFSM
}
/**
* Returns a list of the transports peerInfo has addresses for
*
* @param {PeerInfo} peerInfo
* @returns {Array<Transport>}
*/
availableTransports (peerInfo) {
const myAddrs = peerInfo.multiaddrs.toArray()
const myTransports = Object.keys(this.transports)
// Only listen on transports we actually have addresses for
return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0)
// push Circuit to be the last proto to be dialed, and alphabetize the others
.sort((a, b) => {
if (a === Circuit.tag) return 1
if (b === Circuit.tag) return -1
return a < b ? -1 : 1
})
}
/**
* Adds the `handlerFunc` and `matchFunc` to the Switch's protocol
* handler list for the given `protocol`. If the `matchFunc` returns
* true for a protocol check, the `handlerFunc` will be called.
*
* @param {string} protocol
* @param {function(string, Connection)} handlerFunc
* @param {function(string, string, function(Error, boolean))} matchFunc
* @returns {void}
*/
handle (protocol, handlerFunc, matchFunc) {
this.protocols[protocol] = {
handlerFunc: handlerFunc,
matchFunc: matchFunc
}
this._peerInfo.protocols.add(protocol)
}
/**
* Removes the given protocol from the Switch's protocol list
*
* @param {string} protocol
* @returns {void}
*/
unhandle (protocol) {
if (this.protocols[protocol]) {
delete this.protocols[protocol]
}
this._peerInfo.protocols.delete(protocol)
}
/**
* If a muxed Connection exists for the given peer, it will be closed
* and its reference on the Switch will be removed.
*
* @param {PeerInfo|Multiaddr|PeerId} peer
* @param {function()} callback
* @returns {void}
*/
hangUp (peer, callback) {
const peerInfo = getPeerInfo(peer, this._peerBook)
const key = peerInfo.id.toB58String()
const conns = [...this.connection.getAllById(key)]
each(conns, (conn, cb) => {
conn.once('close', cb)
conn.close()
}, callback)
}
/**
* Returns whether or not the switch has any transports
*
* @returns {boolean}
*/
hasTransports () {
const transports = Object.keys(this.transports).filter((t) => t !== Circuit.tag)
return transports && transports.length > 0
}
/**
* Issues a start on the Switch state.
*
* @param {function} callback deprecated: Listening for the `error` and `start` events are recommended
* @returns {void}
*/
start (callback = () => {}) {
// Add once listener for deprecated callback support
this.once('start', callback)
this.state('start')
}
/**
* Issues a stop on the Switch state.
*
* @param {function} callback deprecated: Listening for the `error` and `stop` events are recommended
* @returns {void}
*/
stop (callback = () => {}) {
// Add once listener for deprecated callback support
this.once('stop', callback)
this.state('stop')
}
/**
* A listener that will start any necessary services and listeners
*
* @private
* @returns {void}
*/
_onStarting () {
this.stats.start()
eachSeries(this.availableTransports(this._peerInfo), (ts, cb) => {
// Listen on the given transport
this.transport.listen(ts, {}, null, cb)
}, (err) => {
if (err) {
log.error(err)
this.emit('error', err)
return this.state('stop')
}
this.state('done')
})
}
/**
* A listener that will turn off all running services and listeners
*
* @private
* @returns {void}
*/
_onStopping () {
this.stats.stop()
series([
(cb) => {
each(this.transports, (transport, cb) => {
each(transport.listeners, (listener, cb) => {
listener.close((err) => {
if (err) log.error(err)
cb()
})
}, cb)
}, cb)
},
(cb) => each(this.connection.getAll(), (conn, cb) => {
conn.once('close', cb)
conn.close()
}, cb)
], (_) => {
this.state('done')
})
}
}
module.exports = Switch
module.exports.errors = Errors

View File

@ -0,0 +1,88 @@
'use strict'
const tryEach = require('async/tryEach')
const debug = require('debug')
const log = debug('libp2p:switch:dialer')
const DialQueue = require('./queue')
/**
* Track dials per peer and limited them.
*/
class LimitDialer {
/**
* Create a new dialer.
*
* @param {number} perPeerLimit
* @param {number} dialTimeout
*/
constructor (perPeerLimit, dialTimeout) {
log('create: %s peer limit, %s dial timeout', perPeerLimit, dialTimeout)
this.perPeerLimit = perPeerLimit
this.dialTimeout = dialTimeout
this.queues = new Map()
}
/**
* Dial a list of multiaddrs on the given transport.
*
* @param {PeerId} peer
* @param {SwarmTransport} transport
* @param {Array<Multiaddr>} addrs
* @param {function(Error, Connection)} callback
* @returns {void}
*/
dialMany (peer, transport, addrs, callback) {
log('dialMany:start')
// we use a token to track if we want to cancel following dials
const token = { cancel: false }
const errors = []
const tasks = addrs.map((m) => {
return (cb) => this.dialSingle(peer, transport, m, token, (err, result) => {
if (err) {
errors.push(err)
return cb(err)
}
return cb(null, result)
})
})
tryEach(tasks, (_, result) => {
if (result && result.conn) {
log('dialMany:success')
return callback(null, result)
}
log('dialMany:error')
callback(errors)
})
}
/**
* Dial a single multiaddr on the given transport.
*
* @param {PeerId} peer
* @param {SwarmTransport} transport
* @param {Multiaddr} addr
* @param {CancelToken} token
* @param {function(Error, Connection)} callback
* @returns {void}
*/
dialSingle (peer, transport, addr, token, callback) {
const ps = peer.toB58String()
log('dialSingle: %s:%s', ps, addr.toString())
let q
if (this.queues.has(ps)) {
q = this.queues.get(ps)
} else {
q = new DialQueue(this.perPeerLimit, this.dialTimeout)
this.queues.set(ps, q)
}
q.push(transport, addr, token, callback)
}
}
module.exports = LimitDialer

View File

@ -0,0 +1,109 @@
'use strict'
const { Connection } = require('libp2p-interfaces/src/connection')
const pull = require('pull-stream/pull')
const empty = require('pull-stream/sources/empty')
const timeout = require('async/timeout')
const queue = require('async/queue')
const debug = require('debug')
const once = require('once')
const log = debug('libp2p:switch:dialer:queue')
log.error = debug('libp2p:switch:dialer:queue:error')
/**
* Queue up the amount of dials to a given peer.
*/
class DialQueue {
/**
* Create a new dial queue.
*
* @param {number} limit
* @param {number} dialTimeout
*/
constructor (limit, dialTimeout) {
this.dialTimeout = dialTimeout
this.queue = queue((task, cb) => {
this._doWork(task.transport, task.addr, task.token, cb)
}, limit)
}
/**
* The actual work done by the queue.
*
* @param {SwarmTransport} transport
* @param {Multiaddr} addr
* @param {CancelToken} token
* @param {function(Error, Connection)} callback
* @returns {void}
* @private
*/
_doWork (transport, addr, token, callback) {
callback = once(callback)
log('work:start')
this._dialWithTimeout(transport, addr, (err, conn) => {
if (err) {
log.error(`${transport.constructor.name}:work`, err)
return callback(err)
}
if (token.cancel) {
log('work:cancel')
// clean up already done dials
pull(empty(), conn)
// If we can close the connection, do it
if (typeof conn.close === 'function') {
return conn.close((_) => callback(null))
}
return callback(null)
}
// one is enough
token.cancel = true
log('work:success')
const proxyConn = new Connection()
proxyConn.setInnerConn(conn)
callback(null, { multiaddr: addr, conn: conn })
})
}
/**
* Dial the given transport, timing out with the set timeout.
*
* @param {SwarmTransport} transport
* @param {Multiaddr} addr
* @param {function(Error, Connection)} callback
* @returns {void}
*
* @private
*/
_dialWithTimeout (transport, addr, callback) {
timeout((cb) => {
const conn = transport.dial(addr, (err) => {
if (err) {
return cb(err)
}
cb(null, conn)
})
}, this.dialTimeout)(callback)
}
/**
* Add new work to the queue.
*
* @param {SwarmTransport} transport
* @param {Multiaddr} addr
* @param {CancelToken} token
* @param {function(Error, Connection)} callback
* @returns {void}
*/
push (transport, addr, token, callback) {
this.queue.push({ transport, addr, token }, callback)
}
}
module.exports = DialQueue

View File

@ -0,0 +1,44 @@
'use strict'
const { Connection } = require('libp2p-interfaces/src/connection')
const pull = require('pull-stream/pull')
/**
* Creates a pull stream to run the given Connection stream through
* the given Observer. This provides a way to more easily monitor connections
* and their metadata. A new Connection will be returned that contains
* has the attached Observer.
*
* @param {Transport} transport
* @param {string} protocol
* @param {Connection} connection
* @param {Observer} observer
* @returns {Connection}
*/
module.exports = (transport, protocol, connection, observer) => {
const peerInfo = new Promise((resolve, reject) => {
connection.getPeerInfo((err, peerInfo) => {
if (!err && peerInfo) {
resolve(peerInfo)
return
}
const setPeerInfo = connection.setPeerInfo
connection.setPeerInfo = (pi) => {
setPeerInfo.call(connection, pi)
resolve(pi)
}
})
})
const stream = {
source: pull(
connection,
observer.incoming(transport, protocol, peerInfo)),
sink: pull(
observer.outgoing(transport, protocol, peerInfo),
connection)
}
return new Connection(stream, connection)
}

48
src/switch/observer.js Normal file
View File

@ -0,0 +1,48 @@
'use strict'
const map = require('pull-stream/throughs/map')
const EventEmitter = require('events')
/**
* Takes a Switch and returns an Observer that can be used in conjunction with
* observe-connection.js. The returned Observer comes with `incoming` and
* `outgoing` properties that can be used in pull streams to emit all metadata
* for messages that pass through a Connection.
*
* @param {Switch} swtch
* @returns {EventEmitter}
*/
module.exports = (swtch) => {
const observer = Object.assign(new EventEmitter(), {
incoming: observe('in'),
outgoing: observe('out')
})
swtch.on('peer-mux-established', (peerInfo) => {
observer.emit('peer:connected', peerInfo.id.toB58String())
})
swtch.on('peer-mux-closed', (peerInfo) => {
observer.emit('peer:closed', peerInfo.id.toB58String())
})
return observer
function observe (direction) {
return (transport, protocol, peerInfo) => {
return map((buffer) => {
willObserve(peerInfo, transport, protocol, direction, buffer.length)
return buffer
})
}
}
function willObserve (peerInfo, transport, protocol, direction, bufferLength) {
peerInfo.then((_peerInfo) => {
if (_peerInfo) {
const peerId = _peerInfo.id.toB58String()
observer.emit('message', peerId, transport, protocol, direction, bufferLength)
}
})
}
}

20
src/switch/plaintext.js Normal file
View File

@ -0,0 +1,20 @@
'use strict'
const setImmediate = require('async/setImmediate')
/**
* An encryption stub in the instance that the default crypto
* has not been overriden for the Switch
*/
module.exports = {
tag: '/plaintext/1.0.0',
encrypt (myId, conn, remoteId, callback) {
if (typeof remoteId === 'function') {
callback = remoteId
remoteId = undefined
}
setImmediate(() => callback())
return conn
}
}

View File

@ -0,0 +1,48 @@
'use strict'
const multistream = require('multistream-select')
const observeConn = require('./observe-connection')
const debug = require('debug')
const log = debug('libp2p:switch:protocol-muxer')
log.error = debug('libp2p:switch:protocol-muxer:error')
module.exports = function protocolMuxer (protocols, observer) {
return (transport) => (_parentConn, msListener) => {
const ms = msListener || new multistream.Listener()
let parentConn
// Only observe the transport if we have one, and there is not already a listener
if (transport && !msListener) {
parentConn = observeConn(transport, null, _parentConn, observer)
} else {
parentConn = _parentConn
}
Object.keys(protocols).forEach((protocol) => {
if (!protocol) {
return
}
const handler = (protocolName, _conn) => {
log('registering handler with protocol %s', protocolName)
const protocol = protocols[protocolName]
if (protocol) {
const handlerFunc = protocol && protocol.handlerFunc
if (handlerFunc) {
const conn = observeConn(null, protocolName, _conn, observer)
handlerFunc(protocol, conn)
}
}
}
ms.addHandler(protocol, handler, protocols[protocol].matchFunc)
})
ms.handle(parentConn, (err) => {
if (err) {
log.error('multistream handshake failed', err)
}
})
}
}

150
src/switch/stats/index.js Normal file
View File

@ -0,0 +1,150 @@
'use strict'
const EventEmitter = require('events')
const Stat = require('./stat')
const OldPeers = require('./old-peers')
const defaultOptions = {
computeThrottleMaxQueueSize: 1000,
computeThrottleTimeout: 2000,
movingAverageIntervals: [
60 * 1000, // 1 minute
5 * 60 * 1000, // 5 minutes
15 * 60 * 1000 // 15 minutes
],
maxOldPeersRetention: 50
}
const initialCounters = [
'dataReceived',
'dataSent'
]
const directionToEvent = {
in: 'dataReceived',
out: 'dataSent'
}
/**
* Binds to message events on the given `observer` to generate stats
* based on the Peer, Protocol and Transport used for the message. Stat
* events will be emitted via the `update` event.
*
* @param {Observer} observer
* @param {any} _options
* @returns {Stats}
*/
module.exports = (observer, _options) => {
const options = Object.assign({}, defaultOptions, _options)
const globalStats = new Stat(initialCounters, options)
const stats = Object.assign(new EventEmitter(), {
start: start,
stop: stop,
global: globalStats,
peers: () => Array.from(peerStats.keys()),
forPeer: (peerId) => {
return peerStats.get(peerId) || oldPeers.get(peerId)
},
transports: () => Array.from(transportStats.keys()),
forTransport: (transport) => transportStats.get(transport),
protocols: () => Array.from(protocolStats.keys()),
forProtocol: (protocol) => protocolStats.get(protocol)
})
globalStats.on('update', propagateChange)
const oldPeers = OldPeers(options.maxOldPeersRetention)
const peerStats = new Map()
const transportStats = new Map()
const protocolStats = new Map()
observer.on('peer:closed', (peerId) => {
const peer = peerStats.get(peerId)
if (peer) {
peer.removeListener('update', propagateChange)
peer.stop()
peerStats.delete(peerId)
oldPeers.set(peerId, peer)
}
})
return stats
function onMessage (peerId, transportTag, protocolTag, direction, bufferLength) {
const event = directionToEvent[direction]
if (transportTag) {
// because it has a transport tag, this message is at the global level, so we account this
// traffic as global.
globalStats.push(event, bufferLength)
// peer stats
let peer = peerStats.get(peerId)
if (!peer) {
peer = oldPeers.get(peerId)
if (peer) {
oldPeers.delete(peerId)
} else {
peer = new Stat(initialCounters, options)
}
peer.on('update', propagateChange)
peer.start()
peerStats.set(peerId, peer)
}
peer.push(event, bufferLength)
}
// transport stats
if (transportTag) {
let transport = transportStats.get(transportTag)
if (!transport) {
transport = new Stat(initialCounters, options)
transport.on('update', propagateChange)
transportStats.set(transportTag, transport)
}
transport.push(event, bufferLength)
}
// protocol stats
if (protocolTag) {
let protocol = protocolStats.get(protocolTag)
if (!protocol) {
protocol = new Stat(initialCounters, options)
protocol.on('update', propagateChange)
protocolStats.set(protocolTag, protocol)
}
protocol.push(event, bufferLength)
}
}
function start () {
observer.on('message', onMessage)
globalStats.start()
for (const peerStat of peerStats.values()) {
peerStat.start()
}
for (const transportStat of transportStats.values()) {
transportStat.start()
}
}
function stop () {
observer.removeListener('message', onMessage)
globalStats.stop()
for (const peerStat of peerStats.values()) {
peerStat.stop()
}
for (const transportStat of transportStats.values()) {
transportStat.stop()
}
}
function propagateChange () {
stats.emit('update')
}
}

View File

@ -0,0 +1,15 @@
'use strict'
const LRU = require('hashlru')
/**
* Creates and returns a Least Recently Used Cache
*
* @param {Number} maxSize
* @returns {LRUCache}
*/
module.exports = (maxSize) => {
const patched = LRU(maxSize)
patched.delete = patched.remove
return patched
}

239
src/switch/stats/stat.js Normal file
View File

@ -0,0 +1,239 @@
'use strict'
const EventEmitter = require('events')
const Big = require('bignumber.js')
const MovingAverage = require('moving-average')
const retimer = require('retimer')
/**
* A queue based manager for stat processing
*
* @param {Array<string>} initialCounters
* @param {any} options
*/
class Stats extends EventEmitter {
constructor (initialCounters, options) {
super()
this._options = options
this._queue = []
this._stats = {}
this._frequencyLastTime = Date.now()
this._frequencyAccumulators = {}
this._movingAverages = {}
this._update = this._update.bind(this)
const intervals = this._options.movingAverageIntervals
for (var i = 0; i < initialCounters.length; i++) {
var key = initialCounters[i]
this._stats[key] = Big(0)
this._movingAverages[key] = {}
for (var k = 0; k < intervals.length; k++) {
var interval = intervals[k]
var ma = this._movingAverages[key][interval] = MovingAverage(interval)
ma.push(this._frequencyLastTime, 0)
}
}
}
/**
* Initializes the internal timer if there are items in the queue. This
* should only need to be called if `Stats.stop` was previously called, as
* `Stats.push` will also start the processing.
*
* @returns {void}
*/
start () {
if (this._queue.length) {
this._resetComputeTimeout()
}
}
/**
* Stops processing and computing of stats by clearing the internal
* timer.
*
* @returns {void}
*/
stop () {
if (this._timeout) {
this._timeout.clear()
this._timeout = null
}
}
/**
* Returns a clone of the current stats.
*
* @returns {Map<string, Stat>}
*/
get snapshot () {
return Object.assign({}, this._stats)
}
/**
* Returns a clone of the internal movingAverages
*
* @returns {Array<MovingAverage>}
*/
get movingAverages () {
return Object.assign({}, this._movingAverages)
}
/**
* Pushes the given operation data to the queue, along with the
* current Timestamp, then resets the update timer.
*
* @param {string} counter
* @param {number} inc
* @returns {void}
*/
push (counter, inc) {
this._queue.push([counter, inc, Date.now()])
this._resetComputeTimeout()
}
/**
* Resets the timeout for triggering updates.
*
* @private
* @returns {void}
*/
_resetComputeTimeout () {
if (this._timeout) {
this._timeout.reschedule(this._nextTimeout())
} else {
this._timeout = retimer(this._update, this._nextTimeout())
}
}
/**
* Calculates and returns the timeout for the next update based on
* the urgency of the update.
*
* @private
* @returns {number}
*/
_nextTimeout () {
// calculate the need for an update, depending on the queue length
const urgency = this._queue.length / this._options.computeThrottleMaxQueueSize
const timeout = Math.max(this._options.computeThrottleTimeout * (1 - urgency), 0)
return timeout
}
/**
* If there are items in the queue, they will will be processed and
* the frequency for all items will be updated based on the Timestamp
* of the last item in the queue. The `update` event will also be emitted
* with the latest stats.
*
* If there are no items in the queue, no action is taken.
*
* @private
* @returns {void}
*/
_update () {
this._timeout = null
if (this._queue.length) {
let last
while (this._queue.length) {
const op = last = this._queue.shift()
this._applyOp(op)
}
this._updateFrequency(last[2]) // contains timestamp of last op
this.emit('update', this._stats)
}
}
/**
* For each key in the stats, the frequncy and moving averages
* will be updated via Stats._updateFrequencyFor based on the time
* difference between calls to this method.
*
* @private
* @param {Timestamp} latestTime
* @returns {void}
*/
_updateFrequency (latestTime) {
const timeDiff = latestTime - this._frequencyLastTime
Object.keys(this._stats).forEach((key) => {
this._updateFrequencyFor(key, timeDiff, latestTime)
})
this._frequencyLastTime = latestTime
}
/**
* Updates the `movingAverages` for the given `key` and also
* resets the `frequencyAccumulator` for the `key`.
*
* @private
* @param {string} key
* @param {number} timeDiffMS Time in milliseconds
* @param {Timestamp} latestTime Time in ticks
* @returns {void}
*/
_updateFrequencyFor (key, timeDiffMS, latestTime) {
const count = this._frequencyAccumulators[key] || 0
this._frequencyAccumulators[key] = 0
// if `timeDiff` is zero, `hz` becomes Infinity, so we fallback to 1ms
const safeTimeDiff = timeDiffMS || 1
const hz = (count / safeTimeDiff) * 1000
let movingAverages = this._movingAverages[key]
if (!movingAverages) {
movingAverages = this._movingAverages[key] = {}
}
const intervals = this._options.movingAverageIntervals
for (var i = 0; i < intervals.length; i++) {
var movingAverageInterval = intervals[i]
var movingAverage = movingAverages[movingAverageInterval]
if (!movingAverage) {
movingAverage = movingAverages[movingAverageInterval] = MovingAverage(movingAverageInterval)
}
movingAverage.push(latestTime, hz)
}
}
/**
* For the given operation, `op`, the stats and `frequencyAccumulator`
* will be updated or initialized if they don't already exist.
*
* @private
* @param {Array<string, number>} op
* @throws {InvalidNumber}
* @returns {void}
*/
_applyOp (op) {
const key = op[0]
const inc = op[1]
if (typeof inc !== 'number') {
throw new Error('invalid increment number:', inc)
}
let n
if (!Object.prototype.hasOwnProperty.call(this._stats, key)) {
n = this._stats[key] = Big(0)
} else {
n = this._stats[key]
}
this._stats[key] = n.plus(inc)
if (!this._frequencyAccumulators[key]) {
this._frequencyAccumulators[key] = 0
}
this._frequencyAccumulators[key] += inc
}
}
module.exports = Stats

272
src/switch/transport.js Normal file
View File

@ -0,0 +1,272 @@
'use strict'
/* eslint no-warning-comments: off */
const parallel = require('async/parallel')
const once = require('once')
const debug = require('debug')
const log = debug('libp2p:switch:transport')
const LimitDialer = require('./limit-dialer')
const { DIAL_TIMEOUT } = require('./constants')
const { uniqueBy } = require('./utils')
// number of concurrent outbound dials to make per peer, same as go-libp2p-swtch
const defaultPerPeerRateLimit = 8
/**
* Manages the transports for the switch. This simplifies dialing and listening across
* multiple transports.
*/
class TransportManager {
constructor (_switch) {
this.switch = _switch
this.dialer = new LimitDialer(defaultPerPeerRateLimit, this.switch._options.dialTimeout || DIAL_TIMEOUT)
}
/**
* Adds a `Transport` to the list of transports on the switch, and assigns it to the given key
*
* @param {String} key
* @param {Transport} transport
* @returns {void}
*/
add (key, transport) {
log('adding %s', key)
if (this.switch.transports[key]) {
throw new Error('There is already a transport with this key')
}
this.switch.transports[key] = transport
if (!this.switch.transports[key].listeners) {
this.switch.transports[key].listeners = []
}
}
/**
* Closes connections for the given transport key
* and removes it from the switch.
*
* @param {String} key
* @param {function(Error)} callback
* @returns {void}
*/
remove (key, callback) {
callback = callback || function () {}
if (!this.switch.transports[key]) {
return callback()
}
this.close(key, (err) => {
delete this.switch.transports[key]
callback(err)
})
}
/**
* Calls `remove` on each transport the switch has
*
* @param {function(Error)} callback
* @returns {void}
*/
removeAll (callback) {
const tasks = Object.keys(this.switch.transports).map((key) => {
return (cb) => {
this.remove(key, cb)
}
})
parallel(tasks, callback)
}
/**
* For a given transport `key`, dial to all that transport multiaddrs
*
* @param {String} key Key of the `Transport` to dial
* @param {PeerInfo} peerInfo
* @param {function(Error, Connection)} callback
* @returns {void}
*/
dial (key, peerInfo, callback) {
const transport = this.switch.transports[key]
let multiaddrs = peerInfo.multiaddrs.toArray()
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
// filter the multiaddrs that are actually valid for this transport
multiaddrs = TransportManager.dialables(transport, multiaddrs, this.switch._peerInfo)
log('dialing %s', key, multiaddrs.map((m) => m.toString()))
// dial each of the multiaddrs with the given transport
this.dialer.dialMany(peerInfo.id, transport, multiaddrs, (errors, success) => {
if (errors) {
return callback(errors)
}
peerInfo.connect(success.multiaddr)
callback(null, success.conn)
})
}
/**
* For a given Transport `key`, listen on all multiaddrs in the switch's `_peerInfo`.
* If a `handler` is not provided, the Switch's `protocolMuxer` will be used.
*
* @param {String} key
* @param {*} _options Currently ignored
* @param {function(Connection)} handler
* @param {function(Error)} callback
* @returns {void}
*/
listen (key, _options, handler, callback) {
handler = this.switch._connectionHandler(key, handler)
const transport = this.switch.transports[key]
let originalAddrs = this.switch._peerInfo.multiaddrs.toArray()
// Until TCP can handle distinct addresses on listen, https://github.com/libp2p/interface-transport/issues/41,
// make sure we aren't trying to listen on duplicate ports. This also applies to websockets.
originalAddrs = uniqueBy(originalAddrs, (addr) => {
// Any non 0 port should register as unique
const port = Number(addr.toOptions().port)
return isNaN(port) || port === 0 ? addr.toString() : port
})
const multiaddrs = TransportManager.dialables(transport, originalAddrs)
if (!transport.listeners) {
transport.listeners = []
}
let freshMultiaddrs = []
const createListeners = multiaddrs.map((ma) => {
return (cb) => {
const done = once(cb)
const listener = transport.createListener(handler)
listener.once('error', done)
listener.listen(ma, (err) => {
if (err) {
return done(err)
}
listener.removeListener('error', done)
listener.getAddrs((err, addrs) => {
if (err) {
return done(err)
}
freshMultiaddrs = freshMultiaddrs.concat(addrs)
transport.listeners.push(listener)
done()
})
})
}
})
parallel(createListeners, (err) => {
if (err) {
return callback(err)
}
// cause we can listen on port 0 or 0.0.0.0
this.switch._peerInfo.multiaddrs.replace(multiaddrs, freshMultiaddrs)
callback()
})
}
/**
* Closes the transport with the given key, by closing all of its listeners
*
* @param {String} key
* @param {function(Error)} callback
* @returns {void}
*/
close (key, callback) {
const transport = this.switch.transports[key]
if (!transport) {
return callback(new Error(`Trying to close non existing transport: ${key}`))
}
parallel(transport.listeners.map((listener) => {
return (cb) => {
listener.close(cb)
}
}), callback)
}
/**
* For a given transport, return its multiaddrs that match the given multiaddrs
*
* @param {Transport} transport
* @param {Array<Multiaddr>} multiaddrs
* @param {PeerInfo} peerInfo Optional - a peer whose addresses should not be returned
* @returns {Array<Multiaddr>}
*/
static dialables (transport, multiaddrs, peerInfo) {
// If we dont have a proper transport, return no multiaddrs
if (!transport || !transport.filter) return []
const transportAddrs = transport.filter(multiaddrs)
if (!peerInfo || !transportAddrs.length) {
return transportAddrs
}
const ourAddrs = ourAddresses(peerInfo)
const result = transportAddrs.filter(transportAddr => {
// If our address is in the destination address, filter it out
return !ourAddrs.some(a => getDestination(transportAddr).startsWith(a))
})
return result
}
}
/**
* Expand addresses in peer info into array of addresses with and without peer
* ID suffix.
*
* @param {PeerInfo} peerInfo Our peer info object
* @returns {String[]}
*/
function ourAddresses (peerInfo) {
const ourPeerId = peerInfo.id.toB58String()
return peerInfo.multiaddrs.toArray()
.reduce((ourAddrs, addr) => {
const peerId = addr.getPeerId()
addr = addr.toString()
const otherAddr = peerId
? addr.slice(0, addr.lastIndexOf(`/ipfs/${peerId}`))
: `${addr}/ipfs/${ourPeerId}`
return ourAddrs.concat([addr, otherAddr])
}, [])
.filter(a => Boolean(a))
.concat(`/ipfs/${ourPeerId}`)
}
const RelayProtos = [
'p2p-circuit',
'p2p-websocket-star',
'p2p-webrtc-star',
'p2p-stardust'
]
/**
* Get the destination address of a (possibly relay) multiaddr as a string
*
* @param {Multiaddr} addr
* @returns {String}
*/
function getDestination (addr) {
const protos = addr.protoNames().reverse()
const splitProto = protos.find(p => RelayProtos.includes(p))
addr = addr.toString()
if (!splitProto) return addr
return addr.slice(addr.lastIndexOf(splitProto) + splitProto.length)
}
module.exports = TransportManager

60
src/switch/utils.js Normal file
View File

@ -0,0 +1,60 @@
'use strict'
const Identify = require('../identify')
/**
* For a given multistream, registers to handle the given connection
* @param {MultistreamDialer} multistream
* @param {Connection} connection
* @returns {Promise}
*/
module.exports.msHandle = (multistream, connection) => {
return new Promise((resolve, reject) => {
multistream.handle(connection, (err) => {
if (err) return reject(err)
resolve()
})
})
}
/**
* For a given multistream, selects the given protocol
* @param {MultistreamDialer} multistream
* @param {string} protocol
* @returns {Promise} Resolves the selected Connection
*/
module.exports.msSelect = (multistream, protocol) => {
return new Promise((resolve, reject) => {
multistream.select(protocol, (err, connection) => {
if (err) return reject(err)
resolve(connection)
})
})
}
/**
* Runs identify for the given connection and verifies it against the
* PeerInfo provided
* @param {Connection} connection
* @param {PeerInfo} cryptoPeerInfo The PeerInfo determined during crypto exchange
* @returns {Promise} Resolves {peerInfo, observedAddrs}
*/
module.exports.identifyDialer = (connection, cryptoPeerInfo) => {
return new Promise((resolve, reject) => {
Identify.dialer(connection, cryptoPeerInfo, (err, peerInfo, observedAddrs) => {
if (err) return reject(err)
resolve({ peerInfo, observedAddrs })
})
})
}
/**
* Get unique values from `arr` using `getValue` to determine
* what is used for uniqueness
* @param {Array} arr The array to get unique values for
* @param {function(value)} getValue The function to determine what is compared
* @returns {Array}
*/
module.exports.uniqueBy = (arr, getValue) => {
return [...new Map(arr.map((i) => [getValue(i), i])).values()]
}

183
src/transport-manager.js Normal file
View File

@ -0,0 +1,183 @@
'use strict'
const pSettle = require('p-settle')
const { codes } = require('./errors')
const errCode = require('err-code')
const debug = require('debug')
const log = debug('libp2p:transports')
log.error = debug('libp2p:transports:error')
class TransportManager {
/**
* @constructor
* @param {object} options
* @param {Libp2p} options.libp2p The Libp2p instance. It will be passed to the transports.
* @param {Upgrader} options.upgrader The upgrader to provide to the transports
*/
constructor ({ libp2p, upgrader }) {
this.libp2p = libp2p
this.upgrader = upgrader
this._transports = new Map()
this._listeners = new Map()
}
/**
* Adds a `Transport` to the manager
*
* @param {String} key
* @param {Transport} Transport
* @returns {void}
*/
add (key, Transport) {
log('adding %s', key)
if (!key) {
throw errCode(new Error(`Transport must have a valid key, was given '${key}'`), codes.ERR_INVALID_KEY)
}
if (this._transports.has(key)) {
throw errCode(new Error('There is already a transport with this key'), codes.ERR_DUPLICATE_TRANSPORT)
}
const transport = new Transport({
libp2p: this.libp2p,
upgrader: this.upgrader
})
this._transports.set(key, transport)
if (!this._listeners.has(key)) {
this._listeners.set(key, [])
}
}
/**
* Stops all listeners
* @async
*/
async close () {
const tasks = []
for (const [key, listeners] of this._listeners) {
log('closing listeners for %s', key)
while (listeners.length) {
const listener = listeners.pop()
tasks.push(listener.close())
}
}
await Promise.all(tasks)
log('all listeners closed')
this._listeners.clear()
}
/**
* Dials the given Multiaddr over it's supported transport
* @param {Multiaddr} ma
* @param {*} options
* @returns {Promise<Connection>}
*/
async dial (ma, options) {
const transport = this.transportForMultiaddr(ma)
if (!transport) {
throw errCode(new Error(`No transport available for address ${String(ma)}`), codes.ERR_TRANSPORT_UNAVAILABLE)
}
try {
return await transport.dial(ma, options)
} catch (err) {
throw errCode(new Error('Transport dial failed'), codes.ERR_TRANSPORT_DIAL_FAILED, err)
}
}
/**
* Returns all Multiaddr's the listeners are using
* @returns {Multiaddr[]}
*/
getAddrs () {
let addrs = []
for (const listeners of this._listeners.values()) {
for (const listener of listeners) {
addrs = [...addrs, ...listener.getAddrs()]
}
}
return addrs
}
/**
* Finds a transport that matches the given Multiaddr
* @param {Multiaddr} ma
* @returns {Transport|null}
*/
transportForMultiaddr (ma) {
for (const transport of this._transports.values()) {
const addrs = transport.filter([ma])
if (addrs.length) return transport
}
return null
}
/**
* Starts listeners for each given Multiaddr.
* @async
* @param {Multiaddr[]} addrs
*/
async listen (addrs) {
for (const [key, transport] of this._transports.entries()) {
const supportedAddrs = transport.filter(addrs)
const tasks = []
// For each supported multiaddr, create a listener
for (const addr of supportedAddrs) {
log('creating listener for %s on %s', key, addr)
const listener = transport.createListener({}, this.onConnection)
this._listeners.get(key).push(listener)
// We need to attempt to listen on everything
tasks.push(listener.listen(addr))
}
const results = await pSettle(tasks)
// If we are listening on at least 1 address, succeed.
// TODO: we should look at adding a retry (`p-retry`) here to better support
// listening on remote addresses as they may be offline. We could then potentially
// just wait for any (`p-any`) listener to succeed on each transport before returning
const isListening = results.find(r => r.isFulfilled === true)
if (!isListening) {
throw errCode(new Error(`Transport (${key}) could not listen on any available address`), codes.ERR_NO_VALID_ADDRESSES)
}
}
}
/**
* Removes the given transport from the manager.
* If a transport has any running listeners, they will be closed.
*
* @async
* @param {string} key
*/
async remove (key) {
log('removing %s', key)
if (this._listeners.has(key)) {
// Close any running listeners
for (const listener of this._listeners.get(key)) {
await listener.close()
}
}
this._transports.delete(key)
this._listeners.delete(key)
}
/**
* Removes all transports from the manager.
* If any listeners are running, they will be closed.
* @async
*/
async removeAll () {
const tasks = []
for (const key of this._transports.keys()) {
tasks.push(this.remove(key))
}
await Promise.all(tasks)
}
}
module.exports = TransportManager

366
src/upgrader.js Normal file
View File

@ -0,0 +1,366 @@
'use strict'
const debug = require('debug')
const log = debug('libp2p:upgrader')
log.error = debug('libp2p:upgrader:error')
const Multistream = require('multistream-select')
const { Connection } = require('libp2p-interfaces/src/connection')
const PeerId = require('peer-id')
const pipe = require('it-pipe')
const errCode = require('err-code')
const { codes } = require('./errors')
/**
* @typedef MultiaddrConnection
* @property {function} sink
* @property {AsyncIterator} source
* @property {*} conn
* @property {Multiaddr} remoteAddr
*/
/**
* @typedef CryptoResult
* @property {*} conn A duplex iterable
* @property {PeerId} remotePeer
* @property {string} protocol
*/
class Upgrader {
/**
* @param {object} options
* @param {PeerId} options.localPeer
* @param {Map<string, Crypto>} options.cryptos
* @param {Map<string, Muxer>} options.muxers
* @param {function(Connection)} options.onConnection Called when a connection is upgraded
* @param {function(Connection)} options.onConnectionEnd
*/
constructor ({
localPeer,
cryptos,
muxers,
onConnectionEnd = () => {},
onConnection = () => {}
}) {
this.localPeer = localPeer
this.cryptos = cryptos || new Map()
this.muxers = muxers || new Map()
this.protector = null
this.protocols = new Map()
this.onConnection = onConnection
this.onConnectionEnd = onConnectionEnd
}
/**
* Upgrades an inbound connection
* @async
* @param {MultiaddrConnection} maConn
* @returns {Promise<Connection>}
*/
async upgradeInbound (maConn) {
let encryptedConn
let remotePeer
let muxedConnection
let Muxer
let cryptoProtocol
log('Starting the inbound connection upgrade')
// Protect
let protectedConn = maConn
if (this.protector) {
protectedConn = await this.protector.protect(maConn)
}
try {
// Encrypt the connection
({
conn: encryptedConn,
remotePeer,
protocol: cryptoProtocol
} = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos))
// Multiplex the connection
;({ stream: muxedConnection, Muxer } = await this._multiplexInbound(encryptedConn, this.muxers))
} catch (err) {
log.error('Failed to upgrade inbound connection', err)
await maConn.close(err)
// TODO: We shouldn't throw here, as there isn't anything to catch the failure
throw err
}
log('Successfully upgraded inbound connection')
return this._createConnection({
cryptoProtocol,
direction: 'inbound',
maConn,
muxedConnection,
Muxer,
remotePeer
})
}
/**
* Upgrades an outbound connection
* @async
* @param {MultiaddrConnection} maConn
* @returns {Promise<Connection>}
*/
async upgradeOutbound (maConn) {
let remotePeerId
try {
remotePeerId = PeerId.createFromB58String(maConn.remoteAddr.getPeerId())
} catch (err) {
log.error('multiaddr did not contain a valid peer id', err)
}
let encryptedConn
let remotePeer
let muxedConnection
let cryptoProtocol
let Muxer
log('Starting the outbound connection upgrade')
// Protect
let protectedConn = maConn
if (this.protector) {
protectedConn = await this.protector.protect(maConn)
}
try {
// Encrypt the connection
({
conn: encryptedConn,
remotePeer,
protocol: cryptoProtocol
} = await this._encryptOutbound(this.localPeer, protectedConn, remotePeerId, this.cryptos))
// Multiplex the connection
;({ stream: muxedConnection, Muxer } = await this._multiplexOutbound(encryptedConn, this.muxers))
} catch (err) {
log.error('Failed to upgrade outbound connection', err)
await maConn.close(err)
throw err
}
log('Successfully upgraded outbound connection')
return this._createConnection({
cryptoProtocol,
direction: 'outbound',
maConn,
muxedConnection,
Muxer,
remotePeer
})
}
/**
* A convenience method for generating a new `Connection`
* @private
* @param {object} options
* @param {string} cryptoProtocol The crypto protocol that was negotiated
* @param {string} direction One of ['inbound', 'outbound']
* @param {MultiaddrConnection} maConn The transport layer connection
* @param {*} muxedConnection A duplex connection returned from multiplexer selection
* @param {Muxer} Muxer The muxer to be used for muxing
* @param {PeerId} remotePeer The peer the connection is with
* @returns {Connection}
*/
_createConnection ({
cryptoProtocol,
direction,
maConn,
muxedConnection,
Muxer,
remotePeer
}) {
// Create the muxer
const muxer = new Muxer({
// Run anytime a remote stream is created
onStream: async muxedStream => {
const mss = new Multistream.Listener(muxedStream)
try {
const { stream, protocol } = await mss.handle(Array.from(this.protocols.keys()))
log('%s: incoming stream opened on %s', direction, protocol)
connection.addStream(stream, protocol)
this._onStream({ connection, stream, protocol })
} catch (err) {
log.error(err)
}
},
// Run anytime a stream closes
onStreamEnd: muxedStream => {
connection.removeStream(muxedStream.id)
}
})
const newStream = async protocols => {
log('%s: starting new stream on %s', direction, protocols)
const muxedStream = muxer.newStream()
const mss = new Multistream.Dialer(muxedStream)
try {
const { stream, protocol } = await mss.select(protocols)
return { stream: { ...muxedStream, ...stream }, protocol }
} catch (err) {
log.error('could not create new stream', err)
throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
}
}
// Pipe all data through the muxer
pipe(muxedConnection, muxer, muxedConnection)
maConn.timeline.upgraded = Date.now()
const timelineProxy = new Proxy(maConn.timeline, {
set: (...args) => {
if (args[1] === 'close' && args[2]) {
this.onConnectionEnd(connection)
}
return Reflect.set(...args)
}
})
// Create the connection
const connection = new Connection({
localAddr: maConn.localAddr,
remoteAddr: maConn.remoteAddr,
localPeer: this.localPeer,
remotePeer: remotePeer,
stat: {
direction,
timeline: timelineProxy,
multiplexer: Muxer.multicodec,
encryption: cryptoProtocol
},
newStream,
getStreams: () => muxer.streams,
close: err => maConn.close(err)
})
this.onConnection(connection)
return connection
}
/**
* Routes incoming streams to the correct handler
* @private
* @param {object} options
* @param {Connection} options.connection The connection the stream belongs to
* @param {Stream} options.stream
* @param {string} options.protocol
*/
_onStream ({ connection, stream, protocol }) {
const handler = this.protocols.get(protocol)
handler({ connection, stream, protocol })
}
/**
* Attempts to encrypt the incoming `connection` with the provided `cryptos`.
* @private
* @async
* @param {PeerId} localPeer The initiators PeerInfo
* @param {*} connection
* @param {Map<string, Crypto>} cryptos
* @returns {CryptoResult} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used
*/
async _encryptInbound (localPeer, connection, cryptos) {
const mss = new Multistream.Listener(connection)
const protocols = Array.from(cryptos.keys())
log('handling inbound crypto protocol selection', protocols)
try {
const { stream, protocol } = await mss.handle(protocols)
const crypto = cryptos.get(protocol)
log('encrypting inbound connection...')
return {
...await crypto.secureInbound(localPeer, stream),
protocol
}
} catch (err) {
throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
}
}
/**
* Attempts to encrypt the given `connection` with the provided `cryptos`.
* The first `Crypto` module to succeed will be used
* @private
* @async
* @param {PeerId} localPeer The initiators PeerInfo
* @param {*} connection
* @param {PeerId} remotePeerId
* @param {Map<string, Crypto>} cryptos
* @returns {CryptoResult} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used
*/
async _encryptOutbound (localPeer, connection, remotePeerId, cryptos) {
const mss = new Multistream.Dialer(connection)
const protocols = Array.from(cryptos.keys())
log('selecting outbound crypto protocol', protocols)
try {
const { stream, protocol } = await mss.select(protocols)
const crypto = cryptos.get(protocol)
log('encrypting outbound connection to %j', remotePeerId)
return {
...await crypto.secureOutbound(localPeer, stream, remotePeerId),
protocol
}
} catch (err) {
throw errCode(err, codes.ERR_ENCRYPTION_FAILED)
}
}
/**
* Selects one of the given muxers via multistream-select. That
* muxer will be used for all future streams on the connection.
* @private
* @async
* @param {*} connection A basic duplex connection to multiplex
* @param {Map<string, Muxer>} muxers The muxers to attempt multiplexing with
* @returns {*} A muxed connection
*/
async _multiplexOutbound (connection, muxers) {
const dialer = new Multistream.Dialer(connection)
const protocols = Array.from(muxers.keys())
log('outbound selecting muxer %s', protocols)
try {
const { stream, protocol } = await dialer.select(protocols)
log('%s selected as muxer protocol', protocol)
const Muxer = muxers.get(protocol)
return { stream, Muxer }
} catch (err) {
throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
}
}
/**
* Registers support for one of the given muxers via multistream-select. The
* selected muxer will be used for all future streams on the connection.
* @private
* @async
* @param {*} connection A basic duplex connection to multiplex
* @param {Map<string, Muxer>} muxers The muxers to attempt multiplexing with
* @returns {*} A muxed connection
*/
async _multiplexInbound (connection, muxers) {
const listener = new Multistream.Listener(connection)
const protocols = Array.from(muxers.keys())
log('inbound handling muxers %s', protocols)
try {
const { stream, protocol } = await listener.handle(protocols)
const Muxer = muxers.get(protocol)
return { stream, Muxer }
} catch (err) {
throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
}
}
}
module.exports = Upgrader

View File

@ -30,4 +30,18 @@ function emitFirst (emitter, events, handler) {
})
}
/**
* Converts BufferList messages to Buffers
* @param {*} source
* @returns {AsyncGenerator}
*/
function toBuffer (source) {
return (async function * () {
for await (const chunk of source) {
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
}
})()
}
module.exports.emitFirst = emitFirst
module.exports.toBuffer = toBuffer

View File

@ -1,4 +0,0 @@
'use strict'
require('./circuit-relay.browser')
require('./transports.browser')

View File

@ -1,93 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const createNode = require('./utils/create-node')
const tryEcho = require('./utils/try-echo')
const echo = require('./utils/echo')
const {
getPeerRelay
} = require('./utils/constants')
function setupNodeWithRelay (addrs, options = {}) {
options = {
config: {
relay: {
enabled: true
},
...options.config
},
...options
}
return new Promise((resolve) => {
createNode(addrs, options, (err, node) => {
expect(err).to.not.exist()
node.handle(echo.multicodec, echo)
node.start((err) => {
expect(err).to.not.exist()
resolve(node)
})
})
})
}
describe('circuit relay', () => {
let browserNode1
let browserNode2
let peerRelay
before('get peer relay', async () => {
peerRelay = await getPeerRelay()
})
before('create the browser nodes', async () => {
[browserNode1, browserNode2] = await Promise.all([
setupNodeWithRelay([]),
setupNodeWithRelay([])
])
})
before('connect to the relay node', async () => {
await Promise.all(
[browserNode1, browserNode2].map((node) => {
return new Promise(resolve => {
node.dialProtocol(peerRelay, (err) => {
expect(err).to.not.exist()
resolve()
})
})
})
)
})
before('give time for HOP support to be determined', async () => {
await new Promise(resolve => {
setTimeout(resolve, 1e3)
})
})
after(async () => {
await Promise.all(
[browserNode1, browserNode2].map((node) => {
return new Promise((resolve) => {
node.stop(resolve)
})
})
)
})
it('should be able to echo over relay', (done) => {
browserNode1.dialProtocol(browserNode2.peerInfo, echo.multicodec, (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.exist()
tryEcho(conn, done)
})
})
})

View File

@ -1,215 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const sinon = require('sinon')
const waterfall = require('async/waterfall')
const series = require('async/series')
const parallel = require('async/parallel')
const Circuit = require('libp2p-circuit')
const multiaddr = require('multiaddr')
const createNode = require('./utils/create-node')
const tryEcho = require('./utils/try-echo')
const echo = require('./utils/echo')
describe('circuit relay', () => {
const handlerSpies = []
let relayNode1
let relayNode2
let nodeWS1
let nodeWS2
let nodeTCP1
let nodeTCP2
function setupNode (addrs, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
return createNode(addrs, options, (err, node) => {
expect(err).to.not.exist()
node.handle('/echo/1.0.0', echo)
node.start((err) => {
expect(err).to.not.exist()
handlerSpies.push(sinon.spy(
node._switch.transports[Circuit.tag].listeners[0].hopHandler, 'handle'
))
callback(node)
})
})
}
before(function (done) {
this.timeout(20 * 1000)
waterfall([
// set up passive relay
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0/ws',
'/ip4/0.0.0.0/tcp/0'
], {
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: false // passive relay
}
}
}
}, (node) => {
relayNode1 = node
cb()
}),
// setup active relay
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0/ws',
'/ip4/0.0.0.0/tcp/0'
], {
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: false // passive relay
}
}
}
}, (node) => {
relayNode2 = node
cb()
}),
// setup node with WS
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0/ws'
], {
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeWS1 = node
cb()
}),
// setup node with WS
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0/ws'
], {
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeWS2 = node
cb()
}),
// set up node with TCP
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0'
], {
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeTCP1 = node
cb()
}),
// set up node with TCP
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0'
], {
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeTCP2 = node
cb()
})
], (err) => {
expect(err).to.not.exist()
series([
(cb) => nodeWS1.dial(relayNode1.peerInfo, cb),
(cb) => nodeWS1.dial(relayNode2.peerInfo, cb),
(cb) => nodeTCP1.dial(relayNode1.peerInfo, cb),
(cb) => nodeTCP2.dial(relayNode2.peerInfo, cb)
], done)
})
})
after((done) => {
parallel([
(cb) => relayNode1.stop(cb),
(cb) => relayNode2.stop(cb),
(cb) => nodeWS1.stop(cb),
(cb) => nodeWS2.stop(cb),
(cb) => nodeTCP1.stop(cb),
(cb) => nodeTCP2.stop(cb)
], done)
})
describe('any relay', function () {
this.timeout(20 * 1000)
it('dial from WS1 to TCP1 over any R', (done) => {
nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.exist()
tryEcho(conn, done)
})
})
it('fail to dial - no R from WS2 to TCP1', (done) => {
nodeWS2.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
expect(err).to.exist()
expect(conn).to.not.exist()
done()
})
})
})
describe('explicit relay', function () {
this.timeout(20 * 1000)
it('dial from WS1 to TCP1 over R1', (done) => {
nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.exist()
tryEcho(conn, () => {
const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString()
expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`)
done()
})
})
})
it('dial from WS1 to TCP2 over R2', (done) => {
nodeWS1.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist()
expect(conn).to.exist()
tryEcho(conn, () => {
const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString()
expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`)
done()
})
})
})
})
})

View File

@ -1,332 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const waterfall = require('async/waterfall')
const WS = require('libp2p-websockets')
const Bootstrap = require('libp2p-bootstrap')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
const DHT = require('libp2p-kad-dht')
const validateConfig = require('../src/config').validate
describe('configuration', () => {
let peerInfo
before((done) => {
waterfall([
(cb) => PeerId.create({ bits: 512 }, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(info, cb) => {
peerInfo = info
cb()
}
], () => done())
})
it('should throw an error if peerInfo is missing', () => {
expect(() => {
validateConfig({
modules: {
transport: [WS]
}
})
}).to.throw()
})
it('should throw an error if modules is missing', () => {
expect(() => {
validateConfig({
peerInfo
})
}).to.throw()
})
it('should throw an error if there are no transports', () => {
expect(() => {
validateConfig({
peerInfo,
modules: {
transport: []
}
})
}).to.throw('ERROR_EMPTY')
})
it('should add defaults to config', () => {
const options = {
peerInfo,
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
dht: DHT
}
}
const expected = {
peerInfo,
connectionManager: {
minPeers: 25
},
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
dht: DHT
},
config: {
peerDiscovery: {
autoDial: true
},
pubsub: {
enabled: true
},
dht: {
kBucketSize: 20,
enabled: false,
randomWalk: {
enabled: false,
queriesPerPeriod: 1,
interval: 300000,
timeout: 10000
}
},
relay: {
enabled: true,
hop: {
active: false,
enabled: false
}
}
}
}
expect(validateConfig(options)).to.deep.equal(expected)
})
it('should add defaults to missing items', () => {
const options = {
peerInfo,
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
dht: DHT
},
config: {
peerDiscovery: {
bootstrap: {
interval: 1000,
enabled: true
}
}
}
}
const expected = {
peerInfo,
connectionManager: {
minPeers: 25
},
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
dht: DHT
},
config: {
peerDiscovery: {
autoDial: true,
bootstrap: {
interval: 1000,
enabled: true
}
},
pubsub: {
enabled: true
},
dht: {
kBucketSize: 20,
enabled: false,
randomWalk: {
enabled: false,
queriesPerPeriod: 1,
interval: 300000,
timeout: 10000
}
},
relay: {
enabled: true,
hop: {
active: false,
enabled: false
}
}
}
}
expect(validateConfig(options)).to.deep.equal(expected)
})
it('should allow for configuring the switch', () => {
const options = {
peerInfo,
switch: {
denyTTL: 60e3,
denyAttempts: 5,
maxParallelDials: 100,
maxColdCalls: 50,
dialTimeout: 30e3
},
modules: {
transport: [WS],
peerDiscovery: []
}
}
expect(validateConfig(options)).to.deep.include({
switch: {
denyTTL: 60e3,
denyAttempts: 5,
maxParallelDials: 100,
maxColdCalls: 50,
dialTimeout: 30e3
}
})
})
it('should allow for delegated content and peer routing', () => {
const peerRouter = new DelegatedPeerRouter()
const contentRouter = new DelegatedContentRouter(peerInfo)
const options = {
peerInfo,
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
peerRouting: [peerRouter],
contentRouting: [contentRouter],
dht: DHT
},
config: {
peerDiscovery: {
bootstrap: {
interval: 1000,
enabled: true
}
}
}
}
expect(validateConfig(options).modules).to.deep.include({
peerRouting: [peerRouter],
contentRouting: [contentRouter]
})
})
it('should not allow for dht to be enabled without it being provided', () => {
const options = {
peerInfo,
modules: {
transport: [WS]
},
config: {
dht: {
enabled: true
}
}
}
expect(() => validateConfig(options)).to.throw()
})
it('should be able to add validators and selectors for dht', () => {
const selectors = {}
const validators = {}
const options = {
peerInfo,
modules: {
transport: [WS],
dht: DHT
},
config: {
dht: {
selectors,
validators
}
}
}
const expected = {
peerInfo,
connectionManager: {
minPeers: 25
},
modules: {
transport: [WS],
dht: DHT
},
config: {
pubsub: {
enabled: true
},
peerDiscovery: {
autoDial: true
},
relay: {
enabled: true,
hop: {
active: false,
enabled: false
}
},
dht: {
selectors,
validators
}
}
}
expect(validateConfig(options)).to.deep.equal(expected)
})
it('should support new properties for the dht config', () => {
const options = {
peerInfo,
modules: {
transport: [WS],
dht: DHT
},
config: {
dht: {
kBucketSize: 20,
enabled: false,
myNewDHTConfigProperty: true,
randomWalk: {
enabled: false,
queriesPerPeriod: 1,
interval: 300000,
timeout: 10000
}
}
}
}
const expected = {
kBucketSize: 20,
enabled: false,
myNewDHTConfigProperty: true,
randomWalk: {
enabled: false,
queriesPerPeriod: 1,
interval: 300000,
timeout: 10000
}
}
const actual = validateConfig(options).config.dht
expect(actual).to.deep.equal(expected)
})
})

View File

@ -1,404 +0,0 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const _times = require('lodash.times')
const CID = require('cids')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
const sinon = require('sinon')
const nock = require('nock')
const ma = require('multiaddr')
const Node = require('./utils/bundle-nodejs')
const createNode = require('./utils/create-node')
const createPeerInfo = createNode.createPeerInfo
describe('.contentRouting', () => {
describe('via the dht', () => {
let nodeA
let nodeB
let nodeC
let nodeD
let nodeE
before(function (done) {
this.timeout(5 * 1000)
const tasks = _times(5, () => (cb) => {
createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
expect(err).to.not.exist()
node.start((err) => cb(err, node))
})
})
parallel(tasks, (err, nodes) => {
expect(err).to.not.exist()
nodeA = nodes[0]
nodeB = nodes[1]
nodeC = nodes[2]
nodeD = nodes[3]
nodeE = nodes[4]
parallel([
(cb) => nodeA.dial(nodeB.peerInfo, cb),
(cb) => nodeB.dial(nodeC.peerInfo, cb),
(cb) => nodeC.dial(nodeD.peerInfo, cb),
(cb) => nodeD.dial(nodeE.peerInfo, cb),
(cb) => nodeE.dial(nodeA.peerInfo, cb)
], done)
})
})
after((done) => {
parallel([
(cb) => nodeA.stop(cb),
(cb) => nodeB.stop(cb),
(cb) => nodeC.stop(cb),
(cb) => nodeD.stop(cb),
(cb) => nodeE.stop(cb)
], done)
})
it('should use the nodes dht to provide', (done) => {
const stub = sinon.stub(nodeA._dht, 'provide').callsFake(() => {
stub.restore()
done()
})
nodeA.contentRouting.provide()
})
it('should use the nodes dht to find providers', (done) => {
const stub = sinon.stub(nodeA._dht, 'findProviders').callsFake(() => {
stub.restore()
done()
})
nodeA.contentRouting.findProviders()
})
describe('le ring', () => {
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
it('let kbucket get filled', (done) => {
setTimeout(() => done(), 250)
})
it('nodeA.contentRouting.provide', (done) => {
nodeA.contentRouting.provide(cid, done)
})
it('nodeE.contentRouting.findProviders for existing record', (done) => {
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
expect(err).to.not.exist()
expect(providers).to.have.length.above(0)
done()
})
})
it('nodeE.contentRouting.findProviders with limited number of providers', (done) => {
parallel([
(cb) => nodeA.contentRouting.provide(cid, cb),
(cb) => nodeB.contentRouting.provide(cid, cb),
(cb) => nodeC.contentRouting.provide(cid, cb)
], (err) => {
expect(err).to.not.exist()
nodeE.contentRouting.findProviders(cid, { maxNumProviders: 2 }, (err, providers) => {
expect(err).to.not.exist()
expect(providers).to.have.length(2)
done()
})
})
})
it('nodeC.contentRouting.findProviders for non existing record (timeout)', (done) => {
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSnnnn')
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
expect(err).to.exist()
expect(err.code).to.eql('ERR_NOT_FOUND')
expect(providers).to.not.exist()
done()
})
})
})
})
describe('via a delegate', () => {
let nodeA
let delegate
before((done) => {
waterfall([
(cb) => {
createPeerInfo(cb)
},
// Create the node using the delegate
(peerInfo, cb) => {
delegate = new DelegatedContentRouter(peerInfo.id, {
host: '0.0.0.0',
protocol: 'http',
port: 60197
}, [
ma('/ip4/0.0.0.0/tcp/60194')
])
nodeA = new Node({
peerInfo,
modules: {
contentRouting: [delegate]
},
config: {
dht: {
enabled: false
},
relay: {
enabled: true,
hop: {
enabled: true,
active: false
}
}
}
})
nodeA.start(cb)
}
], done)
})
after((done) => nodeA.stop(done))
afterEach(() => nock.cleanAll())
describe('provide', () => {
it('should use the delegate router to provide', (done) => {
const stub = sinon.stub(delegate, 'provide').callsFake(() => {
stub.restore()
done()
})
nodeA.contentRouting.provide()
})
it('should be able to register as a provider', (done) => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const mockApi = nock('http://0.0.0.0:60197')
// mock the refs call
.post('/api/v0/refs')
.query({
recursive: false,
arg: cid.toBaseEncodedString(),
'stream-channels': true
})
.reply(200, null, [
'Content-Type', 'application/json',
'X-Chunked-Output', '1'
])
nodeA.contentRouting.provide(cid, (err) => {
expect(err).to.not.exist()
expect(mockApi.isDone()).to.equal(true)
done()
})
})
it('should handle errors when registering as a provider', (done) => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const mockApi = nock('http://0.0.0.0:60197')
// mock the refs call
.post('/api/v0/refs')
.query({
recursive: false,
arg: cid.toBaseEncodedString(),
'stream-channels': true
})
.reply(502, 'Bad Gateway', ['Content-Type', 'application/json'])
nodeA.contentRouting.provide(cid, (err) => {
expect(err).to.exist()
expect(mockApi.isDone()).to.equal(true)
done()
})
})
})
describe('find providers', () => {
it('should use the delegate router to find providers', (done) => {
const stub = sinon.stub(delegate, 'findProviders').callsFake(() => {
stub.restore()
done()
})
nodeA.contentRouting.findProviders()
})
it('should be able to find providers', (done) => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
const mockApi = nock('http://0.0.0.0:60197')
.post('/api/v0/dht/findprovs')
.query({
arg: cid.toBaseEncodedString(),
timeout: '1000ms',
'stream-channels': true
})
.reply(200, `{"Extra":"","ID":"QmWKqWXCtRXEeCQTo3FoZ7g4AfnGiauYYiczvNxFCHicbB","Responses":[{"Addrs":["/ip4/0.0.0.0/tcp/0"],"ID":"${provider}"}],"Type":4}\n`, [
'Content-Type', 'application/json',
'X-Chunked-Output', '1'
])
nodeA.contentRouting.findProviders(cid, 1000, (err, response) => {
expect(err).to.not.exist()
expect(response).to.have.length(1)
expect(response[0].id.toB58String()).to.equal(provider)
expect(mockApi.isDone()).to.equal(true)
done()
})
})
it('should handle errors when finding providers', (done) => {
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
const mockApi = nock('http://0.0.0.0:60197')
.post('/api/v0/dht/findprovs')
.query({
arg: cid.toBaseEncodedString(),
timeout: '30000ms',
'stream-channels': true
})
.reply(502, 'Bad Gateway', [
'X-Chunked-Output', '1'
])
nodeA.contentRouting.findProviders(cid, (err) => {
expect(err).to.exist()
expect(mockApi.isDone()).to.equal(true)
done()
})
})
})
})
describe('via the dht and a delegate', () => {
let nodeA
let delegate
before((done) => {
waterfall([
(cb) => {
createPeerInfo(cb)
},
// Create the node using the delegate
(peerInfo, cb) => {
delegate = new DelegatedContentRouter(peerInfo.id, {
host: '0.0.0.0',
protocol: 'http',
port: 60197
}, [
ma('/ip4/0.0.0.0/tcp/60194')
])
nodeA = new Node({
peerInfo,
modules: {
contentRouting: [delegate]
},
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: false
}
}
}
})
nodeA.start(cb)
}
], done)
})
after((done) => nodeA.stop(done))
describe('provide', () => {
it('should use both the dht and delegate router to provide', (done) => {
const dhtStub = sinon.stub(nodeA._dht, 'provide').callsFake(() => {})
const delegateStub = sinon.stub(delegate, 'provide').callsFake(() => {
expect(dhtStub.calledOnce).to.equal(true)
expect(delegateStub.calledOnce).to.equal(true)
delegateStub.restore()
dhtStub.restore()
done()
})
nodeA.contentRouting.provide()
})
})
describe('findProviders', () => {
it('should only use the dht if it finds providers', (done) => {
const results = [true]
const dhtStub = sinon.stub(nodeA._dht, 'findProviders').callsArgWith(2, null, results)
const delegateStub = sinon.stub(delegate, 'findProviders').throws(() => {
return new Error('the delegate should not have been called')
})
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
expect(err).to.not.exist()
expect(results).to.equal(results)
expect(dhtStub.calledOnce).to.equal(true)
expect(delegateStub.notCalled).to.equal(true)
delegateStub.restore()
dhtStub.restore()
done()
})
})
it('should use the delegate if the dht fails to find providers', (done) => {
const results = [true]
const dhtStub = sinon.stub(nodeA._dht, 'findProviders').callsArgWith(2, null, [])
const delegateStub = sinon.stub(delegate, 'findProviders').callsArgWith(2, null, results)
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
expect(err).to.not.exist()
expect(results).to.deep.equal(results)
expect(dhtStub.calledOnce).to.equal(true)
expect(delegateStub.calledOnce).to.equal(true)
delegateStub.restore()
dhtStub.restore()
done()
})
})
})
})
describe('no routers', () => {
let nodeA
before((done) => {
createNode('/ip4/0.0.0.0/tcp/0', {
config: {
dht: {
enabled: false
}
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
done()
})
})
it('.findProviders should return an error with no options', (done) => {
nodeA.contentRouting.findProviders('a cid', (err) => {
expect(err).to.exist()
done()
})
})
it('.findProviders should return an error with options', (done) => {
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err) => {
expect(err).to.exist()
done()
})
})
})
})

View File

@ -1,143 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const series = require('async/series')
const createNode = require('./utils/create-node')
const sinon = require('sinon')
const { createLibp2p } = require('../src')
const WS = require('libp2p-websockets')
const PeerInfo = require('peer-info')
describe('libp2p creation', () => {
afterEach(() => {
sinon.restore()
})
it('should be able to start and stop successfully', (done) => {
createNode([], {
config: {
pubsub: {
enabled: true
},
dht: {
enabled: true
}
}
}, (err, node) => {
expect(err).to.not.exist()
const sw = node._switch
const cm = node.connectionManager
const dht = node._dht
const pub = node.pubsub
sinon.spy(sw, 'start')
sinon.spy(cm, 'start')
sinon.spy(dht, 'start')
sinon.spy(dht.randomWalk, 'start')
sinon.spy(pub, 'start')
sinon.spy(sw, 'stop')
sinon.spy(cm, 'stop')
sinon.spy(dht, 'stop')
sinon.spy(dht.randomWalk, 'stop')
sinon.spy(pub, 'stop')
sinon.spy(node, 'emit')
series([
(cb) => node.start(cb),
(cb) => {
expect(sw.start.calledOnce).to.equal(true)
expect(cm.start.calledOnce).to.equal(true)
expect(dht.start.calledOnce).to.equal(true)
expect(dht.randomWalk.start.calledOnce).to.equal(true)
expect(pub.start.calledOnce).to.equal(true)
expect(node.emit.calledWith('start')).to.equal(true)
cb()
},
(cb) => node.stop(cb)
], (err) => {
expect(err).to.not.exist()
expect(sw.stop.calledOnce).to.equal(true)
expect(cm.stop.calledOnce).to.equal(true)
expect(dht.stop.calledOnce).to.equal(true)
expect(dht.randomWalk.stop.called).to.equal(true)
expect(pub.stop.calledOnce).to.equal(true)
expect(node.emit.calledWith('stop')).to.equal(true)
done()
})
})
})
it('should not create disabled modules', (done) => {
createNode([], {
config: {
pubsub: {
enabled: false
}
}
}, (err, node) => {
expect(err).to.not.exist()
expect(node._pubsub).to.not.exist()
done()
})
})
it('should not throw errors from switch if node has no error listeners', (done) => {
createNode([], {}, (err, node) => {
expect(err).to.not.exist()
node._switch.emit('error', new Error('bad things'))
done()
})
})
it('should emit errors from switch if node has error listeners', (done) => {
const error = new Error('bad things')
createNode([], {}, (err, node) => {
expect(err).to.not.exist()
node.once('error', (err) => {
expect(err).to.eql(error)
done()
})
node._switch.emit('error', error)
})
})
it('createLibp2p should create a peerInfo instance', function (done) {
this.timeout(10e3)
createLibp2p({
modules: {
transport: [WS]
}
}, (err, libp2p) => {
expect(err).to.not.exist()
expect(libp2p).to.exist()
done()
})
})
it('createLibp2p should allow for a provided peerInfo instance', function (done) {
this.timeout(10e3)
PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist()
sinon.spy(PeerInfo, 'create')
createLibp2p({
peerInfo,
modules: {
transport: [WS]
}
}, (err, libp2p) => {
expect(err).to.not.exist()
expect(libp2p).to.exist()
expect(PeerInfo.create.callCount).to.eql(0)
done()
})
})
})
})

View File

@ -1,168 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const MemoryStore = require('interface-datastore').MemoryDatastore
const createNode = require('./utils/create-node')
describe('.dht', () => {
describe('enabled', () => {
let nodeA
const datastore = new MemoryStore()
before(function (done) {
createNode('/ip4/0.0.0.0/tcp/0', {
datastore
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
// Rewrite validators
nodeA._dht.validators.v = {
func (key, publicKey, callback) {
setImmediate(callback)
},
sign: false
}
// Rewrite selectors
nodeA._dht.selectors.v = () => 0
// Start
nodeA.start(done)
})
})
after((done) => {
nodeA.stop(done)
})
it('should be able to dht.put a value to the DHT', (done) => {
const key = Buffer.from('key')
const value = Buffer.from('value')
nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()
done()
})
})
it('should be able to dht.get a value from the DHT with options', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')
nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()
nodeA.dht.get(key, { maxTimeout: 3000 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.eql(value)
done()
})
})
})
it('should be able to dht.get a value from the DHT with no options defined', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')
nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()
nodeA.dht.get(key, (err, res) => {
expect(err).to.not.exist()
expect(res).to.eql(value)
done()
})
})
})
it('should be able to dht.getMany a value from the DHT with options', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')
nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()
nodeA.dht.getMany(key, 1, { maxTimeout: 3000 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
done()
})
})
})
it('should be able to dht.getMany a value from the DHT with no options defined', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')
nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()
nodeA.dht.getMany(key, 1, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
done()
})
})
})
})
describe('disabled', () => {
let nodeA
before(function (done) {
createNode('/ip4/0.0.0.0/tcp/0', {
config: {
dht: {
enabled: false
}
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
nodeA.start(done)
})
})
after((done) => {
nodeA.stop(done)
})
it('should receive an error on dht.put if the dht is disabled', (done) => {
const key = Buffer.from('key')
const value = Buffer.from('value')
nodeA.dht.put(key, value, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_DHT_DISABLED')
done()
})
})
it('should receive an error on dht.get if the dht is disabled', (done) => {
const key = Buffer.from('key')
nodeA.dht.get(key, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_DHT_DISABLED')
done()
})
})
it('should receive an error on dht.getMany if the dht is disabled', (done) => {
const key = Buffer.from('key')
nodeA.dht.getMany(key, 10, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_DHT_DISABLED')
done()
})
})
})
})

258
test/dialing/direct.node.js Normal file
View File

@ -0,0 +1,258 @@
'use strict'
/* eslint-env mocha */
const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const sinon = require('sinon')
const Transport = require('libp2p-tcp')
const Muxer = require('libp2p-mplex')
const Crypto = require('../../src/insecure/plaintext')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const delay = require('delay')
const pDefer = require('p-defer')
const pipe = require('it-pipe')
const Libp2p = require('../../src')
const Dialer = require('../../src/dialer')
const TransportManager = require('../../src/transport-manager')
const { codes: ErrorCodes } = require('../../src/errors')
const Protector = require('../../src/pnet')
const swarmKeyBuffer = Buffer.from(require('../fixtures/swarm.key'))
const mockUpgrader = require('../utils/mockUpgrader')
const Peers = require('../fixtures/peers')
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999/ws')
describe('Dialing (direct, TCP)', () => {
let remoteTM
let localTM
let remoteAddr
before(async () => {
remoteTM = new TransportManager({
libp2p: {},
upgrader: mockUpgrader
})
remoteTM.add(Transport.prototype[Symbol.toStringTag], Transport)
localTM = new TransportManager({
libp2p: {},
upgrader: mockUpgrader
})
localTM.add(Transport.prototype[Symbol.toStringTag], Transport)
await remoteTM.listen([listenAddr])
remoteAddr = remoteTM.getAddrs()[0]
})
after(() => remoteTM.close())
afterEach(() => {
sinon.restore()
})
it('should be able to connect to a remote node via its multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })
const connection = await dialer.connectToMultiaddr(remoteAddr)
expect(connection).to.exist()
await connection.close()
})
it('should be able to connect to a remote node via its stringified multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })
const connection = await dialer.connectToMultiaddr(remoteAddr.toString())
expect(connection).to.exist()
await connection.close()
})
it('should fail to connect to an unsupported multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })
try {
await dialer.connectToMultiaddr(unsupportedAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
return
}
expect.fail('Dial should have failed')
})
it('should be able to connect to a given peer', async () => {
const dialer = new Dialer({ transportManager: localTM })
const peerId = await PeerId.createFromJSON(Peers[0])
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(remoteAddr)
const connection = await dialer.connectToPeer(peerInfo)
expect(connection).to.exist()
await connection.close()
})
it('should fail to connect to a given peer with unsupported addresses', async () => {
const dialer = new Dialer({ transportManager: localTM })
const peerId = await PeerId.createFromJSON(Peers[0])
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(unsupportedAddr)
try {
await dialer.connectToPeer(peerInfo)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED)
return
}
expect.fail('Dial should have failed')
})
it('should abort dials on queue task timeout', async () => {
const dialer = new Dialer({
transportManager: localTM,
timeout: 50
})
sinon.stub(localTM, 'dial').callsFake(async (addr, options) => {
expect(options.signal).to.exist()
expect(options.signal.aborted).to.equal(false)
expect(addr.toString()).to.eql(remoteAddr.toString())
await delay(60)
expect(options.signal.aborted).to.equal(true)
})
try {
await dialer.connectToMultiaddr(remoteAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT)
return
}
expect.fail('Dial should have failed')
})
it('should dial to the max concurrency', async () => {
const dialer = new Dialer({
transportManager: localTM,
concurrency: 2
})
const deferredDial = pDefer()
sinon.stub(localTM, 'dial').callsFake(async () => {
await deferredDial.promise
})
// Add 3 dials
Promise.all([
dialer.connectToMultiaddr(remoteAddr),
dialer.connectToMultiaddr(remoteAddr),
dialer.connectToMultiaddr(remoteAddr)
])
// Let the call stack run
await delay(0)
// We should have 2 in progress, and 1 waiting
expect(localTM.dial.callCount).to.equal(2)
expect(dialer.queue.pending).to.equal(2)
expect(dialer.queue.size).to.equal(1)
deferredDial.resolve()
// Let the call stack run
await delay(0)
// All dials should have executed
expect(localTM.dial.callCount).to.equal(3)
expect(dialer.queue.pending).to.equal(0)
expect(dialer.queue.size).to.equal(0)
})
describe('libp2p.dialer', () => {
let peerInfo
let remotePeerInfo
let libp2p
let remoteLibp2p
let remoteAddr
before(async () => {
const [peerId, remotePeerId] = await Promise.all([
PeerId.createFromJSON(Peers[0]),
PeerId.createFromJSON(Peers[1])
])
peerInfo = new PeerInfo(peerId)
remotePeerInfo = new PeerInfo(remotePeerId)
remoteLibp2p = new Libp2p({
peerInfo: remotePeerInfo,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})
remoteLibp2p.handle('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
await remoteLibp2p.transportManager.listen([listenAddr])
remoteAddr = remoteLibp2p.transportManager.getAddrs()[0]
})
afterEach(async () => {
sinon.restore()
libp2p && await libp2p.stop()
libp2p = null
})
after(() => remoteLibp2p.stop())
it('should use the dialer for connecting', async () => {
libp2p = new Libp2p({
peerInfo,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})
sinon.spy(libp2p.dialer, 'connectToMultiaddr')
const connection = await libp2p.dial(remoteAddr)
expect(connection).to.exist()
const { stream, protocol } = await connection.newStream('/echo/1.0.0')
expect(stream).to.exist()
expect(protocol).to.equal('/echo/1.0.0')
await connection.close()
expect(libp2p.dialer.connectToMultiaddr.callCount).to.equal(1)
})
it('should use the protectors when provided for connecting', async () => {
const protector = new Protector(swarmKeyBuffer)
libp2p = new Libp2p({
peerInfo,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto],
connProtector: protector
}
})
sinon.spy(libp2p.upgrader.protector, 'protect')
sinon.stub(remoteLibp2p.upgrader, 'protector').value(new Protector(swarmKeyBuffer))
const connection = await libp2p.dialer.connectToMultiaddr(remoteAddr)
expect(connection).to.exist()
const { stream, protocol } = await connection.newStream('/echo/1.0.0')
expect(stream).to.exist()
expect(protocol).to.equal('/echo/1.0.0')
await connection.close()
expect(libp2p.upgrader.protector.protect.callCount).to.equal(1)
})
})
})

245
test/dialing/direct.spec.js Normal file
View File

@ -0,0 +1,245 @@
'use strict'
/* eslint-env mocha */
const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const sinon = require('sinon')
const pDefer = require('p-defer')
const delay = require('delay')
const Transport = require('libp2p-websockets')
const Muxer = require('libp2p-mplex')
const Crypto = require('../../src/insecure/plaintext')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const { codes: ErrorCodes } = require('../../src/errors')
const Constants = require('../../src/constants')
const Dialer = require('../../src/dialer')
const TransportManager = require('../../src/transport-manager')
const Libp2p = require('../../src')
const Peers = require('../fixtures/peers')
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
const mockUpgrader = require('../utils/mockUpgrader')
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999/ws')
const remoteAddr = MULTIADDRS_WEBSOCKETS[0]
describe('Dialing (direct, WebSockets)', () => {
let localTM
before(() => {
localTM = new TransportManager({
libp2p: {},
upgrader: mockUpgrader,
onConnection: () => {}
})
localTM.add(Transport.prototype[Symbol.toStringTag], Transport)
})
afterEach(() => {
sinon.restore()
})
it('should have appropriate defaults', () => {
const dialer = new Dialer({ transportManager: localTM })
expect(dialer.concurrency).to.equal(Constants.MAX_PARALLEL_DIALS)
expect(dialer.timeout).to.equal(Constants.DIAL_TIMEOUT)
})
it('should be able to connect to a remote node via its multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })
const connection = await dialer.connectToMultiaddr(remoteAddr)
expect(connection).to.exist()
await connection.close()
})
it('should be able to connect to a remote node via its stringified multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })
const connection = await dialer.connectToMultiaddr(remoteAddr.toString())
expect(connection).to.exist()
await connection.close()
})
it('should fail to connect to an unsupported multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })
try {
await dialer.connectToMultiaddr(unsupportedAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_DIAL_FAILED)
return
}
expect.fail('Dial should have failed')
})
it('should be able to connect to a given peer', async () => {
const dialer = new Dialer({ transportManager: localTM })
const peerId = await PeerId.createFromJSON(Peers[0])
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(remoteAddr)
const connection = await dialer.connectToPeer(peerInfo)
expect(connection).to.exist()
await connection.close()
})
it('should fail to connect to a given peer with unsupported addresses', async () => {
const dialer = new Dialer({ transportManager: localTM })
const peerId = await PeerId.createFromJSON(Peers[0])
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(unsupportedAddr)
try {
await dialer.connectToPeer(peerInfo)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED)
return
}
expect.fail('Dial should have failed')
})
it('should abort dials on queue task timeout', async () => {
const dialer = new Dialer({
transportManager: localTM,
timeout: 50
})
sinon.stub(localTM, 'dial').callsFake(async (addr, options) => {
expect(options.signal).to.exist()
expect(options.signal.aborted).to.equal(false)
expect(addr.toString()).to.eql(remoteAddr.toString())
await delay(60)
expect(options.signal.aborted).to.equal(true)
})
try {
await dialer.connectToMultiaddr(remoteAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT)
return
}
expect.fail('Dial should have failed')
})
it('should dial to the max concurrency', async () => {
const dialer = new Dialer({
transportManager: localTM,
concurrency: 2
})
const deferredDial = pDefer()
sinon.stub(localTM, 'dial').callsFake(async () => {
await deferredDial.promise
})
// Add 3 dials
Promise.all([
dialer.connectToMultiaddr(remoteAddr),
dialer.connectToMultiaddr(remoteAddr),
dialer.connectToMultiaddr(remoteAddr)
])
// Let the call stack run
await delay(0)
// We should have 2 in progress, and 1 waiting
expect(localTM.dial.callCount).to.equal(2)
expect(dialer.queue.pending).to.equal(2)
expect(dialer.queue.size).to.equal(1)
deferredDial.resolve()
// Let the call stack run
await delay(0)
// All dials should have executed
expect(localTM.dial.callCount).to.equal(3)
expect(dialer.queue.pending).to.equal(0)
expect(dialer.queue.size).to.equal(0)
})
describe('libp2p.dialer', () => {
let peerInfo
let libp2p
let remoteLibp2p
before(async () => {
const peerId = await PeerId.createFromJSON(Peers[0])
peerInfo = new PeerInfo(peerId)
})
afterEach(async () => {
sinon.restore()
libp2p && await libp2p.stop()
libp2p = null
})
after(async () => {
remoteLibp2p && await remoteLibp2p.stop()
})
it('should create a dialer', () => {
libp2p = new Libp2p({
peerInfo,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})
expect(libp2p.dialer).to.exist()
// Ensure the dialer also has the transport manager
expect(libp2p.transportManager).to.equal(libp2p.dialer.transportManager)
})
it('should use the dialer for connecting', async () => {
libp2p = new Libp2p({
peerInfo,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})
sinon.spy(libp2p.dialer, 'connectToMultiaddr')
const connection = await libp2p.dialer.connectToMultiaddr(remoteAddr)
expect(connection).to.exist()
const { stream, protocol } = await connection.newStream('/echo/1.0.0')
expect(stream).to.exist()
expect(protocol).to.equal('/echo/1.0.0')
await connection.close()
expect(libp2p.dialer.connectToMultiaddr.callCount).to.equal(1)
})
it('should run identify automatically after connecting', async () => {
libp2p = new Libp2p({
peerInfo,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})
sinon.spy(libp2p.dialer.identifyService, 'identify')
sinon.spy(libp2p.peerStore, 'update')
const connection = await libp2p.dialer.connectToMultiaddr(remoteAddr)
expect(connection).to.exist()
// Wait for setImmediate to trigger the identify call
await delay(1)
expect(libp2p.dialer.identifyService.identify.callCount).to.equal(1)
await libp2p.dialer.identifyService.identify.firstCall.returnValue
expect(libp2p.peerStore.update.callCount).to.equal(1)
})
})
})

7
test/fixtures/browser.js vendored Normal file
View File

@ -0,0 +1,7 @@
'use strict'
const multiaddr = require('multiaddr')
module.exports.MULTIADDRS_WEBSOCKETS = [
multiaddr('/ip4/127.0.0.1/tcp/15001/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')
]

27
test/fixtures/peers.js vendored Normal file
View File

@ -0,0 +1,27 @@
'use strict'
module.exports = [{
id: 'QmNMMAqSxPetRS1cVMmutW5BCN1qQQyEr4u98kUvZjcfEw',
privKey: 'CAASpQkwggShAgEAAoIBAQDPek2aeHMa0blL42RTKd6xgtkk4Zkldvq4LHxzcag5uXepiQzWANEUvoD3KcUTmMRmx14PvsxdLCNst7S2JSa0R2n5wSRs14zGy6892lx4H4tLBD1KSpQlJ6vabYM1CJhIQRG90BtzDPrJ/X1iJ2HA0PPDz0Mflam2QUMDDrU0IuV2m7gSCJ5r4EmMs3U0xnH/1gShkVx4ir0WUdoWf5KQUJOmLn1clTRHYPv4KL9A/E38+imNAXfkH3c2T7DrCcYRkZSpK+WecjMsH1dCX15hhhggNqfp3iulO1tGPxHjm7PDGTPUjpCWKpD5e50sLqsUwexac1ja6ktMfszIR+FPAgMBAAECggEAB2H2uPRoRCAKU+T3gO4QeoiJaYKNjIO7UCplE0aMEeHDnEjAKC1HQ1G0DRdzZ8sb0fxuIGlNpFMZv5iZ2ZFg2zFfV//DaAwTek9tIOpQOAYHUtgHxkj5FIlg2BjlflGb+ZY3J2XsVB+2HNHkUEXOeKn2wpTxcoJE07NmywkO8Zfr1OL5oPxOPlRN1gI4ffYH2LbfaQVtRhwONR2+fs5ISfubk5iKso6BX4moMYkxubYwZbpucvKKi/rIjUA3SK86wdCUnno1KbDfdXSgCiUlvxt/IbRFXFURQoTV6BOi3sP5crBLw8OiVubMr9/8WE6KzJ0R7hPd5+eeWvYiYnWj4QKBgQD6jRlAFo/MgPO5NZ/HRAk6LUG+fdEWexA+GGV7CwJI61W/Dpbn9ZswPDhRJKo3rquyDFVZPdd7+RlXYg1wpmp1k54z++L1srsgj72vlg4I8wkZ4YLBg0+zVgHlQ0kxnp16DvQdOgiRFvMUUMEgetsoIx1CQWTd67hTExGsW+WAZQKBgQDT/WaHWvwyq9oaZ8G7F/tfeuXvNTk3HIJdfbWGgRXB7lJ7Gf6FsX4x7PeERfL5a67JLV6JdiLLVuYC2CBhipqLqC2DB962aKMvxobQpSljBBZvZyqP1IGPoKskrSo+2mqpYkeCLbDMuJ1nujgMP7gqVjabs2zj6ACKmmpYH/oNowJ/T0ZVtvFsjkg+1VsiMupUARRQuPUWMwa9HOibM1NIZcoQV2NGXB5Z++kR6JqxQO0DZlKArrviclderUdY+UuuY4VRiSEprpPeoW7ZlbTku/Ap8QZpWNEzZorQDro7bnfBW91fX9/81ets/gCPGrfEn+58U3pdb9oleCOQc/ifpQKBgBTYGbi9bYbd9vgZs6bd2M2um+VFanbMytS+g5bSIn2LHXkVOT2UEkB+eGf9KML1n54QY/dIMmukA8HL1oNAyalpw+/aWj+9Ui5kauUhGEywHjSeBEVYM9UXizxz+m9rsoktLLLUI0o97NxCJzitG0Kub3gn0FEogsUeIc7AdinZAoGBANnM1vcteSQDs7x94TDEnvvqwSkA2UWyLidD2jXgE0PG4V6tTkK//QPBmC9eq6TIqXkzYlsErSw4XeKO91knFofmdBzzVh/ddgx/NufJV4tXF+a2iTpqYBUJiz9wpIKgf43/Ob+P1EA99GAhSdxz1ess9O2aTqf3ANzn6v6g62Pv',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDPek2aeHMa0blL42RTKd6xgtkk4Zkldvq4LHxzcag5uXepiQzWANEUvoD3KcUTmMRmx14PvsxdLCNst7S2JSa0R2n5wSRs14zGy6892lx4H4tLBD1KSpQlJ6vabYM1CJhIQRG90BtzDPrJ/X1iJ2HA0PPDz0Mflam2QUMDDrU0IuV2m7gSCJ5r4EmMs3U0xnH/1gShkVx4ir0WUdoWf5KQUJOmLn1clTRHYPv4KL9A/E38+imNAXfkH3c2T7DrCcYRkZSpK+WecjMsH1dCX15hhhggNqfp3iulO1tGPxHjm7PDGTPUjpCWKpD5e50sLqsUwexac1ja6ktMfszIR+FPAgMBAAE='
}, {
id: 'QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t',
privKey: 'CAASpwkwggSjAgEAAoIBAQCTU3gVDv3SRXLOsFln9GEf1nJ/uCEDhOG10eC0H9l9IPpVxjuPT1ep+ykFUdvefq3D3q+W3hbmiHm81o8dYv26RxZIEioToUWp7Ec5M2B/niYoE93za9/ZDwJdl7eh2hNKwAdxTmdbXUPjkIU4vLyHKRFbJIn9X8w9djldz8hoUvC1BK4L1XrT6F2l0ruJXErH2ZwI1youfSzo87TdXIoFKdrQLuW6hOtDCGKTiS+ab/DkMODc6zl8N47Oczv7vjzoWOJMUJs1Pg0ZsD1zmISY38P0y/QyEhatZn0B8BmSWxlLQuukatzOepQI6k+HtfyAAjn4UEqnMaXTP1uwLldVAgMBAAECggEAHq2f8MqpYjLiAFZKl9IUs3uFZkEiZsgx9BmbMAb91Aec+WWJG4OLHrNVTG1KWp+IcaQablEa9bBvoToQnS7y5OpOon1d066egg7Ymfmv24NEMM5KRpktCNcOSA0CySpPIB6yrg6EiUr3ixiaFUGABKkxmwgVz/Q15IqM0ZMmCUsC174PMAz1COFZxD0ZX0zgHblOJQW3dc0X3XSzhht8vU02SMoVObQHQfeXEHv3K/RiVj/Ax0bTc5JVkT8dm8xksTtsFCNOzRBqFS6MYqX6U/u0Onz3Jm5Jt7fLWb5n97gZR4SleyGrqxYNb46d9X7mP0ie7E6bzFW0DsWBIeAqVQKBgQDW0We2L1n44yOvJaMs3evpj0nps13jWidt2I3RlZXjWzWHiYQfvhWUWqps/xZBnAYgnN/38xbKzHZeRNhrqOo+VB0WK1IYl0lZVE4l6TNKCsLsUfQzsb1pePkd1eRZA+TSqsi+I/IOQlQU7HA0bMrah/5FYyUBP0jYvCOvYTlZuwKBgQCvkcVRydVlzjUgv7lY5lYvT8IHV5iYO4Qkk2q6Wjv9VUKAJZauurMdiy05PboWfs5kbETdwFybXMBcknIvZO4ihxmwL8mcoNwDVZHI4bXapIKMTCyHgUKvJ9SeTcKGC7ZuQJ8mslRmYox/HloTOXEJgQgPRxXcwa3amzvdZI+6LwKBgQCLsnQqgxKUi0m6bdR2qf7vzTH4258z6X34rjpT0F5AEyF1edVFOz0XU/q+lQhpNEi7zqjLuvbYfSyA026WXKuwSsz7jMJ/oWqev/duKgAjp2npesY/E9gkjfobD+zGgoS9BzkyhXe1FCdP0A6L2S/1+zg88WOwMvJxl6/xLl24XwKBgCm60xSajX8yIQyUpWBM9yUtpueJ2Xotgz4ST+bVNbcEAddll8gWFiaqgug9FLLuFu5lkYTHiPtgc1RNdphvO+62/9MRuLDixwh/2TPO+iNqwKDKJjda8Nei9vVddCPaOtU/xNQ0xLzFJbG9LBmvqH9izOCcu8SJwGHaTcNUeJj/AoGADCJ26cY30c13F/8awAAmFYpZWCuTP5ppTsRmjd63ixlrqgkeLGpJ7kYb5fXkcTycRGYgP0e1kssBGcmE7DuG955fx3ZJESX3GQZ+XfMHvYGONwF1EiK1f0p6+GReC2VlQ7PIkoD9o0hojM6SnWvv9EXNjCPALEbfPFFvcniKVsE=',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCTU3gVDv3SRXLOsFln9GEf1nJ/uCEDhOG10eC0H9l9IPpVxjuPT1ep+ykFUdvefq3D3q+W3hbmiHm81o8dYv26RxZIEioToUWp7Ec5M2B/niYoE93za9/ZDwJdl7eh2hNKwAdxTmdbXUPjkIU4vLyHKRFbJIn9X8w9djldz8hoUvC1BK4L1XrT6F2l0ruJXErH2ZwI1youfSzo87TdXIoFKdrQLuW6hOtDCGKTiS+ab/DkMODc6zl8N47Oczv7vjzoWOJMUJs1Pg0ZsD1zmISY38P0y/QyEhatZn0B8BmSWxlLQuukatzOepQI6k+HtfyAAjn4UEqnMaXTP1uwLldVAgMBAAE='
}, {
id: 'QmZqCdSzgpsmB3Qweb9s4fojAoqELWzqku21UVrqtVSKi4',
privKey: 'CAASpgkwggSiAgEAAoIBAQCdbSEsTmw7lp5HagRcx57DaLiSUEkh4iBcKc7Y+jHICEIA8NIVi9FlfGEZj9G21FpiTR4Cy+BLVEuf8Nm90bym4iV+cSumeS21fvD8xGTEbeKGljs6OYHy3M45JhWF85gqHQJOqZufI2NRDuRgMZEO2+qGEXmSlv9mMXba/+9ecze8nSpB7bG2Z2pnKDeYwhF9Cz+ElMyn7TBWDjJERGVgFbTpdM3rBnbhB/TGpvs732QqZmIBlxnDb/Jn0l1gNZCgkEDcJ/0NDMBJTQ8vbvcdmaw3eaMPLkn1ix4wdu9QWCA0IBtuY1R7vSUtf4irnLJG7DnAw2GfM5QrF3xF1GLXAgMBAAECggEAQ1N0qHoxl5pmvqv8iaFlqLSUmx5y6GbI6CGJMQpvV9kQQU68yjItr3VuIXx8d/CBZyEMAK4oko7OeOyMcr3MLKLy3gyQWnXgsopDjhZ/8fH8uwps8g2+IZuFJrO+6LaxEPGvFu06fOiphPUVfn40R2KN/iBjGeox+AaXijmCqaV2vEdNJJPpMfz6VKZBDLTrbiqvo/3GN1U99PUqfPWpOWR29oAhh/Au6blSqvqTUPXB2+D/X6e1JXv31mxMPK68atDHSUjZWKB9lE4FMK1bkSKJRbyXmNIlbZ9V8X4/0r8/6T7JnW7ZT8ugRkquohmwgG7KkDXB1YsOCKXYUqzVYQKBgQDtnopFXWYl7XUyePJ/2MA5i7eoko9jmF44L31irqmHc5unNf6JlNBjlxTNx3WyfzhUzrn3c18psnGkqtow0tkBj5hmqn8/WaPbc5UA/5R1FNaNf8W5khn7MDm6KtYRPjN9djqTDiVHyC6ljONYd+5S+MqyKVWZ3t/xvG60sw85qwKBgQCpmpDtL+2JBwkfeUr3LyDcQxvbfzcv8lXj2otopWxWiLiZF1HzcqgAa2CIwu9kCGEt9Zr+9E4uINbe1To0b01/FhvR6xKO/ukceGA/mBB3vsKDcRmvpBUp+3SmnhY0nOk+ArQl4DhJ34k8pDM3EDPrixPf8SfVdU/8IM32lsdHhQKBgHLgpvCKCwxjFLnmBzcPzz8C8TOqR3BbBZIcQ34l+wflOGdKj1hsfaLoM8KYn6pAHzfBCd88A9Hg11hI0VuxVACRL5jS7NnvuGwsIOluppNEE8Ys86aXn7/0vLPoab3EWJhbRE48FIHzobmft3nZ4XpzlWs02JGfUp1IAC2UM9QpAoGAeWy3pZhSr2/iEC5+hUmwdQF2yEbj8+fDpkWo2VrVnX506uXPPkQwE1zM2Bz31t5I9OaJ+U5fSpcoPpDaAwBMs1fYwwlRWB8YNdHY1q6/23svN3uZsC4BGPV2JnO34iMUudilsRg+NGVdk5TbNejbwx7nM8Urh59djFzQGGMKeSECgYA0QMCARPpdMY50Mf2xQaCP7HfMJhESSPaBq9V3xY6ToEOEnXgAR5pNjnU85wnspHp+82r5XrKfEQlFxGpj2YA4DRRmn239sjDa29qP42UNAFg1+C3OvXTht1d5oOabaGhU0udwKmkEKUbb0bG5xPQJ5qeSJ5T1gLzLk3SIP0GlSw==',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCdbSEsTmw7lp5HagRcx57DaLiSUEkh4iBcKc7Y+jHICEIA8NIVi9FlfGEZj9G21FpiTR4Cy+BLVEuf8Nm90bym4iV+cSumeS21fvD8xGTEbeKGljs6OYHy3M45JhWF85gqHQJOqZufI2NRDuRgMZEO2+qGEXmSlv9mMXba/+9ecze8nSpB7bG2Z2pnKDeYwhF9Cz+ElMyn7TBWDjJERGVgFbTpdM3rBnbhB/TGpvs732QqZmIBlxnDb/Jn0l1gNZCgkEDcJ/0NDMBJTQ8vbvcdmaw3eaMPLkn1ix4wdu9QWCA0IBtuY1R7vSUtf4irnLJG7DnAw2GfM5QrF3xF1GLXAgMBAAE='
}, {
id: 'QmR5VwgsL7jyfZHAGyp66tguVrQhCRQuRc3NokocsCZ3fA',
privKey: 'CAASpwkwggSjAgEAAoIBAQCGXYU+uc2nn1zuJhfdFOl34upztnrD1gpHu58ousgHdGlGgYgbqLBAvIAauXdEL0+e30HofjA634SQxE+9nV+0FQBam1DDzHQlXsuwHV+2SKvSDkk4bVllMFpu2SJtts6VH+OXC/2ANJOm+eTALykQPYXgLIBxrhp/eD+Jz5r6wW2nq3k6OmYyK/4pgGzFjo5UyX+fa/171AJ68UPboFpDy6BZCcUjS0ondxPvD7cv5jMNqqMKIB/7rpi8n+Q3oeccRqVL56wH+FE3/QLjwYHwY6ILNRyvNXRqHjwBEXB2R5moXN0AFUWTw9rt3KhFiEjR1U81BTw5/xS7W2Iu0FgZAgMBAAECggEAS64HK8JZfE09eYGJNWPe8ECmD1C7quw21BpwVe+GVPSTizvQHswPohbKDMNj0srXDMPxCnNw1OgqcaOwyjsGuZaOoXoTroTM8nOHRIX27+PUqzaStS6aCG2IsiCozKUHjGTuupftS7XRaF4eIsUtWtFcQ1ytZ9pJYHypRQTi5NMSrTze5ThjnWxtHilK7gnBXik+aR0mYEVfSn13czQEC4rMOs+b9RAc/iibDNoLopfIdvmCCvfxzmySnR7Cu1iSUAONkir7PB+2Mt/qRFCH6P+jMamtCgQ8AmifXgVmDUlun+4MnKg3KrPd6ZjOEKhVe9mCHtGozk65RDREShfDdQKBgQDi+x2MuRa9peEMOHnOyXTS+v+MFcfmG0InsO08rFNBKZChLB+c9UHBdIvexpfBHigSyERfuDye4z6lxi8ZnierWMYJP30nxmrnxwTGTk1MQquhfs1A0kpmDnPsjlOS/drEIEIssNx2WbfJ7YtMxLWBtp+BJzGpQmr0LKC+NHRSrwKBgQCXiy2kJESIUkIs2ihV55hhT6/bZo1B1O5DPA2nkjOBXqXF6fvijzMDX82JjLd07lQZlI0n1Q/Hw0p4iYi9YVd2bLkLXF5UIb2qOeHj76enVFOrPHUSkC9Y2g/0Xs+60Ths2xRd8RrrfQU3kl5iVpBywkCIrb2M5+wRnNTk1W3TtwKBgQCvplyrteAfSurpJhs9JzE8w/hWU9SqAZYkWQp91W1oE95Um2yrbjBAoQxMjaqKS+f/APPIjy56Vqj4aHGyhW11b/Fw3qzfxvCcBKtxOs8eoMlo5FO6QgJJEA4tlcafDcvp0nzjUMqK28safLU7503+33B35fjMXxWdd5u9FaKfCQKBgC4W6j6tuRosymuRvgrCcRnHfpify/5loEFallyMnpWOD6Tt0OnK25z/GifnYDRz96gAAh5HMpFy18dpLOlMHamqz2yhHx8/U8vd5tHIJZlCkF/X91M5/uxrBccwvsT2tM6Got8fYSyVzWxlW8dUxIHiinYHQUsFjkqdBDLEpq5pAoGASoTw5RBEWFM0GuAZdXsyNyxU+4S+grkTS7WdW/Ymkukh+bJZbnvF9a6MkSehqXnknthmufonds2AFNS//63gixENsoOhzT5+2cdfc6tJECvJ9xXVXkf85AoQ6T/RrXF0W4m9yQyCngNJUrKUOIH3oDIfdZITlYzOC3u1ojj7VuQ=',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCGXYU+uc2nn1zuJhfdFOl34upztnrD1gpHu58ousgHdGlGgYgbqLBAvIAauXdEL0+e30HofjA634SQxE+9nV+0FQBam1DDzHQlXsuwHV+2SKvSDkk4bVllMFpu2SJtts6VH+OXC/2ANJOm+eTALykQPYXgLIBxrhp/eD+Jz5r6wW2nq3k6OmYyK/4pgGzFjo5UyX+fa/171AJ68UPboFpDy6BZCcUjS0ondxPvD7cv5jMNqqMKIB/7rpi8n+Q3oeccRqVL56wH+FE3/QLjwYHwY6ILNRyvNXRqHjwBEXB2R5moXN0AFUWTw9rt3KhFiEjR1U81BTw5/xS7W2Iu0FgZAgMBAAE='
}, {
id: 'QmScLDqRg7H6ipCYxm9fVk152UWavQFKscTdoT4YNHxgqp',
privKey: 'CAASpwkwggSjAgEAAoIBAQCWEHaTZ6LBLFP5OPrUqjDM/cF4b2zrfh1Zm3kd02ZtgQB3iYtZqRPJT5ctT3A7WdVF/7dCxPGOCkJlLekTx4Y4gD8JtjA+EfN9fR/2RBKbti2N3CD4vkGp9ss4hbBFcXIhl8zuD/ELHutbV6b8b4QXJGnxfp/B+1kNPnyd7SJznS0QyvI8OLI1nAkVKdYLDRW8kPKeHyx1xhdNDuTQVTFyAjRGQ4e3UYFB7bYIHW3E6kCtCoJDlj+JPC02Yt1LHzIzZVLvPvNFnYY2mag6OiGFuh/oMBIqvnPc1zRZ3eLUqeGZjQVaoR0kdgZUKz7Q2TBeNldxK/s6XO0DnkQTlelNAgMBAAECggEAdmt1dyswR2p4tdIeNpY7Pnj9JNIhTNDPznefI0dArCdBvBMhkVaYk6MoNIxcj6l7YOrDroAF8sXr0TZimMY6B/pERKCt/z1hPWTxRQBBAvnHhwvwRPq2jK6BfhAZoyM8IoBNKowP9mum5QUNdGV4Al8s73KyFX0IsCfgZSvNpRdlt+DzPh+hu/CyoZaMpRchJc1UmK8Fyk3KfO+m0DZNfHP5P08lXNfM6MZLgTJVVgERHyG+vBOzTd2RElMe19nVCzHwb3dPPRZSQ7Fnz3rA+GeLqsM2Zi4HNhfbD1OcD9C4wDj5tYL6hWTkdz4IlfVcjCeUHxgIOhdDV2K+OwbuAQKBgQD0FjUZ09UW2FQ/fitbvIB5f1SkXWPxTF9l6mAeuXhoGv2EtQUO4vq/PK6N08RjrZdWQy6UsqHgffi7lVQ8o3hvCKdbtf4sP+cM92OrY0WZV89os79ndj4tyvmnP8WojwRjt/2XEfgdoWcgWxW9DiYINTOQVimZX+X/3on4s8hEgQKBgQCdY3kOMbyQeLTRkqHXjVTY4ddO+v4S4wOUa1l4rTqAbq1W3JYWwoDQgFuIu3limIHmjnSJpCD4EioXFsM7p6csenoc20sHxsaHnJ6Mn5Te41UYmY9EW0otkQ0C3KbXM0hwQkjyplnEmZawGKmjEHW8DJ3vRYTv9TUCgYKxDHgOzQKBgB4A/NYH7BG61eBYKgxEx6YnuMfbkwV+Vdu5S8d7FQn3B2LgvZZu4FPRqcNVXLbEB+5ao8czjiKCWaj1Wj15+rvrXGcxn+Tglg5J+r5+nXeUC7LbJZQaPNp0MOwWMr3dlrSLUWjYlJ9Pz9VyXOG4c4Rexc/gR4zK9QLW4C7qKpwBAoGAZzyUb0cYlPtYQA+asTU3bnvVKy1f8yuNcZFowst+EDiI4u0WVh+HNzy6zdmLKa03p+/RaWeLaK0hhrubnEnAUmCUMNF3ScaM+u804LDcicc8TkKLwx7ObU0z56isl4RAA8K27tNHFrpYKXJD834cfBkaj5ReOrfw6Y/iFhhDuBECgYEA8gbC76uz7LSHhW30DSRTcqOzTyoe2oYKQaxuxYNp7vSSOkcdRen+mrdflDvud2q/zN2QdL4pgqdldHlR35M/lJ0f0B6zp74jlzbO9700wzsOqreezGc5eWiroDL100U9uIZ50BKb8CKtixIHpinUSPIUcVDkSAZ2y7mbfCxQwqQ=',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCWEHaTZ6LBLFP5OPrUqjDM/cF4b2zrfh1Zm3kd02ZtgQB3iYtZqRPJT5ctT3A7WdVF/7dCxPGOCkJlLekTx4Y4gD8JtjA+EfN9fR/2RBKbti2N3CD4vkGp9ss4hbBFcXIhl8zuD/ELHutbV6b8b4QXJGnxfp/B+1kNPnyd7SJznS0QyvI8OLI1nAkVKdYLDRW8kPKeHyx1xhdNDuTQVTFyAjRGQ4e3UYFB7bYIHW3E6kCtCoJDlj+JPC02Yt1LHzIzZVLvPvNFnYY2mag6OiGFuh/oMBIqvnPc1zRZ3eLUqeGZjQVaoR0kdgZUKz7Q2TBeNldxK/s6XO0DnkQTlelNAgMBAAE='
}, {
id: 'QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN',
privKey: 'CAASpwkwggSjAgEAAoIBAQC1/GFud/7xutux7qRfMj1sIdMRh99/chR6HqVj6LQqrgk4jil0mdN/LCk/tqPqmDtObHdmEhCoybzuhLbCKgUqryKDwO6yBJHSKWY9QqrKZtLJ37SgKwGjE3+NUD4r1dJHhtQrICFdOdSCBzs/v8gi+J+KZLHo7+Nms4z09ysy7qZh94Pd7cW4gmSMergqUeANLD9C0ERw1NXolswOW7Bi7UGr7yuBxejICLO3nkxe0OtpQBrYrqdCD9vs3t/HQZbPWVoiRj4VO7fxkAPKLl30HzcIfxj/ayg8NHcH59d08D+N2v5Sdh28gsiYKIPE9CXvuw//HUY2WVRY5fDC5JglAgMBAAECggEBAKb5aN/1w3pBqz/HqRMbQpYLNuD33M3PexBNPAy+P0iFpDo63bh5Rz+A4lvuFNmzUX70MFz7qENlzi6+n/zolxMB29YtWBUH8k904rTEjXXl//NviQgITZk106tx+4k2x5gPEm57LYGfBOdFAUzNhzDnE2LkXwRNzkS161f7zKwOEsaGWRscj6UvhO4MIFxjb32CVwt5eK4yOVqtyMs9u30K4Og+AZYTlhtm+bHg6ndCCBO6CQurCQ3jD6YOkT+L3MotKqt1kORpvzIB0ujZRf49Um8wlcjC5G9aexBeGriXaVdPF62zm7GA7RMsbQM/6aRbA1fEQXvJhHUNF9UFeaECgYEA8wCjKqQA7UQnHjRwTsktdwG6szfxd7z+5MTqHHTWhWzgcQLgdh5/dO/zanEoOThadMk5C1Bqjq96gH2xim8dg5XQofSVtV3Ui0dDa+XRB3E3fyY4D3RF5hHv85O0GcvQc6DIb+Ja1oOhvHowFB1C+CT3yEgwzX/EK9xpe+KtYAkCgYEAv7hCnj/DcZFU3fAfS+unBLuVoVJT/drxv66P686s7J8UM6tW+39yDBZ1IcwY9vHFepBvxY2fFfEeLI02QFM+lZXVhNGzFkP90agNHK01psGgrmIufl9zAo8WOKgkLgbYbSHzkkDeqyjEPU+B0QSsZOCE+qLCHSdsnTmo/TjQhj0CgYAz1+j3yfGgrS+jVBC53lXi0+2fGspbf2jqKdDArXSvFqFzuudki/EpY6AND4NDYfB6hguzjD6PnoSGMUrVfAtR7X6LbwEZpqEX7eZGeMt1yQPMDr1bHrVi9mS5FMQR1NfuM1lP9Xzn00GIUpE7WVrWUhzDEBPJY/7YVLf0hFH08QKBgDWBRQZJIVBmkNrHktRrVddaSq4U/d/Q5LrsCrpymYwH8WliHgpeTQPWmKXwAd+ZJdXIzYjCt202N4eTeVqGYOb6Q/anV2WVYBbM4avpIxoA28kPGY6nML+8EyWIt2ApBOmgGgvtEreNzwaVU9NzjHEyv6n7FlVwlT1jxCe3XWq5AoGASYPKQoPeDlW+NmRG7z9EJXJRPVtmLL40fmGgtju9QIjLnjuK8XaczjAWT+ySI93Whu+Eujf2Uj7Q+NfUjvAEzJgwzuOd3jlQvoALq11kuaxlNQTn7rx0A1QhBgUJE8AkvShPC9FEnA4j/CLJU0re9H/8VvyN6qE0Mho0+YbjpP8=',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC1/GFud/7xutux7qRfMj1sIdMRh99/chR6HqVj6LQqrgk4jil0mdN/LCk/tqPqmDtObHdmEhCoybzuhLbCKgUqryKDwO6yBJHSKWY9QqrKZtLJ37SgKwGjE3+NUD4r1dJHhtQrICFdOdSCBzs/v8gi+J+KZLHo7+Nms4z09ysy7qZh94Pd7cW4gmSMergqUeANLD9C0ERw1NXolswOW7Bi7UGr7yuBxejICLO3nkxe0OtpQBrYrqdCD9vs3t/HQZbPWVoiRj4VO7fxkAPKLl30HzcIfxj/ayg8NHcH59d08D+N2v5Sdh28gsiYKIPE9CXvuw//HUY2WVRY5fDC5JglAgMBAAE='
}]

5
test/fixtures/swarm.key.js vendored Normal file
View File

@ -0,0 +1,5 @@
'use strict'
module.exports = '/key/swarm/psk/1.0.0/\n' +
'/base16/\n' +
'411f0a244cbbc25ecbb2b070d00a1832516ded521eb3ee3aa13189efe2e2b9a2'

View File

@ -1,5 +0,0 @@
{
"id": "QmaG17D4kfTB2RNUCr16bSfVvUVt2Xn3rPYeqQDvnVcXFr",
"privKey": "CAASqAkwggSkAgEAAoIBAQDBpXRrSLoVhP8C4YI0nm+YTb7UIe+xT9dwaMzKcGsH2zzz1lfxl54e1XNO+6Ut+If5jswpydgHhn9nGPod53sUIR2m+BiHOAH/Blgfa1nUKUkspts1MH3z5ZaO6Xo336Y0Uaw7UqfeIzKliTM6bpev2XIHyu0v/VJ2mylzfbDLMWqZs/shE3xwCJCD/PxoVpTlr/SzzF7MdgDMxkyiC3iLZ5Zkm+baPbi3mpKM0ue25Thedcc0KFjhQrjBfy5FPamrsMn5fnnoHwnQl9u7UWigzeC+7X+38EML1sVrV37ExxHPtM6882Ivjc7VN6zFHOHD2c9eVfbShkIf8YkVQUcFAgMBAAECggEAVE1mgGo58LJknml0WNn8tS5rfEiF5AhhPyOwvBTy04nDYFgZEykxgjTkrSbqgzfmYmOjSDICJUyNXGHISYqDz4CXOyBY9U0RuWeWp58BjVan75N4bRB+VNbHk9HbDkYEQlSoCW9ze0aRfvVa4v5QdRLSDMhwN+stokrsYcX/WIWYTM2e2jW+qQOzS8SJl7wYsgtd3WikrxwXkRL3sCMHEcgcPhoKacoD5Yr9cB0IC5vzhu4t/WMa+N2UEndcKGAbXsh8kA7BPFM6lqnEpOHpWEVEAYasAwFGUvUN9GwhtqpaNNS2sG6Nrz95cC99Nqx58uIXcTAJm3Fh/WfKJ6I1xQKBgQD+g7A5OSWw+i/zhTKVPJg93/eohViL0dGZT9Tf0/VslsFl00FwnZmBKA6BJ6ZL3hD00OcqIL3m6EzZ4q38U97XZxf2OUsPPJtl+Avqtlk16AHRHB9I17LGXJ30xZRkxL665oLms0D2T4NIZZX/uVMoS18lRvCZj1aEYQFCrZYgowKBgQDCxtA695S0vl6E3Q4G6MrDZK+2JqjaGL0XTnpHWiAjnk2lnV2CCZnWpEHT+ebF2fWx5nYQo5sugc6bS+4k9jRNUgxf2sQieZYCBjbnjCEVrPTm/dPTkaw1CQ/ox5/R1/Elbw8vteF9uUAvR0FL8Ss1Dqw6B2SxdTowxMy6qQ7sNwKBgG2N3eMj2DeP2egm45kdliK8L2yYyX6V+HTXyjf2kuQFGIZuIvMIw7S2u1eY65ooon/fFEIsCdJFGB+J1X6R05BAzi2sh8StP+7qkKadi1UK4w1R352JS2jbIRrlmXSuw7LL2njXnBTqMQaOw7xp14O2vePb32EaNBGTd+ltsvulAoGBALGIc4370oA4MIDb2Ag2MXKNmJbnf+piuB/BOTVGEZtFlDKLUArR43W/+/xRgKX/97FyhVS/OxfV21Kzj9oCy0NasMrB5RojRraLoYnFsPZH0mWlIGlsEtG4c9bR9XtYX4WmR+pN1r04mCc/xGWK6b4PpK2zxXT2i9ad2pmctGxbAoGBAIcp0UML5QCqvLmcob2/6PCRaYAxJBb9lDqOHredMgQih2hGnHFCyKk9eBAbFf/KN0guJTBDaAJRclcxsLLn7rV6grMNt+0EUepm7tWT0z5j8gNGbGGhuGDdqcmfJTc2EMdQrfhzYDN3rL1v3l+Ujwla2khL2ozE7SQ/KVeA1saY",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBpXRrSLoVhP8C4YI0nm+YTb7UIe+xT9dwaMzKcGsH2zzz1lfxl54e1XNO+6Ut+If5jswpydgHhn9nGPod53sUIR2m+BiHOAH/Blgfa1nUKUkspts1MH3z5ZaO6Xo336Y0Uaw7UqfeIzKliTM6bpev2XIHyu0v/VJ2mylzfbDLMWqZs/shE3xwCJCD/PxoVpTlr/SzzF7MdgDMxkyiC3iLZ5Zkm+baPbi3mpKM0ue25Thedcc0KFjhQrjBfy5FPamrsMn5fnnoHwnQl9u7UWigzeC+7X+38EML1sVrV37ExxHPtM6882Ivjc7VN6zFHOHD2c9eVfbShkIf8YkVQUcFAgMBAAE="
}

View File

@ -1,5 +0,0 @@
{
"id": "Qmex1SSsueWFsUfjdkugJ5zhcnjddAt8TxcnDLUXKD9Sx7",
"privKey": "CAASqAkwggSkAgEAAoIBAQCXzV127CvVHOGMzvsn/U+/32JM58KA6k0FSCCeNFzNowiDS/vV5eezGN5AFoxsF6icWLoaczz7l9RdVD+I/t6PEt9X7XUdrDCtSS8WmAcCgvZWSSf7yAd3jT4GSZDUIgIEeRZsERDt/yVqTLwsZ1G9dMIeh8sbf2zwjTXZIWaRM6o4lq3DYFfzLvJUXlJodxPogU7l7nLkITPUv+yQAMcVHizbNwJvwiETKYeUj73/m/wEPAlnFESexDstxNiIwE/FH8Ao50QPZRO6E6Jb0hhYSI/4CLRdrzDFm/Vzplei3Wr2DokSROaNyeG37VAueyA+pDqn84um+L9uXLwbv5FbAgMBAAECggEAdBUzV/GaQ0nmoQrWvOnUxmFIho7kCjkh1NwnNVPNc+Msa1r7pcI9wJNPwap8j1w4L/cZuYhOJgcg+o2mWFiuULKZ4F9Ro/M89gZ038457g2/2pPu43c/Xoi/2YcAHXg0Gr+OCe2zCIyITBWKAFqyAzL6DubAxrJW2Ezj1LrZ+EZgMyzbh/go/eEGSJaaGkINeAkY144DqDWWWvzyhKhryipsGkZGEkVy9xJgMEI3ipVvuPez2XAvoyyeuinBBLe+Z2vY5G50XXzbIMhIQGLncHf9MwTv6wt1ilyOSLOXK0BoQbB76J3R3is5dSULXXP9r8VocjLBEkmBuf4FXAKzoQKBgQDNNS4F1XE1gxD8LPkL+aB/hi6eVHVPhr+w0I/9ATikcLGeUfBM2Gd6cZRPFtNVrv1p6ZF1D1UyGDknGbDBSQd9wLUgb0fDoo3jKYMGWq6G+VvaP5rzWQeBV8YV2EhSmUk1i6kiYe2ZE8WyrPie7iwpQIY60e2A8Ly0GKZiBZUcHQKBgQC9YDAVsGnEHFVFkTDpvw5HwEzCgTb2A3NgkGY3rTYZ7L6AFjqCYmUwFB8Fmbyc4kdFWNh8wfmq5Qrvl49NtaeukiqWKUUlB8uPdztB1P0IahA2ks0owStZlRifmwfgYyMd4xE17lhaOgQQJZZPxmP0F6mdOvb3YJafNURCdMS51wKBgEvvIM+h0tmFXXSjQ6kNvzlRMtD92ccKysYn9xAdMpOO6/r0wSH+dhQWEVZO0PcE4NsfReb2PIVj90ojtIdhebcr5xpQc1LORQjJJKXmSmzBux6AqNrhl+hhzXfp56FA/Zkly/lgGWaqrV5XqUxOP+Mn8EO1yNgMvRc7g94DyNB1AoGBAKLBuXHalXwDsdHBUB2Eo3xNLGt6bEcRfia+0+sEBdxQGQWylQScFkU09dh1YaIf44sZKa5HdBFJGpYCVxo9hmjFnK5Dt/Z0daHOonIY4INLzLVqg8KECoLKXkhGEIXsDjFQhukn+G1LMVTDSSU055DQiWjlVX4UWD9qo0jOXIkvAoGBAMP50p2X6PsWWZUuuR7i1JOJHRyQZPWdHh9p8SSLnCtEpHYZfJr4INXNmhnSiB/3TUnHix2vVKjosjMTCk/CjfzXV2H41WPOLZ2/Pi3SxCicWIRj4kCcWhkEuIF2jGkg1+jmNiCl/zNMaBOAIP3QbDPtqOWbYlPd2YIzdj6WQ6R4",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCXzV127CvVHOGMzvsn/U+/32JM58KA6k0FSCCeNFzNowiDS/vV5eezGN5AFoxsF6icWLoaczz7l9RdVD+I/t6PEt9X7XUdrDCtSS8WmAcCgvZWSSf7yAd3jT4GSZDUIgIEeRZsERDt/yVqTLwsZ1G9dMIeh8sbf2zwjTXZIWaRM6o4lq3DYFfzLvJUXlJodxPogU7l7nLkITPUv+yQAMcVHizbNwJvwiETKYeUj73/m/wEPAlnFESexDstxNiIwE/FH8Ao50QPZRO6E6Jb0hhYSI/4CLRdrzDFm/Vzplei3Wr2DokSROaNyeG37VAueyA+pDqn84um+L9uXLwbv5FbAgMBAAE="
}

View File

@ -1,168 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-checkmark'))
const expect = chai.expect
const sinon = require('sinon')
const series = require('async/series')
const createNode = require('./utils/create-node')
describe('libp2p state machine (fsm)', () => {
describe('starting and stopping', () => {
let node
beforeEach((done) => {
createNode([], {
config: {
dht: {
enabled: false
}
}
}, (err, _node) => {
node = _node
done(err)
})
})
afterEach(() => {
node.removeAllListeners()
sinon.restore()
})
after((done) => {
node.stop(done)
node = null
})
it('should be able to start and stop several times', (done) => {
node.on('start', (err) => {
expect(err).to.not.exist().mark()
})
node.on('stop', (err) => {
expect(err).to.not.exist().mark()
})
expect(4).checks(done)
series([
(cb) => node.start(cb),
(cb) => node.stop(cb),
(cb) => node.start(cb),
(cb) => node.stop(cb)
], () => {})
})
it('should noop when stopping a stopped node', (done) => {
node.once('start', node.stop)
node.once('stop', () => {
node.state.on('STOPPING', () => {
throw new Error('should not stop a stopped node')
})
node.once('stop', done)
// stop the stopped node
node.stop(() => {})
})
node.start(() => {})
})
it('should callback with an error when it occurs on stop', (done) => {
const error = new Error('some error starting')
node.once('start', () => {
node.once('error', (err) => {
expect(err).to.eql(error).mark()
})
node.stop((err) => {
expect(err).to.eql(error).mark()
})
})
expect(2).checks(done)
sinon.stub(node._switch, 'stop').callsArgWith(0, error)
node.start(() => {})
})
it('should noop when starting a started node', (done) => {
node.once('start', () => {
node.state.on('STARTING', () => {
throw new Error('should not start a started node')
})
node.once('start', () => {
node.once('stop', done)
node.stop(() => {})
})
// start the started node
node.start(() => {})
})
node.start(() => {})
})
it('should error on start with no transports', (done) => {
const transports = node._modules.transport
node._modules.transport = null
node.on('stop', () => {
node._modules.transport = transports
expect(node._modules.transport).to.exist().mark()
})
node.on('error', (err) => {
expect(err).to.exist().mark()
})
node.on('start', () => {
throw new Error('should not start')
})
expect(2).checks(done)
node.start(() => {})
})
it('should not start if the switch fails to start', (done) => {
const error = new Error('switch didnt start')
const stub = sinon.stub(node._switch, 'start')
.callsArgWith(0, error)
node.on('stop', () => {
expect(stub.calledOnce).to.eql(true).mark()
stub.restore()
})
node.on('error', (err) => {
expect(err).to.eql(error).mark()
})
node.on('start', () => {
throw new Error('should not start')
})
expect(3).checks(done)
node.start((err) => {
expect(err).to.eql(error).mark()
})
})
it('should not dial when the node is stopped', (done) => {
node.on('stop', () => {
node.dial(null, (err) => {
expect(err).to.exist()
expect(err.code).to.eql('ERR_NODE_NOT_STARTED')
done()
})
})
node.stop(() => {})
})
it('should not dial (fsm) when the node is stopped', (done) => {
node.on('stop', () => {
node.dialFSM(null, null, (err) => {
expect(err).to.exist()
expect(err.code).to.eql('ERR_NODE_NOT_STARTED')
done()
})
})
node.stop(() => {})
})
})
})

Some files were not shown because too many files have changed in this diff Show More