synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / evidence / g2-content-defined-chunking / fixture-generator / src / main.rs
| 1 | //! Writes an A2 segment store (64 KiB segments, blobs of 20 to 40 KB stored whole in the |
| 2 | //! segments, a checkpoint with its version 1 segment index, then frames after it) with |
| 3 | //! the A2 store code, plus `EXPECT` (committed sequence and state root). |
| 4 | |
| 5 | use ed25519_dalek::SigningKey; |
| 6 | use std::path::PathBuf; |
| 7 | use synapse_core::{ |
| 8 | BlobDigest, ChangesetEnvelope, FileMode, GitOid, IdempotencyKey, MutationOp, PathMutation, |
| 9 | RepoId, SYNAPSE_ENVELOPE_VERSION, |
| 10 | }; |
| 11 | use synapse_store::{FsyncPolicy, RecoveryPolicy, StoreLimits, SynapseStore}; |
| 12 | |
| 13 | fn bytes(len: usize, seed: u64) -> Vec<u8> { |
| 14 | let mut x = seed | 1; |
| 15 | (0..len) |
| 16 | .map(|_| { |
| 17 | x ^= x << 13; |
| 18 | x ^= x >> 7; |
| 19 | x ^= x << 17; |
| 20 | b'a' + ((x >> 24) % 26) as u8 |
| 21 | }) |
| 22 | .collect() |
| 23 | } |
| 24 | |
| 25 | fn main() { |
| 26 | let out = PathBuf::from(std::env::args().nth(1).expect("usage: generator <out dir>")); |
| 27 | assert!(!out.exists(), "{} exists", out.display()); |
| 28 | let limits = StoreLimits { |
| 29 | segment_bytes: 64 * 1024, |
| 30 | wal_prealloc_bytes: 16 * 1024, |
| 31 | ..Default::default() |
| 32 | }; |
| 33 | let mut store = SynapseStore::open( |
| 34 | &out, |
| 35 | FsyncPolicy::GROUP_COMMIT_DEFAULT, |
| 36 | RecoveryPolicy::StrictFailClosed, |
| 37 | limits, |
| 38 | ) |
| 39 | .unwrap(); |
| 40 | let key = SigningKey::from_bytes(&[0xA2; 32]); |
| 41 | for i in 0..12u64 { |
| 42 | let files: Vec<(String, Vec<u8>)> = (0..1 + i % 2) |
| 43 | .map(|f| (format!("src/f{i}-{f}.rs"), bytes(20_000 + (i as usize * 1700) % 20_000, 100 * i + f))) |
| 44 | .collect(); |
| 45 | let blobs: Vec<(BlobDigest, Vec<u8>)> = files |
| 46 | .iter() |
| 47 | .map(|(_, c)| (BlobDigest::compute(c), c.clone())) |
| 48 | .collect(); |
| 49 | let mut env = ChangesetEnvelope { |
| 50 | version: SYNAPSE_ENVELOPE_VERSION, |
| 51 | repo_id: RepoId::new("aien-spark/synapse").unwrap(), |
| 52 | base_git_oid: GitOid::compute_blob(b"base"), |
| 53 | parent_changesets: vec![], |
| 54 | author_did: String::new(), |
| 55 | idempotency_key: IdempotencyKey::from_label(&format!("a2-fixture-{i}")), |
| 56 | created_at_ms: 1000 + i, |
| 57 | intent_summary: format!("A2 fixture change {i}"), |
| 58 | mutations: files |
| 59 | .iter() |
| 60 | .map(|(p, c)| PathMutation { |
| 61 | path: p.clone(), |
| 62 | op: MutationOp::Upsert { |
| 63 | mode: FileMode::Regular644, |
| 64 | blob_digest: BlobDigest::compute(c), |
| 65 | size_bytes: c.len() as u64, |
| 66 | }, |
| 67 | }) |
| 68 | .collect(), |
| 69 | evidence_receipts: vec![], |
| 70 | signature: vec![], |
| 71 | }; |
| 72 | env.sign(&key); |
| 73 | store.ingest_changeset(env, &blobs).unwrap(); |
| 74 | if i == 7 { |
| 75 | store.checkpoint_and_compact(1000).unwrap(); |
| 76 | } |
| 77 | } |
| 78 | let root = store.state_root_digest(None).unwrap(); |
| 79 | let seq = store.committed_seq(); |
| 80 | drop(store); |
| 81 | std::fs::write( |
| 82 | out.join("EXPECT"), |
| 83 | format!( |
| 84 | "committed_seq {}\nstate_root {}\nwritten_by A2 commit 6431fcf (synapse-store segment store)\n", |
| 85 | seq.0, |
| 86 | hex::encode(root) |
| 87 | ), |
| 88 | ) |
| 89 | .unwrap(); |
| 90 | } |