refactor(swarm)!: remove handler from NetworkBehaviourAction::Dial (#3328)

We create the `ConnectionId` for the new connection as part of `DialOpts`. This allows `NetworkBehaviour`s to accurately track state regarding their own dial attempts.

This patch is the main enabler of https://github.com/libp2p/rust-libp2p/pull/3254. Removing the `handler` field will allow us to deprecate the `NetworkBehaviour::new_handler` function in favor of four new ones that give more control over the connection lifecycle.
This commit is contained in:
Thomas Eizinger
2023-02-14 14:09:29 +13:00
committed by GitHub
parent 9247cfa878
commit caed1fe2c7
31 changed files with 584 additions and 967 deletions

View File

@ -19,6 +19,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::ConnectionId;
use libp2p_core::connection::Endpoint;
use libp2p_core::multiaddr::Protocol;
use libp2p_core::multihash::Multihash;
@ -43,6 +44,7 @@ pub struct DialOpts {
extend_addresses_through_behaviour: bool,
role_override: Endpoint,
dial_concurrency_factor_override: Option<NonZeroU8>,
connection_id: ConnectionId,
}
impl DialOpts {
@ -83,6 +85,14 @@ impl DialOpts {
self.peer_id
}
/// Get the [`ConnectionId`] of this dial attempt.
///
/// All future events of this dial will be associated with this ID.
/// See [`DialFailure`](crate::DialFailure) and [`ConnectionEstablished`](crate::behaviour::ConnectionEstablished).
pub fn connection_id(&self) -> ConnectionId {
self.connection_id
}
/// Retrieves the [`PeerId`] from the [`DialOpts`] if specified or otherwise tries to parse it
/// from the multihash in the `/p2p` part of the address, if present.
///
@ -207,6 +217,7 @@ impl WithPeerId {
extend_addresses_through_behaviour: true,
role_override: self.role_override,
dial_concurrency_factor_override: self.dial_concurrency_factor_override,
connection_id: ConnectionId::next(),
}
}
}
@ -262,6 +273,7 @@ impl WithPeerIdWithAddresses {
extend_addresses_through_behaviour: self.extend_addresses_through_behaviour,
role_override: self.role_override,
dial_concurrency_factor_override: self.dial_concurrency_factor_override,
connection_id: ConnectionId::next(),
}
}
}
@ -305,6 +317,7 @@ impl WithoutPeerIdWithAddress {
extend_addresses_through_behaviour: false,
role_override: self.role_override,
dial_concurrency_factor_override: None,
connection_id: ConnectionId::next(),
}
}
}