From 833f2a4d208288db1f84ee66766b913f6c3d418f Mon Sep 17 00:00:00 2001 From: dgarus Date: Thu, 15 Jun 2023 16:49:25 +0300 Subject: [PATCH 1/8] The client's function register returns a result --- examples/rendezvous/src/bin/rzv-identify.rs | 7 ++- examples/rendezvous/src/bin/rzv-register.rs | 7 ++- protocols/rendezvous/src/client.rs | 31 +++++------- protocols/rendezvous/tests/rendezvous.rs | 55 ++++++++++++++++----- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/examples/rendezvous/src/bin/rzv-identify.rs b/examples/rendezvous/src/bin/rzv-identify.rs index 406f15bdcdb..22fef37ecc5 100644 --- a/examples/rendezvous/src/bin/rzv-identify.rs +++ b/examples/rendezvous/src/bin/rzv-identify.rs @@ -78,11 +78,14 @@ async fn main() { SwarmEvent::Behaviour(MyBehaviourEvent::Identify(identify::Event::Received { .. })) => { - swarm.behaviour_mut().rendezvous.register( + if let Err(error) = swarm.behaviour_mut().rendezvous.register( rendezvous::Namespace::from_static("rendezvous"), rendezvous_point, None, - ); + ) { + log::error!("Failed to register {}", error); + return; + } } SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( rendezvous::client::Event::Registered { diff --git a/examples/rendezvous/src/bin/rzv-register.rs b/examples/rendezvous/src/bin/rzv-register.rs index 4cfefe95070..bb6b092eb20 100644 --- a/examples/rendezvous/src/bin/rzv-register.rs +++ b/examples/rendezvous/src/bin/rzv-register.rs @@ -74,11 +74,14 @@ async fn main() { log::error!("Lost connection to rendezvous point {}", error); } SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == rendezvous_point => { - swarm.behaviour_mut().rendezvous.register( + if let Err(error) = swarm.behaviour_mut().rendezvous.register( rendezvous::Namespace::from_static("rendezvous"), rendezvous_point, None, - ); + ) { + log::error!("Failed to register {}", error); + return; + } log::info!("Connection established with rendezvous point {}", peer_id); } // once `/identify` did its job, we know our external address and can register diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 363e31965eb..c9028484ec0 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -31,7 +31,7 @@ use libp2p_swarm::{ ConnectionDenied, ConnectionId, ExternalAddresses, FromSwarm, NetworkBehaviour, PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, }; -use std::collections::{HashMap, VecDeque}; +use std::collections::HashMap; use std::iter; use std::task::{Context, Poll}; use std::time::Duration; @@ -41,8 +41,6 @@ pub struct Behaviour { keypair: Keypair, - error_events: VecDeque, - waiting_for_register: HashMap, waiting_for_discovery: HashMap)>, @@ -66,7 +64,6 @@ impl Behaviour { iter::once((crate::PROTOCOL_IDENT, ProtocolSupport::Outbound)), libp2p_request_response::Config::default(), ), - error_events: Default::default(), keypair, waiting_for_register: Default::default(), waiting_for_discovery: Default::default(), @@ -82,13 +79,15 @@ impl Behaviour { /// /// External addresses are either manually added via [`libp2p_swarm::Swarm::add_external_address`] or reported /// by other [`NetworkBehaviour`]s via [`ToSwarm::ExternalAddrConfirmed`]. - pub fn register(&mut self, namespace: Namespace, rendezvous_node: PeerId, ttl: Option) { + pub fn register( + &mut self, + namespace: Namespace, + rendezvous_node: PeerId, + ttl: Option, + ) -> Result<(), RegisterError> { let external_addresses = self.external_addresses.iter().cloned().collect::>(); if external_addresses.is_empty() { - self.error_events - .push_back(Event::RegisterFailed(RegisterError::NoExternalAddresses)); - - return; + return Err(RegisterError::NoExternalAddresses); } match PeerRecord::new(&self.keypair, external_addresses) { @@ -99,13 +98,11 @@ impl Behaviour { ); self.waiting_for_register .insert(req_id, (rendezvous_node, namespace)); + + Ok(()) } - Err(signing_error) => { - self.error_events.push_back(Event::RegisterFailed( - RegisterError::FailedToMakeRecord(signing_error), - )); - } - }; + Err(signing_error) => Err(RegisterError::FailedToMakeRecord(signing_error)), + } } /// Unregister ourselves from the given namespace with the given rendezvous peer. @@ -239,10 +236,6 @@ impl NetworkBehaviour for Behaviour { ) -> Poll>> { use libp2p_request_response as req_res; - if let Some(event) = self.error_events.pop_front() { - return Poll::Ready(ToSwarm::GenerateEvent(event)); - } - loop { match self.inner.poll(cx, params) { Poll::Ready(ToSwarm::GenerateEvent(req_res::Event::Message { diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index 494f56551d8..7b5ec33a43a 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -22,6 +22,7 @@ use futures::stream::FuturesUnordered; use futures::StreamExt; use libp2p_identity as identity; use libp2p_rendezvous as rendezvous; +use libp2p_rendezvous::client::RegisterError; use libp2p_swarm::{DialError, Swarm, SwarmEvent}; use libp2p_swarm_test::SwarmExt; use std::convert::TryInto; @@ -36,7 +37,8 @@ async fn given_successful_registration_then_successful_discovery() { alice .behaviour_mut() - .register(namespace.clone(), *robert.local_peer_id(), None); + .register(namespace.clone(), *robert.local_peer_id(), None) + .expect("Should send register request"); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -79,6 +81,23 @@ async fn given_successful_registration_then_successful_discovery() { } } +#[tokio::test] +async fn should_return_error_when_no_external_addresses() { + let _ = env_logger::try_init(); + let namespace = rendezvous::Namespace::from_static("some-namespace"); + let server = new_server(rendezvous::server::Config::default()).await; + let mut client = Swarm::new_ephemeral(rendezvous::client::Behaviour::new); + + let res = client + .behaviour_mut() + .register(namespace.clone(), *server.local_peer_id(), None); + + match res { + Err(RegisterError::NoExternalAddresses) => {} + _ => panic!("Should get the RegisterError::NoExternalAddresses"), + } +} + #[tokio::test] async fn given_successful_registration_then_refresh_ttl() { let _ = env_logger::try_init(); @@ -91,7 +110,8 @@ async fn given_successful_registration_then_refresh_ttl() { alice .behaviour_mut() - .register(namespace.clone(), roberts_peer_id, None); + .register(namespace.clone(), roberts_peer_id, None) + .expect("Should send register request"); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -114,7 +134,8 @@ async fn given_successful_registration_then_refresh_ttl() { alice .behaviour_mut() - .register(namespace.clone(), roberts_peer_id, Some(refresh_ttl)); + .register(namespace.clone(), roberts_peer_id, Some(refresh_ttl)) + .expect("Should send register request"); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -150,11 +171,14 @@ async fn given_invalid_ttl_then_unsuccessful_registration() { let ([mut alice], mut robert) = new_server_with_connected_clients(rendezvous::server::Config::default()).await; - alice.behaviour_mut().register( - namespace.clone(), - *robert.local_peer_id(), - Some(100_000_000), - ); + alice + .behaviour_mut() + .register( + namespace.clone(), + *robert.local_peer_id(), + Some(100_000_000), + ) + .expect("Should send register request"); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -182,7 +206,8 @@ async fn discover_allows_for_dial_by_peer_id() { alice .behaviour_mut() - .register(namespace.clone(), roberts_peer_id, None); + .register(namespace.clone(), roberts_peer_id, None) + .expect("Should send register request"); match alice.next_behaviour_event().await { rendezvous::client::Event::Registered { .. } => {} event => panic!("Unexpected event: {event:?}"), @@ -233,7 +258,8 @@ async fn eve_cannot_register() { eve.connect(&mut robert).await; eve.behaviour_mut() - .register(namespace.clone(), *robert.local_peer_id(), None); + .register(namespace.clone(), *robert.local_peer_id(), None) + .expect("Should send register request"); match libp2p_swarm_test::drive(&mut eve, &mut robert).await { ( @@ -263,7 +289,8 @@ async fn can_combine_client_and_server() { charlie .behaviour_mut() .client - .register(namespace.clone(), *robert.local_peer_id(), None); + .register(namespace.clone(), *robert.local_peer_id(), None) + .expect("Should send register request"); match libp2p_swarm_test::drive(&mut charlie, &mut robert).await { ( [CombinedEvent::Client(rendezvous::client::Event::Registered { .. })], @@ -274,7 +301,8 @@ async fn can_combine_client_and_server() { alice .behaviour_mut() - .register(namespace, *charlie.local_peer_id(), None); + .register(namespace, *charlie.local_peer_id(), None) + .expect("Should send register request"); match libp2p_swarm_test::drive(&mut charlie, &mut alice).await { ( [CombinedEvent::Server(rendezvous::server::Event::PeerRegistered { .. })], @@ -299,7 +327,8 @@ async fn registration_on_clients_expire() { alice .behaviour_mut() - .register(namespace.clone(), roberts_peer_id, Some(registration_ttl)); + .register(namespace.clone(), roberts_peer_id, Some(registration_ttl)) + .expect("Should send register request"); match alice.next_behaviour_event().await { rendezvous::client::Event::Registered { .. } => {} event => panic!("Unexpected event: {event:?}"), From 184a0d98e7fc07bbd883ceb27db9b64641a38c70 Mon Sep 17 00:00:00 2001 From: Denis Garus Date: Fri, 16 Jun 2023 10:38:48 +0300 Subject: [PATCH 2/8] Update examples/rendezvous/src/bin/rzv-identify.rs Co-authored-by: Thomas Eizinger --- examples/rendezvous/src/bin/rzv-identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/rendezvous/src/bin/rzv-identify.rs b/examples/rendezvous/src/bin/rzv-identify.rs index 22fef37ecc5..78fa8d8a972 100644 --- a/examples/rendezvous/src/bin/rzv-identify.rs +++ b/examples/rendezvous/src/bin/rzv-identify.rs @@ -83,7 +83,7 @@ async fn main() { rendezvous_point, None, ) { - log::error!("Failed to register {}", error); + log::error!("Failed to register: {error}"); return; } } From eabd9f2139f53b6cd93cc293e8dced9d3ba7f2a5 Mon Sep 17 00:00:00 2001 From: Denis Garus Date: Fri, 16 Jun 2023 10:38:56 +0300 Subject: [PATCH 3/8] Update examples/rendezvous/src/bin/rzv-register.rs Co-authored-by: Thomas Eizinger --- examples/rendezvous/src/bin/rzv-register.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/rendezvous/src/bin/rzv-register.rs b/examples/rendezvous/src/bin/rzv-register.rs index bb6b092eb20..a9c67f4c153 100644 --- a/examples/rendezvous/src/bin/rzv-register.rs +++ b/examples/rendezvous/src/bin/rzv-register.rs @@ -79,7 +79,7 @@ async fn main() { rendezvous_point, None, ) { - log::error!("Failed to register {}", error); + log::error!("Failed to register: {error}"); return; } log::info!("Connection established with rendezvous point {}", peer_id); From 3e40bd8bf345d2fd6c0878e969b6be8c90dfde19 Mon Sep 17 00:00:00 2001 From: dgarus Date: Fri, 16 Jun 2023 10:42:42 +0300 Subject: [PATCH 4/8] Fixed review comments --- protocols/rendezvous/src/client.rs | 22 +++++++++------------- protocols/rendezvous/tests/rendezvous.rs | 18 +++++++++--------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index c9028484ec0..815634bed2f 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -90,19 +90,15 @@ impl Behaviour { return Err(RegisterError::NoExternalAddresses); } - match PeerRecord::new(&self.keypair, external_addresses) { - Ok(peer_record) => { - let req_id = self.inner.send_request( - &rendezvous_node, - Register(NewRegistration::new(namespace.clone(), peer_record, ttl)), - ); - self.waiting_for_register - .insert(req_id, (rendezvous_node, namespace)); - - Ok(()) - } - Err(signing_error) => Err(RegisterError::FailedToMakeRecord(signing_error)), - } + let peer_record = PeerRecord::new(&self.keypair, external_addresses)?; + let req_id = self.inner.send_request( + &rendezvous_node, + Register(NewRegistration::new(namespace.clone(), peer_record, ttl)), + ); + self.waiting_for_register + .insert(req_id, (rendezvous_node, namespace)); + + Ok(()) } /// Unregister ourselves from the given namespace with the given rendezvous peer. diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index 7b5ec33a43a..aed7ca51253 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -38,7 +38,7 @@ async fn given_successful_registration_then_successful_discovery() { alice .behaviour_mut() .register(namespace.clone(), *robert.local_peer_id(), None) - .expect("Should send register request"); + .unwrap(); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -111,7 +111,7 @@ async fn given_successful_registration_then_refresh_ttl() { alice .behaviour_mut() .register(namespace.clone(), roberts_peer_id, None) - .expect("Should send register request"); + .unwrap(); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -135,7 +135,7 @@ async fn given_successful_registration_then_refresh_ttl() { alice .behaviour_mut() .register(namespace.clone(), roberts_peer_id, Some(refresh_ttl)) - .expect("Should send register request"); + .unwrap(); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -178,7 +178,7 @@ async fn given_invalid_ttl_then_unsuccessful_registration() { *robert.local_peer_id(), Some(100_000_000), ) - .expect("Should send register request"); + .unwrap(); match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( @@ -207,7 +207,7 @@ async fn discover_allows_for_dial_by_peer_id() { alice .behaviour_mut() .register(namespace.clone(), roberts_peer_id, None) - .expect("Should send register request"); + .unwrap(); match alice.next_behaviour_event().await { rendezvous::client::Event::Registered { .. } => {} event => panic!("Unexpected event: {event:?}"), @@ -259,7 +259,7 @@ async fn eve_cannot_register() { eve.behaviour_mut() .register(namespace.clone(), *robert.local_peer_id(), None) - .expect("Should send register request"); + .unwrap(); match libp2p_swarm_test::drive(&mut eve, &mut robert).await { ( @@ -290,7 +290,7 @@ async fn can_combine_client_and_server() { .behaviour_mut() .client .register(namespace.clone(), *robert.local_peer_id(), None) - .expect("Should send register request"); + .unwrap(); match libp2p_swarm_test::drive(&mut charlie, &mut robert).await { ( [CombinedEvent::Client(rendezvous::client::Event::Registered { .. })], @@ -302,7 +302,7 @@ async fn can_combine_client_and_server() { alice .behaviour_mut() .register(namespace, *charlie.local_peer_id(), None) - .expect("Should send register request"); + .unwrap(); match libp2p_swarm_test::drive(&mut charlie, &mut alice).await { ( [CombinedEvent::Server(rendezvous::server::Event::PeerRegistered { .. })], @@ -328,7 +328,7 @@ async fn registration_on_clients_expire() { alice .behaviour_mut() .register(namespace.clone(), roberts_peer_id, Some(registration_ttl)) - .expect("Should send register request"); + .unwrap(); match alice.next_behaviour_event().await { rendezvous::client::Event::Registered { .. } => {} event => panic!("Unexpected event: {event:?}"), From e1111398cd00cd8f760db15194ac08dd65b25e30 Mon Sep 17 00:00:00 2001 From: dgarus Date: Fri, 16 Jun 2023 10:49:27 +0300 Subject: [PATCH 5/8] changelog --- protocols/rendezvous/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/protocols/rendezvous/CHANGELOG.md b/protocols/rendezvous/CHANGELOG.md index 8f2bd968e94..0890740788c 100644 --- a/protocols/rendezvous/CHANGELOG.md +++ b/protocols/rendezvous/CHANGELOG.md @@ -1,8 +1,13 @@ ## 0.13.0 - unreleased +- Changed the signature of the function `client::Behavior::register()`, + it returns `Result<(), RegisterError>` now. + See [PR 4073]. + - Raise MSRV to 1.65. See [PR 3715]. +[PR 4073]: https://github.com/libp2p/rust-libp2p/pull/4073 [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 ## 0.12.1 From fcad7702e25986924548c84a2b37ad095aa5f22b Mon Sep 17 00:00:00 2001 From: dgarus Date: Fri, 16 Jun 2023 11:31:38 +0300 Subject: [PATCH 6/8] fixed a typo --- protocols/rendezvous/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/rendezvous/CHANGELOG.md b/protocols/rendezvous/CHANGELOG.md index 0890740788c..322ee2f1539 100644 --- a/protocols/rendezvous/CHANGELOG.md +++ b/protocols/rendezvous/CHANGELOG.md @@ -30,7 +30,7 @@ - Update to `libp2p-swarm` `v0.41.0`. -- Replace `Client` and `Server`'s `NetworkBehaviour` implemention `inject_*` methods with the new `on_*` methods. +- Replace `Client` and `Server`'s `NetworkBehaviour` implementation `inject_*` methods with the new `on_*` methods. See [PR 3011]. - Update `rust-version` to reflect the actual MSRV: 1.62.0. See [PR 3090]. From b0474a82b9120402785ca3ae2d109af430c3c8bc Mon Sep 17 00:00:00 2001 From: dgarus Date: Fri, 16 Jun 2023 12:37:09 +0300 Subject: [PATCH 7/8] Remove the `Remote` variant from `RegisterError` --- examples/rendezvous/src/bin/rzv-identify.rs | 13 +++++++++++-- examples/rendezvous/src/bin/rzv-register.rs | 15 ++++++++++++--- protocols/rendezvous/CHANGELOG.md | 2 ++ protocols/rendezvous/src/client.rs | 20 +++++++++----------- protocols/rendezvous/tests/rendezvous.rs | 18 ++++++++---------- 5 files changed, 42 insertions(+), 26 deletions(-) diff --git a/examples/rendezvous/src/bin/rzv-identify.rs b/examples/rendezvous/src/bin/rzv-identify.rs index 78fa8d8a972..7c326688231 100644 --- a/examples/rendezvous/src/bin/rzv-identify.rs +++ b/examples/rendezvous/src/bin/rzv-identify.rs @@ -102,9 +102,18 @@ async fn main() { ); } SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( - rendezvous::client::Event::RegisterFailed(error), + rendezvous::client::Event::RegisterFailed { + rendezvous_node, + namespace, + error, + }, )) => { - log::error!("Failed to register {}", error); + log::error!( + "Failed to register: rendezvous_node={}, namespace={}, error_code={:?}", + rendezvous_node, + namespace, + error + ); return; } SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event { diff --git a/examples/rendezvous/src/bin/rzv-register.rs b/examples/rendezvous/src/bin/rzv-register.rs index a9c67f4c153..543316a6d9e 100644 --- a/examples/rendezvous/src/bin/rzv-register.rs +++ b/examples/rendezvous/src/bin/rzv-register.rs @@ -100,9 +100,18 @@ async fn main() { ); } SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( - rendezvous::client::Event::RegisterFailed(error), - )) => { - log::error!("Failed to register {}", error); + rendezvous::client::Event::RegisterFailed { + rendezvous_node, + namespace, + error, + }, + )) => { + log::error!( + "Failed to register: rendezvous_node={}, namespace={}, error_code={:?}", + rendezvous_node, + namespace, + error + ); return; } SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event { diff --git a/protocols/rendezvous/CHANGELOG.md b/protocols/rendezvous/CHANGELOG.md index 322ee2f1539..1762f4c62e4 100644 --- a/protocols/rendezvous/CHANGELOG.md +++ b/protocols/rendezvous/CHANGELOG.md @@ -2,6 +2,8 @@ - Changed the signature of the function `client::Behavior::register()`, it returns `Result<(), RegisterError>` now. + Remove the `Remote` variant from `RegisterError` and instead put the information from `Remote` + directly into the variant from the `Event` enum. See [PR 4073]. - Raise MSRV to 1.65. diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 815634bed2f..505635efda8 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -141,12 +141,6 @@ pub enum RegisterError { NoExternalAddresses, #[error("Failed to make a new PeerRecord")] FailedToMakeRecord(#[from] SigningError), - #[error("Failed to register with Rendezvous node")] - Remote { - rendezvous_node: PeerId, - namespace: Namespace, - error: ErrorCode, - }, } #[derive(Debug)] @@ -171,7 +165,11 @@ pub enum Event { namespace: Namespace, }, /// We failed to register with the contained rendezvous node. - RegisterFailed(RegisterError), + RegisterFailed { + rendezvous_node: PeerId, + namespace: Namespace, + error: ErrorCode, + }, /// The connection details we learned from this node expired. Expired { peer: PeerId }, } @@ -326,11 +324,11 @@ impl NetworkBehaviour for Behaviour { impl Behaviour { fn event_for_outbound_failure(&mut self, req_id: &RequestId) -> Option { if let Some((rendezvous_node, namespace)) = self.waiting_for_register.remove(req_id) { - return Some(Event::RegisterFailed(RegisterError::Remote { + return Some(Event::RegisterFailed { rendezvous_node, namespace, error: ErrorCode::Unavailable, - })); + }); }; if let Some((rendezvous_node, namespace)) = self.waiting_for_discovery.remove(req_id) { @@ -363,11 +361,11 @@ impl Behaviour { if let Some((rendezvous_node, namespace)) = self.waiting_for_register.remove(request_id) { - return Some(Event::RegisterFailed(RegisterError::Remote { + return Some(Event::RegisterFailed { rendezvous_node, namespace, error: error_code, - })); + }); } None diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index aed7ca51253..ef0a0ed5938 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -88,14 +88,12 @@ async fn should_return_error_when_no_external_addresses() { let server = new_server(rendezvous::server::Config::default()).await; let mut client = Swarm::new_ephemeral(rendezvous::client::Behaviour::new); - let res = client + let actual = client .behaviour_mut() - .register(namespace.clone(), *server.local_peer_id(), None); + .register(namespace.clone(), *server.local_peer_id(), None) + .unwrap_err(); - match res { - Err(RegisterError::NoExternalAddresses) => {} - _ => panic!("Should get the RegisterError::NoExternalAddresses"), - } + assert!(matches!(actual, RegisterError::NoExternalAddresses)) } #[tokio::test] @@ -182,10 +180,10 @@ async fn given_invalid_ttl_then_unsuccessful_registration() { match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( - [rendezvous::client::Event::RegisterFailed(rendezvous::client::RegisterError::Remote { + [rendezvous::client::Event::RegisterFailed { error, .. - })], + }], [rendezvous::server::Event::PeerNotRegistered { .. }], ) => { assert_eq!(error, rendezvous::ErrorCode::InvalidTtl); @@ -263,10 +261,10 @@ async fn eve_cannot_register() { match libp2p_swarm_test::drive(&mut eve, &mut robert).await { ( - [rendezvous::client::Event::RegisterFailed(rendezvous::client::RegisterError::Remote { + [rendezvous::client::Event::RegisterFailed { error: err_code, .. - })], + }], [rendezvous::server::Event::PeerNotRegistered { .. }], ) => { assert_eq!(err_code, rendezvous::ErrorCode::NotAuthorized); From 554d338c94e2d7065e2997ddb1412a4fb3166459 Mon Sep 17 00:00:00 2001 From: dgarus Date: Fri, 16 Jun 2023 14:06:58 +0300 Subject: [PATCH 8/8] Fixed fmt --- examples/rendezvous/src/bin/rzv-register.rs | 12 ++++++------ protocols/rendezvous/tests/rendezvous.rs | 8 ++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/examples/rendezvous/src/bin/rzv-register.rs b/examples/rendezvous/src/bin/rzv-register.rs index 543316a6d9e..f9fd12b1493 100644 --- a/examples/rendezvous/src/bin/rzv-register.rs +++ b/examples/rendezvous/src/bin/rzv-register.rs @@ -100,12 +100,12 @@ async fn main() { ); } SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous( - rendezvous::client::Event::RegisterFailed { - rendezvous_node, - namespace, - error, - }, - )) => { + rendezvous::client::Event::RegisterFailed { + rendezvous_node, + namespace, + error, + }, + )) => { log::error!( "Failed to register: rendezvous_node={}, namespace={}, error_code={:?}", rendezvous_node, diff --git a/protocols/rendezvous/tests/rendezvous.rs b/protocols/rendezvous/tests/rendezvous.rs index ef0a0ed5938..992876d1971 100644 --- a/protocols/rendezvous/tests/rendezvous.rs +++ b/protocols/rendezvous/tests/rendezvous.rs @@ -180,10 +180,7 @@ async fn given_invalid_ttl_then_unsuccessful_registration() { match libp2p_swarm_test::drive(&mut alice, &mut robert).await { ( - [rendezvous::client::Event::RegisterFailed { - error, - .. - }], + [rendezvous::client::Event::RegisterFailed { error, .. }], [rendezvous::server::Event::PeerNotRegistered { .. }], ) => { assert_eq!(error, rendezvous::ErrorCode::InvalidTtl); @@ -262,8 +259,7 @@ async fn eve_cannot_register() { match libp2p_swarm_test::drive(&mut eve, &mut robert).await { ( [rendezvous::client::Event::RegisterFailed { - error: err_code, - .. + error: err_code, .. }], [rendezvous::server::Event::PeerNotRegistered { .. }], ) => {