iroh-dht-experiment 0.1.1

Experimental DHT for iroh
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! In memory integration style tests
//!
//! These are long running tests that spawn a lot of nodes and observe the
//! behaviour of an entire swarm. Most tests use in-memory nodes.
use std::{
    path::PathBuf,
    sync::{Arc, Mutex},
};

use iroh::{
    Endpoint, SecretKey, Watcher, discovery::static_provider::StaticProvider, endpoint::BindError,
    protocol::Router,
};
use iroh_blobs::util::connection_pool::ConnectionPool;
use rand::{Rng, rngs::StdRng, seq::SliceRandom};
use testresult::TestResult;
use textplots::{Chart, Plot, Shape};

use super::*;
use crate::{pool::IrohPool, rpc::Blake3Immutable};

#[derive(Debug, Clone)]
struct TestPool {
    clients: Arc<Mutex<BTreeMap<NodeId, RpcClient>>>,
    node_id: NodeId,
}

impl ClientPool for TestPool {
    async fn client(&self, id: NodeId) -> Result<RpcClient, String> {
        let client = self
            .clients
            .lock()
            .unwrap()
            .get(&id)
            .cloned()
            .ok_or_else(|| format!("client not found: {id}"))?;
        Ok(client)
    }

    fn id(&self) -> NodeId {
        self.node_id
    }
}

fn expected_ids(ids: &[NodeId], key: Id, n: usize) -> Vec<NodeId> {
    let mut expected = ids
        .iter()
        .cloned()
        .map(|id| (Distance::between(id.as_bytes(), &key), id))
        .collect::<Vec<_>>();
    // distances are unique!
    expected.sort_unstable();
    expected.dedup();
    expected.truncate(n);
    expected.into_iter().map(|(_, id)| id).collect()
}

type Nodes = Vec<(NodeId, (RpcClient, ApiClient))>;

fn rng(seed: u64) -> StdRng {
    let mut expanded = [0; 32];
    expanded[..8].copy_from_slice(&seed.to_le_bytes());
    StdRng::from_seed(expanded)
}

/// Choose boostrap nodes.
///
/// Selection indexes will be wrapped around.
/// The node itself will never be considered.
/// Duplicates will be removed.
fn apply_selection(this: usize, ids: &[NodeId], selection: &[usize]) -> Vec<NodeId> {
    let mut res = Vec::new();
    for i in selection {
        if *i == this {
            continue;
        }
        let offset = i % ids.len();
        let id = ids[offset];
        if !res.contains(&id) {
            res.push(id);
        }
    }
    res
}

type Clients = Arc<Mutex<BTreeMap<NodeId, RpcClient>>>;

async fn create_nodes(
    ids: &[NodeId],
    select_bootstrap: impl Fn(usize) -> Vec<usize>,
    config: Config,
) -> Nodes {
    create_nodes_and_clients(ids, select_bootstrap, config)
        .await
        .0
}

/// Creates n nodes with the given seed, and at most n_bootstrap bootstrap nodes.
///
/// Bootstrap nodes are just the n_bootstrap next nodes in the ring.
async fn create_nodes_and_clients(
    ids: &[NodeId],
    select_bootstrap: impl Fn(usize) -> Vec<usize>,
    config: Config,
) -> (Nodes, Clients) {
    let clients = Arc::new(Mutex::new(BTreeMap::new()));
    // create n nodes
    let nodes = ids
        .iter()
        .enumerate()
        .map(|(offfset, id)| {
            let pool = TestPool {
                clients: clients.clone(),
                node_id: *id,
            };
            let bootstrap = apply_selection(offfset, ids, &select_bootstrap(offfset));
            (
                *id,
                create_node_impl(*id, pool, bootstrap, None, config.clone()),
            )
        })
        .collect::<Vec<_>>();
    clients
        .lock()
        .unwrap()
        .extend(nodes.iter().map(|(id, (rpc, _))| (*id, rpc.clone())));
    (nodes, clients)
}

/// Brute force init of the routing table of all nodes using a set of ids, that could be the full set.
///
/// Provide a seed to shuffle the ids for each node.
async fn init_routing_tables(nodes: &Nodes, ids: &[NodeId], seed: Option<u64>) -> irpc::Result<()> {
    let mut rng = seed.map(rng);
    let ids = ids.to_vec();
    stream::iter(nodes.iter().enumerate())
        .for_each_concurrent(4096, |(index, (_, (_, api)))| {
            if ids.len() > 10000 {
                println!("{index}");
            }
            let mut ids = ids.clone();
            if let Some(rng) = &mut rng {
                ids.shuffle(rng);
            }
            async move {
                api.nodes_seen(&ids).await.ok();
            }
        })
        .await;
    Ok(())
}

