1
- use crate :: entities:: { Epoch , ImmutableFileNumber } ;
1
+ use crate :: entities:: { ChainPoint , Epoch , ImmutableFileNumber } ;
2
2
use serde:: { Deserialize , Serialize } ;
3
3
use std:: cmp:: Ordering ;
4
4
use std:: fmt:: { Display , Formatter } ;
5
5
6
6
/// TimePoint aggregates all types of point in the Cardano chain and is used by the state machines
7
7
/// for their computations.
8
- #[ derive( Clone , Debug , PartialEq , Eq , Default , Serialize , Deserialize ) ]
8
+ #[ derive( Clone , Debug , PartialEq , Eq , Serialize , Deserialize ) ]
9
9
pub struct TimePoint {
10
10
/// Cardano chain epoch number
11
11
pub epoch : Epoch ,
12
12
13
13
/// Number of the last immutable files used for the digest computation
14
14
pub immutable_file_number : ImmutableFileNumber ,
15
+
16
+ /// Chain point
17
+ pub chain_point : ChainPoint ,
15
18
}
16
19
17
20
impl TimePoint {
18
21
/// [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 {
20
27
TimePoint {
21
28
epoch : Epoch ( epoch) ,
22
29
immutable_file_number,
30
+ chain_point,
23
31
}
24
32
}
25
33
26
34
cfg_test_tools ! {
27
35
/// Create a dummy TimePoint
28
36
pub fn dummy( ) -> Self {
29
- Self :: new( 10 , 100 )
37
+ Self :: new( 10 , 100 , ChainPoint :: dummy ( ) )
30
38
}
31
39
}
32
40
}
@@ -42,6 +50,7 @@ impl Ord for TimePoint {
42
50
self . epoch
43
51
. cmp ( & other. epoch )
44
52
. then ( self . immutable_file_number . cmp ( & other. immutable_file_number ) )
53
+ . then ( self . chain_point . cmp ( & other. chain_point ) )
45
54
}
46
55
}
47
56
@@ -57,56 +66,77 @@ impl Display for TimePoint {
57
66
58
67
#[ cfg( test) ]
59
68
mod tests {
60
- use super :: * ;
61
69
use std:: cmp:: Ordering ;
62
70
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 :: * ;
72
72
73
73
#[ test]
74
- fn time_point_ord_same_epoch_less ( ) {
74
+ fn time_point_ord_cmp_epochs_take_precedence_over_other_fields ( ) {
75
75
let time_point1 = TimePoint {
76
- epoch : Epoch ( 0 ) ,
76
+ epoch : Epoch ( 5 ) ,
77
77
immutable_file_number : 0 ,
78
+ chain_point : ChainPoint {
79
+ slot_number : 10 ,
80
+ block_number : 20 ,
81
+ block_hash : "hash1" . to_string ( ) ,
82
+ } ,
78
83
} ;
79
84
let time_point2 = TimePoint {
80
85
epoch : Epoch ( 0 ) ,
81
86
immutable_file_number : 1 ,
87
+ chain_point : ChainPoint {
88
+ slot_number : 15 ,
89
+ block_number : 25 ,
90
+ block_hash : "hash2" . to_string ( ) ,
91
+ } ,
82
92
} ;
83
93
84
- assert_eq ! ( Ordering :: Less , time_point1. cmp( & time_point2) ) ;
94
+ assert_eq ! ( Ordering :: Greater , time_point1. cmp( & time_point2) ) ;
85
95
}
86
96
87
97
#[ 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 ( ) {
89
99
let time_point1 = TimePoint {
90
100
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
+ } ,
92
107
} ;
93
108
let time_point2 = TimePoint {
94
109
epoch : Epoch ( 0 ) ,
95
110
immutable_file_number : 0 ,
111
+ chain_point : ChainPoint {
112
+ slot_number : 15 ,
113
+ block_number : 25 ,
114
+ block_hash : "hash2" . to_string ( ) ,
115
+ } ,
96
116
} ;
97
117
98
118
assert_eq ! ( Ordering :: Greater , time_point1. cmp( & time_point2) ) ;
99
119
}
100
120
101
121
#[ 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 ( ) {
103
123
let time_point1 = TimePoint {
104
124
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
+ } ,
106
131
} ;
107
132
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
+ } ,
110
140
} ;
111
141
112
142
assert_eq ! ( Ordering :: Less , time_point1. cmp( & time_point2) ) ;
0 commit comments