zebra/zebra-network/src/config/cache_dir.rs

75 lines
2.2 KiB
Rust
Raw Normal View History

feat(net): Cache a list of useful peers on disk (#6739) * Rewrite some state cache docs to clarify * Add a zebra_network::Config.cache_dir for peer address caches * Add new config test files and fix config test failure message * Create some zebra-chain and zebra-network convenience functions * Add methods for reading and writing the peer address cache * Add cached disk peers to the initial peers list * Add metrics and logging for loading and storing the peer cache * Replace log of useless redacted peer IP addresses * Limit the peer cache minimum and maximum size, don't write empty caches * Add a cacheable_peers() method to the address book * Add a peer disk cache updater task to the peer set tasks * Document that the peer cache is shared by multiple instances unless configured otherwise * Disable peer cache read/write in disconnected tests * Make initial peer cache updater sleep shorter for tests * Add unit tests for reading and writing the peer cache * Update the task list in the start command docs * Modify the existing persistent acceptance test to check for peer caches * Update the peer cache directory when writing test configs * Add a CacheDir type so the default config can be enabled, but tests can disable it * Update tests to use the CacheDir config type * Rename some CacheDir internals * Add config file test cases for each kind of CacheDir config * Panic if the config contains invalid socket addresses, rather than continuing * Add a network directory to state cache directory contents tests * Add new network.cache_dir config to the config parsing tests
2023-06-06 01:28:14 -07:00
//! Cache directory configuration for zebra-network.
use std::path::{Path, PathBuf};
use zebra_chain::parameters::Network;
/// A cache directory config field.
///
/// This cache directory configuration field is optional.
/// It defaults to being enabled with the default config path,
/// but also allows a custom path to be set.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum CacheDir {
/// Whether the cache directory is enabled with the default path (`true`),
/// or disabled (`false`).
IsEnabled(bool),
/// Enable the cache directory and use a custom path.
CustomPath(PathBuf),
}
impl CacheDir {
/// Returns a `CacheDir` enabled with the default path.
pub fn default_path() -> Self {
Self::IsEnabled(true)
}
/// Returns a disabled `CacheDir`.
pub fn disabled() -> Self {
Self::IsEnabled(false)
}
/// Returns a custom `CacheDir` enabled with `path`.
pub fn custom_path(path: impl AsRef<Path>) -> Self {
Self::CustomPath(path.as_ref().to_owned())
}
/// Returns `true` if this `CacheDir` is enabled with the default or a custom path.
pub fn is_enabled(&self) -> bool {
match self {
CacheDir::IsEnabled(is_enabled) => *is_enabled,
CacheDir::CustomPath(_) => true,
}
}
/// Returns the peer cache file path for `network`, if enabled.
pub fn peer_cache_file_path(&self, network: Network) -> Option<PathBuf> {
Some(
self.cache_dir()?
.join("network")
.join(format!("{}.peers", network.lowercase_name())),
)
}
/// Returns the `zebra-network` base cache directory, if enabled.
pub fn cache_dir(&self) -> Option<PathBuf> {
match self {
Self::IsEnabled(is_enabled) => is_enabled.then(|| {
dirs::cache_dir()
.unwrap_or_else(|| std::env::current_dir().unwrap().join("cache"))
.join("zebra")
}),
Self::CustomPath(cache_dir) => Some(cache_dir.to_owned()),
}
}
}
impl Default for CacheDir {
fn default() -> Self {
Self::default_path()
}
}