Skip to content

Commit 1a077a0

Browse files
committed
feat: implement 'get_current_chain_point' for FakeChainObserver
1 parent 427a7c0 commit 1a077a0

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

mithril-aggregator/src/tools/mocks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use async_trait::async_trait;
22
use mithril_common::chain_observer::{ChainAddress, ChainObserver, ChainObserverError, TxDatum};
33
use mithril_common::crypto_helper::{KESPeriod, OpCert};
4-
use mithril_common::entities::{Epoch, StakeDistribution};
4+
use mithril_common::entities::{ChainPoint, Epoch, StakeDistribution};
55
use mockall::mock;
66

77
mock! {
@@ -16,6 +16,8 @@ mock! {
1616

1717
async fn get_current_epoch(&self) -> Result<Option<Epoch>, ChainObserverError>;
1818

19+
async fn get_current_chain_point(&self) -> Result<Option<ChainPoint>, ChainObserverError>;
20+
1921
async fn get_current_stake_distribution(
2022
&self,
2123
) -> Result<Option<StakeDistribution>, ChainObserverError>;

mithril-common/src/chain_observer/fake_observer.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub struct FakeObserver {
1818
/// [get_current_epoch]: ChainObserver::get_current_epoch
1919
pub current_time_point: RwLock<Option<TimePoint>>,
2020

21+
/// The current chain point
22+
pub current_chain_point: RwLock<Option<ChainPoint>>,
23+
2124
/// A list of [TxDatum], used by [get_current_datums]
2225
///
2326
/// [get_current_datums]: ChainObserver::get_current_datums
@@ -30,6 +33,7 @@ impl FakeObserver {
3033
Self {
3134
signers: RwLock::new(vec![]),
3235
current_time_point: RwLock::new(current_time_point),
36+
current_chain_point: RwLock::new(None),
3337
datums: RwLock::new(vec![]),
3438
}
3539
}
@@ -52,6 +56,13 @@ impl FakeObserver {
5256
*signers = new_signers;
5357
}
5458

59+
/// Set the chain point that will use to compute the result of
60+
/// [get_current_chain_point][ChainObserver::get_current_chain_point].
61+
pub async fn set_current_chain_point(&self, new_current_chain_point: Option<ChainPoint>) {
62+
let mut current_chain_point = self.current_chain_point.write().await;
63+
*current_chain_point = new_current_chain_point;
64+
}
65+
5566
/// Set the datums that will use to compute the result of
5667
/// [get_current_datums][ChainObserver::get_current_datums].
5768
pub async fn set_datums(&self, new_datums: Vec<TxDatum>) {
@@ -88,6 +99,10 @@ impl ChainObserver for FakeObserver {
8899
.map(|time_point| time_point.epoch))
89100
}
90101

102+
async fn get_current_chain_point(&self) -> Result<Option<ChainPoint>, ChainObserverError> {
103+
Ok(self.current_chain_point.read().await.clone())
104+
}
105+
91106
async fn get_current_stake_distribution(
92107
&self,
93108
) -> Result<Option<StakeDistribution>, ChainObserverError> {
@@ -124,6 +139,21 @@ mod tests {
124139
assert_eq!(Some(time_point.epoch), current_epoch);
125140
}
126141

142+
#[tokio::test]
143+
async fn test_get_current_chain_point() {
144+
let fake_observer = FakeObserver::new(None);
145+
fake_observer
146+
.set_current_chain_point(Some(fake_data::chain_point()))
147+
.await;
148+
let chain_point = fake_observer.get_current_chain_point().await.unwrap();
149+
150+
assert_eq!(
151+
Some(fake_data::chain_point()),
152+
chain_point,
153+
"get current chain point should not fail"
154+
);
155+
}
156+
127157
#[tokio::test]
128158
async fn test_get_current_stake_distribution() {
129159
let fake_observer = FakeObserver::new(None);

mithril-common/src/test_utils/fake_data.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ pub fn beacon() -> entities::CardanoDbBeacon {
2828
entities::CardanoDbBeacon::new(network, *time_point.epoch, time_point.immutable_file_number)
2929
}
3030

31+
/// Fake ChainPoint
32+
pub fn chain_point() -> entities::ChainPoint {
33+
entities::ChainPoint {
34+
slot_number: 500,
35+
block_number: 42,
36+
block_hash: "1b69b3202fbe500".to_string(),
37+
}
38+
}
39+
3140
/// Fake Digest
3241
pub fn digest(beacon: &entities::CardanoDbBeacon) -> Vec<u8> {
3342
format!(

mithril-common/src/time_point_provider.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl TimePointProvider for TimePointProviderImpl {
7676
mod tests {
7777
use crate::chain_observer::{ChainAddress, ChainObserver, ChainObserverError, TxDatum};
7878
use crate::digesters::DumbImmutableFileObserver;
79-
use crate::entities::{Epoch, StakeDistribution};
79+
use crate::entities::{ChainPoint, Epoch, StakeDistribution};
8080
use anyhow::anyhow;
8181

8282
use super::*;
@@ -96,6 +96,14 @@ mod tests {
9696
Ok(Some(Epoch(42)))
9797
}
9898

99+
async fn get_current_chain_point(&self) -> Result<Option<ChainPoint>, ChainObserverError> {
100+
Ok(Some(ChainPoint {
101+
slot_number: 500,
102+
block_number: 42,
103+
block_hash: "1b69b3202fbe500".to_string(),
104+
}))
105+
}
106+
99107
async fn get_current_stake_distribution(
100108
&self,
101109
) -> Result<Option<StakeDistribution>, ChainObserverError> {

0 commit comments

Comments
 (0)