Skip to content

Commit ed0b7de

Browse files
committed
feat: use libp2p protocol topology
BREAKING CHANGE: bitswap start and stop return a promise
1 parent b7552af commit ed0b7de

10 files changed

+49
-35
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"it-length-prefixed": "^3.0.0",
8383
"it-pipe": "^1.1.0",
8484
"just-debounce-it": "^1.1.0",
85+
"libp2p-interfaces": "^0.3.0",
8586
"moving-average": "^1.0.0",
8687
"multicodec": "^1.0.0",
8788
"multihashing-async": "^0.8.0",

src/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -360,23 +360,23 @@ class Bitswap {
360360
/**
361361
* Start the bitswap node.
362362
*
363-
* @returns {void}
363+
* @returns {Promise<void>}
364364
*/
365-
start () {
365+
async start () {
366366
this.wm.start()
367-
this.network.start()
367+
await this.network.start()
368368
this.engine.start()
369369
}
370370

371371
/**
372372
* Stop the bitswap node.
373373
*
374-
* @returns {void}
374+
* @returns {Promise<void>}
375375
*/
376-
stop () {
376+
async stop () {
377377
this._stats.stop()
378378
this.wm.stop()
379-
this.network.stop()
379+
await this.network.stop()
380380
this.engine.stop()
381381
}
382382
}

src/network.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const lp = require('it-length-prefixed')
44
const pipe = require('it-pipe')
55

6+
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')
7+
68
const Message = require('./types/message')
79
const CONSTANTS = require('./constants')
810
const logger = require('./utils').logger
@@ -33,12 +35,19 @@ class Network {
3335
this._onConnection = this._onConnection.bind(this)
3436
}
3537

36-
start () {
38+
async start () {
3739
this._running = true
3840
this.libp2p.handle(this.protocols, this._onConnection)
3941

40-
this.libp2p.connectionManager.on('peer:connect', this._onPeerConnect)
41-
this.libp2p.connectionManager.on('peer:disconnect', this._onPeerDisconnect)
42+
// register protocol with topology
43+
const topology = new MulticodecTopology({
44+
multicodecs: this.protocols,
45+
handlers: {
46+
onConnect: this._onPeerConnect,
47+
onDisconnect: this._onPeerDisconnect
48+
}
49+
})
50+
this._registrarId = await this.libp2p.registrar.register(topology)
4251

4352
// All existing connections are like new ones for us
4453
for (const peer of this.libp2p.peerStore.peers.values()) {
@@ -48,14 +57,14 @@ class Network {
4857
}
4958
}
5059

51-
stop () {
60+
async stop () {
5261
this._running = false
5362

5463
// Unhandle both, libp2p doesn't care if it's not already handled
5564
this.libp2p.unhandle(this.protocols)
5665

57-
this.libp2p.connectionManager.removeListener('peer:connect', this._onPeerConnect)
58-
this.libp2p.connectionManager.removeListener('peer:disconnect', this._onPeerDisconnect)
66+
// unregister protocol and handlers
67+
await this.libp2p.registrar.unregister(this._registrarId)
5968
}
6069

6170
/**
@@ -92,12 +101,12 @@ class Network {
92101
}
93102
}
94103

95-
_onPeerConnect (connection) {
96-
this.bitswap._onPeerConnected(connection.remotePeer)
104+
_onPeerConnect (peerId) {
105+
this.bitswap._onPeerConnected(peerId)
97106
}
98107

99-
_onPeerDisconnect (connection) {
100-
this.bitswap._onPeerDisconnected(connection.remotePeer)
108+
_onPeerDisconnect (peerId) {
109+
this.bitswap._onPeerDisconnected(peerId)
101110
}
102111

103112
/**

test/bitswap-mock-internals.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('bitswap with mocks', function () {
5151
describe('receive message', () => {
5252
it('simple block message', async () => {
5353
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
54-
bs.start()
54+
await bs.start()
5555

5656
const other = ids[1]
5757

@@ -85,7 +85,7 @@ describe('bitswap with mocks', function () {
8585

8686
it('simple want message', async () => {
8787
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
88-
bs.start()
88+
await bs.start()
8989

9090
const other = ids[1]
9191
const b1 = blocks[0]
@@ -110,7 +110,7 @@ describe('bitswap with mocks', function () {
110110
this.timeout(80 * 1000)
111111
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
112112

113-
bs.start()
113+
await bs.start()
114114

115115
const others = await makePeerId(5)
116116
const blocks = await makeBlock(10)
@@ -139,7 +139,7 @@ describe('bitswap with mocks', function () {
139139

140140
it('ignore unwanted blocks', async () => {
141141
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
142-
bs.start()
142+
await bs.start()
143143

144144
const other = ids[1]
145145

@@ -238,7 +238,7 @@ describe('bitswap with mocks', function () {
238238
bs.network = net
239239
bs.wm.network = net
240240
bs.engine.network = net
241-
bs.start()
241+
await bs.start()
242242
const get = bs.get(block.cid)
243243

244244
setTimeout(() => {
@@ -317,13 +317,13 @@ describe('bitswap with mocks', function () {
317317
// Create and start bs1
318318
const bs1 = new Bitswap(mockLibp2pNode(), repo.blocks)
319319
applyNetwork(bs1, n1)
320-
bs1.start()
320+
await bs1.start()
321321

322322
// Create and start bs2
323323
const repo2 = await createTempRepo()
324324
const bs2 = new Bitswap(mockLibp2pNode(), repo2.blocks)
325325
applyNetwork(bs2, n2)
326-
bs2.start()
326+
await bs2.start()
327327

328328
bs1._onPeerConnected(other)
329329
bs2._onPeerConnected(me)
@@ -443,7 +443,7 @@ describe('bitswap with mocks', function () {
443443
describe('unwant', () => {
444444
it('removes blocks that are wanted multiple times', async () => {
445445
const bs = new Bitswap(mockLibp2pNode(), repo.blocks)
446-
bs.start()
446+
await bs.start()
447447

448448
const b = blocks[12]
449449
const p = Promise.all([

test/bitswap-stats.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('bitswap stats', () => {
6565
bs.wm.wantBlocks(blocks.map(b => b.cid))
6666

6767
// start the first bitswap
68-
bs.start()
68+
await bs.start()
6969
})
7070

7171
after(async () => {
@@ -167,7 +167,7 @@ describe('bitswap stats', () => {
167167

168168
before(async () => {
169169
bs2 = bitswaps[1]
170-
bs2.start()
170+
await bs2.start()
171171

172172
libp2pNodes[0].peerStore.addressBook.set(libp2pNodes[1].peerId, libp2pNodes[1].multiaddrs)
173173
await libp2pNodes[0].dial(libp2pNodes[1].peerId)

test/bitswap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function createThing (dht) {
2121
DHT: dht
2222
})
2323
const bitswap = new Bitswap(libp2pNode, repo.blocks)
24-
bitswap.start()
24+
await bitswap.start()
2525
return { repo, libp2pNode, bitswap }
2626
}
2727

test/network/gen-bitswap-network.node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async function exchangeBlocks (nodes, blocksPerNode = 10) {
6666

6767
// put blocksPerNode amount of blocks per node
6868
await Promise.all(nodes.map(async (node, i) => {
69-
node.bitswap.start()
69+
await node.bitswap.start()
7070

7171
const data = range(blocksPerNode).map((j) => {
7272
const index = i * blocksPerNode + j

test/network/network.node.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('network', () => {
5959
_onPeerDisconnected: async () => {}
6060
}
6161

62-
it('instantiate the network obj', () => {
62+
it('instantiate the network obj', async () => {
6363
networkA = new Network(p2pA, bitswapMockA)
6464
networkB = new Network(p2pB, bitswapMockB)
6565
// only bitswap100
@@ -69,9 +69,9 @@ describe('network', () => {
6969
expect(networkB).to.exist()
7070
expect(networkC).to.exist()
7171

72-
networkA.start()
73-
networkB.start()
74-
networkC.start()
72+
await networkA.start()
73+
await networkB.start()
74+
await networkC.start()
7575
})
7676

7777
it('connectTo fail', async () => {
@@ -209,8 +209,8 @@ describe('network', () => {
209209
networkB = new Network(p2pB, bitswapMockB)
210210
networkB.protocols = ['/ipfs/bitswap/1.2.0']
211211

212-
networkA.start()
213-
networkB.start()
212+
await networkA.start()
213+
await networkB.start()
214214

215215
p2pA.peerStore.addressBook.set(p2pB.peerId, p2pB.multiaddrs)
216216

test/utils/create-bitswap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ module.exports = async () => {
1515
}
1616
})
1717
const bitswap = new Bitswap(libp2pNode, repo.blocks)
18-
bitswap.start()
18+
await bitswap.start()
1919
return { bitswap, repo, libp2pNode }
2020
}

test/utils/mocks.js

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ exports.mockLibp2pNode = () => {
2323
multiaddrs: [],
2424
handle () {},
2525
unhandle () {},
26+
registrar: {
27+
register () {},
28+
unregister () {}
29+
},
2630
contentRouting: {
2731
provide: async (cid) => {}, // eslint-disable-line require-await
2832
findProviders: async (cid, timeout) => { return [] } // eslint-disable-line require-await

0 commit comments

Comments
 (0)