ant-bootstrap 0.2.13

Bootstrap functionality for Autonomi
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
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

pub mod cache_data_v0;
pub mod cache_data_v1;

use crate::{BootstrapConfig, Error, Result, craft_valid_multiaddr};
use libp2p::{Multiaddr, PeerId, multiaddr::Protocol};
use rand::Rng;
use std::{collections::HashSet, fs, sync::Arc, time::Duration};
use tokio::sync::RwLock;
use tracing::Instrument;

pub type CacheDataLatest = cache_data_v1::CacheData;
pub const CACHE_DATA_VERSION_LATEST: u32 = cache_data_v1::CacheData::CACHE_DATA_VERSION;

#[derive(Clone, Debug)]
pub struct BootstrapCacheStore {
    /// Configuration for the cache store
    pub(crate) config: Arc<BootstrapConfig>,
    /// In-memory cache data
    pub(crate) data: Arc<RwLock<CacheDataLatest>>,
    /// List of peers to remove from the fs cache, would be done during the next sync_and_flush_to_disk call
    pub(crate) to_remove: Arc<RwLock<HashSet<PeerId>>>,
}

impl BootstrapCacheStore {
    pub fn config(&self) -> &BootstrapConfig {
        &self.config
    }

    /// Create an empty CacheStore with the given configuration
    pub fn new(config: BootstrapConfig) -> Result<Self> {
        info!("Creating new CacheStore with config: {:?}", config);

        // Create cache directory if it doesn't exist
        if !config.cache_dir.exists() {
            info!(
                "Attempting to create cache directory at {:?}",
                config.cache_dir
            );
            fs::create_dir_all(&config.cache_dir).inspect_err(|err| {
                warn!(
                    "Failed to create cache directory at {:?}: {err}",
                    config.cache_dir
                );
            })?;
        }

        let store = Self {
            config: Arc::new(config),
            data: Arc::new(RwLock::new(CacheDataLatest::default())),
            to_remove: Arc::new(RwLock::new(HashSet::new())),
        };

        Ok(store)
    }

    pub async fn peer_count(&self) -> usize {
        self.data.read().await.peers.len()
    }

    pub async fn get_all_addrs(&self) -> Vec<Multiaddr> {
        self.data.read().await.get_all_addrs().cloned().collect()
    }

    /// Queue a peer for removal from the cache. The actual removal will happen during the next sync_and_flush_to_disk call.
    pub async fn queue_remove_peer(&self, peer_id: &PeerId) {
        self.to_remove.write().await.insert(*peer_id);
    }

    /// Add an address to the cache. Note that the address must have a valid peer ID.
    ///
    /// We do not write P2pCircuit addresses to the cache.
    pub async fn add_addr(&self, addr: Multiaddr) {
        if addr.iter().any(|p| matches!(p, Protocol::P2pCircuit)) {
            return;
        }
        let Some(addr) = craft_valid_multiaddr(&addr, false) else {
            return;
        };
        let peer_id = match addr.iter().find(|p| matches!(p, Protocol::P2p(_))) {
            Some(Protocol::P2p(id)) => id,
            _ => return,
        };

        debug!("Adding addr to bootstrap cache: {addr}");

        self.data.write().await.add_peer(
            peer_id,
            [addr].iter(),
            self.config.max_addrs_per_cached_peer,
            self.config.max_cached_peers,
        );
    }

    /// Load cache data from disk
    /// Make sure to have clean addrs inside the cache as we don't call craft_valid_multiaddr
    pub fn load_cache_data(cfg: &BootstrapConfig) -> Result<CacheDataLatest> {
        // try loading latest first
        match cache_data_v1::CacheData::read_from_file(
            &cfg.cache_dir,
            &Self::cache_file_name(cfg.local),
        ) {
            Ok(mut data) => {
                while data.peers.len() > cfg.max_cached_peers {
                    data.peers.pop_back();
                }
                return Ok(data);
            }
            Err(err) => {
                warn!("Failed to load cache data from latest version: {err}");
            }
        }

        // Try loading older version
        match cache_data_v0::CacheData::read_from_file(
            &cfg.cache_dir,
            &Self::cache_file_name(cfg.local),
        ) {
            Ok(data) => {
                warn!("Loaded cache data from older version, upgrading to latest version");
                let mut data: CacheDataLatest = data.into();
                while data.peers.len() > cfg.max_cached_peers {
                    data.peers.pop_back();
                }

                Ok(data)
            }
            Err(err) => {
                warn!("Failed to load cache data from older version: {err}");
                Err(Error::FailedToParseCacheData)
            }
        }
    }

