From 0f2cebb1392828b8751ac7f1d9d93f8e03792884 Mon Sep 17 00:00:00 2001 From: SimonThormeyer Date: Fri, 5 Jul 2024 14:21:52 +0200 Subject: [PATCH] feat: implement trait for memory storage from upstream --- memory_storage/src/lib.rs | 57 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/memory_storage/src/lib.rs b/memory_storage/src/lib.rs index dc1efb626..3008178e0 100644 --- a/memory_storage/src/lib.rs +++ b/memory_storage/src/lib.rs @@ -1,4 +1,4 @@ -use openmls_traits::storage::*; +use openmls_traits::key_store::*; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, sync::RwLock}; @@ -14,7 +14,29 @@ pub struct MemoryStorage { values: RwLock, Vec>>, } -impl MemoryStorage { +impl OpenMlsKeyStore for MemoryStorage { + type Error = MemoryStorageError; + + async fn store(&self, k: &[u8], v: &V) -> Result<(), Self::Error> where Self: Sized { + let mut values = self.values.write()?; + values.insert(k.to_vec(), serde_json::to_vec(v)?); + Ok(()) + } + + async fn read(&self, k: &[u8]) -> Option where Self: Sized { + let values = self.values.read()?; + let value = values.get(k)?; + serde_json::from_slice(value).ok() + } + + async fn delete(&self, k: &[u8]) -> Result<(), Self::Error> { + let mut values = self.values.write()?; + values.remove(k); + Ok(()) + } +} + +/*impl MemoryStorage { /// Internal helper to abstract write operations. #[inline(always)] fn write( @@ -168,7 +190,7 @@ impl MemoryStorage { Ok(()) } -} +}*/ /// Errors thrown by the key store. #[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)] @@ -210,7 +232,7 @@ const GROUP_STATE_LABEL: &[u8] = b"GroupState"; const QUEUED_PROPOSAL_LABEL: &[u8] = b"QueuedProposal"; const PROPOSAL_QUEUE_REFS_LABEL: &[u8] = b"ProposalQueueRefs"; -impl StorageProvider for MemoryStorage { +/*impl StorageProvider for MemoryStorage { type Error = MemoryStorageError; fn queue_proposal< @@ -959,32 +981,7 @@ impl StorageProvider for MemoryStorage { let key = serde_json::to_vec(&(group_id, proposal_ref)).unwrap(); self.delete::(QUEUED_PROPOSAL_LABEL, &key) } -} - -/// Build a key with version and label. -fn build_key_from_vec(label: &[u8], key: Vec) -> Vec { - let mut key_out = label.to_vec(); - key_out.extend_from_slice(&key); - key_out.extend_from_slice(&u16::to_be_bytes(V)); - key_out -} - -/// Build a key with version and label. -fn build_key(label: &[u8], key: K) -> Vec { - build_key_from_vec::(label, serde_json::to_vec(&key).unwrap()) -} - -fn epoch_key_pairs_id( - group_id: &impl traits::GroupId, - epoch: &impl traits::EpochKey, - leaf_index: u32, -) -> Result, >::Error> { - let mut key = serde_json::to_vec(group_id)?; - key.extend_from_slice(&serde_json::to_vec(epoch)?); - key.extend_from_slice(&serde_json::to_vec(&leaf_index)?); - Ok(key) -} - +}*/ impl From for MemoryStorageError { fn from(_: serde_json::Error) -> Self { Self::SerializationError