mithril_client_cli/commands/cardano_stake_distribution/
download.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use anyhow::{anyhow, Context};
use clap::Parser;
use std::sync::Arc;
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
};

use crate::utils::{ExpanderUtils, IndicatifFeedbackReceiver, ProgressOutputType, ProgressPrinter};
use crate::{
    commands::{client_builder, SharedArgs},
    configuration::{ConfigError, ConfigSource},
    CommandContext,
};
use mithril_client::common::Epoch;
use mithril_client::Client;
use mithril_client::{CardanoStakeDistribution, MessageBuilder, MithrilResult};

/// Download and verify a Cardano stake distribution information.
#[derive(Parser, Debug, Clone)]
pub struct CardanoStakeDistributionDownloadCommand {
    #[clap(flatten)]
    shared_args: SharedArgs,

    /// Epoch or hash of the Cardano stake distribution artifact.
    ///
    /// The epoch represents the epoch at the end of which the Cardano stake distribution is computed by the Cardano node.
    ///
    /// If `latest` is specified as unique_identifier, the command will return the latest Cardano stake distribution.
    unique_identifier: String,

    /// Directory where the Cardano stake distribution will be downloaded.
    #[clap(long)]
    download_dir: Option<PathBuf>,

    /// Genesis Verification Key to check the certificate chain.
    #[clap(long, env = "GENESIS_VERIFICATION_KEY")]
    genesis_verification_key: Option<String>,
}

impl CardanoStakeDistributionDownloadCommand {
    /// Is JSON output enabled
    pub fn is_json_output_enabled(&self) -> bool {
        self.shared_args.json
    }

    /// Main command execution
    pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
        let params = context.config_parameters()?.add_source(self)?;
        let download_dir = params.get_or("download_dir", ".");
        let download_dir = Path::new(&download_dir);
        let logger = context.logger();

