Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat(antctl): impl network_id option while adding node" #2516

Merged
merged 7 commits into from
Dec 10, 2024
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions ant-bootstrap/src/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,15 @@ impl BootstrapCacheStore {
/// Create a empty CacheStore from the given peers argument.
/// This also modifies the cfg if provided based on the PeersArgs.
/// And also performs some actions based on the PeersArgs.
///
/// `PeersArgs::bootstrap_cache_dir` will take precedence over the path provided inside `config`.
pub fn new_from_peers_args(
peers_arg: &PeersArgs,
config: Option<BootstrapCacheConfig>,
cfg: Option<BootstrapCacheConfig>,
) -> Result<Self> {
let mut config = if let Some(cfg) = config {
let config = if let Some(cfg) = cfg {
cfg
} else {
BootstrapCacheConfig::default_config()?
};
if let Some(bootstrap_cache_path) = peers_arg.get_bootstrap_cache_path()? {
config.cache_file_path = bootstrap_cache_path;
}

let mut store = Self::new(config)?;

// If it is the first node, clear the cache.
Expand Down
11 changes: 3 additions & 8 deletions ant-bootstrap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::error::{Error, Result};
use ant_protocol::version::{get_network_id, get_truncate_version_str};
use ant_protocol::version::{get_key_version_str, get_truncate_version_str};
use std::{
path::{Path, PathBuf},
time::Duration,
Expand Down Expand Up @@ -118,13 +118,8 @@ fn default_cache_path() -> Result<PathBuf> {

std::fs::create_dir_all(&dir)?;

let path = dir.join(cache_file_name());
let network_id = format!("{}_{}", get_key_version_str(), get_truncate_version_str());
let path = dir.join(format!("bootstrap_cache_{}.json", network_id));

Ok(path)
}

/// Returns the name of the cache file
pub fn cache_file_name() -> String {
let network_id = format!("{}_{}", get_network_id(), get_truncate_version_str());
format!("bootstrap_cache_{network_id}.json")
}
90 changes: 73 additions & 17 deletions ant-bootstrap/src/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl ContactsFetcher {
self.endpoints
);
let mut bootstrap_addresses = Vec::new();
let mut last_error = None;

let mut fetches = stream::iter(self.endpoints.clone())
.map(|endpoint| async move {
Expand Down Expand Up @@ -130,16 +131,37 @@ impl ContactsFetcher {
}
Err(e) => {
warn!("Failed to fetch bootstrap addrs from {}: {}", endpoint, e);
last_error = Some(e);
}
}
}

info!(
"Successfully discovered {} total addresses. First few: {:?}",
bootstrap_addresses.len(),
bootstrap_addresses.iter().take(3).collect::<Vec<_>>()
);
Ok(bootstrap_addresses)
if bootstrap_addresses.is_empty() {
last_error.map_or_else(
|| {
warn!("No bootstrap addrs found from any endpoint and no errors reported");
Err(Error::NoBootstrapAddressesFound(
"No valid peers found from any endpoint".to_string(),
))
},
|e| {
warn!(
"No bootstrap addrs found from any endpoint. Last error: {}",
e
);
Err(Error::NoBootstrapAddressesFound(format!(
"No valid bootstrap addrs found from any endpoint: {e}",
)))
},
)
} else {
info!(
"Successfully discovered {} total addresses. First few: {:?}",
bootstrap_addresses.len(),
bootstrap_addresses.iter().take(3).collect::<Vec<_>>()
);
Ok(bootstrap_addresses)
}
}

/// Fetch the list of multiaddrs from a single endpoint
Expand Down Expand Up @@ -222,13 +244,20 @@ impl ContactsFetcher {
})
.collect::<Vec<_>>();

info!(
"Successfully parsed {} valid peers from JSON",
bootstrap_addresses.len()
);
Ok(bootstrap_addresses)
if bootstrap_addresses.is_empty() {
warn!("No valid peers found in JSON response");
Err(Error::NoBootstrapAddressesFound(
"No valid peers found in JSON response".to_string(),
))
} else {
info!(
"Successfully parsed {} valid peers from JSON",
bootstrap_addresses.len()
);
Ok(bootstrap_addresses)
}
}
Err(_err) => {
Err(e) => {
info!("Attempting to parse response as plain text");
// Try parsing as plain text with one multiaddr per line
// example of contacts file exists in resources/network-contacts-examples
Expand All @@ -237,11 +266,20 @@ impl ContactsFetcher {
.filter_map(|str| craft_valid_multiaddr_from_str(str, ignore_peer_id))
.collect::<Vec<_>>();

info!(
"Successfully parsed {} valid bootstrap addrs from plain text",
bootstrap_addresses.len()
);
Ok(bootstrap_addresses)
if bootstrap_addresses.is_empty() {
warn!(
"No valid bootstrap addrs found in plain text response. Previous Json error: {e:?}"
);
Err(Error::NoBootstrapAddressesFound(
"No valid bootstrap addrs found in plain text response".to_string(),
))
} else {
info!(
"Successfully parsed {} valid bootstrap addrs from plain text",
bootstrap_addresses.len()
);
Ok(bootstrap_addresses)
}
}
}
}
Expand Down Expand Up @@ -349,6 +387,24 @@ mod tests {
assert_eq!(addrs[0].addr, valid_addr);
}

#[tokio::test]
async fn test_empty_response() {
let mock_server = MockServer::start().await;

Mock::given(method("GET"))
.and(path("/"))
.respond_with(ResponseTemplate::new(200).set_body_string(""))
.mount(&mock_server)
.await;

let mut fetcher = ContactsFetcher::new().unwrap();
fetcher.endpoints = vec![mock_server.uri().parse().unwrap()];

let result = fetcher.fetch_bootstrap_addresses().await;

assert!(matches!(result, Err(Error::NoBootstrapAddressesFound(_))));
}

#[tokio::test]
async fn test_whitespace_and_empty_lines() {
let mock_server = MockServer::start().await;
Expand Down
6 changes: 2 additions & 4 deletions ant-bootstrap/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ pub enum Error {
FailedToParseCacheData,
#[error("Could not obtain data directory")]
CouldNotObtainDataDir,
#[error("Invalid bootstrap cache directory")]
InvalidBootstrapCacheDir,
#[error("Could not obtain bootstrap addresses from {0} after {1} retries")]
FailedToObtainAddrsFromUrl(String, usize),
#[error("Failed to parse Url")]
FailedToParseUrl,
#[error("No Bootstrap Addresses found: {0}")]
NoBootstrapAddressesFound(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON error: {0}")]
Expand Down
Loading
Loading