Skip to content

Commit ca3706c

Browse files
committed
refactor: use type aliases for cardano_era, total_spo and total_stake and improve comments
1 parent 57e4b22 commit ca3706c

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

mithril-aggregator/src/services/epoch_service.rs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use thiserror::Error;
1010
use mithril_common::crypto_helper::ProtocolAggregateVerificationKey;
1111
use mithril_common::entities::{
1212
CardanoTransactionsSigningConfig, Epoch, ProtocolParameters, SignedEntityConfig,
13-
SignedEntityTypeDiscriminants, Signer, SignerWithStake,
13+
SignedEntityTypeDiscriminants, Signer, SignerWithStake, Stake,
1414
};
1515
use mithril_common::logging::LoggerExtensions;
1616
use mithril_common::protocol::{MultiSigner as ProtocolMultiSigner, SignerBuilder};
@@ -21,6 +21,9 @@ use crate::{
2121
VerificationKeyStorer,
2222
};
2323

24+
type TotalSPOs = u32;
25+
type CardanoEra = String;
26+
2427
/// Errors dedicated to the CertifierService.
2528
#[derive(Debug, Error)]
2629
pub enum EpochServiceError {
@@ -57,7 +60,7 @@ pub trait EpochService: Sync + Send {
5760
async fn precompute_epoch_data(&mut self) -> StdResult<()>;
5861

5962
/// Get the current Cardano era.
60-
fn cardano_era(&self) -> StdResult<String>;
63+
fn cardano_era(&self) -> StdResult<CardanoEra>;
6164

6265
/// Get the current Mithril era.
6366
fn mithril_era(&self) -> StdResult<SupportedEra>;
@@ -111,15 +114,15 @@ pub trait EpochService: Sync + Send {
111114
/// Get the [SignedEntityConfig] for the current epoch.
112115
fn signed_entity_config(&self) -> StdResult<&SignedEntityConfig>;
113116

114-
/// Get the total number of SPOs for the current epoch.
115-
fn total_spo(&self) -> StdResult<&u32>;
117+
/// Get the total number of SPOs for the current epoch in the Cardano stake distribution.
118+
fn total_spo(&self) -> StdResult<TotalSPOs>;
116119

117-
/// Get the total stake for the current epoch.
118-
fn total_stake(&self) -> StdResult<&u64>;
120+
/// Get the total stake for the current epoch in the Cardano stake distribution.
121+
fn total_stake(&self) -> StdResult<Stake>;
119122
}
120123

121124
struct EpochData {
122-
cardano_era: String,
125+
cardano_era: CardanoEra,
123126
mithril_era: SupportedEra,
124127
epoch: Epoch,
125128
current_epoch_settings: AggregatorEpochSettings,
@@ -130,8 +133,8 @@ struct EpochData {
130133
current_signers: Vec<Signer>,
131134
next_signers: Vec<Signer>,
132135
signed_entity_config: SignedEntityConfig,
133-
total_spo: u32,
134-
total_stake: u64,
136+
total_spo: TotalSPOs,
137+
total_stake: Stake,
135138
}
136139

137140
struct ComputedEpochData {
@@ -209,7 +212,7 @@ impl MithrilEpochService {
209212
}
210213
}
211214

212-
async fn get_cardano_era(&self) -> StdResult<String> {
215+
async fn get_cardano_era(&self) -> StdResult<CardanoEra> {
213216
let cardano_era = self
214217
.chain_observer
215218
.get_current_era()
@@ -219,7 +222,7 @@ impl MithrilEpochService {
219222
Ok(cardano_era)
220223
}
221224

222-
async fn get_total_spo_and_total_stake(&self, epoch: Epoch) -> StdResult<(u32, u64)> {
225+
async fn get_total_spo_and_total_stake(&self, epoch: Epoch) -> StdResult<(TotalSPOs, Stake)> {
223226
let stake_distribution = self
224227
.stake_distribution_service
225228
.get_stake_distribution(epoch)
@@ -229,7 +232,7 @@ impl MithrilEpochService {
229232
})?;
230233

231234
Ok((
232-
stake_distribution.len() as u32,
235+
stake_distribution.len() as TotalSPOs,
233236
stake_distribution.values().sum(),
234237
))
235238
}
@@ -406,7 +409,7 @@ impl EpochService for MithrilEpochService {
406409
Ok(())
407410
}
408411

409-
fn cardano_era(&self) -> StdResult<String> {
412+
fn cardano_era(&self) -> StdResult<CardanoEra> {
410413
Ok(self.unwrap_data()?.cardano_era.clone())
411414
}
412415

@@ -490,12 +493,12 @@ impl EpochService for MithrilEpochService {
490493
Ok(&self.unwrap_data()?.signed_entity_config)
491494
}
492495

493-
fn total_spo(&self) -> StdResult<&u32> {
494-
Ok(&self.unwrap_data()?.total_spo)
496+
fn total_spo(&self) -> StdResult<TotalSPOs> {
497+
Ok(self.unwrap_data()?.total_spo)
495498
}
496499

497-
fn total_stake(&self) -> StdResult<&u64> {
498-
Ok(&self.unwrap_data()?.total_stake)
500+
fn total_stake(&self) -> StdResult<Stake> {
501+
Ok(self.unwrap_data()?.total_stake)
499502
}
500503
}
501504

@@ -510,7 +513,7 @@ pub struct FakeEpochService {
510513

511514
#[cfg(test)]
512515
pub struct FakeEpochServiceBuilder {
513-
pub cardano_era: String,
516+
pub cardano_era: CardanoEra,
514517
pub mithril_era: SupportedEra,
515518
pub epoch: Epoch,
516519
pub current_epoch_settings: AggregatorEpochSettings,
@@ -519,8 +522,8 @@ pub struct FakeEpochServiceBuilder {
519522
pub current_signers_with_stake: Vec<SignerWithStake>,
520523
pub next_signers_with_stake: Vec<SignerWithStake>,
521524
pub signed_entity_config: SignedEntityConfig,
522-
pub total_spo: u32,
523-
pub total_stake: u64,
525+
pub total_spo: TotalSPOs,
526+
pub total_stake: Stake,
524527
}
525528

526529
#[cfg(test)]
@@ -688,7 +691,7 @@ impl EpochService for FakeEpochService {
688691
Ok(())
689692
}
690693

691-
fn cardano_era(&self) -> StdResult<String> {
694+
fn cardano_era(&self) -> StdResult<CardanoEra> {
692695
Ok(self.unwrap_data()?.cardano_era.clone())
693696
}
694697

@@ -772,20 +775,20 @@ impl EpochService for FakeEpochService {
772775
Ok(&self.unwrap_data()?.signed_entity_config)
773776
}
774777

775-
fn total_spo(&self) -> StdResult<&u32> {
776-
Ok(&self.unwrap_data()?.total_spo)
778+
fn total_spo(&self) -> StdResult<u32> {
779+
Ok(self.unwrap_data()?.total_spo)
777780
}
778781

779-
fn total_stake(&self) -> StdResult<&u64> {
780-
Ok(&self.unwrap_data()?.total_stake)
782+
fn total_stake(&self) -> StdResult<u64> {
783+
Ok(self.unwrap_data()?.total_stake)
781784
}
782785
}
783786

784787
#[cfg(test)]
785788
mod tests {
786789
use mithril_common::chain_observer::FakeObserver;
787790
use mithril_common::entities::{
788-
BlockNumber, CardanoTransactionsSigningConfig, PartyId, StakeDistribution,
791+
BlockNumber, CardanoTransactionsSigningConfig, PartyId, Stake, StakeDistribution,
789792
};
790793
use mithril_common::era::SupportedEra;
791794
use mithril_common::test_utils::{
@@ -801,7 +804,10 @@ mod tests {
801804

802805
use super::*;
803806

804-
fn build_uniform_stake_distribution(total_spo: u32, stake_by_spo: u64) -> StakeDistribution {
807+
fn build_uniform_stake_distribution(
808+
total_spo: TotalSPOs,
809+
stake_by_spo: Stake,
810+
) -> StakeDistribution {
805811
let fixture = MithrilFixtureBuilder::default()
806812
.with_signers(total_spo as usize)
807813
.with_stake_distribution(StakeDistributionGenerationMethod::Uniform(stake_by_spo))
@@ -812,7 +818,7 @@ mod tests {
812818

813819
#[derive(Debug, Clone, PartialEq)]
814820
struct ExpectedEpochData {
815-
cardano_era: String,
821+
cardano_era: CardanoEra,
816822
mithril_era: SupportedEra,
817823
epoch: Epoch,
818824
protocol_parameters: ProtocolParameters,
@@ -825,8 +831,8 @@ mod tests {
825831
current_signers: BTreeSet<Signer>,
826832
next_signers: BTreeSet<Signer>,
827833
signed_entity_config: SignedEntityConfig,
828-
total_spo: u32,
829-
total_stake: u64,
834+
total_spo: TotalSPOs,
835+
total_stake: Stake,
830836
}
831837

832838
#[derive(Debug, Clone, PartialEq)]
@@ -865,8 +871,8 @@ mod tests {
865871
current_signers: service.current_signers()?.clone().into_iter().collect(),
866872
next_signers: service.next_signers()?.clone().into_iter().collect(),
867873
signed_entity_config: service.signed_entity_config()?.clone(),
868-
total_spo: *service.total_spo()?,
869-
total_stake: *service.total_stake()?,
874+
total_spo: service.total_spo()?,
875+
total_stake: service.total_stake()?,
870876
})
871877
}
872878
}
@@ -895,16 +901,16 @@ mod tests {
895901
future_protocol_parameters: ProtocolParameters,
896902
network: CardanoNetwork,
897903
allowed_discriminants: BTreeSet<SignedEntityTypeDiscriminants>,
898-
cardano_era: String,
904+
cardano_era: CardanoEra,
899905
mithril_era: SupportedEra,
900906
current_epoch: Epoch,
901907
signers_with_stake: Vec<SignerWithStake>,
902908
next_signers_with_stake: Vec<SignerWithStake>,
903909
stored_current_epoch_settings: AggregatorEpochSettings,
904910
stored_next_epoch_settings: AggregatorEpochSettings,
905911
stored_signer_registration_epoch_settings: AggregatorEpochSettings,
906-
total_spo: u32,
907-
total_stake: u64,
912+
total_spo: TotalSPOs,
913+
total_stake: Stake,
908914
}
909915

910916
impl EpochServiceBuilder {

0 commit comments

Comments
 (0)