synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-bench / src / synapse_arm.rs
| 1 | //! Synapse client and service helpers: deterministic identities and grants, signed |
| 2 | //! submit and read requests, and the JSON wire round trip used as the transport |
| 3 | //! approximation (there is no socket or daemon in Gate 4). |
| 4 | |
| 5 | use crate::workload::{EditOp, Proposal}; |
| 6 | use ed25519_dalek::SigningKey; |
| 7 | use serde::de::DeserializeOwned; |
| 8 | use serde::Serialize; |
| 9 | use sha2::{Digest, Sha256}; |
| 10 | use std::path::Path; |
| 11 | use synapse_api::{SubmitChangesetPayload, SynapseService}; |
| 12 | use synapse_auth::{AgentCapabilityGrant, AuthenticatedRequest, Capability}; |
| 13 | use synapse_core::{ |
| 14 | encode_did_key_ed25519, BlobDigest, ChangesetEnvelope, ChangesetId, FileMode, GitOid, |
| 15 | IdempotencyKey, MutationOp, PathMutation, RepoId, SYNAPSE_ENVELOPE_VERSION, |
| 16 | }; |
| 17 | use synapse_store::{FsyncPolicy, RecoveryPolicy, StoreLimits}; |
| 18 | |
| 19 | /// Repository identifier used for the representative fixture. |
| 20 | pub const BENCH_REPO_ID: &str = "aien-spark/aien-sovereign-core"; |
| 21 | |
| 22 | /// Grant quotas sized so that quotas never throttle a benchmark run. |
| 23 | pub const BENCH_MAX_BYTES_PER_OP: u64 = 1024 * 1024 * 1024; |
| 24 | pub const BENCH_RATE_WINDOW_MS: u64 = 60_000; |
| 25 | |
| 26 | /// One agent's key and capability grant. |
| 27 | #[derive(Debug, Clone)] |
| 28 | pub struct AgentIdentity { |
| 29 | pub key: SigningKey, |
| 30 | pub did: String, |
| 31 | pub grant: AgentCapabilityGrant, |
| 32 | } |
| 33 | |
| 34 | /// Repository authority plus agent identities. |
| 35 | #[derive(Debug, Clone)] |
| 36 | pub struct BenchIdentities { |
| 37 | pub repo_id: RepoId, |
| 38 | pub authority_key: SigningKey, |
| 39 | pub authority_did: String, |
| 40 | pub agents: Vec<AgentIdentity>, |
| 41 | } |
| 42 | |
| 43 | fn derive_bytes(seed: u64, role: &str, index: u64) -> [u8; 32] { |
| 44 | let mut h = Sha256::new(); |
| 45 | h.update(b"SYNAPSE_GATE4_BENCH_V1\0"); |
| 46 | h.update(seed.to_le_bytes()); |
| 47 | h.update(role.as_bytes()); |
| 48 | h.update(index.to_le_bytes()); |
| 49 | h.finalize().into() |
| 50 | } |
| 51 | |
| 52 | /// Deterministic Ed25519 key for `(seed, role, index)`. |
| 53 | pub fn deterministic_key(seed: u64, role: &str, index: u64) -> SigningKey { |
| 54 | SigningKey::from_bytes(&derive_bytes(seed, role, index)) |
| 55 | } |
| 56 | |
| 57 | /// 32 bytes derived from a label (used for request nonces). |
| 58 | pub fn label_bytes(label: &str) -> [u8; 32] { |
| 59 | let mut h = Sha256::new(); |
| 60 | h.update(b"SYNAPSE_GATE4_BENCH_LABEL_V1\0"); |
| 61 | h.update(label.as_bytes()); |
| 62 | h.finalize().into() |
| 63 | } |
| 64 | |
| 65 | /// Issue `agents` identities with `Read` and `Propose` grants valid around `now_ms`. |
| 66 | pub fn issue_identities(seed: u64, agents: usize, now_ms: u64) -> Result<BenchIdentities, String> { |
| 67 | let repo_id = RepoId::new(BENCH_REPO_ID).map_err(|e| e.to_string())?; |
| 68 | let authority_key = deterministic_key(seed, "authority", 0); |
| 69 | let authority_did = encode_did_key_ed25519(&authority_key.verifying_key().to_bytes()); |
| 70 | let agents = (0..agents) |
| 71 | .map(|i| { |
| 72 | let key = deterministic_key(seed, "agent", i as u64); |
| 73 | let did = encode_did_key_ed25519(&key.verifying_key().to_bytes()); |
| 74 | let grant = AgentCapabilityGrant::issue( |
| 75 | &authority_key, |
| 76 | derive_bytes(seed, "grant", i as u64), |
| 77 | repo_id.clone(), |
| 78 | did.clone(), |
| 79 | vec![Capability::Read, Capability::Propose], |
| 80 | BENCH_MAX_BYTES_PER_OP, |
| 81 | u32::MAX, |
| 82 | BENCH_RATE_WINDOW_MS, |
| 83 | now_ms.saturating_sub(3_600_000), |
| 84 | now_ms + 7 * 24 * 3_600_000, |
| 85 | ); |
| 86 | AgentIdentity { key, did, grant } |
| 87 | }) |
| 88 | .collect(); |
| 89 | Ok(BenchIdentities { |
| 90 | repo_id, |
| 91 | authority_key, |
| 92 | authority_did, |
| 93 | agents, |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | /// Open a `SynapseService` and trust the bench authority. |
| 98 | pub fn open_service( |
| 99 | dir: &Path, |
| 100 | ids: &BenchIdentities, |
| 101 | fsync: FsyncPolicy, |
| 102 | recovery: RecoveryPolicy, |
| 103 | ) -> Result<SynapseService, String> { |
| 104 | let mut svc = SynapseService::open( |
| 105 | dir, |
| 106 | ids.repo_id.clone(), |
| 107 | fsync, |
| 108 | recovery, |
| 109 | StoreLimits::default(), |
| 110 | ) |
| 111 | .map_err(|e| e.to_string())?; |
| 112 | svc.register_authority(ids.authority_did.clone()); |
| 113 | Ok(svc) |
| 114 | } |
| 115 | |
| 116 | /// Register the pinned base commit tree from already-loaded `(path, mode, bytes)` files. |
| 117 | pub fn register_base( |
| 118 | svc: &mut SynapseService, |
| 119 | base_commit_hex: &str, |
| 120 | files: Vec<(String, FileMode, Vec<u8>)>, |
| 121 | ) -> Result<GitOid, String> { |
| 122 | let oid = GitOid::from_hex(base_commit_hex).map_err(|e| e.to_string())?; |
| 123 | svc.register_base_tree(oid, files) |
| 124 | .map_err(|e| e.to_string())?; |
| 125 | Ok(oid) |
| 126 | } |
| 127 | |
| 128 | /// Build the signed envelope and signed `submit` request for `proposal`. |
| 129 | pub fn build_submit_request( |
| 130 | agent: &AgentIdentity, |
| 131 | repo_id: &RepoId, |
| 132 | base_oid: GitOid, |
| 133 | proposal: &Proposal, |
| 134 | label: &str, |
| 135 | now_ms: u64, |
| 136 | ) -> Result<AuthenticatedRequest<SubmitChangesetPayload>, String> { |
| 137 | build_submit_request_with_parents( |
| 138 | agent, |
| 139 | repo_id, |
| 140 | base_oid, |
| 141 | Vec::new(), |
| 142 | proposal, |
| 143 | label, |
| 144 | now_ms, |
| 145 | ) |
| 146 | } |
| 147 | |
| 148 | /// Same as [`build_submit_request`] with explicit (sorted, unique) parent changesets. |
| 149 | pub fn build_submit_request_with_parents( |
| 150 | agent: &AgentIdentity, |
| 151 | repo_id: &RepoId, |
| 152 | base_oid: GitOid, |
| 153 | parent_changesets: Vec<ChangesetId>, |
| 154 | proposal: &Proposal, |
| 155 | label: &str, |
| 156 | now_ms: u64, |
| 157 | ) -> Result<AuthenticatedRequest<SubmitChangesetPayload>, String> { |
| 158 | let mut mutations = Vec::with_capacity(proposal.edits.len()); |
| 159 | let mut blobs: Vec<(BlobDigest, Vec<u8>)> = Vec::new(); |
| 160 | for edit in &proposal.edits { |
| 161 | let op = match &edit.op { |
| 162 | EditOp::Upsert { mode, content } => { |
| 163 | let digest = BlobDigest::compute(content); |
| 164 | if !blobs.iter().any(|(d, _)| *d == digest) { |
| 165 | blobs.push((digest, content.as_ref().clone())); |
| 166 | } |
| 167 | MutationOp::Upsert { |
| 168 | mode: *mode, |
| 169 | blob_digest: digest, |
| 170 | size_bytes: content.len() as u64, |
| 171 | } |
| 172 | } |
| 173 | EditOp::Delete => MutationOp::Delete, |
| 174 | }; |
| 175 | mutations.push(PathMutation { |
| 176 | path: edit.path.clone(), |
| 177 | op, |
| 178 | }); |
| 179 | } |
| 180 | let mut envelope = ChangesetEnvelope { |
| 181 | version: SYNAPSE_ENVELOPE_VERSION, |
| 182 | repo_id: repo_id.clone(), |
| 183 | base_git_oid: base_oid, |
| 184 | parent_changesets, |
| 185 | author_did: agent.did.clone(), |
| 186 | idempotency_key: IdempotencyKey::from_label(label), |
| 187 | created_at_ms: now_ms, |
| 188 | intent_summary: proposal.summary.clone(), |
| 189 | mutations, |
| 190 | evidence_receipts: Vec::new(), |
| 191 | signature: Vec::new(), |
| 192 | }; |
| 193 | envelope.sign(&agent.key); |
| 194 | AuthenticatedRequest::sign_new( |
| 195 | &agent.key, |
| 196 | agent.grant.clone(), |
| 197 | repo_id.clone(), |
| 198 | Capability::Propose, |
| 199 | label_bytes(label), |
| 200 | now_ms, |
| 201 | 0, |
| 202 | SubmitChangesetPayload { envelope, blobs }, |
| 203 | ) |
| 204 | .map_err(|e| e.to_string()) |
| 205 | } |
| 206 | |
| 207 | /// Build a signed `Read` request carrying `payload`. |
| 208 | pub fn build_read_request<T: Serialize>( |
| 209 | agent: &AgentIdentity, |
| 210 | repo_id: &RepoId, |
| 211 | payload: T, |
| 212 | label: &str, |
| 213 | now_ms: u64, |
| 214 | ) -> Result<AuthenticatedRequest<T>, String> { |
| 215 | AuthenticatedRequest::sign_new( |
| 216 | &agent.key, |
| 217 | agent.grant.clone(), |
| 218 | repo_id.clone(), |
| 219 | Capability::Read, |
| 220 | label_bytes(label), |
| 221 | now_ms, |
| 222 | 0, |
| 223 | payload, |
| 224 | ) |
| 225 | .map_err(|e| e.to_string()) |
| 226 | } |
| 227 | |
| 228 | /// Encode `value` as JSON and decode it again (client encode plus server decode). |
| 229 | /// Returns the decoded value and the encoded byte count. |
| 230 | pub fn wire_roundtrip<T: Serialize + DeserializeOwned>(value: &T) -> Result<(T, usize), String> { |
| 231 | let bytes = serde_json::to_vec(value).map_err(|e| format!("wire encode: {e}"))?; |
| 232 | let decoded = serde_json::from_slice(&bytes).map_err(|e| format!("wire decode: {e}"))?; |
| 233 | Ok((decoded, bytes.len())) |
| 234 | } |