From 8126160756a957b45b7bd693bf7dae542e5b8e54 Mon Sep 17 00:00:00 2001 From: sameh-farouk Date: Mon, 19 Jun 2023 19:35:13 +0300 Subject: [PATCH 1/3] moving extrinsics logic to separated mod --- .../pallets/pallet-kvstore/src/kvstore.rs | 33 +++++++++++++++++++ .../pallets/pallet-kvstore/src/lib.rs | 19 +++-------- 2 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 substrate-node/pallets/pallet-kvstore/src/kvstore.rs diff --git a/substrate-node/pallets/pallet-kvstore/src/kvstore.rs b/substrate-node/pallets/pallet-kvstore/src/kvstore.rs new file mode 100644 index 000000000..787506def --- /dev/null +++ b/substrate-node/pallets/pallet-kvstore/src/kvstore.rs @@ -0,0 +1,33 @@ +//use super::{*}; +use sp_std::prelude::{Vec}; +use frame_support::pallet_prelude::{DispatchResultWithPostInfo, ensure}; +use super::pallet::{Config, Pallet, TFKVStore, Event, Error}; + +impl Pallet { + pub fn _set( + user: T::AccountId, + key: Vec, + value: Vec, + ) -> DispatchResultWithPostInfo { + ensure!(key.len() <= 512, Error::::KeyIsTooLarge); + ensure!(value.len() <= 2048, Error::::ValueIsTooLarge); + + >::insert(&user, &key, &value); + Self::deposit_event(Event::EntrySet(user, key, value)); + Ok(().into()) + } + + pub fn _delete( + user: T::AccountId, + key: Vec, + ) -> DispatchResultWithPostInfo { + + ensure!( + >::contains_key(&user, &key), + Error::::NoValueStored + ); + let value = >::take(&user, &key); + Self::deposit_event(Event::EntryTaken(user, key, value)); + Ok(().into()) + } +} \ No newline at end of file diff --git a/substrate-node/pallets/pallet-kvstore/src/lib.rs b/substrate-node/pallets/pallet-kvstore/src/lib.rs index 9bda3261b..73b8e36e7 100644 --- a/substrate-node/pallets/pallet-kvstore/src/lib.rs +++ b/substrate-node/pallets/pallet-kvstore/src/lib.rs @@ -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)] @@ -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::*; @@ -71,12 +73,8 @@ pub mod pallet { value: Vec, ) -> DispatchResultWithPostInfo { // A user can only set their own entry - ensure!(key.len() <= 512, Error::::KeyIsTooLarge); - ensure!(value.len() <= 2048, Error::::ValueIsTooLarge); let user = ensure_signed(origin)?; - >::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. @@ -86,14 +84,7 @@ pub mod pallet { pub fn delete(origin: OriginFor, key: Vec) -> DispatchResultWithPostInfo { // A user can only take (delete) their own entry let user = ensure_signed(origin)?; - - ensure!( - >::contains_key(&user, &key), - Error::::NoValueStored - ); - let value = >::take(&user, &key); - Self::deposit_event(Event::EntryTaken(user, key, value)); - Ok(().into()) + Self::_delete(user, key) } } } From afcfd0e5e52c62407376857f10a50ec8963f475e Mon Sep 17 00:00:00 2001 From: sameh-farouk Date: Mon, 19 Jun 2023 20:06:06 +0300 Subject: [PATCH 2/3] cosmetic changes --- substrate-node/pallets/pallet-kvstore/src/kvstore.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/substrate-node/pallets/pallet-kvstore/src/kvstore.rs b/substrate-node/pallets/pallet-kvstore/src/kvstore.rs index 787506def..c69f168a1 100644 --- a/substrate-node/pallets/pallet-kvstore/src/kvstore.rs +++ b/substrate-node/pallets/pallet-kvstore/src/kvstore.rs @@ -1,4 +1,3 @@ -//use super::{*}; use sp_std::prelude::{Vec}; use frame_support::pallet_prelude::{DispatchResultWithPostInfo, ensure}; use super::pallet::{Config, Pallet, TFKVStore, Event, Error}; @@ -30,4 +29,4 @@ impl Pallet { Self::deposit_event(Event::EntryTaken(user, key, value)); Ok(().into()) } -} \ No newline at end of file +} From 50613c54630268c8682ca71e1621994409b4f3e5 Mon Sep 17 00:00:00 2001 From: sameh-farouk Date: Tue, 20 Jun 2023 12:48:32 +0300 Subject: [PATCH 3/3] style: apply formatting --- .../pallets/pallet-kvstore/src/kvstore.rs | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/substrate-node/pallets/pallet-kvstore/src/kvstore.rs b/substrate-node/pallets/pallet-kvstore/src/kvstore.rs index c69f168a1..8993ba2cb 100644 --- a/substrate-node/pallets/pallet-kvstore/src/kvstore.rs +++ b/substrate-node/pallets/pallet-kvstore/src/kvstore.rs @@ -1,26 +1,18 @@ -use sp_std::prelude::{Vec}; -use frame_support::pallet_prelude::{DispatchResultWithPostInfo, ensure}; -use super::pallet::{Config, Pallet, TFKVStore, Event, Error}; +use super::pallet::{Config, Error, Event, Pallet, TFKVStore}; +use frame_support::pallet_prelude::{ensure, DispatchResultWithPostInfo}; +use sp_std::prelude::Vec; impl Pallet { - pub fn _set( - user: T::AccountId, - key: Vec, - value: Vec, - ) -> DispatchResultWithPostInfo { + pub fn _set(user: T::AccountId, key: Vec, value: Vec) -> DispatchResultWithPostInfo { ensure!(key.len() <= 512, Error::::KeyIsTooLarge); ensure!(value.len() <= 2048, Error::::ValueIsTooLarge); - + >::insert(&user, &key, &value); Self::deposit_event(Event::EntrySet(user, key, value)); Ok(().into()) } - pub fn _delete( - user: T::AccountId, - key: Vec, - ) -> DispatchResultWithPostInfo { - + pub fn _delete(user: T::AccountId, key: Vec) -> DispatchResultWithPostInfo { ensure!( >::contains_key(&user, &key), Error::::NoValueStored