Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: use libp2p 0.28.x (#3019)
Browse files Browse the repository at this point in the history
This PR updates libp2p usage to `0.28.x`. 

The biggest impact in `js-ipfs` is the deprecation of `PeerInfo`, which creates breaking changes in several parts of the libp2p API.

For more information you can check the libp2p migration guide and release notes:

- [libp2p/js-libp2p/doc/migrations/v0.27-v.28.md](https://github.com/libp2p/js-libp2p/blob/master/doc/migrations/v0.27-v.28.md)
- [libp2p/js-libp2p#603](libp2p/js-libp2p#603)

BREAKING CHANGES:

- `.libp2p` property of `ipfs` nodes now supports 0.28.x libp2p API
- `PeerInfo` is no longer exported from `require('ipfs')`
  • Loading branch information
vasco-santos authored Jun 8, 2020
1 parent a8b4082 commit 463ec66
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 211 deletions.
16 changes: 7 additions & 9 deletions examples/custom-libp2p/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ const IPFS = require('ipfs')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const Bootstrap = require('libp2p-bootstrap')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')

/**
* Options for the libp2p bundle
* @typedef {Object} libp2pBundle~options
* @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node
* @property {PeerBook} peerBook - The PeerBook of the IPFS node
* @property {PeerId} peerId - The PeerId of the IPFS node
* @property {Object} config - The config of the IPFS node
* @property {Object} options - The options given to the IPFS node
*/
Expand All @@ -27,15 +25,16 @@ const SECIO = require('libp2p-secio')
*/
const libp2pBundle = (opts) => {
// Set convenience variables to clearly showcase some of the useful things that are available
const peerInfo = opts.peerInfo
const peerBook = opts.peerBook
const peerId = opts.peerId
const bootstrapList = opts.config.Bootstrap

// Build and return our libp2p node
// n.b. for full configuration options, see https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md
return new Libp2p({
peerInfo,
peerBook,
peerId,
addresses: {
listen: ['/ip4/127.0.0.1/tcp/0']
},
// Lets limit the connection managers peers and have it check peer health less frequently
connectionManager: {
minPeers: 25,
Expand All @@ -47,8 +46,7 @@ const libp2pBundle = (opts) => {
TCP
],
streamMuxer: [
MPLEX,
SPDY
MPLEX
],
connEncryption: [
SECIO
Expand Down
13 changes: 6 additions & 7 deletions examples/custom-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
"license": "MIT",
"dependencies": {
"ipfs": "^0.46.0",
"libp2p": "^0.27.7",
"libp2p-bootstrap": "^0.10.3",
"libp2p-kad-dht": "^0.18.7",
"libp2p-mdns": "^0.13.1",
"libp2p-mplex": "^0.9.3",
"libp2p-secio": "^0.12.2",
"libp2p-spdy": "^0.13.3",
"libp2p": "^0.28.0",
"libp2p-bootstrap": "^0.11.0",
"libp2p-kad-dht": "^0.19.5",
"libp2p-mdns": "^0.14.1",
"libp2p-mplex": "^0.9.5",
"libp2p-secio": "^0.12.4",
"libp2p-tcp": "^0.14.5"
},
"devDependencies": {
Expand Down
15 changes: 6 additions & 9 deletions examples/custom-libp2p/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ const path = require('path')
const execa = require('execa')
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')

async function test () {
let output = ''
Expand All @@ -29,18 +26,18 @@ async function test () {

console.info('Dialling', address)

const peerInfo = new PeerInfo(await PeerId.create())
peerInfo.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0'))

const peerId = await PeerId.create()
const libp2p = new Libp2p({
peerInfo,
peerId,
addresses: {
listen: ['/ip4/127.0.0.1/tcp/0']
},
modules: {
transport: [
TCP
],
streamMuxer: [
MPLEX,
SPDY
MPLEX
],
connEncryption: [
SECIO
Expand Down
4 changes: 3 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"hoist": true,
"nohoist": [
"ipfs-css",
"tachyons"
"tachyons",
"libp2p-delegated-content-routing",
"libp2p-delegated-peer-routing"
]
},
"run": {
Expand Down
14 changes: 7 additions & 7 deletions packages/interface-ipfs-core/src/swarm/addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ module.exports = (common, options) => {
})

it('should get a list of node addresses', async () => {
const peerInfos = await ipfsA.swarm.addrs()
expect(peerInfos).to.not.be.empty()
expect(peerInfos).to.be.an('array')
const peers = await ipfsA.swarm.addrs()
expect(peers).to.not.be.empty()
expect(peers).to.be.an('array')

for (const peerInfo of peerInfos) {
expect(CID.isCID(new CID(peerInfo.id))).to.be.true()
expect(peerInfo).to.have.a.property('addrs').that.is.an('array')
for (const peer of peers) {
expect(CID.isCID(new CID(peer.id))).to.be.true()
expect(peer).to.have.a.property('addrs').that.is.an('array')

for (const ma of peerInfo.addrs) {
for (const ma of peer.addrs) {
expect(Multiaddr.isMultiaddr(ma)).to.be.true()
}
}
Expand Down
21 changes: 10 additions & 11 deletions packages/ipfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"hapi-pino": "^6.1.0",
"hashlru": "^2.3.0",
"interface-datastore": "^1.0.2",
"ipfs-bitswap": "^1.0.0",
"ipfs-bitswap": "^2.0.0",
"ipfs-block-service": "^0.17.1",
"ipfs-core-utils": "^0.2.3",
"ipfs-http-client": "^44.2.0",
Expand Down Expand Up @@ -128,21 +128,20 @@
"iterable-ndjson": "^1.1.0",
"jsondiffpatch": "^0.4.1",
"just-safe-set": "^2.1.0",
"libp2p": "^0.27.7",
"libp2p-bootstrap": "^0.10.3",
"libp2p": "^0.28.0",
"libp2p-bootstrap": "^0.11.0",
"libp2p-crypto": "^0.17.6",
"libp2p-delegated-content-routing": "^0.4.4",
"libp2p-delegated-peer-routing": "^0.4.2",
"libp2p-floodsub": "^0.20.4",
"libp2p-gossipsub": "^0.3.1",
"libp2p-kad-dht": "^0.18.7",
"libp2p-keychain": "^0.6.0",
"libp2p-mdns": "^0.13.1",
"libp2p-delegated-content-routing": "^0.5.0",
"libp2p-delegated-peer-routing": "^0.5.0",
"libp2p-floodsub": "^0.21.0",
"libp2p-gossipsub": "^0.4.0",
"libp2p-kad-dht": "^0.19.5",
"libp2p-mdns": "^0.14.1",
"libp2p-mplex": "^0.9.3",
"libp2p-record": "^0.7.3",
"libp2p-secio": "^0.12.2",
"libp2p-tcp": "^0.14.5",
"libp2p-webrtc-star": "^0.17.10",
"libp2p-webrtc-star": "^0.18.0",
"libp2p-websockets": "^0.13.3",
"mafmt": "^7.0.0",
"merge-options": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs/src/cli/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Daemon {
}
}

function getLibp2p ({ libp2pOptions, options, config, peerInfo }) {
function getLibp2p ({ libp2pOptions, options, config, peerId }) {
// Attempt to use any of the WebRTC versions available globally
let electronWebRTC
let wrtc
Expand Down Expand Up @@ -98,7 +98,7 @@ function getLibp2p ({ libp2pOptions, options, config, peerInfo }) {
}

libp2pOptions.modules.contentRouting = libp2pOptions.modules.contentRouting || []
libp2pOptions.modules.contentRouting.push(new DelegatedContentRouter(peerInfo.id, delegatedApiOptions))
libp2pOptions.modules.contentRouting.push(new DelegatedContentRouter(peerId, delegatedApiOptions))

libp2pOptions.modules.peerRouting = libp2pOptions.modules.peerRouting || []
libp2pOptions.modules.peerRouting.push(new DelegatedPeerRouter(delegatedApiOptions))
Expand Down
12 changes: 6 additions & 6 deletions packages/ipfs/src/core/components/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ module.exports = ({ libp2p, repo }) => {
options.maxNumProviders = options.numProviders
}

for await (const peerInfo of libp2p._dht.findProviders(key, options)) {
for await (const peer of libp2p._dht.findProviders(key, options)) {
yield {
id: peerInfo.id.toB58String(),
addrs: peerInfo.multiaddrs.toArray()
id: peer.id.toB58String(),
addrs: peer.addrs
}
}
}),
Expand All @@ -96,11 +96,11 @@ module.exports = ({ libp2p, repo }) => {
peerId = PeerId.createFromCID(peerId)
}

const peerInfo = await libp2p._dht.findPeer(peerId)
const peer = await libp2p._dht.findPeer(peerId)

return {
id: peerInfo.id.toB58String(),
addrs: peerInfo.multiaddrs.toArray()
id: peer.id.toB58String(),
addrs: peer.addrs
}
}),

Expand Down
6 changes: 3 additions & 3 deletions packages/ipfs/src/core/components/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const pkgversion = require('../../../package.json').version
const multiaddr = require('multiaddr')
const { withTimeoutOption } = require('../utils')

module.exports = ({ peerInfo, libp2p }) => {
module.exports = ({ peerId, libp2p }) => {
return withTimeoutOption(async function id () { // eslint-disable-line require-await
const id = peerInfo.id.toB58String()
const id = peerId.toB58String()
let addresses = []

if (libp2p) {
Expand All @@ -16,7 +16,7 @@ module.exports = ({ peerInfo, libp2p }) => {

return {
id,
publicKey: peerInfo.id.pubKey.bytes.toString('base64'),
publicKey: peerId.pubKey.bytes.toString('base64'),
addresses: addresses
.map(ma => {
const str = ma.toString()
Expand Down
Loading

0 comments on commit 463ec66

Please sign in to comment.