diff --git a/protocols/identify/CHANGELOG.md b/protocols/identify/CHANGELOG.md index aa66a7af8b6..50b264e6f56 100644 --- a/protocols/identify/CHANGELOG.md +++ b/protocols/identify/CHANGELOG.md @@ -4,6 +4,8 @@ - Update to `libp2p-swarm` `v0.37.0`. +- Extend log message on second identify push stream with peer ID. + # 0.36.1 - Allow at most one inbound identify push stream. diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 461e05a31e8..af4d7f8cba3 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -27,20 +27,48 @@ use futures::prelude::*; use futures_timer::Delay; use libp2p_core::either::{EitherError, EitherOutput}; use libp2p_core::upgrade::{EitherUpgrade, InboundUpgrade, OutboundUpgrade, SelectUpgrade}; +use libp2p_core::{ConnectedPoint, PeerId}; use libp2p_swarm::{ - ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, - NegotiatedSubstream, SubstreamProtocol, + ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler, + KeepAlive, NegotiatedSubstream, SubstreamProtocol, }; use log::warn; use smallvec::SmallVec; use std::{io, pin::Pin, task::Context, task::Poll, time::Duration}; +pub struct IdentifyHandlerProto { + initial_delay: Duration, + interval: Duration, +} + +impl IdentifyHandlerProto { + pub fn new(initial_delay: Duration, interval: Duration) -> Self { + IdentifyHandlerProto { + initial_delay, + interval, + } + } +} + +impl IntoConnectionHandler for IdentifyHandlerProto { + type Handler = IdentifyHandler; + + fn into_handler(self, remote_peer_id: &PeerId, _endpoint: &ConnectedPoint) -> Self::Handler { + IdentifyHandler::new(self.initial_delay, self.interval, *remote_peer_id) + } + + fn inbound_protocol(&self) -> ::InboundProtocol { + SelectUpgrade::new(IdentifyProtocol, IdentifyPushProtocol::inbound()) + } +} + /// Protocol handler for sending and receiving identification requests. /// /// Outbound requests are sent periodically. The handler performs expects /// at least one identification request to be answered by the remote before /// permitting the underlying connection to be closed. pub struct IdentifyHandler { + remote_peer_id: PeerId, inbound_identify_push: Option>>, /// Pending events to yield. events: SmallVec< @@ -81,8 +109,9 @@ pub struct IdentifyPush(pub IdentifyInfo); impl IdentifyHandler { /// Creates a new `IdentifyHandler`. - pub fn new(initial_delay: Duration, interval: Duration) -> Self { + pub fn new(initial_delay: Duration, interval: Duration, remote_peer_id: PeerId) -> Self { IdentifyHandler { + remote_peer_id, inbound_identify_push: Default::default(), events: SmallVec::new(), trigger_next_identify: Delay::new(initial_delay), @@ -120,8 +149,9 @@ impl ConnectionHandler for IdentifyHandler { EitherOutput::Second(fut) => { if self.inbound_identify_push.replace(fut).is_some() { warn!( - "New inbound identify push stream while still upgrading previous one. \ - Replacing previous with new.", + "New inbound identify push stream from {} while still \ + upgrading previous one. Replacing previous with new.", + self.remote_peer_id, ); } } diff --git a/protocols/identify/src/identify.rs b/protocols/identify/src/identify.rs index 5f5e468365f..f2d5ffd641d 100644 --- a/protocols/identify/src/identify.rs +++ b/protocols/identify/src/identify.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::handler::{IdentifyHandler, IdentifyHandlerEvent, IdentifyPush}; +use crate::handler::{IdentifyHandlerEvent, IdentifyHandlerProto, IdentifyPush}; use crate::protocol::{IdentifyInfo, ReplySubstream, UpgradeError}; use futures::prelude::*; use libp2p_core::{ @@ -54,7 +54,7 @@ pub struct Identify { /// Pending replies to send. pending_replies: VecDeque, /// Pending events to be emitted when polled. - events: VecDeque>, + events: VecDeque>, /// Peers to which an active push with current information about /// the local peer should be sent. pending_push: HashSet, @@ -208,11 +208,11 @@ impl Identify { } impl NetworkBehaviour for Identify { - type ConnectionHandler = IdentifyHandler; + type ConnectionHandler = IdentifyHandlerProto; type OutEvent = IdentifyEvent; fn new_handler(&mut self) -> Self::ConnectionHandler { - IdentifyHandler::new(self.config.initial_delay, self.config.interval) + IdentifyHandlerProto::new(self.config.initial_delay, self.config.interval) } fn inject_connection_established( @@ -296,7 +296,7 @@ impl NetworkBehaviour for Identify { &mut self, peer_id: PeerId, connection: ConnectionId, - event: ::OutEvent, + event: <::Handler as ConnectionHandler>::OutEvent, ) { match event { IdentifyHandlerEvent::Identified(mut info) => {