diff --git a/CHANGELOG.md b/CHANGELOG.md index fc1cf2a7c..e27fe6694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ way of [cargo make]. `tendermint-rpc` test coverage has been expanded here too. ([#758]) +* `[light-client]` The `sled`-backed lightstore is now feature-guarded under + the `lightstore-sled` feature, which is enabled by default for now. ([#772]) + [#758]: https://github.com/informalsystems/tendermint-rs/pull/758 +[#772]: https://github.com/informalsystems/tendermint-rs/pull/772 [cargo make]: https://github.com/sagiegurari/cargo-make ## v0.17.0 diff --git a/light-client/Cargo.toml b/light-client/Cargo.toml index 539a77005..df346cefc 100644 --- a/light-client/Cargo.toml +++ b/light-client/Cargo.toml @@ -21,10 +21,18 @@ description = """ [lib] crate-type = ["cdylib", "rlib"] +# docs.rs-specific configuration +[package.metadata.docs.rs] +# document all features +all-features = true +# defines the configuration attribute `docsrs` +rustdoc-args = ["--cfg", "docsrs"] + [features] -default = ["rpc-client"] +default = ["rpc-client", "lightstore-sled"] rpc-client = ["tokio", "tendermint-rpc/http-client"] secp256k1 = ["tendermint/secp256k1", "tendermint-rpc/secp256k1"] +lightstore-sled = ["sled"] [dependencies] tendermint = { version = "0.17.0", path = "../tendermint" } @@ -38,7 +46,7 @@ futures = "0.3.4" serde = "1.0.106" serde_cbor = "0.11.1" serde_derive = "1.0.106" -sled = "0.34.3" +sled = { version = "0.34.3", optional = true } static_assertions = "1.1.0" thiserror = "1.0.15" tokio = { version = "0.2", optional = true } diff --git a/light-client/examples/light_client.rs b/light-client/examples/light_client.rs index df037a4c0..dc14fcd66 100644 --- a/light-client/examples/light_client.rs +++ b/light-client/examples/light_client.rs @@ -85,9 +85,7 @@ fn make_instance( db_path: impl AsRef, opts: &SyncOpts, ) -> Result { - let db = sled::open(db_path)?; - - let light_store = SledStore::new(db); + let light_store = SledStore::open(db_path)?; let rpc_client = rpc::HttpClient::new(addr).unwrap(); let options = light_client::Options { trust_threshold: TrustThreshold::default(), diff --git a/light-client/src/store.rs b/light-client/src/store.rs index 4987acad0..9e18da84c 100644 --- a/light-client/src/store.rs +++ b/light-client/src/store.rs @@ -1,6 +1,7 @@ //! Interface and implementations of the light block store. //! //! See the `memory` and `sled` modules for: +//! //! - a transient, in-memory implementation for testing purposes //! - a persistent, on-disk, sled-backed implementation for production @@ -10,6 +11,9 @@ use crate::types::{Height, LightBlock, Status}; use crate::utils::std_ext; pub mod memory; + +#[cfg(feature = "lightstore-sled")] +#[cfg_attr(docsrs, doc(cfg(feature = "lightstore-sled")))] pub mod sled; /// Store for light blocks. diff --git a/light-client/src/store/sled.rs b/light-client/src/store/sled.rs index d9b6d2588..f30989936 100644 --- a/light-client/src/store/sled.rs +++ b/light-client/src/store/sled.rs @@ -2,6 +2,8 @@ pub mod utils; +use std::path::Path; + use crate::{ store::sled::utils::*, types::{Height, LightBlock}, @@ -26,7 +28,12 @@ pub struct SledStore { } impl SledStore { - /// Create a new persistent store from a sled database + /// Open a sled database and create a new persistent store from it. + pub fn open(db: impl AsRef) -> Result { + Ok(Self::new(sled::open(db)?)) + } + + /// Create a new persistent store from a sled database that is already open. pub fn new(db: SledDb) -> Self { Self { db, diff --git a/light-node/Cargo.toml b/light-node/Cargo.toml index e08af16ff..b008794b0 100644 --- a/light-node/Cargo.toml +++ b/light-node/Cargo.toml @@ -27,7 +27,7 @@ name = "tendermint-light-node" path = "src/bin/tendermint-light-node/main.rs" [dependencies] -anomaly = { version = "0.2", features = [ "serializer" ] } +anomaly = { version = "0.2", features = ["serializer"] } async-trait = "0.1" gumdrop = "0.7" jsonrpc-core = "14.2" @@ -36,10 +36,9 @@ jsonrpc-http-server = "14.2" jsonrpc-derive = "14.2" serde = { version = "1", features = ["serde_derive"] } serde_json = "1.0" -sled = "0.34.3" tendermint = { version = "0.17.0", path = "../tendermint" } -tendermint-light-client = { version = "0.17.0", path = "../light-client" } -tendermint-rpc = { version = "0.17.0", path = "../rpc", features = [ "http-client" ] } +tendermint-light-client = { version = "0.17.0", path = "../light-client", features = ["lightstore-sled"] } +tendermint-rpc = { version = "0.17.0", path = "../rpc", features = ["http-client"] } thiserror = "1.0" tokio = { version = "0.2", features = ["full"] } diff --git a/light-node/src/commands/initialize.rs b/light-node/src/commands/initialize.rs index a7b581842..4ff7623e2 100644 --- a/light-node/src/commands/initialize.rs +++ b/light-node/src/commands/initialize.rs @@ -68,10 +68,8 @@ fn initialize_subjectively( config: &LightClientConfig, timeout: Option, ) -> Result { - let db = sled::open(config.db_path.clone()) - .map_err(|e| format!("could not open database: {}", e))?; - - let light_store = SledStore::new(db); + let light_store = + SledStore::open(&config.db_path).map_err(|e| format!("could not open database: {}", e))?; if let Some(trusted_state) = light_store.latest_trusted_or_verified() { status_warn!( diff --git a/light-node/src/commands/start.rs b/light-node/src/commands/start.rs index 2beedf709..d8e63eef1 100644 --- a/light-node/src/commands/start.rs +++ b/light-node/src/commands/start.rs @@ -96,10 +96,11 @@ impl config::Override for StartCmd { impl StartCmd { fn assert_init_was_run() -> Result<(), String> { - let db_path = app_config().light_clients.first().unwrap().db_path.clone(); - let db = sled::open(db_path).map_err(|e| format!("could not open database: {}", e))?; + let config = app_config(); + let db_path = &config.light_clients.first().unwrap().db_path; + let primary_store = + SledStore::open(db_path).map_err(|e| format!("could not open database: {}", e))?; - let primary_store = SledStore::new(db); if primary_store.latest_trusted_or_verified().is_none() { return Err("no trusted or verified state in store for primary, please initialize with the `initialize` subcommand first".to_string()); } @@ -127,10 +128,8 @@ impl StartCmd { let rpc_client = tendermint_rpc::HttpClient::new(light_config.address.clone()) .map_err(|e| format!("failed to create HTTP client: {}", e))?; - let db_path = light_config.db_path.clone(); - let db = sled::open(db_path).map_err(|e| format!("could not open database: {}", e))?; - - let light_store = SledStore::new(db); + let light_store = SledStore::open(&light_config.db_path) + .map_err(|e| format!("could not open database: {}", e))?; let builder = LightClientBuilder::prod( light_config.peer_id,