Skip to content

Commit c1038be

Browse files
committed
fix: use toB58String everywhere to be consistent (#537)
* chore: update deps * fix: consistently use b58 peerid string The migration to base32 will happen at a later date
1 parent bb2e56e commit c1038be

File tree

14 files changed

+58
-51
lines changed

14 files changed

+58
-51
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"it-protocol-buffers": "^0.2.0",
5858
"latency-monitor": "~0.2.1",
5959
"libp2p-crypto": "^0.17.1",
60-
"libp2p-interfaces": "^0.1.5",
60+
"libp2p-interfaces": "^0.2.3",
6161
"mafmt": "^7.0.0",
6262
"merge-options": "^2.0.0",
6363
"moving-average": "^1.0.0",
@@ -77,7 +77,7 @@
7777
"devDependencies": {
7878
"@nodeutils/defaults-deep": "^1.1.0",
7979
"abortable-iterator": "^2.1.0",
80-
"aegir": "^20.4.1",
80+
"aegir": "^20.5.1",
8181
"chai": "^4.2.0",
8282
"chai-as-promised": "^7.1.1",
8383
"cids": "^0.7.1",
@@ -102,7 +102,7 @@
102102
"p-defer": "^3.0.0",
103103
"p-times": "^2.1.0",
104104
"p-wait-for": "^3.1.0",
105-
"sinon": "^7.2.7",
105+
"sinon": "^8.1.0",
106106
"streaming-iterables": "^4.1.0",
107107
"wrtc": "^0.4.1"
108108
},

src/connection-manager/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ConnectionManager {
3636
constructor (libp2p, options) {
3737
this._libp2p = libp2p
3838
this._registrar = libp2p.registrar
39-
this._peerId = libp2p.peerInfo.id.toString()
39+
this._peerId = libp2p.peerInfo.id.toB58String()
4040
this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options)
4141
assert(
4242
this._options.maxConnections > this._options.minConnections,
@@ -91,8 +91,8 @@ class ConnectionManager {
9191
if (value < 0 || value > 1) {
9292
throw new Error('value should be a number between 0 and 1')
9393
}
94-
if (peerId.toString) {
95-
peerId = peerId.toString()
94+
if (peerId.toB58String) {
95+
peerId = peerId.toB58String()
9696
}
9797
this._peerValues.set(peerId, value)
9898
}
@@ -119,7 +119,7 @@ class ConnectionManager {
119119
* @param {Connection} connection
120120
*/
121121
onConnect (connection) {
122-
const peerId = connection.remotePeer.toString()
122+
const peerId = connection.remotePeer.toB58String()
123123
this._connections.set(connection.id, connection)
124124
if (!this._peerValues.has(peerId)) {
125125
this._peerValues.set(peerId, this._options.defaultPeerValue)
@@ -133,7 +133,7 @@ class ConnectionManager {
133133
*/
134134
onDisconnect (connection) {
135135
this._connections.delete(connection.id)
136-
this._peerValues.delete(connection.remotePeer.toString())
136+
this._peerValues.delete(connection.remotePeer.toB58String())
137137
}
138138

139139
/**
@@ -175,7 +175,7 @@ class ConnectionManager {
175175
debug('%s: lowest value peer is %s', this._peerId, peerId)
176176
debug('%s: closing a connection to %j', this._peerId, peerId)
177177
for (const connection of this._connections.values()) {
178-
if (connection.remotePeer.toString() === peerId) {
178+
if (connection.remotePeer.toB58String() === peerId) {
179179
connection.close()
180180
break
181181
}

src/dialer/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Dialer {
113113
}
114114
const addrs = this.peerStore.multiaddrsForPeer(dialable)
115115
return {
116-
id: dialable.id.toString(),
116+
id: dialable.id.toB58String(),
117117
addrs
118118
}
119119
}

src/identify/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class IdentifyService {
180180

181181
const id = await PeerId.createFromPubKey(publicKey)
182182
const peerInfo = new PeerInfo(id)
183-
if (connection.remotePeer.toString() !== id.toString()) {
183+
if (connection.remotePeer.toB58String() !== id.toB58String()) {
184184
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
185185
}
186186

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class Libp2p extends EventEmitter {
314314
hangUp (peer) {
315315
const peerInfo = getPeerInfo(peer, this.peerStore)
316316
return Promise.all(
317-
this.registrar.connections.get(peerInfo.id.toString()).map(connection => {
317+
this.registrar.connections.get(peerInfo.id.toB58String()).map(connection => {
318318
return connection.close()
319319
})
320320
)
@@ -426,7 +426,7 @@ class Libp2p extends EventEmitter {
426426
* @param {PeerInfo} peerInfo
427427
*/
428428
_onDiscoveryPeer (peerInfo) {
429-
if (peerInfo.id.toString() === this.peerInfo.id.toString()) {
429+
if (peerInfo.id.toB58String() === this.peerInfo.id.toB58String()) {
430430
log.error(new Error(codes.ERR_DISCOVERED_SELF))
431431
return
432432
}
@@ -445,7 +445,7 @@ class Libp2p extends EventEmitter {
445445
if (this._config.peerDiscovery.autoDial === true && !this.registrar.getConnection(peerInfo)) {
446446
const minPeers = this._options.connectionManager.minPeers || 0
447447
if (minPeers > this.connectionManager._connections.size) {
448-
log('connecting to discovered peer %s', peerInfo.id.toString())
448+
log('connecting to discovered peer %s', peerInfo.id.toB58String())
449449
try {
450450
await this.dialer.connectToPeer(peerInfo)
451451
} catch (err) {

src/metrics/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Metrics {
8282
* @returns {Stats}
8383
*/
8484
forPeer (peerId) {
85-
const idString = peerId.toString()
85+
const idString = peerId.toB58String()
8686
return this._peerStats.get(idString) || this._oldPeers.get(idString)
8787
}
8888

@@ -110,7 +110,7 @@ class Metrics {
110110
* @param {PeerId} peerId
111111
*/
112112
onPeerDisconnected (peerId) {
113-
const idString = peerId.toString()
113+
const idString = peerId.toB58String()
114114
const peerStats = this._peerStats.get(idString)
115115
if (peerStats) {
116116
peerStats.stop()
@@ -140,7 +140,7 @@ class Metrics {
140140
let peerStats = this.forPeer(remotePeer)
141141
if (!peerStats) {
142142
peerStats = new Stats(initialCounters, this._options)
143-
this._peerStats.set(remotePeer.toString(), peerStats)
143+
this._peerStats.set(remotePeer.toB58String(), peerStats)
144144
}
145145

146146
// Peer and global stats
@@ -162,13 +162,13 @@ class Metrics {
162162
* Replaces the `PeerId` string with the given `peerId`.
163163
* If stats are already being tracked for the given `peerId`, the
164164
* placeholder stats will be merged with the existing stats.
165-
* @param {string} placeholder A peerId string
165+
* @param {PeerId} placeholder A peerId string
166166
* @param {PeerId} peerId
167167
*/
168168
updatePlaceholder (placeholder, peerId) {
169169
if (!this._running) return
170170
const placeholderStats = this.forPeer(placeholder)
171-
const peerIdString = peerId.toString()
171+
const peerIdString = peerId.toB58String()
172172
const existingStats = this.forPeer(peerId)
173173
let mergedStats = placeholderStats
174174

@@ -180,7 +180,7 @@ class Metrics {
180180
this._oldPeers.delete(peerIdString)
181181
}
182182

183-
this._peerStats.delete(placeholder.toString())
183+
this._peerStats.delete(placeholder.toB58String())
184184
this._peerStats.set(peerIdString, mergedStats)
185185
mergedStats.start()
186186
}

src/registrar.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Registrar {
7474
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
7575
assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection')
7676

77-
const id = peerInfo.id.toString()
77+
const id = peerInfo.id.toB58String()
7878
const storedConn = this.connections.get(id)
7979

8080
if (storedConn) {
@@ -95,7 +95,7 @@ class Registrar {
9595
onDisconnect (peerInfo, connection, error) {
9696
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
9797

98-
const id = peerInfo.id.toString()
98+
const id = peerInfo.id.toB58String()
9999
let storedConn = this.connections.get(id)
100100

101101
if (storedConn && storedConn.length > 1) {
@@ -106,7 +106,7 @@ class Registrar {
106106
topology.disconnect(peerInfo, error)
107107
}
108108

109-
this.connections.delete(peerInfo.id.toString())
109+
this.connections.delete(peerInfo.id.toB58String())
110110
}
111111
}
112112

@@ -118,7 +118,7 @@ class Registrar {
118118
getConnection (peerInfo) {
119119
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
120120

121-
const connections = this.connections.get(peerInfo.id.toString())
121+
const connections = this.connections.get(peerInfo.id.toB58String())
122122
// Return the first, open connection
123123
if (connections) {
124124
return connections.find(connection => connection.stat.status === 'open')

src/upgrader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Upgrader {
7373
if (this.metrics) {
7474
({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
7575
const idString = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
76-
setPeer({ toString: () => idString })
76+
setPeer({ toB58String: () => idString })
7777
maConn = this.metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
7878
}
7979

@@ -147,7 +147,7 @@ class Upgrader {
147147
if (this.metrics) {
148148
({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
149149
const idString = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
150-
setPeer({ toString: () => idString })
150+
setPeer({ toB58String: () => idString })
151151
maConn = this.metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
152152
}
153153

test/connection-manager/index.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Connection Manager', () => {
7575
const spy = sinon.spy(connection, 'close')
7676
// The connections have the same remote id, give them random ones
7777
// so that we can verify the correct connection was closed
78-
sinon.stub(connection.remotePeer, 'toString').returns(index)
78+
sinon.stub(connection.remotePeer, 'toB58String').returns(index)
7979
const value = Math.random()
8080
spies.set(value, spy)
8181
libp2p.connectionManager.setPeerValue(connection.remotePeer, value)

test/dialing/direct.node.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ describe('Dialing (direct, TCP)', () => {
306306
}
307307
})
308308

309-
const connection = await libp2p.dial(`${remoteAddr.toString()}/p2p/${remotePeerInfo.id.toString()}`)
309+
const connection = await libp2p.dial(`${remoteAddr.toString()}/p2p/${remotePeerInfo.id.toB58String()}`)
310310
expect(connection).to.exist()
311311
expect(connection.stat.timeline.close).to.not.exist()
312312
await libp2p.hangUp(connection.remotePeer)
@@ -375,7 +375,7 @@ describe('Dialing (direct, TCP)', () => {
375375
})
376376
const dials = 10
377377

378-
const fullAddress = remoteAddr.encapsulate(`/p2p/${remoteLibp2p.peerInfo.id.toString()}`)
378+
const fullAddress = remoteAddr.encapsulate(`/p2p/${remoteLibp2p.peerInfo.id.toB58String()}`)
379379
const dialResults = await Promise.all([...new Array(dials)].map((_, index) => {
380380
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerInfo)
381381
return libp2p.dial(fullAddress)
@@ -405,7 +405,7 @@ describe('Dialing (direct, TCP)', () => {
405405
const error = new Error('Boom')
406406
sinon.stub(libp2p.transportManager, 'dial').callsFake(() => Promise.reject(error))
407407

408-
const fullAddress = remoteAddr.encapsulate(`/p2p/${remoteLibp2p.peerInfo.id.toString()}`)
408+
const fullAddress = remoteAddr.encapsulate(`/p2p/${remoteLibp2p.peerInfo.id.toB58String()}`)
409409
const dialResults = await pSettle([...new Array(dials)].map((_, index) => {
410410
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerInfo)
411411
return libp2p.dial(fullAddress)

test/dialing/relay.node.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ describe('Dialing (via relay, TCP)', () => {
5959

6060
it('should be able to connect to a peer over a relay with active connections', async () => {
6161
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
62-
const relayIdString = relayLibp2p.peerInfo.id.toString()
62+
const relayIdString = relayLibp2p.peerInfo.id.toB58String()
6363

6464
const dialAddr = relayAddr
6565
.encapsulate(`/p2p/${relayIdString}`)
66-
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
66+
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toB58String()}`)
6767

6868
const tcpAddrs = dstLibp2p.transportManager.getAddrs()
6969
await dstLibp2p.transportManager.listen([multiaddr(`/p2p-circuit${relayAddr}/p2p/${relayIdString}`)])
@@ -94,11 +94,11 @@ describe('Dialing (via relay, TCP)', () => {
9494

9595
it('should fail to connect to a peer over a relay with inactive connections', async () => {
9696
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
97-
const relayIdString = relayLibp2p.peerInfo.id.toString()
97+
const relayIdString = relayLibp2p.peerInfo.id.toB58String()
9898

9999
const dialAddr = relayAddr
100100
.encapsulate(`/p2p/${relayIdString}`)
101-
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
101+
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toB58String()}`)
102102

103103
await expect(srcLibp2p.dial(dialAddr))
104104
.to.eventually.be.rejectedWith(AggregateError)
@@ -107,11 +107,11 @@ describe('Dialing (via relay, TCP)', () => {
107107

108108
it('should not stay connected to a relay when not already connected and HOP fails', async () => {
109109
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
110-
const relayIdString = relayLibp2p.peerInfo.id.toString()
110+
const relayIdString = relayLibp2p.peerInfo.id.toB58String()
111111

112112
const dialAddr = relayAddr
113113
.encapsulate(`/p2p/${relayIdString}`)
114-
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
114+
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toB58String()}`)
115115

116116
await expect(srcLibp2p.dial(dialAddr))
117117
.to.eventually.be.rejectedWith(AggregateError)
@@ -124,11 +124,11 @@ describe('Dialing (via relay, TCP)', () => {
124124

125125
it('dialer should stay connected to an already connected relay on hop failure', async () => {
126126
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
127-
const relayIdString = relayLibp2p.peerInfo.id.toString()
127+
const relayIdString = relayLibp2p.peerInfo.id.toB58String()
128128

129129
const dialAddr = relayAddr
130130
.encapsulate(`/p2p/${relayIdString}`)
131-
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
131+
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toB58String()}`)
132132

133133
await srcLibp2p.dial(relayAddr)
134134

@@ -143,11 +143,11 @@ describe('Dialing (via relay, TCP)', () => {
143143

144144
it('destination peer should stay connected to an already connected relay on hop failure', async () => {
145145
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
146-
const relayIdString = relayLibp2p.peerInfo.id.toString()
146+
const relayIdString = relayLibp2p.peerInfo.id.toB58String()
147147

148148
const dialAddr = relayAddr
149149
.encapsulate(`/p2p/${relayIdString}`)
150-
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
150+
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toB58String()}`)
151151

152152
// Connect the destination peer and the relay
153153
const tcpAddrs = dstLibp2p.transportManager.getAddrs()

test/identify/index.spec.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,20 @@ describe('Identify', () => {
8484
it('should throw if identified peer is the wrong peer', async () => {
8585
const localIdentify = new IdentifyService({
8686
peerInfo: localPeer,
87-
protocols
87+
protocols,
88+
registrar: {
89+
peerStore: {
90+
replace: () => {}
91+
}
92+
}
8893
})
8994
const remoteIdentify = new IdentifyService({
9095
peerInfo: remotePeer,
9196
protocols
9297
})
9398

9499
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
95-
const localConnectionMock = { newStream: () => {}, remotePeer }
100+
const localConnectionMock = { newStream: () => {}, remotePeer: localPeer.id }
96101
const remoteConnectionMock = { remoteAddr: observedAddr }
97102

98103
const [local, remote] = duplexPair()

0 commit comments

Comments
 (0)