From 5719508e41085871c00489c0da2b4e39ac995519 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Fri, 6 Dec 2024 15:08:13 +0530 Subject: [PATCH] feat(bootstrap): write bootstrap cache from the clients --- Cargo.lock | 1 + ant-bootstrap/src/cache_store.rs | 8 ++++---- ant-bootstrap/tests/cache_tests.rs | 8 ++++---- ant-networking/src/driver.rs | 2 +- ant-node/src/bin/antnode/main.rs | 2 +- autonomi/Cargo.toml | 1 + autonomi/src/client/mod.rs | 12 +++++++++++- 7 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9324659bb..34ae07c699 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1539,6 +1539,7 @@ name = "autonomi" version = "0.2.4" dependencies = [ "alloy", + "ant-bootstrap", "ant-evm", "ant-logging", "ant-networking", diff --git a/ant-bootstrap/src/cache_store.rs b/ant-bootstrap/src/cache_store.rs index 6877baf9a4..a6f63b45d8 100644 --- a/ant-bootstrap/src/cache_store.rs +++ b/ant-bootstrap/src/cache_store.rs @@ -155,7 +155,7 @@ impl BootstrapCacheStore { } /// Create a empty CacheStore with the given configuration - pub fn empty(config: BootstrapCacheConfig) -> Result { + pub fn new(config: BootstrapCacheConfig) -> Result { info!("Creating new CacheStore with config: {:?}", config); let cache_path = config.cache_file_path.clone(); @@ -181,7 +181,7 @@ impl BootstrapCacheStore { /// Create a 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. - pub fn empty_from_peers_args( + pub fn new_from_peers_args( peers_arg: &PeersArgs, cfg: Option, ) -> Result { @@ -190,7 +190,7 @@ impl BootstrapCacheStore { } else { BootstrapCacheConfig::default_config()? }; - let mut store = Self::empty(config)?; + let mut store = Self::new(config)?; // If it is the first node, clear the cache. if peers_arg.first { @@ -396,7 +396,7 @@ mod tests { let config = crate::BootstrapCacheConfig::empty().with_cache_path(&cache_file); - let store = BootstrapCacheStore::empty(config).unwrap(); + let store = BootstrapCacheStore::new(config).unwrap(); (store.clone(), store.cache_path.clone()) } diff --git a/ant-bootstrap/tests/cache_tests.rs b/ant-bootstrap/tests/cache_tests.rs index 429e6be54a..4dd9b6edf8 100644 --- a/ant-bootstrap/tests/cache_tests.rs +++ b/ant-bootstrap/tests/cache_tests.rs @@ -23,7 +23,7 @@ async fn test_cache_store_operations() -> Result<(), Box> // Create cache store with config let config = BootstrapCacheConfig::empty().with_cache_path(&cache_path); - let mut cache_store = BootstrapCacheStore::empty(config)?; + let mut cache_store = BootstrapCacheStore::new(config)?; // Test adding and retrieving peers let addr: Multiaddr = @@ -53,7 +53,7 @@ async fn test_cache_max_peers() -> Result<(), Box> { let mut config = BootstrapCacheConfig::empty().with_cache_path(&cache_path); config.max_peers = 2; - let mut cache_store = BootstrapCacheStore::empty(config)?; + let mut cache_store = BootstrapCacheStore::new(config)?; // Add three peers with distinct timestamps let mut addresses = Vec::new(); @@ -94,7 +94,7 @@ async fn test_cache_file_corruption() -> Result<(), Box> // Create cache with some peers let config = BootstrapCacheConfig::empty().with_cache_path(&cache_path); - let mut cache_store = BootstrapCacheStore::empty(config.clone())?; + let mut cache_store = BootstrapCacheStore::new(config.clone())?; // Add a peer let addr: Multiaddr = @@ -108,7 +108,7 @@ async fn test_cache_file_corruption() -> Result<(), Box> tokio::fs::write(&cache_path, "invalid json content").await?; // Create a new cache store - it should handle the corruption gracefully - let mut new_cache_store = BootstrapCacheStore::empty(config)?; + let mut new_cache_store = BootstrapCacheStore::new(config)?; let addrs = new_cache_store.get_all_addrs().collect::>(); assert!(addrs.is_empty(), "Cache should be empty after corruption"); diff --git a/ant-networking/src/driver.rs b/ant-networking/src/driver.rs index 125dc543f0..9276c39237 100644 --- a/ant-networking/src/driver.rs +++ b/ant-networking/src/driver.rs @@ -1037,7 +1037,7 @@ impl SwarmDriver { let config = bootstrap_cache.config().clone(); let mut old_cache = bootstrap_cache.clone(); - let new = match BootstrapCacheStore::empty(config) { + let new = match BootstrapCacheStore::new(config) { Ok(new) => new, Err(err) => { error!("Failed to create a new empty cache: {err}"); diff --git a/ant-node/src/bin/antnode/main.rs b/ant-node/src/bin/antnode/main.rs index eff60ae043..0ed4f843c3 100644 --- a/ant-node/src/bin/antnode/main.rs +++ b/ant-node/src/bin/antnode/main.rs @@ -265,7 +265,7 @@ fn main() -> Result<()> { init_logging(&opt, keypair.public().to_peer_id())?; let rt = Runtime::new()?; - let bootstrap_cache = BootstrapCacheStore::empty_from_peers_args( + let bootstrap_cache = BootstrapCacheStore::new_from_peers_args( &opt.peers, Some(BootstrapCacheConfig::default_config()?), )?; diff --git a/autonomi/Cargo.toml b/autonomi/Cargo.toml index 2c2b4a7c79..2fc17a6d8e 100644 --- a/autonomi/Cargo.toml +++ b/autonomi/Cargo.toml @@ -27,6 +27,7 @@ vault = ["data", "registers"] websockets = ["ant-networking/websockets"] [dependencies] +ant-bootstrap = { path = "../ant-bootstrap", version = "0.1.0" } ant-evm = { path = "../ant-evm", version = "0.1.4" } ant-networking = { path = "../ant-networking", version = "0.19.5" } ant-protocol = { version = "0.17.15", path = "../ant-protocol" } diff --git a/autonomi/src/client/mod.rs b/autonomi/src/client/mod.rs index f039d097a0..9ccf33d716 100644 --- a/autonomi/src/client/mod.rs +++ b/autonomi/src/client/mod.rs @@ -34,6 +34,7 @@ pub mod wasm; // private module with utility functions mod utils; +use ant_bootstrap::{BootstrapCacheConfig, BootstrapCacheStore}; pub use ant_evm::Amount; use ant_networking::{interval, multiaddr_is_global, Network, NetworkBuilder, NetworkEvent}; @@ -132,7 +133,16 @@ impl Client { } fn build_client_and_run_swarm(local: bool) -> (Network, mpsc::Receiver) { - let network_builder = NetworkBuilder::new(Keypair::generate_ed25519(), local); + let mut network_builder = NetworkBuilder::new(Keypair::generate_ed25519(), local); + + if let Ok(mut config) = BootstrapCacheConfig::default_config() { + if local { + config.disable_cache_writing = true; + } + if let Ok(cache) = BootstrapCacheStore::new(config) { + network_builder.bootstrap_cache(cache); + } + } // TODO: Re-export `Receiver` from `ant-networking`. Else users need to keep their `tokio` dependency in sync. // TODO: Think about handling the mDNS error here.