holepunchto / hyperswarm

A distributed networking stack for connecting peers.
https://docs.holepunch.to
MIT License
1.04k stars 84 forks source link

List of topics unavailable at for server connection #117

Open zenineasa opened 1 year ago

zenineasa commented 1 year ago

When a swarm joins a topic as a server, the topic is not available at 'peerInfo.topics'. I have modified a testpoint to showcase the same.

const test = require('brittle')
const { timeout } = require('nonsynchronous')
const createTestnet = require('@hyperswarm/testnet')

const Hyperswarm = require('..')

const CONNECTION_TIMEOUT = 100
const BACKOFFS = [
  100,
  200,
  300
]

test('one server, two clients - topic multiplexing', async (t) => {
    const { bootstrap } = await createTestnet(3, t.teardown)

    const swarm1 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 })
    const swarm2 = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 })

    let clientConnections = 0
    let peerInfoClient = null
    swarm2.on('connection', (conn, info) => {
      clientConnections++
      peerInfoClient = info
      conn.on('error', noop)
    })

    let peerInfoServer = null
    swarm1.on('connection', (conn, info) => {
      peerInfoServer = info
      conn.on('error', noop)
    })

    swarm1.on('connection', (conn) => conn.on('error', noop))

    const topic1 = Buffer.alloc(32).fill('hello world')
    const topic2 = Buffer.alloc(32).fill('hi world')

    await swarm1.join(topic1, { client: false, server: true }).flushed()
    await swarm1.join(topic2, { client: false, server: true }).flushed()
    swarm2.join(topic1, { client: true, server: false })
    swarm2.join(topic2, { client: true, server: false })

    await timeout(250) // 250 ms will be enough for all connections to trigger

    t.is(clientConnections, 1)
    t.is(peerInfoClient.topics.length, 2)
    t.is(peerInfoServer.topics.length, 2) // ERROR: This is not true!!!!

    await swarm1.destroy()
    await swarm2.destroy()
  })

function noop () {}
MKPLKN commented 9 months ago

Expected behavior.

From README.md:

peerInfo.topics An Array of topics that this Peer is associated with -- topics will only be updated when the Peer is in client mode.

cjmont commented 9 months ago

It seems like you've encountered an issue where peerInfoServer.topics.length is not equal to 2 as expected in your test case. This might be due to a variety of reasons, such as a bug in the library, a race condition, or some other unexpected behavior.

Here are a few steps you might take to debug and resolve the issue:

1. Debugging:

2. Check Asynchronous Operations:

3. Check the Library's API and Documentation:

4. Isolate the Issue:

5. Check the Network:

6. Manual Verification:

7. Check for Race Conditions:

8. Check Error Handling:

9. Check the Library's Source Code:

If you've tried all of the above and are still encountering the issue, it might be worth reaching out to the library's maintainers or community for assistance, or even filing a bug report if you believe that the issue is due to a bug in the library.