fn make_histogram(data: &[usize]) -> Vec<usize> {
    let max = data.iter().max().cloned().unwrap_or(0);
    let mut histogram = vec![0usize; max + 1];
    for value in data.iter().cloned() {
        histogram[value] += 1;
    }
    histogram
}

fn plot_console(title: &str, data: &[usize]) {
    let data: Vec<(f32, f32)> = data
        .iter()
        .enumerate()
        .map(|(items_stored, num_nodes)| (items_stored as f32, *num_nodes as f32))
        .collect();

    println!("{title}");
    Chart::new(100, 40, 0.0, (data.len() - 1) as f32)
        .lineplot(&Shape::Bars(&data))
        .nice();
}

fn plot_png(title: &str, data: &[usize]) {
    use plotters::prelude::*;
    let data: Vec<(f32, f32)> = data
        .iter()
        .enumerate()
        .map(|(items_stored, num_nodes)| (items_stored as f32, *num_nodes as f32))
        .collect();
    // PNG plot
    let filename = format!("{}.png", title.replace(' ', "_")).to_lowercase();
    let path = PathBuf::from("img/").join(&filename);
    std::fs::create_dir_all("img/").unwrap();
    let root = BitMapBackend::new(&path, (1024, 768)).into_drawing_area();
    root.fill(&WHITE).unwrap();

    let max_y = data.iter().map(|&(_, y)| y).fold(0f32, f32::max);
    let mut chart = ChartBuilder::on(&root)
        .caption(title, ("sans-serif", 30).into_font())
        .margin(10)
        .x_label_area_size(30)
        .y_label_area_size(30)
        .build_cartesian_2d(0f32..(data.len() as f32), 0f32..max_y * 1.1)
        .unwrap();

    chart
        .configure_mesh()
        .x_labels(5)
        .y_labels(5)
        .draw()
        .unwrap();

    // Draw bars for a bar chart effect
    chart
        .draw_series(
            data.iter()
                .zip(0..)
                .map(|((x, y), _)| Rectangle::new([(*x, 0f32), (*x + 1.0, *y)], BLUE.filled())),
        )
        .unwrap();

    root.present().unwrap();
}

fn plot(title: &str, data: &[usize]) {
    plot_console(title, data);
    plot_png(title, data);
}

