Skip to content

Commit

Permalink
service: remove explict Sync and Send markers from AppService
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Dec 5, 2023
1 parent 09cdf36 commit b6594c0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
2 changes: 1 addition & 1 deletion modules/enclave-api/src/api/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use lcp_proto::lcp::service::elc::v1::{
use log::*;
use store::transaction::CommitStore;

pub trait EnclaveProtoAPI<S: CommitStore>: EnclaveCommandAPI<S> {
pub trait EnclaveProtoAPI<S: CommitStore>: EnclaveCommandAPI<S> + Sync + Send {
fn proto_create_client(&self, msg: MsgCreateClient) -> Result<MsgCreateClientResponse> {
let res = self.init_client(msg.try_into()?)?;
info!(
Expand Down
6 changes: 3 additions & 3 deletions modules/enclave-api/src/enclave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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<S> {
pub struct Enclave<S: CommitStore> {
pub(crate) path: PathBuf,
pub(crate) key_manager: EnclaveKeyManager,
pub(crate) store: Arc<RwLock<HostStore>>,
pub(crate) sgx_enclave: SgxEnclave,
_marker: PhantomData<S>,
}

impl<S> Enclave<S> {
impl<S: CommitStore> Enclave<S> {
pub fn new(
path: impl Into<PathBuf>,
key_manager: EnclaveKeyManager,
Expand Down Expand Up @@ -58,7 +58,7 @@ pub trait EnclaveInfo {
fn get_key_manager(&self) -> &EnclaveKeyManager;
}

impl<S> EnclaveInfo for Enclave<S> {
impl<S: CommitStore> EnclaveInfo for Enclave<S> {
/// `get_eid` returns the enclave id
fn get_eid(&self) -> sgx_enclave_id_t {
self.sgx_enclave.geteid()
Expand Down
13 changes: 0 additions & 13 deletions modules/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ where
}
}

unsafe impl<E, S> Send for AppService<E, S>
where
S: CommitStore + 'static,
E: EnclaveProtoAPI<S> + 'static,
{
}
unsafe impl<E, S> Sync for AppService<E, S>
where
S: CommitStore + 'static,
E: EnclaveProtoAPI<S> + 'static,
{
}

impl<E, S> AppService<E, S>
where
S: CommitStore + 'static,
Expand Down
63 changes: 59 additions & 4 deletions modules/store/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InnerMemStore>);

impl KVStore for MemStore {
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.0.lock().unwrap().get(key)
}

fn set(&mut self, key: Vec<u8>, value: Vec<u8>) {
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<T>(&self, tx_id: TxId, f: impl FnOnce(&dyn KVStore) -> T) -> Result<T> {
self.0.lock().unwrap().run_in_tx(tx_id, f)
}

fn run_in_mut_tx<T>(
&mut self,
tx_id: TxId,
f: impl FnOnce(&mut dyn KVStore) -> T,
) -> Result<T> {
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<crate::transaction::UpdateKey>,
) -> Result<Self::Tx> {
self.0.lock().unwrap().create_transaction(_update_key)
}

fn begin(&mut self, tx: &<Self::Tx as CreatedTx>::PreparedTx) -> Result<()> {
self.0.lock().unwrap().begin(tx)
}

fn commit(&mut self, tx: <Self::Tx as CreatedTx>::PreparedTx) -> Result<()> {
self.0.lock().unwrap().commit(tx)
}

fn rollback(&mut self, tx: <Self::Tx as CreatedTx>::PreparedTx) {
self.0.lock().unwrap().rollback(tx)
}
}

#[derive(Default, Debug)]
pub struct InnerMemStore {
running_tx_exists: bool,
latest_tx_id: TxId,
uncommitted_data: HashMap<Vec<u8>, Option<Vec<u8>>>,
committed_data: HashMap<Vec<u8>, Vec<u8>>,
}

impl KVStore for MemStore {
impl KVStore for InnerMemStore {
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
if self.running_tx_exists {
match self.uncommitted_data.get(key) {
Expand Down Expand Up @@ -42,7 +97,7 @@ impl KVStore for MemStore {
}
}

impl TxAccessor for MemStore {
impl TxAccessor for InnerMemStore {
fn run_in_tx<T>(&self, _tx_id: TxId, f: impl FnOnce(&dyn KVStore) -> T) -> Result<T> {
Ok(f(self))
}
Expand All @@ -56,7 +111,7 @@ impl TxAccessor for MemStore {
}
}

impl CommitStore for MemStore {
impl CommitStore for InnerMemStore {
type Tx = MemTx;

fn create_transaction(
Expand Down
2 changes: 1 addition & 1 deletion modules/store/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit b6594c0

Please sign in to comment.