synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-store / tests / common / mod.rs
| 1 | //! Shared fixture of the store's revision tests: real F1 capability links for byte-labelled |
| 2 | //! chains. A state move carries its chain's leaf link and the leaf audience's signature |
| 3 | //! (ADR 0003 B4, issue #89), so the chains here are digests of real signed links, not |
| 4 | //! arbitrary bytes. The link of label path `L` is issued by the key of `L` without its last |
| 5 | //! label (a fixture authority for a root link), names that parent link's digest, and |
| 6 | //! delegates to the key of `L`. Keys and nonces are SHA-256 of the labels, never fixed byte |
| 7 | //! values. |
| 8 | |
| 9 | #![allow(dead_code)] |
| 10 | |
| 11 | use ed25519_dalek::SigningKey; |
| 12 | use sha2::{Digest, Sha256}; |
| 13 | use synapse_auth::{ |
| 14 | Capability, CapabilityChain, CapabilityLink, ChainQuotas, ChangeScope, LinkCaveats, |
| 15 | OperationSet, PathScope, ReferenceScope, SignedStateMove, ROOT_PARENT, |
| 16 | }; |
| 17 | use synapse_core::{ChainPath, ChangesetId, RepoId, RevisionState, SynapseResult, WalSeq}; |
| 18 | use synapse_store::SynapseStore; |
| 19 | |
| 20 | pub fn repo() -> RepoId { |
| 21 | RepoId::new("aien-spark/synapse").unwrap() |
| 22 | } |
| 23 | |
| 24 | /// SHA-256 of a purpose and a label path. |
| 25 | pub fn derived(what: &str, labels: &[u8]) -> [u8; 32] { |
| 26 | let mut h = Sha256::new(); |
| 27 | h.update(b"synapse-store revision fixture\0"); |
| 28 | h.update(what.as_bytes()); |
| 29 | h.update([0]); |
| 30 | h.update(labels); |
| 31 | h.finalize().into() |
| 32 | } |
| 33 | |
| 34 | /// The key presenting the chain with label path `labels`. |
| 35 | pub fn key_of(labels: &[u8]) -> SigningKey { |
| 36 | SigningKey::from_bytes(&derived("key", labels)) |
| 37 | } |
| 38 | |
| 39 | fn caveats() -> LinkCaveats { |
| 40 | LinkCaveats { |
| 41 | operations: OperationSet::from_capabilities(&[Capability::Read, Capability::Propose]), |
| 42 | paths: PathScope::whole_repository(), |
| 43 | references: ReferenceScope::from_kinds(&[]), |
| 44 | changes: ChangeScope::any(), |
| 45 | quotas: ChainQuotas { |
| 46 | max_ops_per_window: 1_000, |
| 47 | window_duration_ms: 60_000, |
| 48 | storage_bytes: 1 << 30, |
| 49 | footprint_paths: 1 << 20, |
| 50 | max_bytes_per_op: 1 << 20, |
| 51 | }, |
| 52 | issued_at_ms: 0, |
| 53 | expires_at_ms: u64::MAX, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// The last link of the chain with label path `labels` (non-empty). |
| 58 | pub fn link_of(labels: &[u8]) -> CapabilityLink { |
| 59 | let (issuer, parent) = match labels.split_last() { |
| 60 | Some((_, [])) | None => ( |
| 61 | SigningKey::from_bytes(&derived("authority", &[])), |
| 62 | ROOT_PARENT, |
| 63 | ), |
| 64 | Some((_, up)) => (key_of(up), link_of(up).digest()), |
| 65 | }; |
| 66 | CapabilityLink::issue( |
| 67 | &issuer, |
| 68 | parent, |
| 69 | derived("nonce", labels), |
| 70 | repo(), |
| 71 | key_of(labels).verifying_key().to_bytes(), |
| 72 | caveats(), |
| 73 | ) |
| 74 | } |
| 75 | |
| 76 | /// The chain with label path `labels`, root first. |
| 77 | pub fn chain_of(labels: &[u8]) -> CapabilityChain { |
| 78 | CapabilityChain::new((1..=labels.len()).map(|n| link_of(&labels[..n])).collect()) |
| 79 | } |
| 80 | |
| 81 | /// Link digests of the chain with label path `labels`: prefixes of a label path are its |
| 82 | /// ancestors, so dominance follows the labels. |
| 83 | pub fn path(labels: &[u8]) -> ChainPath { |
| 84 | ChainPath::new( |
| 85 | chain_of(labels) |
| 86 | .link_digests() |
| 87 | .iter() |
| 88 | .map(|d| d.0) |
| 89 | .collect(), |
| 90 | ) |
| 91 | .unwrap() |
| 92 | } |
| 93 | |
| 94 | /// A move of `id` to `to` signed by the leaf of the chain with label path `labels`. |
| 95 | pub fn signed(labels: &[u8], id: ChangesetId, to: RevisionState, at: u64) -> SignedStateMove { |
| 96 | SignedStateMove::sign_new(&key_of(labels), repo(), id, to, &chain_of(labels), at).unwrap() |
| 97 | } |
| 98 | |
| 99 | /// Record a signed move by the chain with label path `labels` at server time `at`. |
| 100 | pub fn mv( |
| 101 | s: &mut SynapseStore, |
| 102 | labels: &[u8], |
| 103 | id: ChangesetId, |
| 104 | to: RevisionState, |
| 105 | at: u64, |
| 106 | ) -> SynapseResult<WalSeq> { |
| 107 | s.advance_revision_state(signed(labels, id, to, at), at) |
| 108 | } |