Skip to content

announce addresses does not save on peerStore #1761

@bakhshandeh

Description

@bakhshandeh

In this example, the announce address (4.2.2.4) is not stored in the peerStore.
It was working fine on previous versions.

import { tcp } from "@libp2p/tcp";
import { mplex } from "@libp2p/mplex";
import { yamux } from "@chainsafe/libp2p-yamux";
import { noise } from "@chainsafe/libp2p-noise";
import defaultsDeep from "@nodeutils/defaults-deep";
import { createLibp2p as createNode } from "libp2p";
import { pipe } from 'it-pipe';
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'

async function createLibp2p(_options) {
  const defaults = {
    transports: [tcp()],
    streamMuxers: [
      yamux(), //,mplex()
    ],
    connectionEncryption: [noise()],
  };

  return createNode(defaultsDeep(_options, defaults));
}

async function run() {
  // const listenerId = await createFromJSON(idl)

  // Listener libp2p node
  const listenerNode = await createLibp2p({
    addresses: {
      listen: ["/ip4/0.0.0.0/tcp/10333"],
    },
  });

  await listenerNode.handle("/echo/1.0.0", ({ stream }) =>
    pipe(stream.source, stream.sink)
  );

  // Log a message when we receive a connection
  listenerNode.addEventListener("peer:connect", (evt) => {
    const remotePeer = evt.detail;
    console.log("received dial to me from:", remotePeer.toString());
  });

  console.log("Listener ready, listening on:");
  listenerNode.getMultiaddrs().forEach((ma) => {
    console.log(ma.toString());
  });

  // Dialer
  const dialerNode = await createLibp2p({
    addresses: {
      listen: ["/ip4/0.0.0.0/tcp/0"],
      announce: ['/ip4/4.2.2.4/tcp/1234']
    },
    // peerId: dialerId
  });

  // Dial the listener node
  console.log('Dialing to listener', listenerNode.getMultiaddrs()[0])
  const stream = await dialerNode.dialProtocol(listenerNode.getMultiaddrs()[0], "/echo/1.0.0");

  pipe(
    // Source data
    [uint8ArrayFromString('hey')],
    // Write to the stream, and pass its output to the next function
    stream,
    // Sink function
    async function (source) {
      // For each chunk of data
      for await (const data of source) {
        // Output the data
        console.log('received echo:', uint8ArrayToString(data.subarray()))
      }
    }
  )

  console.log('Dialer peerInfo:', await listenerNode.peerStore.get(dialerNode.peerId));
}

run();

Metadata

Metadata

Assignees

No one assigned

    Labels

    need/triageNeeds initial labeling and prioritization

    Type

    No type

    Projects

    Status

    🎉Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions