Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Item to add noise for saving/loading #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions packages/storage/src/fuzzy_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use cosmwasm_std::{StdResult, Storage};
use secret_toolkit_serialization::{Bincode2, Serde};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::Item;

#[derive(Serialize, Deserialize)]
pub struct FuzzyData<T> {
data: T,
fuzz: u8,
}

pub struct FuzzyItem<'a, T, Ser = Bincode2>
where
T: Serialize + DeserializeOwned + Copy,
Ser: Serde,
{
item: Item<'a, FuzzyData<T>, Ser>,
storage: &'a mut dyn Storage,
}

impl<'a, T: Serialize + DeserializeOwned + Copy, Ser: Serde> FuzzyItem<'a, T, Ser> {
pub fn new(item: Item<'a, FuzzyData<T>, Ser>, storage: &'a mut dyn Storage) -> Self {
Self { item, storage }
}

pub fn add_suffix(&'a mut self, suffix: &[u8]) -> Self {
Self {
item: self.item.add_suffix(suffix),
storage: self.storage,
}
}
}

impl<'a, T, Ser> Drop for FuzzyItem<'a, T, Ser>
where
T: Serialize + DeserializeOwned + Copy,
Ser: Serde,
{
fn drop(&mut self) {
self.update(|fd| Ok(fd)).unwrap(); // This is not ideal but can't return `StdResult`
}
}

impl<'a, T, Ser> FuzzyItem<'a, T, Ser>
where
T: Serialize + DeserializeOwned + Copy,
Ser: Serde,
{
/// save will serialize the model and store, returns an error on serialization issues
pub fn save(&mut self, data: &T) -> StdResult<()> {
let new_data = match self.item.may_load(self.storage)? {
Some(fd) => FuzzyData {
data: *data,
fuzz: fd.fuzz.wrapping_add(1),
},
None => FuzzyData {
data: *data,
fuzz: 0,
},
};

self.item.save(self.storage, &new_data)
}

/// userfacing remove function
pub fn remove(&mut self) {
self.item.remove(self.storage)
}

/// load will return an error if no data is set at the given key, or on parse error
pub fn load(&self) -> StdResult<T> {
let fuzzy_data = self.item.load(self.storage)?;
Ok(fuzzy_data.data)
}

/// may_load will parse the data stored at the key if present, returns `Ok(None)` if no data there.
/// returns an error on issues parsing
pub fn may_load(&self) -> StdResult<Option<T>> {
let maybe_fuzzy_data = self.item.may_load(self.storage)?;
Ok(maybe_fuzzy_data.map(|fd| fd.data))
}

/// efficient way to see if any object is currently saved.
pub fn is_empty(&self) -> bool {
self.item.is_empty(self.storage)
}

/// Loads the data, perform the specified action, and store the result
/// in the database. This is shorthand for some common sequences, which may be useful.
///
/// It assumes, that data was initialized before, and if it doesn't exist, `Err(StdError::NotFound)`
/// is returned.
pub fn update<A>(&mut self, action: A) -> StdResult<T>
where
A: FnOnce(T) -> StdResult<T>,
{
let mut fuzzy_data = self.item.load(self.storage)?;
let input = fuzzy_data.data;
fuzzy_data.data = action(input)?;
fuzzy_data.fuzz = fuzzy_data.fuzz.wrapping_add(1);
self.item.save(self.storage, &fuzzy_data)?;
Ok(fuzzy_data.data)
}
}
1 change: 1 addition & 0 deletions packages/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod append_store;
pub mod deque_store;
pub mod fuzzy_item;
pub mod item;
pub mod keymap;
pub mod keyset;
Expand Down