hyperswarm 0.1.0

Rust implementation of hyperswarm, topic-based peer discovery on top of hyperdht
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Integration tests for hyperswarm

use std::{
    pin::Pin,
    task::{Context, Poll},
};

use dht_rpc::IdBytes;
use futures::{Sink, SinkExt, Stream, StreamExt, join};
use hypercore_handshake::CipherEvent;
use hyperdht::Connection;
use hyperdht::adht::Dht;
use hyperswarm::{DhtConfig, Error, JoinOpts, Swarm, SwarmConfig};
use test_utils::{Result, Testnet, rusty_nodejs_repl::wait};

use hypercore::{HypercoreBuilder, PartialKeypair, Storage};

macro_rules! timeout {
    ($fut:expr, $ms:expr) => {{
        let connection_result =
            tokio::time::timeout(std::time::Duration::from_millis($ms), $fut).await;
        connection_result
    }};
    ($fut:expr) => {{
        let out = timeout!($fut, 300);
        out
    }};
}

#[tokio::test]
async fn replicate_with_hypercore() -> Result<()> {
    let topic = IdBytes::random();

    let mut writer = HypercoreBuilder::new(Storage::new_memory().await.unwrap())
        .build()
        .await
        .unwrap();
    let public_key = writer.key_pair().public;
    let reader = HypercoreBuilder::new(Storage::new_memory().await.unwrap())
        .key_pair(PartialKeypair {
            public: public_key,
            secret: None,
        })
        .build()
        .await
        .unwrap();

    // Dedicated Rust bootstrap node — no JS testnet needed.
    // A 3rd routing node is required: in a 2-node DHT the announce is stored at the peer
    // (swarm_b), so swarm_b's own lookup (which queries swarm_a) would find nothing.
    let mut bs = Dht::with_config(DhtConfig::default().empty_bootstrap_nodes()).await?;
    let bs_addr = bs.local_addr()?;
    let bs_task = tokio::spawn(async move {
        loop {
            let _ = bs.next().await;
        }
    });

    let swarm_a = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let swarm_b = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let (r1, r2) = tokio::join!(swarm_a.bootstrap(), swarm_b.bootstrap());

    r1?;
    r2?;

    // Grab connection streams before joining so no events are missed.
    let a_conns = swarm_a
        .connections()
        .filter_map(|r| async move { r.ok().map(|e| e.connection) });
    let b_conns = swarm_b
        .connections()
        .filter_map(|r| async move { r.ok().map(|e| e.connection) });

    // both swarms join the topic, and connect to the bootstrap node
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;
    swarm_b.join(topic, JoinOpts::Client);
    swarm_b.flush().await?;

    // writer runs in the background to drive replication
    let writer_rep = tokio::spawn(writer.replicator().with_connection_stream(a_conns));
    // reader attaches replicator to drive replication forward when it calls `.get(..).await`
    reader.attach_replicator(reader.replicator().with_connection_stream(b_conns));

    writer.append(b"hello").await?;

    let block = tokio::time::timeout(std::time::Duration::from_secs(5), reader.get(0))
        .await
        .expect("timed out waiting for replication")
        .unwrap();
    assert_eq!(block, Some(b"hello".to_vec()));

    writer_rep.abort();
    bs_task.abort();
    Ok(())
}

/// Server announces and client discovers via lookup
#[tokio::test]
async fn server_announces_client_discovers() -> Result<()> {
    let mut tn = Testnet::new().await?;
    let bs_addr = tn.bootstrap_addr().await?;

    let topic = IdBytes::random();

    // Swarm A: server - listens and announces on topic
    let swarm_a = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let _server_conns = swarm_a.connections();
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;

    // Swarm B: client - discovers peers on topic
    let swarm_b = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    swarm_b.join(topic, JoinOpts::Client);
    swarm_b.flush().await?;

    // B should have discovered A
    assert!(
        swarm_b.peers_count() > 0,
        "swarm_b should have discovered peers"
    );

    Ok(())
}

