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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
use anyhow::anyhow;
use async_trait::async_trait;
use slog::{debug, trace, warn, Logger};
use std::collections::BTreeSet;
use std::sync::Arc;
use thiserror::Error;

use crate::dependency_injection::EpochServiceWrapper;
use crate::entities::SignerEpochSettings;
use crate::services::SignedEntityConfigProvider;
use crate::store::ProtocolInitializerStorer;
use crate::RunnerError;
use mithril_common::crypto_helper::ProtocolInitializer;
use mithril_common::entities::{
    CardanoTransactionsSigningConfig, Epoch, PartyId, ProtocolParameters, SignedEntityConfig,
    SignedEntityTypeDiscriminants, Signer, SignerWithStake,
};
use mithril_common::logging::LoggerExtensions;
use mithril_common::StdResult;
use mithril_persistence::store::StakeStorer;

/// Errors dedicated to the EpochService.
#[derive(Debug, Error)]
pub enum EpochServiceError {
    /// Raised when service has not collected data at least once.
    #[error("Epoch service was not initialized, the function `inform_epoch_settings` must be called first")]
    NotYetInitialized,
}

/// Service that aggregates all data that don't change in a given epoch.
#[async_trait]
pub trait EpochService: Sync + Send {
    /// Inform the service a new epoch has been detected, telling it to update its
    /// internal state for the new epoch.
    async fn inform_epoch_settings(
        &mut self,
        epoch_settings: SignerEpochSettings,
        allowed_discriminants: BTreeSet<SignedEntityTypeDiscriminants>,
    ) -> StdResult<()>;

    /// Get the current epoch for which the data stored in this service are computed.
    fn epoch_of_current_data(&self) -> StdResult<Epoch>;

    /// Get protocol parameters for registration.
    fn registration_protocol_parameters(&self) -> StdResult<&ProtocolParameters>;

    /// Get the protocol initializer for the current epoch if any
    ///
    /// `None` if the signer can't sign for the current epoch.
    fn protocol_initializer(&self) -> StdResult<&Option<ProtocolInitializer>>;

    /// Get signers for the current epoch
    fn current_signers(&self) -> StdResult<&Vec<Signer>>;

    /// Get signers for the next epoch
    fn next_signers(&self) -> StdResult<&Vec<Signer>>;

    /// Get signers with stake for the current epoch
    async fn current_signers_with_stake(&self) -> StdResult<Vec<SignerWithStake>>;

    /// Get signers with stake for the next epoch
    async fn next_signers_with_stake(&self) -> StdResult<Vec<SignerWithStake>>;

    /// Get the list of signed entity types that are allowed to sign for the current epoch
    fn allowed_discriminants(&self) -> StdResult<&BTreeSet<SignedEntityTypeDiscriminants>>;

    /// Get the cardano transactions signing configuration for the current epoch
    fn cardano_transactions_signing_config(
        &self,
    ) -> StdResult<&Option<CardanoTransactionsSigningConfig>>;

    /// Get the cardano transactions signing configuration for the next epoch
    fn next_cardano_transactions_signing_config(
        &self,
    ) -> StdResult<&Option<CardanoTransactionsSigningConfig>>;

    /// Check if the given signer can sign for the current epoch
    fn can_signer_sign_current_epoch(&self, party_id: PartyId) -> StdResult<bool>;
}

pub(crate) struct EpochData {
    pub epoch: Epoch,
    pub registration_protocol_parameters: ProtocolParameters,
    pub protocol_initializer: Option<ProtocolInitializer>,
    pub current_signers: Vec<Signer>,
    pub next_signers: Vec<Signer>,
    pub allowed_discriminants: BTreeSet<SignedEntityTypeDiscriminants>,
    pub cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
    pub next_cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
}

/// Implementation of the [epoch service][EpochService].
pub struct MithrilEpochService {
    stake_storer: Arc<dyn StakeStorer>,
    protocol_initializer_store: Arc<dyn ProtocolInitializerStorer>,
    epoch_data: Option<EpochData>,
    logger: Logger,
}

