mithril_client/cardano_database_client/
mod.rs

1//! A client to retrieve Cardano databases data from an Aggregator.
2//!
3//! In order to do so it defines a [CardanoDatabaseClient] which exposes the following features:
4//!  - [get][CardanoDatabaseClient::get]: get a Cardano database data from its hash
5//!  - [list][CardanoDatabaseClient::list]: get the list of available Cardano database
6//!  - [download_unpack][CardanoDatabaseClient::download_unpack]: download and unpack a Cardano database snapshot for a given immutable files range
7//!  - [download_and_verify_digests][CardanoDatabaseClient::download_and_verify_digests]: download and verify the digests of the immutable files in a Cardano database snapshot
8//! # Get a Cardano database
9//!
10//! To get a Cardano database using the [ClientBuilder][crate::client::ClientBuilder].
11//!
12//! ```no_run
13//! # async fn run() -> mithril_client::MithrilResult<()> {
14//! use mithril_client::ClientBuilder;
15//!
16//! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
17//! let cardano_database = client.cardano_database_v2().get("CARDANO_DATABASE_HASH").await?.unwrap();
18//!
19//! println!(
20//!     "Cardano database hash={}, merkle_root={}, immutable_file_number={:?}",
21//!     cardano_database.hash,
22//!     cardano_database.merkle_root,
23//!     cardano_database.beacon.immutable_file_number
24//! );
25//! #    Ok(())
26//! # }
27//! ```
28//!
29//! # List available Cardano databases
30//!
31//! To list available Cardano databases using the [ClientBuilder][crate::client::ClientBuilder].
32//!
33//! ```no_run
34//! # async fn run() -> mithril_client::MithrilResult<()> {
35//! use mithril_client::ClientBuilder;
36//!
37//! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
38//! let cardano_databases = client.cardano_database_v2().list().await?;
39//!
40//! for cardano_database in cardano_databases {
41//!     println!("Cardano database hash={}, immutable_file_number={}", cardano_database.hash, cardano_database.beacon.immutable_file_number);
42//! }
43//! #    Ok(())
44//! # }
45//! ```
46//!
47//! # List available Cardano databases filtered by an epoch
48//!
49//! To list available Cardano databases using the [ClientBuilder][crate::client::ClientBuilder].
50//!
51//! ```no_run
52//! # async fn run() -> mithril_client::MithrilResult<()> {
53//! use mithril_client::{ClientBuilder, common::Epoch};
54//!
55//! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
56//!
57//! // For a specific epoch
58//! let cardano_databases = client.cardano_database_v2().list_by_epoch(Epoch(8)).await?;
59//! // For the latest epoch known by the Mithril aggregator
60//! let cardano_databases = client.cardano_database_v2().list_for_latest_epoch().await?;
61//! // For the latest epoch known by the Mithril aggregator with an offset
62//! let cardano_databases = client.cardano_database_v2().list_for_latest_epoch_with_offset(4).await?;
63//!
64//! for cardano_database in cardano_databases {
65//!     println!("Cardano database hash={}, immutable_file_number={}", cardano_database.hash, cardano_database.beacon.immutable_file_number);
66//! }
67//! #    Ok(())
68//! # }
69//! ```
70//!
71//! # Download a Cardano database snapshot
72//! **Note:** _Available on crate feature_ **fs** _only._
73//!
74//! To download a partial or a full Cardano database folder the [ClientBuilder][crate::client::ClientBuilder].
75//!
76//! ```no_run
77//! # #[cfg(feature = "fs")]
78//! # async fn run() -> mithril_client::MithrilResult<()> {
79//! use mithril_client::{ClientBuilder, cardano_database_client::{ImmutableFileRange, DownloadUnpackOptions}};
80//! use std::path::Path;
81//!
82//! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
83//! let cardano_database_snapshot = client.cardano_database_v2().get("CARDANO_DATABASE_HASH").await?.unwrap();
84//!
85//! // Note: the directory must already exist, and the user running the binary must have read/write access to it.
86//! let target_directory = Path::new("/home/user/download/");
87//! let immutable_file_range = ImmutableFileRange::Range(3, 6);
88//! let download_unpack_options = DownloadUnpackOptions {
89//!     allow_override: true,
90//!     include_ancillary: true,
91//!     ..DownloadUnpackOptions::default()
92//! };
93//! client
94//!     .cardano_database_v2()
95//!     .download_unpack(
96//!         &cardano_database_snapshot,
97//!         &immutable_file_range,
98//!         &target_directory,
99//!         download_unpack_options,
100//!     )
101//!     .await?;
102//! #
103//! #    Ok(())
104//! # }
105//! ```
106//! # Compute a Merkle proof for a Cardano database snapshot
107//! **Note:** _Available on crate feature_ **fs** _only._
108//!
109//! To compute proof of membership of downloaded immutable files in a Cardano database folder the [ClientBuilder][crate::client::ClientBuilder].
110//!
111//! ```no_run
112//! # #[cfg(feature = "fs")]
113//! # async fn run() -> mithril_client::MithrilResult<()> {
114//! use mithril_client::{ClientBuilder, MessageBuilder, cardano_database_client::{ImmutableFileRange, DownloadUnpackOptions}};
115//! use std::path::Path;
116//!
117//! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
118//! let cardano_database_snapshot = client.cardano_database_v2().get("CARDANO_DATABASE_HASH").await?.unwrap();
119//! let certificate = client.certificate().verify_chain(&cardano_database_snapshot.certificate_hash).await?;
120//!
121//! // Note: the directory must already exist, and the user running the binary must have read/write access to it.
122//! let target_directory = Path::new("/home/user/download/");
123//! let immutable_file_range = ImmutableFileRange::Full;
124//! let download_unpack_options = DownloadUnpackOptions {
125//!     allow_override: true,
126//!     include_ancillary: true,
127//!     ..DownloadUnpackOptions::default()
128//! };
129//! client
130//!     .cardano_database_v2()
131//!     .download_unpack(
132//!         &cardano_database_snapshot,
133//!         &immutable_file_range,
134//!         &target_directory,
135//!         download_unpack_options,
136//!     )
137//!     .await?;
138//!
139//! let verified_digests = client
140//!     .cardano_database_v2()
141//!     .download_and_verify_digests(
142//!         &certificate,
143//!         &cardano_database_snapshot)
144//!     .await?;
145//!
146//! let allow_missing_immutables_files = false;
147//! let merkle_proof = client
148//!    .cardano_database_v2()
149//!    .verify_cardano_database(
150//!        &certificate,
151//!       &cardano_database_snapshot,
152//!       &immutable_file_range,
153//!      allow_missing_immutables_files,
154//!      &target_directory,
155//!      &verified_digests
156//!  ).await?;
157//!
158//!
159//! let message = MessageBuilder::new().compute_cardano_database_message(
160//!         &certificate,
161//!         &merkle_proof,
162//!     ).await?;
163//! #
164//! #    Ok(())
165//! # }
166//! ```
167mod api;
168mod fetch;
169mod statistics;
170
171pub use api::CardanoDatabaseClient;
172#[cfg(test)]
173pub(crate) use api::test_dependency_injector::CardanoDatabaseClientDependencyInjector;
174
175cfg_fs! {
176    mod immutable_file_range;
177    mod download_unpack;
178    mod proving;
179
180    pub use download_unpack::DownloadUnpackOptions;
181    pub use immutable_file_range::ImmutableFileRange;
182    pub use proving::{CardanoDatabaseVerificationError, ImmutableVerificationResult};
183    pub use proving::VerifiedDigests;
184}