mithril-client 0.14.5

Mithril client library
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
use std::collections::HashMap;
#[cfg(not(target_family = "wasm"))]
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{Context, anyhow};
#[cfg(any(feature = "fs", not(target_family = "wasm")))]
use chrono::Utc;
#[cfg(not(target_family = "wasm"))]
use rand::SeedableRng;
#[cfg(not(target_family = "wasm"))]
use rand::rngs::StdRng;
use serde::{Deserialize, Serialize};
use slog::{Logger, o};

use mithril_aggregator_client::AggregatorHttpClient;
#[cfg(not(target_family = "wasm"))]
use mithril_aggregator_discovery::{
    AggregatorDiscoverer, AggregatorEndpoint, AggregatorEndpointWithCapabilities,
    CapableAggregatorDiscoverer, HttpConfigAggregatorDiscoverer, RequiredAggregatorCapabilities,
    ShuffleAggregatorDiscoverer,
};
use mithril_common::{MITHRIL_CLIENT_TYPE_HEADER, MITHRIL_ORIGIN_TAG_HEADER};

use crate::MithrilResult;
#[cfg(feature = "unstable")]
use crate::cardano_block_client::CardanoBlockClient;
use crate::cardano_database_client::CardanoDatabaseClient;
use crate::cardano_stake_distribution_client::CardanoStakeDistributionClient;
use crate::cardano_transaction_client::CardanoTransactionClient;
#[cfg(feature = "unstable")]
use crate::cardano_transaction_v2_client::CardanoTransactionV2Client;
#[cfg(feature = "unstable")]
use crate::certificate_client::CertificateVerifierCache;
use crate::certificate_client::{
    CertificateClient, CertificateVerifier, MithrilCertificateVerifier,
};
#[cfg(not(target_family = "wasm"))]
use crate::common::MithrilNetwork;
use crate::era::{EraFetcher, MithrilEraClient};
use crate::feedback::{FeedbackReceiver, FeedbackSender};
#[cfg(feature = "fs")]
use crate::file_downloader::{
    FileDownloadRetryPolicy, FileDownloader, HttpFileDownloader, RetryDownloader,
};
use crate::mithril_stake_distribution_client::MithrilStakeDistributionClient;
#[cfg(feature = "fs")]
use crate::utils::AncillaryVerifier;
#[cfg(feature = "fs")]
use crate::utils::TimestampTempDirectoryProvider;

const DEFAULT_CLIENT_TYPE: &str = "LIBRARY";

#[cfg(target_family = "wasm")]
const fn one_week_in_seconds() -> u32 {
    604800
}

/// The type of discovery to use to find the aggregator to connect to.
pub enum AggregatorDiscoveryType {
    /// Use a specific URL to connect to the aggregator.
    Url(String),
    /// Automatically discover the aggregator.
    #[cfg(not(target_family = "wasm"))]
    Automatic(MithrilNetwork),
}

#[cfg(not(target_family = "wasm"))]
impl FromStr for AggregatorDiscoveryType {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some(network) = s.strip_prefix("auto:") {
            Ok(AggregatorDiscoveryType::Automatic(MithrilNetwork::new(
                network.to_string(),
            )))
        } else {
            Ok(AggregatorDiscoveryType::Url(s.to_string()))
        }
    }
}

/// The genesis verification key.
pub enum GenesisVerificationKey {
    /// The verification key is provided as a JSON Hex-encoded string.
    JsonHex(String),
}

