synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-core / src / revision_state.rs
| 1 | //! Revision state machine (ADR 0003 section 5.3, section 9 step B4). |
| 2 | //! |
| 3 | //! A revision is `Private` (visible only to its owner's delegation chain) or `Draft` (visible |
| 4 | //! within `Read` scope), then `Proposed`, then `Verified` (every claim and required action |
| 5 | //! discharged on its exact tree), then `Canonical` (promoted by quorum, immutable). States |
| 6 | //! never move backwards: replacing a revision creates a new revision and an obsolescence |
| 7 | //! marker, never an earlier state. See `docs/B4_REVISION_STATES.md`. |
| 8 | //! |
| 9 | //! The only moves are the five forward edges of [`RevisionState::may_move_to`]. Which of them |
| 10 | //! a caller may take depends on who asks: the owner chain may move a revision to `Draft` or |
| 11 | //! `Proposed` ([`RevisionState::check_owner_move`]); `Verified` needs claims (step B3) and |
| 12 | //! required-action receipts (step E1) discharged on the exact tree; `Canonical` is reached |
| 13 | //! only through the promotion queue (step E2), so every other move to it is refused with |
| 14 | //! `CanonicalOutsidePromotionQueue`. |
| 15 | |
| 16 | use crate::{RefusalCode, SynapseError, SynapseResult}; |
| 17 | |
| 18 | /// Where a revision stands. The declaration order is the one forward order, so `Ord` |
| 19 | /// compares states by [`RevisionState::rank`]. |
| 20 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 21 | pub enum RevisionState { |
| 22 | /// Visible only to its owner's delegation chain: the keys that submitted it and the keys |
| 23 | /// their grants descend from (need N14). |
| 24 | Private, |
| 25 | /// Visible within `Read` scope. |
| 26 | Draft, |
| 27 | /// Put forward for verification and promotion. |
| 28 | Proposed, |
| 29 | /// Every claim and required action discharged on its exact tree. |
| 30 | Verified, |
| 31 | /// Promoted by quorum through the promotion queue; immutable. |
| 32 | Canonical, |
| 33 | } |
| 34 | |
| 35 | impl RevisionState { |
| 36 | /// Every state, in forward order. |
| 37 | pub const ALL: [Self; 5] = [ |
| 38 | Self::Private, |
| 39 | Self::Draft, |
| 40 | Self::Proposed, |
| 41 | Self::Verified, |
| 42 | Self::Canonical, |
| 43 | ]; |
| 44 | |
| 45 | /// Position in the forward order (`Private` = 0 .. `Canonical` = 4). |
| 46 | pub fn rank(self) -> u8 { |
| 47 | match self { |
| 48 | Self::Private => 0, |
| 49 | Self::Draft => 1, |
| 50 | Self::Proposed => 2, |
| 51 | Self::Verified => 3, |
| 52 | Self::Canonical => 4, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// Binary tag of the state (1..=5); 0 and anything above 5 are not states. |
| 57 | pub fn tag_byte(self) -> u8 { |
| 58 | self.rank() + 1 |
| 59 | } |
| 60 | |
| 61 | /// The state with binary tag `tag`, if any. |
| 62 | pub fn from_tag_byte(tag: u8) -> Option<Self> { |
| 63 | Self::ALL.get(usize::from(tag.checked_sub(1)?)).copied() |
| 64 | } |
| 65 | |
| 66 | /// True for the five edges of the state machine: `Private -> Draft`, |
| 67 | /// `Private -> Proposed`, `Draft -> Proposed`, `Proposed -> Verified`, and |
| 68 | /// `Verified -> Canonical`. Every edge strictly raises the rank; nothing else is a move. |
| 69 | pub fn may_move_to(self, to: Self) -> bool { |
| 70 | matches!( |
| 71 | (self, to), |
| 72 | (Self::Private, Self::Draft) |
| 73 | | (Self::Private, Self::Proposed) |
| 74 | | (Self::Draft, Self::Proposed) |
| 75 | | (Self::Proposed, Self::Verified) |
| 76 | | (Self::Verified, Self::Canonical) |
| 77 | ) |
| 78 | } |
| 79 | |
| 80 | /// The order rule: `RevisionStateMoveInvalid` for any move that is not an edge |
| 81 | /// (backwards, in place, or skipping a state). |
| 82 | pub fn check_move(self, to: Self) -> SynapseResult<()> { |
| 83 | if self.may_move_to(to) { |
| 84 | return Ok(()); |
| 85 | } |
| 86 | let why = if to < self { |
| 87 | "backwards" |
| 88 | } else if to == self { |
| 89 | "in place" |
| 90 | } else { |
| 91 | "skipping a state" |
| 92 | }; |
| 93 | Err(SynapseError::new( |
| 94 | RefusalCode::RevisionStateMoveInvalid, |
| 95 | format!("revision state move {self:?} -> {to:?} is {why}"), |
| 96 | )) |
| 97 | } |
| 98 | |
| 99 | /// A move asked for by the revision's owner chain. A move to `Canonical` is refused |
| 100 | /// with `CanonicalOutsidePromotionQueue` whatever the current state; a move to |
| 101 | /// `Verified` is refused with `RevisionStateMoveInvalid` (it is recorded only when the |
| 102 | /// claims and required actions are discharged, steps B3 and E1); otherwise the order |
| 103 | /// rule of [`RevisionState::check_move`] applies. |
| 104 | pub fn check_owner_move(self, to: Self) -> SynapseResult<()> { |
| 105 | match to { |
| 106 | Self::Canonical => Err(SynapseError::new( |
| 107 | RefusalCode::CanonicalOutsidePromotionQueue, |
| 108 | format!( |
| 109 | "revision state move {self:?} -> Canonical: canonical moves only through the promotion queue" |
| 110 | ), |
| 111 | )), |
| 112 | Self::Verified => Err(SynapseError::new( |
| 113 | RefusalCode::RevisionStateMoveInvalid, |
| 114 | format!( |
| 115 | "revision state move {self:?} -> Verified: needs every claim and required action discharged on the exact tree, which the owner chain cannot assert" |
| 116 | ), |
| 117 | )), |
| 118 | _ => self.check_move(to), |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[cfg(test)] |
| 124 | mod tests { |
| 125 | use super::*; |
| 126 | |
| 127 | /// Seeded SplitMix64 (no new dependency), as in the F1 property tests. |
| 128 | struct SplitMix64(u64); |
| 129 | |
| 130 | impl SplitMix64 { |
| 131 | fn next(&mut self) -> u64 { |
| 132 | self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); |
| 133 | let mut z = self.0; |
| 134 | z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); |
| 135 | z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); |
| 136 | z ^ (z >> 31) |
| 137 | } |
| 138 | |
| 139 | fn state(&mut self) -> RevisionState { |
| 140 | RevisionState::ALL[(self.next() % 5) as usize] |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn b4_state_tags_round_trip_and_unknown_tags_are_not_states() { |
| 146 | for s in RevisionState::ALL { |
| 147 | assert_eq!(RevisionState::from_tag_byte(s.tag_byte()), Some(s)); |
| 148 | } |
| 149 | assert_eq!(RevisionState::from_tag_byte(0), None); |
| 150 | for tag in 6..=u8::MAX { |
| 151 | assert_eq!(RevisionState::from_tag_byte(tag), None); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn b4_every_allowed_move_raises_the_rank_and_every_other_pair_is_refused() { |
| 157 | let mut allowed = 0; |
| 158 | for from in RevisionState::ALL { |
| 159 | for to in RevisionState::ALL { |
| 160 | match from.check_move(to) { |
| 161 | Ok(()) => { |
| 162 | allowed += 1; |
| 163 | assert!(to.rank() > from.rank(), "{from:?} -> {to:?}"); |
| 164 | } |
| 165 | Err(e) => assert_eq!(e.code, RefusalCode::RevisionStateMoveInvalid), |
| 166 | } |
| 167 | if to.rank() <= from.rank() { |
| 168 | assert!(from.check_move(to).is_err(), "{from:?} -> {to:?}"); |
| 169 | assert!(from.check_owner_move(to).is_err(), "{from:?} -> {to:?}"); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | assert_eq!(allowed, 5, "exactly the five edges of section 5.3"); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn b4_owner_moves_never_reach_verified_or_canonical() { |
| 178 | for from in RevisionState::ALL { |
| 179 | assert_eq!( |
| 180 | from.check_owner_move(RevisionState::Canonical) |
| 181 | .unwrap_err() |
| 182 | .code, |
| 183 | RefusalCode::CanonicalOutsidePromotionQueue |
| 184 | ); |
| 185 | assert_eq!( |
| 186 | from.check_owner_move(RevisionState::Verified) |
| 187 | .unwrap_err() |
| 188 | .code, |
| 189 | RefusalCode::RevisionStateMoveInvalid |
| 190 | ); |
| 191 | } |
| 192 | RevisionState::Private |
| 193 | .check_owner_move(RevisionState::Draft) |
| 194 | .unwrap(); |
| 195 | RevisionState::Private |
| 196 | .check_owner_move(RevisionState::Proposed) |
| 197 | .unwrap(); |
| 198 | RevisionState::Draft |
| 199 | .check_owner_move(RevisionState::Proposed) |
| 200 | .unwrap(); |
| 201 | } |
| 202 | |
| 203 | /// Property: over random sequences of attempted moves (any target, owner or order rule), |
| 204 | /// applying only the accepted ones, the rank never decreases and owner moves never reach |
| 205 | /// `Verified` or `Canonical`. |
| 206 | #[test] |
| 207 | fn b4_property_random_move_sequences_never_move_backwards() { |
| 208 | let mut rng = SplitMix64(0xB4_5EED); |
| 209 | for _case in 0..2_000 { |
| 210 | let mut owner_only = rng.next().is_multiple_of(2); |
| 211 | let mut state = if rng.next().is_multiple_of(2) { |
| 212 | RevisionState::Private |
| 213 | } else { |
| 214 | RevisionState::Draft |
| 215 | }; |
| 216 | for _ in 0..32 { |
| 217 | let to = rng.state(); |
| 218 | let before = state; |
| 219 | let result = if owner_only { |
| 220 | state.check_owner_move(to) |
| 221 | } else { |
| 222 | state.check_move(to) |
| 223 | }; |
| 224 | if result.is_ok() { |
| 225 | state = to; |
| 226 | } |
| 227 | assert!(state.rank() >= before.rank(), "{before:?} -> {state:?}"); |
| 228 | if owner_only { |
| 229 | assert!(state <= RevisionState::Proposed, "owner reached {state:?}"); |
| 230 | } |
| 231 | if rng.next().is_multiple_of(8) { |
| 232 | owner_only = !owner_only && state <= RevisionState::Proposed; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |