Skip to content

Commit d2c98c9

Browse files
Alenarjpraynaud
authored andcommitted
Put snapshot download & snapshot message compute behind a 'fs' feature
- This feature is disabled by default - A `full` feature is also added to enable it and any other features that we would add in the future
1 parent d02447f commit d2c98c9

File tree

7 files changed

+221
-149
lines changed

7 files changed

+221
-149
lines changed

mithril-client/Cargo.toml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,35 @@ include = ["**/*.rs", "Cargo.toml", "README.md", ".gitignore"]
1313
[lib]
1414
crate-type = ["lib", "cdylib", "staticlib"]
1515

16+
[[example]]
17+
name = "snapshot_list_get_show_download_verify"
18+
path = "examples/snapshot_list_get_show_download_verify.rs"
19+
required-features = ["fs"]
20+
21+
[[test]]
22+
name = "snapshot_list_get_show_download_verify"
23+
path = "tests/snapshot_list_get_show_download_verify.rs"
24+
required-features = ["fs"]
25+
1626
[dependencies]
1727
anyhow = "1.0.75"
1828
async-recursion = "1.0.5"
1929
async-trait = "0.1.73"
2030
chrono = { version = "0.4.31", features = ["serde"] }
21-
flate2 = "1.0.27"
22-
flume = "0.11.0"
31+
flate2 = { version = "1.0.27", optional = true }
32+
flume = { version = "0.11.0", optional = true }
2333
futures = "0.3.28"
2434
mithril-common = { path = "../mithril-common", version = "0.2" }
2535
reqwest = { version = "0.11.22", features = ["json", "stream"] }
2636
semver = "1.0.19"
2737
serde = { version = "1.0.188", features = ["derive"] }
2838
serde_json = "1.0.107"
2939
slog = "2.7.0"
30-
tar = "0.4.40"
40+
tar = { version = "0.4.40", optional = true }
3141
thiserror = "1.0.49"
3242
tokio = { version = "1.32.0", features = ["sync"] }
3343
uuid = { version = "1.5.0", features = ["v4"] }
34-
zstd = "0.13.0"
44+
zstd = { version = "0.13.0", optional = true }
3545

3646
[dev-dependencies]
3747
httpmock = "0.6.8"
@@ -47,4 +57,14 @@ tokio = { version = "1.32.0", features = ["macros", "rt"] }
4757
warp = "0.3"
4858

4959
[features]
60+
# Include nothing by default
61+
default = []
62+
full = ["fs"]
63+
# Enable file system releated functionnality, right now that mean ony snapshot download
64+
fs = ["flate2", "flume", "tar", "tokio/rt", "zstd"]
5065
portable = ["mithril-common/portable"]
66+
67+
[package.metadata.docs.rs]
68+
all-features = true
69+
# enable unstable features in the documentation
70+
rustdoc-args = ["--cfg", "docsrs"]