/// Options that can be used to configure the client.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ClientOptions {
    /// HTTP headers to include in the client requests.
    pub http_headers: Option<HashMap<String, String>>,

    /// Tag to retrieve the origin of the client requests.
    #[cfg(target_family = "wasm")]
    #[cfg_attr(target_family = "wasm", serde(default))]
    pub origin_tag: Option<String>,

    /// Whether to enable unstable features in the WASM client.
    #[cfg(target_family = "wasm")]
    #[cfg_attr(target_family = "wasm", serde(default))]
    pub unstable: bool,

    /// Whether to enable certificate chain verification caching in the WASM client.
    ///
    /// `unstable` must be set to `true` for this option to have any effect.
    ///
    /// DANGER: This feature is highly experimental and insecure, and it must not be used in production
    #[cfg(target_family = "wasm")]
    #[cfg_attr(target_family = "wasm", serde(default))]
    pub enable_certificate_chain_verification_cache: bool,

    /// Duration in seconds of certificate chain verification cache in the WASM client.
    ///
    /// Default to one week (604800 seconds).
    ///
    /// `enable_certificate_chain_verification_cache` and `unstable` must both be set to `true`
    /// for this option to have any effect.
    #[cfg(target_family = "wasm")]
    #[cfg_attr(target_family = "wasm", serde(default = "one_week_in_seconds"))]
    pub certificate_chain_verification_cache_duration_in_seconds: u32,
}

impl ClientOptions {
    /// Instantiate a new [ClientOptions].
    pub fn new(http_headers: Option<HashMap<String, String>>) -> Self {
        Self {
            http_headers,
            #[cfg(target_family = "wasm")]
            origin_tag: None,
            #[cfg(target_family = "wasm")]
            unstable: false,
            #[cfg(target_family = "wasm")]
            enable_certificate_chain_verification_cache: false,
            #[cfg(target_family = "wasm")]
            certificate_chain_verification_cache_duration_in_seconds: one_week_in_seconds(),
        }
    }

    /// Enable unstable features in the WASM client.
    #[cfg(target_family = "wasm")]
    pub fn with_unstable_features(self, unstable: bool) -> Self {
        Self { unstable, ..self }
    }
}

/// Structure that aggregates the available clients for each of the Mithril types of certified data.
///
/// Use the [ClientBuilder] to instantiate it easily.
#[derive(Clone)]
pub struct Client {
    certificate_client: Arc<CertificateClient>,
    mithril_stake_distribution_client: Arc<MithrilStakeDistributionClient>,
    cardano_database_client: Arc<CardanoDatabaseClient>,
    cardano_transaction_client: Arc<CardanoTransactionClient>,
    #[cfg(feature = "unstable")]
    cardano_transaction_v2_client: Arc<CardanoTransactionV2Client>,
    #[cfg(feature = "unstable")]
    cardano_block_client: Arc<CardanoBlockClient>,
    cardano_stake_distribution_client: Arc<CardanoStakeDistributionClient>,
    mithril_era_client: Arc<MithrilEraClient>,
}

impl Client {
    /// Get the client that fetches and verifies Mithril certificates.
    pub fn certificate(&self) -> Arc<CertificateClient> {
        self.certificate_client.clone()
    }

    /// Get the client that fetches Mithril stake distributions.
    pub fn mithril_stake_distribution(&self) -> Arc<MithrilStakeDistributionClient> {
        self.mithril_stake_distribution_client.clone()
    }

    /// Get the client that fetches and downloads Cardano database snapshots.
    pub fn cardano_database_v2(&self) -> Arc<CardanoDatabaseClient> {
        self.cardano_database_client.clone()
    }

    /// Get the client that fetches and verifies Mithril Cardano transaction proof.
    pub fn cardano_transaction(&self) -> Arc<CardanoTransactionClient> {
        self.cardano_transaction_client.clone()
    }

    cfg_unstable! {
        /// Get the client that fetches and verifies Mithril Cardano transaction v2 proof.
        pub fn cardano_transaction_v2(&self) -> Arc<CardanoTransactionV2Client> {
            self.cardano_transaction_v2_client.clone()
        }

        /// Get the client that fetches and verifies Mithril Cardano block proof.
        pub fn cardano_block(&self) -> Arc<CardanoBlockClient> {
            self.cardano_block_client.clone()
        }
    }
    /// Get the client that fetches Cardano stake distributions.
    pub fn cardano_stake_distribution(&self) -> Arc<CardanoStakeDistributionClient> {
        self.cardano_stake_distribution_client.clone()
    }

    /// Get the client that fetches the current Mithril era.
    pub fn mithril_era_client(&self) -> Arc<MithrilEraClient> {
        self.mithril_era_client.clone()
    }
}

