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