Skip to content

Commit

Permalink
update kill storage and bug fixes (#11)
Browse files Browse the repository at this point in the history
* 🐛 fix(afloat): modify `kill_storage` function to accept `args` parameter for more granular control over storage deletion
✨ feat(afloat): add `KillStorageArgs` enum to specify different types of storage to be deleted in `kill_storage` function

* 🐛 fix(functions.rs): add authorization check in do_cancel_offer function to ensure only admin or offer creator can cancel the offer
🐛 fix(lib.rs): remove redundant authorization check in cancel_offer function
  • Loading branch information
tlacloc authored Sep 22, 2023
1 parent a8d1072 commit 2a16ce2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
5 changes: 4 additions & 1 deletion pallets/afloat/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,14 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn do_cancel_offer(order_id: StorageId) -> DispatchResult {
pub fn do_cancel_offer(who: T::AccountId, order_id: StorageId) -> DispatchResult {
// ensure offer exists
ensure!(<AfloatOffers<T>>::contains_key(order_id), Error::<T>::OfferNotFound);
//get offer details
let offer = <AfloatOffers<T>>::get(order_id).unwrap();
let is_admin_or_owner = Self::is_admin_or_owner(who.clone())?;
ensure!(is_admin_or_owner || offer.creator_id == who, Error::<T>::Unauthorized);

match offer.status {
OfferStatus::CREATED => {
<AfloatOffers<T>>::try_mutate(order_id, |offer| -> DispatchResult {
Expand Down
39 changes: 33 additions & 6 deletions pallets/afloat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,40 @@ pub mod pallet {

#[pallet::call_index(1)]
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().reads_writes(1,1))]
pub fn kill_storage(origin: OriginFor<T>) -> DispatchResult {
pub fn kill_storage(origin: OriginFor<T>, args: KillStorageArgs) -> DispatchResult {
// ensure sudo origin
T::RemoveOrigin::ensure_origin(origin.clone())?;
Self::do_delete_all_users()?;
Ok(())
match args {
KillStorageArgs::All => {
Self::do_delete_all_users()?;
<AfloatMarketPlaceId<T>>::kill();
<AfloatCollectionId<T>>::kill();
<AfloatAssetId<T>>::kill();
let _ = <AfloatOffers<T>>::clear(1000, None);
let _ = <AfloatTransactions<T>>::clear(1000, None);
},
KillStorageArgs::UserInfo => {
Self::do_delete_all_users()?;
}
KillStorageArgs::AfloatMarketPlaceId => {
<AfloatMarketPlaceId<T>>::kill();
},
KillStorageArgs::AfloatCollectionId => {
<AfloatCollectionId<T>>::kill();
},
KillStorageArgs::AfloatAssetId => {
<AfloatAssetId<T>>::kill();
},
KillStorageArgs::AfloatOffers => {
let _ = <AfloatOffers<T>>::clear(1000, None);
},
KillStorageArgs::AfloatTransactions => {
let _ = <AfloatTransactions<T>>::clear(1000, None);
},

}

Ok(())
}

#[pallet::call_index(2)]
Expand Down Expand Up @@ -404,9 +433,7 @@ pub mod pallet {
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().reads_writes(1,1))]
pub fn cancel_offer(origin: OriginFor<T>, order_id: StorageId) -> DispatchResult {
let who = ensure_signed(origin.clone())?;
let is_admin_or_owner = Self::is_admin_or_owner(who.clone())?;
ensure!(is_admin_or_owner, Error::<T>::Unauthorized);
Self::do_cancel_offer(order_id)
Self::do_cancel_offer(who, order_id)
}
}
}
11 changes: 11 additions & 0 deletions pallets/afloat/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ pub enum CreateOfferArgs<T: Config> {
},
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, TypeInfo)]
pub enum KillStorageArgs {
All,
UserInfo,
AfloatMarketPlaceId,
AfloatCollectionId,
AfloatAssetId,
AfloatOffers,
AfloatTransactions,
}

// ! Transaction structures

#[derive(CloneNoBound, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen, PartialEq)]
Expand Down

0 comments on commit 2a16ce2

Please sign in to comment.