Skip to content

Commit 8e3fb4c

Browse files
committed
test: time consistent cases
1 parent 1b7f653 commit 8e3fb4c

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

rust/cardano-chain-follower/src/network.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,84 @@ mod tests {
313313

314314
assert_eq!(network.time_to_slot(time), Some(expected_slot));
315315
}
316+
317+
#[test]
318+
fn test_slot_to_time_to_slot_consistency() {
319+
let network = Network::Mainnet;
320+
321+
// a few arbitrary slots in different ranges.
322+
let slots_to_test = vec![0, 10_000, 1_000_000, 50_000_000];
323+
324+
for slot in slots_to_test {
325+
let time = network.slot_to_time(slot);
326+
let calculated_slot = network.time_to_slot(time);
327+
328+
assert_eq!(calculated_slot, Some(slot), "Failed for slot: {slot}");
329+
}
330+
}
331+
332+
#[test]
333+
#[allow(clippy::panic)]
334+
fn test_time_to_slot_to_time_consistency() {
335+
let network = Network::Mainnet;
336+
let genesis = network.genesis_values();
337+
338+
// Byron, Shelley, and Conway.
339+
let times_to_test = vec![
340+
Utc.timestamp_opt(i64::try_from(genesis.byron_known_time).unwrap() + 100, 0)
341+
.unwrap(),
342+
Utc.timestamp_opt(
343+
i64::try_from(genesis.shelley_known_time).unwrap() + 1_000,
344+
0,
345+
)
346+
.unwrap(),
347+
Utc.timestamp_opt(
348+
i64::try_from(genesis.shelley_known_time).unwrap() + 10_000_000,
349+
0,
350+
)
351+
.unwrap(),
352+
];
353+
354+
for time in times_to_test {
355+
if let Some(slot) = network.time_to_slot(time) {
356+
let calculated_time = network.slot_to_time(slot);
357+
358+
assert_eq!(
359+
calculated_time.timestamp(),
360+
time.timestamp(),
361+
"Failed for time: {time}"
362+
);
363+
} else {
364+
panic!("time_to_slot returned None for a valid time: {time}");
365+
}
366+
}
367+
}
368+
369+
#[test]
370+
fn test_conway_era_time_to_slot_and_back() {
371+
let network = Network::Mainnet;
372+
let genesis = network.genesis_values();
373+
374+
// a very late time, far in the Conway era.
375+
let conway_time = Utc
376+
.timestamp_opt(
377+
i64::try_from(genesis.shelley_known_time).unwrap() + 20_000_000,
378+
0,
379+
)
380+
.unwrap();
381+
382+
let slot = network.time_to_slot(conway_time);
383+
assert!(
384+
slot.is_some(),
385+
"Failed to calculate slot for Conway era time"
386+
);
387+
388+
let calculated_time = network.slot_to_time(slot.unwrap());
389+
390+
assert_eq!(
391+
calculated_time.timestamp(),
392+
conway_time.timestamp(),
393+
"Inconsistency for Conway era time"
394+
);
395+
}
316396
}

0 commit comments

Comments
 (0)