Skip to content

Commit

Permalink
chore: update interfaces and multiaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 12, 2021
1 parent 2bff878 commit 58bf45e
Show file tree
Hide file tree
Showing 52 changed files with 208 additions and 197 deletions.
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@
"it-drain": "^1.0.3",
"it-filter": "^1.0.1",
"it-first": "^1.0.4",
"it-handshake": "^1.0.2",
"it-length-prefixed": "^4.0.0",
"it-handshake": "^2.0.0",
"it-length-prefixed": "^5.0.2",
"it-map": "^1.0.4",
"it-merge": "1.0.0",
"it-pipe": "^1.1.0",
"it-protocol-buffers": "^0.2.0",
"it-take": "1.0.0",
"libp2p-crypto": "^0.19.0",
"libp2p-interfaces": "^0.9.0",
"libp2p-utils": "^0.3.0",
"mafmt": "^8.0.0",
"libp2p-interfaces": "^0.10.0",
"libp2p-utils": "^0.3.1",
"mafmt": "^9.0.0",
"merge-options": "^3.0.4",
"moving-average": "^1.0.0",
"multiaddr": "^8.1.2",
"multiaddr": "^9.0.1",
"multicodec": "^3.0.1",
"multihashing-async": "^2.1.2",
"multistream-select": "^1.0.0",
"multistream-select": "^2.0.0",
"mutable-proxy": "^1.0.0",
"node-forge": "^0.10.0",
"p-any": "^3.0.0",
Expand Down Expand Up @@ -149,13 +149,12 @@
"libp2p-bootstrap": "^0.12.0",
"libp2p-delegated-content-routing": "^0.9.0",
"libp2p-delegated-peer-routing": "^0.8.0",
"libp2p-floodsub": "^0.24.0",
"libp2p-gossipsub": "^0.8.0",
"libp2p-floodsub": "libp2p/js-libp2p-floodsub#chore/update-deps-and-remove-protons",
"libp2p-gossipsub": "ChainSafe/js-libp2p-gossipsub#chore/update-deps-and-remove-protons",
"libp2p-kad-dht": "^0.21.0",
"libp2p-mdns": "^0.15.0",
"libp2p-mplex": "^0.10.1",
"libp2p-noise": "^2.0.0",
"libp2p-secio": "^0.13.1",
"libp2p-tcp": "^0.15.1",
"libp2p-webrtc-star": "^0.21.2",
"libp2p-websockets": "^0.15.0",
Expand Down
16 changes: 6 additions & 10 deletions src/address-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
/** @typedef {import('../types').EventEmitterFactory} Events */
/** @type Events */
const EventEmitter = require('events')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')
const PeerId = require('peer-id')

/**
* @typedef {import('multiaddr')} Multiaddr
*/

/**
* @typedef {Object} AddressManagerOptions
* @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
Expand Down Expand Up @@ -47,7 +43,7 @@ class AddressManager extends EventEmitter {
* @returns {Multiaddr[]}
*/
getListenAddrs () {
return Array.from(this.listen).map((a) => multiaddr(a))
return Array.from(this.listen).map((a) => new Multiaddr(a))
}

/**
Expand All @@ -56,7 +52,7 @@ class AddressManager extends EventEmitter {
* @returns {Multiaddr[]}
*/
getAnnounceAddrs () {
return Array.from(this.announce).map((a) => multiaddr(a))
return Array.from(this.announce).map((a) => new Multiaddr(a))
}

/**
Expand All @@ -65,7 +61,7 @@ class AddressManager extends EventEmitter {
* @returns {Array<Multiaddr>}
*/
getObservedAddrs () {
return Array.from(this.observed).map((a) => multiaddr(a))
return Array.from(this.observed).map((a) => new Multiaddr(a))
}

