hypercore-protocol / p2p-multiwriter-with-autobase

p2p posting & voting workshop
69 stars 15 forks source link

Problem 1: UnhandledPromiseRejectionWarning: Error: Could not find peer #3

Closed raphael10-collab closed 3 years ago

raphael10-collab commented 3 years ago

Problem 1:

(base) raphy@pc:~/hypercore/p2p-multiwriter-with-autobase/solutions/01$ node server.js 
Connect to:
53d3d0874347e444280f569d9b39e42cbe765d899fbf0f3f055027a26e91c28e

client.js :

import DHT from '@hyperswarm/dht'

const node = new DHT()

const remotePublicKey = Buffer.from('53d3d0874347e444280f569d9b39e42cbe765d899fbf0f3f055027a26e91c28', 'hex')
const encryptedSocket = node.connect(remotePublicKey)

encryptedSocket.on('open', function () {
  console.log('Connected to server')
})

encryptedSocket.on('data', function (data) {
  console.log('Remote said:', data.toString())
})

I get this error:

(base) raphy@pc:~/hypercore/p2p-multiwriter-with-autobase/solutions/01$ node client.js 
(node:192648) UnhandledPromiseRejectionWarning: Error: Could not find peer
    at findAndConnect (/home/raphy/hypercore/p2p-multiwriter-with-autobase/solutions/01/node_modules/@hyperswarm/dht/lib/connect.js:163:83)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async connectAndHolepunch (/home/raphy/hypercore/p2p-multiwriter-with-autobase/solutions/01/node_modules/@hyperswarm/dht/lib/connect.js:48:3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:192648) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:192648) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

How to solve the issue?

mafintosh commented 3 years ago

Can you share server.js

raphael10-collab commented 3 years ago

This is server.js :

/ / https://github.com/hypercore-protocol/p2p-multiwriter-with-autobase/tree/main/problems/01

import DHT from '@hyperswarm/dht'

// Make a Hyperswarm DHT node that connects to the global network.
const node = new DHT()

const server = node.createServer(function (encryptedSocket) {
  // Called when a new connection arrives.
  console.log('New connection from', encryptedSocket.remotePublicKey.toString('hex'))
  encryptedSocket.write('Hello world!')
  encryptedSocket.end()
})

const keyPair = DHT.keyPair()
await server.listen(keyPair)

// Server is now listening.
console.log('Connect to:')
console.log(keyPair.publicKey.toString('hex'))

I just tried again, without modifying anything ( apart from changing the key's value in client.js), and it works fine :

(base) raphy@pc:~/hypercore/p2p-multiwriter-with-autobase/solutions/01$ node server.js 
Connect to:
2914c849f98e816c2ed2380bc0820c4487ad2b9f4aaa1e62002513d8332962ae
New connection from e16382517194c57e2e8413df14c847578dc57bd62d0e42b23d1430128743fc76

(base) raphy@pc:~/hypercore/p2p-multiwriter-with-autobase/solutions/01$ node client.js 
Connected to server
Remote said: Hello world!

client.js :

// https://github.com/hypercore-protocol/p2p-multiwriter-with-autobase/tree/main/problems/01
import DHT from '@hyperswarm/dht'

const node = new DHT()

const remotePublicKey = Buffer.from('2914c849f98e816c2ed2380bc0820c4487ad2b9f4aaa1e62002513d8332962ae', 'hex')
const encryptedSocket = node.connect(remotePublicKey)

encryptedSocket.on('open', function () {
  console.log('Connected to server')
})

encryptedSocket.on('data', function (data) {
  console.log('Remote said:', data.toString())

})

mafintosh commented 3 years ago

Yep works for me also, you prob tried to connect before the listen was done, or a retry was happening due to some fluke