use std::sync::Arc;
use crate::{MithrilResult, MithrilStakeDistribution, MithrilStakeDistributionListItem};
pub struct MithrilStakeDistributionClient {
aggregator_requester: Arc<dyn MithrilStakeDistributionAggregatorRequest>,
}
#[cfg_attr(test, mockall::automock)]
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
pub trait MithrilStakeDistributionAggregatorRequest: Send + Sync {
async fn list_latest(&self) -> MithrilResult<Vec<MithrilStakeDistributionListItem>>;
async fn get_by_hash(&self, hash: &str) -> MithrilResult<Option<MithrilStakeDistribution>>;
}
impl MithrilStakeDistributionClient {
pub fn new(aggregator_requester: Arc<dyn MithrilStakeDistributionAggregatorRequest>) -> Self {
Self {
aggregator_requester,
}
}
pub async fn list(&self) -> MithrilResult<Vec<MithrilStakeDistributionListItem>> {
self.aggregator_requester.list_latest().await
}
pub async fn get(&self, hash: &str) -> MithrilResult<Option<MithrilStakeDistribution>> {
self.aggregator_requester.get_by_hash(hash).await
}
}
#[cfg(test)]
mod tests {
use mockall::predicate::eq;
use mithril_common::test::double::fake_data;
use mithril_common::test::mock_extensions::MockBuilder;
use crate::MithrilSigner;
use crate::common::test::Dummy;
use super::*;
#[tokio::test]
async fn get_mithril_stake_distribution_list() {
let requester =
MockBuilder::<MockMithrilStakeDistributionAggregatorRequest>::configure(|mock| {
let messages = vec![
MithrilStakeDistributionListItem {
hash: "hash-123".to_string(),
..Dummy::dummy()
},
MithrilStakeDistributionListItem {
hash: "hash-456".to_string(),
..Dummy::dummy()
},
];
mock.expect_list_latest().return_once(move || Ok(messages));
});
let client = MithrilStakeDistributionClient::new(requester);
let items = client.list().await.unwrap();
assert_eq!(2, items.len());
assert_eq!("hash-123".to_string(), items[0].hash);
assert_eq!("hash-456".to_string(), items[1].hash);
}
#[tokio::test]
async fn get_mithril_stake_distribution() {
let requester =
MockBuilder::<MockMithrilStakeDistributionAggregatorRequest>::configure(|mock| {
let message = MithrilStakeDistribution {
hash: "hash".to_string(),
signers_with_stake: MithrilSigner::from_signers(
fake_data::signers_with_stakes(2),
),
..Dummy::dummy()
};
mock.expect_get_by_hash()
.with(eq(message.hash.clone()))
.return_once(move |_| Ok(Some(message)));
});
let client = MithrilStakeDistributionClient::new(requester);
let stake_distribution_entity = client
.get("hash")
.await
.unwrap()
.expect("should return a mithril stake distribution");
assert_eq!("hash", &stake_distribution_entity.hash);
assert_eq!(2, stake_distribution_entity.signers_with_stake.len(),);
}
}