Skip to content

Commit

Permalink
Fix folders being deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
ktdlr committed Oct 28, 2024
1 parent b208e7b commit f5d4817
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 35 deletions.
62 changes: 39 additions & 23 deletions packages/ciphernode/config/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,29 @@ pub struct AppConfig {
config_file: PathBuf,
/// Used for testing if required
cwd: PathBuf,
/// The default config dir for the operating system this should not be changed
default_dir: PathBuf,
/// The data dir for enclave defaults to `~/.local/share/enclave`
data_dir: PathBuf,
/// Ethereum Address for the node
address: Option<Address>,
}

impl Default for AppConfig {
fn default() -> Self {
let default_dir = dirs::config_dir().unwrap().join("enclave"); // ~/.config/enclave
Self {
chains: vec![],
key_file: PathBuf::from("key"), // ~/.config/enclave/key
db_file: PathBuf::from("db"), // ~/.config/enclave/db
config_dir: default_dir.clone(), // ~/.config/enclave
key_file: PathBuf::from("key"), // ~/.config/enclave/key
db_file: PathBuf::from("db"), // ~/.config/enclave/db
config_dir: OsDirs::config_dir(), // ~/.config/enclave
data_dir: OsDirs::data_dir(), // ~/.config/enclave
config_file: PathBuf::from("config.yaml"), // ~/.config/enclave/config.yaml
cwd: env::current_dir().unwrap_or_default(),
default_dir, // ~/.config/enclave
address: None,
}
}
}

