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
use slog::Logger;
use std::sync::Arc;
use tokio::sync::RwLock;

use mithril_common::{
    api_version::APIVersionProvider,
    cardano_block_scanner::BlockScanner,
    certificate_chain::CertificateVerifier,
    chain_observer::ChainObserver,
    crypto_helper::ProtocolGenesisVerifier,
    digesters::{ImmutableDigester, ImmutableFileObserver},
    entities::{
        CardanoTransactionsSigningConfig, Epoch, ProtocolParameters, SignerWithStake,
        StakeDistribution,
    },
    era::{EraChecker, EraReader},
    signable_builder::SignableBuilderService,
    signed_entity_type_lock::SignedEntityTypeLock,
    test_utils::MithrilFixture,
    TickerService,
};
use mithril_persistence::{
    sqlite::{SqliteConnection, SqliteConnectionPool},
    store::StakeStorer,
};

use crate::{
    configuration::*,
    database::repository::{
        CertificateRepository, OpenMessageRepository, SignedEntityStorer, SignerGetter,
        StakePoolStore,
    },
    entities::AggregatorEpochSettings,
    event_store::{EventMessage, TransmitterService},
    multi_signer::MultiSigner,
    services::{
        CertifierService, EpochService, MessageService, ProverService, SignedEntityService,
        StakeDistributionService, TransactionStore, UpkeepService,
    },
    signer_registerer::SignerRecorder,
    snapshot_uploaders::SnapshotUploader,
    CertificatePendingStore, EpochSettingsStorer, MetricsService, SignerRegisterer,
    SignerRegistrationRoundOpener, SingleSignatureAuthenticator, Snapshotter,
    VerificationKeyStorer,
};

/// EpochServiceWrapper wraps a [EpochService]
pub type EpochServiceWrapper = Arc<RwLock<dyn EpochService>>;

/// DependencyManager handles the dependencies
pub struct DependencyContainer {
    /// Configuration structure.
    // TODO: remove this field and only use the `Configuration` in the dependencies builder
    #[deprecated]
    pub config: Configuration,

    /// Application root logger
    pub root_logger: Logger,

    /// SQLite database connection
    ///
    /// This is not a real service, but it is needed to instantiate all store
    /// services. Should be a private dependency.
    pub sqlite_connection: Arc<SqliteConnection>,

    /// Cardano transactions SQLite database connection pool
    pub sqlite_connection_cardano_transaction_pool: Arc<SqliteConnectionPool>,

    /// Stake Store used by the StakeDistributionService
    /// It shall be a private dependency.
    pub stake_store: Arc<StakePoolStore>,

    /// Snapshot uploader service.
    pub snapshot_uploader: Arc<dyn SnapshotUploader>,

    /// Multisigner service.
    pub multi_signer: Arc<dyn MultiSigner>,

    /// Certificate pending store.
    pub certificate_pending_store: Arc<CertificatePendingStore>,

    /// Certificate store.
    pub certificate_repository: Arc<CertificateRepository>,

    /// Open message store.
    pub open_message_repository: Arc<OpenMessageRepository>,

    /// Verification key store.
    pub verification_key_store: Arc<dyn VerificationKeyStorer>,

    /// Epoch settings storer.
    pub epoch_settings_storer: Arc<dyn EpochSettingsStorer>,

    /// Chain observer service.
    pub chain_observer: Arc<dyn ChainObserver>,

    /// Cardano transactions store.
    pub transaction_store: Arc<dyn TransactionStore>,

    /// Cardano block scanner.
    pub block_scanner: Arc<dyn BlockScanner>,

    /// Immutable file observer service.
    pub immutable_file_observer: Arc<dyn ImmutableFileObserver>,

    /// Digester service.
    pub digester: Arc<dyn ImmutableDigester>,

    /// Snapshotter service.
    pub snapshotter: Arc<dyn Snapshotter>,

    /// Certificate verifier service.
    pub certificate_verifier: Arc<dyn CertificateVerifier>,

    /// Genesis signature verifier service.
    pub genesis_verifier: Arc<ProtocolGenesisVerifier>,

    /// Signer registerer service
    pub signer_registerer: Arc<dyn SignerRegisterer>,

    /// Signer registration round opener service
    pub signer_registration_round_opener: Arc<dyn SignerRegistrationRoundOpener>,

    /// Era checker service
    pub era_checker: Arc<EraChecker>,

    /// Era reader service
    pub era_reader: Arc<EraReader>,

    /// Event Transmitter Service
    pub event_transmitter: Arc<TransmitterService<EventMessage>>,

    /// API Version provider
    pub api_version_provider: Arc<APIVersionProvider>,

    /// Stake Distribution Service
    pub stake_distribution_service: Arc<dyn StakeDistributionService>,

    /// Signer Recorder
    pub signer_recorder: Arc<dyn SignerRecorder>,

    /// Signable Builder Service
    pub signable_builder_service: Arc<dyn SignableBuilderService>,

    /// Signed Entity Service
    pub signed_entity_service: Arc<dyn SignedEntityService>,

    /// Certifier Service
    pub certifier_service: Arc<dyn CertifierService>,

    /// Epoch service
    pub epoch_service: EpochServiceWrapper,

    /// Ticker Service
    pub ticker_service: Arc<dyn TickerService>,

    /// Signed Entity storer
    pub signed_entity_storer: Arc<dyn SignedEntityStorer>,

    /// Signer getter service
    pub signer_getter: Arc<dyn SignerGetter>,

    /// HTTP message service
    pub message_service: Arc<dyn MessageService>,

    /// Prover service
    pub prover_service: Arc<dyn ProverService>,

    /// Signed Entity Type Lock
    pub signed_entity_type_lock: Arc<SignedEntityTypeLock>,

    /// Upkeep service
    pub upkeep_service: Arc<dyn UpkeepService>,

    /// Single signer authenticator
    pub single_signer_authenticator: Arc<SingleSignatureAuthenticator>,

    /// Metrics service
    pub metrics_service: Arc<MetricsService>,
}

#[doc(hidden)]
impl DependencyContainer {
    /// `TEST METHOD ONLY`
    ///
    /// Get the first two epochs that will be used by a newly started aggregator
    pub async fn get_genesis_epochs(&self) -> (Epoch, Epoch) {
        let current_epoch = self
            .chain_observer
            .get_current_epoch()
            .await
            .expect("get_current_epoch should not fail")
            .expect("an epoch should've been set to the chain observer");
        let work_epoch = current_epoch
            .offset_to_signer_retrieval_epoch()
            .expect("epoch.offset_by SIGNER_EPOCH_RETRIEVAL_OFFSET should not fail");
        let epoch_to_sign = current_epoch.offset_to_next_signer_retrieval_epoch();

        (work_epoch, epoch_to_sign)
    }

    /// `TEST METHOD ONLY`
    ///
    /// Fill the stores of a [DependencyManager] in a way to simulate an aggregator state
    /// using the data from a precomputed fixture.
    pub async fn init_state_from_fixture(&self, fixture: &MithrilFixture, target_epochs: &[Epoch]) {
        for epoch in target_epochs {
            self.epoch_settings_storer
                .save_epoch_settings(
                    *epoch,
                    #[allow(deprecated)]
                    AggregatorEpochSettings {
                        protocol_parameters: fixture.protocol_parameters(),
                        cardano_transactions_signing_config: self
                            .config
                            .cardano_transactions_signing_config
                            .clone(),
                    },
                )
                .await
                .expect("save_epoch_settings should not fail");
            self.fill_verification_key_store(*epoch, &fixture.signers_with_stake())
                .await;
            self.fill_stakes_store(*epoch, fixture.signers_with_stake())
                .await;
        }
    }

    /// `TEST METHOD ONLY`
    ///
    /// Fill the stores of a [DependencyManager] in a way to simulate an aggregator genesis state.
    ///
    /// For the current and the next epoch:
    /// * Fill the [VerificationKeyStorer] with the given signers keys.
    /// * Fill the [StakeStore] with the given signers stakes.
    /// * Fill the [ProtocolParametersStore] with the given parameters.
    pub async fn prepare_for_genesis(
        &self,
        genesis_signers: Vec<SignerWithStake>,
        second_epoch_signers: Vec<SignerWithStake>,
        genesis_protocol_parameters: &ProtocolParameters,
        cardano_transactions_signing_config: &CardanoTransactionsSigningConfig,
    ) {
        self.init_epoch_settings_storer(&AggregatorEpochSettings {
            protocol_parameters: genesis_protocol_parameters.clone(),
            cardano_transactions_signing_config: cardano_transactions_signing_config.clone(),
        })
        .await;

        let (work_epoch, epoch_to_sign) = self.get_genesis_epochs().await;
        for (epoch, signers) in [
            (work_epoch, genesis_signers),
            (epoch_to_sign, second_epoch_signers),
        ] {
            self.fill_verification_key_store(epoch, &signers).await;
            self.fill_stakes_store(epoch, signers).await;
        }
    }

    /// `TEST METHOD ONLY`
    ///
    /// Fill up to the first three epochs of the [EpochSettingsStorer] with the given value.
    pub async fn init_epoch_settings_storer(&self, epoch_settings: &AggregatorEpochSettings) {
        let (work_epoch, epoch_to_sign) = self.get_genesis_epochs().await;
        let mut epochs_to_save = Vec::new();
        epochs_to_save.push(work_epoch);
        epochs_to_save.push(epoch_to_sign);
        epochs_to_save.push(epoch_to_sign.next());
        for epoch in epochs_to_save {
            self.epoch_settings_storer
                .save_epoch_settings(epoch, epoch_settings.clone())
                .await
                .expect("save_epoch_settings should not fail");
        }
    }

    async fn fill_verification_key_store(&self, target_epoch: Epoch, signers: &[SignerWithStake]) {
        for signer in signers {
            self.signer_recorder
                .record_signer_registration(signer.party_id.clone())
                .await
                .expect("record_signer_registration should not fail");
            self.verification_key_store
                .save_verification_key(target_epoch, signer.clone())
                .await
                .expect("save_verification_key should not fail");
        }
    }

    async fn fill_stakes_store(&self, target_epoch: Epoch, signers: Vec<SignerWithStake>) {
        let _ = self
            .stake_store
            .save_stakes(
                target_epoch,
                signers
                    .iter()
                    .map(|s| s.into())
                    .collect::<StakeDistribution>(),
            )
            .await
            .expect("save_stakes should not fail");
    }
}

#[cfg(test)]
pub mod tests {
    use crate::{dependency_injection::DependenciesBuilder, Configuration, DependencyContainer};

    pub async fn initialize_dependencies() -> DependencyContainer {
        let config = Configuration::new_sample();
        let mut builder = DependenciesBuilder::new_with_stdout_logger(config);

        builder.build_dependency_container().await.unwrap()
    }
}