diff --git a/modules/enclave-api/src/enclave.rs b/modules/enclave-api/src/enclave.rs index 9d7c18c3..07cf73d6 100644 --- a/modules/enclave-api/src/enclave.rs +++ b/modules/enclave-api/src/enclave.rs @@ -9,7 +9,7 @@ use store::host::{HostStore, IntoCommitStore}; use store::transaction::{CommitStore, CreatedTx, UpdateKey}; /// `Enclave` keeps an enclave id and reference to the host environement -pub struct Enclave { +pub struct Enclave { pub(crate) path: PathBuf, pub(crate) key_manager: EnclaveKeyManager, pub(crate) store: Arc>, @@ -17,7 +17,7 @@ pub struct Enclave { _marker: PhantomData, } -impl Enclave { +impl Enclave { pub fn new( path: impl Into, key_manager: EnclaveKeyManager, @@ -49,7 +49,7 @@ impl Enclave { } /// `EnclaveInfo` is an accessor to enclave information -pub trait EnclaveInfo { +pub trait EnclaveInfo: Sync + Send { /// `get_eid` returns the enclave id fn get_eid(&self) -> sgx_enclave_id_t; /// `metadata` returns the metadata of the enclave @@ -58,7 +58,7 @@ pub trait EnclaveInfo { fn get_key_manager(&self) -> &EnclaveKeyManager; } -impl EnclaveInfo for Enclave { +impl EnclaveInfo for Enclave { /// `get_eid` returns the enclave id fn get_eid(&self) -> sgx_enclave_id_t { self.sgx_enclave.geteid() diff --git a/modules/service/src/service.rs b/modules/service/src/service.rs index 9bba0c1e..5ad80fd1 100644 --- a/modules/service/src/service.rs +++ b/modules/service/src/service.rs @@ -33,19 +33,6 @@ where } } -unsafe impl Send for AppService -where - S: CommitStore + 'static, - E: EnclaveProtoAPI + 'static, -{ -} -unsafe impl Sync for AppService -where - S: CommitStore + 'static, - E: EnclaveProtoAPI + 'static, -{ -} - impl AppService where S: CommitStore + 'static, diff --git a/modules/store/src/memory.rs b/modules/store/src/memory.rs index 82dcaeeb..f6aee602 100644 --- a/modules/store/src/memory.rs +++ b/modules/store/src/memory.rs @@ -3,17 +3,72 @@ use crate::store::TxId; use crate::transaction::{CommitStore, CreatedTx, Tx, TxAccessor}; use crate::{KVStore, Result}; use std::collections::HashMap; +use std::sync::Mutex; // MemStore is only available for testing purposes #[derive(Default, Debug)] -pub struct MemStore { +pub struct MemStore(Mutex); + +impl KVStore for MemStore { + fn get(&self, key: &[u8]) -> Option> { + self.0.lock().unwrap().get(key) + } + + fn set(&mut self, key: Vec, value: Vec) { + self.0.lock().unwrap().set(key, value) + } + + fn remove(&mut self, key: &[u8]) { + self.0.lock().unwrap().remove(key) + } +} + +impl TxAccessor for MemStore { + fn run_in_tx(&self, tx_id: TxId, f: impl FnOnce(&dyn KVStore) -> T) -> Result { + self.0.lock().unwrap().run_in_tx(tx_id, f) + } + + fn run_in_mut_tx( + &mut self, + tx_id: TxId, + f: impl FnOnce(&mut dyn KVStore) -> T, + ) -> Result { + self.0.lock().unwrap().run_in_mut_tx(tx_id, f) + } +} + +impl CommitStore for MemStore { + type Tx = MemTx; + + fn create_transaction( + &mut self, + _update_key: Option, + ) -> Result { + self.0.lock().unwrap().create_transaction(_update_key) + } + + fn begin(&mut self, tx: &::PreparedTx) -> Result<()> { + self.0.lock().unwrap().begin(tx) + } + + fn commit(&mut self, tx: ::PreparedTx) -> Result<()> { + self.0.lock().unwrap().commit(tx) + } + + fn rollback(&mut self, tx: ::PreparedTx) { + self.0.lock().unwrap().rollback(tx) + } +} + +#[derive(Default, Debug)] +pub struct InnerMemStore { running_tx_exists: bool, latest_tx_id: TxId, uncommitted_data: HashMap, Option>>, committed_data: HashMap, Vec>, } -impl KVStore for MemStore { +impl KVStore for InnerMemStore { fn get(&self, key: &[u8]) -> Option> { if self.running_tx_exists { match self.uncommitted_data.get(key) { @@ -42,7 +97,7 @@ impl KVStore for MemStore { } } -impl TxAccessor for MemStore { +impl TxAccessor for InnerMemStore { fn run_in_tx(&self, _tx_id: TxId, f: impl FnOnce(&dyn KVStore) -> T) -> Result { Ok(f(self)) } @@ -56,7 +111,7 @@ impl TxAccessor for MemStore { } } -impl CommitStore for MemStore { +impl CommitStore for InnerMemStore { type Tx = MemTx; fn create_transaction( diff --git a/modules/store/src/transaction.rs b/modules/store/src/transaction.rs index fd50cefb..e882d620 100644 --- a/modules/store/src/transaction.rs +++ b/modules/store/src/transaction.rs @@ -19,7 +19,7 @@ pub trait CreatedTx: Tx { } /// `CommitStore` is a store that supports transactions -pub trait CommitStore { +pub trait CommitStore: Sync + Send { type Tx: CreatedTx; /// `create_transaction` creates a transaction with a given `update_key`