/// Multiple servers announce, client discovers all
#[tokio::test]
async fn multiple_servers_discovered() -> Result<()> {
    let mut tn = Testnet::new().await?;
    let bs_addr = tn.bootstrap_addr().await?;

    let topic = IdBytes::random();

    // Create two servers announcing on same topic
    let swarm_a = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let _server_a = swarm_a.connections();
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;

    let swarm_b = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let _server_b = swarm_b.connections();
    swarm_b.join(topic, JoinOpts::Server);
    swarm_b.flush().await?;

    // Client discovers
    let swarm_c = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    swarm_c.join(topic, JoinOpts::Client);
    swarm_c.flush().await?;

    // C should have found both A and B
    let peers = swarm_c.peers_count();
    assert!(
        peers >= 2,
        "should discover multiple peers, found {}",
        peers
    );

    Ok(())
}

/// Two peers connect and exchange messages
#[tokio::test]
async fn peers_connect_and_exchange_messages() -> Result<()> {
    let mut tn = Testnet::new().await?;
    let bs_addr = tn.bootstrap_addr().await?;

    let topic = IdBytes::random();

    // Swarm A: server - listens and announces on topic
    let swarm_a = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    swarm_a.bootstrap().await?;
    let mut server_conns = swarm_a.connections();
    let server_addr = swarm_a.local_addr()?;
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;

    // Swarm B: client - connects to A directly using known address and public key
    let swarm_b = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    swarm_b.bootstrap().await?;
    let server_pub_key = swarm_a.keypair().public;

    // Run connect and accept in parallel
    let (mut client_conn, server_event) = join!(
        async {
            swarm_b
                .peer_handshake(server_pub_key.clone(), server_addr)
                .await
                .unwrap()
        },
        async { server_conns.next().await.unwrap().unwrap() }
    );
    let mut server_conn = server_event.connection;

    // Server sends first
    server_conn.send(b"hello from server".into()).await?;
    let CipherEvent::Message(msg) = client_conn.next().await.unwrap() else {
        panic!("Expected message from server");
    };
    assert_eq!(msg.as_slice(), b"hello from server");

    // Client sends back
    client_conn.send(b"hello from client".into()).await?;
    let CipherEvent::Message(msg) = server_conn.next().await.unwrap() else {
        panic!("Expected message from client");
    };
    assert_eq!(msg.as_slice(), b"hello from client");

    Ok(())
}

/// Test that discovery finds peers and enqueues them for connection
#[tokio::test]
async fn discovery_enqueues_peers_for_connection() -> Result<()> {
    let mut tn = Testnet::new().await?;
    let bs_addr = tn.bootstrap_addr().await?;

    let topic = IdBytes::random();

    // Swarm A: server - listens and announces on topic
    let config_a = SwarmConfig::new(DhtConfig::default().add_bootstrap_node(bs_addr));
    let swarm_a = Swarm::with_config(config_a).await?;
    swarm_a.bootstrap().await?;
    let _server_a = swarm_a.connections();
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;

    // Swarm B: client with auto-connect enabled (default)
    let config_b = SwarmConfig::new(DhtConfig::default().add_bootstrap_node(bs_addr));
    let swarm_b = Swarm::with_config(config_b).await?;
    swarm_b.bootstrap().await?;

    // Join as client - should auto-discover peers
    swarm_b.join(topic, JoinOpts::Client);
    swarm_b.flush().await?;

    // Should have discovered the server peer
    assert!(
        swarm_b.peers_count() > 0,
        "client should have discovered at least one peer"
    );

    Ok(())
}