impl MithrilEpochService {
    /// Create a new service instance
    pub fn new(
        stake_storer: Arc<dyn StakeStorer>,
        protocol_initializer_store: Arc<dyn ProtocolInitializerStorer>,
        logger: Logger,
    ) -> Self {
        Self {
            stake_storer,
            protocol_initializer_store,
            epoch_data: None,
            logger: logger.new_with_component_name::<Self>(),
        }
    }

    async fn associate_signers_with_stake(
        &self,
        epoch: Epoch,
        signers: &[Signer],
    ) -> StdResult<Vec<SignerWithStake>> {
        debug!(
            self.logger,
            ">> associate_signers_with_stake(epoch:{epoch})"
        );

        let stakes = self
            .stake_storer
            .get_stakes(epoch)
            .await?
            .ok_or_else(|| RunnerError::NoValueError(format!("stakes at epoch {epoch}")))?;

        let mut signers_with_stake = vec![];

        for signer in signers {
            let stake = stakes
                .get(&*signer.party_id)
                .ok_or_else(|| RunnerError::NoStakeForSigner(signer.party_id.to_string()))?;

            signers_with_stake.push(SignerWithStake::new(
                signer.party_id.to_owned(),
                signer.verification_key.to_owned(),
                signer.verification_key_signature.to_owned(),
                signer.operational_certificate.to_owned(),
                signer.kes_period.to_owned(),
                *stake,
            ));
            trace!(
                self.logger,
                " > Associating signer_id {} with stake {}",
                signer.party_id,
                *stake
            );
        }

        Ok(signers_with_stake)
    }

    fn is_signer_included_in_current_stake_distribution(
        &self,
        party_id: PartyId,
        protocol_initializer: &ProtocolInitializer,
    ) -> StdResult<bool> {
        Ok(self.current_signers()?.iter().any(|s| {
            s.party_id == party_id
                && s.verification_key == protocol_initializer.verification_key().into()
        }))
    }

    fn unwrap_data(&self) -> Result<&EpochData, EpochServiceError> {
        self.epoch_data
            .as_ref()
            .ok_or(EpochServiceError::NotYetInitialized)
    }
}

#[async_trait]
impl EpochService for MithrilEpochService {
    async fn inform_epoch_settings(
        &mut self,
        epoch_settings: SignerEpochSettings,
        allowed_discriminants: BTreeSet<SignedEntityTypeDiscriminants>,
    ) -> StdResult<()> {
        debug!(self.logger, ">> inform_epoch_settings"; "epoch_settings" => ?epoch_settings);

        let epoch = epoch_settings.epoch;
        let protocol_initializer = self
            .protocol_initializer_store
            .get_protocol_initializer(epoch.offset_to_signer_retrieval_epoch()?)
            .await?;

        self.epoch_data = Some(EpochData {
            epoch,
            registration_protocol_parameters: epoch_settings.registration_protocol_parameters,
            protocol_initializer,
            current_signers: epoch_settings.current_signers,
            next_signers: epoch_settings.next_signers,
            allowed_discriminants,
            cardano_transactions_signing_config: epoch_settings.cardano_transactions_signing_config,
            next_cardano_transactions_signing_config: epoch_settings
                .next_cardano_transactions_signing_config,
        });

        Ok(())
    }

    fn epoch_of_current_data(&self) -> StdResult<Epoch> {
        Ok(self.unwrap_data()?.epoch)
    }

    fn registration_protocol_parameters(&self) -> StdResult<&ProtocolParameters> {
        Ok(&self.unwrap_data()?.registration_protocol_parameters)
    }

    fn protocol_initializer(&self) -> StdResult<&Option<ProtocolInitializer>> {
        Ok(&self.unwrap_data()?.protocol_initializer)
    }

    fn current_signers(&self) -> StdResult<&Vec<Signer>> {
        Ok(&self.unwrap_data()?.current_signers)
    }

    fn next_signers(&self) -> StdResult<&Vec<Signer>> {
        Ok(&self.unwrap_data()?.next_signers)
    }

    async fn current_signers_with_stake(&self) -> StdResult<Vec<SignerWithStake>> {
        let current_epoch = self.epoch_of_current_data()?;
        self.associate_signers_with_stake(
            current_epoch.offset_to_signer_retrieval_epoch()?,
            self.current_signers()?,
        )
        .await
    }

