Skip to content

Commit 0dfa192

Browse files
committed
test: adjustments following pr reviews
1 parent 2ce5838 commit 0dfa192

File tree

5 files changed

+149
-46
lines changed

5 files changed

+149
-46
lines changed

mithril-signer/src/database/query/signed_beacon/delete_signed_beacon.rs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,70 @@ mod tests {
4848

4949
use super::*;
5050

51+
// Epoch of the signed entities is irrelevant for those tests, only SignedBeacon.epoch matter
52+
const WHATEVER_EPOCH: Epoch = Epoch(378);
53+
5154
#[test]
52-
fn test_prune_below_epoch_threshold() {
55+
fn test_delete_nothing_if_nothing_strictly_below_epoch_threshold() {
56+
let connection = main_db_connection().unwrap();
57+
insert_signed_beacons(
58+
&connection,
59+
SignedBeaconRecord::fakes(&[(
60+
Epoch(7),
61+
vec![SignedEntityType::MithrilStakeDistribution(WHATEVER_EPOCH)],
62+
)]),
63+
);
64+
65+
let delete_cursor = connection
66+
.fetch(DeleteSignedBeaconRecordQuery::below_epoch_threshold(Epoch(
67+
7,
68+
)))
69+
.unwrap();
70+
assert_eq!(0, delete_cursor.count());
71+
72+
let get_all_cursor = connection.fetch(GetSignedBeaconQuery::all()).unwrap();
73+
assert_eq!(1, get_all_cursor.count());
74+
}
75+
76+
#[test]
77+
fn test_delete_below_epoch_threshold() {
5378
let connection = main_db_connection().unwrap();
5479
insert_signed_beacons(
5580
&connection,
5681
SignedBeaconRecord::fakes(&[
5782
(
5883
Epoch(7),
5984
vec![
60-
SignedEntityType::MithrilStakeDistribution(Epoch(7)),
61-
SignedEntityType::CardanoTransactions(Epoch(7), BlockNumber(12)),
85+
SignedEntityType::MithrilStakeDistribution(WHATEVER_EPOCH),
86+
SignedEntityType::CardanoTransactions(WHATEVER_EPOCH, BlockNumber(12)),
6287
],
6388
),
6489
(
6590
Epoch(8),
6691
vec![
67-
SignedEntityType::MithrilStakeDistribution(Epoch(8)),
68-
SignedEntityType::CardanoStakeDistribution(Epoch(8)),
92+
SignedEntityType::MithrilStakeDistribution(WHATEVER_EPOCH),
93+
SignedEntityType::CardanoStakeDistribution(WHATEVER_EPOCH),
6994
],
7095
),
7196
(
7297
Epoch(9),
7398
vec![
74-
SignedEntityType::MithrilStakeDistribution(Epoch(9)),
75-
SignedEntityType::CardanoStakeDistribution(Epoch(9)),
76-
SignedEntityType::CardanoTransactions(Epoch(9), BlockNumber(23)),
99+
SignedEntityType::MithrilStakeDistribution(WHATEVER_EPOCH),
100+
SignedEntityType::CardanoStakeDistribution(WHATEVER_EPOCH),
101+
SignedEntityType::CardanoTransactions(WHATEVER_EPOCH, BlockNumber(23)),
77102
],
78103
),
79104
]),
80105
);
81106

82-
let cursor = connection
83-
.fetch(DeleteSignedBeaconRecordQuery::below_epoch_threshold(Epoch(
84-
7,
85-
)))
86-
.unwrap();
87-
assert_eq!(0, cursor.count());
88-
89-
let cursor = connection
107+
let delete_cursor = connection
90108
.fetch(DeleteSignedBeaconRecordQuery::below_epoch_threshold(Epoch(
91109
9,
92110
)))
93111
.unwrap();
94-
assert_eq!(4, cursor.count());
112+
assert_eq!(4, delete_cursor.count());
95113

96-
let cursor = connection.fetch(GetSignedBeaconQuery::all()).unwrap();
97-
assert_eq!(3, cursor.count());
114+
let get_all_cursor = connection.fetch(GetSignedBeaconQuery::all()).unwrap();
115+
assert_eq!(3, get_all_cursor.count());
98116
}
99117
}

mithril-signer/src/database/query/signed_beacon/get_signed_beacon.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ mod tests {
146146
let connection = main_db_connection().unwrap();
147147
let records = SignedBeaconRecord::fakes(&[
148148
(
149-
Epoch(3),
149+
Epoch(974),
150150
vec![SignedEntityType::MithrilStakeDistribution(Epoch(3))],
151151
),
152152
(
153-
Epoch(4),
153+
Epoch(975),
154154
vec![SignedEntityType::CardanoStakeDistribution(Epoch(4))],
155155
),
156156
]);
@@ -167,34 +167,34 @@ mod tests {
167167

168168
assert_eq!(
169169
vec![SignedBeaconRecord::fake(
170-
Epoch(3),
170+
Epoch(974),
171171
SignedEntityType::MithrilStakeDistribution(Epoch(3))
172172
)],
173173
stored_records
174174
);
175175
}
176176

177177
#[test]
178-
fn with_multiple_matchings_over_several_epochs() {
178+
fn with_multiple_matching_over_several_epochs() {
179179
let connection = main_db_connection().unwrap();
180180
let records = SignedBeaconRecord::fakes(&[
181181
(
182-
Epoch(3),
182+
Epoch(329),
183183
vec![
184184
SignedEntityType::MithrilStakeDistribution(Epoch(3)),
185185
SignedEntityType::CardanoStakeDistribution(Epoch(3)),
186186
],
187187
),
188188
(
189-
Epoch(4),
189+
Epoch(330),
190190
vec![
191191
SignedEntityType::CardanoStakeDistribution(Epoch(4)),
192192
SignedEntityType::MithrilStakeDistribution(Epoch(4)),
193193
SignedEntityType::CardanoTransactions(Epoch(4), BlockNumber(109)),
194194
],
195195
),
196196
(
197-
Epoch(5),
197+
Epoch(331),
198198
vec![
199199
SignedEntityType::CardanoTransactions(Epoch(5), BlockNumber(124)),
200200
SignedEntityType::CardanoTransactions(Epoch(5), BlockNumber(133)),
@@ -218,26 +218,54 @@ mod tests {
218218
assert_eq!(
219219
SignedBeaconRecord::fakes(&[
220220
(
221-
Epoch(5),
221+
Epoch(331),
222222
vec![SignedEntityType::CardanoTransactions(
223223
Epoch(5),
224224
BlockNumber(133)
225225
),],
226226
),
227227
(
228-
Epoch(4),
228+
Epoch(330),
229229
vec![
230230
SignedEntityType::CardanoTransactions(Epoch(4), BlockNumber(109)),
231231
SignedEntityType::MithrilStakeDistribution(Epoch(4)),
232232
],
233233
),
234234
(
235-
Epoch(3),
235+
Epoch(329),
236236
vec![SignedEntityType::MithrilStakeDistribution(Epoch(3)),],
237237
),
238238
]),
239239
stored_records
240240
);
241241
}
242+
243+
#[test]
244+
fn duplicate_entities_in_parameter_should_not_yield_duplicate_matching() {
245+
let connection = main_db_connection().unwrap();
246+
let records = SignedBeaconRecord::fakes(&[(
247+
Epoch(242),
248+
vec![SignedEntityType::MithrilStakeDistribution(Epoch(3))],
249+
)]);
250+
insert_signed_beacons(&connection, records.clone());
251+
252+
let stored_records: Vec<SignedBeaconRecord> = connection
253+
.fetch_collect(
254+
GetSignedBeaconQuery::by_signed_entities(&[
255+
SignedEntityType::MithrilStakeDistribution(Epoch(3)),
256+
SignedEntityType::MithrilStakeDistribution(Epoch(3)),
257+
])
258+
.unwrap(),
259+
)
260+
.unwrap();
261+
262+
assert_eq!(
263+
vec![SignedBeaconRecord::fake(
264+
Epoch(242),
265+
SignedEntityType::MithrilStakeDistribution(Epoch(3))
266+
)],
267+
stored_records
268+
);
269+
}
242270
}
243271
}

mithril-signer/src/database/record/signed_beacon_record.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,24 @@ mod tests {
148148
signed_at: initiated_at + chrono::TimeDelta::minutes(2),
149149
};
150150

151+
// Check `impl PartialEq<BeaconToSign> for SignedBeaconRecord`
151152
assert_eq!(signed_beacon, beacon_to_sign);
153+
assert_ne!(
154+
signed_beacon,
155+
BeaconToSign {
156+
epoch: beacon_to_sign.epoch + 13,
157+
..beacon_to_sign.clone()
158+
}
159+
);
160+
161+
// Check `impl PartialEq<SignedBeaconRecord> for BeaconToSign`
152162
assert_eq!(beacon_to_sign, signed_beacon);
163+
assert_ne!(
164+
beacon_to_sign,
165+
SignedBeaconRecord {
166+
epoch: signed_beacon.epoch + 11,
167+
..signed_beacon.clone()
168+
}
169+
);
153170
}
154171
}

mithril-signer/src/database/repository/signed_beacon_repository.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,26 @@ mod tests {
233233
.collect(),
234234
);
235235

236-
let to_filter = all_signed_entity_type_for(&time_point);
237236
let available_entities = repository
238-
.filter_out_already_signed_entities(to_filter.clone())
237+
.filter_out_already_signed_entities(vec![
238+
SignedEntityType::MithrilStakeDistribution(time_point.epoch),
239+
SignedEntityType::CardanoStakeDistribution(time_point.epoch),
240+
SignedEntityType::CardanoTransactions(
241+
time_point.epoch,
242+
time_point.chain_point.block_number,
243+
),
244+
SignedEntityType::CardanoStakeDistribution(time_point.epoch + 10),
245+
])
239246
.await
240247
.unwrap();
241248

242-
let expected: Vec<SignedEntityType> = to_filter
243-
.iter()
244-
.filter(|entity| !signed_beacons.contains(entity))
245-
.cloned()
246-
.collect();
247-
assert_eq!(expected, available_entities);
249+
assert_eq!(
250+
vec![
251+
SignedEntityType::CardanoStakeDistribution(time_point.epoch),
252+
SignedEntityType::CardanoStakeDistribution(time_point.epoch + 10),
253+
],
254+
available_entities
255+
);
248256
}
249257

250258
#[tokio::test]

mithril-signer/src/services/certifier.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,13 @@ mod tests {
163163
);
164164

165165
let beacon_to_sign = certifier_service.get_beacon_to_sign().await.unwrap();
166+
let signed_discriminant: Option<SignedEntityTypeDiscriminants> =
167+
beacon_to_sign.map(|b| b.signed_entity_type.into());
166168

167-
assert!(beacon_to_sign.is_some());
169+
assert_eq!(
170+
SignedEntityTypeDiscriminants::all().first().cloned(),
171+
signed_discriminant
172+
);
168173
}
169174

170175
#[tokio::test]
@@ -231,8 +236,7 @@ mod tests {
231236
}
232237

233238
#[tokio::test]
234-
async fn draining_out_all_beacon_to_signs_dont_repeat_value_and_use_signed_entity_discriminant_order(
235-
) {
239+
async fn draining_out_all_beacons_to_sign_use_signed_entity_discriminant_order() {
236240
let ticker_service = DumbTickerService::new(TimePoint::new(1, 14, ChainPoint::dummy()));
237241
let certifier_service = SignerCertifierService::new(
238242
Arc::new(ticker_service),
@@ -249,7 +253,6 @@ mod tests {
249253
.await
250254
.unwrap()
251255
.expect("There should be a beacon to sign since nothing is locked or signed");
252-
let mut all_signed_beacons = vec![previous_beacon_to_sign.clone()];
253256

254257
loop {
255258
certifier_service
@@ -259,25 +262,54 @@ mod tests {
259262
let next_beacon_to_sign = certifier_service.get_beacon_to_sign().await.unwrap();
260263

261264
if let Some(beacon) = next_beacon_to_sign {
262-
assert!(
263-
!all_signed_beacons.contains(&beacon),
264-
"Beacon should not repeat"
265-
);
266265
assert!(
267266
SignedEntityTypeDiscriminants::from(
268267
&previous_beacon_to_sign.signed_entity_type
269268
) < SignedEntityTypeDiscriminants::from(&beacon.signed_entity_type),
270269
"Beacon should follow SignedEntityTypeDiscriminants order"
271270
);
272271

273-
all_signed_beacons.push(beacon.clone());
274272
previous_beacon_to_sign = beacon;
275273
} else {
276274
break;
277275
}
278276
}
279277
}
280278

279+
#[tokio::test]
280+
async fn draining_out_all_beacons_to_sign_doesnt_repeat_value() {
281+
let ticker_service = DumbTickerService::new(TimePoint::new(1, 14, ChainPoint::dummy()));
282+
let certifier_service = SignerCertifierService::new(
283+
Arc::new(ticker_service),
284+
Arc::new(DumbSignedBeaconStore::default()),
285+
Arc::new(DumbSignedEntityConfigProvider::new(
286+
CardanoTransactionsSigningConfig::dummy(),
287+
SignedEntityTypeDiscriminants::all(),
288+
)),
289+
Arc::new(SignedEntityTypeLock::new()),
290+
);
291+
292+
let mut all_signed_beacons = vec![];
293+
while let Some(beacon_to_sign) = certifier_service.get_beacon_to_sign().await.unwrap() {
294+
certifier_service
295+
.mark_beacon_as_signed(&beacon_to_sign)
296+
.await
297+
.unwrap();
298+
all_signed_beacons.push(beacon_to_sign);
299+
}
300+
301+
let mut dedup_signed_beacons = all_signed_beacons.clone();
302+
dedup_signed_beacons.dedup();
303+
assert!(
304+
!all_signed_beacons.is_empty(),
305+
"There should be at least one beacon to sign"
306+
);
307+
assert_eq!(
308+
all_signed_beacons, dedup_signed_beacons,
309+
"Beacon should not repeat"
310+
);
311+
}
312+
281313
pub mod tests_tooling {
282314
use std::collections::BTreeSet;
283315
use tokio::sync::RwLock;

0 commit comments

Comments
 (0)