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

Commit

Permalink
feat: update to the latest libp2p
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Apr 3, 2017
1 parent c680596 commit aca4297
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/cli/commands/swarm/addrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ module.exports = {
}

res.forEach((peer) => {
const count = peer.multiaddrs.length
const count = peer.multiaddrs.size
console.log(`${peer.id.toB58String()} (${count})`)

peer.multiaddrs.forEach((addr) => {
const res = addr.decapsulate('ipfs').toString()
console.log(`\t${res}`)
Expand Down
1 change: 1 addition & 0 deletions src/core/components/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function id (self) {
id: self._peerInfo.id.toB58String(),
publicKey: self._peerInfo.id.pubKey.bytes.toString('base64'),
addresses: self._peerInfo.multiaddrs
.toArray()
.map((ma) => ma.toString())
.sort(),
agentVersion: 'js-ipfs',
Expand Down
10 changes: 3 additions & 7 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ module.exports = function preStart (self) {
(config, cb) => {
const privKey = config.Identity.PrivKey

peerId.createFromPrivKey(privKey, (err, id) => {
cb(err, config, id)
})
peerId.createFromPrivKey(privKey, (err, id) => cb(err, config, id))
},
(config, id, cb) => {
self._peerInfo = new PeerInfo(id)
Expand All @@ -29,12 +27,10 @@ module.exports = function preStart (self) {
let ma = multiaddr(addr)

if (!mafmt.IPFS.matches(ma)) {
ma = ma.encapsulate(
'/ipfs/' + self._peerInfo.id.toB58String()
)
ma = ma.encapsulate('/ipfs/' + self._peerInfo.id.toB58String())
}

self._peerInfo.multiaddr.add(ma)
self._peerInfo.multiaddrs.add(ma)
})

cb()
Expand Down
11 changes: 5 additions & 6 deletions src/core/components/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function swarm (self) {
const peerList = flatMap(keys, (id) => {
const peer = peers[id]

return peer.multiaddrs.map((addr) => {
return peer.multiaddrs.toArray().map((addr) => {
const res = {
addr: addr,
peer: peers[id]
Expand All @@ -53,6 +53,8 @@ module.exports = function swarm (self) {
}

const peers = values(self._peerInfoBook.getAll())
.filter((peer) => peer.isConnected())

callback(null, peers)
}),

Expand All @@ -61,7 +63,7 @@ module.exports = function swarm (self) {
return callback(OFFLINE_ERROR)
}

callback(null, self._libp2pNode.peerInfo.multiaddrs)
callback(null, self._libp2pNode.peerInfo.multiaddrs.toArray())
}),

connect: promisify((maddr, callback) => {
Expand All @@ -88,9 +90,6 @@ module.exports = function swarm (self) {
self._libp2pNode.hangUp(maddr, callback)
}),

filters: promisify((callback) => {
// TODO
throw new Error('Not implemented')
})
filters: promisify((callback) => callback(new Error('Not implemented')))
}
}
3 changes: 2 additions & 1 deletion src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ exports.addrs = {

const addrs = {}
peers.forEach((peer) => {
addrs[peer.id.toB58String()] = peer.multiaddrs.map((addr) => addr.toString())
addrs[peer.id.toB58String()] = peer.multiaddrs.toArray()
.map((addr) => addr.toString())
})

return reply({
Expand Down
43 changes: 12 additions & 31 deletions test/core/bitswap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function makeBlock (cb) {

describe('bitswap', () => {
let inProcNode // Node spawned inside this process
// let swarmAddrsBak

beforeEach((done) => {
const repo = createTempRepo()
Expand Down Expand Up @@ -73,50 +72,34 @@ describe('bitswap', () => {
inProcNode.on('start', () => done())
})

afterEach((done) => {
inProcNode.on('stop', () => done())
inProcNode.stop()
})
afterEach((done) => inProcNode.stop(() => done()))

describe('connections', () => {
function wire (targetNode, dialerNode, done) {
targetNode.id((err, identity) => {
expect(err).to.not.exist()
const addr = identity.addresses
.map((addr) => {
const ma = multiaddr(addr.toString().split('ipfs')[0])
return ma
})
.filter((addr) => {
return _.includes(addr.protoNames(), 'ws')
})[0]

let targetAddr
if (addr) {
targetAddr = addr.encapsulate(multiaddr(`/ipfs/${identity.id}`)).toString()
targetAddr = targetAddr.replace('0.0.0.0', '127.0.0.1')
} else {
// Note: the browser doesn't have a websockets listening addr
.map((addr) => multiaddr(addr.toString().split('ipfs')[0]))
.filter((addr) => _.includes(addr.protoNames(), 'ws'))[0]

// What we really need is a way to dial to a peerId only and another
// to dial to peerInfo
if (!addr) {
// Note: the browser doesn't have a websockets listening addr
return done()
// targetAddr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws/ipfs/${identity.id}`).toString()
}

const targetAddr = addr
.encapsulate(multiaddr(`/ipfs/${identity.id}`)).toString()
.replace('0.0.0.0', '127.0.0.1')

dialerNode.swarm.connect(targetAddr, done)
})
}

function connectNodes (remoteNode, ipn, done) {
series([
(cb) => {
wire(remoteNode, ipn, cb)
},
(cb) => wire(remoteNode, ipn, cb),
(cb) => setTimeout(() => {
// need timeout so we wait for identify
// to happen.

// need timeout so we wait for identify to happen.
// This call is just to ensure identify happened
wire(ipn, remoteNode, cb)
}, 300)
Expand All @@ -129,9 +112,7 @@ describe('bitswap', () => {
const apiUrl = `/ip4/127.0.0.1/tcp/31${num}`
const remoteNode = new API(apiUrl)

connectNodes(remoteNode, inProcNode, (err) => {
done(err, remoteNode)
})
connectNodes(remoteNode, inProcNode, (err) => done(err, remoteNode))
}

describe('fetches a remote block', () => {
Expand Down

0 comments on commit aca4297

Please sign in to comment.