This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle peer-info validation errors (#887)
BREAKING CHANGE. Previously swarm.peers would throw an uncaught error if any peer in the reponse could have its peerId or multiaddr validated. This PR catches errors that occur while validating the peer info. The returned array will contain an entry for every peer in the ipfs response. peer-info objects that couldn't be validated, now have an `error` property and a `rawPeerInfo` property. This at least means the count of peers in the response will be accurate, and there the info is available to the caller. This means that callers now have to deal with peer-info objects that may not have a `peer or `addr` property. Adds `nock` tests to exercice the code under different error conditions. Doing so uncovered a bug in our legacy go-ipfs <= 0.4.4 peer info parsing, which is also fixed. The code was trying to decapusalate the peerId from the multiaddr, but doing so trims the peerId rather than returning it. fixes #885 License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>
- Loading branch information
Showing
4 changed files
with
165 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict' | ||
require('./node/swarm') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const nock = require('nock') | ||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
chai.use(dirtyChai) | ||
|
||
const IPFSApi = require('../../src') | ||
|
||
describe('.swarm.peers', function () { | ||
this.timeout(50 * 1000) // slow CI | ||
|
||
const ipfs = IPFSApi('/ip4/127.0.0.1/tcp/5001') | ||
const apiUrl = 'http://127.0.0.1:5001' | ||
|
||
it('handles a peer response', (done) => { | ||
const response = { Peers: [{ Addr: '/ip4/104.131.131.82/tcp/4001', Peer: 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', Latency: '', Muxer: '', Streams: null }] } | ||
|
||
const scope = nock(apiUrl) | ||
.post('/api/v0/swarm/peers') | ||
.query(true) | ||
.reply(200, response) | ||
|
||
ipfs.swarm.peers((err, res) => { | ||
expect(err).to.not.exist() | ||
expect(res).to.be.a('array') | ||
expect(res.length).to.equal(1) | ||
expect(res[0].error).to.not.exist() | ||
expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr) | ||
expect(res[0].peer.toB58String()).to.equal(response.Peers[0].Peer) | ||
expect(scope.isDone()).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
|
||
it('handles a go-ipfs <= 0.4.4 peer response', (done) => { | ||
const response = { Strings: ['/ip4/73.109.217.59/tcp/49311/ipfs/QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm'] } | ||
|
||
const scope = nock(apiUrl) | ||
.post('/api/v0/swarm/peers') | ||
.query(true) | ||
.reply(200, response) | ||
|
||
ipfs.swarm.peers((err, res) => { | ||
expect(err).to.not.exist() | ||
expect(res).to.be.a('array') | ||
expect(res.length).to.equal(1) | ||
expect(res[0].error).to.not.exist() | ||
expect(res[0].addr.toString()).to.equal('/ip4/73.109.217.59/tcp/49311/ipfs/QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm') | ||
expect(res[0].peer.toB58String()).to.equal('QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm') | ||
expect(scope.isDone()).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
|
||
it('handles an ip6 quic peer', (done) => { | ||
const response = { Peers: [{ Addr: '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/4001/quic', Peer: 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC', Latency: '', Muxer: '', Streams: null }] } | ||
|
||
const scope = nock(apiUrl) | ||
.post('/api/v0/swarm/peers') | ||
.query(true) | ||
.reply(200, response) | ||
|
||
ipfs.swarm.peers((err, res) => { | ||
expect(err).to.not.exist() | ||
expect(res).to.be.a('array') | ||
expect(res.length).to.equal(1) | ||
expect(res[0].error).to.not.exist() | ||
expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr) | ||
expect(res[0].peer.toB58String()).to.equal(response.Peers[0].Peer) | ||
expect(scope.isDone()).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
|
||
it('handles unvalidatable peer addr', (done) => { | ||
const response = { Peers: [{ Addr: '/ip4/104.131.131.82/future-tech', Peer: 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC', Latency: '', Muxer: '', Streams: null }] } | ||
|
||
const scope = nock(apiUrl) | ||
.post('/api/v0/swarm/peers') | ||
.query(true) | ||
.reply(200, response) | ||
|
||
ipfs.swarm.peers((err, res) => { | ||
expect(err).to.not.exist() | ||
expect(res).to.be.a('array') | ||
expect(res.length).to.equal(1) | ||
expect(res[0].error).to.exist() | ||
expect(res[0].rawPeerInfo).to.deep.equal(response.Peers[0]) | ||
expect(scope.isDone()).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
|
||
it('handles an error response', (done) => { | ||
const scope = nock(apiUrl) | ||
.post('/api/v0/swarm/peers') | ||
.query(true) | ||
.replyWithError('something awful happened') | ||
|
||
ipfs.swarm.peers((err, res) => { | ||
expect(err.message).to.equal('something awful happened') | ||
expect(res).to.not.exist() | ||
expect(scope.isDone()).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
}) |