use dirs crate for default location of state and config (#714)
* use dirs crate for default location of state and config * panic if a path isn't specified for zebra-state
This commit is contained in:
parent
132875198e
commit
c1a1493159
|
@ -146,6 +146,12 @@ dependencies = [
|
|||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
|
||||
|
||||
[[package]]
|
||||
name = "bech32"
|
||||
version = "0.7.2"
|
||||
|
@ -525,6 +531,26 @@ dependencies = [
|
|||
"generic-array 0.14.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "3.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_users",
|
||||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "displaydoc"
|
||||
version = "0.1.7"
|
||||
|
@ -1617,6 +1643,17 @@ version = "0.1.57"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"redox_syscall",
|
||||
"rust-argon2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.3.9"
|
||||
|
@ -1665,6 +1702,18 @@ dependencies = [
|
|||
"opaque-debug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-argon2"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"blake2b_simd",
|
||||
"constant_time_eq",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.16"
|
||||
|
@ -2651,6 +2700,7 @@ name = "zebra-state"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"color-eyre",
|
||||
"dirs",
|
||||
"futures",
|
||||
"hex",
|
||||
"lazy_static",
|
||||
|
@ -2697,6 +2747,7 @@ dependencies = [
|
|||
"abscissa_core",
|
||||
"chrono",
|
||||
"color-eyre",
|
||||
"dirs",
|
||||
"futures",
|
||||
"gumdrop",
|
||||
"hyper",
|
||||
|
|
|
@ -46,6 +46,10 @@ where
|
|||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn batch_flushes_on_max_items_test() -> Result<()> {
|
||||
batch_flushes_on_max_items().await
|
||||
}
|
||||
|
||||
#[spandoc::spandoc]
|
||||
async fn batch_flushes_on_max_items() -> Result<()> {
|
||||
use tokio::time::timeout;
|
||||
|
@ -61,6 +65,10 @@ async fn batch_flushes_on_max_items() -> Result<()> {
|
|||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn batch_flushes_on_max_latency_test() -> Result<()> {
|
||||
batch_flushes_on_max_latency().await
|
||||
}
|
||||
|
||||
#[spandoc::spandoc]
|
||||
async fn batch_flushes_on_max_latency() -> Result<()> {
|
||||
use tokio::time::timeout;
|
||||
|
|
|
@ -15,6 +15,7 @@ lazy_static = "1.4.0"
|
|||
hex = "0.4.2"
|
||||
sled = "0.33.0"
|
||||
serde = { version = "1", features = ["serde_derive"] }
|
||||
dirs = "3.0.1"
|
||||
tracing = "0.1"
|
||||
tracing-futures = "0.2"
|
||||
|
||||
|
@ -24,4 +25,4 @@ zebra-test = { path = "../zebra-test/" }
|
|||
spandoc = "0.2"
|
||||
tempdir = "0.3.7"
|
||||
color-eyre = "0.5"
|
||||
once_cell = "1.4"
|
||||
once_cell = "1.4"
|
|
@ -30,20 +30,38 @@ pub mod on_disk;
|
|||
#[serde(deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
/// The root directory for the state storage
|
||||
pub path: PathBuf,
|
||||
pub cache_dir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Generate the appropriate `sled::Config` based on the provided
|
||||
/// `zebra_state::Config`.
|
||||
///
|
||||
/// # Details
|
||||
///
|
||||
/// This function should panic if the user of `zebra-state` doesn't configure
|
||||
/// a directory to store the state.
|
||||
pub(crate) fn sled_config(&self) -> sled::Config {
|
||||
sled::Config::default().path(&self.path)
|
||||
let path = self
|
||||
.cache_dir
|
||||
.as_ref()
|
||||
.unwrap_or_else(|| {
|
||||
todo!("create a nice user facing error explaining how to set the cache directory")
|
||||
})
|
||||
.join("state");
|
||||
|
||||
sled::Config::default().path(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
path: PathBuf::from("./.zebra-state"),
|
||||
}
|
||||
let cache_dir = std::env::var("ZEBRAD_CACHE_DIR")
|
||||
.map(PathBuf::from)
|
||||
.ok()
|
||||
.or_else(|| dirs::cache_dir().map(|dir| dir.join("zebra")));
|
||||
|
||||
Self { cache_dir }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,3 +132,17 @@ fn block_locator_heights(tip_height: BlockHeight) -> impl Iterator<Item = BlockH
|
|||
.map(BlockHeight)
|
||||
.chain(iter::once(BlockHeight(0)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_no_path() {
|
||||
zebra_test::init();
|
||||
|
||||
let bad_config = Config { cache_dir: None };
|
||||
let _unreachable = bad_config.sled_config();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,6 +49,10 @@ static GET_TIP_TRANSCRIPT: Lazy<Vec<(Request, Response)>> = Lazy::new(|| {
|
|||
});
|
||||
|
||||
#[tokio::test]
|
||||
async fn check_transcripts_test() -> Result<(), Report> {
|
||||
check_transcripts().await
|
||||
}
|
||||
|
||||
#[spandoc::spandoc]
|
||||
async fn check_transcripts() -> Result<(), Report> {
|
||||
zebra_test::init();
|
||||
|
@ -61,7 +65,7 @@ async fn check_transcripts() -> Result<(), Report> {
|
|||
|
||||
let storage_guard = TempDir::new("")?;
|
||||
let service = on_disk::init(Config {
|
||||
path: storage_guard.path().to_owned(),
|
||||
cache_dir: Some(storage_guard.path().to_owned()),
|
||||
});
|
||||
let transcript = Transcript::from(transcript_data.iter().cloned());
|
||||
/// SPANDOC: check the on disk service against the transcript
|
||||
|
|
|
@ -34,6 +34,7 @@ tracing-error = "0.1.2"
|
|||
|
||||
metrics-runtime = "0.13"
|
||||
metrics = "0.12"
|
||||
dirs = "3.0.1"
|
||||
|
||||
[dev-dependencies]
|
||||
abscissa_core = { version = "0.5", features = ["testing"] }
|
||||
|
|
|
@ -91,14 +91,14 @@ impl ZebradCmd {
|
|||
impl Configurable<ZebradConfig> for ZebradCmd {
|
||||
/// Location of the configuration file
|
||||
fn config_path(&self) -> Option<PathBuf> {
|
||||
let filename = std::env::current_dir().ok().map(|mut dir_path| {
|
||||
dir_path.push(CONFIG_FILE);
|
||||
dir_path
|
||||
});
|
||||
|
||||
let if_exists = |f: PathBuf| if f.exists() { Some(f) } else { None };
|
||||
|
||||
filename.and_then(if_exists)
|
||||
dirs::preference_dir()
|
||||
.map(|path| path.join(CONFIG_FILE))
|
||||
.and_then(if_exists)
|
||||
.or_else(|| std::env::current_dir().ok())
|
||||
.map(|path| path.join(CONFIG_FILE))
|
||||
.and_then(if_exists)
|
||||
|
||||
// Note: Changes in how configuration is loaded may need usage
|
||||
// edits in generate.rs
|
||||
|
|
Loading…
Reference in New Issue