Skip to main content

mithril_client/
lib.rs

1#![warn(missing_docs)]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3// TODO: Remove this allow once migration from deprecated AggregatorClient types is complete
4#![allow(deprecated)]
5
6//! Define all the tooling necessary to manipulate Mithril certified types from a
7//! [Mithril Aggregator](https://mithril.network/rust-doc/mithril_aggregator/index.html).
8//!
9//! It handles the different types that can be queried to a Mithril aggregator:
10//!
11//! - [Cardano Database v2][cardano_database_client] list, get, download archive and record statistics.
12//! - [Cardano transactions][cardano_transaction_client] list & get snapshot, get proofs.
13//! - [Cardano stake distribution][cardano_stake_distribution_client] list, get and get by epoch.
14//! - [Mithril stake distribution][mithril_stake_distribution_client] list and get.
15//! - [Certificates][certificate_client] list, get, and chain validation.
16//! - [MithrilEraClient][era]: retrieve the current Mithril era.
17//!
18//! The [Client] aggregates the queries of all of those types.
19//!
20//! **NOTE:** Snapshot download and Certificate chain validation can take quite some time even with a fast
21//! computer and network.
22//! For those a feedback mechanism is available, more details on it in the [feedback] submodule.
23//!
24//! # Example
25//!
26//! Below is an example describing the usage of most of the library's functions together:
27//!
28//! **Note:** _Snapshot download and the compute snapshot message functions are available using crate feature_ **fs**.
29//!
30//! ```no_run
31//! # #[cfg(feature = "fs")]
32//! # async fn run() -> mithril_client::MithrilResult<()> {
33//! use mithril_client::{ClientBuilder, MessageBuilder};
34//! use mithril_client::cardano_database_client::{DownloadUnpackOptions, ImmutableFileRange};
35//! use std::path::Path;
36//!
37//! let client =
38//!     ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY")
39//!         .build()?;
40//! let cardano_database_snapshot = client
41//!     .cardano_database_v2()
42//!     .get("CARDANO_DATABASE_HASH")
43//!     .await?
44//!     .unwrap();
45//!
46//! let certificate = client
47//!     .certificate()
48//!     .verify_chain(&cardano_database_snapshot.certificate_hash)
49//!     .await?;
50//!
51//! // Note: the directory must already exist, and the user running the binary must have read/write access to it.
52//! let target_directory = Path::new("/home/user/download/");
53//! let immutable_file_range = ImmutableFileRange::Range(3, 6);
54//! let download_unpack_options = DownloadUnpackOptions {
55//!     allow_override: true,
56//!     include_ancillary: true,
57//!     ..DownloadUnpackOptions::default()
58//! };
59//! client
60//!     .cardano_database_v2()
61//!     .download_unpack(
62//!         &cardano_database_snapshot,
63//!         &immutable_file_range,
64//!         &target_directory,
65//!         download_unpack_options,
66//!     )
67//!     .await?;
68//!
69//! let verified_digests = client
70//!     .cardano_database_v2()
71//!     .download_and_verify_digests(&certificate, &cardano_database_snapshot)
72//!     .await?;
73//!
74//! let full_restoration = immutable_file_range == ImmutableFileRange::Full;
75//! let include_ancillary = download_unpack_options.include_ancillary;
76//! let number_of_immutable_files_restored =
77//!     immutable_file_range.length(cardano_database_snapshot.beacon.immutable_file_number);
78//! if let Err(error) = client
79//!     .cardano_database_v2()
80//!     .add_statistics(
81//!         full_restoration,
82//!         include_ancillary,
83//!         number_of_immutable_files_restored,
84//!     )
85//!     .await
86//! {
87//!     println!("Could not increment snapshot download statistics: {error:?}");
88//! }
89//!
90//! let allow_missing_immutables_files = false;
91//! let merkle_proof = client
92//!     .cardano_database_v2()
93//!     .verify_cardano_database(
94//!         &certificate,
95//!         &cardano_database_snapshot,
96//!         &immutable_file_range,
97//!         allow_missing_immutables_files,
98//!         &target_directory,
99//!         &verified_digests,
100//!     )
101//!     .await?;
102//!
103//! let message = MessageBuilder::new()
104//!     .compute_cardano_database_message(&certificate, &merkle_proof)
105//!     .await?;
106//!
107//! assert!(certificate.match_message(&message));
108//! #    Ok(())
109//! # }
110//! ```
111//!
112//! ## Optional Features
113//!
114//! The following is a list of [Cargo features](https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section) that can be
115//! enabled or disabled:
116//!
117//! - **fs**: Enables file system-related functionalities.
118//! - **unstable**: Enables experimental or in-development `mithril-client` features that may change.
119//! - **rug-backend**: Enables usage of `rug` numerical backend in `mithril-stm` (dependency of `mithril-common`).
120//! - **num-integer-backend**: Enables usage of `num-integer` numerical backend in `mithril-stm` (dependency of `mithril-common`).
121//!
122//! To allow fine-tuning of the http queries, the following [Reqwest](https://docs.rs/reqwest/latest/reqwest/#optional-features) features are re-exported.
123//! No TLS backend is enabled by default: you must enable at least one of the TLS-related
124//! features below, otherwise the crate will fail to compile.
125//! - **native-tls**: Enables TLS functionality provided by `native-tls`.
126//! - **native-tls-vendored**: Enables the `vendored` feature of `native-tls`.
127//! - **native-tls-no-alpn**: Enables `native-tls` without its `alpn` feature.
128//! - **native-tls-vendored-no-alpn**: Enables the `vendored` feature of `native-tls` without its `alpn` feature.
129//! - **rustls**: Enables TLS functionality provided by `rustls`.
130//! - **rustls-no-provider**: Enables TLS functionality provided by `rustls` without setting `aws-lc-rs` as its TLS provider.
131
132// Ensure at least one TLS backend is enabled
133#[cfg(not(any(
134    feature = "native-tls",
135    feature = "native-tls-no-alpn",
136    feature = "native-tls-vendored",
137    feature = "native-tls-vendored-no-alpn",
138    feature = "rustls",
139    feature = "rustls-no-provider",
140)))]
141compile_error!(
142    "At least one TLS backend feature must be enabled. Choose from: \
143    'native-tls', 'native-tls-no-alpn', 'native-tls-vendored', 'native-tls-vendored-no-alpn', \
144    'rustls', or 'rustls-no-provider'"
145);
146
147macro_rules! cfg_fs {
148    ($($item:item)*) => {
149        $(
150            #[cfg(feature = "fs")]
151            #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
152            $item
153        )*
154    }
155}
156
157#[allow(unused_macros)]
158macro_rules! cfg_unstable {
159    ($($item:item)*) => {
160        $(
161            #[cfg(feature = "unstable")]
162            #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
163            $item
164        )*
165    }
166}
167
168#[allow(unused_macros)]
169macro_rules! cfg_fs_unstable {
170    ($($item:item)*) => {
171        $(
172            #[cfg(all(feature = "unstable", feature = "fs"))]
173            #[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", feature = "fs"))))]
174            $item
175        )*
176    }
177}
178
179mod aggregator_client;
180pub mod cardano_database_client;
181pub mod cardano_stake_distribution_client;
182pub mod cardano_transaction_client;
183cfg_unstable! {
184    pub mod cardano_block_client;
185    pub mod cardano_transaction_v2_client;
186}
187pub mod certificate_client;
188mod client;
189pub mod era;
190pub mod feedback;
191mod message;
192pub mod mithril_stake_distribution_client;
193cfg_fs! {
194    pub mod file_downloader;
195}
196
197mod type_alias;
198mod utils;
199
200pub use client::*;
201pub use message::*;
202pub use type_alias::*;
203
204#[cfg(test)]
205pub(crate) mod test_utils {
206    mithril_common::define_test_logger!();
207}