    async fn next_signers_with_stake(&self) -> StdResult<Vec<SignerWithStake>> {
        let current_epoch = self.epoch_of_current_data()?;
        self.associate_signers_with_stake(
            current_epoch.offset_to_next_signer_retrieval_epoch(),
            self.next_signers()?,
        )
        .await
    }

    fn allowed_discriminants(&self) -> StdResult<&BTreeSet<SignedEntityTypeDiscriminants>> {
        Ok(&self.unwrap_data()?.allowed_discriminants)
    }

    fn cardano_transactions_signing_config(
        &self,
    ) -> StdResult<&Option<CardanoTransactionsSigningConfig>> {
        Ok(&self.unwrap_data()?.cardano_transactions_signing_config)
    }

    fn next_cardano_transactions_signing_config(
        &self,
    ) -> StdResult<&Option<CardanoTransactionsSigningConfig>> {
        Ok(&self.unwrap_data()?.next_cardano_transactions_signing_config)
    }

    fn can_signer_sign_current_epoch(&self, party_id: PartyId) -> StdResult<bool> {
        let epoch = self.epoch_of_current_data()?;
        if let Some(protocol_initializer) = self.protocol_initializer()? {
            debug!(
                self.logger,
                " > Got protocol initializer for this epoch ({epoch})"
            );
            if self
                .is_signer_included_in_current_stake_distribution(party_id, protocol_initializer)?
            {
                return Ok(true);
            } else {
                debug!(
                    self.logger,
                    " > Signer not in current stake distribution. Can NOT sign"
                );
            }
        } else {
            warn!(
                self.logger,
                " > NO protocol initializer found for this epoch ({epoch})",
            );
        }

        Ok(false)
    }
}

/// Simple wrapper to the [EpochService] to implement the [SignedEntityConfigProvider] trait.
///
/// Needed because the epoch service is wrapped in an Arc<RwLock<>> in the dependencies, making
/// direct usage of implemented traits methods difficult.
pub struct SignerSignedEntityConfigProvider {
    epoch_service: EpochServiceWrapper,
}

impl SignerSignedEntityConfigProvider {
    /// Create a new instance of the `SignerSignedEntityConfigProvider`.
    pub fn new(epoch_service: EpochServiceWrapper) -> Self {
        Self { epoch_service }
    }
}

#[async_trait]
impl SignedEntityConfigProvider for SignerSignedEntityConfigProvider {
    async fn get(&self) -> StdResult<SignedEntityConfig> {
        let epoch_service = self.epoch_service.read().await;
        let cardano_transactions_signing_config =
            match epoch_service.cardano_transactions_signing_config()? {
                Some(config) => Ok(config.clone()),
                None => Err(anyhow!("No cardano transaction signing config available")),
            }?;

        Ok(SignedEntityConfig {
            allowed_discriminants: epoch_service.allowed_discriminants()?.clone(),
            cardano_transactions_signing_config,
        })
    }
}

#[cfg(test)]
use crate::database::repository::ProtocolInitializerRepository;

#[cfg(test)]
impl MithrilEpochService {
    /// `TEST ONLY` - Create a new instance of the service using dumb dependencies.
    pub fn new_with_dumb_dependencies() -> Self {
        use crate::database::repository::StakePoolStore;
        use crate::database::test_helper::main_db_connection;
        use crate::test_tools::TestLogger;

        let sqlite_connection = Arc::new(main_db_connection().unwrap());
        let stake_store = Arc::new(StakePoolStore::new(sqlite_connection.clone(), None));
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(sqlite_connection, None));

        Self::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        )
    }

    /// `TEST ONLY` - Set all data to either default values, empty values, or fake values
    /// if no default/empty can be set.
    pub fn set_data_to_default_or_fake(mut self, epoch: Epoch) -> Self {
        use mithril_common::test_utils::fake_data;

        let epoch_data = EpochData {
            epoch,
            registration_protocol_parameters: fake_data::protocol_parameters(),
            protocol_initializer: None,
            current_signers: vec![],
            next_signers: vec![],
            allowed_discriminants: BTreeSet::new(),
            cardano_transactions_signing_config: None,
            next_cardano_transactions_signing_config: None,
        };
        self.epoch_data = Some(epoch_data);
        self
    }

    /// `TEST ONLY` - Alter the data stored in the service, won't do anything if no data is stored.
    pub(crate) fn alter_data(mut self, data_config: impl FnOnce(&mut EpochData)) -> Self {
        if let Some(data) = self.epoch_data.as_mut() {
            data_config(data);
        }
        self
    }
}

