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

refactor: iss735 restructure kvstore pallet #753

Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions substrate-node/pallets/pallet-kvstore/src/kvstore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use sp_std::prelude::{Vec};
sameh-farouk marked this conversation as resolved.
Show resolved Hide resolved
use frame_support::pallet_prelude::{DispatchResultWithPostInfo, ensure};
use super::pallet::{Config, Pallet, TFKVStore, Event, Error};

impl<T: Config> Pallet<T> {
pub fn _set(
user: T::AccountId,
key: Vec<u8>,
value: Vec<u8>,
) -> DispatchResultWithPostInfo {
ensure!(key.len() <= 512, Error::<T>::KeyIsTooLarge);
ensure!(value.len() <= 2048, Error::<T>::ValueIsTooLarge);

<TFKVStore<T>>::insert(&user, &key, &value);
Self::deposit_event(Event::EntrySet(user, key, value));
Ok(().into())
}

pub fn _delete(
user: T::AccountId,
key: Vec<u8>,
) -> DispatchResultWithPostInfo {

ensure!(
<TFKVStore<T>>::contains_key(&user, &key),
Error::<T>::NoValueStored
);
let value = <TFKVStore<T>>::take(&user, &key);
Self::deposit_event(Event::EntryTaken(user, key, value));
Ok(().into())
}
}
19 changes: 5 additions & 14 deletions substrate-node/pallets/pallet-kvstore/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! A pallet for Threefold key-value store
#![cfg_attr(not(feature = "std"), no_std)]

pub mod kvstore;

pub use pallet::*;

#[cfg(test)]
Expand All @@ -14,7 +16,7 @@ pub mod weights;
#[frame_support::pallet]
pub mod pallet {
use super::weights::WeightInfo;
use frame_support::{ensure, pallet_prelude::*, traits::IsType};
use frame_support::{pallet_prelude::*, traits::IsType};
use frame_system::{ensure_signed, pallet_prelude::*};
use sp_std::convert::TryInto;
use sp_std::prelude::*;
Expand Down Expand Up @@ -71,12 +73,8 @@ pub mod pallet {
value: Vec<u8>,
) -> DispatchResultWithPostInfo {
// A user can only set their own entry
ensure!(key.len() <= 512, Error::<T>::KeyIsTooLarge);
ensure!(value.len() <= 2048, Error::<T>::ValueIsTooLarge);
let user = ensure_signed(origin)?;
<TFKVStore<T>>::insert(&user, &key, &value);
Self::deposit_event(Event::EntrySet(user, key, value));
Ok(().into())
Self::_set(user, key, value)
}

/// Read the value stored at a particular key, while removing it from the map.
Expand All @@ -86,14 +84,7 @@ pub mod pallet {
pub fn delete(origin: OriginFor<T>, key: Vec<u8>) -> DispatchResultWithPostInfo {
// A user can only take (delete) their own entry
let user = ensure_signed(origin)?;

ensure!(
<TFKVStore<T>>::contains_key(&user, &key),
Error::<T>::NoValueStored
);
let value = <TFKVStore<T>>::take(&user, &key);
Self::deposit_event(Event::EntryTaken(user, key, value));
Ok(().into())
Self::_delete(user, key)
}
}
}