zebra_state/service/finalized_state/
arbitrary.rs

1//! Arbitrary value generation and test harnesses for the finalized state.
2
3#![allow(dead_code)]
4
5use std::{ops::Deref, sync::Arc};
6
7use crate::service::finalized_state::{
8    disk_format::{FromDisk, IntoDisk},
9    FinalizedState, ZebraDb,
10};
11
12// Enable older test code to automatically access the inner database via Deref coercion.
13impl Deref for FinalizedState {
14    type Target = ZebraDb;
15
16    fn deref(&self) -> &Self::Target {
17        &self.db
18    }
19}
20
21pub fn round_trip<T>(input: T) -> T
22where
23    T: IntoDisk + FromDisk,
24{
25    let bytes = input.as_bytes();
26    T::from_bytes(bytes)
27}
28
29pub fn assert_round_trip<T>(input: T)
30where
31    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
32{
33    let before = input.clone();
34    let after = round_trip(input);
35    assert_eq!(before, after);
36}
37
38pub fn round_trip_ref<T>(input: &T) -> T
39where
40    T: IntoDisk + FromDisk,
41{
42    let bytes = input.as_bytes();
43    T::from_bytes(bytes)
44}
45
46pub fn assert_round_trip_ref<T>(input: &T)
47where
48    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
49{
50    let before = input;
51    let after = round_trip_ref(input);
52    assert_eq!(before, &after);
53}
54
55pub fn round_trip_arc<T>(input: Arc<T>) -> T
56where
57    T: IntoDisk + FromDisk,
58{
59    let bytes = input.as_bytes();
60    T::from_bytes(bytes)
61}
62
63pub fn assert_round_trip_arc<T>(input: Arc<T>)
64where
65    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
66{
67    let before = input.clone();
68    let after = round_trip_arc(input);
69    assert_eq!(*before, after);
70}
71
72/// The round trip test covers types that are used as value field in a rocksdb
73/// column family. Only these types are ever deserialized, and so they're the only
74/// ones that implement both `IntoDisk` and `FromDisk`.
75pub fn assert_value_properties<T>(input: T)
76where
77    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
78{
79    assert_round_trip_ref(&input);
80    assert_round_trip_arc(Arc::new(input.clone()));
81    assert_round_trip(input);
82}