multiformats / js-multiaddr

JavaScript implementation of multiaddr
http://multiformats.github.io/js-multiaddr
Other
109 stars 63 forks source link

Having issues connecting to Substrate Network #93

Closed zthomas closed 1 year ago

zthomas commented 5 years ago

I'm trying to connect to the Polkadot Substrate network and it looks like the multiaddr library is converting all p2p protocol into ipfs

So while I'm able to ping bootnodes for IPFS network, when I try to ping bootnodes for Substrate, I get errors like Error: dialed to the wrong peer, Ids do not match or Error: No available transports to dial peer

I feel like one of the reason is that when I run the command to ping an address like /ip4/40.117.153.33/tcp/30363/p2p/QmPiGU1jwL9UDw2FMyMQFr9FdpF9hURKxkfy6PWw6aLsur (one of the substrate bootnodes) it automatically converts it to an IPFS address /ip4/40.117.153.33/tcp/30363/ipfs/QmPiGU1jwL9UDw2FMyMQFr9FdpF9hURKxkfy6PWw6aLsur

This seems to be coded in the spec: https://github.com/multiformats/js-multiaddr/blob/master/test/index.spec.js#L223

This is an extension of the discussions from: https://discuss.libp2p.io/t/libp2p-on-the-polkadot-network/211/3

jacobheun commented 5 years ago

The conversion of /p2p to /ipfs only applies to the string representation, the codecs are equivalent. Over the wire representation won't matter, as any other nodes on the network should know the codec.

We will be migrating over to /p2p by default soon, https://github.com/libp2p/libp2p/issues/79, so the string representation will always be /p2p as we migrate away from using /ipfs.

In regards to the errors you're seeing, those lead me to believe that the node being dialed isn't correct. Pierre may be able to help more in terms of joining the substrate network as I am not familiar with doing that currently.

Error: dialed to the wrong peer, Ids do not match

This indicates that you were able to connect to the node, but SECIO failed because the node you actually connected to is not QmPiGU1jwL9UDw2FMyMQFr9FdpF9hURKxkfy6PWw6aLsur.

Error: No available transports to dial peer

This means no transports are dialing, so TCP is likely failing since that's a TCP address. I turned on debugging for my local node to check the errors and TCP is getting an ECONNREFUSED error. So the boot nodes may not be available.

folex commented 4 years ago

Error: dialed to the wrong peer, Ids do not match

I faced similar issue when connecting Rust & JS via libp2p. The problem is that peer-id JS computes peer id from public key like this:

const computeDigest = (pubKey, cb) => {
  if (pubKey.bytes.length <= 42) {
    const digest = mh.encode(pubKey.bytes, 'identity') // This get's executed
    cb(null, digest)
  } else {
    pubKey.hash((err, digest) => {
      cb(err, digest)
    })
  }
}

Result is that in JS mh.encode(pubKey.bytes, 'identity') results in addresses of format 12D3KooWSwNXzEeGjgwEocRJBzbdoDqxbz3LdrwgSuKmKeGvbM4G, while in Rust PeerId for the same public key looks like QmTESkr2vWDCKqiHVsyvf4iRQCBgvNDqBJ6P3yTTDb6haw.

Error then happens in libp2p-secio/src/handshake/crypto.js in identify function in JS:

if (state.id.remote.toB58String() !== remoteId.toB58String()) { // <-- remoteId here is computed from public key in 12D3 format, while state.id.remote is in Qm format
    return callback(new Error('dialed to the wrong peer, Ids do not match'))
}

Currently, I don't have a solution for that except to patch either JS or Rust.

achingbrain commented 1 year ago

This issue is very stale - the modules referenced have been updated several times, some such as secio aren't used any more.

Please open a new issue if this is still a problem.