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
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed May 28, 2020
1 parent f7dd1c1 commit d0144fd
Show file tree
Hide file tree
Showing 23 changed files with 160 additions and 185 deletions.
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
22 changes: 11 additions & 11 deletions packages/ipfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
"hapi-pino": "^6.1.0",
"hashlru": "^2.3.0",
"interface-datastore": "^1.0.2",
"ipfs-bitswap": "^1.0.0",
"ipfs-bitswap": "ipfs/js-ipfs-bitswap#chore/use-libp2p-0.28",
"ipfs-block-service": "^0.17.1",
"ipfs-core-utils": "^0.2.3",
"ipfs-http-client": "^44.1.0",
"ipfs-http-client": "44.0.3",
"ipfs-http-response": "^0.5.0",
"ipfs-repo": "^3.0.0",
"ipfs-unixfs": "^1.0.2",
Expand Down Expand Up @@ -129,21 +129,21 @@
"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-rc.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.0",
"libp2p-gossipsub": "^0.3.0",
"libp2p-kad-dht": "^0.18.7",
"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.1",
"libp2p-keychain": "^0.6.0",
"libp2p-mdns": "^0.13.1",
"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
72 changes: 36 additions & 36 deletions packages/ipfs/src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
const log = require('debug')('ipfs:components:init')
const PeerId = require('peer-id')
const { Buffer } = require('buffer')
const PeerInfo = require('peer-info')

