refactor(swarm): simplify DialOpts (#3335)

Instead of tracking an inner enum, move all fields of `Opts` into `DialOpts`, setting them directly once we construct them. This makes the getters a lot simpler to implement and reduces code duplication.
This commit is contained in:
Thomas Eizinger
2023-01-20 10:30:09 +11:00
committed by GitHub
parent b5a3f81c3d
commit d82c2a1a7a

View File

@ -36,7 +36,14 @@ use std::num::NonZeroU8;
/// ///
/// - [`DialOpts::unknown_peer_id`] dialing an unknown peer /// - [`DialOpts::unknown_peer_id`] dialing an unknown peer
#[derive(Debug)] #[derive(Debug)]
pub struct DialOpts(pub(super) Opts); pub struct DialOpts {
peer_id: Option<PeerId>,
condition: PeerCondition,
addresses: Vec<Multiaddr>,
extend_addresses_through_behaviour: bool,
role_override: Endpoint,
dial_concurrency_factor_override: Option<NonZeroU8>,
}
impl DialOpts { impl DialOpts {
/// Dial a known peer. /// Dial a known peer.
@ -73,13 +80,7 @@ impl DialOpts {
/// Get the [`PeerId`] specified in a [`DialOpts`] if any. /// Get the [`PeerId`] specified in a [`DialOpts`] if any.
pub fn get_peer_id(&self) -> Option<PeerId> { pub fn get_peer_id(&self) -> Option<PeerId> {
match self { self.peer_id
DialOpts(Opts::WithPeerId(WithPeerId { peer_id, .. })) => Some(*peer_id),
DialOpts(Opts::WithPeerIdWithAddresses(WithPeerIdWithAddresses {
peer_id, ..
})) => Some(*peer_id),
DialOpts(Opts::WithoutPeerIdWithAddress(_)) => None,
}
} }
/// Retrieves the [`PeerId`] from the [`DialOpts`] if specified or otherwise tries to parse it /// Retrieves the [`PeerId`] from the [`DialOpts`] if specified or otherwise tries to parse it
@ -92,15 +93,16 @@ impl DialOpts {
/// ///
/// See <https://github.com/multiformats/rust-multiaddr/issues/73>. /// See <https://github.com/multiformats/rust-multiaddr/issues/73>.
pub(crate) fn get_or_parse_peer_id(&self) -> Result<Option<PeerId>, Multihash> { pub(crate) fn get_or_parse_peer_id(&self) -> Result<Option<PeerId>, Multihash> {
match self { if let Some(peer_id) = self.peer_id {
DialOpts(Opts::WithPeerId(WithPeerId { peer_id, .. })) => Ok(Some(*peer_id)), return Ok(Some(peer_id));
DialOpts(Opts::WithPeerIdWithAddresses(WithPeerIdWithAddresses { }
peer_id, ..
})) => Ok(Some(*peer_id)), let first_address = match self.addresses.first() {
DialOpts(Opts::WithoutPeerIdWithAddress(WithoutPeerIdWithAddress { Some(first_address) => first_address,
address, .. None => return Ok(None),
})) => { };
let peer_id = address
let maybe_peer_id = first_address
.iter() .iter()
.last() .last()
.and_then(|p| { .and_then(|p| {
@ -112,72 +114,27 @@ impl DialOpts {
}) })
.transpose()?; .transpose()?;
Ok(peer_id) Ok(maybe_peer_id)
}
}
} }
pub(crate) fn get_addresses(&self) -> Vec<Multiaddr> { pub(crate) fn get_addresses(&self) -> Vec<Multiaddr> {
match self { self.addresses.clone()
DialOpts(Opts::WithPeerId(WithPeerId { .. })) => vec![],
DialOpts(Opts::WithPeerIdWithAddresses(WithPeerIdWithAddresses {
addresses, ..
})) => addresses.clone(),
DialOpts(Opts::WithoutPeerIdWithAddress(WithoutPeerIdWithAddress {
address, ..
})) => vec![address.clone()],
}
} }
pub(crate) fn extend_addresses_through_behaviour(&self) -> bool { pub(crate) fn extend_addresses_through_behaviour(&self) -> bool {
match self { self.extend_addresses_through_behaviour
DialOpts(Opts::WithPeerId(WithPeerId { .. })) => true,
DialOpts(Opts::WithPeerIdWithAddresses(WithPeerIdWithAddresses {
extend_addresses_through_behaviour,
..
})) => *extend_addresses_through_behaviour,
DialOpts(Opts::WithoutPeerIdWithAddress(WithoutPeerIdWithAddress { .. })) => true,
}
} }
pub(crate) fn peer_condition(&self) -> PeerCondition { pub(crate) fn peer_condition(&self) -> PeerCondition {
match self { self.condition
DialOpts(
Opts::WithPeerId(WithPeerId { condition, .. })
| Opts::WithPeerIdWithAddresses(WithPeerIdWithAddresses { condition, .. }),
) => *condition,
DialOpts(Opts::WithoutPeerIdWithAddress(WithoutPeerIdWithAddress { .. })) => {
PeerCondition::Always
}
}
} }
pub(crate) fn dial_concurrency_override(&self) -> Option<NonZeroU8> { pub(crate) fn dial_concurrency_override(&self) -> Option<NonZeroU8> {
match self { self.dial_concurrency_factor_override
DialOpts(Opts::WithPeerId(WithPeerId {
dial_concurrency_factor_override,
..
})) => *dial_concurrency_factor_override,
DialOpts(Opts::WithPeerIdWithAddresses(WithPeerIdWithAddresses {
dial_concurrency_factor_override,
..
})) => *dial_concurrency_factor_override,
DialOpts(Opts::WithoutPeerIdWithAddress(WithoutPeerIdWithAddress { .. })) => None,
}
} }
pub(crate) fn role_override(&self) -> Endpoint { pub(crate) fn role_override(&self) -> Endpoint {
match self { self.role_override
DialOpts(Opts::WithPeerId(WithPeerId { role_override, .. })) => *role_override,
DialOpts(Opts::WithPeerIdWithAddresses(WithPeerIdWithAddresses {
role_override,
..
})) => *role_override,
DialOpts(Opts::WithoutPeerIdWithAddress(WithoutPeerIdWithAddress {
role_override,
..
})) => *role_override,
}
} }
} }
@ -193,25 +150,12 @@ impl From<PeerId> for DialOpts {
} }
} }
/// Internal options type.
///
/// Not to be constructed manually. Use either of the below instead:
///
/// - [`DialOpts::peer_id`] dialing a known peer
/// - [`DialOpts::unknown_peer_id`] dialing an unknown peer
#[derive(Debug)]
pub(super) enum Opts {
WithPeerId(WithPeerId),
WithPeerIdWithAddresses(WithPeerIdWithAddresses),
WithoutPeerIdWithAddress(WithoutPeerIdWithAddress),
}
#[derive(Debug)] #[derive(Debug)]
pub struct WithPeerId { pub struct WithPeerId {
pub(crate) peer_id: PeerId, peer_id: PeerId,
pub(crate) condition: PeerCondition, condition: PeerCondition,
pub(crate) role_override: Endpoint, role_override: Endpoint,
pub(crate) dial_concurrency_factor_override: Option<NonZeroU8>, dial_concurrency_factor_override: Option<NonZeroU8>,
} }
impl WithPeerId { impl WithPeerId {
@ -256,18 +200,25 @@ impl WithPeerId {
/// Addresses to dial the peer are retrieved via /// Addresses to dial the peer are retrieved via
/// [`NetworkBehaviour::addresses_of_peer`](crate::behaviour::NetworkBehaviour::addresses_of_peer). /// [`NetworkBehaviour::addresses_of_peer`](crate::behaviour::NetworkBehaviour::addresses_of_peer).
pub fn build(self) -> DialOpts { pub fn build(self) -> DialOpts {
DialOpts(Opts::WithPeerId(self)) DialOpts {
peer_id: Some(self.peer_id),
condition: self.condition,
addresses: vec![],
extend_addresses_through_behaviour: true,
role_override: self.role_override,
dial_concurrency_factor_override: self.dial_concurrency_factor_override,
}
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct WithPeerIdWithAddresses { pub struct WithPeerIdWithAddresses {
pub(crate) peer_id: PeerId, peer_id: PeerId,
pub(crate) condition: PeerCondition, condition: PeerCondition,
pub(crate) addresses: Vec<Multiaddr>, addresses: Vec<Multiaddr>,
pub(crate) extend_addresses_through_behaviour: bool, extend_addresses_through_behaviour: bool,
pub(crate) role_override: Endpoint, role_override: Endpoint,
pub(crate) dial_concurrency_factor_override: Option<NonZeroU8>, dial_concurrency_factor_override: Option<NonZeroU8>,
} }
impl WithPeerIdWithAddresses { impl WithPeerIdWithAddresses {
@ -304,7 +255,14 @@ impl WithPeerIdWithAddresses {
/// Build the final [`DialOpts`]. /// Build the final [`DialOpts`].
pub fn build(self) -> DialOpts { pub fn build(self) -> DialOpts {
DialOpts(Opts::WithPeerIdWithAddresses(self)) DialOpts {
peer_id: Some(self.peer_id),
condition: self.condition,
addresses: self.addresses,
extend_addresses_through_behaviour: self.extend_addresses_through_behaviour,
role_override: self.role_override,
dial_concurrency_factor_override: self.dial_concurrency_factor_override,
}
} }
} }
@ -323,8 +281,8 @@ impl WithoutPeerId {
#[derive(Debug)] #[derive(Debug)]
pub struct WithoutPeerIdWithAddress { pub struct WithoutPeerIdWithAddress {
pub(crate) address: Multiaddr, address: Multiaddr,
pub(crate) role_override: Endpoint, role_override: Endpoint,
} }
impl WithoutPeerIdWithAddress { impl WithoutPeerIdWithAddress {
@ -340,7 +298,14 @@ impl WithoutPeerIdWithAddress {
} }
/// Build the final [`DialOpts`]. /// Build the final [`DialOpts`].
pub fn build(self) -> DialOpts { pub fn build(self) -> DialOpts {
DialOpts(Opts::WithoutPeerIdWithAddress(self)) DialOpts {
peer_id: None,
condition: PeerCondition::Always,
addresses: vec![self.address],
extend_addresses_through_behaviour: false,
role_override: self.role_override,
dial_concurrency_factor_override: None,
}
} }
} }