Skip to main content

qualia_core_db/net/peer/replication/
custody.rs

1//! Durable ciphertext custody states (E11.5).
2//!
3//! Length or digest without bytes is never [`CustodyState::Stored`].
4//! [`CustodyState::Delivered`] is not [`CustodyState::ApplicationAcked`].
5
6use crate::net::qdnf::errors::QdnfError;
7
8/// Explicit custody lifecycle. Delivered is not application-acked.
9#[repr(u8)]
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum CustodyState {
12    Accepted = 1,
13    Stored = 2,
14    Delivered = 3,
15    ApplicationAcked = 4,
16}
17
18/// Mark stored only when the bytes themselves are present.
19///
20/// `len_only` or `!bytes_present` → [`QdnfError::Incomplete`], never Stored.
21pub fn mark_stored(len_only: bool, bytes_present: bool) -> Result<CustodyState, QdnfError> {
22    if len_only || !bytes_present {
23        return Err(QdnfError::Incomplete);
24    }
25    Ok(CustodyState::Stored)
26}
27
28/// Accept ciphertext identity (digest/length may be known; bytes not yet).
29#[inline]
30pub fn mark_accepted() -> CustodyState {
31    CustodyState::Accepted
32}
33
34/// Transport delivery. Requires Stored. Does not application-ack.
35pub fn mark_delivered(state: CustodyState) -> Result<CustodyState, QdnfError> {
36    match state {
37        CustodyState::Stored | CustodyState::Delivered => Ok(CustodyState::Delivered),
38        CustodyState::ApplicationAcked => Ok(CustodyState::ApplicationAcked),
39        CustodyState::Accepted => Err(QdnfError::Incomplete),
40    }
41}
42
43/// Application acknowledgement. Requires Delivered. Distinct from Delivered.
44pub fn mark_application_acked(state: CustodyState) -> Result<CustodyState, QdnfError> {
45    match state {
46        CustodyState::Delivered | CustodyState::ApplicationAcked => {
47            Ok(CustodyState::ApplicationAcked)
48        }
49        CustodyState::Accepted | CustodyState::Stored => Err(QdnfError::Incomplete),
50    }
51}
52
53/// Delivered is not application-acknowledged.
54#[inline]
55pub fn delivered_is_application_acked() -> bool {
56    false
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn len_only_is_not_stored() {
65        assert_eq!(mark_stored(true, false), Err(QdnfError::Incomplete));
66        assert_eq!(mark_stored(true, true), Err(QdnfError::Incomplete));
67        assert_eq!(mark_stored(false, false), Err(QdnfError::Incomplete));
68        assert_eq!(mark_stored(false, true).unwrap(), CustodyState::Stored);
69        assert_ne!(mark_accepted(), CustodyState::Stored);
70    }
71
72    #[test]
73    fn delivered_is_not_application_acked() {
74        let stored = mark_stored(false, true).unwrap();
75        let delivered = mark_delivered(stored).unwrap();
76        assert_eq!(delivered, CustodyState::Delivered);
77        assert_ne!(delivered, CustodyState::ApplicationAcked);
78        assert!(!delivered_is_application_acked());
79        assert_eq!(
80            mark_application_acked(CustodyState::Stored),
81            Err(QdnfError::Incomplete)
82        );
83        assert_eq!(
84            mark_application_acked(delivered).unwrap(),
85            CustodyState::ApplicationAcked
86        );
87        assert_eq!(
88            mark_delivered(CustodyState::Accepted),
89            Err(QdnfError::Incomplete)
90        );
91    }
92}