Skip to content

Commit

Permalink
chore: remove peer-info usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 21, 2020
1 parent cdde4c5 commit eb986d7
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 74 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,17 @@
"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#chore/remove-peer-info-usage",
"libp2p-bootstrap": "^0.11.0",
"libp2p-floodsub": "libp2p/js-libp2p-floodsub#chore/remove-peer-info-usage",
"libp2p-gossipsub": "ChainSafe/gossipsub-js#chore/remove-peer-info-usage",
"libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#chore/use-new-content-and-peer-routing-apis",
"libp2p-mplex": "^0.9.3",
"libp2p-secio": "^0.12.2",
"libp2p-tcp": "^0.14.3",
"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",
Expand Down
43 changes: 20 additions & 23 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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.registrar.getConnection(peerId)
const { stream, protocol } = await connection.newStream(proto)

return {
streamInfo: {
peer: peerInfo.id.toBytes(),
peer: peerId.toBytes(),
addr: connection.remoteAddr.buffer,
proto: protocol
},
Expand Down Expand Up @@ -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 peerData = daemon.libp2p.peerStore.get(peerId)
const protos = peerData.protocols
yield OkResponse({ peerStore: { protos } })
} catch (err) {
throw new Error('ERR_INVALID_PEERSTORE_REQUEST')
Expand Down Expand Up @@ -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)
}
}
})
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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.addresses.listen.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((peerData) => {
// TODO: conn mgr
const conn = daemon.libp2p.registrar.getConnection(peerData.id)
const addr = conn.remoteAddr

return {
id: pi.id.toBytes(),
id: peerData.id.toBytes(),
addrs: [addr ? addr.buffer : null]
}
})
Expand Down
29 changes: 11 additions & 18 deletions src/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ const SECIO = require('libp2p-secio')
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<PeerInfo>} Resolves the created PeerInfo
* @returns {Promise<Peerid>} 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')
Expand All @@ -31,9 +30,7 @@ const getPeerInfo = async (privateKeyPath) => {
} finally {
pkFile.close()
}
const peerId = await PeerID.createFromPrivKey(buf)

return new PeerInfo(peerId)
return PeerID.createFromPrivKey(buf)
}

class DaemonLibp2p extends Libp2p {
Expand All @@ -52,10 +49,7 @@ class DaemonLibp2p extends Libp2p {
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)
})
this.addresses = { listen: this.announceAddrs.map((aA) => multiaddr(aA)) }
}
}
}
Expand Down Expand Up @@ -87,19 +81,18 @@ 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 libp2p = new DaemonLibp2p({
peerInfo,
peerId,
addresses: {
listen: listenAddrs.map((a) => multiaddr(a))
},
connectionManager: {
maxPeers: connMgrHi,
minPeers: connMgrLo
Expand Down
6 changes: 3 additions & 3 deletions test/daemon/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('configuration', function () {

await daemon.start()

const addrs = daemon.libp2p.peerInfo.multiaddrs.toArray()
const addrs = daemon.libp2p.addresses.listen
expect(addrs).to.eql([
ma('/dns/ipfs.io')
])
Expand All @@ -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')
})

Expand All @@ -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')
})
})
8 changes: 4 additions & 4 deletions test/daemon/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,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.addresses.listen.map(addr => addr.buffer)
},
streamOpen: null,
streamHandler: null,
Expand Down Expand Up @@ -146,8 +146,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.addresses.listen.map(m => m.buffer)
})
streamHandler.close()
})
Expand Down
18 changes: 9 additions & 9 deletions test/daemon/dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('dht', () => {
streamHandler: null,
dht: {
type: DHTRequest.Type.FIND_PEER,
peer: libp2pPeer.peerInfo.id.toBytes()
peer: libp2pPeer.peerId.toBytes()
},
disconnect: null,
pubsub: null,
Expand All @@ -107,8 +107,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.addresses.listen.map(m => m.buffer)
},
value: null
})
Expand Down Expand Up @@ -174,7 +174,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 () => {
Expand Down Expand Up @@ -219,8 +219,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.addresses.listen.map(m => m.buffer)
})
},
(message) => {
Expand Down Expand Up @@ -315,7 +315,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)
Expand Down Expand Up @@ -344,7 +344,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,
Expand All @@ -358,7 +358,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()
})
Expand Down
4 changes: 2 additions & 2 deletions test/daemon/peerstore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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())
}
}

Expand Down Expand Up @@ -117,7 +117,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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/daemon/pubsub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,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])
Expand Down
12 changes: 6 additions & 6 deletions test/daemon/streams.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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,
Expand All @@ -108,11 +108,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.addresses.listen
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')()
Expand Down Expand Up @@ -145,7 +145,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()
Expand Down Expand Up @@ -175,7 +175,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')

Expand Down
Loading

0 comments on commit eb986d7

Please sign in to comment.