1
0
mirror of https://github.com/fluencelabs/js-libp2p synced 2025-03-16 23:50:51 +00:00
js-libp2p/doc/PEER_DISCOVERY.md
Alex Potsides 199395de4d
feat: convert to typescript ()
Converts this module to typescript.

- Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp`
- Ecosystem module now have named exports
- Configuration has been updated, now pass instances of modules instead of classes:
- Some configuration keys have been renamed to make them more descriptive.  `transport` -> `transports`, `connEncryption` -> `connectionEncryption`.  In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc.  Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc.
- Properties of the `modules` config key have been moved to the root
- Properties of the `config` config key have been moved to the root
```js
// before
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'

await Libp2p.create({
  modules: {
    transport: [
      TCP
    ],
  }
  config: {
    transport: {
      [TCP.tag]: {
        foo: 'bar'
      }
    },
    relay: {
      enabled: true,
      hop: {
        enabled: true,
        active: true
      }
    }
  }
})
```
```js
// after
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'

await createLibp2p({
  transports: [
    new TCP({ foo: 'bar' })
  ],
  relay: {
    enabled: true,
    hop: {
      enabled: true,
      active: true
    }
  }
})
```
- Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config.  Now if you don't want a feature, just don't pass an implementation.   Eg:
```js
// before
await Libp2p.create({
  modules: {
    transport: [
      TCP
    ],
    pubsub: Gossipsub
  },
  config: {
    pubsub: {
      enabled: false
    }
  }
})
```
```js
// after
await createLibp2p({
  transports: [
    new TCP()
  ]
})
```
- `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc
- `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise).  If you need low-level unadulterated addresses, call methods on the address manager.

BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
2022-03-28 14:30:27 +01:00

3.7 KiB

Peer Discovery and Auto Dial

Synopsis:

  • All peers discovered are emitted via peer:discovery so applications can take any desired action.
  • Libp2p defaults to automatically connecting to new peers, when under the ConnectionManager low watermark (minimum peers).
    • Applications can disable this via the connectionManager.autoDial config property, and handle connections themselves.
    • Applications who have not disabled this should never connect on peer discovery. Applications should use the peer:connect event if they wish to take a specific action on new peers.

Scenarios

In any scenario, if a peer is discovered it should be added to the PeerBook. This ensures that even if we don't dial to a node when we discover it, we know about it in the event that it becomes known as a provider for something we need. The scenarios listed below detail what actions the auto dialer will take when peers are discovered.

1. Joining the network

The node is new and needs to join the network. It currently has 0 peers. Discovery Mechanisms: Ambient Discovery

Action to take

Connect to discovered peers. This should have some degree of concurrency limiting. While the case should be low, if we immediately discover more peers than our high watermark we should avoid dialing them all.

2. Connected to some

The node is connected to other nodes. The current number of connections is less than the desired low watermark. Discovery Mechanisms: Ambient Discovery and Active Discovery

Action to take

Connect to discovered peers. This should have some degree of concurrency limiting. The concurrency may need to be modified to reflect the current number of peers connected. The more peers we have, the lower the concurrency may need to be.

3. Connected to enough

Discovery Mechanisms: Ambient Discovery and Active Discovery

Action to take

None. If we are connected to enough peers, the low watermark, we should not connect to discovered peers. As other peers discover us, they may connect to us based on their current scenario.

For example, a long running node with adequate peers is on an MDNS network. A new peer joins the network and both become aware of each other. The new peer should be the peer that dials, as it has too few peers. The existing node has no reason to dial the new peer, but should keep a record of it in case it later becomes an important node due to its contents/capabilities.

Avoiding dials above the low watermark also allows for a pool of connections to be reserved for application specific actions, such as connecting to a specific content provider via a DHT query to find that content (ipfs-bitswap).

4. Connected to too many

The node has more connections than it wants. The current number of connections is greater than the high watermark.

WIP Connection Manager v2 spec Discovery Mechanisms: Ambient Discovery and Active Discovery

Action to take

None, the ConnectionManager will automatically prune connections.

Discovery Mechanisms

Means of which a libp2p node discovers other peers.

Active Discovery

Through active use of the libp2p network, a node may discovery peers.

  • Content/Peer routing (DHT, delegated, etc) provider and peer queries
  • DHT random walk
  • Rendezvous servers

Ambient Discovery

Leveraging known addresses, or network discovery mechanisms, a node may discover peers outside of the bounds of the libp2p network.

  • Bootstrap
  • MDNS
  • proximity based (bluetooth, sound, etc)