Skip to content

Commit af87018

Browse files
committed
Add chain_point to the TimePoint structure
And update the `TimePointProvider` accordingly.
1 parent fbdc177 commit af87018

File tree

3 files changed

+84
-30
lines changed

3 files changed

+84
-30
lines changed

mithril-common/src/chain_observer/fake_observer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl FakeObserver {
3232
pub fn new(current_time_point: Option<TimePoint>) -> Self {
3333
Self {
3434
signers: RwLock::new(vec![]),
35-
current_time_point: RwLock::new(current_time_point),
36-
current_chain_point: RwLock::new(None),
35+
current_time_point: RwLock::new(current_time_point.clone()),
36+
current_chain_point: RwLock::new(current_time_point.map(|t| t.chain_point)),
3737
datums: RwLock::new(vec![]),
3838
}
3939
}

mithril-common/src/entities/time_point.rs

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
1-
use crate::entities::{Epoch, ImmutableFileNumber};
1+
use crate::entities::{ChainPoint, Epoch, ImmutableFileNumber};
22
use serde::{Deserialize, Serialize};
33
use std::cmp::Ordering;
44
use std::fmt::{Display, Formatter};
55

66
/// TimePoint aggregates all types of point in the Cardano chain and is used by the state machines
77
/// for their computations.
8-
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
8+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
99
pub struct TimePoint {
1010
/// Cardano chain epoch number
1111
pub epoch: Epoch,
1212

1313
/// Number of the last immutable files used for the digest computation
1414
pub immutable_file_number: ImmutableFileNumber,
15+
16+
/// Chain point
17+
pub chain_point: ChainPoint,
1518
}
1619

1720
impl TimePoint {
1821
/// [TimePoint] factory
19-
pub fn new(epoch: u64, immutable_file_number: ImmutableFileNumber) -> TimePoint {
22+
pub fn new(
23+
epoch: u64,
24+
immutable_file_number: ImmutableFileNumber,
25+
chain_point: ChainPoint,
26+
) -> TimePoint {
2027
TimePoint {
2128
epoch: Epoch(epoch),
2229
immutable_file_number,
30+
chain_point,
2331
}
2432
}
2533

2634
cfg_test_tools! {
2735
/// Create a dummy TimePoint
2836
pub fn dummy() -> Self {
29-
Self::new(10, 100)
37+
Self::new(10, 100, ChainPoint::dummy())
3038
}
3139
}
3240
}
@@ -42,6 +50,7 @@ impl Ord for TimePoint {
4250
self.epoch
4351
.cmp(&other.epoch)
4452
.then(self.immutable_file_number.cmp(&other.immutable_file_number))
53+
.then(self.chain_point.cmp(&other.chain_point))
4554
}
4655
}
4756

@@ -57,56 +66,77 @@ impl Display for TimePoint {
5766

5867
#[cfg(test)]
5968
mod tests {
60-
use super::*;
6169
use std::cmp::Ordering;
6270

63-
#[test]
64-
fn time_point_ord_equal() {
65-
let time_point1 = TimePoint {
66-
epoch: Epoch(0),
67-
immutable_file_number: 0,
68-
};
69-
70-
assert_eq!(Ordering::Equal, time_point1.cmp(&time_point1));
71-
}
71+
use super::*;
7272

7373
#[test]
74-
fn time_point_ord_same_epoch_less() {
74+
fn time_point_ord_cmp_epochs_take_precedence_over_other_fields() {
7575
let time_point1 = TimePoint {
76-
epoch: Epoch(0),
76+
epoch: Epoch(5),
7777
immutable_file_number: 0,
78+
chain_point: ChainPoint {
79+
slot_number: 10,
80+
block_number: 20,
81+
block_hash: "hash1".to_string(),
82+
},
7883
};
7984
let time_point2 = TimePoint {
8085
epoch: Epoch(0),
8186
immutable_file_number: 1,
87+
chain_point: ChainPoint {
88+
slot_number: 15,
89+
block_number: 25,
90+
block_hash: "hash2".to_string(),
91+
},
8292
};
8393

84-
assert_eq!(Ordering::Less, time_point1.cmp(&time_point2));
94+
assert_eq!(Ordering::Greater, time_point1.cmp(&time_point2));
8595
}
8696

8797
#[test]
88-
fn time_point_ord_same_epoch_greater() {
98+
fn time_point_ord_cmp_if_epoch_equals_then_immutable_take_precedence_over_chain_point() {
8999
let time_point1 = TimePoint {
90100
epoch: Epoch(0),
91-
immutable_file_number: 1,
101+
immutable_file_number: 5,
102+
chain_point: ChainPoint {
103+
slot_number: 10,
104+
block_number: 20,
105+
block_hash: "hash1".to_string(),
106+
},
92107
};
93108
let time_point2 = TimePoint {
94109
epoch: Epoch(0),
95110
immutable_file_number: 0,
111+
chain_point: ChainPoint {
112+
slot_number: 15,
113+
block_number: 25,
114+
block_hash: "hash2".to_string(),
115+
},
96116
};
97117

98118
assert_eq!(Ordering::Greater, time_point1.cmp(&time_point2));
99119
}
100120

101121
#[test]
102-
fn time_point_ord_cmp_epochs_less() {
122+
fn time_point_ord_cmp_if_epoch_and_immutables_equals_then_compare_over_chain_points() {
103123
let time_point1 = TimePoint {
104124
epoch: Epoch(0),
105-
immutable_file_number: 99,
125+
immutable_file_number: 0,
126+
chain_point: ChainPoint {
127+
slot_number: 10,
128+
block_number: 20,
129+
block_hash: "hash1".to_string(),
130+
},
106131
};
107132
let time_point2 = TimePoint {
108-
epoch: Epoch(1),
109-
immutable_file_number: 99,
133+
epoch: Epoch(0),
134+
immutable_file_number: 0,
135+
chain_point: ChainPoint {
136+
slot_number: 15,
137+
block_number: 25,
138+
block_hash: "hash2".to_string(),
139+
},
110140
};
111141

112142
assert_eq!(Ordering::Less, time_point1.cmp(&time_point2));

mithril-common/src/time_point_provider.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ where
2222
pub enum TimePointProviderError {
2323
/// Raised reading the current epoch succeeded but yield no result.
2424
#[error("No epoch yield by the chain observer, is your cardano node ready ?")]
25-
NoEpoch(),
25+
NoEpoch,
26+
27+
/// Raised reading the current chain point succeeded but yield no result.
28+
#[error("No chain point yield by the chain observer, is your cardano node ready ?")]
29+
NoChainPoint,
2630
}
2731

2832
/// A [TimePointProvider] using a [ChainObserver] and a [ImmutableFileObserver].
@@ -53,7 +57,7 @@ impl TimePointProvider for TimePointProviderImpl {
5357
.await
5458
.map_err(|e| anyhow!(e))
5559
.with_context(|| "TimePoint Provider can not get current epoch")?
56-
.ok_or(TimePointProviderError::NoEpoch())?;
60+
.ok_or(TimePointProviderError::NoEpoch)?;
5761

5862
let immutable_file_number = self
5963
.immutable_observer
@@ -65,9 +69,18 @@ impl TimePointProvider for TimePointProviderImpl {
6569
)
6670
})?;
6771

72+
let chain_point = self
73+
.chain_observer
74+
.get_current_chain_point()
75+
.await
76+
.map_err(|e| anyhow!(e))
77+
.with_context(|| "TimePoint Provider can not get current chain point")?
78+
.ok_or(TimePointProviderError::NoChainPoint)?;
79+
6880
Ok(TimePoint {
6981
epoch,
7082
immutable_file_number,
83+
chain_point,
7184
})
7285
}
7386
}
@@ -98,8 +111,8 @@ mod tests {
98111

99112
async fn get_current_chain_point(&self) -> Result<Option<ChainPoint>, ChainObserverError> {
100113
Ok(Some(ChainPoint {
101-
slot_number: 500,
102-
block_number: 42,
114+
slot_number: 800,
115+
block_number: 51,
103116
block_hash: "1b69b3202fbe500".to_string(),
104117
}))
105118
}
@@ -121,7 +134,18 @@ mod tests {
121134
);
122135
let time_point = time_point_provider.get_current_time_point().await.unwrap();
123136

124-
assert_eq!(TimePoint::new(42, 500), time_point);
137+
assert_eq!(
138+
TimePoint::new(
139+
42,
140+
500,
141+
ChainPoint {
142+
slot_number: 800,
143+
block_number: 51,
144+
block_hash: "1b69b3202fbe500".to_string(),
145+
}
146+
),
147+
time_point
148+
);
125149
}
126150

127151
#[tokio::test]

0 commit comments

Comments
 (0)