interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / crates / synapse-store / tests / b5_derived_record.rs

103 lines · 3613 bytes Raw Blame History
1//! ADR 0003 step B5 (issue #29): the `DerivedRevisionAdmitted` WAL record (kind 11) round
2//! trips through the binary codec and malformed bytes are refused typed. Admission, replay
3//! verification and recomputation are tested through `synapse-api`
4//! (`tests/b5_derivation_attestation.rs`), which holds the tree store.
5
6use ed25519_dalek::SigningKey;
7use sha2::{Digest, Sha256};
8use synapse_core::{
9 BlobDigest, ChainPath, ChangeId, ChangesetId, DerivationAttestation, DerivationInput,
10 DerivationRule, DerivedRevision, FileMode, MergeTier, MutationOp, PathMutation, RepoId,
11 RevisionContent, TreeDigest,
12};
13use synapse_store::codec::{decode_record, encode_record};
14use synapse_store::WalRecord;
15
16/// 32 bytes derived from a label (no literal key material).
17fn label(s: &str) -> [u8; 32] {
18 Sha256::digest(format!("synapse b5 store test: {s}").as_bytes()).into()
19}
20
21fn derived(rule: DerivationRule, inputs: Vec<DerivationInput>) -> DerivedRevision {
22 let mut change = [0u8; 16];
23 change.copy_from_slice(&label("change")[..16]);
24 let content = RevisionContent {
25 change_id: ChangeId(change),
26 base_tree: TreeDigest(label("base")),
27 parents: vec![],
28 mutations: vec![PathMutation {
29 path: "src/lib.rs".into(),
30 op: MutationOp::Upsert {
31 mode: FileMode::Regular644,
32 blob_digest: BlobDigest::compute(b"x"),
33 size_bytes: 1,
34 },
35 }],
36 intent_summary: "derived".into(),
37 };
38 let attestation = DerivationAttestation::sign(
39 &SigningKey::from_bytes(&label("server")),
40 RepoId::new("aien-spark/synapse").unwrap(),
41 rule,
42 inputs,
43 content.changeset_id(),
44 TreeDigest(label("output")),
45 );
46 DerivedRevision {
47 content,
48 attestation,
49 }
50}
51
52#[test]
53fn b5_derived_revision_records_round_trip_and_malformed_bytes_are_refused_typed() {
54 let a = ChangesetId(label("a"));
55 let b = ChangesetId(label("b"));
56 let records = [
57 derived(
58 DerivationRule::Rebase,
59 vec![
60 DerivationInput::Revision(a),
61 DerivationInput::Tree(TreeDigest(label("onto"))),
62 ],
63 ),
64 derived(
65 DerivationRule::Merge {
66 tier: MergeTier::T0Path,
67 },
68 vec![
69 DerivationInput::Tree(TreeDigest(label("base"))),
70 DerivationInput::Revision(a),
71 DerivationInput::Revision(b),
72 ],
73 ),
74 ]
75 .map(|derived| WalRecord::DerivedRevisionAdmitted {
76 derived,
77 chain: ChainPath::new(vec![label("chain")]).unwrap(),
78 admitted_at_ms: 7,
79 seal: None,
80 });
81 for record in records {
82 assert_eq!(record.kind_byte(), 11);
83 let bytes = encode_record(&record).unwrap();
84 assert_eq!(decode_record(&bytes, 1 << 20).unwrap(), record);
85 for cut in 0..bytes.len() {
86 assert!(decode_record(&bytes[..cut], 1 << 20).is_err(), "cut {cut}");
87 }
88 let mut longer = bytes.clone();
89 longer.push(0);
90 assert!(decode_record(&longer, 1 << 20).is_err());
91 // Flip each byte after the header: refused or decodes to a different record, never a
92 // panic.
93 for i in 2..bytes.len() {
94 let mut b = bytes.clone();
95 b[i] ^= 0x41;
96 if let Ok(r) = decode_record(&b, 1 << 20) {
97 assert_ne!(r, record);
98 }
99 }
100 // No v1 JSON form.
101 assert!(serde_json::to_vec(&record).is_err());
102 }
103}