Skip to content

Commit 57013bb

Browse files
committed
review: separate Info details provided by the behaviour,
provided on new_handler constructor Everything that doesn't change over the lifetime.
1 parent 11a9254 commit 57013bb

File tree

2 files changed

+99
-46
lines changed

2 files changed

+99
-46
lines changed

protocols/identify/src/behaviour.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::handler::{self, InEvent, Proto};
21+
use crate::handler::{self, BehaviourInfo, InEvent, Proto};
2222
use crate::protocol::{Info, UpgradeError};
2323
use libp2p_core::{
2424
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Multiaddr, PeerId, PublicKey,
@@ -48,8 +48,8 @@ pub struct Behaviour {
4848
config: Config,
4949
/// For each peer we're connected to, the observed address to send back to it.
5050
connected: HashMap<PeerId, HashMap<ConnectionId, Multiaddr>>,
51-
/// Pending requests to respond.
52-
requests: VecDeque<Request>,
51+
/// Information requests from the handlers to be fullfiled.
52+
requests: VecDeque<PeerId>,
5353
/// Pending events to be emitted when polled.
5454
events: VecDeque<NetworkBehaviourAction<Event, Proto>>,
5555
/// Peers to which an active push with current information about
@@ -59,12 +59,6 @@ pub struct Behaviour {
5959
discovered_peers: PeerCache,
6060
}
6161

62-
/// An inbound identification request.
63-
struct Request {
64-
peer: PeerId,
65-
observed: Multiaddr,
66-
}
67-
6862
/// Configuration for the [`identify::Behaviour`](Behaviour).
6963
#[non_exhaustive]
7064
#[derive(Debug, Clone)]
@@ -228,13 +222,19 @@ impl NetworkBehaviour for Behaviour {
228222
type OutEvent = Event;
229223

230224
fn new_handler(&mut self) -> Self::ConnectionHandler {
231-
Proto::new(self.config.initial_delay, self.config.interval)
225+
Proto::new(
226+
self.config.initial_delay,
227+
self.config.interval,
228+
self.config.local_public_key.clone(),
229+
self.config.protocol_version.clone(),
230+
self.config.agent_version.clone(),
231+
)
232232
}
233233

234234
fn on_connection_handler_event(
235235
&mut self,
236236
peer_id: PeerId,
237-
connection: ConnectionId,
237+
_connection: ConnectionId,
238238
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
239239
) {
240240
match event {
@@ -272,19 +272,7 @@ impl NetworkBehaviour for Behaviour {
272272
}));
273273
}
274274
handler::Event::Identify => {
275-
let observed = self
276-
.connected
277-
.get(&peer_id)
278-
.and_then(|addrs| addrs.get(&connection))
279-
.expect(
280-
"`on_connection_handler_event` is only called \
281-
with an established connection and calling `NetworkBehaviour::on_event` \
282-
with `FromSwarm::ConnectionEstablished ensures there is an entry; qed",
283-
);
284-
self.requests.push_back(Request {
285-
peer: peer_id,
286-
observed: observed.clone(),
287-
});
275+
self.requests.push_back(peer_id);
288276
}
289277
handler::Event::IdentificationError(error) => {
290278
self.events
@@ -339,15 +327,11 @@ impl NetworkBehaviour for Behaviour {
339327
});
340328
}
341329

342-
// Check for pending requests to send back to the handler for reply.
343-
if let Some(Request { peer, observed }) = self.requests.pop_front() {
344-
let info = Info {
330+
// Check for information requests from the handlers.
331+
if let Some(peer) = self.requests.pop_front() {
332+
let info = BehaviourInfo {
345333
listen_addrs: listen_addrs(params),
346334
protocols: supported_protocols(params),
347-
public_key: self.config.local_public_key.clone(),
348-
protocol_version: self.config.protocol_version.clone(),
349-
agent_version: self.config.agent_version.clone(),
350-
observed_addr: observed,
351335
};
352336
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
353337
peer_id: peer,

protocols/identify/src/handler.rs

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use futures::prelude::*;
2626
use futures_timer::Delay;
2727
use libp2p_core::either::{EitherError, EitherOutput};
2828
use libp2p_core::upgrade::{EitherUpgrade, SelectUpgrade};
29-
use libp2p_core::{ConnectedPoint, PeerId};
29+
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, PublicKey};
3030
use libp2p_swarm::handler::{
3131
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
3232
};
@@ -42,22 +42,47 @@ use std::{io, pin::Pin, task::Context, task::Poll, time::Duration};
4242
pub struct Proto {
4343
initial_delay: Duration,
4444
interval: Duration,
45+
public_key: PublicKey,
46+
protocol_version: String,
47+
agent_version: String,
4548
}
4649

4750
impl Proto {
48-
pub fn new(initial_delay: Duration, interval: Duration) -> Self {
51+
pub fn new(
52+
initial_delay: Duration,
53+
interval: Duration,
54+
public_key: PublicKey,
55+
protocol_version: String,
56+
agent_version: String,
57+
) -> Self {
4958
Proto {
5059
initial_delay,
5160
interval,
61+
public_key,
62+
protocol_version,
63+
agent_version,
5264
}
5365
}
5466
}
5567

5668
impl IntoConnectionHandler for Proto {
5769
type Handler = Handler;
5870

59-
fn into_handler(self, remote_peer_id: &PeerId, _endpoint: &ConnectedPoint) -> Self::Handler {
60-
Handler::new(self.initial_delay, self.interval, *remote_peer_id)
71+
fn into_handler(self, remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
72+
let observed_addr = match endpoint {
73+
ConnectedPoint::Dialer { address, .. } => address,
74+
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr,
75+
};
76+
77+
Handler::new(
78+
self.initial_delay,
79+
self.interval,
80+
*remote_peer_id,
81+
self.public_key,
82+
self.protocol_version,
83+
self.agent_version,
84+
observed_addr.clone(),
85+
)
6186
}
6287

6388
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
@@ -94,9 +119,6 @@ pub struct Handler {
94119
>; 4],
95120
>,
96121

97-
/// Identify request information.
98-
info: Option<Info>,
99-
100122
/// Pending replies to send.
101123
pending_replies: VecDeque<Pending>,
102124

@@ -108,6 +130,33 @@ pub struct Handler {
108130

109131
/// The interval of `trigger_next_identify`, i.e. the recurrent delay.
110132
interval: Duration,
133+
134+
/// The public key of the local peer.
135+
public_key: PublicKey,
136+
137+
/// Application-specific version of the protocol family used by the peer,
138+
/// e.g. `ipfs/1.0.0` or `polkadot/1.0.0`.
139+
protocol_version: String,
140+
141+
/// Name and version of the peer, similar to the `User-Agent` header in
142+
/// the HTTP protocol.
143+
agent_version: String,
144+
145+
/// Address observed by or for the remote.
146+
observed_addr: Multiaddr,
147+
148+
/// Information provided by the `Behaviour` upon requesting.
149+
behaviour_info: Option<BehaviourInfo>,
150+
}
151+
152+
/// Information provided by the `Behaviour` upon requesting.
153+
#[derive(Debug)]
154+
pub struct BehaviourInfo {
155+
/// The addresses that the peer is listening on.
156+
pub listen_addrs: Vec<Multiaddr>,
157+
158+
/// The list of protocols supported by the peer, e.g. `/ipfs/ping/1.0.0`.
159+
pub protocols: Vec<String>,
111160
}
112161

113162
/// Event produced by the `Handler`.
@@ -132,21 +181,33 @@ pub enum InEvent {
132181
/// Identifying information of the local node that is pushed to a remote.
133182
Push(Info),
134183
/// Identifying information requested from this node.
135-
Identify(Info),
184+
Identify(BehaviourInfo),
136185
}
137186

138187
impl Handler {
139188
/// Creates a new `Handler`.
140-
pub fn new(initial_delay: Duration, interval: Duration, remote_peer_id: PeerId) -> Self {
189+
pub fn new(
190+
initial_delay: Duration,
191+
interval: Duration,
192+
remote_peer_id: PeerId,
193+
public_key: PublicKey,
194+
protocol_version: String,
195+
agent_version: String,
196+
observed_addr: Multiaddr,
197+
) -> Self {
141198
Self {
142199
remote_peer_id,
143200
inbound_identify_push: Default::default(),
144201
events: SmallVec::new(),
145-
info: None,
146202
pending_replies: VecDeque::new(),
147203
trigger_next_identify: Delay::new(initial_delay),
148204
keep_alive: KeepAlive::Yes,
149205
interval,
206+
public_key,
207+
protocol_version,
208+
agent_version,
209+
observed_addr,
210+
behaviour_info: None,
150211
}
151212
}
152213

@@ -161,9 +222,9 @@ impl Handler {
161222
) {
162223
match output {
163224
EitherOutput::First(substream) => {
164-
// If we already have `Info` we can proceed responding to the Identify request,
165-
// if not, we request `Info` from the behaviour.
166-
if self.info.is_none() {
225+
// If we already have `BehaviourInfo` we can proceed responding to the Identify request,
226+
// if not, we request it .
227+
if self.behaviour_info.is_none() {
167228
self.events
168229
.push(ConnectionHandlerEvent::Custom(Event::Identify));
169230
}
@@ -259,7 +320,7 @@ impl ConnectionHandler for Handler {
259320
});
260321
}
261322
InEvent::Identify(info) => {
262-
self.info = Some(info);
323+
self.behaviour_info = Some(info);
263324
}
264325
}
265326
}
@@ -303,11 +364,19 @@ impl ConnectionHandler for Handler {
303364
}
304365

305366
// Check for pending replies to send.
306-
if let Some(ref info) = self.info {
367+
if let Some(ref info) = self.behaviour_info {
307368
if let Some(mut pending) = self.pending_replies.pop_front() {
308369
loop {
309370
match pending {
310371
Pending::Queued(io) => {
372+
let info = Info {
373+
public_key: self.public_key.clone(),
374+
protocol_version: self.protocol_version.clone(),
375+
agent_version: self.agent_version.clone(),
376+
listen_addrs: info.listen_addrs.clone(),
377+
protocols: info.protocols.clone(),
378+
observed_addr: self.observed_addr.clone(),
379+
};
311380
let io = Box::pin(io.send(info.clone()));
312381
pending = Pending::Sending {
313382
peer: self.remote_peer_id,

0 commit comments

Comments
 (0)