diff --git a/zebra-state/Cargo.toml b/zebra-state/Cargo.toml index 4fb34e56..fa7d8ab0 100644 --- a/zebra-state/Cargo.toml +++ b/zebra-state/Cargo.toml @@ -25,6 +25,7 @@ thiserror = "1.0.22" tokio = { version = "0.2.22", features = ["sync"] } displaydoc = "0.1.7" rocksdb = "0.15.0" +tempdir = "0.3.7" [dev-dependencies] zebra-chain = { path = "../zebra-chain", features = ["proptest-impl"] } diff --git a/zebra-state/src/config.rs b/zebra-state/src/config.rs index 720ffaa8..baa14ddf 100644 --- a/zebra-state/src/config.rs +++ b/zebra-state/src/config.rs @@ -1,5 +1,6 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; +use tempdir::TempDir; use zebra_chain::parameters::Network; /// Configuration for the state service. @@ -29,7 +30,9 @@ pub struct Config { /// Whether to use an ephemeral database. /// - /// Ephemeral databases are stored in memory on Linux, and in a temporary directory on other OSes. + /// Ephemeral databases are stored in a temporary directory. + /// They are deleted when Zebra exits successfully. + /// (If Zebra panics or crashes, the ephemeral database won't be deleted.) /// /// Set to `false` by default. If this is set to `true`, [`cache_dir`] is ignored. /// @@ -42,34 +45,10 @@ pub struct Config { pub debug_stop_at_height: Option, } -fn gen_temp_path() -> PathBuf { - use std::sync::atomic::{AtomicUsize, Ordering}; - use std::time::SystemTime; - - static SALT_COUNTER: AtomicUsize = AtomicUsize::new(0); - - let seed = SALT_COUNTER.fetch_add(1, Ordering::SeqCst) as u128; - - let now = SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_nanos() - << 48; - - #[cfg(not(miri))] - let pid = u128::from(std::process::id()); - - #[cfg(miri)] - let pid = 0; - - let salt = (pid << 16) + now + seed; - - if cfg!(target_os = "linux") { - // use shared memory for temporary linux files - format!("/dev/shm/pagecache.tmp.{}", salt).into() - } else { - std::env::temp_dir().join(format!("pagecache.tmp.{}", salt)) - } +fn gen_temp_path(prefix: &str) -> PathBuf { + TempDir::new(prefix) + .expect("temporary directory is created successfully") + .into_path() } impl Config { @@ -95,7 +74,11 @@ impl Config { opts.create_missing_column_families(true); let path = if self.ephemeral { - gen_temp_path() + gen_temp_path(&format!( + "zebra-state-v{}-{}", + crate::constants::DATABASE_FORMAT_VERSION, + net_dir + )) } else { self.cache_dir .join("state")