/// Builder that can be used to create a [Client] easily or with custom dependencies.
pub struct ClientBuilder {
    aggregator_discovery: AggregatorDiscoveryType,
    #[cfg(not(target_family = "wasm"))]
    aggregator_capabilities: Option<RequiredAggregatorCapabilities>,
    #[cfg(not(target_family = "wasm"))]
    aggregator_discoverer: Option<Arc<dyn AggregatorDiscoverer<AggregatorEndpoint>>>,
    genesis_verification_key: Option<GenesisVerificationKey>,
    origin_tag: Option<String>,
    client_type: Option<String>,
    #[cfg(feature = "fs")]
    ancillary_verification_key: Option<String>,
    certificate_verifier: Option<Arc<dyn CertificateVerifier>>,
    #[cfg(feature = "fs")]
    http_file_downloader: Option<Arc<dyn FileDownloader>>,
    #[cfg(feature = "unstable")]
    certificate_verifier_cache: Option<Arc<dyn CertificateVerifierCache>>,
    era_fetcher: Option<Arc<dyn EraFetcher>>,
    logger: Option<Logger>,
    feedback_receivers: Vec<Arc<dyn FeedbackReceiver>>,
    options: ClientOptions,
}

impl ClientBuilder {
    /// Constructs a new `ClientBuilder` that fetches data from the aggregator at the given
    /// endpoint and with the given genesis verification key.
    #[deprecated(
        since = "0.12.36",
        note = "Use `new` method instead and set the genesis verification key with `set_genesis_verification_key`"
    )]
    pub fn aggregator(endpoint: &str, genesis_verification_key: &str) -> ClientBuilder {
        Self::new(AggregatorDiscoveryType::Url(endpoint.to_string())).set_genesis_verification_key(
            GenesisVerificationKey::JsonHex(genesis_verification_key.to_string()),
        )
    }

    /// Constructs a new `ClientBuilder` that automatically discovers the aggregator for the given
    /// Mithril network and with the given genesis verification key.
    #[cfg(not(target_family = "wasm"))]
    pub fn automatic(network: &str, genesis_verification_key: &str) -> ClientBuilder {
        Self::new(AggregatorDiscoveryType::Automatic(MithrilNetwork::new(
            network.to_string(),
        )))
        .set_genesis_verification_key(GenesisVerificationKey::JsonHex(
            genesis_verification_key.to_string(),
        ))
    }

    /// Constructs a new `ClientBuilder` without any dependency set.
    pub fn new(aggregator_discovery: AggregatorDiscoveryType) -> ClientBuilder {
        Self {
            aggregator_discovery,
            #[cfg(not(target_family = "wasm"))]
            aggregator_capabilities: None,
            #[cfg(not(target_family = "wasm"))]
            aggregator_discoverer: None,
            genesis_verification_key: None,
            origin_tag: None,
            client_type: None,
            #[cfg(feature = "fs")]
            ancillary_verification_key: None,
            certificate_verifier: None,
            #[cfg(feature = "fs")]
            http_file_downloader: None,
            #[cfg(feature = "unstable")]
            certificate_verifier_cache: None,
            era_fetcher: None,
            logger: None,
            feedback_receivers: vec![],
            options: ClientOptions::default(),
        }
    }

    /// Sets the genesis verification key to use when verifying certificates.
    pub fn set_genesis_verification_key(
        mut self,
        genesis_verification_key: GenesisVerificationKey,
    ) -> ClientBuilder {
        self.genesis_verification_key = Some(genesis_verification_key);

        self
    }

    /// Sets the aggregator capabilities expected to be matched by the aggregator with which the client will interact.
    #[cfg(not(target_family = "wasm"))]
    pub fn with_capabilities(
        mut self,
        capabilities: RequiredAggregatorCapabilities,
    ) -> ClientBuilder {
        self.aggregator_capabilities = Some(capabilities);

        self
    }

    /// Sets the aggregator discoverer to use to find the aggregator endpoint when in automatic discovery.
    #[cfg(not(target_family = "wasm"))]
    pub fn with_aggregator_discoverer(
        mut self,
        discoverer: Arc<dyn AggregatorDiscoverer<AggregatorEndpoint>>,
    ) -> ClientBuilder {
        self.aggregator_discoverer = Some(discoverer);

        self
    }

    /// Returns a `Client` that uses the dependencies provided to this `ClientBuilder`.
    ///
    /// The builder will try to create the missing dependencies using default implementations
    /// if possible.
    pub fn build(self) -> MithrilResult<Client> {
        let logger = self
            .logger
            .clone()
            .unwrap_or_else(|| Logger::root(slog::Discard, o!()));

        let genesis_verification_key = match self.genesis_verification_key {
            Some(GenesisVerificationKey::JsonHex(ref key)) => key,
            None => {
                return Err(anyhow!(
                    "The genesis verification key must be provided to build the client with the 'set_genesis_verification_key' function"
                ));
            }
        };

        let feedback_sender = FeedbackSender::new(&self.feedback_receivers);

        let aggregator_client = Arc::new(self.build_aggregator_client(logger.clone())?);

        let mithril_era_client = match self.era_fetcher {
            None => Arc::new(MithrilEraClient::new(aggregator_client.clone())),
            Some(era_fetcher) => Arc::new(MithrilEraClient::new(era_fetcher)),
        };

        let certificate_verifier = match self.certificate_verifier {
            None => Arc::new(
                MithrilCertificateVerifier::new(
                    aggregator_client.clone(),
                    genesis_verification_key,
                    feedback_sender.clone(),
                    #[cfg(feature = "unstable")]
                    self.certificate_verifier_cache,
                    logger.clone(),
                )
                .with_context(|| "Building certificate verifier failed")?,
            ),
            Some(verifier) => verifier,
        };
        let certificate_client = Arc::new(CertificateClient::new(
            aggregator_client.clone(),
            certificate_verifier,
            logger.clone(),
        ));

        let mithril_stake_distribution_client = Arc::new(MithrilStakeDistributionClient::new(
            aggregator_client.clone(),
        ));

        #[cfg(feature = "fs")]
        let http_file_downloader = match self.http_file_downloader {
            None => Arc::new(RetryDownloader::new(
                Arc::new(
                    HttpFileDownloader::new(feedback_sender.clone(), logger.clone())
                        .with_context(|| "Building http file downloader failed")?,
                ),
                FileDownloadRetryPolicy::default(),
            )),
            Some(http_file_downloader) => http_file_downloader,
        };

        #[cfg(feature = "fs")]
        let ancillary_verifier = match self.ancillary_verification_key {
            None => None,
            Some(verification_key) => Some(Arc::new(AncillaryVerifier::new(
                verification_key
                    .try_into()
                    .with_context(|| "Building ancillary verifier failed")?,
            ))),
        };

        let cardano_database_client = Arc::new(CardanoDatabaseClient::new(
            aggregator_client.clone(),
            #[cfg(feature = "fs")]
            http_file_downloader,
            #[cfg(feature = "fs")]
            ancillary_verifier,
            #[cfg(feature = "fs")]
            feedback_sender,
            #[cfg(feature = "fs")]
            Arc::new(TimestampTempDirectoryProvider::new(&format!(
                "{}",
                Utc::now().timestamp_micros()
            ))),
            #[cfg(feature = "fs")]
            logger,
        ));

        let cardano_transaction_client =
            Arc::new(CardanoTransactionClient::new(aggregator_client.clone()));

        #[cfg(feature = "unstable")]
        let cardano_transaction_v2_client =
            Arc::new(CardanoTransactionV2Client::new(aggregator_client.clone()));

        #[cfg(feature = "unstable")]
        let cardano_block_client = Arc::new(CardanoBlockClient::new(aggregator_client.clone()));

        let cardano_stake_distribution_client =
            Arc::new(CardanoStakeDistributionClient::new(aggregator_client));

        Ok(Client {
            certificate_client,
            mithril_stake_distribution_client,
            cardano_database_client,
            cardano_transaction_client,
            #[cfg(feature = "unstable")]
            cardano_transaction_v2_client,
            #[cfg(feature = "unstable")]
            cardano_block_client,
            cardano_stake_distribution_client,
            mithril_era_client,
        })
    }

    /// Discover available aggregator endpoints for the given Mithril network and required capabilities.
    #[cfg(not(target_family = "wasm"))]
    pub fn discover_aggregator(
        &self,
        network: &MithrilNetwork,
    ) -> MithrilResult<impl Iterator<Item = AggregatorEndpointWithCapabilities>> {
        let discoverer = self
            .aggregator_discoverer
            .clone()
            .unwrap_or_else(|| Self::default_aggregator_discoverer());
        let discoverer = if let Some(capabilities) = &self.aggregator_capabilities {
            Arc::new(CapableAggregatorDiscoverer::new(
                capabilities.to_owned(),
                discoverer.clone(),
            )) as Arc<dyn AggregatorDiscoverer<AggregatorEndpointWithCapabilities>>
        } else {
            Arc::new(CapableAggregatorDiscoverer::new(
                RequiredAggregatorCapabilities::All,
                discoverer.clone(),
            )) as Arc<dyn AggregatorDiscoverer<AggregatorEndpointWithCapabilities>>
        };

        tokio::task::block_in_place(move || {
            tokio::runtime::Handle::current().block_on(async move {
                discoverer
                    .get_available_aggregators(network.to_owned())
                    .await
                    .with_context(|| "Discovering aggregator endpoint failed")
            })
        })
    }

    /// Default aggregator discoverer to use to find the aggregator endpoint when in automatic discovery.
    #[cfg(not(target_family = "wasm"))]
    fn default_aggregator_discoverer() -> Arc<dyn AggregatorDiscoverer<AggregatorEndpoint>> {
        Arc::new(ShuffleAggregatorDiscoverer::new(
            Arc::new(HttpConfigAggregatorDiscoverer::default()),
            {
                let mut seed = [0u8; 32];
                let timestamp = Utc::now().timestamp_nanos_opt().unwrap_or(0);
                seed[..8].copy_from_slice(&timestamp.to_le_bytes());

                StdRng::from_seed(seed)
            },
        ))
    }

    fn build_aggregator_client(&self, logger: Logger) -> MithrilResult<AggregatorHttpClient> {
        let aggregator_endpoint = match self.aggregator_discovery {
            AggregatorDiscoveryType::Url(ref url) => url.clone(),
            #[cfg(not(target_family = "wasm"))]
            AggregatorDiscoveryType::Automatic(ref network) => self
                .discover_aggregator(network)?
                .next()
                .with_context(|| "No aggregator was available through discovery")?
                .into(),
        };
        let headers = self.compute_http_headers();

        AggregatorHttpClient::builder(aggregator_endpoint)
            .with_logger(logger)
            .with_headers(headers)
            .build()
    }

    fn compute_http_headers(&self) -> HashMap<String, String> {
        let mut headers = self.options.http_headers.clone().unwrap_or_default();
        if let Some(origin_tag) = self.origin_tag.clone() {
            headers.insert(MITHRIL_ORIGIN_TAG_HEADER.to_string(), origin_tag);
        }
        if let Some(client_type) = self.client_type.clone() {
            headers.insert(MITHRIL_CLIENT_TYPE_HEADER.to_string(), client_type);
        } else if !headers.contains_key(MITHRIL_CLIENT_TYPE_HEADER) {
            headers.insert(
                MITHRIL_CLIENT_TYPE_HEADER.to_string(),
                DEFAULT_CLIENT_TYPE.to_string(),
            );
        }

        headers
    }

    /// Sets the [EraFetcher] that will be used by the client to retrieve the current Mithril era.
    pub fn with_era_fetcher(mut self, era_fetcher: Arc<dyn EraFetcher>) -> ClientBuilder {
        self.era_fetcher = Some(era_fetcher);
        self
    }

    /// Set the [CertificateVerifier] that will be used to validate certificates.
    pub fn with_certificate_verifier(
        mut self,
        certificate_verifier: Arc<dyn CertificateVerifier>,
    ) -> ClientBuilder {
        self.certificate_verifier = Some(certificate_verifier);
        self
    }

    cfg_unstable! {
        /// Set the [CertificateVerifierCache] that will be used to cache certificate validation results.
        ///
        /// Passing a `None` value will disable the cache if any was previously set.
        pub fn with_certificate_verifier_cache(
            mut self,
            certificate_verifier_cache: Option<Arc<dyn CertificateVerifierCache>>,
        ) -> ClientBuilder {
            self.certificate_verifier_cache = certificate_verifier_cache;
            self
        }
    }

    cfg_fs! {
        /// Set the [FileDownloader] that will be used to download artifacts with HTTP.
        pub fn with_http_file_downloader(
            mut self,
            http_file_downloader: Arc<dyn FileDownloader>,
        ) -> ClientBuilder {
            self.http_file_downloader = Some(http_file_downloader);
            self
        }

        /// Set the ancillary verification key to use when verifying the downloaded ancillary files.
        pub fn set_ancillary_verification_key<T: Into<Option<String>>>(
            mut self,
            ancillary_verification_key: T,
        ) -> ClientBuilder {
            self.ancillary_verification_key = ancillary_verification_key.into();
            self
        }
    }

    /// Set the [Logger] to use.
    pub fn with_logger(mut self, logger: Logger) -> Self {
        self.logger = Some(logger);
        self
    }

    /// Set the origin tag.
    pub fn with_origin_tag(mut self, origin_tag: Option<String>) -> Self {
        self.origin_tag = origin_tag;
        self
    }

    /// Set the client type.
    pub fn with_client_type(mut self, client_type: Option<String>) -> Self {
        self.client_type = client_type;
        self
    }

    /// Sets the options to be used by the client.
    pub fn with_options(mut self, options: ClientOptions) -> Self {
        self.options = options;
        self
    }

    /// Add a [feedback receiver][FeedbackReceiver] to receive [events][crate::feedback::MithrilEvent]
    /// for tasks that can have a long duration (ie: snapshot download or a long certificate chain
    /// validation).
    pub fn add_feedback_receiver(mut self, receiver: Arc<dyn FeedbackReceiver>) -> Self {
        self.feedback_receivers.push(receiver);
        self
    }
}

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

    fn default_headers() -> HashMap<String, String> {
        HashMap::from([(
            MITHRIL_CLIENT_TYPE_HEADER.to_string(),
            DEFAULT_CLIENT_TYPE.to_string(),
        )])
    }

    #[tokio::test]
    async fn compute_http_headers_returns_options_http_headers() {
        let http_headers = default_headers();
        let client_builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions {
                http_headers: Some(http_headers.clone()),
            });

        let computed_headers = client_builder.compute_http_headers();

        assert_eq!(computed_headers, http_headers);
    }

    #[tokio::test]
    async fn compute_http_headers_with_origin_tag_returns_options_http_headers_with_origin_tag() {
        let http_headers = default_headers();
        let client_builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions {
                http_headers: Some(http_headers.clone()),
            })
            .with_origin_tag(Some("CLIENT_TAG".to_string()));
        let mut expected_headers = http_headers.clone();
        expected_headers.insert(
            MITHRIL_ORIGIN_TAG_HEADER.to_string(),
            "CLIENT_TAG".to_string(),
        );

        let computed_headers = client_builder.compute_http_headers();
        assert_eq!(computed_headers, expected_headers);
    }

    #[tokio::test]
    async fn test_with_origin_tag_not_overwrite_other_client_options_attributes() {
        let builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions { http_headers: None })
            .with_origin_tag(Some("TEST".to_string()));
        assert_eq!(None, builder.options.http_headers);
        assert_eq!(Some("TEST".to_string()), builder.origin_tag);

        let http_headers = HashMap::from([("Key".to_string(), "Value".to_string())]);
        let builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions {
                http_headers: Some(http_headers.clone()),
            })
            .with_origin_tag(Some("TEST".to_string()));
        assert_eq!(Some(http_headers), builder.options.http_headers);
        assert_eq!(Some("TEST".to_string()), builder.origin_tag);
    }

    #[tokio::test]
    async fn test_with_origin_tag_can_be_unset() {
        let http_headers = HashMap::from([("Key".to_string(), "Value".to_string())]);
        let client_options = ClientOptions {
            http_headers: Some(http_headers.clone()),
        };
        let builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(client_options)
            .with_origin_tag(None);

        assert_eq!(Some(http_headers), builder.options.http_headers);
        assert_eq!(None, builder.origin_tag);
    }

    #[tokio::test]
    async fn compute_http_headers_with_client_type_returns_options_http_headers_with_client_type() {
        let http_headers = HashMap::from([("Key".to_string(), "Value".to_string())]);
        let client_builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions {
                http_headers: Some(http_headers.clone()),
            })
            .with_client_type(Some("CLIENT_TYPE".to_string()));

        let computed_headers = client_builder.compute_http_headers();

        assert_eq!(
            computed_headers,
            HashMap::from([
                ("Key".to_string(), "Value".to_string()),
                (
                    MITHRIL_CLIENT_TYPE_HEADER.to_string(),
                    "CLIENT_TYPE".to_string()
                )
            ])
        );
    }

    #[tokio::test]
    async fn compute_http_headers_with_options_containing_client_type_returns_client_type() {
        let http_headers = HashMap::from([(
            MITHRIL_CLIENT_TYPE_HEADER.to_string(),
            "client type from options".to_string(),
        )]);
        let client_builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions {
                http_headers: Some(http_headers.clone()),
            });

        let computed_headers = client_builder.compute_http_headers();

        assert_eq!(computed_headers, http_headers);
    }

    #[tokio::test]
    async fn test_with_client_type_not_overwrite_other_client_options_attributes() {
        let builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions { http_headers: None })
            .with_client_type(Some("TEST".to_string()));
        assert_eq!(None, builder.options.http_headers);
        assert_eq!(Some("TEST".to_string()), builder.client_type);

        let http_headers = HashMap::from([("Key".to_string(), "Value".to_string())]);
        let builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions {
                http_headers: Some(http_headers.clone()),
            })
            .with_client_type(Some("TEST".to_string()));
        assert_eq!(Some(http_headers), builder.options.http_headers);
        assert_eq!(Some("TEST".to_string()), builder.client_type);
    }

    #[tokio::test]
    async fn test_given_a_none_client_type_compute_http_headers_will_set_client_type_to_default_value()
     {
        let builder_without_client_type =
            ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()));
        let computed_headers = builder_without_client_type.compute_http_headers();

        assert_eq!(
            computed_headers,
            HashMap::from([(
                MITHRIL_CLIENT_TYPE_HEADER.to_string(),
                DEFAULT_CLIENT_TYPE.to_string()
            )])
        );

        let builder_with_none_client_type =
            ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string())).with_client_type(None);
        let computed_headers = builder_with_none_client_type.compute_http_headers();

        assert_eq!(
            computed_headers,
            HashMap::from([(
                MITHRIL_CLIENT_TYPE_HEADER.to_string(),
                DEFAULT_CLIENT_TYPE.to_string()
            )])
        );
    }

    #[tokio::test]
    async fn test_compute_http_headers_will_compute_client_type_header_from_struct_attribute_over_options()
     {
        let http_headers = HashMap::from([(
            MITHRIL_CLIENT_TYPE_HEADER.to_string(),
            "client type from options".to_string(),
        )]);
        let client_builder = ClientBuilder::new(AggregatorDiscoveryType::Url("".to_string()))
            .with_options(ClientOptions {
                http_headers: Some(http_headers.clone()),
            })
            .with_client_type(Some("client type".to_string()));

        let computed_headers = client_builder.compute_http_headers();

        assert_eq!(
            computed_headers,
            HashMap::from([(
                MITHRIL_CLIENT_TYPE_HEADER.to_string(),
                "client type".to_string()
            )])
        );
    }
}