/// Test that auto-connect actually establishes connections to discovered peers
#[tokio::test]
async fn auto_connect_establishes_connection() -> Result<()> {
    let mut tn = Testnet::new().await?;
    let bs_addr = tn.bootstrap_addr().await?;

    let topic = IdBytes::random();

    let swarm_a = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let swarm_b = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;

    swarm_a.bootstrap().await?;
    swarm_b.bootstrap().await?;

    let mut server_conns = swarm_a.connections();
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;

    // Get connection stream to receive auto-connect events
    let mut client_conns = swarm_b.connections();

    // Join as client - should auto-discover and auto-connect
    swarm_b.join(topic, JoinOpts::Client);
    swarm_b.flush().await?;

    let a = tokio::spawn(async move {
        let Some(Ok(client_event)) = timeout!(client_conns.next()).unwrap() else {
            todo!()
        };
        let mut client_conn = client_event.connection;
        let Some(CipherEvent::Message(msg)) = client_conn.next().await else {
            todo!()
        };
        assert_eq!(msg, b"from server");
        client_conn.send(b"from client".into()).await.unwrap();
        wait!(100);
    });
    let b = tokio::spawn(async move {
        let Some(Ok(server_event)) = timeout!(server_conns.next(), 1000).unwrap() else {
            todo!()
        };
        let mut server_conn = server_event.connection;
        server_conn.send(b"from server".into()).await.unwrap();
        let Some(CipherEvent::Message(msg)) = server_conn.next().await else {
            todo!()
        };
        assert_eq!(msg, b"from client");
        wait!(100);
    });
    _ = join!(a, b);
    Ok(())
}

/// Test that ConnectionEvent contains the discovered topics for client connections
#[tokio::test]
async fn connection_event_has_topics() -> Result<()> {
    let mut tn = Testnet::new().await?;
    let bs_addr = tn.bootstrap_addr().await?;

    let topic = IdBytes::random();

    let swarm_a = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let swarm_b = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;

    swarm_a.bootstrap().await?;
    swarm_b.bootstrap().await?;

    // Server joins topic
    let mut server_conns = swarm_a.connections();
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;

    // Client joins topic - should discover server and auto-connect
    let mut client_conns = swarm_b.connections();
    swarm_b.join(topic, JoinOpts::Client);
    swarm_b.flush().await?;

    // Both streams must be polled for auto-connect to complete
    let client_task = tokio::spawn(async move {
        let Some(Ok(conn_event)) = timeout!(client_conns.next(), 1000).unwrap() else {
            panic!("Expected client connection");
        };
        assert!(conn_event.client, "should be a client connection");
        assert!(
            conn_event.topics.contains(&topic),
            "connection event should contain the discovered topic, got {:?}",
            conn_event.topics
        );
        wait!(100);
    });

    let client_pub_key = swarm_b.public_key();
    let server_task = tokio::spawn(async move {
        let Some(Ok(conn_event)) = timeout!(server_conns.next(), 1000).unwrap() else {
            panic!("Expected server connection");
        };
        assert!(!conn_event.client, "should be a server connection");
        assert!(
            conn_event.topics.is_empty(),
            "server connection should have empty topics"
        );
        // Server should know the client's public key from Noise handshake
        assert_eq!(
            conn_event.remote_public_key, client_pub_key,
            "server should have client's public key"
        );
        wait!(100);
    });

    _ = join!(client_task, server_task);
    Ok(())
}

async fn two_connected_swarms() -> Result<(Testnet, (Swarm, Swarm))> {
    let mut tn = Testnet::new().await?;
    let bs_addr = tn.bootstrap_addr().await?;

    let topic = IdBytes::random();

    let swarm_a = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;
    let swarm_b = Swarm::new(DhtConfig::default().add_bootstrap_node(bs_addr)).await?;

    swarm_a.bootstrap().await?;
    swarm_b.bootstrap().await?;

    // Server joins topic
    swarm_a.join(topic, JoinOpts::Server);
    swarm_a.flush().await?;

    // Client joins topic - should discover server and auto-connect
    swarm_b.join(topic, JoinOpts::Client);
    swarm_b.flush().await?;
    Ok((tn, (swarm_a, swarm_b)))
}
pub struct MessageCipher(pub Connection);

