-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ping multiaddr from peer not previously stored in peerstore
- Loading branch information
1 parent
6c7e5e5
commit 18690b9
Showing
4 changed files
with
70 additions
and
6 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,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() |
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