Skip to content

Commit 1843219

Browse files
committed
Add type conversions in crypto helper
1 parent 1ddb4e4 commit 1843219

File tree

4 files changed

+91
-59
lines changed

4 files changed

+91
-59
lines changed

mithril-aggregator/src/main.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clap::Parser;
55
use config::{Map, Source, Value, ValueKind};
66
use mithril_aggregator::{
77
AggregatorRuntime, Config, DependencyManager, MemoryBeaconStore, MultiSigner, MultiSignerImpl,
8-
ProtocolPartyId, ProtocolStake, Server,
8+
ProtocolStakeDistribution, Server,
99
};
1010
use mithril_common::fake_data;
1111
use slog::{Drain, Level, Logger};
@@ -181,14 +181,9 @@ fn init_multi_signer() -> impl MultiSigner {
181181

182182
// Update stake distribution
183183
let total_signers = 5;
184-
let stakes = fake_data::signers_with_stakes(total_signers)
185-
.iter()
186-
.map(|signer| {
187-
(
188-
signer.party_id as ProtocolPartyId,
189-
signer.stake as ProtocolStake,
190-
)
191-
})
184+
let stakes: ProtocolStakeDistribution = fake_data::signers_with_stakes(total_signers)
185+
.into_iter()
186+
.map(|signer| signer.into())
192187
.collect::<_>();
193188
multi_signer
194189
.update_stake_distribution(&stakes)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use super::super::entities;
2+
use super::types;
3+
4+
impl From<types::ProtocolParameters> for entities::ProtocolParameters {
5+
fn from(other: types::ProtocolParameters) -> Self {
6+
entities::ProtocolParameters::new(other.k, other.m, other.phi_f as f32)
7+
}
8+
}
9+
10+
impl From<entities::ProtocolParameters> for types::ProtocolParameters {
11+
fn from(other: entities::ProtocolParameters) -> Self {
12+
types::ProtocolParameters {
13+
k: other.k,
14+
m: other.m,
15+
phi_f: other.phi_f as f64,
16+
}
17+
}
18+
}
19+
20+
impl From<entities::SignerWithStake> for entities::Signer {
21+
fn from(other: entities::SignerWithStake) -> Self {
22+
entities::Signer::new(other.party_id as types::ProtocolPartyId, "".to_string())
23+
}
24+
}
25+
26+
impl From<entities::SignerWithStake> for (types::ProtocolPartyId, types::ProtocolStake) {
27+
fn from(other: entities::SignerWithStake) -> Self {
28+
(
29+
other.party_id as types::ProtocolPartyId,
30+
other.stake as types::ProtocolStake,
31+
)
32+
}
33+
}
34+
35+
impl From<(types::ProtocolPartyId, types::ProtocolStake)> for entities::SignerWithStake {
36+
fn from(other: (types::ProtocolPartyId, types::ProtocolStake)) -> Self {
37+
entities::SignerWithStake::new(other.0, "".to_string(), other.1)
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
pub mod tests {
43+
use super::*;
44+
45+
#[test]
46+
fn test_protocol_parameters_from_into() {
47+
let protocol_parameters_expected = types::ProtocolParameters {
48+
k: 100,
49+
m: 1000,
50+
phi_f: 1.0,
51+
};
52+
let protocol_initializer_entities_expected = entities::ProtocolParameters::new(
53+
protocol_parameters_expected.k,
54+
protocol_parameters_expected.m,
55+
protocol_parameters_expected.phi_f as f32,
56+
);
57+
58+
let protocol_initializer_entities_into: entities::ProtocolParameters =
59+
protocol_parameters_expected.into();
60+
assert_eq!(
61+
protocol_initializer_entities_expected,
62+
protocol_initializer_entities_into
63+
);
64+
65+
let protocol_initializer_from: types::ProtocolParameters =
66+
protocol_initializer_entities_expected.into();
67+
assert_eq!(protocol_parameters_expected, protocol_initializer_from);
68+
}
69+
70+
#[test]
71+
fn test_stake_distribution_from_into() {
72+
let stake_expected = (1 as types::ProtocolPartyId, 100 as types::ProtocolStake);
73+
let signer_with_stake_expected = entities::SignerWithStake::new(1, "".to_string(), 100);
74+
();
75+
76+
let signer_with_stake_expected_into: (types::ProtocolPartyId, types::ProtocolStake) =
77+
signer_with_stake_expected.clone().into();
78+
assert_eq!(stake_expected, signer_with_stake_expected_into);
79+
80+
let stake_expected_from = stake_expected.into();
81+
assert_eq!(signer_with_stake_expected, stake_expected_from);
82+
}
83+
}

mithril-common/src/crypto_helper/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod codec;
2+
mod conversions;
23
pub mod tests_setup;
34
mod types;
45

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use mithril::key_reg::KeyReg;
22
use mithril::stm::{
3-
Index, PartyId, Stake, StmAggrSig, StmClerk, StmInitializer, StmParameters, StmSig, StmSigner,
4-
StmVerificationKeyPoP,
3+
Index, PartyId, Stake, StmAggrSig, StmAggrVerificationKey, StmClerk, StmInitializer,
4+
StmParameters, StmSig, StmSigner, StmVerificationKeyPoP,
55
};
66

7-
use super::super::entities;
8-
97
pub type Bytes = Vec<u8>;
108

119
// Protocol types alias
@@ -22,49 +20,4 @@ pub type ProtocolKeyRegistration = KeyReg;
2220
pub type ProtocolSingleSignature = StmSig<D>;
2321
pub type ProtocolMultiSignature = StmAggrSig<D>;
2422
pub type ProtocolSignerVerificationKey = StmVerificationKeyPoP;
25-
26-
impl From<ProtocolParameters> for entities::ProtocolParameters {
27-
fn from(other: ProtocolParameters) -> Self {
28-
entities::ProtocolParameters::new(other.k, other.m, other.phi_f as f32)
29-
}
30-
}
31-
32-
impl From<entities::ProtocolParameters> for ProtocolParameters {
33-
fn from(other: entities::ProtocolParameters) -> Self {
34-
ProtocolParameters {
35-
k: other.k,
36-
m: other.m,
37-
phi_f: other.phi_f as f64,
38-
}
39-
}
40-
}
41-
42-
#[cfg(test)]
43-
pub mod tests {
44-
use super::*;
45-
46-
#[test]
47-
fn test_protocol_parameters_from_into() {
48-
let protocol_parameters_expected = ProtocolParameters {
49-
k: 100,
50-
m: 1000,
51-
phi_f: 1.0,
52-
};
53-
let protocol_initializer_entities_expected = entities::ProtocolParameters::new(
54-
protocol_parameters_expected.k,
55-
protocol_parameters_expected.m,
56-
protocol_parameters_expected.phi_f as f32,
57-
);
58-
59-
let protocol_initializer_entities_into: entities::ProtocolParameters =
60-
protocol_parameters_expected.into();
61-
assert_eq!(
62-
protocol_initializer_entities_expected,
63-
protocol_initializer_entities_into
64-
);
65-
66-
let protocol_initializer_from: ProtocolParameters =
67-
protocol_initializer_entities_expected.into();
68-
assert_eq!(protocol_parameters_expected, protocol_initializer_from);
69-
}
70-
}
23+
pub type ProtocolAggregateVerificationKey = StmAggrVerificationKey<D>;

0 commit comments

Comments
 (0)