Skip to content

Commit

Permalink
fix: ping multiaddr from peer not previously stored in peerstore (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Jul 27, 2020
1 parent 6c7e5e5 commit 2440c87
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class Libp2p extends EventEmitter {
* @returns {Promise<Connection|*>}
*/
async dialProtocol (peer, protocols, options) {
const { id, multiaddrs } = getPeer(peer, this.peerStore)
const { id, multiaddrs } = getPeer(peer)
let connection = this.connectionManager.get(id)

if (!connection) {
Expand Down Expand Up @@ -396,7 +396,12 @@ class Libp2p extends EventEmitter {
* @returns {Promise<number>}
*/
ping (peer) {
const { id } = getPeer(peer)
const { id, multiaddrs } = getPeer(peer)

// If received multiaddr, ping it
if (multiaddrs) {
return ping(this, multiaddrs[0])
}

return ping(this, id)
}
Expand Down
4 changes: 2 additions & 2 deletions src/ping/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const { PROTOCOL, PING_LENGTH } = require('./constants')
/**
* Ping a given peer and wait for its response, getting the operation latency.
* @param {Libp2p} node
* @param {PeerId} peer
* @param {PeerId|multiaddr} peer
* @returns {Promise<Number>}
*/
async function ping (node, peer) {
log('dialing %s to %s', PROTOCOL, peer.toB58String())
log('dialing %s to %s', PROTOCOL, peer.toB58String ? peer.toB58String() : peer)

const { stream } = await node.dialProtocol(peer, PROTOCOL)

Expand Down
11 changes: 9 additions & 2 deletions test/core/ping.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ describe('ping', () => {

beforeEach(async () => {
nodes = await peerUtils.createPeer({
number: 2,
number: 3,
config: baseOptions
})

nodes[0].peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)
nodes[1].peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
})

it('ping once from peer0 to peer1', async () => {
it('ping once from peer0 to peer1 using a multiaddr', async () => {
const ma = `${nodes[2].multiaddrs[0]}/p2p/${nodes[2].peerId.toB58String()}`
const latency = await nodes[0].ping(ma)

expect(latency).to.be.a('Number')
})

it('ping once from peer0 to peer1 using a peerId', async () => {
const latency = await nodes[0].ping(nodes[1].peerId)

expect(latency).to.be.a('Number')
Expand Down

0 comments on commit 2440c87

Please sign in to comment.