mithril-client/src/client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::certificate_client::{
55
use crate::feedback::{FeedbackReceiver, FeedbackSender};
66
use crate::mithril_stake_distribution_client::MithrilStakeDistributionClient;
77
use crate::snapshot_client::SnapshotClient;
8+
#[cfg(feature = "fs")]
89
use crate::snapshot_downloader::{HttpSnapshotDownloader, SnapshotDownloader};
910
use crate::MithrilResult;
1011
use anyhow::{anyhow, Context};
@@ -45,6 +46,7 @@ pub struct ClientBuilder {
4546
genesis_verification_key: String,
4647
aggregator_client: Option<Arc<dyn AggregatorClient>>,
4748
certificate_verifier: Option<Arc<dyn CertificateVerifier>>,
49+
#[cfg(feature = "fs")]
4850
snapshot_downloader: Option<Arc<dyn SnapshotDownloader>>,
4951
logger: Option<Logger>,
5052
feedback_receivers: Vec<Arc<dyn FeedbackReceiver>>,
@@ -59,6 +61,7 @@ impl ClientBuilder {
5961
genesis_verification_key: genesis_verification_key.to_string(),
6062
aggregator_client: None,
6163
certificate_verifier: None,
64+
#[cfg(feature = "fs")]
6265
snapshot_downloader: None,
6366
logger: None,
6467
feedback_receivers: vec![],
@@ -75,6 +78,7 @@ impl ClientBuilder {
7578
genesis_verification_key: genesis_verification_key.to_string(),
7679
aggregator_client: None,
7780
certificate_verifier: None,
81+
#[cfg(feature = "fs")]
7882
snapshot_downloader: None,
7983
logger: None,
8084
feedback_receivers: vec![],
@@ -115,6 +119,7 @@ impl ClientBuilder {
115119
Some(client) => client,
116120
};
117121

122+
#[cfg(feature = "fs")]
118123
let snapshot_downloader = match self.snapshot_downloader {
119124
None => Arc::new(
120125
HttpSnapshotDownloader::new(feedback_sender.clone(), logger.clone())
@@ -146,6 +151,7 @@ impl ClientBuilder {
146151
));
147152
let snapshot_client = Arc::new(SnapshotClient::new(
148153
aggregator_client,
154+
#[cfg(feature = "fs")]
149155
snapshot_downloader,
150156
feedback_sender,
151157
logger,
@@ -176,6 +182,7 @@ impl ClientBuilder {
176182
self
177183
}
178184

185+
cfg_fs! {
179186
/// Set the [SnapshotDownloader] that will be used to download snapshots.
180187
pub fn with_snapshot_downloader(
181188
mut self,
@@ -184,6 +191,7 @@ impl ClientBuilder {
184191
self.snapshot_downloader = Some(snapshot_downloader);
185192
self
186193
}
194+
}
187195

188196
/// Set the [Logger] to use.
189197
pub fn with_logger(mut self, logger: Logger) -> Self {

mithril-client/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(missing_docs)]
2+
#![cfg_attr(docsrs, feature(doc_cfg))]
23

34
//! Define all the tooling necessary to manipulate Mithril certified types from a
45
//! [Mithril Aggregator](https://mithril.network/rust-doc/mithril_aggregator/index.html).
@@ -19,6 +20,8 @@
1920
//!
2021
//! Below is an example describing the usage of most of the library's functions together:
2122
//!
23+
//! **Note:** _Snapshot download and the compute snapshot message functions are available using crate feature_ **fs**.
24+
//!
2225
//! ```no_run
2326
//! # async fn run() -> mithril_client::MithrilResult<()> {
2427
//! use mithril_client::{ClientBuilder, MessageBuilder};
@@ -52,14 +55,26 @@
5255
//! # }
5356
//! ```
5457
58+
macro_rules! cfg_fs {
59+
($($item:item)*) => {
60+
$(
61+
#[cfg(feature = "fs")]
62+
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
63+
$item
64+
)*
65+
}
66+
}
67+
5568
pub mod aggregator_client;
5669
pub mod certificate_client;
5770
mod client;
5871
pub mod feedback;
5972
mod message;
6073
pub mod mithril_stake_distribution_client;
6174
pub mod snapshot_client;
62-
pub mod snapshot_downloader;
75+
cfg_fs! {
76+
pub mod snapshot_downloader;
77+
}
6378
mod type_alias;
6479
mod utils;
6580

mithril-client/src/message.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,31 @@ impl MessageBuilder {
2525
}
2626
}
2727

28-
/// Set the [ImmutableDigester] to be used for the message computation for snapshot.
29-
///
30-
/// If not set a default implementation will be used.
31-
pub fn with_immutable_digester(
32-
mut self,
33-
immutable_digester: Arc<dyn ImmutableDigester>,
34-
) -> Self {
35-
self.immutable_digester = Some(immutable_digester);
36-
self
37-
}
38-
3928
/// Set the [Logger] to use.
4029
pub fn with_logger(mut self, logger: Logger) -> Self {
4130
self.logger = logger;
4231
self
4332
}
4433

34+
cfg_fs! {
4535
fn get_immutable_digester(&self) -> Arc<dyn ImmutableDigester> {
4636
match self.immutable_digester.as_ref() {
4737
None => Arc::new(CardanoImmutableDigester::new(None, self.logger.clone())),
4838
Some(digester) => digester.clone(),
4939
}
5040
}
5141

42+
/// Set the [ImmutableDigester] to be used for the message computation for snapshot.
43+
///
44+
/// If not set a default implementation will be used.
45+
pub fn with_immutable_digester(
46+
mut self,
47+
immutable_digester: Arc<dyn ImmutableDigester>,
48+
) -> Self {
49+
self.immutable_digester = Some(immutable_digester);
50+
self
51+
}
52+
5253
/// Compute message for a snapshot (based on the directory where it was unpacked).
5354
///
5455
/// Warning: this operation can be quite long depending on the snapshot size.
@@ -74,6 +75,7 @@ impl MessageBuilder {
7475

7576
Ok(message)
7677
}
78+
}
7779

7880
/// Compute message for a Mithril stake distribution.
7981
pub fn compute_mithril_stake_distribution_message(

mithril-client/src/snapshot_client.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
//! ```
4141
//!
4242
//! # Download a snapshot
43+
//! **Note:** _Available on crate feature_ **fs** _only._
4344
//!
4445
//! To download and simultaneously unpack the tarball of a snapshots using the [ClientBuilder][crate::client::ClientBuilder].
4546
//!
@@ -63,12 +64,13 @@
6364
//! ```
6465
6566
use anyhow::Context;
66-
use slog::{warn, Logger};
67-
use std::{path::Path, sync::Arc};
67+
use slog::Logger;
68+
use std::sync::Arc;
6869
use thiserror::Error;
6970

7071
use crate::aggregator_client::{AggregatorClient, AggregatorClientError, AggregatorRequest};
71-
use crate::feedback::{FeedbackSender, MithrilEvent};
72+
use crate::feedback::FeedbackSender;
73+
#[cfg(feature = "fs")]
7274
use crate::snapshot_downloader::SnapshotDownloader;
7375
use crate::{MithrilResult, Snapshot, SnapshotListItem};
7476

@@ -89,6 +91,7 @@ pub enum SnapshotClientError {
8991
/// Aggregator client for the snapshot artifact
9092
pub struct SnapshotClient {
9193
aggregator_client: Arc<dyn AggregatorClient>,
94+
#[cfg(feature = "fs")]
9295
snapshot_downloader: Arc<dyn SnapshotDownloader>,
9396
feedback_sender: FeedbackSender,
9497
logger: Logger,
@@ -98,12 +101,13 @@ impl SnapshotClient {
98101
/// Constructs a new `SnapshotClient`.
99102
pub fn new(
100103
aggregator_client: Arc<dyn AggregatorClient>,
101-
snapshot_downloader: Arc<dyn SnapshotDownloader>,
104+
#[cfg(feature = "fs")] snapshot_downloader: Arc<dyn SnapshotDownloader>,
102105
feedback_sender: FeedbackSender,
103106
logger: Logger,
104107
) -> Self {
105108
Self {
106109
aggregator_client,
110+
#[cfg(feature = "fs")]
107111
snapshot_downloader,
108112
feedback_sender,
109113
logger,
@@ -143,15 +147,18 @@ impl SnapshotClient {
143147
}
144148
}
145149

150+
cfg_fs! {
146151
/// Download and unpack the given snapshot to the given directory
147152
///
148153
/// **NOTE**: The directory should already exist, and the user running the binary
149154
/// must have read/write access to it.
150155
pub async fn download_unpack(
151156
&self,
152157
snapshot: &Snapshot,
153-
target_dir: &Path,
158+
target_dir: &std::path::Path,
154159
) -> MithrilResult<()> {
160+
use crate::feedback::MithrilEvent;
161+
155162
for location in snapshot.locations.as_slice() {
156163
if self.snapshot_downloader.probe(location).await.is_ok() {
157164
let download_id = MithrilEvent::new_snapshot_download_id();
@@ -182,7 +189,7 @@ impl SnapshotClient {
182189
Ok(())
183190
}
184191
Err(e) => {
185-
warn!(
192+
slog::warn!(
186193
self.logger,
187194
"Failed downloading snapshot from '{location}' Error: {e}."
188195
);
@@ -200,14 +207,18 @@ impl SnapshotClient {
200207
}
201208
.into())
202209
}
210+
}
203211
}
204212

205-
#[cfg(test)]
206-
mod tests {
213+
#[cfg(all(test, feature = "fs"))]
214+
mod tests_download {
207215
use crate::{
208-
aggregator_client::MockAggregatorHTTPClient, feedback::StackFeedbackReceiver,
209-
snapshot_downloader::MockHttpSnapshotDownloader, test_utils,
216+
aggregator_client::MockAggregatorHTTPClient,
217+
feedback::{MithrilEvent, StackFeedbackReceiver},
218+
snapshot_downloader::MockHttpSnapshotDownloader,
219+
test_utils,
210220
};
221+
use std::path::Path;
211222

212223
use super::*;
213224

mithril-client/src/utils/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Utilities module
22
//! This module contains tools needed mostly for the snapshot download and unpack.
33
4-
mod stream_reader;
5-
mod unpacker;
4+
cfg_fs! {
5+
mod stream_reader;
6+
mod unpacker;
67

7-
pub use stream_reader::*;
8-
pub use unpacker::*;
8+
pub use stream_reader::*;
9+
pub use unpacker::*;
10+
}

0 commit comments

Comments
 (0)