This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathlibp2p.spec.js
206 lines (179 loc) · 5.12 KB
/
libp2p.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* eslint-env mocha */
'use strict'
const { expect } = require('aegir/utils/chai')
const MemoryStore = require('interface-datastore').MemoryDatastore
const PeerId = require('peer-id')
const Libp2p = require('libp2p')
const EE = require('events')
const libp2pComponent = require('../src/components/libp2p')
const { NOISE: Crypto } = require('libp2p-noise')
class DummyTransport {
get [Symbol.toStringTag] () {
return 'DummyTransport'
}
filter () {
return []
}
}
class DummyDiscovery extends EE {
get [Symbol.toStringTag] () {
return 'DummyDiscovery'
}
start () {
return Promise.resolve()
}
stop () {
return Promise.resolve()
}
}
describe('libp2p customization', function () {
// Provide some extra time for ci since we're starting libp2p nodes in each test
this.timeout(25 * 1000)
let datastore
let peerId
let testConfig
let libp2p
before(async function () {
this.timeout(25 * 1000)
testConfig = {
Addresses: {
Swarm: ['/ip4/0.0.0.0/tcp/4002'],
API: '/ip4/127.0.0.1/tcp/5002',
Gateway: '/ip4/127.0.0.1/tcp/9090'
},
Discovery: {
MDNS: {
Enabled: false
},
webRTCStar: {
Enabled: false
}
}
}
datastore = new MemoryStore()
peerId = await PeerId.create()
})
afterEach(async () => {
if (libp2p) {
await libp2p.stop()
libp2p = null
}
})
describe('bundle', () => {
it('should allow for using a libp2p bundle', async () => {
libp2p = libp2pComponent({
options: {
libp2p: (opts) => {
return new Libp2p({
peerId: opts.peerId,
modules: { transport: [DummyTransport], connEncryption: [Crypto] },
config: { relay: { enabled: false } }
})
}
},
peerId,
repo: { datastore },
print: console.log, // eslint-disable-line no-console
config: testConfig
})
await libp2p.start()
expect(libp2p._config.peerDiscovery).to.eql({ autoDial: true })
const transports = Array.from(libp2p.transportManager.getTransports())
expect(transports).to.have.length(1)
})
it('should pass libp2p options to libp2p bundle function', async () => {
libp2p = libp2pComponent({
options: {
libp2p: (opts) => {
return new Libp2p({
peerId: opts.peerId,
modules: { transport: [DummyTransport], connEncryption: [Crypto] },
config: { relay: { enabled: false } }
})
}
},
peerId,
repo: { datastore },
print: console.log, // eslint-disable-line no-console
config: testConfig
})
await libp2p.start()
expect(libp2p._config.peerDiscovery).to.eql({ autoDial: true })
const transports = Array.from(libp2p.transportManager.getTransports())
expect(transports[0] instanceof DummyTransport).to.equal(true)
})
})
describe('options', () => {
it('should use options by default', async () => {
libp2p = libp2pComponent({
peerId,
repo: { datastore },
print: console.log, // eslint-disable-line no-console
config: testConfig
})
await libp2p.start()
expect(libp2p._config).to.deep.include({
peerDiscovery: {
autoDial: true,
bootstrap: {
enabled: true,
list: []
},
mdns: {
enabled: false
},
webRTCStar: {
enabled: false
}
},
pubsub: {
enabled: true,
emitSelf: true,
signMessages: true,
strictSigning: true
}
})
const transports = Array.from(libp2p.transportManager.getTransports())
expect(transports).to.have.length(3)
})
it('should allow for overriding via options', async () => {
libp2p = libp2pComponent({
peerId,
repo: { datastore },
print: console.log, // eslint-disable-line no-console
config: testConfig,
options: {
libp2p: {
modules: {
transport: [DummyTransport],
peerDiscovery: [DummyDiscovery]
},
config: { relay: { enabled: false } }
}
}
})
await libp2p.start()
const transports = Array.from(libp2p.transportManager.getTransports())
expect(transports).to.have.length(1)
expect(transports[0] instanceof DummyTransport).to.be.true()
const discoveries = Array.from(libp2p._discovery.values())
expect(discoveries).to.have.length(1)
expect(discoveries[0] instanceof DummyDiscovery).to.be.true()
})
})
describe('config', () => {
it('should select gossipsub as pubsub router', async () => {
libp2p = libp2pComponent({
peerId,
repo: { datastore },
print: console.log, // eslint-disable-line no-console
config: {
...testConfig,
Pubsub: { Router: 'gossipsub' }
}
})
await libp2p.start()
expect(libp2p._modules.pubsub).to.eql(require('libp2p-gossipsub'))
})
})
})