Skip to content

Commit 683fd96

Browse files
committed
WIP: now retrieving epoch settings from MithrilNetworkConfiguration, adapt inform_epoch_settings signature, TODO fix tests
1 parent 608bc05 commit 683fd96

File tree

6 files changed

+305
-114
lines changed

6 files changed

+305
-114
lines changed

internal/mithril-protocol-config/src/model.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,20 @@ pub struct MithrilNetworkConfiguration {
2626
pub signed_entity_types_config:
2727
HashMap<SignedEntityTypeDiscriminants, SignedEntityTypeConfiguration>,
2828
}
29+
30+
impl MithrilNetworkConfiguration {
31+
/// Get the Cardano Transactions signing configuration
32+
pub fn get_cardano_transactions_signing_config(
33+
&self,
34+
) -> Option<CardanoTransactionsSigningConfig> {
35+
match self
36+
.signed_entity_types_config
37+
.get(&SignedEntityTypeDiscriminants::CardanoTransactions)
38+
{
39+
Some(SignedEntityTypeConfiguration::CardanoTransactions(config)) => {
40+
Some(config.clone())
41+
}
42+
_ => None,
43+
}
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::collections::{BTreeSet, HashMap};
2+
3+
use mithril_common::{
4+
entities::{CardanoTransactionsSigningConfig, SignedEntityTypeDiscriminants},
5+
test::double::{Dummy, fake_data},
6+
};
7+
8+
use crate::model::{MithrilNetworkConfiguration, SignedEntityTypeConfiguration};
9+
10+
impl Dummy for MithrilNetworkConfiguration {
11+
/// Return a dummy [MithrilNetworkConfiguration] (test-only).
12+
fn dummy() -> Self {
13+
let beacon = fake_data::beacon();
14+
15+
let signer_registration_protocol_parameters = fake_data::protocol_parameters();
16+
17+
let mut available_signed_entity_types = BTreeSet::new();
18+
available_signed_entity_types.insert(SignedEntityTypeDiscriminants::CardanoTransactions);
19+
20+
let mut signed_entity_types_config = HashMap::new();
21+
signed_entity_types_config.insert(
22+
SignedEntityTypeDiscriminants::CardanoTransactions,
23+
SignedEntityTypeConfiguration::CardanoTransactions(
24+
CardanoTransactionsSigningConfig::dummy(),
25+
),
26+
);
27+
28+
Self {
29+
epoch: beacon.epoch,
30+
signer_registration_protocol_parameters,
31+
available_signed_entity_types,
32+
signed_entity_types_config,
33+
}
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
mod dummies;
12
mod fake_data;
23
pub mod mithril_network_configuration_provider;

mithril-signer/src/runtime/runner.rs

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ pub trait Runner: Send + Sync {
4242
async fn can_sign_current_epoch(&self) -> StdResult<bool>;
4343

4444
/// Register epoch information
45-
async fn inform_epoch_settings(&self, epoch_settings: SignerEpochSettings) -> StdResult<()>;
45+
async fn inform_epoch_settings(
46+
&self,
47+
mithril_network_configuration: MithrilNetworkConfiguration,
48+
current_signer: Vec<Signer>,
49+
next_signer: Vec<Signer>,
50+
) -> StdResult<()>;
4651

4752
/// Create the message to be signed with the single signature.
4853
async fn compute_message(
@@ -250,25 +255,22 @@ impl Runner for SignerRunner {
250255
epoch_service.can_signer_sign_current_epoch(self.services.single_signer.get_party_id())
251256
}
252257

253-
async fn inform_epoch_settings(&self, epoch_settings: SignerEpochSettings) -> StdResult<()> {
258+
async fn inform_epoch_settings(
259+
&self,
260+
mithril_network_configuration: MithrilNetworkConfiguration,
261+
current_signer: Vec<Signer>,
262+
next_signer: Vec<Signer>,
263+
) -> StdResult<()> {
254264
debug!(
255265
self.logger,
256-
">> inform_epoch_settings(epoch:{})", epoch_settings.epoch
266+
">> inform_epoch_settings(epoch:{})", mithril_network_configuration.epoch
257267
);
258-
let aggregator_features = self
259-
.services
260-
.certificate_handler
261-
.retrieve_aggregator_features()
262-
.await?;
263268

264269
self.services
265270
.epoch_service
266271
.write()
267272
.await
268-
.inform_epoch_settings(
269-
epoch_settings,
270-
aggregator_features.capabilities.signed_entity_types,
271-
)
273+
.inform_epoch_settings(mithril_network_configuration, current_signer, next_signer)
272274
.await
273275
}
274276

@@ -638,14 +640,20 @@ mod tests {
638640
.unwrap();
639641

640642
let runner = init_runner(Some(services), None).await;
641-
// inform epoch settings
642-
let epoch_settings = SignerEpochSettings {
643+
644+
let mithril_network_configuration = MithrilNetworkConfiguration {
643645
epoch: current_epoch,
644-
current_signers: fixture.signers(),
645-
next_signers: fixture.signers(),
646-
..SignerEpochSettings::dummy().clone()
646+
..MithrilNetworkConfiguration::dummy()
647647
};
648-
runner.inform_epoch_settings(epoch_settings).await.unwrap();
648+
649+
runner
650+
.inform_epoch_settings(
651+
mithril_network_configuration,
652+
fixture.signers(),
653+
fixture.signers(),
654+
)
655+
.await
656+
.unwrap();
649657

650658
runner
651659
.register_signer_to_aggregator()
@@ -697,26 +705,39 @@ mod tests {
697705
async fn test_inform_epoch_setting_pass_allowed_discriminant_to_epoch_service() {
698706
let mut services = init_services().await;
699707
let certificate_handler = Arc::new(DumbAggregatorClient::default());
700-
certificate_handler
701-
.set_aggregator_features(AggregatorFeaturesMessage {
702-
capabilities: AggregatorCapabilities {
703-
signed_entity_types: BTreeSet::from([
704-
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
705-
SignedEntityTypeDiscriminants::CardanoTransactions,
706-
]),
707-
..AggregatorFeaturesMessage::dummy().capabilities
708-
},
709-
..AggregatorFeaturesMessage::dummy()
710-
})
711-
.await;
708+
// certificate_handler
709+
// .set_aggregator_features(AggregatorFeaturesMessage {
710+
// capabilities: AggregatorCapabilities {
711+
// signed_entity_types: BTreeSet::from([
712+
// SignedEntityTypeDiscriminants::MithrilStakeDistribution,
713+
// SignedEntityTypeDiscriminants::CardanoTransactions,
714+
// ]),
715+
// ..AggregatorFeaturesMessage::dummy().capabilities
716+
// },
717+
// ..AggregatorFeaturesMessage::dummy()
718+
// })
719+
// .await;
712720
services.certificate_handler = certificate_handler;
713721
let runner = init_runner(Some(services), None).await;
714722

715-
let epoch_settings = SignerEpochSettings {
723+
let mithril_network_configuration = MithrilNetworkConfiguration {
716724
epoch: Epoch(1),
717-
..SignerEpochSettings::dummy()
725+
available_signed_entity_types: BTreeSet::from([
726+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
727+
SignedEntityTypeDiscriminants::CardanoTransactions,
728+
]),
729+
..MithrilNetworkConfiguration::dummy()
718730
};
719-
runner.inform_epoch_settings(epoch_settings).await.unwrap();
731+
732+
// Signers
733+
let signers = fake_data::signers(5);
734+
let current_signers = signers[1..3].to_vec();
735+
let next_signers = signers[2..5].to_vec();
736+
737+
runner
738+
.inform_epoch_settings(mithril_network_configuration, current_signers, next_signers)
739+
.await
740+
.unwrap();
720741

721742
let epoch_service = runner.services.epoch_service.read().await;
722743
let recorded_allowed_discriminants = epoch_service.allowed_discriminants().unwrap();

mithril-signer/src/runtime/state_machine.rs

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
use anyhow::Error;
22
use chrono::Local;
3+
use mithril_protocol_config::model::MithrilNetworkConfiguration;
34
use slog::{Logger, debug, info};
45
use std::{fmt::Display, ops::Deref, sync::Arc, time::Duration};
56
use tokio::sync::Mutex;
67

78
use mithril_common::{
89
crypto_helper::ProtocolInitializerError,
9-
entities::{Epoch, TimePoint},
10+
entities::{Epoch, Signer, TimePoint},
1011
logging::LoggerExtensions,
1112
};
1213

13-
use crate::{
14-
MetricsService,
15-
entities::{BeaconToSign, SignerEpochSettings},
16-
services::AggregatorClientError,
17-
};
14+
use crate::{MetricsService, entities::BeaconToSign, services::AggregatorClientError};
1815

1916
use super::{Runner, RuntimeError};
2017

@@ -167,7 +164,7 @@ impl StateMachine {
167164
"→ Epoch has changed, transiting to Unregistered"
168165
);
169166
*state = self.transition_from_unregistered_to_unregistered(new_epoch).await?;
170-
} else if let Some(epoch_settings) = self
167+
} else if let Some(signer_settings) = self
171168
.runner
172169
.get_epoch_settings()
173170
.await
@@ -176,19 +173,33 @@ impl StateMachine {
176173
nested_error: Some(e),
177174
})?
178175
{
179-
info!(self.logger, "→ Epoch settings found");
180-
if epoch_settings.epoch >= *epoch {
176+
info!(self.logger, "→ Epoch settings found"); //TODO Do we switch the logs and type from SignerEpochSettings to SignerSettings since now we only read current and next signer from it ?
177+
let network_configuration = self
178+
.runner
179+
.get_mithril_network_configuration()
180+
.await
181+
.map_err(|e| RuntimeError::KeepState {
182+
message: "could not retrieve mithril network configuration".to_string(),
183+
nested_error: Some(e),
184+
})?;
185+
info!(self.logger, "→ Mithril network configuration found");
186+
187+
if network_configuration.epoch >= *epoch {
181188
info!(self.logger, "New Epoch found");
182189
info!(self.logger, " ⋅ Transiting to Registered");
183190
*state = self
184191
.transition_from_unregistered_to_one_of_registered_states(
185-
epoch_settings,
192+
network_configuration,
193+
signer_settings.current_signers,
194+
signer_settings.next_signers,
186195
)
187196
.await?;
188197
} else {
189198
info!(
190-
self.logger, " ⋅ Epoch settings found, but its epoch is behind the known epoch, waiting…";
191-
"epoch_settings" => ?epoch_settings,
199+
self.logger, " ⋅ Signer settings and Network Configuration found, but its epoch is behind the known epoch, waiting…";
200+
"network_configuration" => ?network_configuration,
201+
"current_singer" => ?signer_settings.current_signers,
202+
"next_signer" => ?signer_settings.next_signers,
192203
"known_epoch" => ?epoch,
193204
);
194205
}
@@ -286,7 +297,9 @@ impl StateMachine {
286297
/// Launch the transition process from the `Unregistered` to `ReadyToSign` or `RegisteredNotAbleToSign` state.
287298
async fn transition_from_unregistered_to_one_of_registered_states(
288299
&self,
289-
epoch_settings: SignerEpochSettings,
300+
mithril_network_configuration: MithrilNetworkConfiguration,
301+
current_signer: Vec<Signer>,
302+
next_signer: Vec<Signer>,
290303
) -> Result<SignerState, RuntimeError> {
291304
self.metrics_service
292305
.get_signer_registration_total_since_startup_counter()
@@ -302,7 +315,7 @@ impl StateMachine {
302315
})?;
303316

304317
self.runner
305-
.inform_epoch_settings(epoch_settings)
318+
.inform_epoch_settings(mithril_network_configuration, current_signer, next_signer)
306319
.await
307320
.map_err(|e| RuntimeError::KeepState {
308321
message: format!(
@@ -474,11 +487,13 @@ impl StateMachine {
474487
mod tests {
475488
use anyhow::anyhow;
476489
use chrono::DateTime;
490+
use mithril_protocol_config::model::MithrilNetworkConfiguration;
477491
use mockall::predicate;
478492

479493
use mithril_common::entities::{ChainPoint, Epoch, ProtocolMessage, SignedEntityType};
480494
use mithril_common::test::double::{Dummy, fake_data};
481495

496+
use crate::SignerEpochSettings;
482497
use crate::runtime::runner::MockSignerRunner;
483498
use crate::services::AggregatorClientError;
484499
use crate::test_tools::TestLogger;
@@ -528,7 +543,7 @@ mod tests {
528543
async fn unregistered_epoch_settings_behind_known_epoch() {
529544
let mut runner = MockSignerRunner::new();
530545
let epoch_settings = SignerEpochSettings {
531-
epoch: Epoch(3),
546+
epoch: Epoch(999), // epoch is no longer read from get_epoch_settings anymore but from mithril network configuration
532547
registration_protocol_parameters: fake_data::protocol_parameters(),
533548
current_signers: vec![],
534549
next_signers: vec![],
@@ -539,6 +554,17 @@ mod tests {
539554
.expect_get_epoch_settings()
540555
.once()
541556
.returning(move || Ok(Some(epoch_settings.to_owned())));
557+
runner
558+
.expect_get_mithril_network_configuration()
559+
.once()
560+
.returning(|| {
561+
Ok(MithrilNetworkConfiguration {
562+
epoch: Epoch(3),
563+
signer_registration_protocol_parameters: fake_data::protocol_parameters(),
564+
available_signed_entity_types: Default::default(),
565+
signed_entity_types_config: Default::default(),
566+
})
567+
});
542568
runner.expect_get_current_time_point().once().returning(|| {
543569
Ok(TimePoint {
544570
epoch: Epoch(4),
@@ -567,11 +593,21 @@ mod tests {
567593
.once()
568594
.returning(|| Ok(Some(SignerEpochSettings::dummy())));
569595

596+
runner
597+
.expect_get_mithril_network_configuration()
598+
.once()
599+
.returning(|| Ok(MithrilNetworkConfiguration::dummy()));
600+
570601
runner
571602
.expect_inform_epoch_settings()
572-
.with(predicate::eq(SignerEpochSettings::dummy()))
603+
.with(
604+
//todo do we really want to specify a WITH ?
605+
predicate::eq(MithrilNetworkConfiguration::dummy()),
606+
predicate::eq(SignerEpochSettings::dummy().current_signers),
607+
predicate::eq(SignerEpochSettings::dummy().next_signers),
608+
)
573609
.once()
574-
.returning(|_| Ok(()));
610+
.returning(|_, _, _| Ok(()));
575611

576612
runner
577613
.expect_get_current_time_point()
@@ -615,11 +651,21 @@ mod tests {
615651
.once()
616652
.returning(|| Ok(Some(SignerEpochSettings::dummy())));
617653

654+
runner
655+
.expect_get_mithril_network_configuration()
656+
.once()
657+
.returning(|| Ok(MithrilNetworkConfiguration::dummy()));
658+
618659
runner
619660
.expect_inform_epoch_settings()
620-
.with(predicate::eq(SignerEpochSettings::dummy()))
661+
.with(
662+
//todo do we really want to specify a WITH ?
663+
predicate::eq(MithrilNetworkConfiguration::dummy()),
664+
predicate::eq(SignerEpochSettings::dummy().current_signers),
665+
predicate::eq(SignerEpochSettings::dummy().next_signers),
666+
)
621667
.once()
622-
.returning(|_| Ok(()));
668+
.returning(|_, _, _| Ok(()));
623669

624670
runner
625671
.expect_get_current_time_point()
@@ -667,11 +713,21 @@ mod tests {
667713
.once()
668714
.returning(|| Ok(Some(SignerEpochSettings::dummy())));
669715

716+
runner
717+
.expect_get_mithril_network_configuration()
718+
.once()
719+
.returning(|| Ok(MithrilNetworkConfiguration::dummy()));
720+
670721
runner
671722
.expect_inform_epoch_settings()
672-
.with(predicate::eq(SignerEpochSettings::dummy()))
723+
.with(
724+
//todo do we really want to specify a WITH ?
725+
predicate::eq(MithrilNetworkConfiguration::dummy()),
726+
predicate::eq(SignerEpochSettings::dummy().current_signers),
727+
predicate::eq(SignerEpochSettings::dummy().next_signers),
728+
)
673729
.once()
674-
.returning(|_| Ok(()));
730+
.returning(|_, _, _| Ok(()));
675731

676732
runner
677733
.expect_get_current_time_point()

0 commit comments

Comments
 (0)