Skip to content

Commit

Permalink
fix: ping multiaddr from peer not previously stored in peerstore
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 27, 2020
1 parent 6c7e5e5 commit 18690b9
Show file tree
Hide file tree
Showing 4 changed files with 70 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
52 changes: 52 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const Libp2p = require('./')
const TCP = require('libp2p-tcp')
const SECIO = require('libp2p-secio')
const MPLEX = require('libp2p-mplex')

const multiaddr = require('multiaddr')

const main = async () => {
const node = await Libp2p.create({
addresses: {
// add a listen address (localhost) to accept TCP connections on a random port
listen: ['/ip4/127.0.0.1/tcp/0']
},
modules: {
transport: [TCP],
connEncryption: [SECIO],
streamMuxer: [MPLEX]
}
})

// start libp2p
await node.start()
console.log('libp2p has started')

// print out listening addresses
console.log('listening on addresses:')
node.multiaddrs.forEach(addr => {
console.log(`${addr.toString()}/p2p/${node.peerId.toB58String()}`)
})

// ping peer if received multiaddr
if (process.argv.length >= 3) {
const ma = multiaddr(process.argv[2])
console.log(`pinging remote peer at ${process.argv[2]}`)
const latency = await node.ping(ma)
console.log(`pinged ${process.argv[2]} in ${latency}ms`)
} else {
console.log('no remote peer address given, skipping ping')
}

const stop = async () => {
// stop libp2p
await node.stop()
console.log('libp2p has stopped')
process.exit(0)
}

process.on('SIGTERM', stop)
process.on('SIGINT', stop)
}

main()
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 18690b9

Please sign in to comment.