From 236c563e66808a672900f8930d20f437f045d5fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 12 Sep 2022 22:22:19 +0200 Subject: [PATCH 1/4] Make `BasePath::new_temp_dir` return the same path for the program lifetime Instead of returning always a different path, this now returns the same path for the entire lifetime of the program. We still ensure that the path is cleared at the end of the program. --- Cargo.lock | 37 +++++++++++++++++++++++++++++++++++- client/service/Cargo.toml | 1 + client/service/src/config.rs | 37 ++++++++++++++++++++++-------------- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 335e8b3526792..b6932696e9f48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -873,6 +873,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "chacha20" version = "0.8.1" @@ -8744,6 +8750,7 @@ dependencies = [ "sp-transaction-storage-proof", "sp-trie", "sp-version", + "static_init", "substrate-prometheus-endpoint", "substrate-test-runtime", "substrate-test-runtime-client", @@ -10263,6 +10270,34 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "static_init" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a2a1c578e98c1c16fc3b8ec1328f7659a500737d7a0c6d625e73e830ff9c1f6" +dependencies = [ + "bitflags", + "cfg_aliases", + "libc", + "parking_lot 0.11.2", + "parking_lot_core 0.8.5", + "static_init_macro", + "winapi", +] + +[[package]] +name = "static_init_macro" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70a2595fc3aa78f2d0e45dd425b22282dd863273761cc77780914b2cf3003acf" +dependencies = [ + "cfg_aliases", + "memchr", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "statrs" version = "0.15.0" @@ -11132,7 +11167,7 @@ checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ "cfg-if 1.0.0", "digest 0.10.3", - "rand 0.7.3", + "rand 0.8.5", "static_assertions", ] diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index 0acdbb1b1b63e..cbce321fe689c 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -84,6 +84,7 @@ async-trait = "0.1.57" tokio = { version = "1.17.0", features = ["time", "rt-multi-thread", "parking_lot"] } tempfile = "3.1.0" directories = "4.0.1" +static_init = "1.0.3" [dev-dependencies] substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 2fb3f820ebf15..1ade5c4fa51e8 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -262,31 +262,43 @@ impl Default for RpcMethods { } } +#[static_init::dynamic(drop)] +static mut BASE_PATH_TEMP: Option = None; + /// The base path that is used for everything that needs to be write on disk to run a node. #[derive(Debug)] -pub enum BasePath { - /// A temporary directory is used as base path and will be deleted when dropped. - Temporary(TempDir), - /// A path on the disk. - Permanenent(PathBuf), +pub struct BasePath { + path: PathBuf, } impl BasePath { /// Create a `BasePath` instance using a temporary directory prefixed with "substrate" and use /// it as base path. /// - /// Note: the temporary directory will be created automatically and deleted when the `BasePath` - /// instance is dropped. + /// Note: The temporary directory will be created automatically and deleted when the program + /// exits. Every call to this function will return the same path for the lifetime of the + /// program. pub fn new_temp_dir() -> io::Result { - Ok(BasePath::Temporary(tempfile::Builder::new().prefix("substrate").tempdir()?)) + let mut temp = BASE_PATH_TEMP.write(); + + match &*temp { + Some(p) => Ok(Self::new(p.path())), + None => { + let temp_dir = tempfile::Builder::new().prefix("substrate").tempdir()?; + let path = PathBuf::from(temp_dir.path()); + + *temp = Some(temp_dir); + Ok(Self::new(path)) + }, + } } /// Create a `BasePath` instance based on an existing path on disk. /// /// Note: this function will not ensure that the directory exist nor create the directory. It /// will also not delete the directory when the instance is dropped. - pub fn new>(path: P) -> BasePath { - BasePath::Permanenent(path.as_ref().to_path_buf()) + pub fn new>(path: P) -> BasePath { + Self { path: path.into() } } /// Create a base path from values describing the project. @@ -300,10 +312,7 @@ impl BasePath { /// Retrieve the base path. pub fn path(&self) -> &Path { - match self { - BasePath::Temporary(temp_dir) => temp_dir.path(), - BasePath::Permanenent(path) => path.as_path(), - } + &self.path } /// Returns the configuration directory inside this base path. From 197acd01d642bd8b7035b1aa925f1d322b5c1744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 13 Sep 2022 07:12:42 +0100 Subject: [PATCH 2/4] Update client/service/src/config.rs Co-authored-by: Koute --- client/service/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 1ade5c4fa51e8..cb845ac3a92c7 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -262,7 +262,7 @@ impl Default for RpcMethods { } } -#[static_init::dynamic(drop)] +#[static_init::dynamic(drop,lazy)] static mut BASE_PATH_TEMP: Option = None; /// The base path that is used for everything that needs to be write on disk to run a node. From 634684d34e43bfeb802e110be139a70e4ea24f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 13 Sep 2022 10:04:51 +0200 Subject: [PATCH 3/4] Update client/service/src/config.rs Co-authored-by: Nitwit <47109040+nitwit69@users.noreply.github.com> --- client/service/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/src/config.rs b/client/service/src/config.rs index cb845ac3a92c7..bb5aa6220d2c7 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -265,7 +265,7 @@ impl Default for RpcMethods { #[static_init::dynamic(drop,lazy)] static mut BASE_PATH_TEMP: Option = None; -/// The base path that is used for everything that needs to be write on disk to run a node. +/// The base path that is used for everything that needs to be written on disk to run a node. #[derive(Debug)] pub struct BasePath { path: PathBuf, From 0a1a8697ca23ccd6d3d3bd58b7db67d126f25eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 13 Sep 2022 10:05:13 +0200 Subject: [PATCH 4/4] FMT --- client/service/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/service/src/config.rs b/client/service/src/config.rs index bb5aa6220d2c7..44153e3b914f3 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -262,7 +262,7 @@ impl Default for RpcMethods { } } -#[static_init::dynamic(drop,lazy)] +#[static_init::dynamic(drop, lazy)] static mut BASE_PATH_TEMP: Option = None; /// The base path that is used for everything that needs to be written on disk to run a node.