File size: 12,232 Bytes
f0f4f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use futures::StreamExt;
use libp2p_identify as identify;
use libp2p_swarm::{Swarm, SwarmEvent};
use libp2p_swarm_test::SwarmExt;
use std::collections::HashSet;
use std::iter;
use std::time::{Duration, Instant};
use tracing_subscriber::EnvFilter;

#[async_std::test]
async fn periodic_identify() {
    let _ = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .try_init();

    let mut swarm1 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("a".to_string(), identity.public())
                .with_agent_version("b".to_string()),
        )
    });
    let swarm1_peer_id = *swarm1.local_peer_id();

    let mut swarm2 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("c".to_string(), identity.public())
                .with_agent_version("d".to_string()),
        )
    });
    let swarm2_peer_id = *swarm2.local_peer_id();

    let (swarm1_memory_listen, swarm1_tcp_listen_addr) =
        swarm1.listen().with_memory_addr_external().await;
    let (swarm2_memory_listen, swarm2_tcp_listen_addr) = swarm2.listen().await;
    swarm2.connect(&mut swarm1).await;

    use identify::Event::Received;
    use identify::Event::Sent;

    match libp2p_swarm_test::drive(&mut swarm1, &mut swarm2).await {
        (
            [Received { info: s1_info, .. }, Sent { .. }],
            [Received { info: s2_info, .. }, Sent { .. }],
        )
        | (
            [Sent { .. }, Received { info: s1_info, .. }],
            [Sent { .. }, Received { info: s2_info, .. }],
        )
        | (
            [Received { info: s1_info, .. }, Sent { .. }],
            [Sent { .. }, Received { info: s2_info, .. }],
        )
        | (
            [Sent { .. }, Received { info: s1_info, .. }],
            [Received { info: s2_info, .. }, Sent { .. }],
        ) => {
            assert_eq!(s1_info.public_key.to_peer_id(), swarm2_peer_id);
            assert_eq!(s1_info.protocol_version, "c");
            assert_eq!(s1_info.agent_version, "d");
            assert!(!s1_info.protocols.is_empty());
            assert_eq!(s1_info.observed_addr, swarm1_memory_listen);
            assert!(s1_info.listen_addrs.contains(&swarm2_tcp_listen_addr));
            assert!(s1_info.listen_addrs.contains(&swarm2_memory_listen));

            assert_eq!(s2_info.public_key.to_peer_id(), swarm1_peer_id);
            assert_eq!(s2_info.protocol_version, "a");
            assert_eq!(s2_info.agent_version, "b");
            assert!(!s2_info.protocols.is_empty());

            // Cannot assert observed address of dialer because memory transport uses ephemeral, outgoing ports.
            // assert_eq!(
            //     s2_info.observed_addr,
            //     swarm2_memory_listen.with(Protocol::P2p(swarm2_peer_id.into()))
            // );
            assert!(s2_info.listen_addrs.contains(&swarm1_tcp_listen_addr));
            assert!(s2_info.listen_addrs.contains(&swarm1_memory_listen));
        }
        other => panic!("Unexpected events: {other:?}"),
    }
}
#[async_std::test]
async fn only_emits_address_candidate_once_per_connection() {
    let _ = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .try_init();

    let mut swarm1 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("a".to_string(), identity.public())
                .with_agent_version("b".to_string())
                .with_interval(Duration::from_secs(1)),
        )
    });
    let mut swarm2 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("c".to_string(), identity.public())
                .with_agent_version("d".to_string()),
        )
    });

    swarm2.listen().with_memory_addr_external().await;
    swarm1.connect(&mut swarm2).await;

    async_std::task::spawn(swarm2.loop_on_next());

    let swarm_events = futures::stream::poll_fn(|cx| swarm1.poll_next_unpin(cx))
        .take(8)
        .collect::<Vec<_>>()
        .await;

    let infos = swarm_events
        .iter()
        .filter_map(|e| match e {
            SwarmEvent::Behaviour(identify::Event::Received { info, .. }) => Some(info.clone()),
            _ => None,
        })
        .collect::<Vec<_>>();

    assert!(
        infos.len() > 1,
        "should exchange identify payload more than once"
    );

    let varying_observed_addresses = infos
        .iter()
        .map(|i| i.observed_addr.clone())
        .collect::<HashSet<_>>();
    assert_eq!(
        varying_observed_addresses.len(),
        1,
        "Observed address should not vary on persistent connection"
    );

    let external_address_candidates = swarm_events
        .iter()
        .filter_map(|e| match e {
            SwarmEvent::NewExternalAddrCandidate { address } => Some(address.clone()),
            _ => None,
        })
        .collect::<Vec<_>>();

    assert_eq!(
        external_address_candidates.len(),
        1,
        "To only have one external address candidate"
    );
    assert_eq!(
        &external_address_candidates[0],
        varying_observed_addresses.iter().next().unwrap()
    );
}