impl AppConfig {
fn ensure_full_path(&self, file: &PathBuf) -> PathBuf {
fn ensure_full_path(&self, dir:&PathBuf, file: &PathBuf) -> PathBuf {
normalize_path({
// If this is absolute return it
if file.is_absolute() || file.to_string_lossy().starts_with("~") {
Expand All @@ -71,27 +70,26 @@ impl AppConfig {

// We have to find where it should be relative from
// Assume it should be the config_dir
self.config_dir().join(file)
dir.join(file)
})
}

fn config_dir_impl(&self) -> PathBuf {
let config_dir = &self.config_dir;
fn resolve_base_dir(&self, base_dir:&PathBuf, default_base_dir:&PathBuf) -> PathBuf {

if config_dir.is_relative() {
if base_dir.is_relative() {
// ConfigDir is relative and the config file is absolute then use the location of the
// config file. That way all paths are relative to the config file
if self.config_file.is_absolute() {
self.config_file
.parent()
.map_or_else(|| config_dir.clone(), |p| p.join(config_dir))
.map_or_else(|| base_dir.clone(), |p| p.join(base_dir))
} else {
// If the config_file is not set but there are relative paths use the default dir use the default dir
self.default_dir.join(config_dir)
default_base_dir.join(base_dir)
}
} else {
// Use the absolute config_dir
config_dir.to_owned()
// Use the absolute base_dir
base_dir.to_owned()
}
}

Expand All @@ -102,25 +100,29 @@ impl AppConfig {
pub fn address(&self) -> Option<Address> {
self.address
}


pub fn data_dir(&self) -> PathBuf {
normalize_path(self.resolve_base_dir(&self.data_dir, &OsDirs::data_dir()))
}

pub fn config_dir(&self) -> PathBuf {
normalize_path(self.config_dir_impl())
normalize_path(self.resolve_base_dir(&self.config_dir, &OsDirs::config_dir()))
}

pub fn chains(&self) -> &Vec<ChainConfig> {
&self.chains
}

pub fn key_file(&self) -> PathBuf {
self.ensure_full_path(&self.key_file)
self.ensure_full_path(&self.config_dir(), &self.key_file)
}

pub fn db_file(&self) -> PathBuf {
self.ensure_full_path(&self.db_file)
self.ensure_full_path(&self.data_dir(), &self.db_file)
}

pub fn config_file(&self) -> PathBuf {
self.ensure_full_path(&self.config_file)
self.ensure_full_path(&self.config_dir(), &self.config_file)
}

pub fn cwd(&self) -> PathBuf {
Expand Down Expand Up @@ -173,6 +175,17 @@ fn normalize_path(path: impl AsRef<Path>) -> PathBuf {
result
}

struct OsDirs;
impl OsDirs {
pub fn config_dir() -> PathBuf {
dirs::config_dir().unwrap().join("enclave")
}

pub fn data_dir() -> PathBuf {
dirs::data_local_dir().unwrap().join("enclave")
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -186,11 +199,12 @@ mod tests {
let config = AppConfig {
config_file: "/home/testuser/docs/myconfig.yaml".into(),
config_dir: "../foo".into(),
data_dir: "../bar".into(),
..AppConfig::default()
};

assert_eq!(config.key_file(), PathBuf::from("/home/testuser/foo/key"));
assert_eq!(config.db_file(), PathBuf::from("/home/testuser/foo/db"));
assert_eq!(config.db_file(), PathBuf::from("/home/testuser/bar/db"));

Ok(())
});
Expand All @@ -210,7 +224,7 @@ mod tests {

assert_eq!(
config.db_file(),
PathBuf::from("/home/testuser/.config/enclave/db")
PathBuf::from("/home/testuser/.local/share/enclave/db")
);

assert_eq!(
Expand Down Expand Up @@ -262,3 +276,5 @@ chains:
});
}
}


2 changes: 2 additions & 0 deletions tests/basic_integration/lib/ag/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db
key
1 change: 1 addition & 0 deletions tests/basic_integration/lib/ag/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
config_dir: .
data_dir: .
address: "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199"
chains:
- name: "hardhat"
Expand Down
14 changes: 14 additions & 0 deletions tests/basic_integration/lib/clean_folders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
clean_folders() {
local SCRIPT_DIR=$1

# Delete output artifacts
rm -rf "$SCRIPT_DIR/output/"*

# Delete enclave artifacts
for name in cn1 cn2 cn3 cn4 ag; do
# List all files and directories except config.yaml, then delete them
find "$SCRIPT_DIR/lib/$name" -mindepth 1 ! -regex '.*/config\.yaml$' ! -regex '.*/.gitignore$' -exec rm -rf {} +
done
}

clean_folders $1
2 changes: 2 additions & 0 deletions tests/basic_integration/lib/cn1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db
key
1 change: 1 addition & 0 deletions tests/basic_integration/lib/cn1/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
config_dir: .
data_dir: .
address: "0x2546BcD3c84621e976D8185a91A922aE77ECEc30"
chains:
- name: "hardhat"
Expand Down
2 changes: 2 additions & 0 deletions tests/basic_integration/lib/cn2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db
key
2 changes: 2 additions & 0 deletions tests/basic_integration/lib/cn3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db
key
1 change: 1 addition & 0 deletions tests/basic_integration/lib/cn3/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
config_dir: .
data_dir: .
address: "0xdD2FD4581271e230360230F9337D5c0430Bf44C0"
chains:
- name: "hardhat"
Expand Down
2 changes: 2 additions & 0 deletions tests/basic_integration/lib/cn4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db
key
1 change: 1 addition & 0 deletions tests/basic_integration/lib/cn4/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
config_dir: .
data_dir: .
address: "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199"
chains:
- name: "hardhat"
Expand Down
14 changes: 2 additions & 12 deletions tests/basic_integration/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,16 @@ launch_aggregator() {
--plaintext-write-path "$SCRIPT_DIR/output/plaintext.txt" &
}

clean_folders() {
# Delete output artifacts
rm -rf "$SCRIPT_DIR/output/"*

# Delete enclave artifacts
for name in cn1 cn2 cn3 cn4 ag; do
# List all files and directories except config.yaml, then delete them
find "$SCRIPT_DIR/lib/$name" -mindepth 1 ! -regex '.*/config\.yaml$' -exec rm -rf {} +
done
}


pkill -9 -f "target/debug/enclave" || true
pkill -9 -f "hardhat node" || true

# Set up trap to catch errors and interrupts
trap 'cleanup $?' ERR INT TERM

# Tidy up files from previous runs
clean_folders

$SCRIPT_DIR/lib/clean_folders.sh "$SCRIPT_DIR"
$SCRIPT_DIR/lib/prebuild.sh

heading "Start the EVM node"
Expand Down

0 comments on commit f5d4817

Please sign in to comment.