#[cfg(test)]
pub mod mock_epoch_service {
    use mockall::mock;

    use super::*;

    mock! {
        pub EpochServiceImpl {}

        #[async_trait]
        impl EpochService for EpochServiceImpl {
            async fn inform_epoch_settings(
                &mut self,
                epoch_settings: SignerEpochSettings,
                allowed_discriminants: BTreeSet<SignedEntityTypeDiscriminants>,
            ) -> StdResult<()>;

            fn epoch_of_current_data(&self) -> StdResult<Epoch>;

            fn registration_protocol_parameters(&self) -> StdResult<&'static ProtocolParameters>;

            fn protocol_initializer(&self) -> StdResult<&'static Option<ProtocolInitializer>>;

            fn current_signers(&self) -> StdResult<&'static Vec<Signer>>;

            fn next_signers(&self) -> StdResult<&'static Vec<Signer>>;

            async fn current_signers_with_stake(&self) -> StdResult<Vec<SignerWithStake>>;

            async fn next_signers_with_stake(&self) -> StdResult<Vec<SignerWithStake>>;

            fn allowed_discriminants(&self) -> StdResult<&'static BTreeSet<SignedEntityTypeDiscriminants>>;

            fn cardano_transactions_signing_config(
                &self,
            ) -> StdResult<&'static Option<CardanoTransactionsSigningConfig>>;

            fn next_cardano_transactions_signing_config(
                &self,
            ) -> StdResult<&'static Option<CardanoTransactionsSigningConfig>>;

            fn can_signer_sign_current_epoch(&self, party_id: PartyId) -> StdResult<bool>;
        }
    }

    impl MockEpochServiceImpl {
        pub fn new_with_config(
            config: impl FnOnce(&mut MockEpochServiceImpl),
        ) -> MockEpochServiceImpl {
            let mut epoch_service_mock = MockEpochServiceImpl::new();
            config(&mut epoch_service_mock);

            epoch_service_mock
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use tokio::sync::RwLock;

    use mithril_common::entities::{Epoch, StakeDistribution};
    use mithril_common::test_utils::{fake_data, MithrilFixtureBuilder};

    use crate::database::repository::{ProtocolInitializerRepository, StakePoolStore};
    use crate::database::test_helper::main_db_connection;
    use crate::entities::SignerEpochSettings;
    use crate::services::MithrilProtocolInitializerBuilder;
    use crate::test_tools::TestLogger;

    use super::*;

    #[test]
    fn test_is_signer_included_in_current_stake_distribution_returns_error_when_epoch_settings_is_not_set(
    ) {
        let party_id = "party_id".to_string();
        let protocol_initializer = MithrilProtocolInitializerBuilder::build(
            &100,
            &fake_data::protocol_parameters(),
            None,
            None,
        )
        .unwrap();
        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = Arc::new(StakePoolStore::new(connection.clone(), None));
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));
        let service = MithrilEpochService::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        );

