Skip to content

Commit 25e0b7a

Browse files
committed
Implement Mithril Stake Distribution Artifact Builder in aggregator
1 parent 2570d1d commit 25e0b7a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use async_trait::async_trait;
2+
use serde::{Deserialize, Serialize};
3+
use std::sync::Arc;
4+
use tokio::sync::RwLock;
5+
6+
use super::ArtifactBuilder;
7+
use crate::MultiSigner;
8+
use mithril_common::{
9+
entities::{Certificate, Epoch, SignerWithStake},
10+
signable_builder::Artifact,
11+
StdResult,
12+
};
13+
14+
/// Mithril Stake Distribution
15+
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
16+
pub struct MithrilStakeDistribution {
17+
signers_with_stake: Vec<SignerWithStake>,
18+
}
19+
20+
impl MithrilStakeDistribution {
21+
/// MithrilStakeDistribution artifact factory
22+
pub fn new(signers_with_stake: Vec<SignerWithStake>) -> Self {
23+
Self { signers_with_stake }
24+
}
25+
}
26+
27+
impl Artifact for MithrilStakeDistribution {}
28+
29+
/// A [MithrilStakeDistributionArtifact] builder
30+
pub struct MithrilStakeDistributionArtifactBuilder {
31+
multi_signer: Arc<RwLock<dyn MultiSigner>>,
32+
}
33+
34+
impl MithrilStakeDistributionArtifactBuilder {
35+
/// MithrilStakeDistribution artifact builder factory
36+
pub fn new(multi_signer: Arc<RwLock<dyn MultiSigner>>) -> Self {
37+
Self { multi_signer }
38+
}
39+
}
40+
41+
#[async_trait]
42+
impl ArtifactBuilder<Epoch, MithrilStakeDistribution> for MithrilStakeDistributionArtifactBuilder {
43+
async fn compute_artifact(
44+
&self,
45+
_beacon: Epoch,
46+
_certificate: &Certificate,
47+
) -> StdResult<MithrilStakeDistribution> {
48+
let multi_signer = self.multi_signer.read().await;
49+
Ok(MithrilStakeDistribution::new(
50+
multi_signer.get_signers_with_stake().await?,
51+
))
52+
}
53+
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use mithril_common::test_utils::fake_data;
58+
59+
use super::*;
60+
61+
use crate::multi_signer::MockMultiSigner;
62+
63+
#[tokio::test]
64+
async fn test_compute_artifact() {
65+
let signers_with_stake = fake_data::signers_with_stakes(5);
66+
let signers_with_stake_clone = signers_with_stake.clone();
67+
let certificate = fake_data::certificate("cert-123".to_string());
68+
let mut mock_multi_signer = MockMultiSigner::new();
69+
mock_multi_signer
70+
.expect_get_signers_with_stake()
71+
.return_once(move || Ok(signers_with_stake_clone));
72+
let mithril_stake_distribution_artifact_builder =
73+
MithrilStakeDistributionArtifactBuilder::new(Arc::new(RwLock::new(mock_multi_signer)));
74+
let artifact = mithril_stake_distribution_artifact_builder
75+
.compute_artifact(Epoch(1), &certificate)
76+
.await
77+
.unwrap();
78+
let artifact_expected = MithrilStakeDistribution::new(signers_with_stake);
79+
assert_eq!(artifact_expected, artifact);
80+
}
81+
}

mithril-aggregator/src/artifact_builder/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
mod artifact_builder_service;
44
mod dummy_artifact;
55
mod interface;
6+
mod mithril_stake_distribution;
67

78
pub use artifact_builder_service::*;
89
pub use dummy_artifact::*;
910
pub use interface::*;
11+
pub use mithril_stake_distribution::*;

0 commit comments

Comments
 (0)