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

Create a dir per node restart #51

Merged
merged 1 commit into from
Nov 11, 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
4 changes: 4 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ where
&self.dir
}

fn set_dir(&mut self, new_dir: PathBuf) {
self.dir = new_dir
}

fn rpc_bind_host(&self) -> &str {
&self.rollup.rpc.bind_host
}
Expand Down
24 changes: 22 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{
fs::File,
path::PathBuf,
process::Stdio,
sync::Arc,
sync::{
atomic::{AtomicU8, Ordering},
Arc,
},
time::{Duration, SystemTime},
};

Expand All @@ -23,7 +26,7 @@ use crate::{
docker::DockerEnv,
log_provider::LogPathProvider,
traits::{NodeT, Restart, SpawnOutput},
utils::{get_citrea_path, get_genesis_path},
utils::{copy_directory, get_citrea_path, get_genesis_path},
Result,
};

Expand Down Expand Up @@ -64,6 +67,7 @@ pub trait Config: Clone {
type NodeConfig: Serialize;

fn dir(&self) -> &PathBuf;
fn set_dir(&mut self, new_dir: PathBuf);
fn rpc_bind_host(&self) -> &str;
fn rpc_bind_port(&self) -> u16;
fn env(&self) -> Vec<(&'static str, &'static str)>;
Expand Down Expand Up @@ -248,9 +252,25 @@ where

async fn start(&mut self, new_config: Option<Self::Config>) -> Result<()> {
let config = self.config_mut();

if let Some(new_config) = new_config {
*config = new_config;
}

// Update and copy to new dir in order not to overwrite the previous datadir when re-spawning
// Keep track of multiple restarts by creating {node_kind}-{INDEX} directories per restart
static INDEX: AtomicU8 = AtomicU8::new(0);
INDEX.fetch_add(1, Ordering::SeqCst);

let old_dir = config.dir();
let new_dir = old_dir.parent().unwrap().join(format!(
"{}-{}",
Self::Config::node_kind(),
INDEX.load(Ordering::SeqCst)
));
copy_directory(old_dir, &new_dir)?;
config.set_dir(new_dir);

*self.spawn_output() = Self::spawn(config)?;
self.wait_for_ready(None).await
}
Expand Down
Loading