js-libp2p
js-libp2p copied to clipboard
ping is not responding on local tcp node
- Version:
node 16.13
"@chainsafe/libp2p-noise": "^7.0.1",
"@libp2p/mplex": "^4.0.0",
"@libp2p/tcp": "^3.0.1",
"@libp2p/websockets": "^3.0.0",
"libp2p": "^0.37.3",
"multiaddr": "^10.0.1"
-
Platform: MacOS
-
Subsystem:
Severity:
High - The main functionality of the application does not work, API breakage, repo format breakage, etc.
Description:
I am trying to run the base tutorial ping project. When I try to ping the node, it never returns:
libp2p has started
listening on addresses:
/ip4/127.0.0.1/tcp/63244/p2p/12D3KooWMuhGCu75cebfNE96DLQyAWRcz6aY9dKAWDCRd4GiZM3n
pinging remote peer at /ip4/127.0.0.1/tcp/63177/p2p/12D3KooWDCn4xg5TqtQjTYBvdSK5wKU7EeovnXZ9ZTCEPWT5B9Tc
Steps to reproduce the error:
run the following index.js file
import process from "node:process"
import { createLibp2p } from "libp2p"
import { TCP } from "@libp2p/tcp"
import { Noise } from "@chainsafe/libp2p-noise"
import { Mplex } from "@libp2p/mplex"
import { multiaddr } from "multiaddr"
const main = async () => {
const node = await createLibp2p({
addresses: {
// add a listen address (localhost) to accept TCP connections on a random port
listen: ["/ip4/127.0.0.1/tcp/0"],
},
transports: [new TCP()],
connectionEncryption: [new Noise()],
streamMuxers: [new Mplex()],
})
// start libp2p
await node.start()
console.log("libp2p has started")
// print out listening addresses
console.log("listening on addresses:")
node.getMultiaddrs().forEach((addr) => {
console.log(addr.toString())
})
// 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()
Terminal 1:
node src/index.js
output:
libp2p has started
listening on addresses:
/ip4/127.0.0.1/tcp/63177/p2p/12D3KooWDCn4xg5TqtQjTYBvdSK5wKU7EeovnXZ9ZTCEPWT5B9Tc
no remote peer address given, skipping ping
Terminal 2:
node src/index.js /ip4/127.0.0.1/tcp/63177/p2p/12D3KooWDCn4xg5TqtQjTYBvdSK5wKU7EeovnXZ9ZTCEPWT5B9Tc
output (get stuck here):
libp2p has started
listening on addresses:
/ip4/127.0.0.1/tcp/63244/p2p/12D3KooWMuhGCu75cebfNE96DLQyAWRcz6aY9dKAWDCRd4GiZM3n
pinging remote peer at /ip4/127.0.0.1/tcp/63177/p2p/12D3KooWDCn4xg5TqtQjTYBvdSK5wKU7EeovnXZ9ZTCEPWT5B9Tc
the same code using old version with CommonJS works fine:
"dependencies": {
"@libp2p/tcp": "^3.0.1",
"libp2p": "^0.36.2",
"libp2p-mplex": "^0.10.7",
"libp2p-noise": "^4.0.0",
"multiaddr": "^10.0.1"
}
import process from "node:process"
import { createLibp2p } from "libp2p"
import { TCP } from "@libp2p/tcp"
import { Noise } from "@chainsafe/libp2p-noise"
import { Mplex } from "@libp2p/mplex"
import { multiaddr } from "multiaddr"
const main = async () => {
const node = await createLibp2p({
addresses: {
// add a listen address (localhost) to accept TCP connections on a random port
listen: ["/ip4/127.0.0.1/tcp/0"],
},
transports: [new TCP()],
connectionEncryption: [new Noise()],
streamMuxers: [new Mplex()],
})
// start libp2p
await node.start()
console.log("libp2p has started")
// print out listening addresses
console.log("listening on addresses:")
node.getMultiaddrs().forEach((addr) => {
console.log(addr.toString())
})
// 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()
@calummoore does this still happening with latest version of dependencies?
in fact, when I changed the lib versions to these bellow, it worked:
"dependencies": {
"@chainsafe/libp2p-noise": "^8.0.1",
"@libp2p/mplex": "^5.1.0",
"@libp2p/tcp": "^3.0.4",
"@libp2p/websockets": "^3.0.2",
"libp2p": "^0.38.0",
"multiaddr": "^10.0.1"
}
I created a PR to the existing docs to explicitly add lib versions: https://github.com/libp2p/docs/pull/173
Closing as it works with latest version of dependencies