impl Stream for MessageCipher {
    type Item = Vec<u8>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        loop {
            match Pin::new(&mut self.0).poll_next(cx) {
                Poll::Ready(Some(CipherEvent::Message(data))) => return Poll::Ready(Some(data)),
                Poll::Ready(Some(CipherEvent::HandshakePayload(_))) => continue,
                Poll::Ready(Some(CipherEvent::ErrStuff(_))) => continue,
                Poll::Ready(None) => return Poll::Ready(None),
                Poll::Pending => return Poll::Pending,
            }
        }
    }
}

impl Sink<Vec<u8>> for MessageCipher {
    type Error = std::io::Error;

    fn poll_ready(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<std::result::Result<(), Self::Error>> {
        Pin::new(&mut self.0).poll_ready(cx)
    }

    fn start_send(mut self: Pin<&mut Self>, item: Vec<u8>) -> std::result::Result<(), Self::Error> {
        Pin::new(&mut self.0).start_send(item)
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<std::result::Result<(), Self::Error>> {
        Pin::new(&mut self.0).poll_flush(cx)
    }

    fn poll_close(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<std::result::Result<(), Self::Error>> {
        Pin::new(&mut self.0).poll_close(cx)
    }
}

/// just doing the same thincg as hypercore_protocol's 'basic_protocol' test
#[tokio::test]
async fn protocol() -> Result<()> {
    use hypercore_protocol::{Event, Message, Protocol, schema};
    let (mut _tn, (swarm_i, swarm_r)) = two_connected_swarms().await?;

    let key = [3u8; 32];
    let i = tokio::spawn(async move {
        // get connection event
        let mut cstream = swarm_i.connections();
        let conn_event = cstream.next().await.unwrap().unwrap();

        // plug into Protocol
        let mut p = Protocol::new(Box::new(conn_event.connection));

        // get some events from protocol?
        assert!(matches!(p.next().await.unwrap()?, Event::Handshake(_)));
        p.open(key).await?;
        // NB: this is required. We need to a 'wake' in Protocol somewhere. k
        // that will trigger after the responder does an open
        wait!(1000);
        let Event::Channel(mut chan) = p.next().await.unwrap()? else {
            todo!()
        };
        chan.send(Message::Want(schema::Want {
            start: 0,
            length: 5,
        }))
        .await?;
        let pres = tokio::spawn(async move {
            assert!(matches!(p.next().await.unwrap().unwrap(), Event::Close(_)));
            println!("init RX close");
            //  TODO can't drop proto yet or the "Close" message will never get to the responder
            _ = timeout!(p.next(), 1000);
        });
        let cev = chan.next().await;
        assert!(matches!(
            cev.unwrap(),
            Message::Want(schema::Want {
                start: 10,
                length: 3
            })
        ));
        println!("init got chan message");
        chan.close().await?;
        println!("init sent close ");
        pres.await.unwrap();
        println!("init done close ");
        assert!(chan.closed());

        Ok::<_, Error>(())
    });

    let r = tokio::spawn(async move {
        // get connection event
        let mut cstream = swarm_r.connections();
        let conn_event = cstream.next().await.unwrap().unwrap();

        let mut p = Protocol::new(Box::new(conn_event.connection));

        // get some events from protocol?
        assert!(matches!(p.next().await.unwrap()?, Event::Handshake(_)));
        assert!(matches!(p.next().await.unwrap()?, Event::DiscoveryKey(_)));

        p.open(key).await?;
        let Event::Channel(mut chan) = p.next().await.unwrap()? else {
            todo!()
        };
        chan.send(Message::Want(schema::Want {
            start: 10,
            length: 3,
        }))
        .await?;
        let pres = tokio::spawn(async move {
            assert!(matches!(p.next().await.unwrap().unwrap(), Event::Close(_)));
            println!("resp RX close");
        });
        let cev = chan.next().await;
        assert!(matches!(
            cev.unwrap(),
            Message::Want(schema::Want {
                start: 0,
                length: 5
            })
        ));
        println!("resp got chan message");
        pres.await.unwrap();
        assert!(chan.closed());

        Ok::<_, Error>(())
    });

    let (ires, rres) = join!(i, r);
    ires??;
    rres??;

    Ok(())
}