        service
            .is_signer_included_in_current_stake_distribution(party_id, &protocol_initializer)
            .expect_err("can_signer_sign should return error when epoch settings is not set");
    }

    #[tokio::test]
    async fn test_is_signer_included_in_current_stake_distribution_returns_true_when_signer_verification_key_and_pool_id_found(
    ) {
        let fixtures = MithrilFixtureBuilder::default().with_signers(10).build();
        let protocol_initializer = fixtures.signers_fixture()[0]
            .protocol_initializer
            .to_owned();
        let epoch = Epoch(12);
        let signers = fixtures.signers();

        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = Arc::new(StakePoolStore::new(connection.clone(), None));
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));

        let epoch_settings = SignerEpochSettings {
            epoch,
            current_signers: signers[..5].to_vec(),
            ..SignerEpochSettings::dummy().clone()
        };

        let mut service = MithrilEpochService::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        );
        service
            .inform_epoch_settings(epoch_settings.clone(), BTreeSet::new())
            .await
            .unwrap();

        let party_id = fixtures.signers_fixture()[0].party_id();
        assert!(service
            .is_signer_included_in_current_stake_distribution(
                party_id.clone(),
                &protocol_initializer
            )
            .unwrap());

        let party_id_not_included = fixtures.signers_fixture()[6].party_id();
        assert!(!service
            .is_signer_included_in_current_stake_distribution(
                party_id_not_included,
                &protocol_initializer
            )
            .unwrap());

        let protocol_initializer_not_included = fixtures.signers_fixture()[6]
            .protocol_initializer
            .to_owned();
        assert!(!service
            .is_signer_included_in_current_stake_distribution(
                party_id,
                &protocol_initializer_not_included
            )
            .unwrap());
    }

    mod can_signer_sign_current_epoch {
        use super::*;

        #[test]
        fn cant_sign_if_no_protocol_initializer_are_stored() {
            let epoch = Epoch(12);
            let fixtures = MithrilFixtureBuilder::default().with_signers(10).build();

            let epoch_service = MithrilEpochService::new_with_dumb_dependencies()
                .set_data_to_default_or_fake(epoch)
                .alter_data(|data| {
                    data.protocol_initializer = None;
                    data.current_signers = fixtures.signers();
                });

            let can_sign_current_epoch = epoch_service
                .can_signer_sign_current_epoch(fixtures.signers()[0].party_id.clone())
                .unwrap();
            assert!(!can_sign_current_epoch);
        }

        #[test]
        fn cant_sign_if_stored_protocol_initializer_belong_to_another_party() {
            let epoch = Epoch(12);
            let fixtures = MithrilFixtureBuilder::default().with_signers(10).build();

            let epoch_service = MithrilEpochService::new_with_dumb_dependencies()
                .set_data_to_default_or_fake(epoch)
                .alter_data(|data| {
                    data.protocol_initializer =
                        Some(fixtures.signers_fixture()[2].protocol_initializer.clone());
                    data.current_signers = fixtures.signers();
                });

            let can_sign_current_epoch = epoch_service
                .can_signer_sign_current_epoch(fixtures.signers()[0].party_id.clone())
                .unwrap();
            assert!(!can_sign_current_epoch);
        }

        #[test]
        fn cant_sign_if_not_in_current_signers() {
            let epoch = Epoch(12);
            let fixtures = MithrilFixtureBuilder::default().with_signers(10).build();

            let epoch_service = MithrilEpochService::new_with_dumb_dependencies()
                .set_data_to_default_or_fake(epoch)
                .alter_data(|data| {
                    data.protocol_initializer =
                        Some(fixtures.signers_fixture()[0].protocol_initializer.clone());
                    data.current_signers = fixtures.signers()[2..].to_vec();
                });

            let can_sign_current_epoch = epoch_service
                .can_signer_sign_current_epoch(fixtures.signers()[0].party_id.clone())
                .unwrap();
            assert!(!can_sign_current_epoch);
        }

        #[test]
        fn can_sign_if_in_current_signers_and_use_the_stored_protocol_initializer() {
            let epoch = Epoch(12);
            let fixtures = MithrilFixtureBuilder::default().with_signers(10).build();

            let epoch_service = MithrilEpochService::new_with_dumb_dependencies()
                .set_data_to_default_or_fake(epoch)
                .alter_data(|data| {
                    data.protocol_initializer =
                        Some(fixtures.signers_fixture()[0].protocol_initializer.clone());
                    data.current_signers = fixtures.signers();
                });

            let can_sign_current_epoch = epoch_service
                .can_signer_sign_current_epoch(fixtures.signers()[0].party_id.clone())
                .unwrap();
            assert!(can_sign_current_epoch);
        }
    }

    #[tokio::test]
    async fn test_retrieve_data_return_error_before_register_epoch_settings_was_call() {
        let epoch = Epoch(12);
        // Signers and stake distribution
        let signers = fake_data::signers(10);
        let stake_distribution: StakeDistribution = signers
            .iter()
            .enumerate()
            .map(|(i, signer)| (signer.party_id.clone(), (i + 1) as u64 * 100))
            .collect();

        // Init stores
        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = Arc::new(StakePoolStore::new(connection.clone(), None));
        stake_store
            .save_stakes(epoch, stake_distribution.clone())
            .await
            .expect("save_stakes should not fail");
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));

        // Build service and register epoch settings
        let service = MithrilEpochService::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        );
        assert!(service.epoch_of_current_data().is_err());
        assert!(service.registration_protocol_parameters().is_err());
        assert!(service.protocol_initializer().is_err());
        assert!(service.current_signers().is_err());
        assert!(service.next_signers().is_err());
        assert!(service.current_signers_with_stake().await.is_err());
        assert!(service.next_signers_with_stake().await.is_err());
        assert!(service.cardano_transactions_signing_config().is_err());
        assert!(service.next_cardano_transactions_signing_config().is_err());
    }

    #[tokio::test]
    async fn test_data_are_available_after_register_epoch_settings_call() {
        let epoch = Epoch(12);
        // Signers and stake distribution
        let signers = fake_data::signers(10);

        // Init stores
        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = Arc::new(StakePoolStore::new(connection.clone(), None));
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));

        // Epoch settings
        let epoch_settings = SignerEpochSettings {
            epoch,
            current_signers: signers[2..5].to_vec(),
            next_signers: signers[3..7].to_vec(),
            ..SignerEpochSettings::dummy().clone()
        };

        // Build service and register epoch settings
        let mut service = MithrilEpochService::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        );

        service
            .inform_epoch_settings(
                epoch_settings.clone(),
                BTreeSet::from([SignedEntityTypeDiscriminants::CardanoImmutableFilesFull]),
            )
            .await
            .unwrap();

        // Check current_signers
        {
            let current_signers = service.current_signers().unwrap();
            let expected_current_signers = epoch_settings.current_signers.clone();
            assert_eq!(expected_current_signers, *current_signers);
        }
        // Check next_signers
        {
            let next_signers = service.next_signers().unwrap();
            let expected_next_signers = epoch_settings.next_signers.clone();
            assert_eq!(expected_next_signers, *next_signers);
        }

        // Check other data
        assert_eq!(
            epoch_settings.epoch,
            service.epoch_of_current_data().unwrap()
        );
        assert_eq!(
            epoch_settings.registration_protocol_parameters,
            *service.registration_protocol_parameters().unwrap()
        );
        assert!(
            service.protocol_initializer().unwrap().is_none(),
            "protocol_initializer should be None since nothing was in store"
        );

        // Check allowed_discriminants
        assert_eq!(
            BTreeSet::from([SignedEntityTypeDiscriminants::CardanoImmutableFilesFull]),
            *service.allowed_discriminants().unwrap()
        );

        // Check cardano_transactions_signing_config
        assert_eq!(
            epoch_settings.cardano_transactions_signing_config,
            *service.cardano_transactions_signing_config().unwrap()
        );
        // Check next_cardano_transactions_signing_config
        assert_eq!(
            epoch_settings.next_cardano_transactions_signing_config,
            *service.next_cardano_transactions_signing_config().unwrap()
        );
    }

    #[tokio::test]
    async fn test_signers_with_stake_are_available_after_register_epoch_settings_call() {
        fn build_stake_distribution(signers: &[Signer], first_stake: u64) -> StakeDistribution {
            signers
                .iter()
                .enumerate()
                .map(|(i, signer)| (signer.party_id.clone(), first_stake + i as u64))
                .collect()
        }

        let epoch = Epoch(12);

        // Signers and stake distribution
        let signers = fake_data::signers(10);
        let stake_distribution: StakeDistribution = build_stake_distribution(&signers, 100);
        let next_stake_distribution: StakeDistribution = build_stake_distribution(&signers, 500);

        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = {
            let store = Arc::new(StakePoolStore::new(connection.clone(), None));
            store
                .save_stakes(
                    epoch.offset_to_signer_retrieval_epoch().unwrap(),
                    stake_distribution.clone(),
                )
                .await
                .unwrap();
            store
                .save_stakes(
                    epoch.offset_to_next_signer_retrieval_epoch(),
                    next_stake_distribution.clone(),
                )
                .await
                .unwrap();
            store
        };
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));

        // Epoch settings
        let epoch_settings = SignerEpochSettings {
            epoch,
            current_signers: signers[2..5].to_vec(),
            next_signers: signers[3..7].to_vec(),
            ..SignerEpochSettings::dummy().clone()
        };

        // Build service and register epoch settings
        let mut service = MithrilEpochService::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        );
        service
            .inform_epoch_settings(epoch_settings.clone(), BTreeSet::new())
            .await
            .unwrap();

        // Check current signers with stake
        {
            let current_signers = service.current_signers_with_stake().await.unwrap();

            assert_eq!(epoch_settings.current_signers.len(), current_signers.len());
            for signer in current_signers {
                let expected_stake = stake_distribution.get(&signer.party_id).unwrap();
                assert_eq!(expected_stake, &signer.stake);
            }
        }

        // Check next signers with stake
        {
            let next_signers = service.next_signers_with_stake().await.unwrap();

            assert_eq!(epoch_settings.next_signers.len(), next_signers.len());
            for signer in next_signers {
                let expected_stake = next_stake_distribution.get(&signer.party_id).unwrap();
                assert_eq!(expected_stake, &signer.stake);
            }
        }
    }

    #[tokio::test]
    async fn test_protocol_initializer_is_available_after_register_epoch_settings_call_if_in_store()
    {
        let epoch = Epoch(12);
        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = Arc::new(StakePoolStore::new(connection.clone(), None));
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));
        protocol_initializer_store
            .save_protocol_initializer(
                epoch.offset_to_signer_retrieval_epoch().unwrap(),
                fake_data::protocol_initializer("seed", 1245),
            )
            .await
            .unwrap();

        let mut service = MithrilEpochService::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        );
        let epoch_settings = SignerEpochSettings {
            epoch,
            ..SignerEpochSettings::dummy().clone()
        };
        service
            .inform_epoch_settings(epoch_settings, BTreeSet::new())
            .await
            .unwrap();

        let protocol_initializer = service.protocol_initializer().unwrap();
        assert_eq!(
            Some(1245),
            protocol_initializer.as_ref().map(|p| p.get_stake()),
        );
    }

    #[tokio::test]
    async fn is_source_of_signed_entity_config() {
        let connection = Arc::new(main_db_connection().unwrap());
        let stake_store = Arc::new(StakePoolStore::new(connection.clone(), None));
        let protocol_initializer_store =
            Arc::new(ProtocolInitializerRepository::new(connection, None));
        let epoch_service = Arc::new(RwLock::new(MithrilEpochService::new(
            stake_store,
            protocol_initializer_store,
            TestLogger::stdout(),
        )));
        let config_provider = SignerSignedEntityConfigProvider {
            epoch_service: epoch_service.clone(),
        };

        // Fail before the first `inform_epoch_settings`
        {
            config_provider.get().await.expect_err(
                "Should fail since sources data are not set before the first inform_epoch_settings",
            );
        }
        // Fail after `inform_epoch_settings` if `cardano_transactions_signing_config` is not set
        {
            let allowed_discriminants =
                BTreeSet::from([SignedEntityTypeDiscriminants::CardanoImmutableFilesFull]);

            epoch_service
                .write()
                .await
                .inform_epoch_settings(
                    SignerEpochSettings {
                        cardano_transactions_signing_config: None,
                        ..SignerEpochSettings::dummy()
                    },
                    allowed_discriminants.clone(),
                )
                .await
                .unwrap();

            config_provider
                .get()
                .await
                .expect_err("Should fail since cardano_transactions_signing_config is not set");
        }
        // Success after `inform_epoch_settings` if `cardano_transactions_signing_config` is set
        {
            let allowed_discriminants =
                BTreeSet::from([SignedEntityTypeDiscriminants::CardanoImmutableFilesFull]);
            epoch_service
                .write()
                .await
                .inform_epoch_settings(
                    SignerEpochSettings {
                        cardano_transactions_signing_config: Some(
                            CardanoTransactionsSigningConfig::dummy(),
                        ),
                        ..SignerEpochSettings::dummy()
                    },
                    allowed_discriminants.clone(),
                )
                .await
                .unwrap();

            let config = config_provider.get().await.unwrap();

            assert_eq!(
                SignedEntityConfig {
                    allowed_discriminants,
                    cardano_transactions_signing_config: CardanoTransactionsSigningConfig::dummy(),
                },
                config
            );
        }
    }
}