From a7ae23ccb79d557bdb013d03753bff47254811fb Mon Sep 17 00:00:00 2001 From: Dave Huseby Date: Wed, 16 Aug 2023 20:34:31 -0600 Subject: [PATCH 1/4] added behavior event when local protocols change Signed-off-by: Dave Huseby --- protocols/identify/src/behaviour.rs | 13 ++++++++++- protocols/identify/src/handler.rs | 35 ++++++++++++++++++----------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index f572b937d38..a7eb68ae9b0 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -26,7 +26,7 @@ use libp2p_identity::PublicKey; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::{ ConnectionDenied, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, - NotifyHandler, PollParameters, StreamUpgradeError, THandlerInEvent, ToSwarm, + NotifyHandler, PollParameters, StreamProtocol, StreamUpgradeError, THandlerInEvent, ToSwarm, }; use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent}; use lru::LruCache; @@ -309,6 +309,10 @@ impl NetworkBehaviour for Behaviour { self.events .push_back(ToSwarm::GenerateEvent(Event::Error { peer_id, error })); } + handler::Event::LocalProtocolsChanged(protocols) => { + self.events + .push_back(ToSwarm::GenerateEvent(Event::LocalProtocolsChanged { peer_id, protocols })); + } } } @@ -439,6 +443,13 @@ pub enum Event { /// The error that occurred. error: StreamUpgradeError, }, + /// Local protocols have changed. + LocalProtocolsChanged { + /// The peer we are advertising protocols to. + peer_id: PeerId, + /// The list of local supported protocols we are advertising to the peer. + protocols: Vec, + }, } /// If there is a given peer_id in the multiaddr, make sure it is the same as diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 5a1712e8c3d..a6a99194e6f 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -104,6 +104,8 @@ pub enum Event { IdentificationPushed, /// Failed to identify the remote, or to reply to an identification request. IdentificationError(StreamUpgradeError), + /// Local supported protocols changed + LocalProtocolsChanged(Vec), } impl Handler { @@ -370,20 +372,27 @@ impl ConnectionHandler for Handler { .then(|| self.local_protocols_to_string()) .unwrap_or_default(); - if protocols_changed && self.exchanged_one_periodic_identify { - log::debug!( - "Supported listen protocols changed from [{before}] to [{after}], pushing to {}", - self.remote_peer_id - ); - - let info = self.build_info(); + if protocols_changed { self.events - .push(ConnectionHandlerEvent::OutboundSubstreamRequest { - protocol: SubstreamProtocol::new( - Either::Right(Push::outbound(info)), - (), - ), - }); + .push(ConnectionHandlerEvent::NotifyBehaviour(Event::LocalProtocolsChanged( + Vec::from_iter(self.local_supported_protocols.iter().cloned()) + ))); + + if self.exchanged_one_periodic_identify { + log::debug!( + "Supported listen protocols changed from [{before}] to [{after}], pushing to {}", + self.remote_peer_id + ); + + let info = self.build_info(); + self.events + .push(ConnectionHandlerEvent::OutboundSubstreamRequest { + protocol: SubstreamProtocol::new( + Either::Right(Push::outbound(info)), + (), + ), + }); + } } } } From 4a0012b8489ea8fcd033cc513297bcc6f1b7cab2 Mon Sep 17 00:00:00 2001 From: Dave Huseby Date: Thu, 17 Aug 2023 18:28:30 -0600 Subject: [PATCH 2/4] feat!: adding Info to pushed event to detect when local protocols change Signed-off-by: Dave Huseby --- protocols/identify/src/behaviour.rs | 19 ++++-------- protocols/identify/src/handler.rs | 46 +++++++++++++---------------- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index a7eb68ae9b0..b5509fc63c9 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -26,7 +26,7 @@ use libp2p_identity::PublicKey; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::{ ConnectionDenied, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, - NotifyHandler, PollParameters, StreamProtocol, StreamUpgradeError, THandlerInEvent, ToSwarm, + NotifyHandler, PollParameters, StreamUpgradeError, THandlerInEvent, ToSwarm, }; use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent}; use lru::LruCache; @@ -301,18 +301,14 @@ impl NetworkBehaviour for Behaviour { self.events .push_back(ToSwarm::GenerateEvent(Event::Sent { peer_id })); } - handler::Event::IdentificationPushed => { + handler::Event::IdentificationPushed(info) => { self.events - .push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id })); + .push_back(ToSwarm::GenerateEvent(Event::Pushed { peer_id, info })); } handler::Event::IdentificationError(error) => { self.events .push_back(ToSwarm::GenerateEvent(Event::Error { peer_id, error })); } - handler::Event::LocalProtocolsChanged(protocols) => { - self.events - .push_back(ToSwarm::GenerateEvent(Event::LocalProtocolsChanged { peer_id, protocols })); - } } } @@ -435,6 +431,8 @@ pub enum Event { Pushed { /// The peer that the information has been sent to. peer_id: PeerId, + /// The information pushed to the peer. + info: Info, }, /// Error while attempting to identify the remote. Error { @@ -443,13 +441,6 @@ pub enum Event { /// The error that occurred. error: StreamUpgradeError, }, - /// Local protocols have changed. - LocalProtocolsChanged { - /// The peer we are advertising protocols to. - peer_id: PeerId, - /// The list of local supported protocols we are advertising to the peer. - protocols: Vec, - }, } /// If there is a given peer_id in the multiaddr, make sure it is the same as diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index a6a99194e6f..cd04e75c53b 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -101,11 +101,9 @@ pub enum Event { /// We replied to an identification request from the remote. Identification, /// We actively pushed our identification information to the remote. - IdentificationPushed, + IdentificationPushed(Info), /// Failed to identify the remote, or to reply to an identification request. IdentificationError(StreamUpgradeError), - /// Local supported protocols changed - LocalProtocolsChanged(Vec), } impl Handler { @@ -184,9 +182,12 @@ impl Handler { remote_info, ))); } - future::Either::Right(()) => self.events.push(ConnectionHandlerEvent::NotifyBehaviour( - Event::IdentificationPushed, - )), + future::Either::Right(()) => { + let local_info = self.build_info(); + self.events.push(ConnectionHandlerEvent::NotifyBehaviour( + Event::IdentificationPushed(local_info), + )); + } } } @@ -372,27 +373,20 @@ impl ConnectionHandler for Handler { .then(|| self.local_protocols_to_string()) .unwrap_or_default(); - if protocols_changed { + if protocols_changed && self.exchanged_one_periodic_identify { + log::debug!( + "Supported listen protocols changed from [{before}] to [{after}], pushing to {}", + self.remote_peer_id + ); + + let info = self.build_info(); self.events - .push(ConnectionHandlerEvent::NotifyBehaviour(Event::LocalProtocolsChanged( - Vec::from_iter(self.local_supported_protocols.iter().cloned()) - ))); - - if self.exchanged_one_periodic_identify { - log::debug!( - "Supported listen protocols changed from [{before}] to [{after}], pushing to {}", - self.remote_peer_id - ); - - let info = self.build_info(); - self.events - .push(ConnectionHandlerEvent::OutboundSubstreamRequest { - protocol: SubstreamProtocol::new( - Either::Right(Push::outbound(info)), - (), - ), - }); - } + .push(ConnectionHandlerEvent::OutboundSubstreamRequest { + protocol: SubstreamProtocol::new( + Either::Right(Push::outbound(info)), + (), + ), + }); } } } From d0960761e16f79647ab1f6b9b3d49a17dfd24575 Mon Sep 17 00:00:00 2001 From: Dave Huseby Date: Thu, 17 Aug 2023 19:35:05 -0600 Subject: [PATCH 3/4] bumped crate version and started CHANGELIG entry Signed-off-by: Dave Huseby --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/identify/CHANGELOG.md | 6 +++++- protocols/identify/Cargo.toml | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 459afe3ece5..42d76710150 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2760,7 +2760,7 @@ dependencies = [ [[package]] name = "libp2p-identify" -version = "0.43.0" +version = "0.44.0" dependencies = [ "async-std", "asynchronous-codec", diff --git a/Cargo.toml b/Cargo.toml index 03b3833033b..d7936d5bca4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,7 +72,7 @@ libp2p-deflate = { version = "0.40.0", path = "transports/deflate" } libp2p-dns = { version = "0.40.0", path = "transports/dns" } libp2p-floodsub = { version = "0.43.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.45.1", path = "protocols/gossipsub" } -libp2p-identify = { version = "0.43.0", path = "protocols/identify" } +libp2p-identify = { version = "0.44.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.2" } libp2p-kad = { version = "0.44.4", path = "protocols/kad" } libp2p-mdns = { version = "0.44.0", path = "protocols/mdns" } diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index 79632954718..f67587dab04 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,4 +1,8 @@ -## 0.43.0 +## 0.44.0 + +- Added `Info` to the `libp2p-identify::Event::Pushed` to report what was pushed. + +## 0.43.0 - Observed addresses (aka. external address candidates) of the local node, reported by a remote node via `libp2p-identify`, are no longer automatically considered confirmed external addresses, in other words they are no longer trusted by default. Instead users need to confirm the reported observed address either manually, or by using `libp2p-autonat`. diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index fb207dd7e8a..4db3f328333 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-identify" edition = "2021" rust-version = { workspace = true } description = "Nodes identifcation protocol for libp2p" -version = "0.43.0" +version = "0.44.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From cc02b21e12f23ee39f11718c9b1ecbec2a2cfee1 Mon Sep 17 00:00:00 2001 From: Dave Huseby Date: Fri, 18 Aug 2023 17:51:41 -0600 Subject: [PATCH 4/4] Update protocols/identify/CHANGELOG.md Co-authored-by: Thomas Eizinger --- protocols/identify/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index f67587dab04..0914fae6b74 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.44.0 +## 0.44.0 - unreleased - Added `Info` to the `libp2p-identify::Event::Pushed` to report what was pushed.