libp2p / js-libp2p

The JavaScript Implementation of libp2p networking stack.
https://libp2p.github.io/js-libp2p/
Other
2.33k stars 445 forks source link

no more gossipsub messaging after node.stop()/start() #677

Closed olivier-nerot closed 4 years ago

olivier-nerot commented 4 years ago

As said here, I don’t receive anymore messages after a node.stop() / node.start(), as shown in the following code. It works at first launch, but if I stop/start one node (by functions calls or by stoping/starting the code), messages are not sent anymore between nodes (the log in sendmessage() shows that one node is well connected to 2 peers though… but the other node is only connected to itself). And if I disable/enable wifi on node1, node2 is not informed, and only node2 can send messages to node1. node1 messages are lost. Nevertheless, the discover/connect events are well sent between peers. It looks like gossipsub peers list is not updated on disconnected node.

libp2p : 0.28.0 libp2p-gossipsub : 0.4.5

async function init() {
      const peerid = ...;

      node = await Libp2p.create({
        peerId: peerid,
        addresses: {
          listen: ['/ip4/0.0.0.0/tcp/0'],
        },
        modules: {
          transport: [TCP],
          streamMuxer: [Mplex],
          connEncryption: [SECIO],
          peerDiscovery: [MulticastDNS],
          pubsub: Gossipsub,
        },
        config: {
          peerDiscovery: {
            autoDial: true,
            mdns: {
              interval: 5000,
              enabled: true,
            },
          },
          pubsub: {
            enabled: true,
            emitSelf: false,
          },
        },
      });

     node.on('error', (err) => {
        debug('error', err);
      });

     node.on('peer:discovery', (peerId) => {
        debug('Discovered: %O', peerId);
      });

     node.connectionManager.on('peer:connect', (connection) => {
        debug('peer connected %O', connection);
      });

     node.connectionManager.on('peer:disconnect', (connection) => {
        debug('peer disconnected %O', connection);
      });

      start();
    }

    async function start() {
      await node.start();

      // required ? seems to not work when this line is removed.
     node.peerStore.addressBook.add(node.peerId, node.multiaddrs);

     node.pubsub.subscribe('messages', (msg) => {
        debug(`pubsub messages received: ${msg.data.toString()}`);
      });      
    }

    async function stop() {
      await node.stop();
    }

    sendmessage(msg) {
      const s = node.pubsub.getSubscribers('messages');
      debug('pubsub subscribers %O', s);
      node.pubsub.publish('messages', Buffer.from(msg));
    },
jacobheun commented 4 years ago

This is a duplicate of #658

jacobheun commented 4 years ago

This should be fixed with the latest patch to libp2p-interfaces, https://github.com/libp2p/js-libp2p/issues/658#issuecomment-653563368.