diff --git a/packages/ciphernode/config/src/app_config.rs b/packages/ciphernode/config/src/app_config.rs index 9f92fc1b..7be01c9c 100644 --- a/packages/ciphernode/config/src/app_config.rs +++ b/packages/ciphernode/config/src/app_config.rs @@ -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
, } 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("~") { @@ -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() } } @@ -102,9 +100,13 @@ impl AppConfig { pub fn address(&self) -> Option
{ 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 { @@ -112,15 +114,15 @@ impl AppConfig { } 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 { @@ -173,6 +175,17 @@ fn normalize_path(path: impl AsRef) -> 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::*; @@ -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(()) }); @@ -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!( @@ -262,3 +276,5 @@ chains: }); } } + + diff --git a/tests/basic_integration/lib/ag/.gitignore b/tests/basic_integration/lib/ag/.gitignore new file mode 100644 index 00000000..e8d99d12 --- /dev/null +++ b/tests/basic_integration/lib/ag/.gitignore @@ -0,0 +1,2 @@ +db +key diff --git a/tests/basic_integration/lib/ag/config.yaml b/tests/basic_integration/lib/ag/config.yaml index 4b840a87..55bdb526 100644 --- a/tests/basic_integration/lib/ag/config.yaml +++ b/tests/basic_integration/lib/ag/config.yaml @@ -1,4 +1,5 @@ config_dir: . +data_dir: . address: "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199" chains: - name: "hardhat" diff --git a/tests/basic_integration/lib/clean_folders.sh b/tests/basic_integration/lib/clean_folders.sh new file mode 100755 index 00000000..ff7e9d22 --- /dev/null +++ b/tests/basic_integration/lib/clean_folders.sh @@ -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 diff --git a/tests/basic_integration/lib/cn1/.gitignore b/tests/basic_integration/lib/cn1/.gitignore new file mode 100644 index 00000000..e8d99d12 --- /dev/null +++ b/tests/basic_integration/lib/cn1/.gitignore @@ -0,0 +1,2 @@ +db +key diff --git a/tests/basic_integration/lib/cn1/config.yaml b/tests/basic_integration/lib/cn1/config.yaml index f4d9c94d..508112cd 100644 --- a/tests/basic_integration/lib/cn1/config.yaml +++ b/tests/basic_integration/lib/cn1/config.yaml @@ -1,4 +1,5 @@ config_dir: . +data_dir: . address: "0x2546BcD3c84621e976D8185a91A922aE77ECEc30" chains: - name: "hardhat" diff --git a/tests/basic_integration/lib/cn2/.gitignore b/tests/basic_integration/lib/cn2/.gitignore new file mode 100644 index 00000000..e8d99d12 --- /dev/null +++ b/tests/basic_integration/lib/cn2/.gitignore @@ -0,0 +1,2 @@ +db +key diff --git a/tests/basic_integration/lib/cn3/.gitignore b/tests/basic_integration/lib/cn3/.gitignore new file mode 100644 index 00000000..e8d99d12 --- /dev/null +++ b/tests/basic_integration/lib/cn3/.gitignore @@ -0,0 +1,2 @@ +db +key diff --git a/tests/basic_integration/lib/cn3/config.yaml b/tests/basic_integration/lib/cn3/config.yaml index b98e0c63..258b8488 100644 --- a/tests/basic_integration/lib/cn3/config.yaml +++ b/tests/basic_integration/lib/cn3/config.yaml @@ -1,4 +1,5 @@ config_dir: . +data_dir: . address: "0xdD2FD4581271e230360230F9337D5c0430Bf44C0" chains: - name: "hardhat" diff --git a/tests/basic_integration/lib/cn4/.gitignore b/tests/basic_integration/lib/cn4/.gitignore new file mode 100644 index 00000000..e8d99d12 --- /dev/null +++ b/tests/basic_integration/lib/cn4/.gitignore @@ -0,0 +1,2 @@ +db +key diff --git a/tests/basic_integration/lib/cn4/config.yaml b/tests/basic_integration/lib/cn4/config.yaml index 4b840a87..55bdb526 100644 --- a/tests/basic_integration/lib/cn4/config.yaml +++ b/tests/basic_integration/lib/cn4/config.yaml @@ -1,4 +1,5 @@ config_dir: . +data_dir: . address: "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199" chains: - name: "hardhat" diff --git a/tests/basic_integration/test.sh b/tests/basic_integration/test.sh index df895af9..1fb9e3f5 100755 --- a/tests/basic_integration/test.sh +++ b/tests/basic_integration/test.sh @@ -113,16 +113,7 @@ 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 @@ -130,9 +121,8 @@ 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"