Skip to content

Commit

Permalink
feat(bootstrap): write bootstrap cache from the clients
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Dec 6, 2024
1 parent dfeac3b commit 56865c6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
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.

8 changes: 4 additions & 4 deletions ant-bootstrap/src/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl BootstrapCacheStore {
}

/// Create a empty CacheStore with the given configuration
pub fn empty(config: BootstrapCacheConfig) -> Result<Self> {
pub fn new(config: BootstrapCacheConfig) -> Result<Self> {
info!("Creating new CacheStore with config: {:?}", config);
let cache_path = config.cache_file_path.clone();

Expand All @@ -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<BootstrapCacheConfig>,
) -> Result<Self> {
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
}

Expand Down
8 changes: 4 additions & 4 deletions ant-bootstrap/tests/cache_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn test_cache_store_operations() -> Result<(), Box<dyn std::error::Error>>
// 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 =
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn test_cache_max_peers() -> Result<(), Box<dyn std::error::Error>> {
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();
Expand Down Expand Up @@ -94,7 +94,7 @@ async fn test_cache_file_corruption() -> Result<(), Box<dyn std::error::Error>>
// 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 =
Expand All @@ -108,7 +108,7 @@ async fn test_cache_file_corruption() -> Result<(), Box<dyn std::error::Error>>
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::<Vec<_>>();
assert!(addrs.is_empty(), "Cache should be empty after corruption");

Expand Down
2 changes: 1 addition & 1 deletion ant-networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
4 changes: 3 additions & 1 deletion ant-node/src/bin/antnode/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,12 @@ fn main() -> Result<()> {
init_logging(&opt, keypair.public().to_peer_id())?;

let rt = Runtime::new()?;
let bootstrap_cache = BootstrapCacheStore::empty_from_peers_args(
let mut bootstrap_cache = BootstrapCacheStore::new_from_peers_args(
&opt.peers,
Some(BootstrapCacheConfig::default_config()?),
)?;
// To create the file before startup if it doesn't exist.
bootstrap_cache.sync_and_flush_to_disk(true)?;

let msg = format!(
"Running {} v{}",
Expand Down
1 change: 1 addition & 0 deletions autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
12 changes: 11 additions & 1 deletion autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -132,7 +133,16 @@ impl Client {
}

fn build_client_and_run_swarm(local: bool) -> (Network, mpsc::Receiver<NetworkEvent>) {
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<T>` from `ant-networking`. Else users need to keep their `tokio` dependency in sync.
// TODO: Think about handling the mDNS error here.
Expand Down

0 comments on commit 56865c6

Please sign in to comment.