Skip to content

Commit ec5f742

Browse files
jacobheundirkmc
authored andcommitted
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
1 parent 6ed576d commit ec5f742

File tree

96 files changed

+482
-12165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+482
-12165
lines changed

.aegir.js

+12-119
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,23 @@
11
'use strict'
22

3-
const pull = require('pull-stream')
4-
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
5-
const sigServer = require('libp2p-webrtc-star/src/sig-server')
6-
const promisify = require('promisify-es6')
7-
const mplex = require('pull-mplex')
8-
const spdy = require('libp2p-spdy')
9-
const PeerBook = require('peer-book')
10-
const PeerId = require('peer-id')
11-
const PeerInfo = require('peer-info')
12-
const path = require('path')
13-
const Switch = require('./src/switch')
14-
const WebSockets = require('libp2p-websockets')
15-
16-
const Node = require('./test/utils/bundle-nodejs.js')
17-
const {
18-
getPeerRelay,
19-
WRTC_RENDEZVOUS_MULTIADDR,
20-
WS_RENDEZVOUS_MULTIADDR
21-
} = require('./test/utils/constants')
22-
23-
let wrtcRendezvous
24-
let wsRendezvous
25-
let node
26-
let peerInfo
27-
let switchA
28-
let switchB
29-
30-
function echo (protocol, conn) { pull(conn, conn) }
31-
function idJSON (id) {
32-
const p = path.join(__dirname, `./test/switch/test-data/id-${id}.json`)
33-
return require(p)
34-
}
3+
const TransportManager = require('./src/transport-manager')
4+
const mockUpgrader = require('./test/utils/mockUpgrader')
5+
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
6+
let tm
357

36-
function createSwitchA () {
37-
return new Promise((resolve, reject) => {
38-
PeerId.createFromJSON(idJSON(1), (err, id) => {
39-
if (err) { return reject(err) }
40-
41-
const peerA = new PeerInfo(id)
42-
const maA = '/ip4/127.0.0.1/tcp/15337/ws'
43-
44-
peerA.multiaddrs.add(maA)
45-
const sw = new Switch(peerA, new PeerBook())
46-
47-
sw.transport.add('ws', new WebSockets())
48-
sw.start((err) => {
49-
if (err) { return reject(err) }
50-
resolve(sw)
51-
})
52-
})
53-
})
54-
}
55-
56-
function createSwitchB () {
57-
return new Promise((resolve, reject) => {
58-
PeerId.createFromJSON(idJSON(2), (err, id) => {
59-
if (err) { return reject(err) }
60-
61-
const peerB = new PeerInfo(id)
62-
const maB = '/ip4/127.0.0.1/tcp/15347/ws'
63-
64-
peerB.multiaddrs.add(maB)
65-
const sw = new Switch(peerB, new PeerBook())
66-
67-
sw.transport.add('ws', new WebSockets())
68-
sw.connection.addStreamMuxer(mplex)
69-
sw.connection.addStreamMuxer(spdy)
70-
sw.connection.reuse()
71-
sw.handle('/echo/1.0.0', echo)
72-
sw.start((err) => {
73-
if (err) { return reject(err) }
74-
resolve(sw)
75-
})
76-
})
77-
})
78-
}
8+
const WebSockets = require('libp2p-websockets')
799

