chore: remove peer-info usage

BREAKING CHANGE: all API methods with peer-info parameters or return values were changed. You can check the API.md document, in order to check the new values to use
This commit is contained in:
Vasco Santos
2020-04-14 14:05:30 +02:00
committed by Jacob Heun
parent ed6d5bb4b4
commit 12e48adafa
45 changed files with 608 additions and 695 deletions

View File

@ -12,7 +12,6 @@ const Muxer = require('libp2p-mplex')
const Crypto = require('libp2p-secio')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const delay = require('delay')
const pDefer = require('p-defer')
const pSettle = require('p-settle')
@ -32,7 +31,7 @@ const swarmKeyBuffer = Buffer.from(require('../fixtures/swarm.key'))
const mockUpgrader = require('../utils/mockUpgrader')
const createMockConnection = require('../utils/mockConnection')
const Peers = require('../fixtures/peers')
const { createPeerInfo } = require('../utils/creators/peer')
const { createPeerId } = require('../utils/creators/peer')
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')
@ -81,9 +80,7 @@ describe('Dialing (direct, TCP)', () => {
it('should be able to connect to a remote node via its stringified multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM, peerStore })
const dialable = Dialer.getDialable(remoteAddr.toString())
const connection = await dialer.connectToPeer(dialable)
const connection = await dialer.connectToPeer(remoteAddr.toString())
expect(connection).to.exist()
await connection.close()
})
@ -96,24 +93,6 @@ describe('Dialing (direct, TCP)', () => {
.and.to.have.nested.property('._errors[0].code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
})
it('should be able to connect to a given peer info', async () => {
const dialer = new Dialer({
transportManager: localTM,
peerStore: {
addressBook: {
add: () => {},
getMultiaddrsForPeer: () => [remoteAddr]
}
}
})
const peerId = await PeerId.createFromJSON(Peers[0])
const peerInfo = new PeerInfo(peerId)
const connection = await dialer.connectToPeer(peerInfo)
expect(connection).to.exist()
await connection.close()
})
it('should be able to connect to a given peer id', async () => {
const peerStore = new PeerStore()
const dialer = new Dialer({
@ -122,11 +101,9 @@ describe('Dialing (direct, TCP)', () => {
})
const peerId = await PeerId.createFromJSON(Peers[0])
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(remoteAddr)
peerStore.addressBook.set(peerInfo.id, peerInfo.multiaddrs.toArray())
peerStore.addressBook.set(peerId, [remoteAddr])
const connection = await dialer.connectToPeer(peerInfo)
const connection = await dialer.connectToPeer(peerId)
expect(connection).to.exist()
await connection.close()
})
@ -142,9 +119,8 @@ describe('Dialing (direct, TCP)', () => {
}
})
const peerId = await PeerId.createFromJSON(Peers[0])
const peerInfo = new PeerInfo(peerId)
await expect(dialer.connectToPeer(peerInfo))
await expect(dialer.connectToPeer(peerId))
.to.eventually.be.rejectedWith(AggregateError)
.and.to.have.nested.property('._errors[0].code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
})
@ -191,10 +167,10 @@ describe('Dialing (direct, TCP)', () => {
const deferredDial = pDefer()
sinon.stub(localTM, 'dial').callsFake(() => deferredDial.promise)
const [peerInfo] = await createPeerInfo()
const [peerId] = await createPeerId()
// Perform 3 multiaddr dials
dialer.connectToPeer(peerInfo)
dialer.connectToPeer(peerId)
// Let the call stack run
await delay(0)
@ -213,30 +189,28 @@ describe('Dialing (direct, TCP)', () => {
})
describe('libp2p.dialer', () => {
let peerInfo
let remotePeerInfo
let peerId, remotePeerId
let libp2p
let remoteLibp2p
let remoteAddr
before(async () => {
const [peerId, remotePeerId] = await Promise.all([
[peerId, remotePeerId] = await Promise.all([
PeerId.createFromJSON(Peers[0]),
PeerId.createFromJSON(Peers[1])
])
peerInfo = new PeerInfo(peerId)
remotePeerInfo = new PeerInfo(remotePeerId)
remoteLibp2p = new Libp2p({
peerInfo: remotePeerInfo,
peerId: remotePeerId,
addresses: {
listen: [listenAddr]
},
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
})
remoteLibp2p.peerInfo.multiaddrs.add(listenAddr)
remoteLibp2p.handle('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
await remoteLibp2p.start()
@ -253,7 +227,7 @@ describe('Dialing (direct, TCP)', () => {
it('should fail if no peer id is provided', async () => {
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -266,7 +240,7 @@ describe('Dialing (direct, TCP)', () => {
try {
await libp2p.dial(remoteLibp2p.transportManager.getAddrs()[0])
} catch (err) {
expect(err).to.have.property('code', ErrorCodes.ERR_INVALID_PEER)
expect(err).to.have.property('code', ErrorCodes.ERR_INVALID_MULTIADDR)
return
}
@ -275,7 +249,7 @@ describe('Dialing (direct, TCP)', () => {
it('should use the dialer for connecting to a multiaddr', async () => {
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -296,7 +270,7 @@ describe('Dialing (direct, TCP)', () => {
it('should use the dialer for connecting to a peer', async () => {
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -305,8 +279,9 @@ describe('Dialing (direct, TCP)', () => {
})
sinon.spy(libp2p.dialer, 'connectToPeer')
libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.addresses.listen)
const connection = await libp2p.dial(remotePeerInfo)
const connection = await libp2p.dial(remotePeerId)
expect(connection).to.exist()
const { stream, protocol } = await connection.newStream('/echo/1.0.0')
expect(stream).to.exist()
@ -317,7 +292,7 @@ describe('Dialing (direct, TCP)', () => {
it('should be able to use hangup to close connections', async () => {
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -334,7 +309,7 @@ describe('Dialing (direct, TCP)', () => {
it('should be able to use hangup by address string to close connections', async () => {
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -352,7 +327,7 @@ describe('Dialing (direct, TCP)', () => {
it('should use the protectors when provided for connecting', async () => {
const protector = new Protector(swarmKeyBuffer)
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -375,7 +350,7 @@ describe('Dialing (direct, TCP)', () => {
it('should coalesce parallel dials to the same peer (id in multiaddr)', async () => {
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -384,9 +359,11 @@ describe('Dialing (direct, TCP)', () => {
})
const dials = 10
const fullAddress = remoteAddr.encapsulate(`/p2p/${remoteLibp2p.peerInfo.id.toB58String()}`)
const fullAddress = remoteAddr.encapsulate(`/p2p/${remoteLibp2p.peerId.toB58String()}`)
libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.addresses.listen)
const dialResults = await Promise.all([...new Array(dials)].map((_, index) => {
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerInfo)
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerId)
return libp2p.dial(fullAddress)
}))
@ -403,7 +380,7 @@ describe('Dialing (direct, TCP)', () => {
it('should coalesce parallel dials to the same error on failure', async () => {
libp2p = new Libp2p({
peerInfo,
peerId,
modules: {
transport: [Transport],
streamMuxer: [Muxer],
@ -414,8 +391,9 @@ describe('Dialing (direct, TCP)', () => {
const error = new Error('Boom')
sinon.stub(libp2p.transportManager, 'dial').callsFake(() => Promise.reject(error))
libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.addresses.listen)
const dialResults = await pSettle([...new Array(dials)].map((_, index) => {
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerInfo)
if (index % 2 === 0) return libp2p.dial(remoteLibp2p.peerId)
return libp2p.dial(remoteAddr)
}))