    /// Flush the cache to disk after syncing with the CacheData from the file.
    ///
    /// Note: This clears the data in memory after writing to disk.
    pub async fn sync_and_flush_to_disk(&self) -> Result<()> {
        if self.config.disable_cache_writing {
            info!("Cache writing is disabled, skipping sync to disk");
            return Ok(());
        }

        if self.data.read().await.peers.is_empty() {
            info!("Cache is empty, skipping sync and flush to disk");
            return Ok(());
        }

        info!(
            "Flushing cache to disk, with data containing: {} peers",
            self.data.read().await.peers.len(),
        );

        if let Ok(data_from_file) = Self::load_cache_data(&self.config) {
            self.data.write().await.sync(
                &data_from_file,
                self.config.max_addrs_per_cached_peer,
                self.config.max_cached_peers,
            );
        } else {
            warn!("Failed to load cache data from file, overwriting with new data");
        }

        // Remove queued peers
        let to_remove: Vec<PeerId> = self.to_remove.write().await.drain().collect();
        if !to_remove.is_empty() {
            info!("Removing {} peers from cache", to_remove.len());
            for peer_id in to_remove {
                self.data.write().await.remove_peer(&peer_id);
            }
        } else {
            debug!("No peers queued for removal from cache");
        }

        self.write().await.inspect_err(|e| {
            error!("Failed to save cache to disk: {e}");
        })?;

        // Flush after writing
        debug!("Clearing in-memory cache data after flush to disk");
        self.data.write().await.peers.clear();

        Ok(())
    }

    /// Write the cache to disk atomically. This will overwrite the existing cache file, use sync_and_flush_to_disk to
    /// sync with the file first.
    pub async fn write(&self) -> Result<()> {
        if self.config.disable_cache_writing {
            info!("Cache writing is disabled, skipping sync to disk");
            return Ok(());
        }

        let filename = Self::cache_file_name(self.config.local);

        self.data
            .write()
            .await
            .write_to_file(&self.config.cache_dir, &filename)?;

        if self.config.backwards_compatible_writes {
            let data = self.data.read().await;
            cache_data_v0::CacheData::from(&*data)
                .write_to_file(&self.config.cache_dir, &filename)?;
        }

        Ok(())
    }

    /// Returns the name of the cache filename based on the local flag
    pub fn cache_file_name(local: bool) -> String {
        if local {
            format!(
                "bootstrap_cache_local_{}.json",
                crate::get_network_version()
            )
        } else {
            format!("bootstrap_cache_{}.json", crate::get_network_version())
        }
    }

    /// Runs the sync_and_flush_to_disk method periodically
    /// This is useful for keeping the cache up-to-date without blocking the main thread.
    pub(crate) fn sync_and_flush_periodically(&self) -> tokio::task::JoinHandle<()> {
        let store = self.clone();

        let current_span = tracing::Span::current();
        tokio::spawn(async move {
            // add a variance of 10% to the interval, to avoid all nodes writing to disk at the same time.
            let mut sleep_interval =
                duration_with_variance(store.config.min_cache_save_duration, 10);
            if store.config.disable_cache_writing {
                info!("Cache writing is disabled, skipping periodic sync and flush task");
                return;
            }
            info!("Starting periodic cache sync and flush task, first sync in {sleep_interval:?}");

            loop {
                tokio::time::sleep(sleep_interval).await;
                if let Err(e) = store.sync_and_flush_to_disk().await {
                    error!("Failed to sync and flush cache to disk: {e}");
                }
                // add a variance of 1% to the max interval to avoid all nodes writing to disk at the same time.
                let max_cache_save_duration =
                    duration_with_variance(store.config.max_cache_save_duration, 1);

                let new_interval = sleep_interval
                    .checked_mul(store.config.cache_save_scaling_factor)
                    .unwrap_or(max_cache_save_duration);
                sleep_interval = new_interval.min(max_cache_save_duration);
                info!("Cache synced and flushed to disk successfully - next sync in {sleep_interval:?}");
            }
        }.instrument(current_span))
    }
}