/// Let each node do a random lookup
async fn random_lookup(nodes: &Nodes, rng: &mut StdRng) -> irpc::Result<()> {
    stream::iter(nodes.iter())
        .for_each_concurrent(4096, |(_, (_, api))| {
            let key = Id::from(rng.r#gen::<[u8; 32]>());
            async move {
                // perform a random lookup
                api.lookup(key, None).await.ok();
            }
        })
        .await;
    Ok(())
}

async fn random_lookup_n(nodes: &Nodes, n: usize, seed: u64) -> irpc::Result<()> {
    let mut rng = rng(seed);
    for _ in 0..n {
        random_lookup(nodes, &mut rng).await?;
    }
    Ok(())
}

async fn store_random_values(prefix: &str, nodes: &Nodes, n: usize) -> irpc::Result<()> {
    let (_, (_, api)) = nodes[nodes.len() / 2].clone();
    let ids = nodes.iter().map(|(id, _)| *id).collect::<Vec<_>>();
    let mut common_count = vec![0usize; n];
    #[allow(clippy::needless_range_loop)]
    for i in 0..n {
        if nodes.len() > 10000 {
            println!("Value {i}");
        }
        let text = format!("Item {i}");
        let expected_ids = expected_ids(&ids, Id::blake3_hash(text.as_bytes()), 20);
        let (hash, ids) = api.put_immutable(text.as_bytes()).await.unwrap();
        let mut common = expected_ids.clone();
        common.retain(|id| ids.contains(id));
        common_count[i] = common.len();
        let data = api.get_immutable(hash).await.unwrap();
        assert_eq!(
            data,
            Some(text.as_bytes().to_vec()),
            "Data mismatch for item {i}"
        );
    }

    let mut storage_count = vec![0usize; nodes.len()];
    let mut routing_table_size = vec![0usize; nodes.len()];
    for (index, (_, (_, api))) in nodes.iter().enumerate() {
        let stats = api.get_storage_stats().await?;
        if !stats.is_empty() {
            let n = stats
                .values()
                .map(|kinds| kinds.values().sum::<usize>())
                .sum::<usize>();
            storage_count[index] = n;
            // println!("Storage stats for node {index}: {n}");
        }
    }

    for (index, (_, (_, api))) in nodes.iter().enumerate() {
        let routing_table = api.get_routing_table().await?;
        let count = routing_table.iter().map(|peers| peers.len()).sum::<usize>();
        // println!("Routing table {index}: {count} nodes");
        routing_table_size[index] = count;
    }

    plot(
        &format!("{prefix} - Histogram - Commonality with perfect set of 20 ids"),
        &make_histogram(&common_count),
    );
    plot(
        &format!("{prefix} - Storage usage per node"),
        &storage_count,
    );
    plot(
        &format!("{prefix} - Histogram - Storage usage per node"),
        &make_histogram(&storage_count),
    );
    plot(
        &format!("{prefix} - Routing table size per node"),
        &routing_table_size,
    );
    plot(
        &format!("{prefix} - Histogram - Routing table size per node"),
        &make_histogram(&routing_table_size),
    );
    Ok(())
}

/// Performs n random lookups without storing anything, then plots stats
async fn plot_random_lookup_stats(prefix: &str, nodes: &Nodes, n: usize) -> irpc::Result<()> {
    let (_, (_, api)) = nodes[nodes.len() / 2].clone();
    let ids = nodes.iter().map(|(id, _)| *id).collect::<Vec<_>>();
    let mut common_count = vec![0usize; n];
    let mut storage_count = vec![0usize; nodes.len()];
    #[allow(clippy::needless_range_loop)]
    for i in 0..n {
        if nodes.len() > 10000 {
            println!("{i}");
        }
        let text = format!("Item {i}");
        let id = Id::from(blake3::hash(text.as_bytes()));
        let storage_ids = api.lookup(id, None).await.unwrap();
        let expected_ids = expected_ids(&ids, Id::blake3_hash(text.as_bytes()), 20);
        let mut common = expected_ids.clone();
        common.retain(|id| storage_ids.contains(id));
        common_count[i] = common.len();
        for id in &storage_ids {
            let idx = ids.iter().position(|x| *x == *id).unwrap();
            storage_count[idx] += 1;
        }
    }

    let mut routing_table_size = vec![0usize; nodes.len()];
    for (index, (_, (_, api))) in nodes.iter().enumerate() {
        let routing_table = api.get_routing_table().await?;
        let count = routing_table.iter().map(|peers| peers.len()).sum::<usize>();
        // println!("Routing table {index}: {count} nodes");
        routing_table_size[index] = count;
    }

    plot(
        &format!("{prefix} - Histogram - Commonality with perfect set of 20 ids"),
        &make_histogram(&common_count),
    );
    plot(
        &format!("{prefix} - Storage usage per node"),
        &storage_count,
    );
    plot(
        &format!("{prefix} - Histogram - Storage usage per node"),
        &make_histogram(&storage_count),
    );
    plot(
        &format!("{prefix} - Routing table size per node"),
        &routing_table_size,
    );
    plot(
        &format!("{prefix} - Histogram - Routing table size per node"),
        &make_histogram(&routing_table_size),
    );
    Ok(())
}

/// Create routing table buckets for the given ids.
///
/// Note that if there are a lot of ids, they won't all fit.
#[allow(dead_code)]
fn create_buckets(ids: &[NodeId]) -> Box<Buckets> {
    let secret = SecretKey::from_bytes(&[0; 32]);
    let node_id = secret.public();
    let mut routing_table = RoutingTable::new(node_id, None);
    for id in ids {
        routing_table.add_node(NodeInfo {
            id: *id,
            last_seen: now(),
        });
    }
    routing_table.buckets
}

fn next_n(n: usize) -> impl Fn(usize) -> Vec<usize> {
    move |offset| (1..=n).map(|i| offset + i).collect::<Vec<_>>()
}

#[tokio::test(flavor = "multi_thread")]
async fn no_routing_1k() {
    let prefix = "no_routing_1k";
    let n = 1000;
    let seed = 0;
    let bootstrap = next_n(0);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let nodes = create_nodes(&ids, bootstrap, Config::default()).await;
    let clients = nodes.iter().cloned().collect::<BTreeMap<_, _>>();

    for i in 0..100 {
        let text = format!("Item {i}");
        let key = Id::blake3_hash(text.as_bytes());
        for id in expected_ids(&ids, key, 20) {
            let (rpc, _) = clients.get(&id).expect("Node not found");
            rpc.set(
                key,
                Value::Blake3Immutable(Blake3Immutable {
                    timestamp: now(),
                    data: text.as_bytes().to_vec(),
                }),
            )
            .await
            .ok();
        }
    }

    let mut storage_count = vec![0usize; nodes.len()];
    for (index, (_, (_, api))) in nodes.iter().enumerate() {
        let stats: BTreeMap<Id, BTreeMap<Kind, usize>> = api.get_storage_stats().await.unwrap();
        if !stats.is_empty() {
            let n = stats
                .values()
                .map(|kinds| kinds.values().sum::<usize>())
                .sum::<usize>();
            storage_count[index] = n;
        }
    }
    plot(
        &format!("{prefix} - Storage usage per node"),
        &storage_count,
    );
    plot(
        &format!("{prefix} -Histogram - Storage usage per node"),
        &make_histogram(&storage_count),
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn perfect_routing_tables_1k() {
    let n = 1000;
    let seed = 0;
    let bootstrap = next_n(0);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let nodes = create_nodes(&ids, bootstrap, Config::default()).await;
    init_routing_tables(&nodes, &ids, Some(seed)).await.ok();
    store_random_values("perfect_routing_tables_1k", &nodes, 100)
        .await
        .ok();
}

#[tokio::test(flavor = "multi_thread")]
async fn perfect_routing_tables_10k() {
    let n = 10000;
    let seed = 0;
    let bootstrap = next_n(0);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let nodes = create_nodes(&ids, bootstrap, Config::default()).await;

    // tell all nodes about all ids, shuffled for each node
    println!("init routing tables");
    init_routing_tables(&nodes, &ids, Some(seed)).await.ok();
    println!("store random values");
    store_random_values("perfect_routing_tables_10k", &nodes, 100)
        .await
        .ok();
}

#[tokio::test(flavor = "multi_thread")]
#[ignore = "runs very long and takes ~20GiB"]
async fn perfect_routing_tables_100k() {
    let n = 100000;
    let seed = 0;
    let bootstrap = next_n(0);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let nodes = create_nodes(&ids, bootstrap, Config::default()).await;

    println!("init routing tables");
    init_routing_tables(&nodes, &ids, Some(seed)).await.ok();

    println!("store random values");
    store_random_values("perfect_routing_tables_100k", &nodes, 100)
        .await
        .ok();
}

#[tokio::test(flavor = "multi_thread")]
async fn just_bootstrap_1k() {
    let n = 1000;
    let seed = 0;
    let bootstrap = next_n(20);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let nodes = create_nodes(&ids, bootstrap, Config::default()).await;

    // tell all nodes about all ids, shuffled for each node
    // init_routing_tables(&nodes, &ids, Some(seed)).await.ok();
    store_random_values("just_bootstrap_1k", &nodes, 100)
        .await
        .ok();
}

async fn random_lookup_test(prefix: &str, n: usize, seed: u64, lookups: usize) {
    // bootstrap must be set so the random lookups have a chance to work!
    let bootstrap = next_n(20);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let nodes = create_nodes(&ids, bootstrap, Config::default()).await;

    random_lookup_n(&nodes, lookups, seed).await.ok();

    // tell all nodes about all ids, shuffled for each node
    // init_routing_tables(&nodes, &ids, Some(seed)).await.ok();
    store_random_values(prefix, &nodes, 100).await.ok();
}

#[tokio::test(flavor = "multi_thread")]
async fn random_lookup_1k() {
    for lookups in 0..10 {
        random_lookup_test(&format!("random_lookup_1k_{lookups}"), 1000, 0, lookups).await;
    }
}

const DHT_TEST_ALPN: &[u8] = b"iroh/dht/test-0";

type IrohNodes = Vec<(Endpoint, (RpcClient, ApiClient))>;

/// Creates n nodes with the given seed, and at most n_bootstrap bootstrap nodes.
///
/// Bootstrap nodes are just the n_bootstrap next nodes in the ring.
///
/// These will be full iroh nodes with static discovery configured in such a way that they can find each other without bothering
/// the discovery service!
async fn iroh_create_nodes(
    secrets: &[SecretKey],
    mut n_bootstrap: usize,
    buckets: Option<Box<Buckets>>,
) -> std::result::Result<IrohNodes, BindError> {
    let n = secrets.len();
    let node_ids = secrets.iter().map(|s| s.public()).collect::<Vec<_>>();
    let node_ids = Arc::new(node_ids);
    let buckets = Arc::new(buckets);
    let discovery = StaticProvider::new();
    n_bootstrap = n_bootstrap.min(n - 1);
    // create n nodes
    stream::iter(secrets.iter().zip(node_ids.iter()).enumerate())
        .map(|(offfset, (secret, node_id))| {
            let buckets = buckets.clone();
            let node_ids = node_ids.clone();
            let discovery = discovery.clone();
            async move {
                let endpoint = Endpoint::builder()
                    .secret_key(secret.clone())
                    .relay_mode(iroh::RelayMode::Disabled)
                    .discovery(discovery.clone())
                    .bind()
                    .await?;
                let addr = endpoint.node_addr().initialized().await;
                discovery.add_node_info(addr.clone());
                let pool = ConnectionPool::new(
                    endpoint.clone(),
                    DHT_TEST_ALPN,
                    iroh_blobs::util::connection_pool::Options {
                        max_connections: 32,
                        idle_timeout: Duration::from_secs(1),
                        connect_timeout: Duration::from_secs(1),
                        on_connected: None,
                    },
                );
                let pool = IrohPool::new(endpoint.clone(), pool);
                let bootstrap = (0..n_bootstrap)
                    .map(|i| node_ids[(offfset + i + 1) % n])
                    .collect::<Vec<_>>();
                let (rpc, api) = create_node_impl(
                    *node_id,
                    pool.clone(),
                    bootstrap,
                    (*buckets).clone(),
                    Default::default(),
                );
                pool.set_self_client(Some(rpc.downgrade()));
                Ok((endpoint, (rpc, api)))
            }
        })
        .buffered_unordered(32)
        .collect::<Vec<_>>()
        .await
        .into_iter()
        .collect()
}

fn create_secrets(seed: u64, n: usize) -> Vec<SecretKey> {
    // std rng is good enough for tests!
    let mut rng = rng(seed);
    (0..n)
        .map(|_| SecretKey::from_bytes(&rng.r#gen::<[u8; 32]>()))
        .collect()
}

fn create_node_ids(secrets: &[SecretKey]) -> Vec<NodeId> {
    secrets.iter().map(|s| s.public()).collect()
}

// todo: we need a special protocol handler that validates the requester id of
// incoming FindNode messages to be the remote node id. This is pretty
// straightforward, but I can't write it right now because of some
// dependency weirdness due to all the patching.
fn spawn_routers(iroh_nodes: &IrohNodes) -> Vec<Router> {
    iroh_nodes
        .iter()
        .map(|(endpoint, (rpc, _))| {
            let sender = rpc.0.as_local().unwrap();
            Router::builder(endpoint.clone())
                .accept(DHT_TEST_ALPN, irpc_iroh::IrohProtocol::with_sender(sender))
                .spawn()
        })
        .collect()
}
async fn iroh_perfect_routing_tables(prefix: &str, n: usize) -> TestResult<()> {
    let seed = 0;
    let bootstrap = 0;
    let secrets = create_secrets(seed, n);
    println!("Creating {n} nodes");
    let iroh_nodes = iroh_create_nodes(&secrets, bootstrap, None).await?;
    let nodes = iroh_nodes
        .iter()
        .map(|(ep, x)| (ep.node_id(), x.clone()))
        .collect::<Vec<_>>();
    let ids = nodes.iter().map(|(id, _)| *id).collect::<Vec<_>>();
    println!("Initializing routing tables");
    init_routing_tables(&nodes, &ids, Some(seed)).await.ok();
    println!("Spawning {n} routers");
    let _routers = spawn_routers(&iroh_nodes);
    println!("Storing random values");
    store_random_values(prefix, &nodes, 100).await.ok();
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn iroh_perfect_routing_tables_500() -> TestResult<()> {
    iroh_perfect_routing_tables("perfect_routing_tables_500", 500).await
}

#[tokio::test(flavor = "multi_thread")]
#[ignore = "runs very long and takes a lot of mem"]
async fn iroh_perfect_routing_tables_10k() -> TestResult<()> {
    iroh_perfect_routing_tables("perfect_routing_tables_10k", 10000).await
}

#[tokio::test(flavor = "multi_thread")]
async fn random_lookup_strategy() {
    let n = 1000;
    let seed = 0;
    let bootstrap = next_n(20);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let config = Config::default().random_lookup_strategy(RandomLookupStrategy {
        interval: Duration::from_secs(1),
        blended: false,
    });
    let nodes = create_nodes(&ids, bootstrap, config).await;
    for _i in 0..20 {
        tokio::time::sleep(Duration::from_secs(1)).await;
        plot_random_lookup_stats("random_lookup_strategy", &nodes, 100)
            .await
            .ok();
        tokio::time::sleep(Duration::from_secs(1)).await;
    }
}

#[tokio::test(flavor = "multi_thread")]
async fn self_lookup_strategy() {
    let n = 1000;
    let seed = 0;
    let bootstrap = next_n(20);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let config = Config::default().self_lookup_strategy(SelfLookupStrategy {
        interval: Duration::from_secs(1),
    });
    let nodes = create_nodes(&ids, bootstrap, config).await;
    for i in 0..20 {
        plot_random_lookup_stats(&format!("self_lookup_strategy-{i}"), &nodes, 100)
            .await
            .ok();
        tokio::time::sleep(Duration::from_secs(1)).await;
        println!();
    }
}

#[tokio::test(flavor = "multi_thread")]
async fn self_and_random_lookup_strategy() {
    let n = 1000;
    let seed = 0;
    let bootstrap = next_n(20);
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let config = Config::default()
        .self_lookup_strategy(SelfLookupStrategy {
            interval: Duration::from_secs(1),
        })
        .random_lookup_strategy(RandomLookupStrategy {
            interval: Duration::from_secs(1),
            blended: false,
        });
    let nodes = create_nodes(&ids, bootstrap, config).await;
    for _i in 0..20 {
        tokio::time::sleep(Duration::from_secs(1)).await;
        plot_random_lookup_stats("self_and_random_lookup_strategy", &nodes, 100)
            .await
            .ok();
        println!();
    }
}

use std::{fs::File, path::Path};

use gif::{Encoder, Frame, Repeat};

struct Frames {
    data: Vec<Vec<bool>>,
    stride: usize,
}

impl Frames {
    fn new(stride: usize) -> Self {
        Frames {
            data: Vec::new(),
            stride,
        }
    }

    fn height(&self) -> Option<usize> {
        let first = self.data.first()?;
        assert!(
            self.data.iter().all(|f| f.len() == first.len()),
            "All frames must have the same length"
        );
        assert!(self.stride > 0, "Stride must be greater than zero");
        assert!(
            first.len().is_multiple_of(self.stride),
            "Size must be a multiple of stride"
        );
        Some(first.len() / self.stride)
    }

    fn side_by_side(frames: &[&Frames], gap: usize) -> TestResult<Frames> {
        let first = frames.first().unwrap();
        let height = first.height().unwrap();
        let n_frames = first.data.len();
        assert!(
            frames.iter().all(|f| f.stride > 0),
            "Stride must be greater than zero"
        );
        assert!(
            frames.iter().all(|f| f.height() == Some(height)),
            "All frames must have the same height"
        );
        assert!(
            frames.iter().all(|f| f.data.len() == n_frames),
            "All frames must have the same number of frames"
        );
        // width of the resulting frames
        let width = frames.iter().map(|f| f.stride).sum::<usize>() + gap * (frames.len() - 1);
        let mut res = Frames::new(width);
        for n_frame in 0..n_frames {
            let mut frame = Vec::new();
            // iterate each frame line by line. We know these iters will have the same size!
            let mut iters = frames
                .iter()
                .map(|f| f.data[n_frame].chunks(f.stride))
                .collect::<Vec<_>>();
            for _y in 0..height {
                for (i, iter) in iters.iter_mut().enumerate() {
                    let line = iter.next().unwrap();
                    if i > 0 {
                        // add gap between frames
                        frame.extend(std::iter::repeat_n(false, gap));
                    }
                    frame.extend_from_slice(line);
                }
            }
            res.data.push(frame);
        }
        Ok(res)
    }

    fn make_gif(&self, delay_ms: u64, target: impl AsRef<Path>) -> TestResult<()> {
        if self.data.is_empty() {
            return Ok(());
        }
        let size = self.data[0].len();
        assert!(self.stride > 0, "Stride must be greater than zero");
        assert!(
            size.is_multiple_of(self.stride),
            "Height must be a multiple of stride"
        );
        assert!(
            self.data.iter().all(|f| f.len() == size),
            "All frames must have the same length"
        );
        if let Some(parent) = target.as_ref().parent() {
            std::fs::create_dir_all(parent)?;
        }

        let width = self.stride;
        let height = size / self.stride;

        // Create output file
        let mut output = File::create(target)?;

        // Create encoder with a simple black/white palette
        let palette = [0, 0, 0, 255, 255, 255]; // Black and white
        let mut encoder = Encoder::new(&mut output, width as u16, height as u16, &palette)?;
        encoder.set_repeat(Repeat::Infinite)?;

        // Convert each bool array to a frame
        for frame_data in &self.data {
            // Convert bool array to indexed pixel data
            let mut pixels = Vec::with_capacity(frame_data.len());
            for &pixel in frame_data {
                pixels.push(if pixel { 1u8 } else { 0 }); // 0 = black, 1 = white
            }

            // Create frame with 100ms delay (10/100 seconds)
            let mut frame = Frame::from_indexed_pixels(width as u16, height as u16, pixels, None);
            frame.delay = (delay_ms / 10) as u16; // Convert ms to 1/100 seconds

            encoder.write_frame(&frame)?;
        }

        Ok(())
    }
}

async fn make_frame(ids: &[NodeId], nodes: &Nodes) -> TestResult<Vec<bool>> {
    let mut res = Vec::new();
    for (_, (_, api)) in nodes.iter() {
        let routing_table = api.get_routing_table().await?;
        let node_ids = routing_table
            .iter()
            .flat_map(|peers| peers.iter().map(|x| x.id))
            .collect::<HashSet<_>>();
        res.extend(ids.iter().map(|id| node_ids.contains(id)));
    }
    Ok(res)
}

#[tokio::test(flavor = "multi_thread")]
async fn partition_1k() -> TestResult<()> {
    let n = 1000;
    let k = 900;
    let seed = 0;
    let bootstrap = |i| {
        if i < k {
            // nodes below k form a ring
            (1..=20).map(|j| (i + j) % k).collect::<Vec<_>>()
        } else {
            // nodes above k don't have bootstrap peers
            vec![]
        }
    };
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    // all nodes have all the strategies enabled
    let config = Config::persistent()
        .self_lookup_strategy(SelfLookupStrategy {
            interval: Duration::from_secs(1),
        })
        .random_lookup_strategy(RandomLookupStrategy {
            interval: Duration::from_secs(1),
            blended: false,
        })
        .candidate_lookup_strategy(CandidateLookupStrategy {
            max_lookups: 1,
            interval: Duration::from_secs(1),
        });
    let (nodes, _clients) = create_nodes_and_clients(&ids, bootstrap, config).await;
    let mut frames = Vec::new();
    for i in 0..10 {
        tokio::time::sleep(Duration::from_secs(1)).await;
        plot_random_lookup_stats(&format!("partition_1k-{i}-"), &nodes, 100)
            .await
            .ok();
        frames.push(make_frame(&ids, &nodes).await?);
        println!();
    }
    let id0 = nodes[0].0;
    // tell the partitioned nodes about id0
    for node in &nodes[k..] {
        let (_, (_, api)) = node;
        api.nodes_seen(&[id0]).await.ok();
    }
    let mut frames = Frames::new(n);
    for i in 0..30 {
        tokio::time::sleep(Duration::from_secs(1)).await;
        plot_random_lookup_stats(&format!("partition_1k-{}-", i + 10), &nodes, 100)
            .await
            .ok();
        let last_id = nodes.last().unwrap().0;
        let mut knows_last_id = 0;
        for (_, (_, api)) in nodes.iter() {
            let routing_table = api.get_routing_table().await?;
            knows_last_id += routing_table
                .iter()
                .map(|peers| peers.iter().filter(|x| x.id == last_id).count())
                .sum::<usize>();
        }
        frames.data.push(make_frame(&ids, &nodes).await?);
        println!("Nodes that know about last_id: {knows_last_id}");
    }
    frames.make_gif(100, "img/partition_1k.gif")?;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn remove_1k() -> TestResult<()> {
    let n = 1000;
    let k = 900;
    let seed = 0;
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    // all nodes have all the strategies enabled
    let config = Config::persistent()
        .self_lookup_strategy(SelfLookupStrategy {
            interval: Duration::from_secs(1),
        })
        .random_lookup_strategy(RandomLookupStrategy {
            interval: Duration::from_secs(1),
            blended: false,
        })
        .candidate_lookup_strategy(CandidateLookupStrategy {
            max_lookups: 1,
            interval: Duration::from_secs(1),
        });
    let (nodes, clients) = create_nodes_and_clients(&ids, next_n(20), config).await;
    let mut frames = Frames {
        data: Vec::new(),
        stride: n,
    };
    for _i in 0..10 {
        tokio::time::sleep(Duration::from_secs(1)).await;
        frames.data.push(make_frame(&ids, &nodes).await?);
        println!();
    }
    for id in &ids[k..] {
        clients.lock().unwrap().remove(id);
    }
    for _i in 0..40 {
        tokio::time::sleep(Duration::from_secs(1)).await;
        frames.data.push(make_frame(&ids, &nodes).await?);
        println!();
    }
    frames.make_gif(100, "img/remove_1k.gif")?;
    Ok(())
}

async fn trigger_random_lookups(nodes: &Nodes) {
    stream::iter(nodes.iter())
        .for_each_concurrent(512, |(_, (_, api))| async move {
            api.random_lookup().await;
        })
        .await;
}

/// Compares random and blended random lookups with 1000 nodes over 60 seconds
#[tokio::test(flavor = "multi_thread")]
async fn random_vs_blended_1k() -> TestResult<()> {
    let n = 1000;
    let seed = 0;
    let n_frames = 50;
    let secrets = create_secrets(seed, n);
    let ids = create_node_ids(&secrets);
    let random = {
        // all nodes have all the strategies enabled
        let config = Config::persistent().random_lookup_strategy(RandomLookupStrategy {
            // disable time based lookups
            interval: Duration::MAX,
            blended: false,
        });
        let nodes = create_nodes(&ids, next_n(20), config).await;
        let mut frames = Frames {
            data: Vec::new(),
            stride: n,
        };
        for _i in 0..n_frames {
            frames.data.push(make_frame(&ids, &nodes).await?);
            trigger_random_lookups(&nodes).await;
            println!();
        }
        frames
    };
    let blended = {
        // all nodes have all the strategies enabled
        let config = Config::persistent()
            // .self_lookup_strategy(SelfLookupStrategy {
            //     interval: Duration::from_secs(1),
            // })
            .random_lookup_strategy(RandomLookupStrategy {
                // disable time based lookups
                interval: Duration::MAX,
                blended: true,
            });
        let nodes = create_nodes(&ids, next_n(20), config).await;
        let mut frames = Frames {
            data: Vec::new(),
            stride: n,
        };
        for _i in 0..n_frames {
            frames.data.push(make_frame(&ids, &nodes).await?);
            trigger_random_lookups(&nodes).await;
            println!();
        }
        frames
    };
    let perfect = {
        // all nodes have all the strategies enabled
        let config = Config::persistent()
            // .self_lookup_strategy(SelfLookupStrategy {
            //     interval: Duration::from_secs(1),
            // })
            .random_lookup_strategy(RandomLookupStrategy {
                // disable time based lookups
                interval: Duration::MAX,
                blended: true,
            });
        let nodes = create_nodes(&ids, next_n(20), config).await;
        init_routing_tables(&nodes, &ids, Some(seed)).await.ok();
        let mut frames = Frames {
            data: Vec::new(),
            stride: n,
        };
        for _i in 0..n_frames {
            frames.data.push(make_frame(&ids, &nodes).await?);
            // trigger_random_lookups(&nodes).await;
            println!();
        }
        frames
    };
    let frames = Frames::side_by_side(&[&random, &blended, &perfect], 20)?;
    frames.make_gif(100, "img/random_vs_blended_1k.gif")?;
    Ok(())
}