|
| 1 | +use async_trait::async_trait; |
| 2 | +use mithril_common::{chain_observer::ChainObserver, digesters::ImmutableFile, entities::Beacon}; |
| 3 | +use std::{error::Error, path::PathBuf, sync::Arc}; |
| 4 | +use tokio::sync::RwLock; |
| 5 | + |
| 6 | +use crate::runtime::RuntimeError; |
| 7 | + |
| 8 | +#[async_trait] |
| 9 | +pub trait BeaconProvider |
| 10 | +where |
| 11 | + Self: Sync + Send, |
| 12 | +{ |
| 13 | + async fn get_current_beacon(&self) -> Result<Beacon, Box<dyn Error + Sync + Send>>; |
| 14 | +} |
| 15 | +pub struct BeaconProviderImpl { |
| 16 | + observer: Arc<RwLock<dyn ChainObserver>>, |
| 17 | + db_path: PathBuf, |
| 18 | + network: String, |
| 19 | +} |
| 20 | + |
| 21 | +impl BeaconProviderImpl { |
| 22 | + pub fn new(observer: Arc<RwLock<dyn ChainObserver>>, db_path: PathBuf, network: &str) -> Self { |
| 23 | + let network = network.to_string(); |
| 24 | + |
| 25 | + Self { |
| 26 | + observer, |
| 27 | + db_path, |
| 28 | + network, |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[async_trait] |
| 34 | +impl BeaconProvider for BeaconProviderImpl { |
| 35 | + async fn get_current_beacon(&self) -> Result<Beacon, Box<dyn Error + Sync + Send>> { |
| 36 | + let epoch = self |
| 37 | + .observer |
| 38 | + .read() |
| 39 | + .await |
| 40 | + .get_current_epoch() |
| 41 | + .await? |
| 42 | + .ok_or_else(|| RuntimeError::General("could not get Epoch".to_string().into()))?; |
| 43 | + let immutable_file_number = ImmutableFile::list_completed_in_dir(&self.db_path) |
| 44 | + .map_err(RuntimeError::ImmutableFile)? |
| 45 | + .into_iter() |
| 46 | + .last() |
| 47 | + .ok_or_else(|| { |
| 48 | + RuntimeError::General("no immutable file was returned".to_string().into()) |
| 49 | + })? |
| 50 | + .number; |
| 51 | + let beacon = Beacon { |
| 52 | + network: self.network.clone(), |
| 53 | + epoch, |
| 54 | + immutable_file_number, |
| 55 | + }; |
| 56 | + |
| 57 | + Ok(beacon) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + |
| 65 | + use mithril_common::chain_observer::{ChainObserver, ChainObserverError}; |
| 66 | + use mithril_common::entities::{Epoch, StakeDistribution}; |
| 67 | + |
| 68 | + struct TestChainObserver {} |
| 69 | + |
| 70 | + #[async_trait] |
| 71 | + impl ChainObserver for TestChainObserver { |
| 72 | + async fn get_current_epoch(&self) -> Result<Option<Epoch>, ChainObserverError> { |
| 73 | + Ok(Some(42)) |
| 74 | + } |
| 75 | + |
| 76 | + async fn get_current_stake_distribution( |
| 77 | + &self, |
| 78 | + ) -> Result<Option<StakeDistribution>, ChainObserverError> { |
| 79 | + let stake_distribution: StakeDistribution = [ |
| 80 | + ( |
| 81 | + "pool1qqyjr9pcrv97gwrueunug829fs5znw6p2wxft3fvqkgu5f4qlrg".to_string(), |
| 82 | + 2_493_000 as u64, |
| 83 | + ), |
| 84 | + ( |
| 85 | + "pool1qqfnw2fwajdnam7xsqhhrje5cgd8jcltzfrx655rd23eqlxjfef".to_string(), |
| 86 | + 21_640, |
| 87 | + ), |
| 88 | + ( |
| 89 | + "pool1qqnjh80kudcjphrxftj74x22q3a4uvw8wknlxptgs7gdqtstqad".to_string(), |
| 90 | + 80, |
| 91 | + ), |
| 92 | + ( |
| 93 | + "pool1qquwwu6680fr72y4779r2kpc7mxtch8rp2uhuqcc7v9p6q4f7ph".to_string(), |
| 94 | + 70, |
| 95 | + ), |
| 96 | + ( |
| 97 | + "pool1qptl80vq84xm28pt3t2lhpfzqag28csjhktxz5k6a74n260clmt".to_string(), |
| 98 | + 56, |
| 99 | + ), |
| 100 | + ( |
| 101 | + "pool1qpuckgzxwgdru9vvq3ydmuqa077ur783yn2uywz7zq2c29p506e".to_string(), |
| 102 | + 51_610, |
| 103 | + ), |
| 104 | + ( |
| 105 | + "pool1qz2vzszautc2c8mljnqre2857dpmheq7kgt6vav0s38tvvhxm6w".to_string(), |
| 106 | + 1_051, |
| 107 | + ), |
| 108 | + ] |
| 109 | + .into_iter() |
| 110 | + .collect(); |
| 111 | + |
| 112 | + Ok(Some(stake_distribution)) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #[tokio::test] |
| 117 | + async fn test() { |
| 118 | + let beacon_provider = BeaconProviderImpl::new( |
| 119 | + Arc::new(RwLock::new(TestChainObserver {})), |
| 120 | + PathBuf::new().join("/tmp/db"), |
| 121 | + "whatever", |
| 122 | + ); |
| 123 | + let res = beacon_provider.get_current_beacon().await; |
| 124 | + assert!(res.is_err()); |
| 125 | + } |
| 126 | +} |
0 commit comments