Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update peer store api and libp2p api without peer-info #38

Merged
merged 4 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@
"it-length-prefixed": "^3.0.0",
"it-pipe": "^1.1.0",
"it-pushable": "^1.4.0",
"libp2p": "^0.27.6",
"libp2p-bootstrap": "^0.10.3",
"libp2p-floodsub": "^0.20.0",
"libp2p-gossipsub": "^0.3.0",
"libp2p-kad-dht": "^0.18.3",
"libp2p": "^0.28.0",
"libp2p-bootstrap": "^0.11.0",
"libp2p-floodsub": "^0.21.3",
"libp2p-gossipsub": "^0.4.5",
"libp2p-kad-dht": "^0.19.5",
"libp2p-mplex": "^0.9.3",
"libp2p-noise": "^1.0.0",
"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.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
},
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.toB58String())
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')
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.multiaddrs.toArray().map(m => m.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.multiaddrs.toArray().map(m => m.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.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]
}
})
Expand Down
54 changes: 12 additions & 42 deletions src/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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<void>}
*/
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)
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
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 = Array.from(daemon.libp2p.addressManager.announce)
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 @@ -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,
Expand Down Expand Up @@ -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()
})
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 @@ -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,
Expand All @@ -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
})
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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()
})
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 @@ -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())
}
}

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

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 @@ -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])
Expand Down
Loading