kona_protocol/batch/
validity.rs

1//! Contains the [`BatchValidity`] and its encodings.
2
3/// Batch Validity
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum BatchValidity {
7    /// The batch is invalid now and in the future, unless we reorg, so it can be discarded.
8    Drop,
9    /// The batch is valid and should be processed
10    Accept,
11    /// We are lacking L1 information until we can proceed batch filtering
12    Undecided,
13    /// The batch may be valid, but cannot be processed yet and should be checked again later
14    Future,
15    /// Introduced in Holocene, a special variant of the `Drop` variant that signals not to flush
16    /// the active batch and channel, in the case of processing an old batch
17    Past,
18}
19
20impl core::fmt::Display for BatchValidity {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        match self {
23            Self::Drop => write!(f, "Drop"),
24            Self::Accept => write!(f, "Accept"),
25            Self::Undecided => write!(f, "Undecided"),
26            Self::Future => write!(f, "Future"),
27            Self::Past => write!(f, "Past"),
28        }
29    }
30}
31
32impl BatchValidity {
33    /// Returns whether the batch is accepted.
34    pub const fn is_accept(&self) -> bool {
35        matches!(self, Self::Accept)
36    }
37
38    /// Returns whether the batch is dropped.
39    pub const fn is_drop(&self) -> bool {
40        matches!(self, Self::Drop)
41    }
42
43    /// Returns whether the batch is outdated.
44    pub const fn is_outdated(&self) -> bool {
45        matches!(self, Self::Past)
46    }
47
48    /// Returns whether the batch is future.
49    pub const fn is_future(&self) -> bool {
50        matches!(self, Self::Future)
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_batch_validity() {
60        assert!(BatchValidity::Accept.is_accept());
61        assert!(BatchValidity::Drop.is_drop());
62        assert!(BatchValidity::Past.is_outdated());
63        assert!(BatchValidity::Future.is_future());
64    }
65}