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 = 6;
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    /// Maximum number of peers to keep in the cache
36    pub max_peers: usize,
37    /// Maximum number of addresses stored per peer.
38    pub max_addrs_per_peer: usize,
39    /// Path to the bootstrap cache file
40    pub cache_file_path: PathBuf,
41    /// Flag to disable writing to the cache file
42    pub disable_cache_writing: bool,
43    /// The min time duration until we save the bootstrap cache to disk.
44    pub min_cache_save_duration: Duration,
45    /// The max time duration until we save the bootstrap cache to disk.
46    pub max_cache_save_duration: Duration,
47    /// The cache save scaling factor. We start with the min_cache_save_duration and scale it up to the max_cache_save_duration.
48    pub cache_save_scaling_factor: u64,
49}
50
51impl BootstrapCacheConfig {
52    /// Creates a new BootstrapConfig with default settings
53    ///
54    /// When `local` is set to true, a different cache file name is used.
55    /// I.e. the file name will include `_local_` in the name.
56    pub fn default_config(local: bool) -> Result<Self> {
57        let cache_file_path = if local {
58            default_cache_path_local()?
59        } else {
60            default_cache_path()?
61        };
62        Ok(Self {
63            cache_file_path,
64            ..Self::empty()
65        })
66    }
67
68    /// Creates a new BootstrapConfig with empty settings
69    pub fn empty() -> Self {
70        Self {
71            addr_expiry_duration: ADDR_EXPIRY_DURATION,
72            max_peers: MAX_PEERS,
73            max_addrs_per_peer: MAX_ADDRS_PER_PEER,
74            cache_file_path: PathBuf::new(),
75            disable_cache_writing: 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 a new addr expiry duration
83    pub fn with_addr_expiry_duration(mut self, duration: Duration) -> Self {
84        self.addr_expiry_duration = duration;
85        self
86    }
87
88    /// Update the config with a custom cache file path
89    pub fn with_cache_path<P: AsRef<Path>>(mut self, path: P) -> Self {
90        self.cache_file_path = path.as_ref().to_path_buf();
91        self
92    }
93
94    /// Sets the maximum number of peers
95    pub fn with_max_peers(mut self, max_peers: usize) -> Self {
96        self.max_peers = max_peers;
97        self
98    }
99
100    /// Sets the maximum number of addresses for a single peer.
101    pub fn with_addrs_per_peer(mut self, max_addrs: usize) -> Self {
102        self.max_addrs_per_peer = max_addrs;
103        self
104    }
105
106    /// Sets the flag to disable writing to the cache file
107    pub fn with_disable_cache_writing(mut self, disable: bool) -> Self {
108        self.disable_cache_writing = disable;
109        self
110    }
111}
112
113/// Returns the default path for the bootstrap cache file
114fn default_cache_path() -> Result<PathBuf> {
115    Ok(default_cache_dir()?.join(cache_file_name()))
116}
117/// Returns the default path for the bootstrap cache file that is used for
118/// local networks
119fn default_cache_path_local() -> Result<PathBuf> {
120    Ok(default_cache_dir()?.join(cache_file_name_local()))
121}
122
123/// Returns the default dir that should contain the bootstrap cache file
124fn default_cache_dir() -> Result<PathBuf> {
125    let dir = dirs_next::data_dir()
126        .ok_or_else(|| Error::CouldNotObtainDataDir)?
127        .join("autonomi")
128        .join("bootstrap_cache");
129
130    std::fs::create_dir_all(&dir)?;
131
132    Ok(dir)
133}
134
135/// Returns the name of the cache file
136pub fn cache_file_name() -> String {
137    format!("bootstrap_cache_{}.json", crate::get_network_version())
138}
139
140/// Returns the name of the cache file for local networks
141pub fn cache_file_name_local() -> String {
142    format!(
143        "bootstrap_cache_local_{}.json",
144        crate::get_network_version()
145    )
146}