Show the crate…

1 unstable release

0.27.0 Mar 15, 2021

#13 in #local-node

Download history 5/week @ 2023-12-11 8/week @ 2023-12-18 6/week @ 2023-12-25 4/week @ 2024-01-08 15/week @ 2024-02-12 16/week @ 2024-02-19 31/week @ 2024-02-26 27/week @ 2024-03-04 20/week @ 2024-03-11 26/week @ 2024-03-18

107 downloads per month
Used in 7 crates

MIT license

650KB
11K SLoC

High level manager of the network.

A Swarm contains the state of the network as a whole. The entire behaviour of a libp2p network can be controlled through the Swarm. The Swarm struct contains all active and pending connections to remotes and manages the state of all the substreams that have been opened, and all the upgrades that were built upon these substreams.

Initializing a Swarm

Creating a Swarm requires three things:

  1. A network identity of the local node in form of a PeerId.
  2. An implementation of the Transport trait. This is the type that will be used in order to reach nodes on the network based on their address. See the transport module for more information.
  3. An implementation of the NetworkBehaviour trait. This is a state machine that defines how the swarm should behave once it is connected to a node.

Network Behaviour

The NetworkBehaviour trait is implemented on types that indicate to the swarm how it should behave. This includes which protocols are supported and which nodes to try to connect to. It is the NetworkBehaviour that controls what happens on the network. Multiple types that implement NetworkBehaviour can be composed into a single behaviour.

Protocols Handler

The ProtocolsHandler trait defines how each active connection to a remote should behave: how to handle incoming substreams, which protocols are supported, when to open a new outbound substream, etc.

Dependencies

~10–15MB
~316K SLoC