Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm/: Enable advanced dialing requests #2317

Merged
merged 31 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2fd729c
swarm/: Enable advanced dialing requests
mxinden Oct 13, 2021
09688c7
swarm/src/lib: Emit SwarmEvent::Dialing
mxinden Oct 29, 2021
85b6238
Merge branch 'libp2p/master' into advanced-dialing
mxinden Nov 3, 2021
147c533
swarm/src/lib: Fold condition check into first match
mxinden Nov 3, 2021
2bda3b3
protocols/kad: Adjust to API changes
mxinden Nov 3, 2021
ad083e9
swarm/src/lib: Wrap Opts in DialOpts
mxinden Nov 8, 2021
eea9e12
protocols/identify: Update to latest changes
mxinden Nov 8, 2021
26dedbe
swarm/src/lib: Allow WithPeerIdWithAddresses::condition
mxinden Nov 8, 2021
eb67288
protocols/floodsub: Adjust to latest changes
mxinden Nov 8, 2021
a04eaa3
protocols/req-resp: Adjust to latest changes
mxinden Nov 8, 2021
57f0648
protocols/relay/src/behaviour: Update to recent changes
mxinden Nov 8, 2021
770e3af
protocols/gossipsub: Adjust to latest changes
mxinden Nov 8, 2021
96ac37e
*: Adjust to latest changes
mxinden Nov 8, 2021
9f941fd
protocols/identify: Wait for connection to close before redialing
mxinden Nov 9, 2021
d4410bb
swarm/: Add documentation
mxinden Nov 9, 2021
163229c
swarm/src/lib: Deduplicate addresses
mxinden Nov 9, 2021
8334f36
swarm/CHANGELOG: Add entry
mxinden Nov 9, 2021
1e561e8
*: Bump versions
mxinden Nov 9, 2021
2f1ae01
Merge branch 'libp2p/master' into advanced-dialing
mxinden Nov 9, 2021
036cf24
swarm/src/lib: Move dial_opts to separate file
mxinden Nov 9, 2021
eab21a6
swarm/src/dial_opts: Remove outdated comment
mxinden Nov 9, 2021
5e3edb3
swarm/src/dial_opts: Implement From<Multiaddr> for DialOpts
mxinden Nov 14, 2021
fed72bf
Merge branch 'libp2p/master' into advanced-dialing
mxinden Nov 14, 2021
09293c4
swarm/src/dial_opts: Implement From<PeerId> for DialOpts
mxinden Nov 15, 2021
9a52b30
Merge branch 'master' into advanced-dialing
mxinden Nov 15, 2021
8d9b548
misc/metrics: Fix type inference
mxinden Nov 15, 2021
1562159
examples: Help type inference
mxinden Nov 15, 2021
d11a4ed
protocols/.*/examples: Run cargo fix
mxinden Nov 15, 2021
03ef106
*: Run cargo fmt
mxinden Nov 15, 2021
8415ba2
src/tutorial: Fix type inference
mxinden Nov 15, 2021
9ada868
swarm/src/lib: Help type inference
mxinden Nov 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 15 additions & 95 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::dial_opts::DialOpts;
use crate::protocols_handler::{IntoProtocolsHandler, ProtocolsHandler};
use crate::{AddressRecord, AddressScore, DialError};
use libp2p_core::{
Expand Down Expand Up @@ -269,31 +270,7 @@ pub enum NetworkBehaviourAction<
/// Instructs the `Swarm` to return an event when it is being polled.
GenerateEvent(TOutEvent),

/// Instructs the swarm to dial the given multiaddress optionally including a [`PeerId`].
///
/// On success, [`NetworkBehaviour::inject_connection_established`] is invoked.
/// On failure, [`NetworkBehaviour::inject_dial_failure`] is invoked.
///
/// 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. See [`NetworkBehaviourAction::DialPeer`] for
/// example.
DialAddress {
/// The address to dial.
address: Multiaddr,
/// The handler to be used to handle the connection to the peer.
handler: THandler,
},

/// Instructs the swarm to dial a known `PeerId`.
///
/// The [`NetworkBehaviour::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.
/// Instructs the swarm to start a dial.
///
/// On success, [`NetworkBehaviour::inject_connection_established`] is invoked.
/// On failure, [`NetworkBehaviour::inject_dial_failure`] is invoked.
Expand All @@ -316,10 +293,11 @@ pub enum NetworkBehaviourAction<
/// # use libp2p::core::PeerId;
/// # use libp2p::plaintext::PlainText2Config;
/// # use libp2p::swarm::{
/// # DialError, DialPeerCondition, IntoProtocolsHandler, KeepAlive, NegotiatedSubstream,
/// # DialError, IntoProtocolsHandler, KeepAlive, NegotiatedSubstream,
/// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ProtocolsHandler,
/// # ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent,
/// # };
/// # use libp2p::swarm::dial_opts::{DialOpts, PeerCondition};
/// # use libp2p::yamux;
/// # use std::collections::VecDeque;
/// # use std::task::{Context, Poll};
Expand Down Expand Up @@ -362,9 +340,10 @@ pub enum NetworkBehaviourAction<
/// # impl MyBehaviour {
/// # fn send(&mut self, peer_id: PeerId, msg: PreciousMessage) {
/// # self.outbox_to_swarm
/// # .push_back(NetworkBehaviourAction::DialPeer {
/// # peer_id,
/// # condition: DialPeerCondition::Always,
/// # .push_back(NetworkBehaviourAction::Dial {
/// # opts: DialOpts::peer_id(peer_id)
/// # .condition(PeerCondition::Always)
/// # .build(),
/// # handler: MyHandler { message: Some(msg) },
/// # });
/// # }
Expand Down Expand Up @@ -476,14 +455,7 @@ pub enum NetworkBehaviourAction<
/// # #[derive(Debug, PartialEq, Eq)]
/// # struct PreciousMessage(String);
/// ```
DialPeer {
/// The peer to try reach.
peer_id: PeerId,
/// The condition for initiating a new dialing attempt.
condition: DialPeerCondition,
/// The handler to be used to handle the connection to the peer.
handler: THandler,
},
Dial { opts: DialOpts, handler: THandler },

/// Instructs the `Swarm` to send an event to the handler dedicated to a
/// connection with a peer.
Expand Down Expand Up @@ -553,18 +525,9 @@ impl<TOutEvent, THandler: IntoProtocolsHandler, TInEventOld>
) -> NetworkBehaviourAction<TOutEvent, THandler, TInEventNew> {
match self {
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
NetworkBehaviourAction::DialAddress { address, handler } => {
NetworkBehaviourAction::DialAddress { address, handler }
NetworkBehaviourAction::Dial { opts, handler } => {
NetworkBehaviourAction::Dial { opts, handler }
}
NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
} => NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
},
NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
Expand Down Expand Up @@ -593,18 +556,9 @@ impl<TOutEvent, THandler: IntoProtocolsHandler> NetworkBehaviourAction<TOutEvent
pub fn map_out<E>(self, f: impl FnOnce(TOutEvent) -> E) -> NetworkBehaviourAction<E, THandler> {
match self {
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(f(e)),
NetworkBehaviourAction::DialAddress { address, handler } => {
NetworkBehaviourAction::DialAddress { address, handler }
NetworkBehaviourAction::Dial { opts, handler } => {
NetworkBehaviourAction::Dial { opts, handler }
}
NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
} => NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
},
NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
Expand Down Expand Up @@ -644,19 +598,8 @@ where
{
match self {
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
NetworkBehaviourAction::DialAddress { address, handler } => {
NetworkBehaviourAction::DialAddress {
address,
handler: f(handler),
}
}
NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
} => NetworkBehaviourAction::DialPeer {
peer_id,
condition,
NetworkBehaviourAction::Dial { opts, handler } => NetworkBehaviourAction::Dial {
opts,
handler: f(handler),
},
NetworkBehaviourAction::NotifyHandler {
Expand Down Expand Up @@ -691,29 +634,6 @@ pub enum NotifyHandler {
Any,
}

/// The available conditions under which a new dialing attempt to
/// a peer is initiated when requested by [`NetworkBehaviourAction::DialPeer`].
#[derive(Debug, Copy, Clone)]
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.
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.
NotDialing,
/// A new dialing attempt is always initiated, only subject to the
/// configured connection limits.
Always,
}

impl Default for DialPeerCondition {
fn default() -> Self {
DialPeerCondition::Disconnected
}
}

/// The options which connections to close.
#[derive(Debug, Clone)]
pub enum CloseConnection {
Expand Down