ant_bootstrap/
config.rs

1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use crate::error::{Error, Result};
10use std::{
11    path::{Path, PathBuf},
12    time::Duration,
13};
14
15/// The duration since last)seen before removing the address of a Peer.
16const ADDR_EXPIRY_DURATION: Duration = Duration::from_secs(24 * 60 * 60); // 24 hours
17
18/// Maximum peers to store
19const MAX_PEERS: usize = 1500;
20
21/// Maximum number of addresses to store for a Peer
22const MAX_ADDRS_PER_PEER: usize = 3;
23
24// Min time until we save the bootstrap cache to disk. 5 mins
25const MIN_BOOTSTRAP_CACHE_SAVE_INTERVAL: Duration = Duration::from_secs(5 * 60);
26
27// Max time until we save the bootstrap cache to disk. 3 hours
28const MAX_BOOTSTRAP_CACHE_SAVE_INTERVAL: Duration = Duration::from_secs(3 * 60 * 60);
29
30/// Configuration for the bootstrap cache
31#[derive(Clone, Debug)]
32pub struct BootstrapCacheConfig {
33    /// The duration since last)seen before removing the address of a Peer.
34    pub addr_expiry_duration: Duration,
35    /// Enable backwards compatibility while writing the cache file.
36    /// This will write the cache file in all versions of the cache file format.
37    pub backwards_compatible_writes: bool,
38    /// The directory to load and store the bootstrap cache. If not provided, the default path will be used.
39    pub cache_dir: PathBuf,
40    /// The cache save scaling factor. We start with the min_cache_save_duration and scale it up to the max_cache_save_duration.
41    pub cache_save_scaling_factor: u64,
42    /// Flag to disable writing to the cache file
43    pub disable_cache_writing: bool,
44    /// If set to true, the cache filename will be suffixed with "_local"
45    pub local: bool,
46    /// The max time duration until we save the bootstrap cache to disk.
47    pub max_cache_save_duration: Duration,
48    /// Maximum number of peers to keep in the cache
49    pub max_peers: usize,
50    /// Maximum number of addresses stored per peer.
51    pub max_addrs_per_peer: usize,
52    /// The min time duration until we save the bootstrap cache to disk.
53    pub min_cache_save_duration: Duration,
54}
55
56impl BootstrapCacheConfig {
57    /// Creates a new BootstrapConfig with default settings
58    pub fn new(local: bool) -> Result<Self> {
59        Ok(Self {
60            local,
61            cache_dir: default_cache_dir()?,
62            ..Self::empty()
63        })
64    }
65
66    /// Creates a new BootstrapConfig with empty settings
67    pub fn empty() -> Self {
68        Self {
69            addr_expiry_duration: ADDR_EXPIRY_DURATION,
70            backwards_compatible_writes: false,
71            max_peers: MAX_PEERS,
72            max_addrs_per_peer: MAX_ADDRS_PER_PEER,
73            cache_dir: PathBuf::new(),
74            disable_cache_writing: false,
75            local: false,
76            min_cache_save_duration: MIN_BOOTSTRAP_CACHE_SAVE_INTERVAL,
77            max_cache_save_duration: MAX_BOOTSTRAP_CACHE_SAVE_INTERVAL,
78            cache_save_scaling_factor: 2,
79        }
80    }
81
82    /// Set backwards compatible writes
83    pub fn with_backwards_compatible_writes(mut self, enable: bool) -> Self {
84        self.backwards_compatible_writes = enable;
85        self
86    }
87
88    /// Set the local flag
89    pub fn with_local(mut self, enable: bool) -> Self {
90        self.local = enable;
91        self
92    }
93
94    /// Set a new addr expiry duration
95    pub fn with_addr_expiry_duration(mut self, duration: Duration) -> Self {
96        self.addr_expiry_duration = duration;
97        self
98    }
99
100    /// Update the config with a custom cache file path
101    pub fn with_cache_dir<P: AsRef<Path>>(mut self, path: P) -> Self {
102        self.cache_dir = path.as_ref().to_path_buf();
103        self
104    }
105
106    /// Sets the maximum number of peers
107    pub fn with_max_peers(mut self, max_peers: usize) -> Self {
108        self.max_peers = max_peers;
109        self
110    }
111
112    /// Sets the maximum number of addresses for a single peer.
113    pub fn with_addrs_per_peer(mut self, max_addrs: usize) -> Self {
114        self.max_addrs_per_peer = max_addrs;
115        self
116    }
117
118    /// Sets the flag to disable writing to the cache file
119    pub fn with_disable_cache_writing(mut self, disable: bool) -> Self {
120        self.disable_cache_writing = disable;
121        self
122    }
123}
124
125/// Returns the default dir that should contain the bootstrap cache file
126fn default_cache_dir() -> Result<PathBuf> {
127    let dir = dirs_next::data_dir()
128        .ok_or_else(|| Error::CouldNotObtainDataDir)?
129        .join("autonomi")
130        .join("bootstrap_cache");
131
132    std::fs::create_dir_all(&dir)?;
133
134    Ok(dir)
135}