|
1 |
| -use crate::{interface::MithrilNetworkConfigurationProvider, model::MithrilNetworkConfiguration}; |
| 1 | +use std::{ |
| 2 | + collections::{BTreeSet, HashMap}, |
| 3 | + sync::Arc, |
| 4 | +}; |
| 5 | + |
| 6 | +use tokio::sync::RwLock; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + interface::MithrilNetworkConfigurationProvider, |
| 10 | + model::{MithrilNetworkConfiguration, SignedEntityTypeConfiguration}, |
| 11 | +}; |
| 12 | +use anyhow::Error; |
2 | 13 | use async_trait::async_trait;
|
3 |
| -use mithril_common::StdResult; |
| 14 | +use mithril_common::{ |
| 15 | + StdResult, |
| 16 | + entities::{ProtocolParameters, SignedEntityTypeDiscriminants, TimePoint}, |
| 17 | +}; |
| 18 | +use mithril_ticker::{MithrilTickerService, TickerService}; |
4 | 19 |
|
5 | 20 | /// A fake [MithrilNetworkConfigurationProvider] that return [MithrilNetworkConfiguration]
|
6 | 21 | pub struct FakeMithrilNetworkConfigurationProvider {
|
7 |
| - configuration: MithrilNetworkConfiguration, |
8 |
| -} |
| 22 | + pub signer_registration_protocol_parameters: ProtocolParameters, |
9 | 23 |
|
10 |
| -impl FakeMithrilNetworkConfigurationProvider { |
11 |
| - /// Create a new [FakeMithrilNetworkConfigurationProvider] |
12 |
| - pub fn from_mithril_network_configuration(configuration: MithrilNetworkConfiguration) -> Self { |
13 |
| - Self { configuration } |
14 |
| - } |
| 24 | + pub available_signed_entity_types: RwLock<BTreeSet<SignedEntityTypeDiscriminants>>, |
| 25 | + |
| 26 | + pub signed_entity_types_config: |
| 27 | + HashMap<SignedEntityTypeDiscriminants, SignedEntityTypeConfiguration>, |
| 28 | + |
| 29 | + ticker_service: Arc<MithrilTickerService>, |
15 | 30 | }
|
16 | 31 |
|
17 |
| -impl Default for FakeMithrilNetworkConfigurationProvider { |
18 |
| - fn default() -> Self { |
| 32 | +impl FakeMithrilNetworkConfigurationProvider { |
| 33 | + pub fn new( |
| 34 | + signer_registration_protocol_parameters: ProtocolParameters, |
| 35 | + available_signed_entity_types: BTreeSet<SignedEntityTypeDiscriminants>, |
| 36 | + signed_entity_types_config: HashMap< |
| 37 | + SignedEntityTypeDiscriminants, |
| 38 | + SignedEntityTypeConfiguration, |
| 39 | + >, |
| 40 | + ticker_service: Arc<MithrilTickerService>, |
| 41 | + ) -> Self { |
19 | 42 | Self {
|
20 |
| - configuration: MithrilNetworkConfiguration { |
21 |
| - epoch: Default::default(), |
22 |
| - signer_registration_protocol_parameters: Default::default(), |
23 |
| - available_signed_entity_types: Default::default(), |
24 |
| - signed_entity_types_config: Default::default(), |
25 |
| - }, |
| 43 | + signer_registration_protocol_parameters, |
| 44 | + available_signed_entity_types: RwLock::new(available_signed_entity_types), |
| 45 | + signed_entity_types_config, |
| 46 | + ticker_service, |
26 | 47 | }
|
27 | 48 | }
|
| 49 | + |
| 50 | + async fn get_time_point(&self) -> Result<TimePoint, Error> { |
| 51 | + //TODO improve error handling ? |
| 52 | + let time_point = self.ticker_service.get_current_time_point().await?; |
| 53 | + |
| 54 | + Ok(time_point) |
| 55 | + } |
| 56 | + |
| 57 | + pub async fn change_allowed_discriminants( |
| 58 | + &self, |
| 59 | + discriminants: &BTreeSet<SignedEntityTypeDiscriminants>, |
| 60 | + ) { |
| 61 | + //let mut signed_entity_types_config = self.signed_entity_types_config.write().await; |
| 62 | + let mut available_signed_entity_types = self.available_signed_entity_types.write().await; |
| 63 | + *available_signed_entity_types = discriminants.clone(); |
| 64 | + } |
28 | 65 | }
|
29 | 66 |
|
30 | 67 | #[cfg_attr(target_family = "wasm", async_trait(?Send))]
|
31 | 68 | #[cfg_attr(not(target_family = "wasm"), async_trait)]
|
32 | 69 | impl MithrilNetworkConfigurationProvider for FakeMithrilNetworkConfigurationProvider {
|
33 | 70 | async fn get(&self) -> StdResult<MithrilNetworkConfiguration> {
|
34 |
| - Ok(self.configuration.clone()) |
35 |
| - } |
36 |
| -} |
37 |
| - |
38 |
| -#[cfg(test)] |
39 |
| -mod tests { |
40 |
| - use std::collections::{BTreeSet, HashMap}; |
41 |
| - |
42 |
| - use mithril_common::{ |
43 |
| - StdResult, |
44 |
| - entities::{BlockNumber, CardanoTransactionsSigningConfig, SignedEntityTypeDiscriminants}, |
45 |
| - }; |
46 |
| - |
47 |
| - use super::*; |
48 |
| - use crate::{model::SignedEntityTypeConfiguration, test::double::fake_data}; |
49 |
| - |
50 |
| - #[tokio::test] |
51 |
| - async fn fake_mithril_network_configuration_provider_returns_configuration() -> StdResult<()> { |
52 |
| - let mut available_signed_entity_types = BTreeSet::new(); |
53 |
| - available_signed_entity_types.insert(SignedEntityTypeDiscriminants::CardanoTransactions); |
54 |
| - |
55 |
| - let mut signed_entity_types_config = HashMap::new(); |
56 |
| - signed_entity_types_config.insert( |
57 |
| - SignedEntityTypeDiscriminants::CardanoTransactions, |
58 |
| - SignedEntityTypeConfiguration::CardanoTransactions(CardanoTransactionsSigningConfig { |
59 |
| - security_parameter: BlockNumber(13), |
60 |
| - step: BlockNumber(26), |
61 |
| - }), |
62 |
| - ); |
63 |
| - |
64 |
| - let configuration = fake_data::mithril_network_configuration( |
65 |
| - 42, |
66 |
| - 1, |
67 |
| - 2, |
68 |
| - 0.123, |
69 |
| - available_signed_entity_types, |
70 |
| - signed_entity_types_config, |
71 |
| - ); |
72 |
| - let provider = FakeMithrilNetworkConfigurationProvider::from_mithril_network_configuration( |
73 |
| - configuration.clone(), |
74 |
| - ); |
| 71 | + let time_point = self.get_time_point().await?; |
75 | 72 |
|
76 |
| - let retrieved_configuration = provider.get().await?; |
| 73 | + let available_signed_entity_types = self.available_signed_entity_types.read().await; |
77 | 74 |
|
78 |
| - assert_eq!(configuration, retrieved_configuration); |
79 |
| - Ok(()) |
| 75 | + Ok(MithrilNetworkConfiguration { |
| 76 | + epoch: time_point.epoch, |
| 77 | + signer_registration_protocol_parameters: self |
| 78 | + .signer_registration_protocol_parameters |
| 79 | + .clone(), |
| 80 | + available_signed_entity_types: available_signed_entity_types.clone(), |
| 81 | + signed_entity_types_config: self.signed_entity_types_config.clone(), |
| 82 | + }) |
80 | 83 | }
|
81 | 84 | }
|
0 commit comments