Skip to content

Commit

Permalink
refactor: Make invocation::Store take &self instead of &mut self
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Mar 20, 2024
1 parent 11c8c05 commit 32164a3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/invocation/agent.rs
Expand Up @@ -635,7 +635,7 @@ mod tests {
);

let inv_store = crate::invocation::store::MemoryStore::default();
let mut del_store = crate::delegation::store::MemoryStore::default();
let del_store = crate::delegation::store::MemoryStore::default();

// Scenario
// ========
Expand Down Expand Up @@ -750,17 +750,17 @@ mod tests {

#[test_log::test]
fn test_chain_ok() -> TestResult {
let mut ctx = setup_test_chain()?;
let ctx = setup_test_chain()?;

let mut agent: Agent<
'_,
&mut crate::invocation::store::MemoryStore<AccountManage>,
&crate::invocation::store::MemoryStore<AccountManage>,
&crate::delegation::store::MemoryStore,
AccountManage,
> = Agent::new(
&ctx.server,
&ctx.server_signer,
&mut ctx.inv_store,
&ctx.inv_store,
&ctx.del_store,
);

Expand All @@ -771,17 +771,17 @@ mod tests {

#[test_log::test]
fn test_chain_wrong_sub() -> TestResult {
let mut ctx = setup_test_chain()?;
let ctx = setup_test_chain()?;

let mut agent: Agent<
'_,
&mut crate::invocation::store::MemoryStore<AccountManage>,
&crate::invocation::store::MemoryStore<AccountManage>,
&crate::delegation::store::MemoryStore,
AccountManage,
> = Agent::new(
&ctx.server,
&ctx.server_signer,
&mut ctx.inv_store,
&ctx.inv_store,
&ctx.del_store,
);

Expand Down
65 changes: 52 additions & 13 deletions src/invocation/store.rs
Expand Up @@ -4,6 +4,7 @@ use super::Invocation;
use crate::ability;
use crate::{crypto::varsig, did::Did};
use libipld_core::{cid::Cid, codec::Codec};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{collections::BTreeMap, convert::Infallible};

pub trait Store<T, DID: Did, V: varsig::Header<C>, C: Codec + Into<u64> + TryFrom<u64>> {
Expand All @@ -12,10 +13,10 @@ pub trait Store<T, DID: Did, V: varsig::Header<C>, C: Codec + Into<u64> + TryFro
fn get(
&self,
cid: Cid,
) -> Result<Option<&Invocation<T, DID, V, C>>, Self::InvocationStoreError>;
) -> Result<Option<Arc<Invocation<T, DID, V, C>>>, Self::InvocationStoreError>;

fn put(
&mut self,
&self,
cid: Cid,
invocation: Invocation<T, DID, V, C>,
) -> Result<(), Self::InvocationStoreError>;
Expand All @@ -31,43 +32,81 @@ impl<
DID: Did,
V: varsig::Header<C>,
C: Codec + Into<u64> + TryFrom<u64>,
> Store<T, DID, V, C> for &mut S
> Store<T, DID, V, C> for &S
{
type InvocationStoreError = <S as Store<T, DID, V, C>>::InvocationStoreError;

fn get(
&self,
cid: Cid,
) -> Result<Option<&Invocation<T, DID, V, C>>, <S as Store<T, DID, V, C>>::InvocationStoreError>
{
) -> Result<
Option<Arc<Invocation<T, DID, V, C>>>,
<S as Store<T, DID, V, C>>::InvocationStoreError,
> {
(**self).get(cid)
}

fn put(
&mut self,
&self,
cid: Cid,
invocation: Invocation<T, DID, V, C>,
) -> Result<(), <S as Store<T, DID, V, C>>::InvocationStoreError> {
(**self).put(cid, invocation)
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone)]
pub struct MemoryStore<
T = crate::ability::preset::Preset,
DID: crate::did::Did = crate::did::preset::Verifier,
V: varsig::Header<C> = varsig::header::Preset,
C: Codec + TryFrom<u64> + Into<u64> = varsig::encoding::Preset,
> {
store: BTreeMap<Cid, Invocation<T, DID, V, C>>,
inner: Arc<RwLock<MemoryStoreInner<T, DID, V, C>>>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MemoryStoreInner<
T = crate::ability::preset::Preset,
DID: crate::did::Did = crate::did::preset::Verifier,
V: varsig::Header<C> = varsig::header::Preset,
C: Codec + TryFrom<u64> + Into<u64> = varsig::encoding::Preset,
> {
store: BTreeMap<Cid, Arc<Invocation<T, DID, V, C>>>,
}

impl<T, DID: Did, V: varsig::Header<Enc>, Enc: Codec + Into<u64> + TryFrom<u64>>
MemoryStore<T, DID, V, Enc>
{
fn read(&self) -> RwLockReadGuard<'_, MemoryStoreInner<T, DID, V, Enc>> {
match self.inner.read() {
Ok(guard) => guard,
Err(poison) => {
// There's no logic errors through lock poisoning in our case
poison.into_inner()
}
}
}

fn write(&self) -> RwLockWriteGuard<'_, MemoryStoreInner<T, DID, V, Enc>> {
match self.inner.write() {
Ok(guard) => guard,
Err(poison) => {
// There's no logic errors through lock poisoning in our case
poison.into_inner()
}
}
}
}

impl<T, DID: Did, V: varsig::Header<Enc>, Enc: Codec + Into<u64> + TryFrom<u64>> Default
for MemoryStore<T, DID, V, Enc>
{
fn default() -> Self {
Self {
store: BTreeMap::new(),
inner: Arc::new(RwLock::new(MemoryStoreInner {
store: BTreeMap::new(),
})),
}
}
}
Expand All @@ -80,16 +119,16 @@ impl<T, DID: Did, V: varsig::Header<Enc>, Enc: Codec + Into<u64> + TryFrom<u64>>
fn get(
&self,
cid: Cid,
) -> Result<Option<&Invocation<T, DID, V, Enc>>, Self::InvocationStoreError> {
Ok(self.store.get(&cid))
) -> Result<Option<Arc<Invocation<T, DID, V, Enc>>>, Self::InvocationStoreError> {
Ok(self.read().store.get(&cid).cloned())
}

fn put(
&mut self,
&self,
cid: Cid,
invocation: Invocation<T, DID, V, Enc>,
) -> Result<(), Self::InvocationStoreError> {
self.store.insert(cid, invocation);
self.write().store.insert(cid, Arc::new(invocation));
Ok(())
}
}

0 comments on commit 32164a3

Please sign in to comment.