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
This commit is contained in:
Jacob Heun
2020-01-22 11:47:30 +01:00
parent bb2e56e6c7
commit c1038bec92
14 changed files with 58 additions and 51 deletions

View File

@ -36,7 +36,7 @@ class ConnectionManager {
constructor (libp2p, options) {
this._libp2p = libp2p
this._registrar = libp2p.registrar
this._peerId = libp2p.peerInfo.id.toString()
this._peerId = libp2p.peerInfo.id.toB58String()
this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options)
assert(
this._options.maxConnections > this._options.minConnections,
@ -91,8 +91,8 @@ class ConnectionManager {
if (value < 0 || value > 1) {
throw new Error('value should be a number between 0 and 1')
}
if (peerId.toString) {
peerId = peerId.toString()
if (peerId.toB58String) {
peerId = peerId.toB58String()
}
this._peerValues.set(peerId, value)
}
@ -119,7 +119,7 @@ class ConnectionManager {
* @param {Connection} connection
*/
onConnect (connection) {
const peerId = connection.remotePeer.toString()
const peerId = connection.remotePeer.toB58String()
this._connections.set(connection.id, connection)
if (!this._peerValues.has(peerId)) {
this._peerValues.set(peerId, this._options.defaultPeerValue)
@ -133,7 +133,7 @@ class ConnectionManager {
*/
onDisconnect (connection) {
this._connections.delete(connection.id)
this._peerValues.delete(connection.remotePeer.toString())
this._peerValues.delete(connection.remotePeer.toB58String())
}
/**
@ -175,7 +175,7 @@ class ConnectionManager {
debug('%s: lowest value peer is %s', this._peerId, peerId)
debug('%s: closing a connection to %j', this._peerId, peerId)
for (const connection of this._connections.values()) {
if (connection.remotePeer.toString() === peerId) {
if (connection.remotePeer.toB58String() === peerId) {
connection.close()
break
}

View File

@ -113,7 +113,7 @@ class Dialer {
}
const addrs = this.peerStore.multiaddrsForPeer(dialable)
return {
id: dialable.id.toString(),
id: dialable.id.toB58String(),
addrs
}
}

View File

@ -180,7 +180,7 @@ class IdentifyService {
const id = await PeerId.createFromPubKey(publicKey)
const peerInfo = new PeerInfo(id)
if (connection.remotePeer.toString() !== id.toString()) {
if (connection.remotePeer.toB58String() !== id.toB58String()) {
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
}

View File

@ -314,7 +314,7 @@ class Libp2p extends EventEmitter {
hangUp (peer) {
const peerInfo = getPeerInfo(peer, this.peerStore)
return Promise.all(
this.registrar.connections.get(peerInfo.id.toString()).map(connection => {
this.registrar.connections.get(peerInfo.id.toB58String()).map(connection => {
return connection.close()
})
)
@ -426,7 +426,7 @@ class Libp2p extends EventEmitter {
* @param {PeerInfo} peerInfo
*/
_onDiscoveryPeer (peerInfo) {
if (peerInfo.id.toString() === this.peerInfo.id.toString()) {
if (peerInfo.id.toB58String() === this.peerInfo.id.toB58String()) {
log.error(new Error(codes.ERR_DISCOVERED_SELF))
return
}
@ -445,7 +445,7 @@ class Libp2p extends EventEmitter {
if (this._config.peerDiscovery.autoDial === true && !this.registrar.getConnection(peerInfo)) {
const minPeers = this._options.connectionManager.minPeers || 0
if (minPeers > this.connectionManager._connections.size) {
log('connecting to discovered peer %s', peerInfo.id.toString())
log('connecting to discovered peer %s', peerInfo.id.toB58String())
try {
await this.dialer.connectToPeer(peerInfo)
} catch (err) {

View File

@ -82,7 +82,7 @@ class Metrics {
* @returns {Stats}
*/
forPeer (peerId) {
const idString = peerId.toString()
const idString = peerId.toB58String()
return this._peerStats.get(idString) || this._oldPeers.get(idString)
}
@ -110,7 +110,7 @@ class Metrics {
* @param {PeerId} peerId
*/
onPeerDisconnected (peerId) {
const idString = peerId.toString()
const idString = peerId.toB58String()
const peerStats = this._peerStats.get(idString)
if (peerStats) {
peerStats.stop()
@ -140,7 +140,7 @@ class Metrics {
let peerStats = this.forPeer(remotePeer)
if (!peerStats) {
peerStats = new Stats(initialCounters, this._options)
this._peerStats.set(remotePeer.toString(), peerStats)
this._peerStats.set(remotePeer.toB58String(), peerStats)
}
// Peer and global stats
@ -162,13 +162,13 @@ class Metrics {
* Replaces the `PeerId` string with the given `peerId`.
* If stats are already being tracked for the given `peerId`, the
* placeholder stats will be merged with the existing stats.
* @param {string} placeholder A peerId string
* @param {PeerId} placeholder A peerId string
* @param {PeerId} peerId
*/
updatePlaceholder (placeholder, peerId) {
if (!this._running) return
const placeholderStats = this.forPeer(placeholder)
const peerIdString = peerId.toString()
const peerIdString = peerId.toB58String()
const existingStats = this.forPeer(peerId)
let mergedStats = placeholderStats
@ -180,7 +180,7 @@ class Metrics {
this._oldPeers.delete(peerIdString)
}
this._peerStats.delete(placeholder.toString())
this._peerStats.delete(placeholder.toB58String())
this._peerStats.set(peerIdString, mergedStats)
mergedStats.start()
}

View File

@ -74,7 +74,7 @@ class Registrar {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection')
const id = peerInfo.id.toString()
const id = peerInfo.id.toB58String()
const storedConn = this.connections.get(id)
if (storedConn) {
@ -95,7 +95,7 @@ class Registrar {
onDisconnect (peerInfo, connection, error) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
const id = peerInfo.id.toString()
const id = peerInfo.id.toB58String()
let storedConn = this.connections.get(id)
if (storedConn && storedConn.length > 1) {
@ -106,7 +106,7 @@ class Registrar {
topology.disconnect(peerInfo, error)
}
this.connections.delete(peerInfo.id.toString())
this.connections.delete(peerInfo.id.toB58String())
}
}
@ -118,7 +118,7 @@ class Registrar {
getConnection (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
const connections = this.connections.get(peerInfo.id.toString())
const connections = this.connections.get(peerInfo.id.toB58String())
// Return the first, open connection
if (connections) {
return connections.find(connection => connection.stat.status === 'open')

View File

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