use thiserror::Error;
#[derive(Debug, Clone)]
pub struct WritePayload {
pub id: u64,
pub vector: Vec<f32>,
pub metadata: Vec<u8>,
pub agent_id: [u8; 16],
pub timestamp_ns: u64,
}
impl WritePayload {
pub fn new(id: u64, vector: Vec<f32>) -> Self {
Self {
id,
vector,
metadata: Vec::new(),
agent_id: [0u8; 16],
timestamp_ns: 0,
}
}
pub fn with_metadata(mut self, metadata: Vec<u8>) -> Self {
self.metadata = metadata;
self
}
pub fn with_agent(mut self, agent_id: [u8; 16]) -> Self {
self.agent_id = agent_id;
self
}
pub fn with_timestamp(mut self, timestamp_ns: u64) -> Self {
self.timestamp_ns = timestamp_ns;
self
}
pub fn payload_hash(&self) -> [u8; 32] {
use sha2::{Digest, Sha256};
let mut h = Sha256::new();
h.update(self.id.to_le_bytes());
h.update((self.vector.len() as u32).to_le_bytes());
for f in &self.vector {
h.update(f.to_le_bytes());
}
h.update((self.metadata.len() as u32).to_le_bytes());
h.update(&self.metadata);
h.update(self.agent_id);
h.update(self.timestamp_ns.to_le_bytes());
h.finalize().into()
}
pub fn canonical_bytes(&self) -> Vec<u8> {
let vec_len = self.vector.len() * 4;
let mut out = Vec::with_capacity(8 + vec_len + 4 + self.metadata.len() + 16 + 8);
out.extend_from_slice(&self.id.to_le_bytes());
out.extend_from_slice(&(self.vector.len() as u32).to_le_bytes());
for f in &self.vector {
out.extend_from_slice(&f.to_le_bytes());
}
out.extend_from_slice(&(self.metadata.len() as u32).to_le_bytes());
out.extend_from_slice(&self.metadata);
out.extend_from_slice(&self.agent_id);
out.extend_from_slice(&self.timestamp_ns.to_le_bytes());
out
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum GateVariant {
Null = 0,
HashChain = 1,
Merkle = 2,
}
#[derive(Debug, Clone)]
pub struct WriteReceipt {
pub sequence: u64,
pub payload_hash: [u8; 32],
pub chain_commitment: [u8; 32],
pub gate_variant: GateVariant,
}
#[derive(Debug, Error)]
pub enum GateError {
#[error("admission denied: {0}")]
Denied(String),
}