Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit a55c7c4

Browse files
vasco-santosjacobheun
authored andcommitted
chore: remove peer-info usage on topology (#42)
* chore: remove peer-info usage on topology BREAKING CHANGE: topology api now uses peer-id instead of peer-info
1 parent 87e2e89 commit a55c7c4

File tree

7 files changed

+72
-54
lines changed

7 files changed

+72
-54
lines changed

src/topology/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const toplogy = new MulticodecTopology({
5656
max: 50,
5757
multicodecs: ['/echo/1.0.0'],
5858
handlers: {
59-
onConnect: (peerInfo, conn) => {},
60-
onDisconnect: (peerInfo) => {}
59+
onConnect: (peerId, conn) => {},
60+
onDisconnect: (peerId) => {}
6161
}
6262
})
6363
```
@@ -69,8 +69,8 @@ The `MulticodecTopology` extends the `Topology`, which makes the `Topology` API
6969
### Topology
7070

7171
- `Topology`
72-
- `peers<Map<string, PeerInfo>>`: A Map of peers belonging to the topology.
73-
- `disconnect<function(PeerInfo)>`: Called when a peer has been disconnected
72+
- `peers<Map<string, PeerId>>`: A Map of peers belonging to the topology.
73+
- `disconnect<function(PeerId)>`: Called when a peer has been disconnected
7474

7575
#### Constructor
7676

@@ -79,8 +79,8 @@ const toplogy = new Topology({
7979
min: 0,
8080
max: 50,
8181
handlers: {
82-
onConnect: (peerInfo, conn) => {},
83-
onDisconnect: (peerInfo) => {}
82+
onConnect: (peerId, conn) => {},
83+
onDisconnect: (peerId) => {}
8484
}
8585
})
8686
```
@@ -95,27 +95,27 @@ const toplogy = new Topology({
9595

9696
#### Set a peer
9797

98-
- `topology.peers.set(id, peerInfo)`
98+
- `topology.peers.set(id, peerId)`
9999

100100
Add a peer to the topology.
101101

102102
**Parameters**
103103
- `id` is the `string` that identifies the peer to add.
104-
- `peerInfo` is the [PeerInfo][peer-info] of the peer to add.
104+
- `peerId` is the [PeerId][peer-id] of the peer to add.
105105

106106
#### Notify about a peer disconnected event
107107

108-
- `topology.disconnect(peerInfo)`
108+
- `topology.disconnect(peerId)`
109109

110110
**Parameters**
111-
- `peerInfo` is the [PeerInfo][peer-info] of the peer disconnected.
111+
- `peerId` is the [PeerId][peer-id] of the peer disconnected.
112112

113113
### Multicodec Topology
114114

115115
- `MulticodecTopology`
116116
- `registrar<Registrar>`: The `Registrar` of the topology. This is set by the `Registrar` during registration.
117-
- `peers<Map<string, PeerInfo>>`: The Map of peers that belong to the topology
118-
- `disconnect<function(PeerInfo)>`: Disconnects a peer from the topology.
117+
- `peers<Map<string, PeerId>>`: The Map of peers that belong to the topology
118+
- `disconnect<function(PeerId)>`: Disconnects a peer from the topology.
119119

120120
#### Constructor
121121

@@ -125,8 +125,8 @@ const toplogy = new MulticodecTopology({
125125
max: 50,
126126
multicodecs: ['/echo/1.0.0'],
127127
handlers: {
128-
onConnect: (peerInfo, conn) => {},
129-
onDisconnect: (peerInfo) => {}
128+
onConnect: (peerId, conn) => {},
129+
onDisconnect: (peerId) => {}
130130
}
131131
})
132132
```
@@ -139,3 +139,5 @@ const toplogy = new MulticodecTopology({
139139
- `handlers` is an optional `Object` containing the handler called when a peer is connected or disconnected.
140140
- `onConnect` is a `function` called everytime a peer is connected in the topology context.
141141
- `onDisconnect` is a `function` called everytime a peer is disconnected in the topology context.
142+
143+
[peer-id]: https://github.com/libp2p/js-peer-id

src/topology/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ class Topology {
2626
this._onConnect = handlers.onConnect || noop
2727
this._onDisconnect = handlers.onDisconnect || noop
2828

29-
this.peers = new Map()
29+
/**
30+
* Set of peers that support the protocol.
31+
* @type {Set<string>}
32+
*/
33+
this.peers = new Set()
3034
}
3135

3236
set registrar (registrar) {
@@ -35,11 +39,11 @@ class Topology {
3539

3640
/**
3741
* Notify about peer disconnected event.
38-
* @param {PeerInfo} peerInfo
42+
* @param {PeerId} peerId
3943
* @returns {void}
4044
*/
41-
disconnect (peerInfo) {
42-
this._onDisconnect(peerInfo)
45+
disconnect (peerId) {
46+
this._onDisconnect(peerId)
4347
}
4448
}
4549

src/topology/multicodec-topology.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,43 +55,44 @@ class MulticodecTopology extends Topology {
5555

5656
/**
5757
* Update topology.
58-
* @param {Array<PeerInfo>} peerInfoIterable
58+
* @param {Array<{id: PeerId, multiaddrs: Array<Multiaddr>, protocols: Array<string>}>} peerDataIterable
5959
* @returns {void}
6060
*/
61-
_updatePeers (peerInfoIterable) {
62-
for (const peerInfo of peerInfoIterable) {
63-
if (this.multicodecs.filter(multicodec => peerInfo.protocols.has(multicodec)).length) {
61+
_updatePeers (peerDataIterable) {
62+
for (const { id, protocols } of peerDataIterable) {
63+
if (this.multicodecs.filter(multicodec => protocols.includes(multicodec)).length) {
6464
// Add the peer regardless of whether or not there is currently a connection
65-
this.peers.set(peerInfo.id.toB58String(), peerInfo)
65+
this.peers.add(id.toB58String())
6666
// If there is a connection, call _onConnect
67-
const connection = this._registrar.getConnection(peerInfo)
68-
connection && this._onConnect(peerInfo, connection)
67+
const connection = this._registrar.getConnection(id)
68+
connection && this._onConnect(id, connection)
6969
} else {
7070
// Remove any peers we might be tracking that are no longer of value to us
71-
this.peers.delete(peerInfo.id.toB58String())
71+
this.peers.delete(id.toB58String())
7272
}
7373
}
7474
}
7575

7676
/**
7777
* Check if a new peer support the multicodecs for this topology.
7878
* @param {Object} props
79-
* @param {PeerInfo} props.peerInfo
79+
* @param {PeerId} props.peerId
8080
* @param {Array<string>} props.protocols
8181
*/
82-
_onProtocolChange ({ peerInfo, protocols }) {
83-
const existingPeer = this.peers.get(peerInfo.id.toB58String())
82+
_onProtocolChange ({ peerId, protocols }) {
83+
const hadPeer = this.peers.has(peerId.toB58String())
8484
const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol))
8585

8686
// Not supporting the protocol anymore?
87-
if (existingPeer && hasProtocol.length === 0) {
88-
this._onDisconnect(peerInfo)
87+
if (hadPeer && hasProtocol.length === 0) {
88+
this._onDisconnect(peerId)
8989
}
9090

9191
// New to protocol support
9292
for (const protocol of protocols) {
9393
if (this.multicodecs.includes(protocol)) {
94-
this._updatePeers([peerInfo])
94+
const peerData = this._registrar.peerStore.get(peerId)
95+
this._updatePeers([peerData])
9596
return
9697
}
9798
}

src/topology/tests/multicodec-topology.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ chai.use(require('dirty-chai'))
88
const sinon = require('sinon')
99

1010
const PeerId = require('peer-id')
11-
const PeerInfo = require('peer-info')
11+
1212
const peers = require('../../utils/peers')
1313

1414
module.exports = (test) => {
1515
describe('multicodec topology', () => {
16-
let topology, peer
16+
let topology, id
1717

1818
beforeEach(async () => {
1919
topology = await test.setup()
2020
if (!topology) throw new Error('missing multicodec topology')
2121

22-
const id = await PeerId.createFromJSON(peers[0])
23-
peer = await PeerInfo.create(id)
22+
id = await PeerId.createFromJSON(peers[0])
2423
})
2524

2625
afterEach(async () => {
@@ -38,7 +37,7 @@ module.exports = (test) => {
3837

3938
it('should trigger "onDisconnect" on peer disconnected', () => {
4039
sinon.spy(topology, '_onDisconnect')
41-
topology.disconnect(peer)
40+
topology.disconnect(id)
4241

4342
expect(topology._onDisconnect.callCount).to.equal(1)
4443
})
@@ -47,13 +46,16 @@ module.exports = (test) => {
4746
sinon.spy(topology, '_updatePeers')
4847
expect(topology.peers.size).to.eql(0)
4948

49+
const peerStore = topology._registrar.peerStore
50+
5051
const id2 = await PeerId.createFromJSON(peers[1])
51-
const peer2 = await PeerInfo.create(id2)
52-
topology.multicodecs.forEach((m) => peer2.protocols.add(m))
52+
peerStore.peers.set(id2.toB58String(), {
53+
id: id2,
54+
protocols: Array.from(topology.multicodecs)
55+
})
5356

54-
const peerStore = topology._registrar.peerStore
5557
peerStore.emit('change:protocols', {
56-
peerInfo: peer2,
58+
peerId: id2,
5759
protocols: Array.from(topology.multicodecs)
5860
})
5961

@@ -65,28 +67,34 @@ module.exports = (test) => {
6567
sinon.spy(topology, '_onDisconnect')
6668
expect(topology.peers.size).to.eql(0)
6769

70+
const peerStore = topology._registrar.peerStore
71+
6872
const id2 = await PeerId.createFromJSON(peers[1])
69-
const peer2 = await PeerInfo.create(id2)
70-
topology.multicodecs.forEach((m) => peer2.protocols.add(m))
73+
peerStore.peers.set(id2.toB58String(), {
74+
id: id2,
75+
protocols: Array.from(topology.multicodecs)
76+
})
7177

72-
const peerStore = topology._registrar.peerStore
7378
peerStore.emit('change:protocols', {
74-
peerInfo: peer2,
79+
peerId: id2,
7580
protocols: Array.from(topology.multicodecs)
7681
})
7782

7883
expect(topology.peers.size).to.eql(1)
7984

80-
topology.multicodecs.forEach((m) => peer2.protocols.delete(m))
85+
peerStore.peers.set(id2.toB58String(), {
86+
id: id2,
87+
protocols: []
88+
})
8189
// Peer does not support the protocol anymore
8290
peerStore.emit('change:protocols', {
83-
peerInfo: peer2,
91+
peerId: id2,
8492
protocols: []
8593
})
8694

8795
expect(topology.peers.size).to.eql(1)
8896
expect(topology._onDisconnect.callCount).to.equal(1)
89-
expect(topology._onDisconnect.calledWith(peer2)).to.equal(true)
97+
expect(topology._onDisconnect.calledWith(id2)).to.equal(true)
9098
})
9199
})
92100
}

src/topology/tests/topology.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ chai.use(require('dirty-chai'))
88
const sinon = require('sinon')
99

1010
const PeerId = require('peer-id')
11-
const PeerInfo = require('peer-info')
1211
const peers = require('../../utils/peers')
1312

1413
module.exports = (test) => {
1514
describe('topology', () => {
16-
let topology, peer
15+
let topology, id
1716

1817
beforeEach(async () => {
1918
topology = await test.setup()
2019
if (!topology) throw new Error('missing multicodec topology')
2120

22-
const id = await PeerId.createFromJSON(peers[0])
23-
peer = await PeerInfo.create(id)
21+
id = await PeerId.createFromJSON(peers[0])
2422
})
2523

2624
afterEach(async () => {
@@ -38,7 +36,7 @@ module.exports = (test) => {
3836

3937
it('should trigger "onDisconnect" on peer disconnected', () => {
4038
sinon.spy(topology, '_onDisconnect')
41-
topology.disconnect(peer)
39+
topology.disconnect(id)
4240

4341
expect(topology._onDisconnect.callCount).to.equal(1)
4442
})

test/topology/mock-peer-store.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class MockPeerStore extends EventEmitter {
77
super()
88
this.peers = peers
99
}
10+
11+
get (peerId) {
12+
return this.peers.get(peerId.toB58String())
13+
}
1014
}
1115

1216
module.exports = MockPeerStore

test/topology/multicodec-topology.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ describe('multicodec topology compliance tests', () => {
2121
})
2222

2323
if (!registrar) {
24-
const peerStore = new MockPeerStore([])
24+
const peers = new Map()
25+
const peerStore = new MockPeerStore(peers)
2526

2627
registrar = {
2728
peerStore,

0 commit comments

Comments
 (0)