ChainSafe / js-libp2p-gossipsub

TypeScript implementation of Gossipsub
Apache License 2.0
151 stars 43 forks source link

new found discovered peers not added to peers #481

Open koh-osug opened 10 months ago

koh-osug commented 10 months ago

I'm running two nodes on the same machine and can see:

libp2p:mdns peer found 12D3KooWFvEoC1NJz5Cgcji15WyNGRjo85ytgMi7PJCQnTeTHM2B +0ms libp2p:mdns discovered peer in mDNS query response 12D3KooWFvEoC1NJz5Cgcji15WyNGRjo85ytgMi7PJCQnTeTHM2B +1ms

Now, I would strongly assume that private onPeerConnected must be called to add this peer. But this never happens. The integration seems to be not working. I'm using version ^11.0.1

The code:

import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { tcp } from '@libp2p/tcp'
import defaultsDeep from '@nodeutils/defaults-deep'
import { createLibp2p as create } from 'libp2p'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { mdns } from '@libp2p/mdns'
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'

async function createLibp2p (_options) {
    const defaults = {
        addresses: {
            listen: ['/ip4/0.0.0.0/tcp/0']
        },
        transports: [
            tcp(),
        ],
        streamMuxers: [
            yamux()
        ],
        connectionEncryption: [
            noise()
        ],
        peerDiscovery: [
            mdns({
                interval: 20e3
            }),
            pubsubPeerDiscovery()
        ],
        services: {
            pubsub: gossipsub({
                allowPublishToZeroPeers: true
            })
        }
    }

    return create(defaultsDeep(_options, defaults))
}

async function run () {

    const node = await createLibp2p({
    })
    console.log('node ready, listening on:')
    node.getMultiaddrs().forEach((ma) => {
        console.log(ma.toString())
    })
    node.addEventListener('peer:discovery', (evt) => console.log('Discovered:', evt.detail.id.toString()))

    node.addEventListener('peer:connect', (evt) => {
        const remotePeer = evt.detail
        console.log('connected to: ', remotePeer.toString())
    })

    node.services.pubsub.addEventListener('message', (message) => {
        console.log(`${message.detail.topic}:`, new TextDecoder().decode(message.detail.data))
    })

    node.services.pubsub.subscribe('rendezvous')

    setInterval((async () => {
        try {
            await node.services.pubsub.publish('rendezvous', new TextEncoder().encode('test'))
        }
        catch (e) {
            console.log("error publishing", e)
        }
    }), 10000)

    return node.start()
}

await run()
koh-osug commented 10 months ago

The solution is to add to the config:

        connectionManager: {
        }

This is not covered by the examples or tutorials. Any reasons why? Should I create a PR to fix this?

wemeetagain commented 10 months ago

Yes this seems like a bug. You should not need to explicitly add an empty connection manager configuration.

wemeetagain commented 10 months ago

cc @achingbrain this seems like an issue in js-libp2p core

wemeetagain commented 1 month ago

@achingbrain I'm clearing out older issues now, I wonder if this is still an issue?