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

feat(manager): implement PeersArgs into ant node manager #2503

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions ant-bootstrap/src/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,21 @@ 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,
cfg: Option<BootstrapCacheConfig>,
config: Option<BootstrapCacheConfig>,
) -> Result<Self> {
let config = if let Some(cfg) = cfg {
let mut config = if let Some(cfg) = config {
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
9 changes: 7 additions & 2 deletions ant-bootstrap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ fn default_cache_path() -> Result<PathBuf> {

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

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

Ok(path)
}

/// Returns the name of the cache file
pub fn cache_file_name() -> String {
let network_id = format!("{}_{}", get_key_version_str(), get_truncate_version_str());
format!("bootstrap_cache_{network_id}.json")
}
90 changes: 17 additions & 73 deletions ant-bootstrap/src/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ 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 @@ -131,37 +130,16 @@ impl ContactsFetcher {
}
Err(e) => {
warn!("Failed to fetch bootstrap addrs from {}: {}", endpoint, e);
last_error = Some(e);
}
}
}

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)
}
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 @@ -244,20 +222,13 @@ impl ContactsFetcher {
})
.collect::<Vec<_>>();

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)
}
info!(
"Successfully parsed {} valid peers from JSON",
bootstrap_addresses.len()
);
Ok(bootstrap_addresses)
}
Err(e) => {
Err(_err) => {
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 @@ -266,20 +237,11 @@ impl ContactsFetcher {
.filter_map(|str| craft_valid_multiaddr_from_str(str, ignore_peer_id))
.collect::<Vec<_>>();

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)
}
info!(
"Successfully parsed {} valid bootstrap addrs from plain text",
bootstrap_addresses.len()
);
Ok(bootstrap_addresses)
}
}
}
Expand Down Expand Up @@ -387,24 +349,6 @@ 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: 4 additions & 2 deletions ant-bootstrap/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ 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("No Bootstrap Addresses found: {0}")]
NoBootstrapAddressesFound(String),
#[error("Failed to parse Url")]
FailedToParseUrl,
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON error: {0}")]
Expand Down
Loading
Loading