#[async_std::test]
async fn emits_unique_listen_addresses() {
    let _ = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .try_init();

    let mut swarm1 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("a".to_string(), identity.public())
                .with_agent_version("b".to_string())
                .with_interval(Duration::from_secs(1))
                .with_cache_size(10),
        )
    });
    let mut swarm2 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("c".to_string(), identity.public())
                .with_agent_version("d".to_string()),
        )
    });

    let (swarm2_mem_listen_addr, swarm2_tcp_listen_addr) =
        swarm2.listen().with_memory_addr_external().await;
    let swarm2_peer_id = *swarm2.local_peer_id();
    swarm1.connect(&mut swarm2).await;

    async_std::task::spawn(swarm2.loop_on_next());

    let swarm_events = futures::stream::poll_fn(|cx| swarm1.poll_next_unpin(cx))
        .take(8)
        .collect::<Vec<_>>()
        .await;

    let infos = swarm_events
        .iter()
        .filter_map(|e| match e {
            SwarmEvent::Behaviour(identify::Event::Received { info, .. }) => Some(info.clone()),
            _ => None,
        })
        .collect::<Vec<_>>();

    assert!(
        infos.len() > 1,
        "should exchange identify payload more than once"
    );

    let listen_addrs = infos
        .iter()
        .map(|i| i.listen_addrs.clone())
        .collect::<Vec<_>>();

    for addrs in listen_addrs {
        assert_eq!(addrs.len(), 2);
        assert!(addrs.contains(&swarm2_mem_listen_addr));
        assert!(addrs.contains(&swarm2_tcp_listen_addr));
    }

    let reported_addrs = swarm_events
        .iter()
        .filter_map(|e| match e {
            SwarmEvent::NewExternalAddrOfPeer { peer_id, address } => {
                Some((*peer_id, address.clone()))
            }
            _ => None,
        })
        .collect::<Vec<_>>();

    assert_eq!(reported_addrs.len(), 2, "To have two addresses of remote");
    assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_mem_listen_addr)));
    assert!(reported_addrs.contains(&(swarm2_peer_id, swarm2_tcp_listen_addr)));
}

#[async_std::test]
async fn identify_push() {
    let _ = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .try_init();

    let mut swarm1 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(identify::Config::new("a".to_string(), identity.public()))
    });
    let mut swarm2 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("a".to_string(), identity.public())
                .with_agent_version("b".to_string()),
        )
    });

    swarm1.listen().with_memory_addr_external().await;
    swarm2.connect(&mut swarm1).await;

    // First, let the periodic identify do its thing.
    let ([e1, e2], [e3, e4]) = libp2p_swarm_test::drive(&mut swarm1, &mut swarm2).await;

    {
        use identify::Event::{Received, Sent};

        // These can be received in any order, hence assert them here.
        assert!(matches!(e1, Received { .. } | Sent { .. }));
        assert!(matches!(e2, Received { .. } | Sent { .. }));
        assert!(matches!(e3, Received { .. } | Sent { .. }));
        assert!(matches!(e4, Received { .. } | Sent { .. }));
    }

    // Second, actively push.
    swarm2
        .behaviour_mut()
        .push(iter::once(*swarm1.local_peer_id()));

    let swarm1_received_info = match libp2p_swarm_test::drive(&mut swarm1, &mut swarm2).await {
        ([identify::Event::Received { info, .. }], [identify::Event::Pushed { .. }]) => info,
        other => panic!("Unexpected events: {other:?}"),
    };

    assert_eq!(
        swarm1_received_info.public_key.to_peer_id(),
        *swarm2.local_peer_id()
    );
    assert_eq!(swarm1_received_info.protocol_version, "a");
    assert_eq!(swarm1_received_info.agent_version, "b");
    assert!(!swarm1_received_info.protocols.is_empty());
    assert!(swarm1_received_info.listen_addrs.is_empty());
}

#[async_std::test]
async fn discover_peer_after_disconnect() {
    let _ = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .try_init();

    let mut swarm1 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(identify::Config::new("a".to_string(), identity.public()))
    });
    let mut swarm2 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("a".to_string(), identity.public())
                .with_agent_version("b".to_string()),
        )
    });

    swarm1.listen().with_memory_addr_external().await;
    swarm2.connect(&mut swarm1).await;

    let swarm1_peer_id = *swarm1.local_peer_id();
    async_std::task::spawn(swarm1.loop_on_next());

    // Wait until we identified.
    swarm2
        .wait(|event| {
            matches!(
                event,
                SwarmEvent::Behaviour(identify::Event::Received { .. })
            )
            .then_some(())
        })
        .await;

    swarm2.disconnect_peer_id(swarm1_peer_id).unwrap();

    // Wait for connection to close.
    swarm2
        .wait(|event| matches!(event, SwarmEvent::ConnectionClosed { .. }).then_some(()))
        .await;

    // We should still be able to dial now!
    swarm2.dial(swarm1_peer_id).unwrap();

    let connected_peer = swarm2
        .wait(|event| match event {
            SwarmEvent::ConnectionEstablished { peer_id, .. } => Some(peer_id),
            _ => None,
        })
        .await;

    assert_eq!(connected_peer, swarm1_peer_id);
}

#[async_std::test]
async fn configured_interval_starts_after_first_identify() {
    let _ = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .try_init();

    let identify_interval = Duration::from_secs(5);

    let mut swarm1 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("a".to_string(), identity.public())
                .with_interval(identify_interval),
        )
    });
    let mut swarm2 = Swarm::new_ephemeral(|identity| {
        identify::Behaviour::new(
            identify::Config::new("a".to_string(), identity.public())
                .with_agent_version("b".to_string()),
        )
    });

    swarm1.listen().with_memory_addr_external().await;
    swarm2.connect(&mut swarm1).await;

    async_std::task::spawn(swarm2.loop_on_next());

    let start = Instant::now();

    // Wait until we identified.
    swarm1
        .wait(|event| {
            matches!(event, SwarmEvent::Behaviour(identify::Event::Sent { .. })).then_some(())
        })
        .await;

    let time_to_first_identify = Instant::now().duration_since(start);

    assert!(time_to_first_identify < identify_interval)
}