Add a feature to create automatically a random temporary directory for base path & remove Clone (#6221)

* Initial commit

Forked at: 4adac40c07
Parent branch: origin/master

* Add a feature to create automatically a temporary directory for base path

* doc fix and todos

* use parking_lot instead

* use refcell instead since we stay in the main thread

* remove Clone derives

* add test

* solving dependency issue

* clarifying doc

* conflict argument with base-path

* WIP

Forked at: 4adac40c07
Parent branch: origin/master

* revert dep deletion

* fixing test and making base_path optional

* hold basepath while the service is running

* fixes

* Update client/cli/src/params/shared_params.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/service/Cargo.toml

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/cli/src/commands/mod.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/service/src/config.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* WIP

Forked at: 4adac40c07
Parent branch: origin/master

* improve doc

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Cecile Tonglet
2020-06-10 13:13:25 +02:00
committed by GitHub
parent f9c0c6a719
commit e3fc4f7fba
33 changed files with 227 additions and 63 deletions
+61 -1
View File
@@ -24,12 +24,14 @@ pub use sc_network::config::{ExtTransport, MultiaddrWithPeerId, NetworkConfigura
pub use sc_executor::WasmExecutionMethod;
use sc_client_api::execution_extensions::ExecutionStrategies;
use std::{future::Future, path::{PathBuf, Path}, pin::Pin, net::SocketAddr, sync::Arc};
use std::{io, future::Future, path::{PathBuf, Path}, pin::Pin, net::SocketAddr, sync::Arc};
pub use sc_transaction_pool::txpool::Options as TransactionPoolOptions;
use sc_chain_spec::ChainSpec;
use sp_core::crypto::Protected;
pub use sc_telemetry::TelemetryEndpoints;
use prometheus_endpoint::Registry;
#[cfg(not(target_os = "unknown"))]
use tempfile::TempDir;
/// Service configuration.
pub struct Configuration {
@@ -102,6 +104,8 @@ pub struct Configuration {
pub max_runtime_instances: usize,
/// Announce block automatically after they have been imported
pub announce_block: bool,
/// Base path of the configuration
pub base_path: Option<BasePath>,
}
/// Type for tasks spawned by the executor.
@@ -191,3 +195,59 @@ impl Default for RpcMethods {
RpcMethods::Auto
}
}
/// The base path that is used for everything that needs to be write on disk to run a node.
pub enum BasePath {
/// A temporary directory is used as base path and will be deleted when dropped.
#[cfg(not(target_os = "unknown"))]
Temporary(TempDir),
/// A path on the disk.
Permanenent(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.
#[cfg(not(target_os = "unknown"))]
pub fn new_temp_dir() -> io::Result<BasePath> {
Ok(BasePath::Temporary(
tempfile::Builder::new().prefix("substrate").tempdir()?,
))
}
/// 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<P: AsRef<Path>>(path: P) -> BasePath {
BasePath::Permanenent(path.as_ref().to_path_buf())
}
/// Create a base path from values describing the project.
#[cfg(not(target_os = "unknown"))]
pub fn from_project(qualifier: &str, organization: &str, application: &str) -> BasePath {
BasePath::new(
directories::ProjectDirs::from(qualifier, organization, application)
.expect("app directories exist on all supported platforms; qed")
.data_local_dir(),
)
}
/// Retrieve the base path.
pub fn path(&self) -> &Path {
match self {
#[cfg(not(target_os = "unknown"))]
BasePath::Temporary(temp_dir) => temp_dir.path(),
BasePath::Permanenent(path) => path.as_path(),
}
}
}
impl std::convert::From<PathBuf> for BasePath {
fn from(path: PathBuf) -> Self {
BasePath::new(path)
}
}