|
| 1 | +//! Bitemporal CRDT sync: version preservation + live-scoped UNIQUE + |
| 2 | +//! receiver-stamped system time. |
| 3 | +//! |
| 4 | +//! The CRDT engine must: |
| 5 | +//! - Scope UNIQUE checks to currently-live rows (`_ts_valid_until == MAX`) |
| 6 | +//! so a superseded version does not falsely collide with a new live row. |
| 7 | +//! - Stamp `_ts_system` with the receiving node's clock on `validate_and_apply`, |
| 8 | +//! ignoring whatever the sender supplied — keeping system-time receiver- |
| 9 | +//! authoritative for convergence. |
| 10 | +
|
| 11 | +use loro::LoroValue; |
| 12 | + |
| 13 | +use nodedb::engine::crdt::tenant_state::TenantCrdtEngine; |
| 14 | +use nodedb::types::TenantId; |
| 15 | +use nodedb_crdt::CrdtAuthContext; |
| 16 | +use nodedb_crdt::constraint::ConstraintSet; |
| 17 | +use nodedb_crdt::policy::CollectionPolicy; |
| 18 | +use nodedb_crdt::validator::ProposedChange; |
| 19 | +use nodedb_crdt::validator::bitemporal::VALID_UNTIL_OPEN; |
| 20 | + |
| 21 | +const USERS: &str = "users"; |
| 22 | + |
| 23 | +fn tenant() -> TenantCrdtEngine { |
| 24 | + let mut cs = ConstraintSet::new(); |
| 25 | + cs.add_unique("users_email_unique", USERS, "email"); |
| 26 | + let mut eng = TenantCrdtEngine::new(TenantId::new(1), 1, cs).unwrap(); |
| 27 | + eng.mark_bitemporal(USERS); |
| 28 | + eng.set_collection_policy_typed(USERS, CollectionPolicy::strict()); |
| 29 | + eng |
| 30 | +} |
| 31 | + |
| 32 | +fn change( |
| 33 | + row_id: &str, |
| 34 | + email: &str, |
| 35 | + valid_from: i64, |
| 36 | + valid_until: i64, |
| 37 | + sender_system_ts: i64, |
| 38 | +) -> ProposedChange { |
| 39 | + ProposedChange { |
| 40 | + collection: USERS.into(), |
| 41 | + row_id: row_id.into(), |
| 42 | + fields: vec![ |
| 43 | + ("name".into(), LoroValue::String("Alice".into())), |
| 44 | + ("email".into(), LoroValue::String(email.into())), |
| 45 | + ("_ts_valid_from".into(), LoroValue::I64(valid_from)), |
| 46 | + ("_ts_valid_until".into(), LoroValue::I64(valid_until)), |
| 47 | + ("_ts_system".into(), LoroValue::I64(sender_system_ts)), |
| 48 | + ], |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// A superseded version with the same email as a new live row should not |
| 53 | +/// trigger a UNIQUE collision: the old version is no longer live. |
| 54 | +#[test] |
| 55 | +fn bitemporal_unique_scoped_to_current() { |
| 56 | + let mut eng = tenant(); |
| 57 | + |
| 58 | + // Version 1: Alice at alice@old.com, terminated at t=1000. |
| 59 | + eng.validate_and_apply( |
| 60 | + 1, |
| 61 | + CrdtAuthContext::default(), |
| 62 | + &change("u1-v1", "alice@old.com", 0, 1_000, 100), |
| 63 | + b"delta1".to_vec(), |
| 64 | + ) |
| 65 | + .expect("v1 should accept"); |
| 66 | + |
| 67 | + // A second user insert at the SAME email (old one) — but the old row is |
| 68 | + // no longer live, so this must succeed, not collide. |
| 69 | + eng.validate_and_apply( |
| 70 | + 1, |
| 71 | + CrdtAuthContext::default(), |
| 72 | + &change("u2-v1", "alice@old.com", 2_000, VALID_UNTIL_OPEN, 200), |
| 73 | + b"delta2".to_vec(), |
| 74 | + ) |
| 75 | + .expect("reusing superseded email should not collide"); |
| 76 | +} |
| 77 | + |
| 78 | +/// Two currently-live rows with the same UNIQUE value MUST collide even in |
| 79 | +/// bitemporal mode — bitemporal relaxes only the old-vs-new axis. |
| 80 | +#[test] |
| 81 | +fn bitemporal_unique_still_collides_for_live_rows() { |
| 82 | + let mut eng = tenant(); |
| 83 | + |
| 84 | + eng.validate_and_apply( |
| 85 | + 1, |
| 86 | + CrdtAuthContext::default(), |
| 87 | + &change("u1", "bob@example.com", 0, VALID_UNTIL_OPEN, 100), |
| 88 | + b"delta1".to_vec(), |
| 89 | + ) |
| 90 | + .expect("first live insert accepted"); |
| 91 | + |
| 92 | + let err = eng.validate_and_apply( |
| 93 | + 1, |
| 94 | + CrdtAuthContext::default(), |
| 95 | + &change("u2", "bob@example.com", 0, VALID_UNTIL_OPEN, 200), |
| 96 | + b"delta2".to_vec(), |
| 97 | + ); |
| 98 | + assert!( |
| 99 | + err.is_err(), |
| 100 | + "two live rows with same UNIQUE value must collide" |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +/// The sender's `_ts_system` must be overwritten with receiver's clock on |
| 105 | +/// apply — receiver-authoritative system time. |
| 106 | +#[test] |
| 107 | +fn bitemporal_crdt_system_ts_receiver_stamped() { |
| 108 | + let mut eng = tenant(); |
| 109 | + |
| 110 | + let sender_ts = 100_000i64; // some arbitrary sender-supplied stamp |
| 111 | + let before_ms = std::time::SystemTime::now() |
| 112 | + .duration_since(std::time::UNIX_EPOCH) |
| 113 | + .unwrap() |
| 114 | + .as_millis() as i64; |
| 115 | + |
| 116 | + eng.validate_and_apply( |
| 117 | + 1, |
| 118 | + CrdtAuthContext::default(), |
| 119 | + &change("u1", "c@example.com", 0, VALID_UNTIL_OPEN, sender_ts), |
| 120 | + b"delta".to_vec(), |
| 121 | + ) |
| 122 | + .unwrap(); |
| 123 | + |
| 124 | + let row = eng.read_row(USERS, "u1").expect("row present"); |
| 125 | + let map = match row { |
| 126 | + LoroValue::Map(m) => m, |
| 127 | + other => panic!("expected map, got {other:?}"), |
| 128 | + }; |
| 129 | + let stamped = match map.get("_ts_system") { |
| 130 | + Some(LoroValue::I64(n)) => *n, |
| 131 | + other => panic!("expected i64 _ts_system, got {other:?}"), |
| 132 | + }; |
| 133 | + assert_ne!( |
| 134 | + stamped, sender_ts, |
| 135 | + "receiver must overwrite sender-supplied _ts_system" |
| 136 | + ); |
| 137 | + assert!( |
| 138 | + stamped >= before_ms, |
| 139 | + "stamped ts ({stamped}) must be >= receiver clock at apply ({before_ms})" |
| 140 | + ); |
| 141 | +} |
0 commit comments