Skip to content

Commit 1b74219

Browse files
committed
test(aggregator): simplify & rework serve deps container test tooling
Rework `init_state_from_fixture` to not save epoch_settings and works with the fixed window of three epoch (aggregate/next aggregate/signer registration), epoch settings should already exists, most of the time they will be inserted by the handle discrepancies system
1 parent 64d77cf commit 1b74219

File tree

5 files changed

+92
-236
lines changed

5 files changed

+92
-236
lines changed

mithril-aggregator/src/dependency_injection/builder/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ impl DependenciesBuilder {
386386
certificate_repository: self.get_certificate_repository().await?,
387387
verification_key_store: self.get_verification_key_store().await?,
388388
epoch_settings_storer: self.get_epoch_settings_store().await?,
389-
chain_observer: self.get_chain_observer().await?,
390389
certificate_chain_synchronizer: self.get_certificate_chain_synchronizer().await?,
391390
signer_registerer: self.get_signer_registerer().await?,
392391
signer_synchronizer: self.get_signer_synchronizer().await?,

mithril-aggregator/src/dependency_injection/containers/serve.rs

Lines changed: 19 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ use slog::Logger;
22
use std::sync::Arc;
33
use tokio::sync::RwLock;
44

5-
use mithril_cardano_node_chain::chain_observer::ChainObserver;
65
use mithril_common::{
76
api_version::APIVersionProvider,
8-
entities::{
9-
CardanoTransactionsSigningConfig, Epoch, ProtocolParameters, SignerWithStake,
10-
StakeDistribution,
11-
},
7+
entities::{Epoch, SignerWithStake, StakeDistribution},
128
signable_builder::SignableBuilderService,
139
test::builder::MithrilFixture,
1410
};
@@ -24,7 +20,6 @@ use crate::{
2420
database::repository::{
2521
CertificateRepository, SignedEntityStorer, SignerGetter, StakePoolStore,
2622
},
27-
entities::AggregatorEpochSettings,
2823
event_store::{EventMessage, TransmitterService},
2924
services::{
3025
CertificateChainSynchronizer, CertifierService, EpochService, MessageService,
@@ -55,9 +50,6 @@ pub struct ServeCommandDependenciesContainer {
5550
/// Epoch settings storer.
5651
pub epoch_settings_storer: Arc<dyn EpochSettingsStorer>,
5752

58-
/// Chain observer service.
59-
pub(crate) chain_observer: Arc<dyn ChainObserver>,
60-
6153
/// Certificate chain synchronizer service
6254
pub(crate) certificate_chain_synchronizer: Arc<dyn CertificateChainSynchronizer>,
6355

@@ -132,94 +124,27 @@ pub struct ServeCommandDependenciesContainer {
132124
impl ServeCommandDependenciesContainer {
133125
/// `TEST METHOD ONLY`
134126
///
135-
/// Get the first two epochs that will be used by a newly started aggregator
136-
pub async fn get_genesis_epochs(&self) -> (Epoch, Epoch) {
137-
let current_epoch = self
138-
.chain_observer
139-
.get_current_epoch()
140-
.await
141-
.expect("get_current_epoch should not fail")
142-
.expect("an epoch should've been set to the chain observer");
143-
let work_epoch = current_epoch
144-
.offset_to_signer_retrieval_epoch()
145-
.expect("epoch.offset_by SIGNER_EPOCH_RETRIEVAL_OFFSET should not fail");
146-
let epoch_to_sign = current_epoch.offset_to_next_signer_retrieval_epoch();
147-
148-
(work_epoch, epoch_to_sign)
149-
}
150-
151-
/// `TEST METHOD ONLY`
152-
///
153-
/// Fill the stores of a [DependencyManager] in a way to simulate an aggregator state
127+
/// Fill the stores of this container in a way to simulate an aggregator state
154128
/// using the data from a precomputed fixture.
129+
///
130+
/// Data will be inserted in the given `next_aggregation_epoch`, the current aggregation epoch
131+
/// (`next_aggregation_epoch - 1`), and the signer registration epoch (`next_aggregation_epoch + 1`).
132+
///
133+
/// Note: `epoch_settings` store must have data for the inserted epochs, this should be done
134+
/// automatically when building the [ServeCommandDependenciesContainer] by `handle_discrepancies_at_startup`
155135
pub async fn init_state_from_fixture(
156136
&self,
157137
fixture: &MithrilFixture,
158-
cardano_transactions_signing_config: &CardanoTransactionsSigningConfig,
159-
target_epochs: &[Epoch],
138+
next_aggregation_epoch: Epoch,
160139
) {
161-
for epoch in target_epochs {
162-
self.epoch_settings_storer
163-
.save_epoch_settings(
164-
*epoch,
165-
AggregatorEpochSettings {
166-
protocol_parameters: fixture.protocol_parameters(),
167-
cardano_transactions_signing_config: cardano_transactions_signing_config
168-
.clone(),
169-
},
170-
)
171-
.await
172-
.expect("save_epoch_settings should not fail");
173-
self.fill_verification_key_store(*epoch, &fixture.signers_with_stake())
140+
for epoch in [
141+
next_aggregation_epoch.offset_to_signer_retrieval_epoch().unwrap(),
142+
next_aggregation_epoch,
143+
next_aggregation_epoch.offset_to_recording_epoch(),
144+
] {
145+
self.fill_verification_key_store(epoch, &fixture.signers_with_stake())
174146
.await;
175-
self.fill_stakes_store(*epoch, fixture.signers_with_stake()).await;
176-
}
177-
}
178-
179-
/// `TEST METHOD ONLY`
180-
///
181-
/// Fill the stores of a [DependencyManager] in a way to simulate an aggregator genesis state.
182-
///
183-
/// For the current and the next epoch:
184-
/// * Fill the [VerificationKeyStorer] with the given signers keys.
185-
/// * Fill the [StakeStore] with the given signers stakes.
186-
/// * Fill the [ProtocolParametersStore] with the given parameters.
187-
pub async fn prepare_for_genesis(
188-
&self,
189-
genesis_signers: Vec<SignerWithStake>,
190-
second_epoch_signers: Vec<SignerWithStake>,
191-
genesis_protocol_parameters: &ProtocolParameters,
192-
cardano_transactions_signing_config: &CardanoTransactionsSigningConfig,
193-
) {
194-
self.init_epoch_settings_storer(&AggregatorEpochSettings {
195-
protocol_parameters: genesis_protocol_parameters.clone(),
196-
cardano_transactions_signing_config: cardano_transactions_signing_config.clone(),
197-
})
198-
.await;
199-
200-
let (work_epoch, epoch_to_sign) = self.get_genesis_epochs().await;
201-
for (epoch, signers) in
202-
[(work_epoch, genesis_signers), (epoch_to_sign, second_epoch_signers)]
203-
{
204-
self.fill_verification_key_store(epoch, &signers).await;
205-
self.fill_stakes_store(epoch, signers).await;
206-
}
207-
}
208-
209-
/// `TEST METHOD ONLY`
210-
///
211-
/// Fill up to the first three epochs of the [EpochSettingsStorer] with the given value.
212-
pub async fn init_epoch_settings_storer(&self, epoch_settings: &AggregatorEpochSettings) {
213-
let (work_epoch, epoch_to_sign) = self.get_genesis_epochs().await;
214-
let mut epochs_to_save = Vec::new();
215-
epochs_to_save.push(work_epoch);
216-
epochs_to_save.push(epoch_to_sign);
217-
epochs_to_save.push(epoch_to_sign.next());
218-
for epoch in epochs_to_save {
219-
self.epoch_settings_storer
220-
.save_epoch_settings(epoch, epoch_settings.clone())
221-
.await
222-
.expect("save_epoch_settings should not fail");
147+
self.fill_stakes_store(epoch, fixture.signers_with_stake()).await;
223148
}
224149
}
225150

@@ -250,13 +175,11 @@ impl ServeCommandDependenciesContainer {
250175

251176
#[cfg(test)]
252177
pub(crate) mod tests {
178+
use std::path::PathBuf;
253179

254-
use std::{path::PathBuf, sync::Arc};
180+
use crate::{ServeCommandConfiguration, dependency_injection::DependenciesBuilder};
255181

256-
use crate::{
257-
ServeCommandConfiguration, ServeCommandDependenciesContainer,
258-
dependency_injection::DependenciesBuilder,
259-
};
182+
use super::*;
260183

261184
/// Initialize dependency container with a unique temporary snapshot directory build from test path.
262185
/// This macro should used directly in a function test to be able to retrieve the function name.

mithril-aggregator/src/runtime/runner.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,8 @@ pub mod tests {
530530
use mithril_common::{
531531
StdResult,
532532
entities::{
533-
CardanoTransactionsSigningConfig, ChainPoint, Epoch, ProtocolMessage,
534-
SignedEntityConfig, SignedEntityType, SignedEntityTypeDiscriminants, StakeDistribution,
535-
TimePoint,
533+
ChainPoint, Epoch, ProtocolMessage, SignedEntityConfig, SignedEntityType,
534+
SignedEntityTypeDiscriminants, StakeDistribution, TimePoint,
536535
},
537536
signable_builder::SignableBuilderService,
538537
temp_dir,
@@ -549,12 +548,11 @@ pub mod tests {
549548
MithrilSignerRegistrationLeader, ServeCommandConfiguration,
550549
ServeCommandDependenciesContainer, SignerRegistrationRound,
551550
dependency_injection::DependenciesBuilder,
552-
entities::{AggregatorEpochSettings, OpenMessage},
551+
entities::OpenMessage,
553552
initialize_dependencies,
554553
runtime::{AggregatorRunner, AggregatorRunnerTrait},
555554
services::{
556-
FakeEpochService, FakeEpochServiceBuilder, MithrilStakeDistributionService,
557-
MockCertifierService, MockUpkeepService,
555+
FakeEpochService, FakeEpochServiceBuilder, MockCertifierService, MockUpkeepService,
558556
},
559557
};
560558

@@ -576,17 +574,8 @@ pub mod tests {
576574
deps: ServeCommandDependenciesContainer,
577575
) -> AggregatorRunner {
578576
let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
579-
let current_epoch = deps.chain_observer.get_current_epoch().await.unwrap().unwrap();
580-
deps.init_state_from_fixture(
581-
&fixture,
582-
&CardanoTransactionsSigningConfig::dummy(),
583-
&[
584-
current_epoch.offset_to_signer_retrieval_epoch().unwrap(),
585-
current_epoch,
586-
current_epoch.next(),
587-
],
588-
)
589-
.await;
577+
let current_epoch = deps.ticker_service.get_current_epoch().await.unwrap();
578+
deps.init_state_from_fixture(&fixture, current_epoch).await;
590579

591580
AggregatorRunner::new(Arc::new(deps))
592581
}
@@ -704,13 +693,11 @@ pub mod tests {
704693
async fn test_update_stake_distribution() {
705694
let chain_observer = Arc::new(FakeChainObserver::default());
706695
let deps = {
707-
let mut deps = initialize_dependencies!().await;
708-
deps.chain_observer = chain_observer.clone();
709-
deps.stake_distribution_service = Arc::new(MithrilStakeDistributionService::new(
710-
deps.stake_store.clone(),
711-
chain_observer.clone(),
712-
));
713-
Arc::new(deps)
696+
let config = ServeCommandConfiguration::new_sample(temp_dir!());
697+
let mut builder = DependenciesBuilder::new_with_stdout_logger(Arc::new(config));
698+
builder.chain_observer = Some(chain_observer.clone());
699+
700+
Arc::new(builder.build_serve_dependencies_container().await.unwrap())
714701
};
715702
let runner = AggregatorRunner::new(deps.clone());
716703
let time_point = runner.get_time_point_from_chain().await.unwrap();
@@ -865,7 +852,7 @@ pub mod tests {
865852
.returning(|_| Ok(()))
866853
.times(1);
867854
let mut deps = initialize_dependencies!().await;
868-
let current_epoch = deps.chain_observer.get_current_epoch().await.unwrap().unwrap();
855+
let current_epoch = deps.ticker_service.get_current_epoch().await.unwrap();
869856

870857
deps.certifier_service = Arc::new(mock_certifier_service);
871858
deps.epoch_service = Arc::new(RwLock::new(FakeEpochService::from_fixture(
@@ -898,7 +885,7 @@ pub mod tests {
898885
#[tokio::test]
899886
async fn test_precompute_epoch_data() {
900887
let mut deps = initialize_dependencies!().await;
901-
let current_epoch = deps.chain_observer.get_current_epoch().await.unwrap().unwrap();
888+
let current_epoch = deps.ticker_service.get_current_epoch().await.unwrap();
902889

903890
deps.epoch_service = Arc::new(RwLock::new(FakeEpochService::from_fixture(
904891
current_epoch,

0 commit comments

Comments
 (0)