2019-04-04 12:25:42 -03:00
// Copyright 2019 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2022-04-08 01:06:06 +02:00
mod either ;
2022-12-14 11:50:08 +11:00
mod external_addresses ;
mod listen_addresses ;
2021-12-09 03:00:47 -08:00
pub mod toggle ;
2022-12-14 11:50:08 +11:00
pub use external_addresses ::ExternalAddresses ;
pub use listen_addresses ::ListenAddresses ;
2021-11-15 14:17:23 +01:00
use crate ::dial_opts ::DialOpts ;
2022-02-21 13:32:24 +01:00
use crate ::handler ::{ ConnectionHandler , IntoConnectionHandler } ;
2021-08-31 17:00:51 +02:00
use crate ::{ AddressRecord , AddressScore , DialError } ;
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
use libp2p_core ::{
2022-07-04 04:16:57 +02:00
connection ::ConnectionId , transport ::ListenerId , ConnectedPoint , Multiaddr , PeerId ,
2021-08-11 13:12:12 +02:00
} ;
2021-10-14 18:05:07 +02:00
use std ::{ task ::Context , task ::Poll } ;
2019-04-04 12:25:42 -03:00
2022-02-21 13:32:24 +01:00
/// Custom event that can be received by the [`ConnectionHandler`].
2022-02-16 13:15:52 +01:00
pub ( crate ) type THandlerInEvent < THandler > =
2022-02-21 13:32:24 +01:00
< < THandler as IntoConnectionHandler > ::Handler as ConnectionHandler > ::InEvent ;
2021-08-31 17:00:51 +02:00
2022-02-16 13:15:52 +01:00
pub ( crate ) type THandlerOutEvent < THandler > =
2022-02-21 13:32:24 +01:00
< < THandler as IntoConnectionHandler > ::Handler as ConnectionHandler > ::OutEvent ;
2022-02-16 13:15:52 +01:00
2021-12-08 13:44:37 +01:00
/// A [`NetworkBehaviour`] defines the behaviour of the local node on the network.
2019-04-04 12:25:42 -03:00
///
2021-12-08 13:44:37 +01:00
/// In contrast to [`Transport`](libp2p_core::Transport) which defines **how** to send bytes on the
/// network, [`NetworkBehaviour`] defines **what** bytes to send and **to whom**.
2020-01-14 13:48:16 +02:00
///
2021-12-08 13:44:37 +01:00
/// Each protocol (e.g. `libp2p-ping`, `libp2p-identify` or `libp2p-kad`) implements
/// [`NetworkBehaviour`]. Multiple implementations of [`NetworkBehaviour`] can be composed into a
/// hierarchy of [`NetworkBehaviour`]s where parent implementations delegate to child
/// implementations. Finally the root of the [`NetworkBehaviour`] hierarchy is passed to
/// [`Swarm`](crate::Swarm) where it can then control the behaviour of the local node on a libp2p
/// network.
2020-01-14 13:48:16 +02:00
///
2021-12-08 13:44:37 +01:00
/// # Hierarchy of [`NetworkBehaviour`]
///
/// To compose multiple [`NetworkBehaviour`] implementations into a single [`NetworkBehaviour`]
/// implementation, potentially building a multi-level hierarchy of [`NetworkBehaviour`]s, one can
/// use one of the [`NetworkBehaviour`] combinators, and/or use the [`NetworkBehaviour`] derive
/// macro.
///
/// ## Combinators
///
/// [`NetworkBehaviour`] combinators wrap one or more [`NetworkBehaviour`] implementations and
2021-12-09 03:00:47 -08:00
/// implement [`NetworkBehaviour`] themselves. Example is the
/// [`Toggle`](crate::behaviour::toggle::Toggle) [`NetworkBehaviour`].
2021-10-30 11:20:31 +02:00
///
2021-12-08 13:44:37 +01:00
/// ``` rust
2022-10-06 03:50:11 +11:00
/// # use libp2p_swarm::dummy;
2021-12-09 03:00:47 -08:00
/// # use libp2p_swarm::behaviour::toggle::Toggle;
2022-10-06 03:50:11 +11:00
/// let my_behaviour = dummy::Behaviour;
2021-12-08 13:44:37 +01:00
/// let my_toggled_behaviour = Toggle::from(Some(my_behaviour));
/// ```
///
2022-03-17 16:24:43 +01:00
/// ## Custom [`NetworkBehaviour`] with the Derive Macro
2021-12-08 13:44:37 +01:00
///
/// One can derive [`NetworkBehaviour`] for a custom `struct` via the `#[derive(NetworkBehaviour)]`
/// proc macro re-exported by the `libp2p` crate. The macro generates a delegating `trait`
/// implementation for the custom `struct`. Each [`NetworkBehaviour`] trait method is simply
/// delegated to each `struct` member in the order the `struct` is defined. For example for
/// [`NetworkBehaviour::poll`] it will first poll the first `struct` member until it returns
/// [`Poll::Pending`] before moving on to later members. For [`NetworkBehaviour::addresses_of_peer`]
/// it will delegate to each `struct` member and return a concatenated array of all addresses
/// returned by the struct members.
2020-01-14 13:48:16 +02:00
///
2022-08-08 07:18:32 +02:00
/// Events ([`NetworkBehaviour::OutEvent`]) returned by each `struct` member are wrapped in a new
/// `enum` event, with an `enum` variant for each `struct` member. Users can define this event
/// `enum` themselves and provide the name to the derive macro via `#[behaviour(out_event =
/// "MyCustomOutEvent")]`. If the user does not specify an `out_event`, the derive macro generates
/// the event definition itself, naming it `<STRUCT_NAME>Event`.
2022-03-17 16:24:43 +01:00
///
2022-08-16 06:58:17 +02:00
/// The aforementioned conversion of each of the event types generated by the struct members to the
/// custom `out_event` is handled by [`From`] implementations which the user needs to define in
/// addition to the event `enum` itself.
2020-07-08 19:32:47 +10:00
///
2021-12-08 13:44:37 +01:00
/// ``` rust
2022-11-13 10:59:14 +11:00
/// # use libp2p_identify as identify;
/// # use libp2p_ping as ping;
/// # use libp2p_swarm_derive::NetworkBehaviour;
2021-12-08 13:44:37 +01:00
/// #[derive(NetworkBehaviour)]
/// #[behaviour(out_event = "Event")]
2022-11-13 10:59:14 +11:00
/// # #[behaviour(prelude = "libp2p_swarm::derive_prelude")]
2021-12-08 13:44:37 +01:00
/// struct MyBehaviour {
2022-10-04 01:17:31 +01:00
/// identify: identify::Behaviour,
2022-10-01 00:19:34 +10:00
/// ping: ping::Behaviour,
2021-12-08 13:44:37 +01:00
/// }
///
/// enum Event {
2022-10-04 01:17:31 +01:00
/// Identify(identify::Event),
2022-10-01 00:19:34 +10:00
/// Ping(ping::Event),
2021-12-08 13:44:37 +01:00
/// }
///
2022-10-04 01:17:31 +01:00
/// impl From<identify::Event> for Event {
/// fn from(event: identify::Event) -> Self {
2021-12-08 13:44:37 +01:00
/// Self::Identify(event)
/// }
/// }
///
2022-10-01 00:19:34 +10:00
/// impl From<ping::Event> for Event {
/// fn from(event: ping::Event) -> Self {
2021-12-08 13:44:37 +01:00
/// Self::Ping(event)
/// }
/// }
/// ```
2022-02-28 10:27:58 +01:00
pub trait NetworkBehaviour : 'static {
2019-06-03 18:16:02 +02:00
/// Handler for all the protocols the network behaviour supports.
2022-02-21 13:32:24 +01:00
type ConnectionHandler : IntoConnectionHandler ;
2019-06-03 18:16:02 +02:00
/// Event generated by the `NetworkBehaviour` and that the swarm will report back.
2020-02-07 16:29:30 +01:00
type OutEvent : Send + 'static ;
2019-04-04 12:25:42 -03:00
2022-02-21 13:32:24 +01:00
/// Creates a new [`ConnectionHandler`] for a connection with a peer.
2019-06-03 18:16:02 +02:00
///
2021-08-31 17:00:51 +02:00
/// Every time an incoming connection is opened, and every time another [`NetworkBehaviour`]
/// emitted a dial request, this method is called.
2019-06-03 18:16:02 +02:00
///
/// The returned object is a handler for that specific connection, and will be moved to a
/// background task dedicated to that connection.
///
2021-08-31 17:00:51 +02:00
/// The network behaviour (ie. the implementation of this trait) and the handlers it has spawned
/// (ie. the objects returned by `new_handler`) can communicate by passing messages. Messages
/// sent from the handler to the behaviour are injected with [`NetworkBehaviour::inject_event`],
/// and the behaviour can send a message to the handler by making [`NetworkBehaviour::poll`]
/// return [`NetworkBehaviourAction::NotifyHandler`].
///
/// Note that the handler is returned to the [`NetworkBehaviour`] on connection failure and
/// connection closing.
2022-02-21 13:32:24 +01:00
fn new_handler ( & mut self ) -> Self ::ConnectionHandler ;
2019-04-04 12:25:42 -03:00
/// Addresses that this behaviour is aware of for this specific peer, and that may allow
/// reaching the peer.
2019-06-03 18:16:02 +02:00
///
/// The addresses will be tried in the order returned by this function, which means that they
/// should be ordered by decreasing likelihood of reachability. In other words, the first
/// address should be the most likely to be reachable.
2021-07-31 03:48:32 +10:00
fn addresses_of_peer ( & mut self , _ : & PeerId ) -> Vec < Multiaddr > {
vec! [ ]
}
2019-04-04 12:25:42 -03:00
2022-11-17 09:28:40 +00:00
/// Informs the behaviour about an event from the [`Swarm`](crate::Swarm).
fn on_swarm_event ( & mut self , _event : FromSwarm < Self ::ConnectionHandler > ) { }
/// Informs the behaviour about an event generated by the [`ConnectionHandler`] dedicated to the
/// peer identified by `peer_id`. for the behaviour.
///
/// The [`PeerId`] is guaranteed to be in a connected state. In other words,
/// [`FromSwarm::ConnectionEstablished`] has previously been received with this [`PeerId`].
fn on_connection_handler_event (
& mut self ,
_peer_id : PeerId ,
_connection_id : ConnectionId ,
_event : < < Self ::ConnectionHandler as IntoConnectionHandler > ::Handler as
ConnectionHandler > ::OutEvent ,
) {
}
2020-03-31 15:41:13 +02:00
/// Informs the behaviour about a newly established connection to a peer.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::ConnectionEstablished` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
2021-10-14 18:05:07 +02:00
fn inject_connection_established (
& mut self ,
2022-11-17 09:28:40 +00:00
peer_id : & PeerId ,
connection_id : & ConnectionId ,
endpoint : & ConnectedPoint ,
failed_addresses : Option < & Vec < Multiaddr > > ,
other_established : usize ,
2021-10-14 18:05:07 +02:00
) {
2022-11-17 09:28:40 +00:00
self . on_swarm_event ( FromSwarm ::ConnectionEstablished ( ConnectionEstablished {
peer_id : * peer_id ,
connection_id : * connection_id ,
endpoint ,
failed_addresses : failed_addresses
. map ( | v | v . as_slice ( ) )
. unwrap_or_else ( | | & [ ] ) ,
other_established ,
} ) ) ;
2021-10-14 18:05:07 +02:00
}
2020-03-31 15:41:13 +02:00
/// Informs the behaviour about a closed connection to a peer.
///
/// A call to this method is always paired with an earlier call to
2022-08-10 12:50:31 +04:30
/// [`NetworkBehaviour::inject_connection_established`] with the same peer ID, connection ID and endpoint.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::ConnectionClosed` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
2021-08-31 17:00:51 +02:00
fn inject_connection_closed (
& mut self ,
2022-11-17 09:28:40 +00:00
peer_id : & PeerId ,
connection_id : & ConnectionId ,
endpoint : & ConnectedPoint ,
handler : < Self ::ConnectionHandler as IntoConnectionHandler > ::Handler ,
remaining_established : usize ,
2021-08-31 17:00:51 +02:00
) {
2022-11-17 09:28:40 +00:00
self . on_swarm_event ( FromSwarm ::ConnectionClosed ( ConnectionClosed {
peer_id : * peer_id ,
connection_id : * connection_id ,
endpoint ,
handler ,
remaining_established ,
} ) ) ;
2021-08-31 17:00:51 +02:00
}
2019-04-04 12:25:42 -03:00
2020-06-30 17:10:53 +02:00
/// Informs the behaviour that the [`ConnectedPoint`] of an existing connection has changed.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::AddressChange` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
2020-06-30 17:10:53 +02:00
fn inject_address_change (
& mut self ,
2022-11-17 09:28:40 +00:00
peer_id : & PeerId ,
connection_id : & ConnectionId ,
old : & ConnectedPoint ,
new : & ConnectedPoint ,
2020-06-30 17:10:53 +02:00
) {
2022-11-17 09:28:40 +00:00
self . on_swarm_event ( FromSwarm ::AddressChange ( AddressChange {
peer_id : * peer_id ,
connection_id : * connection_id ,
old ,
new ,
} ) ) ;
2020-06-30 17:10:53 +02:00
}
2019-06-03 18:16:02 +02:00
/// Informs the behaviour about an event generated by the handler dedicated to the peer identified by `peer_id`.
/// for the behaviour.
2019-04-04 12:25:42 -03:00
///
2022-08-10 12:50:31 +04:30
/// The `peer_id` is guaranteed to be in a connected state. In other words,
/// [`NetworkBehaviour::inject_connection_established`] has previously been called with this `PeerId`.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Implement `NetworkBehaviour::on_connection_handler_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
fn inject_event (
2019-04-04 12:25:42 -03:00
& mut self ,
peer_id : PeerId ,
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
connection : ConnectionId ,
2022-02-21 13:32:24 +01:00
event : < < Self ::ConnectionHandler as IntoConnectionHandler > ::Handler as ConnectionHandler > ::OutEvent ,
2022-11-17 09:28:40 +00:00
) {
self . on_connection_handler_event ( peer_id , connection , event ) ;
}
2019-04-04 12:25:42 -03:00
2021-10-14 18:05:07 +02:00
/// Indicates to the behaviour that the dial to a known or unknown node failed.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `InEvent::DialFailure` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
2021-08-31 17:00:51 +02:00
fn inject_dial_failure (
& mut self ,
2022-11-17 09:28:40 +00:00
peer_id : Option < PeerId > ,
handler : Self ::ConnectionHandler ,
error : & DialError ,
2021-08-31 17:00:51 +02:00
) {
2022-11-17 09:28:40 +00:00
self . on_swarm_event ( FromSwarm ::DialFailure ( DialFailure {
peer_id ,
handler ,
error ,
} ) ) ;
2021-08-31 17:00:51 +02:00
}
/// Indicates to the behaviour that an error happened on an incoming connection during its
/// initial handshake.
///
/// This can include, for example, an error during the handshake of the encryption layer, or the
/// connection unexpectedly closed.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::ListenFailure` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
2021-08-31 17:00:51 +02:00
fn inject_listen_failure (
& mut self ,
2022-11-17 09:28:40 +00:00
local_addr : & Multiaddr ,
send_back_addr : & Multiaddr ,
handler : Self ::ConnectionHandler ,
2021-08-31 17:00:51 +02:00
) {
2022-11-17 09:28:40 +00:00
self . on_swarm_event ( FromSwarm ::ListenFailure ( ListenFailure {
local_addr ,
send_back_addr ,
handler ,
} ) ) ;
2021-08-31 17:00:51 +02:00
}
2019-04-04 12:25:42 -03:00
2021-03-24 17:21:53 +01:00
/// Indicates to the behaviour that a new listener was created.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::NewListener` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
fn inject_new_listener ( & mut self , id : ListenerId ) {
self . on_swarm_event ( FromSwarm ::NewListener ( NewListener { listener_id : id } ) ) ;
}
2019-04-16 15:36:08 +02:00
2021-03-24 17:21:53 +01:00
/// Indicates to the behaviour that we have started listening on a new multiaddr.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::NewListenAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
fn inject_new_listen_addr ( & mut self , id : ListenerId , addr : & Multiaddr ) {
self . on_swarm_event ( FromSwarm ::NewListenAddr ( NewListenAddr {
listener_id : id ,
addr ,
} ) ) ;
}
2019-04-16 15:36:08 +02:00
2021-03-24 17:21:53 +01:00
/// Indicates to the behaviour that a multiaddr we were listening on has expired,
2022-11-17 09:28:40 +00:00
/// which means that we are no longer listening on it.
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::ExpiredListenAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
fn inject_expired_listen_addr ( & mut self , id : ListenerId , addr : & Multiaddr ) {
self . on_swarm_event ( FromSwarm ::ExpiredListenAddr ( ExpiredListenAddr {
listener_id : id ,
addr ,
} ) ) ;
}
2019-04-16 17:00:20 +02:00
2019-08-13 15:41:12 +02:00
/// A listener experienced an error.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::ListenerError` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
fn inject_listener_error ( & mut self , id : ListenerId , err : & ( dyn std ::error ::Error + 'static ) ) {
self . on_swarm_event ( FromSwarm ::ListenerError ( ListenerError {
listener_id : id ,
err ,
} ) ) ;
2019-08-13 15:41:12 +02:00
}
/// A listener closed.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::ListenerClosed` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
fn inject_listener_closed ( & mut self , id : ListenerId , reason : Result < ( ) , & std ::io ::Error > ) {
self . on_swarm_event ( FromSwarm ::ListenerClosed ( ListenerClosed {
listener_id : id ,
reason ,
} ) ) ;
}
2019-08-13 15:41:12 +02:00
2021-03-24 17:21:53 +01:00
/// Indicates to the behaviour that we have discovered a new external address for us.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::NewExternalAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
fn inject_new_external_addr ( & mut self , addr : & Multiaddr ) {
self . on_swarm_event ( FromSwarm ::NewExternalAddr ( NewExternalAddr { addr } ) ) ;
}
2021-03-24 17:21:53 +01:00
/// Indicates to the behaviour that an external address was removed.
2022-11-17 09:28:40 +00:00
#[ deprecated(
since = " 0.40.2 " ,
note = " Handle `FromSwarm::ExpiredExternalAddr` in `NetworkBehaviour::on_swarm_event` instead. The default implementation of this `inject_*` method delegates to it. "
) ]
fn inject_expired_external_addr ( & mut self , addr : & Multiaddr ) {
self . on_swarm_event ( FromSwarm ::ExpiredExternalAddr ( ExpiredExternalAddr { addr } ) ) ;
}
2021-03-24 17:21:53 +01:00
2019-04-04 12:25:42 -03:00
/// Polls for things that swarm should do.
///
2019-06-03 18:16:02 +02:00
/// This API mimics the API of the `Stream` trait. The method may register the current task in
/// order to wake it up at a later point in time.
2021-08-31 17:00:51 +02:00
fn poll (
& mut self ,
cx : & mut Context < '_ > ,
params : & mut impl PollParameters ,
2022-02-21 13:32:24 +01:00
) -> Poll < NetworkBehaviourAction < Self ::OutEvent , Self ::ConnectionHandler > > ;
2019-04-04 12:25:42 -03:00
}
2019-06-18 10:23:26 +02:00
/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.
pub trait PollParameters {
2020-02-10 15:17:08 +01:00
/// Iterator returned by [`supported_protocols`](PollParameters::supported_protocols).
2019-06-18 10:23:26 +02:00
type SupportedProtocolsIter : ExactSizeIterator < Item = Vec < u8 > > ;
2020-02-10 15:17:08 +01:00
/// Iterator returned by [`listened_addresses`](PollParameters::listened_addresses).
2019-06-18 10:23:26 +02:00
type ListenedAddressesIter : ExactSizeIterator < Item = Multiaddr > ;
2020-02-10 15:17:08 +01:00
/// Iterator returned by [`external_addresses`](PollParameters::external_addresses).
2020-11-18 15:52:33 +01:00
type ExternalAddressesIter : ExactSizeIterator < Item = AddressRecord > ;
2019-06-18 10:23:26 +02:00
/// Returns the list of protocol the behaviour supports when a remote negotiates a protocol on
/// an inbound substream.
///
/// The iterator's elements are the ASCII names as reported on the wire.
///
/// Note that the list is computed once at initialization and never refreshed.
fn supported_protocols ( & self ) -> Self ::SupportedProtocolsIter ;
/// Returns the list of the addresses we're listening on.
2022-12-14 11:50:08 +11:00
#[ deprecated(
since = " 0.42.0 " ,
note = " Use `libp2p_swarm::ListenAddresses` instead. "
) ]
2019-06-18 10:23:26 +02:00
fn listened_addresses ( & self ) -> Self ::ListenedAddressesIter ;
/// Returns the list of the addresses nodes can use to reach us.
2022-12-14 11:50:08 +11:00
#[ deprecated(
since = " 0.42.0 " ,
note = " Use `libp2p_swarm::ExternalAddresses` instead. "
) ]
2019-06-18 10:23:26 +02:00
fn external_addresses ( & self ) -> Self ::ExternalAddressesIter ;
/// Returns the peer id of the local node.
2022-12-14 11:50:08 +11:00
#[ deprecated(
since = " 0.42.0 " ,
note = " Pass the node's `PeerId` into the behaviour instead. "
) ]
2019-06-18 10:23:26 +02:00
fn local_peer_id ( & self ) -> & PeerId ;
}
2019-04-16 15:57:29 +02:00
/// An action that a [`NetworkBehaviour`] can trigger in the [`Swarm`]
/// in whose context it is executing.
2020-02-10 15:17:08 +01:00
///
/// [`Swarm`]: super::Swarm
2021-08-31 17:00:51 +02:00
//
// Note: `TInEvent` is needed to be able to implement
// [`NetworkBehaviourAction::map_in`], mapping the handler `InEvent` leaving the
// handler itself untouched.
2020-12-17 11:01:45 +01:00
#[ derive(Debug) ]
2021-08-31 17:00:51 +02:00
pub enum NetworkBehaviourAction <
TOutEvent ,
2022-02-21 13:32:24 +01:00
THandler : IntoConnectionHandler ,
2021-08-31 17:00:51 +02:00
TInEvent = THandlerInEvent < THandler > ,
> {
2019-04-16 15:57:29 +02:00
/// Instructs the `Swarm` to return an event when it is being polled.
2019-04-04 12:25:42 -03:00
GenerateEvent ( TOutEvent ) ,
2021-11-15 14:17:23 +01:00
/// Instructs the swarm to start a dial.
2019-06-03 18:16:02 +02:00
///
2021-08-31 17:00:51 +02:00
/// On success, [`NetworkBehaviour::inject_connection_established`] is invoked.
2019-04-16 15:57:29 +02:00
/// On failure, [`NetworkBehaviour::inject_dial_failure`] is invoked.
2021-08-31 17:00:51 +02:00
///
/// Note that the provided handler is returned to the [`NetworkBehaviour`] on connection failure
/// and connection closing. Thus it can be used to carry state, which otherwise would have to be
/// tracked in the [`NetworkBehaviour`] itself. E.g. a message destined to an unconnected peer
/// can be included in the handler, and thus directly send on connection success or extracted by
/// the [`NetworkBehaviour`] on connection failure.
///
2021-11-15 14:17:23 +01:00
/// # Example carrying state in the handler
2021-08-31 17:00:51 +02:00
///
/// ```rust
/// # use futures::executor::block_on;
/// # use futures::stream::StreamExt;
2022-11-13 10:59:14 +11:00
/// # use libp2p_core::connection::ConnectionId;
/// # use libp2p_core::identity;
/// # use libp2p_core::transport::{MemoryTransport, Transport};
/// # use libp2p_core::upgrade::{self, DeniedUpgrade, InboundUpgrade, OutboundUpgrade};
/// # use libp2p_core::PeerId;
/// # use libp2p_plaintext::PlainText2Config;
/// # use libp2p_swarm::{
2022-02-21 13:32:24 +01:00
/// # DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream,
/// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler,
/// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent,
2021-08-31 17:00:51 +02:00
/// # };
2022-11-13 10:59:14 +11:00
/// # use libp2p_swarm::dial_opts::{DialOpts, PeerCondition};
/// # use libp2p_yamux as yamux;
2021-08-31 17:00:51 +02:00
/// # use std::collections::VecDeque;
/// # use std::task::{Context, Poll};
/// # use void::Void;
/// #
/// # let local_key = identity::Keypair::generate_ed25519();
/// # let local_public_key = local_key.public();
/// # let local_peer_id = PeerId::from(local_public_key.clone());
/// #
/// # let transport = MemoryTransport::default()
/// # .upgrade(upgrade::Version::V1)
/// # .authenticate(PlainText2Config { local_public_key })
/// # .multiplex(yamux::YamuxConfig::default())
/// # .boxed();
/// #
2022-12-09 15:44:08 +01:00
/// # let mut swarm = Swarm::with_threadpool_executor(transport, MyBehaviour::default(), local_peer_id);
2021-08-31 17:00:51 +02:00
/// #
/// // Super precious message that we should better not lose.
/// let message = PreciousMessage("My precious message".to_string());
///
/// // Unfortunately this peer is offline, thus sending our message to it will fail.
/// let offline_peer = PeerId::random();
///
/// // Let's send it anyways. We should get it back in case connecting to the peer fails.
/// swarm.behaviour_mut().send(offline_peer, message);
///
/// block_on(async {
/// // As expected, sending failed. But great news, we got our message back.
/// matches!(
/// swarm.next().await.expect("Infinite stream"),
/// SwarmEvent::Behaviour(PreciousMessage(_))
/// );
/// });
///
2021-11-15 14:17:23 +01:00
/// #[derive(Default)]
/// struct MyBehaviour {
/// outbox_to_swarm: VecDeque<NetworkBehaviourAction<PreciousMessage, MyHandler>>,
/// }
///
/// impl MyBehaviour {
/// fn send(&mut self, peer_id: PeerId, msg: PreciousMessage) {
/// self.outbox_to_swarm
/// .push_back(NetworkBehaviourAction::Dial {
/// opts: DialOpts::peer_id(peer_id)
/// .condition(PeerCondition::Always)
/// .build(),
/// handler: MyHandler { message: Some(msg) },
/// });
/// }
/// }
2021-08-31 17:00:51 +02:00
/// #
/// impl NetworkBehaviour for MyBehaviour {
2022-02-21 13:32:24 +01:00
/// # type ConnectionHandler = MyHandler;
2021-08-31 17:00:51 +02:00
/// # type OutEvent = PreciousMessage;
/// #
2022-02-21 13:32:24 +01:00
/// # fn new_handler(&mut self) -> Self::ConnectionHandler {
2021-08-31 17:00:51 +02:00
/// # MyHandler { message: None }
/// # }
/// #
/// #
/// # fn inject_event(
/// # &mut self,
/// # _: PeerId,
/// # _: ConnectionId,
2022-02-21 13:32:24 +01:00
/// # _: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
2021-08-31 17:00:51 +02:00
/// # ) {
/// # unreachable!();
/// # }
/// #
/// fn inject_dial_failure(
/// &mut self,
2021-10-14 18:05:07 +02:00
/// _: Option<PeerId>,
2022-02-21 13:32:24 +01:00
/// handler: Self::ConnectionHandler,
2021-10-14 18:05:07 +02:00
/// _: &DialError,
2021-08-31 17:00:51 +02:00
/// ) {
/// // As expected, sending the message failed. But lucky us, we got the handler back, thus
/// // the precious message is not lost and we can return it back to the user.
/// let msg = handler.message.unwrap();
/// self.outbox_to_swarm
/// .push_back(NetworkBehaviourAction::GenerateEvent(msg))
/// }
/// #
/// # fn poll(
/// # &mut self,
/// # _: &mut Context<'_>,
/// # _: &mut impl PollParameters,
2022-02-21 13:32:24 +01:00
/// # ) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
2021-08-31 17:00:51 +02:00
/// # if let Some(action) = self.outbox_to_swarm.pop_front() {
/// # return Poll::Ready(action);
/// # }
/// # Poll::Pending
/// # }
/// }
///
/// # struct MyHandler {
/// # message: Option<PreciousMessage>,
/// # }
/// #
2022-02-21 13:32:24 +01:00
/// # impl ConnectionHandler for MyHandler {
2021-08-31 17:00:51 +02:00
/// # type InEvent = Void;
/// # type OutEvent = Void;
/// # type Error = Void;
/// # type InboundProtocol = DeniedUpgrade;
/// # type OutboundProtocol = DeniedUpgrade;
/// # type InboundOpenInfo = ();
/// # type OutboundOpenInfo = Void;
/// #
/// # fn listen_protocol(
/// # &self,
/// # ) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
/// # SubstreamProtocol::new(DeniedUpgrade, ())
/// # }
/// #
/// # fn inject_fully_negotiated_inbound(
/// # &mut self,
/// # _: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
/// # _: Self::InboundOpenInfo,
/// # ) {
/// # }
/// #
/// # fn inject_fully_negotiated_outbound(
/// # &mut self,
/// # _: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
/// # _: Self::OutboundOpenInfo,
/// # ) {
/// # }
/// #
/// # fn inject_event(&mut self, _event: Self::InEvent) {}
/// #
/// # fn inject_dial_upgrade_error(
/// # &mut self,
/// # _: Self::OutboundOpenInfo,
2022-02-21 13:32:24 +01:00
/// # _: ConnectionHandlerUpgrErr<Void>,
2021-08-31 17:00:51 +02:00
/// # ) {
/// # }
/// #
/// # fn connection_keep_alive(&self) -> KeepAlive {
/// # KeepAlive::Yes
/// # }
/// #
/// # fn poll(
/// # &mut self,
/// # _: &mut Context<'_>,
/// # ) -> Poll<
2022-02-21 13:32:24 +01:00
/// # ConnectionHandlerEvent<
2021-08-31 17:00:51 +02:00
/// # Self::OutboundProtocol,
/// # Self::OutboundOpenInfo,
/// # Self::OutEvent,
/// # Self::Error,
/// # >,
/// # > {
/// # todo!("If `Self::message.is_some()` send the message to the remote.")
/// # }
/// # }
/// # #[derive(Debug, PartialEq, Eq)]
/// # struct PreciousMessage(String);
/// ```
2021-11-15 14:17:23 +01:00
Dial { opts : DialOpts , handler : THandler } ,
2019-04-04 12:25:42 -03:00
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
/// Instructs the `Swarm` to send an event to the handler dedicated to a
/// connection with a peer.
///
/// If the `Swarm` is connected to the peer, the message is delivered to the
2022-02-21 13:32:24 +01:00
/// [`ConnectionHandler`] instance identified by the peer ID and connection ID.
2019-04-04 12:25:42 -03:00
///
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
/// If the specified connection no longer exists, the event is silently dropped.
///
/// Typically the connection ID given is the same as the one passed to
/// [`NetworkBehaviour::inject_event`], i.e. whenever the behaviour wishes to
/// respond to a request on the same connection (and possibly the same
2022-02-21 13:32:24 +01:00
/// substream, as per the implementation of [`ConnectionHandler`]).
2019-06-03 18:16:02 +02:00
///
/// Note that even if the peer is currently connected, connections can get closed
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
/// at any time and thus the event may not reach a handler.
NotifyHandler {
2022-02-21 13:32:24 +01:00
/// The peer for whom a [`ConnectionHandler`] should be notified.
2019-04-04 12:25:42 -03:00
peer_id : PeerId ,
2020-12-17 11:01:45 +01:00
/// The options w.r.t. which connection handler to notify of the event.
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
handler : NotifyHandler ,
/// The event to send.
2019-04-04 12:25:42 -03:00
event : TInEvent ,
} ,
2020-11-18 15:52:33 +01:00
/// Informs the `Swarm` about an address observed by a remote for
/// the local node by which the local node is supposedly publicly
/// reachable.
2019-04-04 12:25:42 -03:00
///
2019-05-02 19:46:27 +02:00
/// It is advisable to issue `ReportObservedAddr` actions at a fixed frequency
/// per node. This way address information will be more accurate over time
/// and individual outliers carry less weight.
2019-04-04 12:25:42 -03:00
ReportObservedAddr {
2019-04-16 15:57:29 +02:00
/// The observed address of the local node.
2019-04-04 12:25:42 -03:00
address : Multiaddr ,
2020-11-18 15:52:33 +01:00
/// The score to associate with this observation, i.e.
/// an indicator for the trusworthiness of this address
/// relative to other observed addresses.
score : AddressScore ,
2019-04-04 12:25:42 -03:00
} ,
2021-07-03 00:35:51 +07:00
/// Instructs the `Swarm` to initiate a graceful close of one or all connections
/// with the given peer.
///
/// Note: Closing a connection via
/// [`NetworkBehaviourAction::CloseConnection`] does not inform the
2022-02-21 13:32:24 +01:00
/// corresponding [`ConnectionHandler`].
/// Closing a connection via a [`ConnectionHandler`] can be done
/// either in a collaborative manner across [`ConnectionHandler`]s
/// with [`ConnectionHandler::connection_keep_alive`] or directly with
/// [`ConnectionHandlerEvent::Close`](crate::ConnectionHandlerEvent::Close).
2021-07-03 00:35:51 +07:00
CloseConnection {
/// The peer to disconnect.
peer_id : PeerId ,
/// Whether to close a specific or all connections to the given peer.
connection : CloseConnection ,
} ,
2019-04-04 12:25:42 -03:00
}
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
2022-02-21 13:32:24 +01:00
impl < TOutEvent , THandler : IntoConnectionHandler , TInEventOld >
2021-08-31 17:00:51 +02:00
NetworkBehaviourAction < TOutEvent , THandler , TInEventOld >
{
2020-08-12 16:04:54 +02:00
/// Map the handler event.
2021-08-31 17:00:51 +02:00
pub fn map_in < TInEventNew > (
self ,
f : impl FnOnce ( TInEventOld ) -> TInEventNew ,
) -> NetworkBehaviourAction < TOutEvent , THandler , TInEventNew > {
2020-08-12 16:04:54 +02:00
match self {
NetworkBehaviourAction ::GenerateEvent ( e ) = > NetworkBehaviourAction ::GenerateEvent ( e ) ,
2021-11-15 14:17:23 +01:00
NetworkBehaviourAction ::Dial { opts , handler } = > {
NetworkBehaviourAction ::Dial { opts , handler }
2021-08-11 13:12:12 +02:00
}
2020-08-12 16:04:54 +02:00
NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
event ,
} = > NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
event : f ( event ) ,
} ,
2020-11-18 15:52:33 +01:00
NetworkBehaviourAction ::ReportObservedAddr { address , score } = > {
2021-07-03 00:35:51 +07:00
NetworkBehaviourAction ::ReportObservedAddr { address , score }
2021-08-11 13:12:12 +02:00
}
2021-07-03 00:35:51 +07:00
NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} = > NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} ,
2020-08-12 16:04:54 +02:00
}
}
2021-08-31 17:00:51 +02:00
}
2020-08-12 16:04:54 +02:00
2022-02-21 13:32:24 +01:00
impl < TOutEvent , THandler : IntoConnectionHandler > NetworkBehaviourAction < TOutEvent , THandler > {
2020-08-12 16:04:54 +02:00
/// Map the event the swarm will return.
2021-08-31 17:00:51 +02:00
pub fn map_out < E > ( self , f : impl FnOnce ( TOutEvent ) -> E ) -> NetworkBehaviourAction < E , THandler > {
2020-08-12 16:04:54 +02:00
match self {
NetworkBehaviourAction ::GenerateEvent ( e ) = > NetworkBehaviourAction ::GenerateEvent ( f ( e ) ) ,
2021-11-15 14:17:23 +01:00
NetworkBehaviourAction ::Dial { opts , handler } = > {
NetworkBehaviourAction ::Dial { opts , handler }
2021-08-11 13:12:12 +02:00
}
2020-08-12 16:04:54 +02:00
NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
event ,
} = > NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
event ,
} ,
2020-11-18 15:52:33 +01:00
NetworkBehaviourAction ::ReportObservedAddr { address , score } = > {
2021-07-03 00:35:51 +07:00
NetworkBehaviourAction ::ReportObservedAddr { address , score }
2021-08-11 13:12:12 +02:00
}
2021-07-03 00:35:51 +07:00
NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} = > NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} ,
2020-08-12 16:04:54 +02:00
}
}
}
2021-08-31 17:00:51 +02:00
impl < TInEvent , TOutEvent , THandlerOld > NetworkBehaviourAction < TOutEvent , THandlerOld >
where
2022-02-21 13:32:24 +01:00
THandlerOld : IntoConnectionHandler ,
< THandlerOld as IntoConnectionHandler > ::Handler : ConnectionHandler < InEvent = TInEvent > ,
2021-08-31 17:00:51 +02:00
{
/// Map the handler.
pub fn map_handler < THandlerNew > (
self ,
f : impl FnOnce ( THandlerOld ) -> THandlerNew ,
) -> NetworkBehaviourAction < TOutEvent , THandlerNew >
where
2022-02-21 13:32:24 +01:00
THandlerNew : IntoConnectionHandler ,
< THandlerNew as IntoConnectionHandler > ::Handler : ConnectionHandler < InEvent = TInEvent > ,
2021-08-31 17:00:51 +02:00
{
match self {
NetworkBehaviourAction ::GenerateEvent ( e ) = > NetworkBehaviourAction ::GenerateEvent ( e ) ,
2021-11-15 14:17:23 +01:00
NetworkBehaviourAction ::Dial { opts , handler } = > NetworkBehaviourAction ::Dial {
opts ,
2021-08-31 17:00:51 +02:00
handler : f ( handler ) ,
} ,
NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
event ,
} = > NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
2021-09-14 16:00:05 +03:00
event ,
2021-08-31 17:00:51 +02:00
} ,
NetworkBehaviourAction ::ReportObservedAddr { address , score } = > {
NetworkBehaviourAction ::ReportObservedAddr { address , score }
}
NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} = > NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} ,
}
}
}
2021-12-12 03:51:02 -08:00
impl < TInEventOld , TOutEvent , THandlerOld > NetworkBehaviourAction < TOutEvent , THandlerOld >
where
2022-02-21 13:32:24 +01:00
THandlerOld : IntoConnectionHandler ,
< THandlerOld as IntoConnectionHandler > ::Handler : ConnectionHandler < InEvent = TInEventOld > ,
2021-12-12 03:51:02 -08:00
{
/// Map the handler and handler event.
pub fn map_handler_and_in < THandlerNew , TInEventNew > (
self ,
f_handler : impl FnOnce ( THandlerOld ) -> THandlerNew ,
f_in_event : impl FnOnce ( TInEventOld ) -> TInEventNew ,
) -> NetworkBehaviourAction < TOutEvent , THandlerNew >
where
2022-02-21 13:32:24 +01:00
THandlerNew : IntoConnectionHandler ,
< THandlerNew as IntoConnectionHandler > ::Handler : ConnectionHandler < InEvent = TInEventNew > ,
2021-12-12 03:51:02 -08:00
{
match self {
NetworkBehaviourAction ::GenerateEvent ( e ) = > NetworkBehaviourAction ::GenerateEvent ( e ) ,
NetworkBehaviourAction ::Dial { opts , handler } = > NetworkBehaviourAction ::Dial {
opts ,
handler : f_handler ( handler ) ,
} ,
NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
event ,
} = > NetworkBehaviourAction ::NotifyHandler {
peer_id ,
handler ,
event : f_in_event ( event ) ,
} ,
NetworkBehaviourAction ::ReportObservedAddr { address , score } = > {
NetworkBehaviourAction ::ReportObservedAddr { address , score }
}
NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} = > NetworkBehaviourAction ::CloseConnection {
peer_id ,
connection ,
} ,
}
}
}
2020-12-17 11:01:45 +01:00
/// The options w.r.t. which connection handler to notify of an event.
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
#[ derive(Debug, Clone) ]
pub enum NotifyHandler {
/// Notify a particular connection handler.
One ( ConnectionId ) ,
/// Notify an arbitrary connection handler.
Any ,
}
2021-07-03 00:35:51 +07:00
/// The options which connections to close.
#[ derive(Debug, Clone) ]
pub enum CloseConnection {
/// Disconnect a particular connection.
One ( ConnectionId ) ,
/// Disconnect all connections.
All ,
}
impl Default for CloseConnection {
fn default ( ) -> Self {
CloseConnection ::All
}
}
2022-11-17 09:28:40 +00:00
/// Enumeration with the list of the possible events
/// to pass to [`on_swarm_event`](NetworkBehaviour::on_swarm_event).
pub enum FromSwarm < ' a , Handler : IntoConnectionHandler > {
/// Informs the behaviour about a newly established connection to a peer.
ConnectionEstablished ( ConnectionEstablished < ' a > ) ,
/// Informs the behaviour about a closed connection to a peer.
///
/// This event is always paired with an earlier
/// [`FromSwarm::ConnectionEstablished`] with the same peer ID, connection ID
/// and endpoint.
ConnectionClosed ( ConnectionClosed < ' a , Handler > ) ,
/// Informs the behaviour that the [`ConnectedPoint`] of an existing
/// connection has changed.
AddressChange ( AddressChange < ' a > ) ,
/// Informs the behaviour that the dial to a known
/// or unknown node failed.
DialFailure ( DialFailure < ' a , Handler > ) ,
/// Informs the behaviour that an error
/// happened on an incoming connection during its initial handshake.
///
/// This can include, for example, an error during the handshake of the encryption layer, or the
/// connection unexpectedly closed.
ListenFailure ( ListenFailure < ' a , Handler > ) ,
/// Informs the behaviour that a new listener was created.
NewListener ( NewListener ) ,
/// Informs the behaviour that we have started listening on a new multiaddr.
NewListenAddr ( NewListenAddr < ' a > ) ,
/// Informs the behaviour that a multiaddr
/// we were listening on has expired,
/// which means that we are no longer listening on it.
ExpiredListenAddr ( ExpiredListenAddr < ' a > ) ,
/// Informs the behaviour that a listener experienced an error.
ListenerError ( ListenerError < ' a > ) ,
/// Informs the behaviour that a listener closed.
ListenerClosed ( ListenerClosed < ' a > ) ,
/// Informs the behaviour that we have discovered a new external address for us.
NewExternalAddr ( NewExternalAddr < ' a > ) ,
/// Informs the behaviour that an external address was removed.
ExpiredExternalAddr ( ExpiredExternalAddr < ' a > ) ,
}
/// [`FromSwarm`] variant that informs the behaviour about a newly established connection to a peer.
#[ derive(Clone, Copy) ]
pub struct ConnectionEstablished < ' a > {
pub peer_id : PeerId ,
pub connection_id : ConnectionId ,
pub endpoint : & ' a ConnectedPoint ,
pub failed_addresses : & ' a [ Multiaddr ] ,
pub other_established : usize ,
}
/// [`FromSwarm`] variant that informs the behaviour about a closed connection to a peer.
///
/// This event is always paired with an earlier
/// [`FromSwarm::ConnectionEstablished`] with the same peer ID, connection ID
/// and endpoint.
pub struct ConnectionClosed < ' a , Handler : IntoConnectionHandler > {
pub peer_id : PeerId ,
pub connection_id : ConnectionId ,
pub endpoint : & ' a ConnectedPoint ,
pub handler : < Handler as IntoConnectionHandler > ::Handler ,
pub remaining_established : usize ,
}
/// [`FromSwarm`] variant that informs the behaviour that the [`ConnectedPoint`] of an existing
/// connection has changed.
#[ derive(Clone, Copy) ]
pub struct AddressChange < ' a > {
pub peer_id : PeerId ,
pub connection_id : ConnectionId ,
pub old : & ' a ConnectedPoint ,
pub new : & ' a ConnectedPoint ,
}
/// [`FromSwarm`] variant that informs the behaviour that the dial to a known
/// or unknown node failed.
#[ derive(Clone, Copy) ]
pub struct DialFailure < ' a , Handler > {
pub peer_id : Option < PeerId > ,
pub handler : Handler ,
pub error : & ' a DialError ,
}
/// [`FromSwarm`] variant that informs the behaviour that an error
/// happened on an incoming connection during its initial handshake.
///
/// This can include, for example, an error during the handshake of the encryption layer, or the
/// connection unexpectedly closed.
#[ derive(Clone, Copy) ]
pub struct ListenFailure < ' a , Handler > {
pub local_addr : & ' a Multiaddr ,
pub send_back_addr : & ' a Multiaddr ,
pub handler : Handler ,
}
/// [`FromSwarm`] variant that informs the behaviour that a new listener was created.
#[ derive(Clone, Copy) ]
pub struct NewListener {
pub listener_id : ListenerId ,
}
/// [`FromSwarm`] variant that informs the behaviour
/// that we have started listening on a new multiaddr.
#[ derive(Clone, Copy) ]
pub struct NewListenAddr < ' a > {
pub listener_id : ListenerId ,
pub addr : & ' a Multiaddr ,
}
/// [`FromSwarm`] variant that informs the behaviour that a multiaddr
/// we were listening on has expired,
/// which means that we are no longer listening on it.
#[ derive(Clone, Copy) ]
pub struct ExpiredListenAddr < ' a > {
pub listener_id : ListenerId ,
pub addr : & ' a Multiaddr ,
}
/// [`FromSwarm`] variant that informs the behaviour that a listener experienced an error.
#[ derive(Clone, Copy) ]
pub struct ListenerError < ' a > {
pub listener_id : ListenerId ,
pub err : & ' a ( dyn std ::error ::Error + 'static ) ,
}
/// [`FromSwarm`] variant that informs the behaviour that a listener closed.
#[ derive(Clone, Copy) ]
pub struct ListenerClosed < ' a > {
pub listener_id : ListenerId ,
pub reason : Result < ( ) , & ' a std ::io ::Error > ,
}
/// [`FromSwarm`] variant that informs the behaviour
/// that we have discovered a new external address for us.
#[ derive(Clone, Copy) ]
pub struct NewExternalAddr < ' a > {
pub addr : & ' a Multiaddr ,
}
/// [`FromSwarm`] variant that informs the behaviour that an external address was removed.
#[ derive(Clone, Copy) ]
pub struct ExpiredExternalAddr < ' a > {
pub addr : & ' a Multiaddr ,
}
impl < ' a , Handler : IntoConnectionHandler > FromSwarm < ' a , Handler > {
fn map_handler < NewHandler > (
self ,
map_into_handler : impl FnOnce ( Handler ) -> NewHandler ,
map_handler : impl FnOnce (
< Handler as IntoConnectionHandler > ::Handler ,
) -> < NewHandler as IntoConnectionHandler > ::Handler ,
) -> FromSwarm < ' a , NewHandler >
where
NewHandler : IntoConnectionHandler ,
{
self . maybe_map_handler ( | h | Some ( map_into_handler ( h ) ) , | h | Some ( map_handler ( h ) ) )
. expect ( " To return Some as all closures return Some. " )
}
fn maybe_map_handler < NewHandler > (
self ,
map_into_handler : impl FnOnce ( Handler ) -> Option < NewHandler > ,
map_handler : impl FnOnce (
< Handler as IntoConnectionHandler > ::Handler ,
) -> Option < < NewHandler as IntoConnectionHandler > ::Handler > ,
) -> Option < FromSwarm < ' a , NewHandler > >
where
NewHandler : IntoConnectionHandler ,
{
match self {
FromSwarm ::ConnectionClosed ( ConnectionClosed {
peer_id ,
connection_id ,
endpoint ,
handler ,
remaining_established ,
} ) = > Some ( FromSwarm ::ConnectionClosed ( ConnectionClosed {
peer_id ,
connection_id ,
endpoint ,
handler : map_handler ( handler ) ? ,
remaining_established ,
} ) ) ,
FromSwarm ::ConnectionEstablished ( ConnectionEstablished {
peer_id ,
connection_id ,
endpoint ,
failed_addresses ,
other_established ,
} ) = > Some ( FromSwarm ::ConnectionEstablished ( ConnectionEstablished {
peer_id ,
connection_id ,
endpoint ,
failed_addresses ,
other_established ,
} ) ) ,
FromSwarm ::AddressChange ( AddressChange {
peer_id ,
connection_id ,
old ,
new ,
} ) = > Some ( FromSwarm ::AddressChange ( AddressChange {
peer_id ,
connection_id ,
old ,
new ,
} ) ) ,
FromSwarm ::DialFailure ( DialFailure {
peer_id ,
handler ,
error ,
} ) = > Some ( FromSwarm ::DialFailure ( DialFailure {
peer_id ,
handler : map_into_handler ( handler ) ? ,
error ,
} ) ) ,
FromSwarm ::ListenFailure ( ListenFailure {
local_addr ,
send_back_addr ,
handler ,
} ) = > Some ( FromSwarm ::ListenFailure ( ListenFailure {
local_addr ,
send_back_addr ,
handler : map_into_handler ( handler ) ? ,
} ) ) ,
FromSwarm ::NewListener ( NewListener { listener_id } ) = > {
Some ( FromSwarm ::NewListener ( NewListener { listener_id } ) )
}
FromSwarm ::NewListenAddr ( NewListenAddr { listener_id , addr } ) = > {
Some ( FromSwarm ::NewListenAddr ( NewListenAddr {
listener_id ,
addr ,
} ) )
}
FromSwarm ::ExpiredListenAddr ( ExpiredListenAddr { listener_id , addr } ) = > {
Some ( FromSwarm ::ExpiredListenAddr ( ExpiredListenAddr {
listener_id ,
addr ,
} ) )
}
FromSwarm ::ListenerError ( ListenerError { listener_id , err } ) = > {
Some ( FromSwarm ::ListenerError ( ListenerError { listener_id , err } ) )
}
FromSwarm ::ListenerClosed ( ListenerClosed {
listener_id ,
reason ,
} ) = > Some ( FromSwarm ::ListenerClosed ( ListenerClosed {
listener_id ,
reason ,
} ) ) ,
FromSwarm ::NewExternalAddr ( NewExternalAddr { addr } ) = > {
Some ( FromSwarm ::NewExternalAddr ( NewExternalAddr { addr } ) )
}
FromSwarm ::ExpiredExternalAddr ( ExpiredExternalAddr { addr } ) = > {
Some ( FromSwarm ::ExpiredExternalAddr ( ExpiredExternalAddr { addr } ) )
}
}
}
}
/// Helper function to call [`NetworkBehaviour`]'s `inject_*` methods given a `FromSwarm.
/// TODO: Remove this function when we remove the remaining `inject_*` calls
/// from [`Either`] and [`Toggle`].
pub ( crate ) fn inject_from_swarm < T : NetworkBehaviour > (
behaviour : & mut T ,
event : FromSwarm < T ::ConnectionHandler > ,
) {
match event {
FromSwarm ::ConnectionEstablished ( ConnectionEstablished {
peer_id ,
connection_id ,
endpoint ,
failed_addresses ,
other_established ,
} ) = > {
#[ allow(deprecated) ]
behaviour . inject_connection_established (
& peer_id ,
& connection_id ,
endpoint ,
Some ( & failed_addresses . into ( ) ) ,
other_established ,
) ;
}
FromSwarm ::ConnectionClosed ( ConnectionClosed {
peer_id ,
connection_id ,
endpoint ,
handler ,
remaining_established ,
} ) = > {
#[ allow(deprecated) ]
behaviour . inject_connection_closed (
& peer_id ,
& connection_id ,
endpoint ,
handler ,
remaining_established ,
) ;
}
FromSwarm ::AddressChange ( AddressChange {
peer_id ,
connection_id ,
old ,
new ,
} ) = > {
#[ allow(deprecated) ]
behaviour . inject_address_change ( & peer_id , & connection_id , old , new ) ;
}
FromSwarm ::DialFailure ( DialFailure {
peer_id ,
handler ,
error ,
} ) = > {
#[ allow(deprecated) ]
behaviour . inject_dial_failure ( peer_id , handler , error ) ;
}
FromSwarm ::ListenFailure ( ListenFailure {
local_addr ,
send_back_addr ,
handler ,
} ) = > {
#[ allow(deprecated) ]
behaviour . inject_listen_failure ( local_addr , send_back_addr , handler ) ;
}
FromSwarm ::NewListener ( NewListener { listener_id } ) = > {
#[ allow(deprecated) ]
behaviour . inject_new_listener ( listener_id ) ;
}
FromSwarm ::NewListenAddr ( NewListenAddr { listener_id , addr } ) = > {
#[ allow(deprecated) ]
behaviour . inject_new_listen_addr ( listener_id , addr ) ;
}
FromSwarm ::ExpiredListenAddr ( ExpiredListenAddr { listener_id , addr } ) = > {
#[ allow(deprecated) ]
behaviour . inject_expired_listen_addr ( listener_id , addr ) ;
}
FromSwarm ::ListenerError ( ListenerError { listener_id , err } ) = > {
#[ allow(deprecated) ]
behaviour . inject_listener_error ( listener_id , err ) ;
}
FromSwarm ::ListenerClosed ( ListenerClosed {
listener_id ,
reason ,
} ) = > {
#[ allow(deprecated) ]
behaviour . inject_listener_closed ( listener_id , reason ) ;
}
FromSwarm ::NewExternalAddr ( NewExternalAddr { addr } ) = > {
#[ allow(deprecated) ]
behaviour . inject_new_external_addr ( addr ) ;
}
FromSwarm ::ExpiredExternalAddr ( ExpiredExternalAddr { addr } ) = >
{
#[ allow(deprecated) ]
behaviour . inject_expired_external_addr ( addr )
}
}
}