[−][src]Crate libp2p
Libp2p is a peer-to-peer framework.
Major libp2p concepts
Here is a list of all the major concepts of libp2p.
Multiaddr
A Multiaddr
is a way to reach a node. Examples:
/ip4/80.123.90.4/tcp/5432
/ip6/[::1]/udp/10560/quic
/unix//path/to/socket
Transport
Transport
is a trait that represents an object capable of dialing multiaddresses or
listening on multiaddresses. The Transport
produces an output which varies depending on the
object that implements the trait.
Each implementation of Transport
typically supports only some multiaddresses. For example
the TcpConfig
type (which implements Transport
) only supports multiaddresses of the format
/ip4/.../tcp/...
.
Example:
use libp2p::{Multiaddr, Transport, tcp::TcpConfig}; let tcp = TcpConfig::new(); let addr: Multiaddr = "/ip4/98.97.96.95/tcp/20500".parse().expect("invalid multiaddr"); let _outgoing_connec = tcp.dial(addr); // Note that `_outgoing_connec` is a `Future`, and therefore doesn't do anything by itself // unless it is run through a tokio runtime.
The easiest way to create a transport is to use the build_development_transport
function.
This function provides support for the most common protocols.
Example:
let key = libp2p::secio::SecioKeyPair::ed25519_generated().unwrap(); let _transport = libp2p::build_development_transport(key); // _transport.dial(...);
See the documentation of the libp2p-core
crate for more details about transports.
Connection upgrades
Once a connection has been opened with a remote through a Transport
, it can be upgraded.
This consists in negotiating a protocol with the remote (through a negotiation protocol
multistream-select
), and applying that protocol on the socket.
Example:
use libp2p::{Transport, tcp::TcpConfig, secio::{SecioConfig, SecioKeyPair}}; let tcp = TcpConfig::new(); let secio_upgrade = SecioConfig::new(SecioKeyPair::ed25519_generated().unwrap()); let with_security = tcp.with_upgrade(secio_upgrade); // let _ = with_security.dial(...); // `with_security` also implements the `Transport` trait, and all the connections opened // through it will automatically negotiate the `secio` protocol.
See the documentation of the libp2p-core
crate for more details about upgrades.
Topology
The Topology
trait is implemented for types that hold the layout of a network. When other
components need the network layout to operate, they are passed an instance of a Topology
.
The most basic implementation of Topology
is the MemoryTopology
, which is essentially a
HashMap
. Creating your own Topology
makes it possible to add for example a reputation
system.
Network behaviour
The NetworkBehaviour
trait is implemented on types that provide some capability to the
network. Examples of network behaviours include: periodically ping the nodes we are connected
to, periodically ask for information from the nodes we are connected to, connect to a DHT and
make queries to it, propagate messages to the nodes we are connected to (pubsub), and so on.
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.
It combines a Transport
, a NetworkBehaviour
and a Topology
together.
See the documentation of the libp2p-core
crate for more details about creating a swarm.
Using libp2p
This section contains details about how to use libp2p in practice.
The most simple way to use libp2p consists in the following steps:
- Create a base implementation of
Transport
that combines all the protocols you want and the upgrades you want, such as the security layer and multiplexing. - Create a struct that implements the
NetworkBehaviour
trait and that combines all the network behaviours that you want. - Create and implement the
Topology
trait that to store the topology of the network. - Create a swarm that combines your base transport, the network behaviour, and the topology.
- This swarm can now be polled with the
tokio
library in order to start the network.
Re-exports
pub extern crate bytes; |
pub extern crate futures; |
pub extern crate multiaddr; |
pub extern crate multihash; |
pub extern crate tokio_io; |
pub extern crate tokio_codec; |
pub extern crate libp2p_core as core; |
pub extern crate libp2p_dns as dns; |
pub extern crate libp2p_identify as identify; |
pub extern crate libp2p_kad as kad; |
pub extern crate libp2p_floodsub as floodsub; |
pub extern crate libp2p_mplex as mplex; |
pub extern crate libp2p_mdns as mdns; |
pub extern crate libp2p_ping as ping; |
pub extern crate libp2p_plaintext as plaintext; |
pub extern crate libp2p_ratelimit as ratelimit; |
pub extern crate libp2p_secio as secio; |
pub extern crate libp2p_tcp as tcp; |
pub extern crate libp2p_uds as uds; |
pub extern crate libp2p_websocket as websocket; |
pub extern crate libp2p_yamux as yamux; |
pub use libp2p_core_derive::NetworkBehaviour; |
pub use self::simple::SimpleProtocol; |
Modules
simple |
Macros
multiaddr | The |
Structs
Multiaddr | Representation of a Multiaddr. |
PeerId | Identifier of a peer of the network. |
Swarm | Contains the state of the network, plus the way it should behave. |
Traits
InboundUpgrade | Possible upgrade on an inbound connection or substream. |
InboundUpgradeExt | Extension trait for |
OutboundUpgrade | Possible upgrade on an outbound connection or substream. |
OutboundUpgradeExt | Extention trait for |
Transport | A transport is an object that can be used to produce connections by listening or dialing a peer. |
TransportExt | Trait automatically implemented on all objects that implement |
Functions
build_development_transport | Builds a |
build_tcp_ws_secio_mplex_yamux | Builds an implementation of |