const mergeOptions = require('merge-options')
const getDefaultConfig = require('../runtime/config-nodejs.js')
const createRepo = require('../runtime/repo-nodejs')
const Keychain = require('libp2p-keychain')
const NoKeychain = require('./no-keychain')
const mortice = require('mortice')
const { DAGNode } = require('ipld-dag-pb')
const UnixFs = require('ipfs-unixfs')
Expand Down Expand Up @@ -80,7 +78,7 @@ module.exports = ({
: await initNewRepo(repo, { ...options, print })

log('peer created')
const peerInfo = new PeerInfo(peerId)

const blockService = new BlockService(repo)
const ipld = new Ipld(getDefaultIpldOptions(blockService, constructorOptions.ipld, log))

Expand Down Expand Up @@ -142,7 +140,7 @@ module.exports = ({
// Setup the offline routing for IPNS.
// This is primarily used for offline ipns modifications, such as the initializeKeyspace feature.
const offlineDatastore = new OfflineDatastore(repo)
const ipns = new IPNS(offlineDatastore, repo.datastore, peerInfo, keychain, { pass: options.pass })
const ipns = new IPNS(offlineDatastore, repo.datastore, peerId, keychain, { pass: options.pass })
await ipns.initializeKeyspace(peerId.privKey, emptyDirCid.toString())
}

Expand All @@ -158,7 +156,7 @@ module.exports = ({
ipld,
keychain,
object,
peerInfo,
peerId,
pin,
pinManager,
preload,
Expand Down Expand Up @@ -190,7 +188,6 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi
}

const peerId = await createPeerId({ privateKey, bits, print })
let keychain = new NoKeychain()

log('identity generated')

Expand All @@ -201,23 +198,32 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi

privateKey = peerId.privKey

config.Keychain = Keychain.generateOptions()

log('peer identity: %s', config.Identity.PeerID)

await repo.init(config)
await repo.open()

log('repo opened')

if (pass) {
log('creating keychain')
const keychainOptions = { passPhrase: pass, ...config.Keychain }
keychain = new Keychain(repo.keys, keychainOptions)
await keychain.importPeer('self', { privKey: privateKey })
// Create libp2p for Keychain creation
const libp2p = Components.libp2p({
peerId,
repo,
config,
keychainConfig: {
pass
}
})

if (libp2p.keychain && libp2p.keychain.opts) {
await libp2p.loadKeychain()

await repo.config.set('Keychain', {
dek: libp2p.keychain.opts.dek
})
}

return { peerId, keychain }
return { peerId, keychain: libp2p.keychain }
}

async function initExistingRepo (repo, { config: newConfig, profiles, pass }) {
Expand All @@ -233,27 +239,21 @@ async function initExistingRepo (repo, { config: newConfig, profiles, pass }) {
await repo.config.set(config)
}

let keychain = new NoKeychain()

if (pass) {
const keychainOptions = { passPhrase: pass, ...config.Keychain }
keychain = new Keychain(repo.keys, keychainOptions)
log('keychain constructed')
}

const peerId = await PeerId.createFromPrivKey(config.Identity.PrivKey)

// Import the private key as 'self', if needed.
if (pass) {
try {
await keychain.findKeyByName('self')
} catch (err) {
log('Creating "self" key')
await keychain.importPeer('self', peerId)
const libp2p = Components.libp2p({
peerId,
repo,
config,
keychainConfig: {
pass,
...config.Keychain
}
}
})

libp2p.keychain && await libp2p.loadKeychain()

return { peerId, keychain }
return { peerId, keychain: libp2p.keychain }
}

function createPeerId ({ privateKey, bits, print }) {
Expand Down Expand Up @@ -303,7 +303,7 @@ function createApi ({
ipld,
keychain,
object,
peerInfo,
peerId,
pin,
pinManager,
preload,
Expand Down Expand Up @@ -337,7 +337,7 @@ function createApi ({
dns: Components.dns(),
files: Components.files({ ipld, block, blockService, repo, preload, options: constructorOptions }),
get: Components.get({ ipld, preload }),
id: Components.id({ peerInfo }),
id: Components.id({ peerId }),
init: async () => { throw new AlreadyInitializedError() }, // eslint-disable-line require-await
isOnline: Components.isOnline({}),
key: {
Expand Down Expand Up @@ -367,7 +367,7 @@ function createApi ({
initOptions,
ipld,
keychain,
peerInfo,
peerId,
pinManager,
preload,
print,
Expand All @@ -383,7 +383,7 @@ function createApi ({
addrs: notStarted,
connect: notStarted,
disconnect: notStarted,
localAddrs: Components.swarm.localAddrs({ peerInfo }),
localAddrs: Components.swarm.localAddrs({ multiaddrs: [] }),
peers: notStarted
},
version: Components.version({ repo })
Expand Down
32 changes: 25 additions & 7 deletions packages/ipfs/src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,37 @@ const PubsubRouters = require('../runtime/libp2p-pubsub-routers-nodejs')

module.exports = ({
options,
peerInfo,
peerId,
multiaddrs = [],
repo,
keychainConfig = {},
config
}) => {
options = options || {}
config = config || {}

const { datastore } = repo
const libp2pOptions = getLibp2pOptions({ options, config, datastore, peerInfo })
const { datastore, keys } = repo

const libp2pOptions = getLibp2pOptions({
options,
config,
datastore,
keys,
keychainConfig,
peerId,
multiaddrs
})

if (typeof options.libp2p === 'function') {
return options.libp2p({ libp2pOptions, options, config, datastore, peerInfo })
return options.libp2p({ libp2pOptions, options, config, datastore, peerId })
}

// Required inline to reduce startup time
const Libp2p = require('libp2p')
return new Libp2p(mergeOptions(libp2pOptions, get(options, 'libp2p', {})))
}

function getLibp2pOptions ({ options, config, datastore, peerInfo }) {
function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, peerId, multiaddrs }) {
const getPubsubRouter = () => {
const router = get(config, 'Pubsub.Router') || 'gossipsub'

Expand All @@ -39,7 +50,7 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo }) {

const libp2pDefaults = {
datastore,
peerInfo,
peerId: peerId,
modules: {}
}

Expand Down Expand Up @@ -80,12 +91,19 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo }) {
get(config, 'Pubsub.Enabled', true))
}
},
addresses: {
listen: multiaddrs
},
connectionManager: get(options, 'connectionManager', {
maxConnections: get(options, 'config.Swarm.ConnMgr.HighWater',
get(config, 'Swarm.ConnMgr.HighWater')),
minConnections: get(options, 'config.Swarm.ConnMgr.LowWater',
get(config, 'Swarm.ConnMgr.LowWater'))
})
}),
keychain: {
datastore: keys,
...keychainConfig
}
}

// Required inline to reduce startup time
Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs/src/core/components/name/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const { resolvePath } = require('./utils')
* @param {IPFS} self
* @returns {Object}
*/
module.exports = ({ ipns, dag, peerInfo, isOnline, keychain, options: constructorOptions }) => {
module.exports = ({ ipns, dag, peerId, isOnline, keychain, options: constructorOptions }) => {
const lookupKey = async keyName => {
if (keyName === 'self') {
return peerInfo.id.privKey
return peerId.privKey
}

try {
Expand Down
Loading

0 comments on commit d0144fd

Please sign in to comment.