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.
|
|
|
|
|
2020-11-18 15:52:33 +01:00
|
|
|
use crate::{AddressScore, AddressRecord};
|
2019-07-04 14:47:59 +02:00
|
|
|
use crate::protocols_handler::{IntoProtocolsHandler, ProtocolsHandler};
|
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::{ConnectedPoint, Multiaddr, PeerId, connection::{ConnectionId, ListenerId}};
|
2019-09-16 11:08:44 +02:00
|
|
|
use std::{error, task::Context, task::Poll};
|
2019-04-04 12:25:42 -03:00
|
|
|
|
|
|
|
/// A behaviour for the network. Allows customizing the swarm.
|
|
|
|
///
|
|
|
|
/// This trait has been designed to be composable. Multiple implementations can be combined into
|
|
|
|
/// one that handles all the behaviours at once.
|
2020-01-14 13:48:16 +02:00
|
|
|
///
|
|
|
|
/// # Deriving `NetworkBehaviour`
|
|
|
|
///
|
|
|
|
/// Crate users can implement this trait by using the the `#[derive(NetworkBehaviour)]`
|
|
|
|
/// proc macro re-exported by the `libp2p` crate. The macro generates a delegating `trait`
|
2020-07-08 19:32:47 +10:00
|
|
|
/// implementation for the `struct`, which delegates method calls to all trait members.
|
2020-01-14 13:48:16 +02:00
|
|
|
///
|
2020-07-08 19:32:47 +10:00
|
|
|
/// By default the derive sets the [`NetworkBehaviour::OutEvent`] as `()` but this can be overridden
|
2020-01-14 13:48:16 +02:00
|
|
|
/// with `#[behaviour(out_event = "AnotherType")]`.
|
|
|
|
///
|
2020-07-08 19:32:47 +10:00
|
|
|
/// Struct members that don't implement [`NetworkBehaviour`] must be annotated with `#[behaviour(ignore)]`.
|
|
|
|
///
|
|
|
|
/// By default, events generated by the remaining members are delegated to [`NetworkBehaviourEventProcess`]
|
|
|
|
/// implementations. Those must be provided by the user on the type that [`NetworkBehaviour`] is
|
|
|
|
/// derived on.
|
|
|
|
///
|
|
|
|
/// Alternatively, users can specify `#[behaviour(event_process = false)]`. In this case, users
|
|
|
|
/// should provide a custom `out_event` and implement [`From`] for each of the event types generated
|
|
|
|
/// by the struct members.
|
|
|
|
/// Not processing events within the derived [`NetworkBehaviour`] will cause them to be emitted as
|
|
|
|
/// part of polling the swarm in [`SwarmEvent::Behaviour`](crate::SwarmEvent::Behaviour).
|
|
|
|
///
|
|
|
|
/// Optionally one can provide a custom `poll` function through the `#[behaviour(poll_method = "poll")]`
|
|
|
|
/// attribute.
|
|
|
|
/// This function must have the same signature as the [`NetworkBehaviour#poll`] function and will
|
|
|
|
/// be called last within the generated [`NetworkBehaviour`] implementation.
|
2020-02-07 16:29:30 +01:00
|
|
|
pub trait NetworkBehaviour: Send + 'static {
|
2019-06-03 18:16:02 +02:00
|
|
|
/// Handler for all the protocols the network behaviour supports.
|
2019-04-04 12:25:42 -03:00
|
|
|
type ProtocolsHandler: IntoProtocolsHandler;
|
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
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Creates a new `ProtocolsHandler` for a connection with a peer.
|
2019-06-03 18:16:02 +02:00
|
|
|
///
|
|
|
|
/// Every time an incoming connection is opened, and every time we start dialing a node, this
|
|
|
|
/// method is called.
|
|
|
|
///
|
|
|
|
/// The returned object is a handler for that specific connection, and will be moved to a
|
|
|
|
/// background task dedicated to that connection.
|
|
|
|
///
|
|
|
|
/// 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.
|
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
|
|
|
/// Messages sent from the handler to the behaviour are injected with `inject_event`, and
|
2019-06-03 18:16:02 +02:00
|
|
|
/// the behaviour can send a message to the handler by making `poll` return `SendEvent`.
|
2019-04-04 12:25:42 -03:00
|
|
|
fn new_handler(&mut self) -> Self::ProtocolsHandler;
|
|
|
|
|
|
|
|
/// 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.
|
2019-04-04 12:25:42 -03:00
|
|
|
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>;
|
|
|
|
|
2020-03-31 15:41:13 +02:00
|
|
|
/// Indicates the behaviour that we connected to the node with the given peer id.
|
2019-06-03 18:16:02 +02:00
|
|
|
///
|
|
|
|
/// This node now has a handler (as spawned by `new_handler`) running in the background.
|
2020-03-31 15:41:13 +02:00
|
|
|
///
|
|
|
|
/// This method is only called when the connection to the peer is
|
|
|
|
/// established, preceded by `inject_connection_established`.
|
|
|
|
fn inject_connected(&mut self, peer_id: &PeerId);
|
2019-04-04 12:25:42 -03:00
|
|
|
|
2020-03-31 15:41:13 +02:00
|
|
|
/// Indicates the behaviour that we disconnected from the node with the given peer id.
|
2019-06-03 18:16:02 +02:00
|
|
|
///
|
|
|
|
/// There is no handler running anymore for this node. Any event that has been sent to it may
|
|
|
|
/// or may not have been processed by the handler.
|
2020-03-31 15:41:13 +02:00
|
|
|
///
|
|
|
|
/// This method is only called when the last established connection to the peer
|
|
|
|
/// is closed, preceded by `inject_connection_closed`.
|
|
|
|
fn inject_disconnected(&mut self, peer_id: &PeerId);
|
|
|
|
|
|
|
|
/// Informs the behaviour about a newly established connection to a peer.
|
|
|
|
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/// Informs the behaviour about a closed connection to a peer.
|
|
|
|
///
|
|
|
|
/// A call to this method is always paired with an earlier call to
|
|
|
|
/// `inject_connection_established` with the same peer ID, connection ID and
|
|
|
|
/// endpoint.
|
|
|
|
fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)
|
|
|
|
{}
|
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.
|
|
|
|
fn inject_address_change(
|
|
|
|
&mut self,
|
|
|
|
_: &PeerId,
|
|
|
|
_: &ConnectionId,
|
|
|
|
_old: &ConnectedPoint,
|
|
|
|
_new: &ConnectedPoint
|
|
|
|
) {}
|
|
|
|
|
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
|
|
|
///
|
2019-06-03 18:16:02 +02:00
|
|
|
/// The `peer_id` is guaranteed to be in a connected state. In other words, `inject_connected`
|
|
|
|
/// has previously been called with this `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
|
|
|
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,
|
2019-04-04 12:25:42 -03:00
|
|
|
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Indicates to the behaviour that we tried to reach an address, but failed.
|
|
|
|
///
|
|
|
|
/// If we were trying to reach a specific node, its ID is passed as parameter. If this is the
|
|
|
|
/// last address to attempt for the given node, then `inject_dial_failure` is called afterwards.
|
|
|
|
fn inject_addr_reach_failure(&mut self, _peer_id: Option<&PeerId>, _addr: &Multiaddr, _error: &dyn error::Error) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates to the behaviour that we tried to dial all the addresses known for a node, but
|
|
|
|
/// failed.
|
2019-06-03 18:16:02 +02:00
|
|
|
///
|
|
|
|
/// The `peer_id` is guaranteed to be in a disconnected state. In other words,
|
|
|
|
/// `inject_connected` has not been called, or `inject_disconnected` has been called since then.
|
2019-04-04 12:25:42 -03:00
|
|
|
fn inject_dial_failure(&mut self, _peer_id: &PeerId) {
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:36:08 +02:00
|
|
|
/// Indicates to the behaviour that we have started listening on a new multiaddr.
|
|
|
|
fn inject_new_listen_addr(&mut self, _addr: &Multiaddr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates to the behaviour that a new multiaddr we were listening on has expired,
|
|
|
|
/// which means that we are no longer listening in it.
|
|
|
|
fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr) {
|
|
|
|
}
|
|
|
|
|
2019-04-16 17:00:20 +02:00
|
|
|
/// Indicates to the behaviour that we have discovered a new external address for us.
|
|
|
|
fn inject_new_external_addr(&mut self, _addr: &Multiaddr) {
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:41:12 +02:00
|
|
|
/// A listener experienced an error.
|
|
|
|
fn inject_listener_error(&mut self, _id: ListenerId, _err: &(dyn std::error::Error + 'static)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A listener closed.
|
2020-04-01 22:49:42 +10:00
|
|
|
fn inject_listener_closed(&mut self, _id: ListenerId, _reason: Result<(), &std::io::Error>) {
|
2019-08-13 15:41:12 +02: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.
|
2020-07-27 20:27:33 +00:00
|
|
|
fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters)
|
2019-09-16 11:08:44 +02:00
|
|
|
-> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>;
|
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.
|
|
|
|
fn listened_addresses(&self) -> Self::ListenedAddressesIter;
|
|
|
|
|
|
|
|
/// Returns the list of the addresses nodes can use to reach us.
|
|
|
|
fn external_addresses(&self) -> Self::ExternalAddressesIter;
|
|
|
|
|
|
|
|
/// Returns the peer id of the local node.
|
|
|
|
fn local_peer_id(&self) -> &PeerId;
|
|
|
|
}
|
|
|
|
|
2020-07-08 19:32:47 +10:00
|
|
|
/// When deriving [`NetworkBehaviour`] this trait must by default be implemented for all the
|
|
|
|
/// possible event types generated by the inner behaviours.
|
|
|
|
///
|
|
|
|
/// You can opt out of this behaviour through `#[behaviour(event_process = false)]`. See the
|
|
|
|
/// documentation of [`NetworkBehaviour`] for details.
|
2019-04-04 12:25:42 -03:00
|
|
|
pub trait NetworkBehaviourEventProcess<TEvent> {
|
|
|
|
/// Called when one of the fields of the type you're deriving `NetworkBehaviour` on generates
|
|
|
|
/// an event.
|
|
|
|
fn inject_event(&mut self, event: TEvent);
|
|
|
|
}
|
|
|
|
|
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
|
2019-04-04 12:25:42 -03:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
|
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),
|
|
|
|
|
2019-06-03 18:16:02 +02:00
|
|
|
/// Instructs the swarm to dial the given multiaddress, with no knowledge of the `PeerId` that
|
|
|
|
/// may be reached.
|
2019-04-04 12:25:42 -03:00
|
|
|
DialAddress {
|
|
|
|
/// The address to dial.
|
|
|
|
address: Multiaddr,
|
|
|
|
},
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Instructs the swarm to dial a known `PeerId`.
|
2019-04-04 12:25:42 -03:00
|
|
|
///
|
2019-06-03 18:16:02 +02:00
|
|
|
/// The `addresses_of_peer` method is called to determine which addresses to attempt to reach.
|
|
|
|
///
|
|
|
|
/// If we were already trying to dial this node, the addresses that are not yet in the queue of
|
|
|
|
/// addresses to try are added back to this queue.
|
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// On success, [`NetworkBehaviour::inject_connected`] is invoked.
|
|
|
|
/// On failure, [`NetworkBehaviour::inject_dial_failure`] is invoked.
|
2019-04-04 12:25:42 -03:00
|
|
|
DialPeer {
|
|
|
|
/// The peer to try reach.
|
|
|
|
peer_id: PeerId,
|
2020-03-31 15:41:13 +02:00
|
|
|
/// The condition for initiating a new dialing attempt.
|
|
|
|
condition: DialPeerCondition,
|
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
|
|
|
|
/// `ProtocolsHandler` 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
|
|
|
|
/// substream, as per the implementation of `ProtocolsHandler`).
|
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 {
|
|
|
|
/// The peer for whom a `ProtocolsHandler` should be notified.
|
2019-04-04 12:25:42 -03:00
|
|
|
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
|
|
|
/// The ID of the connection whose `ProtocolsHandler` to notify.
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
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
|
|
|
|
2020-08-12 16:04:54 +02:00
|
|
|
impl<TInEvent, TOutEvent> NetworkBehaviourAction<TInEvent, TOutEvent> {
|
|
|
|
/// Map the handler event.
|
|
|
|
pub fn map_in<E>(self, f: impl FnOnce(TInEvent) -> E) -> NetworkBehaviourAction<E, TOutEvent> {
|
|
|
|
match self {
|
|
|
|
NetworkBehaviourAction::GenerateEvent(e) =>
|
|
|
|
NetworkBehaviourAction::GenerateEvent(e),
|
|
|
|
NetworkBehaviourAction::DialAddress { address } =>
|
|
|
|
NetworkBehaviourAction::DialAddress { address },
|
|
|
|
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
|
|
|
|
NetworkBehaviourAction::DialPeer { peer_id, condition },
|
|
|
|
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 } =>
|
|
|
|
NetworkBehaviourAction::ReportObservedAddr { address, score }
|
2020-08-12 16:04:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map the event the swarm will return.
|
|
|
|
pub fn map_out<E>(self, f: impl FnOnce(TOutEvent) -> E) -> NetworkBehaviourAction<TInEvent, E> {
|
|
|
|
match self {
|
|
|
|
NetworkBehaviourAction::GenerateEvent(e) =>
|
|
|
|
NetworkBehaviourAction::GenerateEvent(f(e)),
|
|
|
|
NetworkBehaviourAction::DialAddress { address } =>
|
|
|
|
NetworkBehaviourAction::DialAddress { address },
|
|
|
|
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
|
|
|
|
NetworkBehaviourAction::DialPeer { peer_id, condition },
|
|
|
|
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
|
|
|
|
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event },
|
2020-11-18 15:52:33 +01:00
|
|
|
NetworkBehaviourAction::ReportObservedAddr { address, score } =>
|
|
|
|
NetworkBehaviourAction::ReportObservedAddr { address, score }
|
2020-08-12 16:04:54 +02: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
|
|
|
/// The options w.r.t. which connection handlers to notify of an event.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum NotifyHandler {
|
|
|
|
/// Notify a particular connection handler.
|
|
|
|
One(ConnectionId),
|
|
|
|
/// Notify an arbitrary connection handler.
|
|
|
|
Any,
|
|
|
|
/// Notify all connection handlers.
|
|
|
|
All
|
|
|
|
}
|
|
|
|
|
2020-03-31 15:41:13 +02:00
|
|
|
/// The available conditions under which a new dialing attempt to
|
|
|
|
/// a peer is initiated when requested by [`NetworkBehaviourAction::DialPeer`].
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum DialPeerCondition {
|
|
|
|
/// A new dialing attempt is initiated _only if_ the peer is currently
|
|
|
|
/// considered disconnected, i.e. there is no established connection
|
|
|
|
/// and no ongoing dialing attempt.
|
|
|
|
///
|
|
|
|
/// If there is an ongoing dialing attempt, the addresses reported by
|
|
|
|
/// [`NetworkBehaviour::addresses_of_peer`] are added to the ongoing
|
|
|
|
/// dialing attempt, ignoring duplicates.
|
|
|
|
Disconnected,
|
|
|
|
/// A new dialing attempt is initiated _only if_ there is currently
|
|
|
|
/// no ongoing dialing attempt, i.e. the peer is either considered
|
|
|
|
/// disconnected or connected but without an ongoing dialing attempt.
|
|
|
|
///
|
|
|
|
/// If there is an ongoing dialing attempt, the addresses reported by
|
|
|
|
/// [`NetworkBehaviour::addresses_of_peer`] are added to the ongoing
|
|
|
|
/// dialing attempt, ignoring duplicates.
|
|
|
|
NotDialing,
|
2020-05-12 13:10:18 +02:00
|
|
|
/// A new dialing attempt is always initiated, only subject to the
|
|
|
|
/// configured connection limits.
|
|
|
|
Always,
|
2020-03-31 15:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for DialPeerCondition {
|
|
|
|
fn default() -> Self {
|
|
|
|
DialPeerCondition::Disconnected
|
|
|
|
}
|
|
|
|
}
|