diff --git a/package.json b/package.json index bd5aef0c..79f8a6a4 100644 --- a/package.json +++ b/package.json @@ -56,11 +56,11 @@ "it-length-prefixed": "^3.0.0", "it-pipe": "^1.1.0", "it-pushable": "^1.4.0", - "libp2p": "libp2p/js-libp2p#chore/deprecate-old-peer-store-api", - "libp2p-bootstrap": "^0.10.3", - "libp2p-floodsub": "^0.20.0", - "libp2p-gossipsub": "^0.2.1", - "libp2p-kad-dht": "^0.19.0-pre.0", + "libp2p": "libp2p/js-libp2p#0.28.x", + "libp2p-bootstrap": "^0.11.0", + "libp2p-floodsub": "^0.21.0", + "libp2p-gossipsub": "^0.4.0", + "libp2p-kad-dht": "^0.19.1", "libp2p-mplex": "^0.9.3", "libp2p-noise": "^1.0.0", "libp2p-secio": "^0.12.2", @@ -68,7 +68,6 @@ "libp2p-websockets": "^0.13.2", "multiaddr": "^7.1.0", "peer-id": "^0.13.3", - "peer-info": "^0.17.1", "promisify-es6": "^1.0.3", "protons": "^1.1.0", "stream-to-it": "^0.2.0", diff --git a/src/daemon.js b/src/daemon.js index f69a7ede..553f3776 100644 --- a/src/daemon.js +++ b/src/daemon.js @@ -4,7 +4,6 @@ const TCP = require('libp2p-tcp') const Libp2p = require('./libp2p') -const PeerInfo = require('peer-info') const PeerId = require('peer-id') const ma = require('multiaddr') const CID = require('cids') @@ -58,15 +57,11 @@ class Daemon { */ connect (connectRequest) { const peer = connectRequest.connect.peer - const addrs = connectRequest.connect.addrs - const peerInfo = new PeerInfo( - PeerId.createFromBytes(peer) - ) - addrs.forEach((a) => { - peerInfo.multiaddrs.add(ma(a)) - }) + const addrs = connectRequest.connect.addrs.map((a) => ma(a)) + const peerId = PeerId.createFromBytes(peer) - return this.libp2p.dial(peerInfo) + this.libp2p.peerStore.addressBook.set(peerId, addrs) + return this.libp2p.dial(peerId) } /** @@ -85,16 +80,14 @@ class Daemon { async openStream (request) { const { peer, proto } = request.streamOpen - const peerInfo = new PeerInfo( - PeerId.createFromB58String(peer.toString()) - ) + const peerId = PeerId.createFromB58String(peer.toString()) - const connection = this.libp2p.registrar.getConnection(peerInfo) + const connection = this.libp2p.connectionManager.get(peerId) const { stream, protocol } = await connection.newStream(proto) return { streamInfo: { - peer: peerInfo.id.toBytes(), + peer: peerId.toBytes(), addr: connection.remoteAddr.buffer, proto: protocol }, @@ -195,8 +188,8 @@ class Daemon { [PeerstoreRequest.Type.GET_PROTOCOLS]: function * (daemon) { try { const peerId = PeerId.createFromBytes(peerStore.id) - const peerInfo = daemon.libp2p.peerStore.get(peerId) - const protos = Array.from(peerInfo.protocols) + const peer = daemon.libp2p.peerStore.get(peerId) + const protos = peer.protocols yield OkResponse({ peerStore: { protos } }) } catch (err) { throw new Error('ERR_INVALID_PEERSTORE_REQUEST') @@ -277,12 +270,13 @@ class Daemon { const peerId = PeerId.createFromBytes(dht.peer) try { const peer = await daemon.libp2p.peerRouting.findPeer(peerId) + yield OkResponse({ dht: { type: DHTResponse.Type.VALUE, peer: { id: peer.id.toBytes(), - addrs: peer.multiaddrInfos.map(mi => mi.multiaddr.buffer) + addrs: peer.multiaddrs.map(m => m.buffer) } } }) @@ -311,7 +305,7 @@ class Daemon { type: DHTResponse.Type.VALUE, peer: { id: provider.id.toBytes(), - addrs: provider.multiaddrInfos.map(mi => mi.multiaddr.buffer) + addrs: (provider.multiaddrs || []).map(m => m.buffer) } }) } @@ -430,18 +424,21 @@ class Daemon { case Request.Type.IDENTIFY: { yield OkResponse({ identify: { - id: daemon.libp2p.peerInfo.id.toBytes(), - addrs: daemon.libp2p.peerInfo.multiaddrs.toArray().map(m => m.buffer) + id: daemon.libp2p.peerId.toBytes(), + addrs: daemon.libp2p.multiaddrs.map(m => m.buffer) } }) break } // Get a list of our current peers case Request.Type.LIST_PEERS: { - const peers = Array.from(daemon.libp2p.peerStore.peers.values()).map((pi) => { - const addr = pi.isConnected() + const peers = Array.from(daemon.libp2p.peerStore.peers.values()).map((peer) => { + // TODO: conn mgr + const conn = daemon.libp2p.registrar.getConnection(peer.id) + const addr = conn.remoteAddr + return { - id: pi.id.toBytes(), + id: peer.id.toBytes(), addrs: [addr ? addr.buffer : null] } }) diff --git a/src/libp2p.js b/src/libp2p.js index dac8d575..a05f9066 100644 --- a/src/libp2p.js +++ b/src/libp2p.js @@ -10,19 +10,18 @@ const { NOISE } = require('libp2p-noise') const KadDHT = require('libp2p-kad-dht') const FloodSub = require('libp2p-floodsub') const GossipSub = require('libp2p-gossipsub') -const PeerInfo = require('peer-info') const PeerID = require('peer-id') const multiaddr = require('multiaddr') const fsPromises = require('fs').promises /** - * Creates a PeerInfo from scratch, or via the supplied private key + * Creates a Peerid from scratch, or via the supplied private key * @param {string} privateKeyPath Path to private key - * @returns {Promise} Resolves the created PeerInfo + * @returns {Promise} Resolves the created Peerid */ -const getPeerInfo = async (privateKeyPath) => { +const getPeerId = async (privateKeyPath) => { if (!privateKeyPath) { - return PeerInfo.create() + return PeerID.create() } const pkFile = await fsPromises.open(privateKeyPath, 'r') @@ -32,33 +31,7 @@ const getPeerInfo = async (privateKeyPath) => { } finally { pkFile.close() } - const peerId = await PeerID.createFromPrivKey(buf) - - return new PeerInfo(peerId) -} - -class DaemonLibp2p extends Libp2p { - constructor (libp2pOpts, { announceAddrs }) { - super(libp2pOpts) - this.announceAddrs = announceAddrs - this.needsPullStream = libp2pOpts.config.pubsub.enabled - } - - /** - * Starts the libp2p node - * @override - * @returns {Promise} - */ - async start () { - await super.start() - // replace with announce addrs until libp2p supports this directly - if (this.announceAddrs.length > 0) { - this.peerInfo.multiaddrs.clear() - this.announceAddrs.forEach(addr => { - this.peerInfo.multiaddrs.add(addr) - }) - } - } + return PeerID.createFromPrivKey(buf) } /** @@ -90,22 +63,23 @@ const createLibp2p = async ({ pubsub, pubsubRouter } = {}) => { - const peerInfo = await getPeerInfo(id) + const peerId = await getPeerId(id) const bootstrapList = bootstrapPeers ? bootstrapPeers.split(',').filter(s => s !== '') : null const listenAddrs = hostAddrs ? hostAddrs.split(',').filter(s => s !== '') : ['/ip4/0.0.0.0/tcp/0'] announceAddrs = announceAddrs ? announceAddrs.split(',').filter(s => s !== '') : [] announceAddrs = announceAddrs.map(addr => multiaddr(addr)) - listenAddrs.forEach(addr => { - peerInfo.multiaddrs.add(multiaddr(addr)) - }) const connEncryption = [] if (secio !== false) connEncryption.push(SECIO) if (noise) connEncryption.push(NOISE) - const libp2p = new DaemonLibp2p({ - peerInfo, + const libp2p = Libp2p.create({ + peerId, + addresses: { + listen: listenAddrs, + announce: announceAddrs + }, connectionManager: { maxPeers: connMgrHi, minPeers: connMgrLo @@ -148,10 +122,6 @@ const createLibp2p = async ({ enabled: Boolean(pubsub) } } - }, { - // using a secondary config until https://github.com/libp2p/js-libp2p/issues/202 - // is completed - announceAddrs }) return libp2p diff --git a/test/daemon/config.spec.js b/test/daemon/config.spec.js index 2f4e814b..8f85af90 100644 --- a/test/daemon/config.spec.js +++ b/test/daemon/config.spec.js @@ -41,7 +41,7 @@ describe('configuration', function () { await daemon.start() - const addrs = daemon.libp2p.peerInfo.multiaddrs.toArray() + const addrs = Array.from(daemon.libp2p.addressManager.announce) expect(addrs).to.eql([ ma('/dns/ipfs.io') ]) @@ -64,7 +64,7 @@ describe('configuration', function () { }) await daemon.start() - const peerId = daemon.libp2p.peerInfo.id + const peerId = daemon.libp2p.peerId expect(peerId.toB58String()).to.eql('QmPFdSzvgd1HbZSd6oX2N2vCSnhSEeocbQZsMB42UG8smE') }) @@ -85,7 +85,7 @@ describe('configuration', function () { }) await daemon.start() - const peerId = daemon.libp2p.peerInfo.id + const peerId = daemon.libp2p.peerId expect(peerId.toB58String()).to.eql('16Uiu2HAm7txvwZbeK5g3oB3DrRhnARTEjTNorVreWJomfHJHbEu2') }) }) diff --git a/test/daemon/core.spec.js b/test/daemon/core.spec.js index 3e55f27a..361a04a7 100644 --- a/test/daemon/core.spec.js +++ b/test/daemon/core.spec.js @@ -82,8 +82,8 @@ describe('core features', () => { const request = { type: Request.Type.CONNECT, connect: { - peer: Buffer.from(libp2pPeer.peerInfo.id.toBytes()), - addrs: libp2pPeer.peerInfo.multiaddrs.toArray().map(addr => addr.buffer) + peer: Buffer.from(libp2pPeer.peerId.toBytes()), + addrs: libp2pPeer.multiaddrs.map(addr => addr.buffer) }, streamOpen: null, streamHandler: null, @@ -150,8 +150,8 @@ describe('core features', () => { expect(response.type).to.eql(Response.Type.OK) expect(response.identify).to.eql({ - id: daemon.libp2p.peerInfo.id.toBytes(), - addrs: daemon.libp2p.peerInfo.multiaddrs.toArray().map(m => m.buffer) + id: daemon.libp2p.peerId.toBytes(), + addrs: daemon.libp2p.multiaddrs.map(m => m.buffer) }) streamHandler.close() }) diff --git a/test/daemon/dht.spec.js b/test/daemon/dht.spec.js index 082aa335..7b1091ec 100644 --- a/test/daemon/dht.spec.js +++ b/test/daemon/dht.spec.js @@ -97,7 +97,7 @@ describe('dht', () => { streamHandler: null, dht: { type: DHTRequest.Type.FIND_PEER, - peer: libp2pPeer.peerInfo.id.toBytes() + peer: libp2pPeer.peerId.toBytes() }, disconnect: null, pubsub: null, @@ -111,8 +111,8 @@ describe('dht', () => { expect(response.dht).to.eql({ type: DHTResponse.Type.VALUE, peer: { - id: libp2pPeer.peerInfo.id.toBytes(), - addrs: libp2pPeer.peerInfo.multiaddrs.toArray().map(m => m.buffer) + id: libp2pPeer.peerId.toBytes(), + addrs: libp2pPeer.multiaddrs.map(m => m.buffer) }, value: null }) @@ -178,7 +178,7 @@ describe('dht', () => { for await (const provider of libp2pPeer.contentRouting.findProviders(cid, { maxNumProviders: 1 })) { providers.push(provider) } - expect(daemon.libp2p.peerInfo.id.isEqual(providers[0].id)).to.eql(true) + expect(daemon.libp2p.peerId.isEqual(providers[0].id)).to.eql(true) }) it('should be able to find providers', async () => { @@ -223,8 +223,8 @@ describe('dht', () => { const response = DHTResponse.decode(message) expect(response.type).to.eql(DHTResponse.Type.VALUE) expect(response.peer).to.eql({ - id: libp2pPeer.peerInfo.id.toBytes(), - addrs: libp2pPeer.peerInfo.multiaddrs.toArray().map(m => m.buffer) + id: libp2pPeer.peerId.toBytes(), + addrs: libp2pPeer.multiaddrs.map(m => m.buffer) }) }, (message) => { @@ -319,7 +319,7 @@ describe('dht', () => { (message) => { const response = DHTResponse.decode(message) expect(response.type).to.eql(DHTResponse.Type.VALUE) - expect(response.value.toString()).to.eql(libp2pPeer.peerInfo.id.toB58String()) + expect(response.value.toString()).to.eql(libp2pPeer.peerId.toB58String()) }, (message) => { const response = DHTResponse.decode(message) @@ -348,7 +348,7 @@ describe('dht', () => { streamHandler: null, dht: { type: DHTRequest.Type.GET_PUBLIC_KEY, - peer: libp2pPeer.peerInfo.id.toBytes() + peer: libp2pPeer.peerId.toBytes() }, disconnect: null, pubsub: null, @@ -362,7 +362,7 @@ describe('dht', () => { expect(response.dht).to.eql({ type: DHTResponse.Type.VALUE, peer: null, - value: libp2pPeer.peerInfo.id.pubKey.bytes + value: libp2pPeer.peerId.pubKey.bytes }) streamHandler.close() }) diff --git a/test/daemon/peerstore.spec.js b/test/daemon/peerstore.spec.js index f59d4cdb..01a14b93 100644 --- a/test/daemon/peerstore.spec.js +++ b/test/daemon/peerstore.spec.js @@ -89,7 +89,7 @@ describe('peerstore features', () => { type: Request.Type.PEERSTORE, peerStore: { type: PeerstoreRequest.Type.GET_PROTOCOLS, - id: Buffer.from(libp2pPeer.peerInfo.id.toBytes()) + id: Buffer.from(libp2pPeer.peerId.toBytes()) } } @@ -121,7 +121,7 @@ describe('peerstore features', () => { type: Request.Type.PEERSTORE, peerStore: { type: PeerstoreRequest.Type.GET_PEER_INFO, - id: Buffer.from(libp2pPeer.peerInfo.id.toBytes()) + id: Buffer.from(libp2pPeer.peerId.toBytes()) } } diff --git a/test/daemon/pubsub.spec.js b/test/daemon/pubsub.spec.js index 75b6f419..08febd66 100644 --- a/test/daemon/pubsub.spec.js +++ b/test/daemon/pubsub.spec.js @@ -282,7 +282,7 @@ const testPubsub = (router) => { }, (message) => { const response = PSMessage.decode(message) - expect(response.from.toString()).to.eql(libp2pPeer.peerInfo.id.toB58String()) + expect(response.from.toString()).to.eql(libp2pPeer.peerId.toB58String()) expect(response.data).to.exist() expect(response.data).to.equalBytes(data) expect(response.topicIDs).to.eql([topic]) diff --git a/test/daemon/streams.spec.js b/test/daemon/streams.spec.js index 908bce4f..70a4cb3f 100644 --- a/test/daemon/streams.spec.js +++ b/test/daemon/streams.spec.js @@ -98,7 +98,7 @@ describe('streams', function () { type: Request.Type.STREAM_OPEN, connect: null, streamOpen: { - peer: Buffer.from(libp2pPeer.peerInfo.id.toB58String()), + peer: Buffer.from(libp2pPeer.peerId.toB58String()), proto: ['/echo/1.0.0'] }, streamHandler: null, @@ -112,11 +112,11 @@ describe('streams', function () { // Verify the response const response = Response.decode(await streamHandler.read()) expect(response.type).to.eql(Response.Type.OK) - expect(response.streamInfo).to.have.deep.property('peer', libp2pPeer.peerInfo.id.toBytes()) + expect(response.streamInfo).to.have.deep.property('peer', libp2pPeer.peerId.toBytes()) expect(response.streamInfo).to.have.property('proto', '/echo/1.0.0') expect(response.streamInfo.addr).to.satisfy(function (buffer) { - const addrs = libp2pPeer.peerInfo.multiaddrs.toArray() - return addrs.filter(addr => buffer.equals(addr.encapsulate(`/p2p/${libp2pPeer.peerInfo.id.toB58String()}`).buffer)).length > 0 + const addrs = libp2pPeer.multiaddrs + return addrs.filter(addr => buffer.equals(addr.encapsulate(`/p2p/${libp2pPeer.peerId.toB58String()}`).buffer)).length > 0 }, 'Did not contain a valid multiaddr') const source = require('it-pushable')() @@ -149,7 +149,7 @@ describe('streams', function () { const message = await streamHandler.read() const response = StreamInfo.decode(message) - expect(response.peer).to.eql(libp2pPeer.peerInfo.id.toBytes()) + expect(response.peer).to.eql(libp2pPeer.peerId.toBytes()) expect(response.proto).to.eql('/echo/1.0.0') const stream = streamHandler.rest() @@ -179,7 +179,7 @@ describe('streams', function () { // Open a connection between the peer and our daemon // Then send hello from the peer to the daemon - const connection = await libp2pPeer.dial(daemon.libp2p.peerInfo) + const connection = await libp2pPeer.dial(daemon.libp2p.peerId) const { stream } = await connection.newStream('/echo/1.0.0') const hello = Buffer.from('hello, peer') @@ -189,7 +189,6 @@ describe('streams', function () { toBuffer, collect ) - await connection.close() expect(results).to.eql([hello]) }) diff --git a/test/util/index.js b/test/util/index.js index 106041ec..4380fe24 100644 --- a/test/util/index.js +++ b/test/util/index.js @@ -28,8 +28,8 @@ async function connect ({ const request = { type: Request.Type.CONNECT, connect: { - peer: Buffer.from(libp2pPeer.peerInfo.id.toBytes()), - addrs: libp2pPeer.peerInfo.multiaddrs.toArray().map(addr => addr.buffer) + peer: Buffer.from(libp2pPeer.peerId.toBytes()), + addrs: libp2pPeer.multiaddrs.map(addr => addr.buffer) } }