/**
Expand All @@ -74,7 +70,7 @@ class AddressManager extends EventEmitter {
* @param {string | Multiaddr} addr
*/
addObservedAddr (addr) {
let ma = multiaddr(addr)
let ma = new Multiaddr(addr)
const remotePeer = ma.getPeerId()

// strip our peer id if it has been passed
Expand All @@ -83,7 +79,7 @@ class AddressManager extends EventEmitter {

// use same encoding for comparison
if (remotePeerId.equals(this.peerId)) {
ma = ma.decapsulate(multiaddr(`/p2p/${this.peerId}`))
ma = ma.decapsulate(new Multiaddr(`/p2p/${this.peerId}`))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/circuit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Libp2p circuit configuration can be seen at [Setup with Relay](../../doc/CONFIGU
Once you have a circuit relay node running, you can configure other nodes to use it as a relay as follows:

```js
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
Expand All @@ -47,7 +47,7 @@ const relayAddr = ...

const node = await Libp2p.create({
addresses: {
listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
listen: [new Multiaddr(`${relayAddr}/p2p-circuit`)]
},
modules: {
transport: [TCP],
Expand Down
4 changes: 2 additions & 2 deletions src/circuit/auto-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const log = Object.assign(debug('libp2p:auto-relay'), {

const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')
const PeerId = require('peer-id')

const { relay: multicodec } = require('./multicodec')
Expand Down Expand Up @@ -157,7 +157,7 @@ class AutoRelay {

// Attempt to listen on relay
try {
await this._transportManager.listen([multiaddr(listenAddr)])
await this._transportManager.listen([new Multiaddr(listenAddr)])
// Announce multiaddrs will update on listen success by TransportManager event being triggered
} catch (err) {
log.error(err)
Expand Down
1 change: 1 addition & 0 deletions src/circuit/circuit/stream-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class StreamHandler {
this.stream = stream

this.shake = handshake(this.stream)
// @ts-ignore options are not optional
this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength })
}

Expand Down
6 changes: 3 additions & 3 deletions src/circuit/circuit/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')
const { CircuitRelay } = require('../protocol')

/**
Expand Down Expand Up @@ -31,7 +31,7 @@ function validateAddrs (msg, streamHandler) {
try {
if (msg.dstPeer && msg.dstPeer.addrs) {
msg.dstPeer.addrs.forEach((addr) => {
return multiaddr(addr)
return new Multiaddr(addr)
})
}
} catch (err) {
Expand All @@ -44,7 +44,7 @@ function validateAddrs (msg, streamHandler) {
try {
if (msg.srcPeer && msg.srcPeer.addrs) {
msg.srcPeer.addrs.forEach((addr) => {
return multiaddr(addr)
return new Multiaddr(addr)
})
}
} catch (err) {
Expand Down
5 changes: 2 additions & 3 deletions src/circuit/listener.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict'

const { EventEmitter } = require('events')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('libp2p-interfaces/src/transport/types').Listener} Listener
*/

Expand All @@ -24,7 +23,7 @@ module.exports = (libp2p) => {
async function listen (addr) {
const addrString = String(addr).split('/p2p-circuit').find(a => a !== '')

const relayConn = await libp2p.dial(multiaddr(addrString))
const relayConn = await libp2p.dial(new Multiaddr(addrString))
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')

listeningAddrs.set(relayConn.remotePeer.toB58String(), relayedAddr)
Expand Down
28 changes: 19 additions & 9 deletions src/circuit/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const log = Object.assign(debug('libp2p:circuit'), {
error: debug('libp2p:circuit:err')
})

const errCode = require('err-code')
const mafmt = require('mafmt')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')
const PeerId = require('peer-id')
const { CircuitRelay: CircuitPB } = require('./protocol')
const { codes } = require('../errors')

const toConnection = require('libp2p-utils/src/stream-to-ma-conn')

Expand All @@ -21,7 +23,6 @@ const StreamHandler = require('./circuit/stream-handler')
const transportSymbol = Symbol.for('@libp2p/js-libp2p-circuit/circuit')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
*/
Expand Down Expand Up @@ -96,9 +97,9 @@ class Circuit {

if (virtualConnection) {
// @ts-ignore dst peer will not be undefined
const remoteAddr = multiaddr(request.dstPeer.addrs[0])
const remoteAddr = new Multiaddr(request.dstPeer.addrs[0])
// @ts-ignore src peer will not be undefined
const localAddr = multiaddr(request.srcPeer.addrs[0])
const localAddr = new Multiaddr(request.srcPeer.addrs[0])
const maConn = toConnection({
stream: virtualConnection,
remoteAddr,
Expand All @@ -124,10 +125,19 @@ class Circuit {
async dial (ma, options) {
// Check the multiaddr to see if it contains a relay and a destination peer
const addrs = ma.toString().split('/p2p-circuit')
const relayAddr = multiaddr(addrs[0])
const destinationAddr = multiaddr(addrs[addrs.length - 1])
const relayPeer = PeerId.createFromCID(relayAddr.getPeerId())
const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())
const relayAddr = new Multiaddr(addrs[0])
const destinationAddr = new Multiaddr(addrs[addrs.length - 1])
const relayId = relayAddr.getPeerId()
const destinationId = destinationAddr.getPeerId()

if (!relayId || !destinationId) {
const errMsg = 'Circuit relay dial failed as addresses did not have peer id'
log.error(errMsg)
throw errCode(new Error(errMsg), codes.ERR_RELAYED_DIAL)
}

const relayPeer = PeerId.createFromCID(relayId)
const destinationPeer = PeerId.createFromCID(destinationId)

let disconnectOnFailure = false
let relayConnection = this._connectionManager.get(relayPeer)
Expand All @@ -147,7 +157,7 @@ class Circuit {
},
dstPeer: {
id: destinationPeer.toBytes(),
addrs: [multiaddr(destinationAddr).bytes]
addrs: [new Multiaddr(destinationAddr).bytes]
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
const { FaultTolerance } = require('./transport-manager')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
* @typedef {import('.').Libp2pOptions} Libp2pOptions
* @typedef {import('.').constructorOptions} constructorOptions
*/
Expand Down
2 changes: 1 addition & 1 deletion src/content-routing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { pipe } = require('it-pipe')

/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
* @typedef {import('cids')} CID
* @typedef {import('libp2p-interfaces/src/content-routing/types')} ContentRoutingModule
*/
Expand Down
2 changes: 1 addition & 1 deletion src/content-routing/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const take = require('it-take')

/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion src/dialer/dial-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pAny = require('p-any')
/**
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('./')} Dialer
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/

/**
Expand Down
11 changes: 5 additions & 6 deletions src/dialer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const log = Object.assign(debug('libp2p:dialer'), {
error: debug('libp2p:dialer:err')
})
const errCode = require('err-code')
const multiaddr = require('multiaddr')
const { Multiaddr } = require('multiaddr')
// @ts-ignore timeout-abourt-controles does not export types
const TimeoutController = require('timeout-abort-controller')
const { anySignal } = require('any-signal')
Expand All @@ -23,7 +23,6 @@ const {

/**
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('peer-id')} PeerId
* @typedef {import('../peer-store')} PeerStore
* @typedef {import('../peer-store/address-book').Address} Address
Expand Down Expand Up @@ -79,7 +78,7 @@ class Dialer {
this._pendingDials = new Map()

for (const [key, value] of Object.entries(resolvers)) {
multiaddr.resolvers.set(key, value)
Multiaddr.resolvers.set(key, value)
}
}

Expand Down Expand Up @@ -151,7 +150,7 @@ class Dialer {

// If received a multiaddr to dial, it should be the first to use
// But, if we know other multiaddrs for the peer, we should try them too.
if (multiaddr.isMultiaddr(peer)) {
if (Multiaddr.isMultiaddr(peer)) {
knownAddrs = knownAddrs.filter((addr) => !peer.equals(addr))
knownAddrs.unshift(peer)
}
Expand Down Expand Up @@ -180,7 +179,7 @@ class Dialer {
*/
_createPendingDial (dialTarget, options = {}) {
/**
* @param {multiaddr} addr
* @param {Multiaddr} addr
* @param {{ signal: { aborted: any; }; }} options
*/
const dialAction = (addr, options) => {
Expand Down Expand Up @@ -271,7 +270,7 @@ class Dialer {
*/
async _resolveRecord (ma) {
try {
ma = multiaddr(ma.toString()) // Use current multiaddr module
ma = new Multiaddr(ma.toString()) // Use current multiaddr module
const multiaddrs = await ma.resolve()
return multiaddrs
} catch (_) {
Expand Down
1 change: 1 addition & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports.codes = {
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
ERR_ALREADY_ABORTED: 'ERR_ALREADY_ABORTED',
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
ERR_RELAYED_DIAL: 'ERR_RELAYED_DIAL',
ERR_DIALED_SELF: 'ERR_DIALED_SELF',
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
Expand Down
Loading

0 comments on commit 58bf45e

Please sign in to comment.