ant_bootstrap/
initial_peers.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::{
10    config::cache_file_name,
11    craft_valid_multiaddr, craft_valid_multiaddr_from_str,
12    error::{Error, Result},
13    BootstrapAddr, BootstrapCacheConfig, BootstrapCacheStore, ContactsFetcher,
14};
15use ant_protocol::version::{get_network_id, ALPHANET_ID, MAINNET_ID};
16use clap::Args;
17use libp2p::Multiaddr;
18use serde::{Deserialize, Serialize};
19use std::path::PathBuf;
20use url::Url;
21
22/// The name of the environment variable that can be used to pass peers to the node.
23pub const ANT_PEERS_ENV: &str = "ANT_PEERS";
24
25/// Configurations to fetch the initial peers which is used to bootstrap the network.
26/// This could optionally also be used as a command line argument struct.
27#[derive(Args, Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28pub struct InitialPeersConfig {
29    /// Set to indicate this is the first node in a new network
30    ///
31    /// If this argument is used, any others will be ignored because they do not apply to the first
32    /// node.
33    #[clap(long, default_value = "false")]
34    pub first: bool,
35    /// Addr(s) to use for bootstrap, in a 'multiaddr' format containing the peer ID.
36    ///
37    /// A multiaddr looks like
38    /// '/ip4/1.2.3.4/tcp/1200/tcp/p2p/12D3KooWRi6wF7yxWLuPSNskXc6kQ5cJ6eaymeMbCRdTnMesPgFx' where
39    /// `1.2.3.4` is the IP, `1200` is the port and the (optional) last part is the peer ID.
40    ///
41    /// This argument can be provided multiple times to connect to multiple peers.
42    ///
43    /// Alternatively, the `ANT_PEERS` environment variable can provide a comma-separated peer
44    /// list.
45    #[clap(
46        long = "peer",
47        value_name = "multiaddr",
48        value_delimiter = ',',
49        conflicts_with = "first"
50    )]
51    pub addrs: Vec<Multiaddr>,
52    /// Specify the URL to fetch the network contacts from.
53    ///
54    /// The URL can point to a text file containing Multiaddresses separated by newline character, or
55    /// a bootstrap cache JSON file.
56    #[clap(long, conflicts_with = "first", value_delimiter = ',')]
57    pub network_contacts_url: Vec<String>,
58    /// Set to indicate this is a local network.
59    #[clap(long, conflicts_with = "network_contacts_url", default_value = "false")]
60    pub local: bool,
61    /// Set to not load the bootstrap addresses from the local cache.
62    #[clap(long, default_value = "false")]
63    pub ignore_cache: bool,
64    /// The directory to load and store the bootstrap cache. If not provided, the default path will be used.
65    ///
66    /// The JSON filename will be derived automatically from the network ID
67    ///
68    /// The default location is platform specific:
69    ///  - Linux: $HOME/.local/share/autonomi/bootstrap_cache/bootstrap_cache_<network_id>.json
70    ///  - macOS: $HOME/Library/Application Support/autonomi/bootstrap_cache/bootstrap_cache_<network_id>.json
71    ///  - Windows: C:\Users\<username>\AppData\Roaming\autonomi\bootstrap_cache\bootstrap_cache_<network_id>.json
72    #[clap(long)]
73    pub bootstrap_cache_dir: Option<PathBuf>,
74}
75
76impl InitialPeersConfig {
77    /// Get bootstrap peers sorted by the failure rate.
78    ///
79    /// The peer with the lowest failure rate will be the first in the list.
80    pub async fn get_addrs(
81        &self,
82        config: Option<BootstrapCacheConfig>,
83        count: Option<usize>,
84    ) -> Result<Vec<Multiaddr>> {
85        Ok(self
86            .get_bootstrap_addr(config, count)
87            .await?
88            .into_iter()
89            .map(|addr| addr.addr)
90            .collect())
91    }
92
93    /// Get bootstrap peers sorted by the failure rate.
94    ///
95    /// The peer with the lowest failure rate will be the first in the list.
96    pub async fn get_bootstrap_addr(
97        &self,
98        config: Option<BootstrapCacheConfig>,
99        count: Option<usize>,
100    ) -> Result<Vec<BootstrapAddr>> {
101        // If this is the first node, return an empty list
102        if self.first {
103            info!("First node in network, no initial bootstrap peers");
104            return Ok(vec![]);
105        }
106
107        let mut bootstrap_addresses = vec![];
108
109        // Read from ANT_PEERS environment variable if present
110        bootstrap_addresses.extend(Self::read_bootstrap_addr_from_env());
111
112        if !bootstrap_addresses.is_empty() {
113            return Ok(bootstrap_addresses);
114        }
115
116        // Add addrs from arguments if present
117        for addr in &self.addrs {
118            if let Some(addr) = craft_valid_multiaddr(addr, false) {
119                info!("Adding addr from arguments: {addr}");
120                bootstrap_addresses.push(BootstrapAddr::new(addr));
121            } else {
122                warn!("Invalid multiaddress format from arguments: {addr}");
123            }
124        }
125
126        if let Some(count) = count {
127            if bootstrap_addresses.len() >= count {
128                bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
129                bootstrap_addresses.truncate(count);
130                info!("Returning early as enough bootstrap addresses are found");
131                return Ok(bootstrap_addresses);
132            }
133        }
134
135        // load from cache if present
136        if !self.ignore_cache {
137            let cfg = if let Some(config) = config {
138                Some(config)
139            } else {
140                BootstrapCacheConfig::default_config(self.local).ok()
141            };
142            if let Some(mut cfg) = cfg {
143                if let Some(file_path) = self.get_bootstrap_cache_path()? {
144                    cfg.cache_file_path = file_path;
145                }
146                info!("Loading bootstrap addresses from cache");
147                if let Ok(data) = BootstrapCacheStore::load_cache_data(&cfg) {
148                    let from_cache = data.peers.into_iter().filter_map(|(_, addrs)| {
149                        addrs
150                            .0
151                            .into_iter()
152                            .min_by_key(|addr| addr.failure_rate() as u64)
153                    });
154                    bootstrap_addresses.extend(from_cache);
155
156                    if let Some(count) = count {
157                        if bootstrap_addresses.len() >= count {
158                            bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
159                            bootstrap_addresses.truncate(count);
160                            info!("Returning early as enough bootstrap addresses are found");
161                            return Ok(bootstrap_addresses);
162                        }
163                    }
164                }
165            }
166        } else {
167            info!("Ignoring cache, not loading bootstrap addresses from cache");
168        }
169
170        // If we have a network contacts URL, fetch addrs from there.
171        if !self.local && !self.network_contacts_url.is_empty() {
172            info!(
173                "Fetching bootstrap address from network contacts URLs: {:?}",
174                self.network_contacts_url
175            );
176            let addrs = self
177                .network_contacts_url
178                .iter()
179                .map(|url| url.parse::<Url>().map_err(|_| Error::FailedToParseUrl))
180                .collect::<Result<Vec<Url>>>()?;
181            let mut contacts_fetcher = ContactsFetcher::with_endpoints(addrs)?;
182            if let Some(count) = count {
183                contacts_fetcher.set_max_addrs(count);
184            }
185            let addrs = contacts_fetcher.fetch_bootstrap_addresses().await?;
186            bootstrap_addresses.extend(addrs);
187
188            if let Some(count) = count {
189                if bootstrap_addresses.len() >= count {
190                    bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
191                    bootstrap_addresses.truncate(count);
192                    info!("Returning early as enough bootstrap addresses are found");
193                    return Ok(bootstrap_addresses);
194                }
195            }
196        }
197
198        if !self.local && get_network_id() == MAINNET_ID {
199            let mut contacts_fetcher = ContactsFetcher::with_mainnet_endpoints()?;
200            if let Some(count) = count {
201                contacts_fetcher.set_max_addrs(count);
202            }
203            let addrs = contacts_fetcher.fetch_bootstrap_addresses().await?;
204            bootstrap_addresses.extend(addrs);
205        } else if !self.local && get_network_id() == ALPHANET_ID {
206            let mut contacts_fetcher = ContactsFetcher::with_alphanet_endpoints()?;
207            if let Some(count) = count {
208                contacts_fetcher.set_max_addrs(count);
209            }
210            let addrs = contacts_fetcher.fetch_bootstrap_addresses().await?;
211            bootstrap_addresses.extend(addrs);
212        }
213
214        if !bootstrap_addresses.is_empty() {
215            bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
216            if let Some(count) = count {
217                bootstrap_addresses.truncate(count);
218            }
219            Ok(bootstrap_addresses)
220        } else {
221            error!("No initial bootstrap peers found through any means");
222            Err(Error::NoBootstrapPeersFound)
223        }
224    }
225
226    pub fn read_addr_from_env() -> Vec<Multiaddr> {
227        Self::read_bootstrap_addr_from_env()
228            .into_iter()
229            .map(|addr| addr.addr)
230            .collect()
231    }
232
233    pub fn read_bootstrap_addr_from_env() -> Vec<BootstrapAddr> {
234        let mut bootstrap_addresses = Vec::new();
235        // Read from ANT_PEERS environment variable if present
236        if let Ok(addrs) = std::env::var(ANT_PEERS_ENV) {
237            for addr_str in addrs.split(',') {
238                if let Some(addr) = craft_valid_multiaddr_from_str(addr_str, false) {
239                    info!("Adding addr from environment variable: {addr}");
240                    bootstrap_addresses.push(BootstrapAddr::new(addr));
241                } else {
242                    warn!("Invalid multiaddress format from environment variable: {addr_str}");
243                }
244            }
245        }
246        bootstrap_addresses
247    }
248
249    /// Get the path to the bootstrap cache JSON file if `Self::bootstrap_cache_dir` is set
250    pub fn get_bootstrap_cache_path(&self) -> Result<Option<PathBuf>> {
251        if let Some(dir) = &self.bootstrap_cache_dir {
252            if dir.is_file() {
253                return Err(Error::InvalidBootstrapCacheDir);
254            }
255
256            if !dir.exists() {
257                std::fs::create_dir_all(dir)?;
258            }
259
260            let path = dir.join(cache_file_name());
261            Ok(Some(path))
262        } else {
263            Ok(None)
264        }
265    }
266}