Skip to content

Commit 83c6cde

Browse files
chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <[email protected]>
1 parent 9abc2d3 commit 83c6cde

File tree

5 files changed

+195
-115
lines changed

5 files changed

+195
-115
lines changed

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ class Libp2p extends EventEmitter {
181181

182182
// Once we start, emit and dial any peers we may have already discovered
183183
this.state.on('STARTED', () => {
184-
this.peerStore.getAllArray().forEach((peerInfo) => {
184+
for (const peerInfo of this.peerStore.peers) {
185185
this.emit('peer:discovery', peerInfo)
186186
this._maybeConnect(peerInfo)
187-
})
187+
}
188188
})
189189

190190
this._peerDiscovered = this._peerDiscovered.bind(this)
@@ -279,17 +279,19 @@ class Libp2p extends EventEmitter {
279279
connection = await this.dialer.connectToPeer(peer, options)
280280
}
281281

282+
const peerInfo = getPeerInfo(connection.remotePeer)
283+
282284
// If a protocol was provided, create a new stream
283285
if (protocols) {
284286
const stream = await connection.newStream(protocols)
285-
const peerInfo = getPeerInfo(connection.remotePeer)
286287

287288
peerInfo.protocols.add(stream.protocol)
288289
this.peerStore.put(peerInfo)
289290

290291
return stream
291292
}
292293

294+
this.peerStore.put(peerInfo)
293295
return connection
294296
}
295297

src/peer-store/index.js

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const assert = require('assert')
44
const debug = require('debug')
55
const log = debug('libp2p:peer-store')
66
log.error = debug('libp2p:peer-store:error')
7-
const errCode = require('err-code')
87

98
const { EventEmitter } = require('events')
109

@@ -60,18 +59,43 @@ class PeerStore extends EventEmitter {
6059
add (peerInfo) {
6160
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
6261

63-
this.peers.set(peerInfo.id.toB58String(), peerInfo)
62+
// Create new instance and add values to it
63+
const newPeerInfo = new PeerInfo(peerInfo.id)
64+
65+
peerInfo.multiaddrs.forEach((ma) => newPeerInfo.multiaddrs.add(ma))
66+
peerInfo.protocols.forEach((p) => newPeerInfo.protocols.add(p))
67+
68+
const connectedMa = peerInfo.isConnected()
69+
connectedMa && newPeerInfo.connect(connectedMa)
70+
71+
const peerProxy = new Proxy(newPeerInfo, {
72+
set: (obj, prop, value) => {
73+
if (prop === 'multiaddrs') {
74+
this.emit('change:multiaddrs', {
75+
peerInfo: obj,
76+
multiaddrs: value.toArray()
77+
})
78+
} else if (prop === 'protocols') {
79+
this.emit('change:protocols', {
80+
peerInfo: obj,
81+
protocols: Array.from(value)
82+
})
83+
}
84+
return true
85+
}
86+
})
87+
88+
this.peers.set(peerInfo.id.toB58String(), peerProxy)
6489
}
6590

6691
/**
6792
* Updates an already known peer.
68-
* If already exist, updates ids info if outdated.
6993
* @param {PeerInfo} peerInfo
7094
*/
7195
update (peerInfo) {
7296
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
73-
74-
const recorded = this.peers.get(peerInfo.id.toB58String())
97+
const id = peerInfo.id.toB58String()
98+
const recorded = this.peers.get(id)
7599

76100
// pass active connection state
77101
const ma = peerInfo.isConnected()
@@ -81,22 +105,41 @@ class PeerStore extends EventEmitter {
81105

82106
// Verify new multiaddrs
83107
// TODO: better track added and removed multiaddrs
84-
if (peerInfo.multiaddrs.size || recorded.multiaddrs.size) {
85-
recorded.multiaddrs = peerInfo.multiaddrs
108+
const multiaddrsIntersection = [
109+
...recorded.multiaddrs.toArray()
110+
].filter((m) => peerInfo.multiaddrs.has(m))
111+
112+
if (multiaddrsIntersection.length !== peerInfo.multiaddrs.size ||
113+
multiaddrsIntersection.length !== recorded.multiaddrs.size) {
114+
// recorded.multiaddrs = peerInfo.multiaddrs
115+
recorded.multiaddrs.clear()
116+
117+
for (const ma of peerInfo.multiaddrs.toArray()) {
118+
recorded.multiaddrs.add(ma)
119+
}
86120

87121
this.emit('change:multiaddrs', {
88-
peerInfo: recorded,
89-
multiaddrs: Array.from(recorded.multiaddrs)
122+
peerInfo: peerInfo,
123+
multiaddrs: recorded.multiaddrs.toArray()
90124
})
91125
}
92126

93127
// Update protocols
94128
// TODO: better track added and removed protocols
95-
if (peerInfo.protocols.size || recorded.protocols.size) {
96-
recorded.protocols = new Set(peerInfo.protocols)
129+
const protocolsIntersection = new Set(
130+
[...recorded.protocols].filter((p) => peerInfo.protocols.has(p))
131+
)
132+
133+
if (protocolsIntersection.size !== peerInfo.protocols.size ||
134+
protocolsIntersection.size !== recorded.protocols.size) {
135+
recorded.protocols.clear()
136+
137+
for (const protocol of peerInfo.protocols) {
138+
recorded.protocols.add(protocol)
139+
}
97140

98141
this.emit('change:protocols', {
99-
peerInfo: recorded,
142+
peerInfo: peerInfo,
100143
protocols: Array.from(recorded.protocols)
101144
})
102145
}
@@ -105,6 +148,8 @@ class PeerStore extends EventEmitter {
105148
if (!recorded.id.pubKey && peerInfo.id.pubKey) {
106149
recorded.id.pubKey = peerInfo.id.pubKey
107150
}
151+
152+
// this.peers.set(id, recorded)
108153
}
109154

110155
/**
@@ -119,19 +164,11 @@ class PeerStore extends EventEmitter {
119164
return peerInfo
120165
}
121166

122-
throw errCode(new Error('PeerInfo was not found'), 'ERR_NO_PEER_INFO')
123-
}
124-
125-
/**
126-
* Get an array with all peers known.
127-
* @returns {Array<PeerInfo>}
128-
*/
129-
getAllArray () {
130-
return Array.from(this.peers.values())
167+
return undefined
131168
}
132169

133170
/**
134-
* Remove the info of the peer with the given id.
171+
* Removes the Peer with the matching `peerId` from the PeerStore
135172
* @param {string} peerId b58str id
136173
* @returns {boolean} true if found and removed
137174
*/
@@ -140,7 +177,7 @@ class PeerStore extends EventEmitter {
140177
}
141178

142179
/**
143-
* Replace the info stored of the given peer.
180+
* Completely replaces the existing peers metadata with the given `peerInfo`
144181
* @param {PeerInfo} peerInfo
145182
* @returns {void}
146183
*/

test/peer-store/peer-store.node.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)