Skip to content

Commit 06f01b9

Browse files
committed
feat: use libp2p protocol topology
1 parent b7552af commit 06f01b9

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
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/network.js

Lines changed: 17 additions & 8 deletions
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
@@ -37,8 +39,15 @@ class Network {
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 = 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()) {
@@ -54,8 +63,8 @@ class Network {
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+
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/utils/mocks.js

Lines changed: 4 additions & 0 deletions
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)