Skip to content

Commit 0f0d971

Browse files
committed
feature(protocol-config): add test double on MithrilNetworkConfigurationProvider
1 parent 3a02191 commit 0f0d971

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const HTTP_REQUEST_TIMEOUT_DURATION: u64 = 30000;
33
pub mod http_client;
44
pub mod interface;
55
pub mod model;
6+
pub mod test;
67

78
#[cfg(test)]
89
pub(crate) mod test_tools {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ use mithril_common::entities::{
44
CardanoTransactionsSigningConfig, Epoch, ProtocolParameters, SignedEntityTypeDiscriminants,
55
};
66

7+
#[derive(PartialEq, Clone, Debug)]
78
pub enum SignedEntityTypeConfiguration {
89
/// Cardano Transactions
910
CardanoTransactions(CardanoTransactionsSigningConfig),
1011
}
1112

1213
/// A Mithril network configuration
14+
#[derive(PartialEq, Clone, Debug)]
1315
pub struct MithrilNetworkConfiguration {
1416
/// Epoch
1517
pub epoch: Epoch,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::collections::{BTreeSet, HashMap};
2+
3+
use mithril_common::entities::{Epoch, ProtocolParameters, SignedEntityTypeDiscriminants};
4+
5+
use crate::model::{MithrilNetworkConfiguration, SignedEntityTypeConfiguration};
6+
7+
pub fn mithril_network_configuration(
8+
epoch: u64,
9+
k: u64,
10+
m: u64,
11+
phi_f: f64,
12+
available_signed_entity_types: BTreeSet<SignedEntityTypeDiscriminants>,
13+
signed_entity_types_config: HashMap<
14+
SignedEntityTypeDiscriminants,
15+
SignedEntityTypeConfiguration,
16+
>,
17+
) -> MithrilNetworkConfiguration {
18+
MithrilNetworkConfiguration {
19+
epoch: Epoch(epoch),
20+
signer_registration_protocol_parameters: ProtocolParameters { k, m, phi_f },
21+
available_signed_entity_types,
22+
signed_entity_types_config,
23+
}
24+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::{interface::MithrilNetworkConfigurationProvider, model::MithrilNetworkConfiguration};
2+
use async_trait::async_trait;
3+
use mithril_common::StdResult;
4+
5+
/// A fake [MithrilNetworkConfigurationProvider] that return [MithrilNetworkConfiguration]
6+
pub struct FakeMithrilNetworkConfigurationProvider {
7+
configuration: MithrilNetworkConfiguration,
8+
}
9+
10+
impl FakeMithrilNetworkConfigurationProvider {
11+
/// Create a new [FakeMithrilNetworkConfigurationProvider]
12+
pub fn from_mithril_network_configuration(configuration: MithrilNetworkConfiguration) -> Self {
13+
Self { configuration }
14+
}
15+
}
16+
17+
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
18+
#[cfg_attr(not(target_family = "wasm"), async_trait)]
19+
impl MithrilNetworkConfigurationProvider for FakeMithrilNetworkConfigurationProvider {
20+
async fn get(&self) -> StdResult<MithrilNetworkConfiguration> {
21+
Ok(self.configuration.clone())
22+
}
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use std::collections::{BTreeSet, HashMap};
28+
29+
use mithril_common::{
30+
StdResult,
31+
entities::{BlockNumber, CardanoTransactionsSigningConfig, SignedEntityTypeDiscriminants},
32+
};
33+
34+
use super::*;
35+
use crate::{model::SignedEntityTypeConfiguration, test::double::fake_data};
36+
37+
#[tokio::test]
38+
async fn fake_mithril_network_configuration_provider_returns_configuration() -> StdResult<()> {
39+
let mut available_signed_entity_types = BTreeSet::new();
40+
available_signed_entity_types.insert(SignedEntityTypeDiscriminants::CardanoTransactions);
41+
42+
let mut signed_entity_types_config = HashMap::new();
43+
signed_entity_types_config.insert(
44+
SignedEntityTypeDiscriminants::CardanoTransactions,
45+
SignedEntityTypeConfiguration::CardanoTransactions(CardanoTransactionsSigningConfig {
46+
security_parameter: BlockNumber(13),
47+
step: BlockNumber(26),
48+
}),
49+
);
50+
51+
let configuration = fake_data::mithril_network_configuration(
52+
42,
53+
1,
54+
2,
55+
0.123,
56+
available_signed_entity_types,
57+
signed_entity_types_config,
58+
);
59+
let provider = FakeMithrilNetworkConfigurationProvider::from_mithril_network_configuration(
60+
configuration.clone(),
61+
);
62+
63+
let retrieved_configuration = provider.get().await?;
64+
65+
assert_eq!(configuration, retrieved_configuration);
66+
Ok(())
67+
}
68+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod fake_data;
2+
mod mithril_network_configuration_provider;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod double;

0 commit comments

Comments
 (0)