8010
const before = async () => {
81-
[
82-
wrtcRendezvous,
83-
wsRendezvous,
84-
peerInfo,
85-
switchA,
86-
switchB
87-
] = await Promise.all([
88-
sigServer.start({
89-
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
90-
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
91-
}),
92-
WebSocketStarRendezvous.start({
93-
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
94-
refreshPeerListIntervalMS: 1000,
95-
strictMultiaddr: false,
96-
cryptoChallenge: true
97-
}),
98-
getPeerRelay(),
99-
createSwitchA(),
100-
createSwitchB()
101-
])
102-
103-
node = new Node({
104-
peerInfo,
105-
config: {
106-
relay: {
107-
enabled: true,
108-
hop: {
109-
enabled: true,
110-
active: true
111-
}
112-
}
113-
}
11+
tm = new TransportManager({
12+
upgrader: mockUpgrader,
13+
onConnection: () => {}
11414
})
115-
116-
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
117-
await node.start()
15+
tm.add(WebSockets.prototype[Symbol.toStringTag], WebSockets)
16+
await tm.listen(MULTIADDRS_WEBSOCKETS)
11817
}
11918

120-
const after = () => {
121-
return Promise.all([
122-
wrtcRendezvous.stop(),
123-
wsRendezvous.stop(),
124-
node.stop(),
125-
promisify(switchA.stop, { context: switchA })(),
126-
promisify(switchB.stop, { context: switchB })()
127-
])
19+
const after = async () => {
20+
await tm.close()
12821
}
12922

13023
module.exports = {

.travis.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ jobs:
2929
addons:
3030
chrome: stable
3131
script:
32-
- npx aegir test -t browser
33-
- npx aegir test -t webworker
32+
- npx aegir test -t browser -t webworker
3433

3534
- stage: test
3635
name: firefox
3736
addons:
3837
firefox: latest
3938
script:
40-
- npx aegir test -t browser -- --browsers FirefoxHeadless
41-
- npx aegir test -t webworker -- --browsers FirefoxHeadless
39+
- npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
4240

4341
notifications:
4442
email: false

package.json

+9-15
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
},
3737
"homepage": "https://libp2p.io",
3838
"license": "MIT",
39-
"browser": {
40-
"./test/utils/bundle-nodejs": "./test/utils/bundle-browser"
41-
},
4239
"engines": {
4340
"node": ">=10.0.0",
4441
"npm": ">=6.0.0"
@@ -54,25 +51,24 @@
5451
"interface-connection": "~0.3.3",
5552
"latency-monitor": "~0.2.1",
5653
"libp2p-crypto": "^0.16.2",
57-
"libp2p-websockets": "^0.12.2",
58-
"mafmt": "^6.0.7",
54+
"mafmt": "^7.0.0",
5955
"merge-options": "^1.0.1",
6056
"moving-average": "^1.0.0",
61-
"multiaddr": "^6.1.0",
57+
"multiaddr": "^7.1.0",
6258
"multistream-select": "~0.14.6",
6359
"once": "^1.4.0",
60+
"p-settle": "^3.1.0",
6461
"peer-book": "^0.9.1",
65-
"peer-id": "^0.12.2",
66-
"peer-info": "~0.15.1",
62+
"peer-id": "^0.13.3",
63+
"peer-info": "^0.17.0",
64+
"promisify-es6": "^1.0.3",
65+
"protons": "^1.0.1",
6766
"pull-cat": "^1.1.11",
6867
"pull-defer": "~0.2.3",
6968
"pull-handshake": "^1.1.4",
7069
"pull-reader": "^1.3.1",
7170
"pull-stream": "^3.6.9",
72-
"promisify-es6": "^1.0.3",
73-
"protons": "^1.0.1",
7471
"retimer": "^2.0.0",
75-
"superstruct": "^0.6.0",
7672
"xsalsa20": "^1.0.2"
7773
},
7874
"devDependencies": {
@@ -96,10 +92,8 @@
9692
"libp2p-pnet": "~0.1.0",
9793
"libp2p-secio": "^0.11.1",
9894
"libp2p-spdy": "^0.13.2",
99-
"libp2p-tcp": "^0.13.0",
100-
"libp2p-webrtc-star": "^0.16.1",
101-
"libp2p-websocket-star": "~0.10.2",
102-
"libp2p-websocket-star-rendezvous": "~0.4.1",
95+
"libp2p-tcp": "^0.14.1",
96+
"libp2p-websockets": "^0.13.0",
10397
"lodash.times": "^4.3.2",
10498
"nock": "^10.0.6",
10599
"portfinder": "^1.0.20",

src/config.js

+2-61
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict'
22

33
const mergeOptions = require('merge-options')
4-
const { struct, superstruct } = require('superstruct')
5-
const { optional, list } = struct
64

75
const DefaultConfig = {
86
connectionManager: {
@@ -38,67 +36,10 @@ const DefaultConfig = {
3836
}
3937
}
4038

41-
// Define custom types
42-
const s = superstruct({
43-
types: {
44-
transport: value => {
45-
if (value.length === 0) return 'ERROR_EMPTY'
46-
value.forEach(i => {
47-
if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
48-
})
49-
return true
50-
},
51-
protector: value => {
52-
if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
53-
return true
54-
}
55-
}
56-
})
57-
58-
const modulesSchema = s({
59-
connEncryption: optional(list([s('object|function')])),
60-
// this is hacky to simulate optional because interface doesnt work correctly with it
61-
// change to optional when fixed upstream
62-
connProtector: s('undefined|protector'),
63-
contentRouting: optional(list(['object'])),
64-
dht: optional(s('null|function|object')),
65-
pubsub: optional(s('null|function|object')),
66-
peerDiscovery: optional(list([s('object|function')])),
67-
peerRouting: optional(list(['object'])),
68-
streamMuxer: optional(list([s('object|function')])),
69-
transport: 'transport'
70-
})
71-
72-
const configSchema = s({
73-
peerDiscovery: 'object?',
74-
relay: 'object?',
75-
dht: 'object?',
76-
pubsub: 'object?'
77-
})
78-
79-
const optionsSchema = s({
80-
switch: 'object?',
81-
connectionManager: 'object?',
82-
datastore: 'object?',
83-
peerInfo: 'object',
84-
peerBook: 'object?',
85-
modules: modulesSchema,
86-
config: configSchema
87-
})
88-
8939
module.exports.validate = (opts) => {
9040
opts = mergeOptions(DefaultConfig, opts)
91-
const [error, options] = optionsSchema.validate(opts)
9241

93-
// Improve errors throwed, reduce stack by throwing here and add reason to the message
94-
if (error) {
95-
throw new Error(`${error.message}${error.reason ? ' - ' + error.reason : ''}`)
96-
} else {
97-
// Throw when dht is enabled but no dht module provided
98-
if (options.config.dht.enabled) {
99-
s('function|object')(options.modules.dht)
100-
}
101-
}
42+
if (opts.modules.transport.length < 1) throw new Error("'options.modules.transport' must contain at least 1 transport")
10243

103-
return options
44+
return opts
10445
}

src/errors.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ exports.codes = {
99
DHT_DISABLED: 'ERR_DHT_DISABLED',
1010
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
1111
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
12-
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF'
12+
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
13+
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
14+
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
15+
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
16+
ERR_TRANSPORT_UNAVAILABLE: 'ERR_TRANSPORT_UNAVAILABLE'
1317
}

0 commit comments

Comments
 (0)