        let progress_output_type = if self.is_json_output_enabled() {
            ProgressOutputType::JsonReporter
        } else {
            ProgressOutputType::Tty
        };
        let progress_printer = ProgressPrinter::new(progress_output_type, 4);
        let client = client_builder(&params)?
            .add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
                progress_output_type,
                logger.clone(),
            )))
            .with_logger(logger.clone())
            .build()?;

        progress_printer.report_step(
            1,
            &format!(
                "Fetching Cardano stake distribution for identifier: '{}' …",
                self.unique_identifier
            ),
        )?;
        let cardano_stake_distribution =
            Self::fetch_cardano_stake_distribution_from_unique_identifier(
                &client,
                &self.unique_identifier,
            )
            .await
            .with_context(|| {
                format!(
                    "Can not fetch Cardano stake distribution from unique identifier: '{}'",
                    &self.unique_identifier
                )
            })?;

        progress_printer.report_step(
            2,
            "Fetching the certificate and verifying the certificate chain…",
        )?;
        let certificate = client
            .certificate()
            .verify_chain(&cardano_stake_distribution.certificate_hash)
            .await
            .with_context(|| {
                format!(
                    "Can not verify the certificate chain from certificate_hash: '{}'",
                    &cardano_stake_distribution.certificate_hash
                )
            })?;

        progress_printer.report_step(
            3,
            "Verify that the Cardano stake distribution is signed in the associated certificate",
        )?;
        let message = MessageBuilder::new()
            .compute_cardano_stake_distribution_message(&certificate, &cardano_stake_distribution)
            .with_context(|| {
                "Can not compute the message for the given Cardano stake distribution"
            })?;

        if !certificate.match_message(&message) {
            return Err(anyhow!(
                    "Certificate and message did not match:\ncertificate_message: '{}'\n computed_message: '{}'",
                    certificate.signed_message,
                    message.compute_hash()
                ));
        }

        progress_printer.report_step(4, "Writing fetched Cardano stake distribution to a file")?;
        if !download_dir.is_dir() {
            std::fs::create_dir_all(download_dir)?;
        }
        let filepath = PathBuf::new().join(download_dir).join(format!(
            "cardano_stake_distribution-{}.json",
            cardano_stake_distribution.epoch
        ));
        std::fs::write(
            &filepath,
            serde_json::to_string(&cardano_stake_distribution).with_context(|| {
                format!(
                    "Can not serialize Cardano stake distribution artifact '{:?}'",
                    cardano_stake_distribution
                )
            })?,
        )?;

        if self.is_json_output_enabled() {
            println!(
                r#"{{"cardano_stake_distribution_epoch": "{}", "filepath": "{}"}}"#,
                cardano_stake_distribution.epoch,
                filepath.display()
            );
        } else {
            println!(
                "Cardano stake distribution for epoch '{}' has been verified and saved as '{}'.",
                cardano_stake_distribution.epoch,
                filepath.display()
            );
        }

        Ok(())
    }

    fn is_sha256_hash(identifier: &str) -> bool {
        identifier.len() == 64 && identifier.chars().all(|c| c.is_ascii_hexdigit())
    }

    // The unique identifier can be either a SHA256 hash, an epoch,  or 'latest'.
    async fn fetch_cardano_stake_distribution_from_unique_identifier(
        client: &Client,
        unique_identifier: &str,
    ) -> MithrilResult<CardanoStakeDistribution> {
        let cardano_stake_distribution = if Self::is_sha256_hash(unique_identifier) {
            client
                .cardano_stake_distribution()
                .get(unique_identifier)
                .await
                .with_context(|| {
                    format!(
                        "Can not download and verify the artifact for hash: '{}'",
                        unique_identifier
                    )
                })?
                .ok_or(anyhow!(
                    "No Cardano stake distribution could be found for hash: '{}'",
                    unique_identifier
                ))
        } else {
            let epoch = {
                let get_list_of_artifact_epochs = || async {
                    let cardano_stake_distributions = client.cardano_stake_distribution().list().await.with_context(|| {
                        "Can not get the list of artifacts while retrieving the latest Cardano stake distribution epoch"
                    })?;

                    Ok(cardano_stake_distributions
                        .iter()
                        .map(|csd| csd.epoch.to_string())
                        .collect::<Vec<String>>())
                };

                let epoch = ExpanderUtils::expand_eventual_id_alias(
                    unique_identifier,
                    get_list_of_artifact_epochs(),
                )
                .await?;

                Epoch(
                    epoch.parse().with_context(|| {
                        format!("Can not convert: '{}' into a valid Epoch", epoch)
                    })?,
                )
            };

            client
                .cardano_stake_distribution()
                .get_by_epoch(epoch)
                .await
                .with_context(|| {
                    format!(
                        "Can not download and verify the artifact for epoch: '{}'",
                        epoch
                    )
                })?
                .ok_or(anyhow!(
                    "No Cardano stake distribution could be found for epoch: '{}'",
                    epoch
                ))
        };

        cardano_stake_distribution
    }
}

impl ConfigSource for CardanoStakeDistributionDownloadCommand {
    fn collect(&self) -> Result<HashMap<String, String>, ConfigError> {
        let mut map = HashMap::new();

        if let Some(download_dir) = self.download_dir.clone() {
            map.insert(
                "download_dir".to_string(),
                download_dir
                    .to_str()
                    .ok_or_else(|| {
                        ConfigError::Conversion(format!(
                            "Could not read download directory: '{}'.",
                            download_dir.display()
                        ))
                    })?
                    .to_string(),
            );
        }

        if let Some(genesis_verification_key) = self.genesis_verification_key.clone() {
            map.insert(
                "genesis_verification_key".to_string(),
                genesis_verification_key,
            );
        }

        Ok(map)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_sha_256_returns_false_with_len_different_than_64_and_hex_digit() {
        let len_65_hex_digit = "65aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        assert!(!CardanoStakeDistributionDownloadCommand::is_sha256_hash(
            len_65_hex_digit
        ));

        let len_63_hex_digit = "63aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        assert!(!CardanoStakeDistributionDownloadCommand::is_sha256_hash(
            len_63_hex_digit
        ));
    }

    #[test]
    fn is_sha_256_returns_false_with_len_equal_to_64_and_not_hex_digit() {
        let len_64_not_hex_digit =
            "64zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
        assert!(!CardanoStakeDistributionDownloadCommand::is_sha256_hash(
            len_64_not_hex_digit
        ));
    }

    #[test]
    fn is_sha_256_returns_true_with_len_equal_to_64_and_hex_digit() {
        let len_64_hex_digit = "64aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        assert!(CardanoStakeDistributionDownloadCommand::is_sha256_hash(
            len_64_hex_digit
        ));
    }
}