/// Returns a new duration that is within +/- variance of the provided duration.
fn duration_with_variance(duration: Duration, variance: u32) -> Duration {
    let variance = duration.as_secs() as f64 * (variance as f64 / 100.0);

    let random_adjustment = Duration::from_secs(rand::thread_rng().gen_range(0..variance as u64));
    if random_adjustment.as_secs().is_multiple_of(2) {
        duration.saturating_sub(random_adjustment)
    } else {
        duration.saturating_add(random_adjustment)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        cache_store::{cache_data_v0, cache_data_v1},
        multiaddr_get_peer_id,
    };
    use libp2p::{Multiaddr, PeerId};
    use std::{collections::HashSet, time::SystemTime};
    use tempfile::TempDir;
    use tokio::{
        task,
        time::{Duration, sleep},
    };

    #[tokio::test]
    async fn test_duration_variance_fn() {
        let duration = Duration::from_secs(150);
        let variance = 10;
        let expected_variance = Duration::from_secs(15); // 10% of 150
        for _ in 0..10000 {
            let new_duration = duration_with_variance(duration, variance);
            println!("new_duration: {new_duration:?}");
            if new_duration < duration - expected_variance
                || new_duration > duration + expected_variance
            {
                panic!("new_duration: {new_duration:?} is not within the expected range",);
            }
        }
    }

    fn temp_config(dir: &TempDir) -> BootstrapConfig {
        BootstrapConfig::default().with_cache_dir(dir.path())
    }

    #[tokio::test]
    async fn test_empty_cache() {
        let dir = TempDir::new().expect("temp dir");
        let config = temp_config(&dir);
        let cache = BootstrapCacheStore::new(config.clone()).expect("create cache");

        cache.write().await.expect("write empty cache");
        let loaded = BootstrapCacheStore::load_cache_data(&config).expect("load cache");
        assert!(loaded.peers.is_empty());
    }

    #[tokio::test]
    async fn test_max_peer_limit_enforcement() {
        let dir = TempDir::new().expect("temp dir");
        let config = BootstrapConfig::default()
            .with_cache_dir(dir.path())
            .with_max_cached_peers(3);
        let cache = BootstrapCacheStore::new(config.clone()).expect("create cache");

        let samples = [
            "/ip4/127.0.0.1/udp/1200/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UERE",
            "/ip4/127.0.0.2/udp/1201/quic-v1/p2p/12D3KooWD2aV1f3qkhggzEFaJ24CEFYkSdZF5RKoMLpU6CwExYV5",
            "/ip4/127.0.0.3/udp/1202/quic-v1/p2p/12D3KooWHehYgXKLxsXjzFzDqMLKhcAVc4LaktnT7Zei1G2zcpJB",
            "/ip4/127.0.0.4/udp/1203/quic-v1/p2p/12D3KooWQF3NMWHRmMQBY8GVdpQh1V6TFYuQqZkKKvYE7yCS6fYK",
            "/ip4/127.0.0.5/udp/1204/quic-v1/p2p/12D3KooWRi6wF7yxWLuPSNskXc6kQ5cJ6eaymeMbCRdTnMesPgFx",
        ];

        let mut recorded = Vec::new();
        for addr_str in samples {
            let addr: Multiaddr = addr_str.parse().unwrap();
            recorded.push(addr.clone());
            cache.add_addr(addr).await;
            sleep(Duration::from_millis(5)).await;
        }

        let current = cache.get_all_addrs().await;
        assert_eq!(current.len(), 3);
        assert!(current.iter().all(|addr| recorded[2..].contains(addr)));

        cache.write().await.expect("persist cache");
        let persisted = BootstrapCacheStore::load_cache_data(&config)
            .expect("load persisted")
            .get_all_addrs()
            .cloned()
            .collect::<Vec<_>>();
        assert_eq!(persisted.len(), 3);
        assert!(persisted.iter().all(|addr| recorded[2..].contains(addr)));
    }

    #[tokio::test]
    async fn test_queued_peer_not_removed_until_flush() {
        let dir = TempDir::new().expect("temp dir");
        let config = temp_config(&dir);
        let cache = BootstrapCacheStore::new(config.clone()).expect("create cache");

        let addr: Multiaddr = "/ip4/127.0.0.6/udp/1205/quic-v1/p2p/12D3KooWQnE7zXkVUEGBnJtNfR88Ujz4ezgm6bVnkvxHCzhF7S5S"
            .parse()
            .unwrap();
        cache.add_addr(addr.clone()).await;
        cache.write().await.expect("persist initial cache state");
        let peer_id = multiaddr_get_peer_id(&addr).expect("peer id");
        cache.queue_remove_peer(&peer_id).await;
        let addrs_before_flush = cache.get_all_addrs().await;
        assert_eq!(
            addrs_before_flush.len(),
            1,
            "peer should remain available before flush"
        );
        assert_eq!(
            addrs_before_flush[0], addr,
            "cached address should match the inserted peer"
        );

        let persisted_before_flush =
            BootstrapCacheStore::load_cache_data(&config).expect("load persisted cache");
        let persisted_before_flush: Vec<_> =
            persisted_before_flush.get_all_addrs().cloned().collect();
        assert!(
            persisted_before_flush.iter().any(|stored| stored == &addr),
            "queued removal must not affect persisted cache before flush"
        );

        cache
            .sync_and_flush_to_disk()
            .await
            .expect("flush cache to disk");

        let persisted_after_flush =
            BootstrapCacheStore::load_cache_data(&config).expect("load persisted cache");
        let persisted_after_flush: Vec<_> =
            persisted_after_flush.get_all_addrs().cloned().collect();
        assert!(
            persisted_after_flush.iter().all(|stored| stored != &addr),
            "peer should be absent from persisted cache after flush"
        );
    }

    #[tokio::test]
    async fn test_queued_peer_removal_queue_drained_after_flush() {
        let dir = TempDir::new().expect("temp dir");
        let config = temp_config(&dir);
        let cache = BootstrapCacheStore::new(config.clone()).expect("create cache");

        let addr: Multiaddr = "/ip4/127.0.0.1/udp/8080/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UERE"
            .parse()
            .unwrap();
        cache.add_addr(addr.clone()).await;
        cache.write().await.expect("persist initial cache state");

        let peer_id = multiaddr_get_peer_id(&addr).expect("peer id");
        cache.queue_remove_peer(&peer_id).await;
        cache
            .sync_and_flush_to_disk()
            .await
            .expect("flush queued removals");
        let persisted_after_removal =
            BootstrapCacheStore::load_cache_data(&config).expect("load cache data");
        assert!(
            persisted_after_removal.get_all_addrs().next().is_none(),
            "peer should be removed from persisted cache after flush"
        );

        cache.add_addr(addr.clone()).await;

        cache
            .sync_and_flush_to_disk()
            .await
            .expect("flush cache after re-adding peer");
        let persisted_after_re_add =
            BootstrapCacheStore::load_cache_data(&config).expect("load cache data");
        let persisted_after_re_add: Vec<_> =
            persisted_after_re_add.get_all_addrs().cloned().collect();
        assert_eq!(
            persisted_after_re_add.len(),
            1,
            "re-added peer should persist after subsequent flush"
        );
        assert_eq!(
            persisted_after_re_add[0], addr,
            "persisted address should match the re-added peer"
        );
    }

    #[tokio::test]
    async fn test_cache_file_corruption() {
        let dir = TempDir::new().expect("temp dir");
        let cache_dir = dir.path();
        let config = BootstrapConfig::default().with_cache_dir(cache_dir);
        let cache = BootstrapCacheStore::new(config.clone()).expect("create cache");

        let addr: Multiaddr = "/ip4/127.0.0.1/udp/8080/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UERE"
            .parse()
            .unwrap();
        cache.add_addr(addr).await;
        cache.write().await.expect("write cache");

        let corrupted_path = cache_dir
            .join(format!(
                "version_{}",
                cache_data_v1::CacheData::CACHE_DATA_VERSION
            ))
            .join(BootstrapCacheStore::cache_file_name(false));
        std::fs::write(&corrupted_path, "{not valid json}").expect("corrupt file");

        let load_err = BootstrapCacheStore::load_cache_data(&config);
        assert!(load_err.is_err(), "loading corrupted cache should error");

        let new_store =
            BootstrapCacheStore::new(config.clone()).expect("create store after corruption");
        assert_eq!(
            new_store.peer_count().await,
            0,
            "new cache should start empty after corruption"
        );
        new_store.write().await.expect("write clean cache");

        let reloaded = BootstrapCacheStore::load_cache_data(&config).expect("reload cache");
        assert!(
            reloaded.peers.is_empty(),
            "cache data should be empty after regeneration"
        );
    }

    #[tokio::test]
    async fn test_max_addrs_per_peer() {
        let dir = TempDir::new().expect("temp dir");
        let config = BootstrapConfig::default()
            .with_cache_dir(dir.path())
            .with_max_addrs_per_cached_peer(2);
        let cache = BootstrapCacheStore::new(config.clone()).expect("create cache");

        let peer_id = "12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UERE";
        for octet in 1..=4 {
            let addr: Multiaddr = format!("/ip4/127.0.0.{octet}/udp/8080/quic-v1/p2p/{peer_id}")
                .parse()
                .unwrap();
            cache.add_addr(addr).await;
        }

        cache.write().await.expect("write cache");
        let reloaded = BootstrapCacheStore::load_cache_data(&config).expect("load cache");
        let collected: Vec<_> = reloaded.get_all_addrs().cloned().collect();
        assert!(
            collected.len() <= 2,
            "should honor max_addrs_per_peer limit"
        );
    }

    #[tokio::test]
    async fn test_concurrent_cache_access() {
        let dir = TempDir::new().expect("temp dir");
        let cache_dir = dir.path().to_path_buf();
        let config = BootstrapConfig::default().with_cache_dir(cache_dir.clone());

        let mut handles = Vec::new();
        for idx in 0..5 {
            let config_clone = config.clone();
            handles.push(task::spawn(async move {
                let store = BootstrapCacheStore::new(config_clone)?;
                let addr: Multiaddr = format!(
                    "/ip4/127.0.0.{}/udp/8080/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UER{}",
                    idx + 1,
                    idx + 1
                )
                .parse()
                .unwrap();
                store.add_addr(addr).await;
                sleep(Duration::from_millis(10)).await;
                store.sync_and_flush_to_disk().await
            }));
        }

        for handle in handles {
            let result = handle.await.expect("task join");
            result.expect("task result");
        }

        let final_store = BootstrapCacheStore::new(config).expect("create final store");
        let loaded = BootstrapCacheStore::load_cache_data(final_store.config()).expect("load");
        assert_eq!(loaded.peers.len(), 5, "should persist peers from all tasks");
    }

    #[tokio::test]
    async fn test_cache_sync_functionality() {
        let dir = TempDir::new().expect("temp dir");
        let cache_dir = dir.path();

        let config = BootstrapConfig::default().with_cache_dir(cache_dir);
        let first_store = BootstrapCacheStore::new(config.clone()).expect("create cache");
        let addr1: Multiaddr = "/ip4/127.0.0.1/udp/8080/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UERE"
            .parse()
            .unwrap();
        first_store.add_addr(addr1.clone()).await;
        first_store.write().await.expect("write first cache");

        let second_store = BootstrapCacheStore::new(config.clone()).expect("create cache");
        let addr2: Multiaddr = "/ip4/127.0.0.2/udp/8080/quic-v1/p2p/12D3KooWD2aV1f3qkhggzEFaJ24CEFYkSdZF5RKoMLpU6CwExYV5"
            .parse()
            .unwrap();
        second_store.add_addr(addr2.clone()).await;
        second_store
            .sync_and_flush_to_disk()
            .await
            .expect("sync cache");

        let file_name = BootstrapCacheStore::cache_file_name(false);
        let cache_path = cache_data_v1::CacheData::cache_file_path(cache_dir, &file_name);
        let cache_content = std::fs::read_to_string(&cache_path).expect("read cache file");
        assert!(
            cache_content.contains(&addr1.to_string())
                && cache_content.contains(&addr2.to_string()),
            "cache content should include both addresses"
        );

        let check_store = BootstrapCacheStore::new(config).expect("create verifying store");
        let loaded = BootstrapCacheStore::load_cache_data(check_store.config()).expect("load");
        let addrs: Vec<_> = loaded.get_all_addrs().cloned().collect();
        assert!(
            addrs
                .iter()
                .any(|addr| addr.to_string() == addr1.to_string())
                && addrs
                    .iter()
                    .any(|addr| addr.to_string() == addr2.to_string()),
            "both addresses should be present after sync"
        );
    }

    #[tokio::test]
    async fn test_sync_duplicates_overlapping_peers() {
        let mut cache1 = CacheDataLatest::default();
        let mut cache2 = CacheDataLatest::default();

        let peers: Vec<PeerId> = (0..3).map(|_| PeerId::random()).collect();
        let addr1: Multiaddr = "/ip4/127.0.0.1/udp/8080/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UERE"
            .parse()
            .unwrap();
        let addr2: Multiaddr = "/ip4/127.0.0.2/udp/8081/quic-v1/p2p/12D3KooWD2aV1f3qkhggzEFaJ24CEFYkSdZF5RKoMLpU6CwExYV5"
            .parse()
            .unwrap();
        let addr3: Multiaddr = "/ip4/127.0.0.3/udp/8082/quic-v1/p2p/12D3KooWCKCeqLPSgMnDjyFsJuWqREDtKNHx1JEBiwxME7Zdw68n"
            .parse()
            .unwrap();

        cache1.add_peer(peers[0], [addr1.clone()].iter(), 10, 10);
        cache1.add_peer(peers[1], [addr2.clone()].iter(), 10, 10);
        cache2.add_peer(peers[1], [addr2.clone()].iter(), 10, 10);
        cache2.add_peer(peers[2], [addr3.clone()].iter(), 10, 10);

        cache1.sync(&cache2, 10, 10);
        let result: HashSet<_> = cache1
            .get_all_addrs()
            .map(|addr| addr.to_string())
            .collect();
        assert_eq!(result.len(), 3, "should merge and deduplicate addresses");
        assert!(result.contains(&addr1.to_string()));
        assert!(result.contains(&addr2.to_string()));
        assert!(result.contains(&addr3.to_string()));
    }

    #[tokio::test]
    async fn test_sync_at_limit_overwrites_unique_peers() {
        let mut cache1 = CacheDataLatest::default();
        let mut cache2 = CacheDataLatest::default();

        let addrs: Vec<Multiaddr> = (1..=7)
            .map(|i| {
                format!(
                    "/ip4/127.0.0.1/udp/808{i}/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UER{i}"
                )
                .parse()
                .unwrap()
            })
            .collect();
        let peers: Vec<_> = addrs
            .iter()
            .map(|addr| match multiaddr_get_peer_id(addr) {
                Some(peer) => peer,
                None => panic!("address missing peer id"),
            })
            .collect();

        for idx in 0..5 {
            cache1.add_peer(peers[idx], [addrs[idx].clone()].iter(), 10, 5);
        }
        for idx in 2..7 {
            cache2.add_peer(peers[idx], [addrs[idx].clone()].iter(), 10, 5);
        }

        cache1.sync(&cache2, 10, 5);
        let after: HashSet<_> = cache1.peers.iter().map(|(peer_id, _)| *peer_id).collect();
        assert_eq!(cache1.peers.len(), 5, "should respect max peers");
        assert!(after.contains(&peers[0]));
        assert!(after.contains(&peers[1]));
    }

    #[tokio::test]
    async fn test_sync_other_at_limit_self_below_limit() {
        let mut cache1 = CacheDataLatest::default();
        let mut cache2 = CacheDataLatest::default();

        let addrs: Vec<Multiaddr> = (1..=7)
            .map(|i| {
                format!(
                    "/ip4/127.0.0.1/udp/908{i}/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UER{i}"
                )
                .parse()
                .unwrap()
            })
            .collect();
        let peers: Vec<_> = addrs
            .iter()
            .map(|addr| multiaddr_get_peer_id(addr).expect("peer id"))
            .collect();

        for idx in 0..2 {
            cache1.add_peer(peers[idx], [addrs[idx].clone()].iter(), 10, 5);
        }
        for idx in 2..7 {
            cache2.add_peer(peers[idx], [addrs[idx].clone()].iter(), 10, 5);
        }

        cache1.sync(&cache2, 10, 5);
        let after: HashSet<_> = cache1.peers.iter().map(|(peer_id, _)| *peer_id).collect();
        assert_eq!(cache1.peers.len(), 5);
        assert!(after.contains(&peers[0]));
        assert!(after.contains(&peers[1]));
    }

    #[tokio::test]
    async fn test_cache_version_upgrade() {
        let dir = TempDir::new().expect("temp dir");
        let cache_dir = dir.path();

        let mut v0_data = cache_data_v0::CacheData {
            peers: Default::default(),
            last_updated: SystemTime::now(),
            network_version: crate::get_network_version(),
        };
        let peer_id = PeerId::random();
        let addr: Multiaddr = "/ip4/127.0.0.1/udp/8080/quic-v1"
            .parse()
            .expect("parse addr");
        let boot_addr = cache_data_v0::BootstrapAddr {
            addr: addr.clone(),
            success_count: 1,
            failure_count: 0,
            last_seen: SystemTime::now(),
        };
        v0_data
            .peers
            .insert(peer_id, cache_data_v0::BootstrapAddresses(vec![boot_addr]));

        let config = BootstrapConfig::default().with_cache_dir(cache_dir);
        let filename = BootstrapCacheStore::cache_file_name(false);
        v0_data
            .write_to_file(cache_dir, &filename)
            .expect("write v0 cache");

        let upgraded = BootstrapCacheStore::load_cache_data(&config).expect("load cache");
        assert!(
            !upgraded.peers.is_empty(),
            "peers should carry over after upgrade"
        );
        assert!(
            upgraded.get_all_addrs().next().is_some(),
            "addresses should be preserved after upgrade"
        );
        assert_eq!(
            upgraded.cache_version,
            cache_data_v1::CacheData::CACHE_DATA_VERSION.to_string()
        );
    }

    #[tokio::test]
    async fn test_backwards_compatible_writes() {
        let dir = TempDir::new().expect("temp dir");
        let cache_dir = dir.path();

        let config = BootstrapConfig::default()
            .with_cache_dir(cache_dir)
            .with_backwards_compatible_writes(true);
        let cache = BootstrapCacheStore::new(config.clone()).expect("create cache");
        let addr: Multiaddr = "/ip4/127.0.0.1/udp/8080/quic-v1/p2p/12D3KooWRBhwfeP2Y4TCx1SM6s9rUoHhR5STiGwxBhgFRcw3UERE"
            .parse()
            .unwrap();
        cache.add_addr(addr).await;
        cache.write().await.expect("write cache");

        let filename = BootstrapCacheStore::cache_file_name(false);
        let v0_data =
            cache_data_v0::CacheData::read_from_file(cache_dir, &filename).expect("read v0");
        let v1_data =
            cache_data_v1::CacheData::read_from_file(cache_dir, &filename).expect("read v1");
        assert!(!v0_data.peers.is_empty(), "v0 data should be populated");
        assert!(!v1_data.peers.is_empty(), "v1 data should be populated");
    }

    #[tokio::test]
    async fn test_version_specific_file_paths() {
        let dir = TempDir::new().expect("temp dir");
        let cache_dir = dir.path();

        let filename = BootstrapCacheStore::cache_file_name(false);
        let v0_path = cache_data_v0::CacheData::cache_file_path(cache_dir, &filename);
        let v1_path = cache_data_v1::CacheData::cache_file_path(cache_dir, &filename);

        assert!(
            v1_path.to_string_lossy().contains(&format!(
                "version_{}",
                cache_data_v1::CacheData::CACHE_DATA_VERSION
            )),
            "v1 path should include version directory"
        );
        assert!(
            !v0_path.to_string_lossy().contains("version_"),
            "v0 path should not include version segment"
        );
    }
}