synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-store / tests / b1_change_identity.rs
| 1 | //! ADR 0003 step B1 acceptance tests (issue #27): change identity bound to its creating |
| 2 | //! chain, admission witnesses, obsolescence markers, envelope v2 with a native base, and v1 |
| 3 | //! identities unchanged. Chains here are digests of real F1 links from the shared fixture |
| 4 | //! (`common`); `synapse-api` tests the same rules end to end through the request path. |
| 5 | |
| 6 | use ed25519_dalek::SigningKey; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | use synapse_core::{ |
| 9 | encode_did_key_ed25519, BlobDigest, ChainPath, ChangeId, ChangesetEnvelope, |
| 10 | ChangesetEnvelopeV2, ChangesetId, FileMode, GitOid, IdempotencyKey, MutationOp, |
| 11 | ObsolescenceMarker, PathMutation, RefusalCode, RepoId, RevisionContent, RevisionState, |
| 12 | TreeDigest, WalSeq, |
| 13 | }; |
| 14 | mod common; |
| 15 | use synapse_store::codec::{decode_record, encode_record}; |
| 16 | use synapse_store::{ |
| 17 | AdmissionAuthority, FsyncPolicy, IngestOutcome, RecordCodec, RecoveryPolicy, RevisionOutcome, |
| 18 | StoreLimits, SynapseStore, WalRecord, |
| 19 | }; |
| 20 | |
| 21 | const STRICT: RecoveryPolicy = RecoveryPolicy::StrictFailClosed; |
| 22 | const CHANGE: ChangeId = ChangeId([0xC1; 16]); |
| 23 | |
| 24 | fn temp_dir(name: &str) -> PathBuf { |
| 25 | let nanos = std::time::SystemTime::now() |
| 26 | .duration_since(std::time::UNIX_EPOCH) |
| 27 | .unwrap() |
| 28 | .as_nanos(); |
| 29 | let dir = |
| 30 | std::env::temp_dir().join(format!("synapse-b1-{name}-{}-{nanos}", std::process::id())); |
| 31 | std::fs::create_dir_all(&dir).unwrap(); |
| 32 | dir |
| 33 | } |
| 34 | |
| 35 | fn limits() -> StoreLimits { |
| 36 | StoreLimits { |
| 37 | wal_prealloc_bytes: 64 * 1024, |
| 38 | ..Default::default() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn open(dir: &Path, policy: FsyncPolicy) -> SynapseStore { |
| 43 | SynapseStore::open(dir, policy, STRICT, limits()).unwrap() |
| 44 | } |
| 45 | |
| 46 | fn repo() -> RepoId { |
| 47 | RepoId::new("aien-spark/synapse").unwrap() |
| 48 | } |
| 49 | |
| 50 | /// The delegation tree of these tests: root authority `R`, orchestrator `O` under it, |
| 51 | /// workers `W1` and `W2` under `O`, and an outsider `X` under `R` beside `O`. |
| 52 | struct Chains { |
| 53 | r: ChainPath, |
| 54 | o: ChainPath, |
| 55 | w1: ChainPath, |
| 56 | w2: ChainPath, |
| 57 | x: ChainPath, |
| 58 | } |
| 59 | |
| 60 | fn chains() -> Chains { |
| 61 | let p = common::path; |
| 62 | Chains { |
| 63 | r: p(&[1]), |
| 64 | o: p(&[1, 2]), |
| 65 | w1: p(&[1, 2, 3]), |
| 66 | w2: p(&[1, 2, 4]), |
| 67 | x: p(&[1, 9]), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn key(n: u8) -> SigningKey { |
| 72 | SigningKey::from_bytes(&[n; 32]) |
| 73 | } |
| 74 | |
| 75 | fn auth(chain: &ChainPath) -> AdmissionAuthority { |
| 76 | AdmissionAuthority { |
| 77 | chain: chain.clone(), |
| 78 | change_scope: Vec::new(), |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// A revision of `CHANGE` writing `body` to `src/lib.rs`, with its blob. |
| 83 | fn revision( |
| 84 | author: &SigningKey, |
| 85 | body: &[u8], |
| 86 | idem: u8, |
| 87 | parents: Vec<ChangesetId>, |
| 88 | ) -> (ChangesetEnvelopeV2, Vec<(BlobDigest, Vec<u8>)>) { |
| 89 | let digest = BlobDigest::compute(body); |
| 90 | let content = RevisionContent { |
| 91 | change_id: CHANGE, |
| 92 | base_tree: TreeDigest([0x7E; 32]), |
| 93 | parents, |
| 94 | mutations: vec![PathMutation { |
| 95 | path: "src/lib.rs".into(), |
| 96 | op: MutationOp::Upsert { |
| 97 | mode: FileMode::Regular644, |
| 98 | blob_digest: digest, |
| 99 | size_bytes: body.len() as u64, |
| 100 | }, |
| 101 | }], |
| 102 | intent_summary: "implement the task".into(), |
| 103 | }; |
| 104 | let env = |
| 105 | ChangesetEnvelopeV2::sign_new(author, repo(), content, IdempotencyKey([idem; 32]), 1_000); |
| 106 | (env, vec![(digest, body.to_vec())]) |
| 107 | } |
| 108 | |
| 109 | fn admitted(outcome: RevisionOutcome) -> (WalSeq, ChangesetId) { |
| 110 | match outcome { |
| 111 | RevisionOutcome::Admitted { |
| 112 | wal_seq, |
| 113 | changeset_id, |
| 114 | .. |
| 115 | } => (wal_seq, changeset_id), |
| 116 | other => panic!("expected an admission, got {other:?}"), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn admit( |
| 121 | s: &mut SynapseStore, |
| 122 | author: &SigningKey, |
| 123 | chain: &ChainPath, |
| 124 | body: &[u8], |
| 125 | idem: u8, |
| 126 | ) -> ChangesetId { |
| 127 | let (env, blobs) = revision(author, body, idem, vec![]); |
| 128 | admitted(s.admit_revision(env, &blobs, auth(chain), 5_000).unwrap()).1 |
| 129 | } |
| 130 | |
| 131 | fn marker(signer: &SigningKey, pred: ChangesetId, succ: Vec<ChangesetId>) -> ObsolescenceMarker { |
| 132 | ObsolescenceMarker::sign_new(signer, repo(), CHANGE, pred, succ, 6_000) |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn b1_rewrite_keeps_change_id_and_gets_new_changeset_id() { |
| 137 | let c = chains(); |
| 138 | let dir = temp_dir("rewrite"); |
| 139 | let mut s = open(&dir, FsyncPolicy::EveryCommit); |
| 140 | let a = admit(&mut s, &key(2), &c.o, b"fn a() {}", 1); |
| 141 | let a2 = admit(&mut s, &key(2), &c.o, b"fn a() { /* rewritten */ }", 2); |
| 142 | assert_ne!(a, a2, "a rewrite gets a new ChangesetId"); |
| 143 | let rev = s.get_revision(&repo(), a2).unwrap(); |
| 144 | assert_eq!(rev.content.change_id, CHANGE, "and keeps its ChangeId"); |
| 145 | assert_eq!( |
| 146 | s.get_revision(&repo(), a).unwrap().content.change_id, |
| 147 | CHANGE |
| 148 | ); |
| 149 | s.record_obsolescence(marker(&key(2), a, vec![a2]), c.o.clone(), 6_000) |
| 150 | .unwrap(); |
| 151 | let st = s.change_status(&repo(), CHANGE).unwrap(); |
| 152 | assert_eq!(st.revisions, vec![a, a2]); |
| 153 | assert_eq!(st.live, vec![a2]); |
| 154 | assert_eq!(st.obsolete, vec![a]); |
| 155 | assert!(!st.divergent); |
| 156 | assert_eq!(st.creating_chain, c.o); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn b1_divergence_two_live_successors_is_detected() { |
| 161 | let c = chains(); |
| 162 | let dir = temp_dir("diverge"); |
| 163 | let mut s = open(&dir, FsyncPolicy::EveryCommit); |
| 164 | let a = admit(&mut s, &key(2), &c.o, b"attempt 0", 1); |
| 165 | let b1 = admit(&mut s, &key(3), &c.w1, b"attempt 1", 1); |
| 166 | let b2 = admit(&mut s, &key(4), &c.w2, b"attempt 2", 1); |
| 167 | s.record_obsolescence(marker(&key(2), a, vec![b1]), c.o.clone(), 6_000) |
| 168 | .unwrap(); |
| 169 | s.record_obsolescence(marker(&key(2), a, vec![b2]), c.o.clone(), 6_001) |
| 170 | .unwrap(); |
| 171 | let st = s.change_status(&repo(), CHANGE).unwrap(); |
| 172 | assert_eq!(st.obsolete, vec![a]); |
| 173 | assert_eq!( |
| 174 | st.live, |
| 175 | vec![b1, b2], |
| 176 | "two live successors of one predecessor" |
| 177 | ); |
| 178 | assert!(st.divergent, "divergence is detected"); |
| 179 | // Selecting one attempt prunes the other: no longer divergent. |
| 180 | s.record_obsolescence(marker(&key(2), b2, vec![]), c.o.clone(), 6_002) |
| 181 | .unwrap(); |
| 182 | let st = s.change_status(&repo(), CHANGE).unwrap(); |
| 183 | assert_eq!(st.live, vec![b1]); |
| 184 | assert!(!st.divergent); |
| 185 | // Divergence survives a reopen (replay) unchanged. |
| 186 | drop(s); |
| 187 | let s = open(&dir, FsyncPolicy::EveryCommit); |
| 188 | assert_eq!(s.change_status(&repo(), CHANGE).unwrap(), st); |
| 189 | } |
| 190 | |
| 191 | fn identical_content_case(policy: FsyncPolicy, name: &str) { |
| 192 | let c = chains(); |
| 193 | let dir = temp_dir(name); |
| 194 | let mut s = open(&dir, policy); |
| 195 | let (env_o, blobs) = revision(&key(2), b"same bytes", 1, vec![]); |
| 196 | let (env_w, _) = revision(&key(3), b"same bytes", 7, vec![]); |
| 197 | let (s1, id) = admitted(s.admit_revision(env_o, &blobs, auth(&c.o), 5_000).unwrap()); |
| 198 | // The second agent provides no blob: it is already visible. |
| 199 | let out = s.admit_revision(env_w, &[], auth(&c.w1), 5_001).unwrap(); |
| 200 | let RevisionOutcome::Admitted { |
| 201 | wal_seq: s2, |
| 202 | changeset_id: id2, |
| 203 | joined_existing_revision: true, |
| 204 | } = out |
| 205 | else { |
| 206 | panic!("expected a join of the existing revision, got {out:?}"); |
| 207 | }; |
| 208 | assert_eq!(id, id2, "identical content is one revision"); |
| 209 | let rev = s.get_revision(&repo(), id).unwrap(); |
| 210 | assert_eq!(rev.attestations.len(), 2, "two attestations"); |
| 211 | assert_eq!(rev.witnesses.len(), 2, "two admission witnesses"); |
| 212 | assert_eq!( |
| 213 | rev.attestations[0].author_key, |
| 214 | key(2).verifying_key().to_bytes() |
| 215 | ); |
| 216 | assert_eq!( |
| 217 | rev.attestations[1].author_key, |
| 218 | key(3).verifying_key().to_bytes() |
| 219 | ); |
| 220 | assert_eq!( |
| 221 | ( |
| 222 | rev.witnesses[0].admission_seq, |
| 223 | rev.witnesses[1].admission_seq |
| 224 | ), |
| 225 | (s1, s2) |
| 226 | ); |
| 227 | assert_eq!( |
| 228 | ( |
| 229 | rev.witnesses[0].chain.clone(), |
| 230 | rev.witnesses[1].chain.clone() |
| 231 | ), |
| 232 | (c.o.clone(), c.w1) |
| 233 | ); |
| 234 | assert_eq!( |
| 235 | rev.witnesses[1].admitted_at_ms, 5_001, |
| 236 | "server time, not the agent's" |
| 237 | ); |
| 238 | assert_ne!(rev.witnesses[0].digest(), rev.witnesses[1].digest()); |
| 239 | assert_eq!( |
| 240 | s.change_status(&repo(), CHANGE).unwrap().revisions, |
| 241 | vec![id] |
| 242 | ); |
| 243 | let root = s.state_root_digest(None).unwrap(); |
| 244 | // Replay, then a checkpoint, then a reopen from the checkpoint rebuild the same ledger. |
| 245 | drop(s); |
| 246 | let mut s = open(&dir, policy); |
| 247 | assert_eq!(s.get_revision(&repo(), id).unwrap(), rev); |
| 248 | assert_eq!(s.state_root_digest(None).unwrap(), root); |
| 249 | s.checkpoint_and_compact(9_000).unwrap(); |
| 250 | drop(s); |
| 251 | let s = open(&dir, policy); |
| 252 | assert_eq!(s.get_revision(&repo(), id).unwrap(), rev); |
| 253 | assert_eq!(s.state_root_digest(None).unwrap(), root); |
| 254 | assert_eq!( |
| 255 | s.get_blob_visible(BlobDigest::compute(b"same bytes"), None, None) |
| 256 | .unwrap(), |
| 257 | b"same bytes" |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn b1_identical_content_two_agents_one_chain_one_change_is_one_revision_two_attestations_two_witnesses( |
| 263 | ) { |
| 264 | identical_content_case(FsyncPolicy::EveryCommit, "identical-wal-log"); |
| 265 | identical_content_case(FsyncPolicy::GROUP_COMMIT_DEFAULT, "identical-segments"); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn b1_join_from_key_outside_creating_chain_without_change_scope_is_refused() { |
| 270 | let c = chains(); |
| 271 | let dir = temp_dir("join"); |
| 272 | let mut s = open(&dir, FsyncPolicy::EveryCommit); |
| 273 | admit(&mut s, &key(2), &c.o, b"created by the orchestrator", 1); |
| 274 | let before = s.committed_seq(); |
| 275 | let (env, blobs) = revision(&key(9), b"outsider attempt", 1, vec![]); |
| 276 | let err = s |
| 277 | .admit_revision(env.clone(), &blobs, auth(&c.x), 5_000) |
| 278 | .unwrap_err(); |
| 279 | assert_eq!(err.code, RefusalCode::AuthChangeOutOfScope); |
| 280 | // A scope naming another change, or the root's parent chain, is not enough either. |
| 281 | let other = AdmissionAuthority { |
| 282 | chain: c.x.clone(), |
| 283 | change_scope: vec![ChangeId([0xEE; 16])], |
| 284 | }; |
| 285 | assert_eq!( |
| 286 | s.admit_revision(env.clone(), &blobs, other, 5_000) |
| 287 | .unwrap_err() |
| 288 | .code, |
| 289 | RefusalCode::AuthChangeOutOfScope |
| 290 | ); |
| 291 | assert_eq!( |
| 292 | s.admit_revision(env.clone(), &blobs, auth(&c.r), 5_000) |
| 293 | .unwrap_err() |
| 294 | .code, |
| 295 | RefusalCode::AuthChangeOutOfScope, |
| 296 | "an ancestor chain does not descend from the creating chain" |
| 297 | ); |
| 298 | assert_eq!(s.committed_seq(), before, "nothing written on refusal"); |
| 299 | // The same key with a grant naming the change in its change scope may join. |
| 300 | let scoped = AdmissionAuthority { |
| 301 | chain: c.x.clone(), |
| 302 | change_scope: vec![CHANGE], |
| 303 | }; |
| 304 | admitted(s.admit_revision(env, &blobs, scoped, 5_000).unwrap()); |
| 305 | // A worker whose chain descends from the creating chain joins without any scope. |
| 306 | admit(&mut s, &key(3), &c.w1, b"worker attempt", 1); |
| 307 | } |
| 308 | |
| 309 | #[test] |
| 310 | fn b1_prune_by_chain_not_dominating_creating_chain_is_refused() { |
| 311 | let c = chains(); |
| 312 | let dir = temp_dir("prune"); |
| 313 | let mut s = open(&dir, FsyncPolicy::EveryCommit); |
| 314 | let a = admit(&mut s, &key(2), &c.o, b"orchestrator attempt", 1); |
| 315 | let b = admit(&mut s, &key(3), &c.w1, b"worker attempt", 1); |
| 316 | // The owner shares it (ADR 0003 B4), so the other chains see it and are refused for |
| 317 | // dominance rather than as an unknown revision. |
| 318 | common::mv(&mut s, &[1, 2], a, RevisionState::Draft, 5_500).unwrap(); |
| 319 | let before = s.committed_seq(); |
| 320 | for (signer, chain) in [(key(3), &c.w1), (key(9), &c.x), (key(4), &c.w2)] { |
| 321 | let err = s |
| 322 | .record_obsolescence(marker(&signer, a, vec![]), chain.clone(), 6_000) |
| 323 | .unwrap_err(); |
| 324 | assert_eq!(err.code, RefusalCode::AuthChainNotDominant, "{chain:?}"); |
| 325 | } |
| 326 | assert_eq!(s.committed_seq(), before, "nothing written on refusal"); |
| 327 | // A worker may rewrite its own attempt (it dominates that revision's first admission) |
| 328 | // but not prune it, and not rewrite the orchestrator's. |
| 329 | let b2 = admit(&mut s, &key(3), &c.w1, b"worker attempt, rewritten", 2); |
| 330 | s.record_obsolescence(marker(&key(3), b, vec![b2]), c.w1.clone(), 6_000) |
| 331 | .unwrap(); |
| 332 | assert_eq!( |
| 333 | s.record_obsolescence(marker(&key(3), b2, vec![]), c.w1.clone(), 6_000) |
| 334 | .unwrap_err() |
| 335 | .code, |
| 336 | RefusalCode::AuthChainNotDominant |
| 337 | ); |
| 338 | assert_eq!( |
| 339 | s.record_obsolescence(marker(&key(3), a, vec![b2]), c.w1.clone(), 6_000) |
| 340 | .unwrap_err() |
| 341 | .code, |
| 342 | RefusalCode::AuthChainNotDominant |
| 343 | ); |
| 344 | // The creating chain and a chain that dominates it may prune. |
| 345 | s.record_obsolescence(marker(&key(2), b2, vec![]), c.o.clone(), 6_001) |
| 346 | .unwrap(); |
| 347 | s.record_obsolescence(marker(&key(1), a, vec![]), c.r.clone(), 6_002) |
| 348 | .unwrap(); |
| 349 | let st = s.change_status(&repo(), CHANGE).unwrap(); |
| 350 | assert!(st.live.is_empty()); |
| 351 | // Markers that would close a cycle are refused. |
| 352 | assert_eq!( |
| 353 | s.record_obsolescence(marker(&key(2), b2, vec![b]), c.o.clone(), 6_003) |
| 354 | .unwrap_err() |
| 355 | .code, |
| 356 | RefusalCode::ObsolescenceMarkerInvalid |
| 357 | ); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn b1_repeat_same_repo_did_idempotency_key_returns_first_wal_seq() { |
| 362 | let c = chains(); |
| 363 | let dir = temp_dir("idem"); |
| 364 | let mut s = open(&dir, FsyncPolicy::EveryCommit); |
| 365 | let (env, blobs) = revision(&key(2), b"first", 5, vec![]); |
| 366 | let (first, id) = admitted( |
| 367 | s.admit_revision(env.clone(), &blobs, auth(&c.o), 5_000) |
| 368 | .unwrap(), |
| 369 | ); |
| 370 | admit(&mut s, &key(3), &c.w1, b"something else", 1); |
| 371 | let dup = RevisionOutcome::IdempotentDuplicate { |
| 372 | wal_seq: first, |
| 373 | changeset_id: id, |
| 374 | }; |
| 375 | let before = s.committed_seq(); |
| 376 | assert_eq!( |
| 377 | s.admit_revision(env.clone(), &blobs, auth(&c.o), 7_000) |
| 378 | .unwrap(), |
| 379 | dup |
| 380 | ); |
| 381 | assert_eq!(s.committed_seq(), before, "a repeat writes nothing"); |
| 382 | // The same key bound to other content is a conflict. |
| 383 | let (other, other_blobs) = revision(&key(2), b"second", 5, vec![]); |
| 384 | assert_eq!( |
| 385 | s.admit_revision(other, &other_blobs, auth(&c.o), 7_000) |
| 386 | .unwrap_err() |
| 387 | .code, |
| 388 | RefusalCode::IdempotencyKeyConflict |
| 389 | ); |
| 390 | // Another DID with the same key and content is its own admission. |
| 391 | let (env_w, _) = revision(&key(3), b"first", 5, vec![]); |
| 392 | let (seq_w, id_w) = admitted(s.admit_revision(env_w, &[], auth(&c.w1), 7_000).unwrap()); |
| 393 | assert_eq!(id_w, id); |
| 394 | assert_ne!(seq_w, first); |
| 395 | // Still the first WalSeq after replay and after a checkpoint. |
| 396 | drop(s); |
| 397 | let mut s = open(&dir, FsyncPolicy::EveryCommit); |
| 398 | assert_eq!( |
| 399 | s.admit_revision(env.clone(), &blobs, auth(&c.o), 8_000) |
| 400 | .unwrap(), |
| 401 | dup |
| 402 | ); |
| 403 | s.checkpoint_and_compact(9_000).unwrap(); |
| 404 | drop(s); |
| 405 | let mut s = open(&dir, FsyncPolicy::EveryCommit); |
| 406 | assert_eq!( |
| 407 | s.admit_revision(env, &blobs, auth(&c.o), 9_000).unwrap(), |
| 408 | dup |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | fn fixture(name: &str) -> PathBuf { |
| 413 | let src = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 414 | .join("tests/fixtures/a1-v1") |
| 415 | .join(name); |
| 416 | let dst = temp_dir(&format!("v1-{name}")); |
| 417 | copy_tree(&src, &dst); |
| 418 | let _ = std::fs::remove_file(dst.join("EXPECT")); |
| 419 | dst |
| 420 | } |
| 421 | |
| 422 | fn copy_tree(src: &Path, dst: &Path) { |
| 423 | std::fs::create_dir_all(dst).unwrap(); |
| 424 | for e in std::fs::read_dir(src).unwrap() { |
| 425 | let e = e.unwrap(); |
| 426 | let to = dst.join(e.file_name()); |
| 427 | if e.file_type().unwrap().is_dir() { |
| 428 | copy_tree(&e.path(), &to); |
| 429 | } else { |
| 430 | std::fs::copy(e.path(), &to).unwrap(); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /// Every v1 changeset of `s` resolves to its record, whose envelope still hashes to it. |
| 436 | fn assert_v1_ids_resolve(s: &SynapseStore, ids: &[ChangesetId]) { |
| 437 | for id in ids { |
| 438 | let cs = s.get_changeset(*id, None, None).unwrap(); |
| 439 | assert_eq!(cs.changeset_id, *id); |
| 440 | assert_eq!(cs.envelope.compute_id(), *id, "v1 identity unchanged"); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | fn v1_case(name: &str, policy: FsyncPolicy) { |
| 445 | let c = chains(); |
| 446 | let dir = fixture(name); |
| 447 | let mut s = open(&dir, policy); |
| 448 | assert_eq!(s.record_codec(), RecordCodec::JsonV1); |
| 449 | let ids: Vec<ChangesetId> = s |
| 450 | .list_changesets(None, None, None) |
| 451 | .unwrap() |
| 452 | .iter() |
| 453 | .map(|cs| cs.changeset_id) |
| 454 | .collect(); |
| 455 | assert!(!ids.is_empty(), "{name}: the fixture holds v1 changesets"); |
| 456 | assert_v1_ids_resolve(&s, &ids); |
| 457 | // Envelope v2 needs the binary codec: refused before the migration checkpoint. |
| 458 | let (env, blobs) = revision(&key(2), b"v2 on v1 history", 1, vec![ids[0]]); |
| 459 | assert_eq!( |
| 460 | s.admit_revision(env.clone(), &blobs, auth(&c.o), 0) |
| 461 | .unwrap_err() |
| 462 | .code, |
| 463 | RefusalCode::EnvelopeVersionUnsupported |
| 464 | ); |
| 465 | s.checkpoint_and_compact(0).unwrap(); |
| 466 | assert_v1_ids_resolve(&s, &ids); |
| 467 | // A v2 revision on top of a v1 parent; its id is in its own domain. |
| 468 | let (_, v2) = admitted(s.admit_revision(env, &blobs, auth(&c.o), 0).unwrap()); |
| 469 | assert!(!ids.contains(&v2)); |
| 470 | assert!( |
| 471 | s.get_changeset(v2, None, None).is_err(), |
| 472 | "v2 ids are not v1 records" |
| 473 | ); |
| 474 | assert_v1_ids_resolve(&s, &ids); |
| 475 | drop(s); |
| 476 | let mut s = open(&dir, policy); |
| 477 | assert_v1_ids_resolve(&s, &ids); |
| 478 | // Compaction keeps the v1 parent of a revision even outside the retention window. |
| 479 | s.checkpoint_and_compact(u64::MAX).unwrap(); |
| 480 | drop(s); |
| 481 | let s = open(&dir, policy); |
| 482 | assert_v1_ids_resolve(&s, &[ids[0]]); |
| 483 | assert_eq!( |
| 484 | s.get_revision(&repo(), v2).unwrap().content.parents, |
| 485 | vec![ids[0]] |
| 486 | ); |
| 487 | } |
| 488 | |
| 489 | /// A v1 changeset far outside the retention window, with no other root, stays because a |
| 490 | /// revision names it as parent; without that root compaction evicts it. |
| 491 | fn v1_parent_outside_retention_case(policy: FsyncPolicy, name: &str) { |
| 492 | let c = chains(); |
| 493 | let dir = temp_dir(name); |
| 494 | let mut s = open(&dir, policy); |
| 495 | let author = key(2); |
| 496 | let mut v1 = |body: &[u8], idem: u8| { |
| 497 | let mut env = ChangesetEnvelope { |
| 498 | version: 1, |
| 499 | repo_id: repo(), |
| 500 | base_git_oid: GitOid([0x0B; 20]), |
| 501 | parent_changesets: vec![], |
| 502 | author_did: encode_did_key_ed25519(&author.verifying_key().to_bytes()), |
| 503 | idempotency_key: IdempotencyKey([idem; 32]), |
| 504 | created_at_ms: 1, |
| 505 | intent_summary: "v1 work".into(), |
| 506 | mutations: vec![PathMutation { |
| 507 | path: "src/v1.rs".into(), |
| 508 | op: MutationOp::Upsert { |
| 509 | mode: FileMode::Regular644, |
| 510 | blob_digest: BlobDigest::compute(body), |
| 511 | size_bytes: body.len() as u64, |
| 512 | }, |
| 513 | }], |
| 514 | evidence_receipts: vec![], |
| 515 | signature: vec![], |
| 516 | }; |
| 517 | env.sign(&author); |
| 518 | let blobs = vec![(BlobDigest::compute(body), body.to_vec())]; |
| 519 | match s.ingest_changeset(env, &blobs).unwrap() { |
| 520 | IngestOutcome::Committed { changeset_id, .. } => changeset_id, |
| 521 | other => panic!("{other:?}"), |
| 522 | } |
| 523 | }; |
| 524 | let named = v1(b"named by a revision", 1); |
| 525 | let unnamed = v1(b"named by nothing", 2); |
| 526 | let (env, blobs) = revision(&key(2), b"on v1", 1, vec![named]); |
| 527 | admitted(s.admit_revision(env, &blobs, auth(&c.o), 0).unwrap()); |
| 528 | s.checkpoint_and_compact(u64::MAX).unwrap(); |
| 529 | assert!( |
| 530 | s.get_changeset(unnamed, None, None).is_err(), |
| 531 | "control: evicted" |
| 532 | ); |
| 533 | assert_v1_ids_resolve(&s, &[named]); |
| 534 | drop(s); |
| 535 | let s = open(&dir, policy); |
| 536 | assert_v1_ids_resolve(&s, &[named]); |
| 537 | assert_eq!( |
| 538 | s.get_blob_visible(BlobDigest::compute(b"named by a revision"), None, None) |
| 539 | .unwrap(), |
| 540 | b"named by a revision" |
| 541 | ); |
| 542 | } |
| 543 | |
| 544 | #[test] |
| 545 | fn b1_every_v1_changeset_id_still_resolves_to_its_record() { |
| 546 | v1_parent_outside_retention_case(FsyncPolicy::EveryCommit, "v1-parent-wal-log"); |
| 547 | v1_parent_outside_retention_case(FsyncPolicy::GROUP_COMMIT_DEFAULT, "v1-parent-segments"); |
| 548 | v1_case("v1-wal-log", FsyncPolicy::EveryCommit); |
| 549 | v1_case("v1-wal-v2-log", FsyncPolicy::GROUP_COMMIT_DEFAULT); |
| 550 | } |
| 551 | |
| 552 | #[test] |
| 553 | fn b1_revision_records_round_trip_and_malformed_bytes_are_refused_typed() { |
| 554 | let c = chains(); |
| 555 | let (env, _) = revision(&key(2), b"x", 1, vec![]); |
| 556 | let records = [ |
| 557 | WalRecord::RevisionAdmitted { |
| 558 | envelope: env.clone(), |
| 559 | chain: c.w1.clone(), |
| 560 | admitted_at_ms: 5, |
| 561 | new_blobs: vec![BlobDigest::compute(b"x")], |
| 562 | seal: None, |
| 563 | }, |
| 564 | WalRecord::ObsolescenceRecorded { |
| 565 | marker: marker(&key(2), env.changeset_id(), vec![ChangesetId([3; 32])]), |
| 566 | chain: c.o.clone(), |
| 567 | recorded_at_ms: 6, |
| 568 | seal: None, |
| 569 | }, |
| 570 | ]; |
| 571 | for record in records { |
| 572 | let bytes = encode_record(&record).unwrap(); |
| 573 | assert_eq!(decode_record(&bytes, 1 << 20).unwrap(), record); |
| 574 | for cut in 0..bytes.len() { |
| 575 | assert!(decode_record(&bytes[..cut], 1 << 20).is_err(), "cut {cut}"); |
| 576 | } |
| 577 | let mut longer = bytes.clone(); |
| 578 | longer.push(0); |
| 579 | assert!(decode_record(&longer, 1 << 20).is_err()); |
| 580 | // Flip each byte of the embedded value: refused or decodes to a different record, |
| 581 | // never a panic. |
| 582 | for i in 2..bytes.len() { |
| 583 | let mut b = bytes.clone(); |
| 584 | b[i] ^= 0x41; |
| 585 | if let Ok(r) = decode_record(&b, 1 << 20) { |
| 586 | assert_ne!(r, record); |
| 587 | } |
| 588 | } |
| 589 | // No v1 JSON form. |
| 590 | assert!(serde_json::to_vec(&record).is_err()); |
| 591 | } |
| 592 | } |