use CountedMap in pallet-bags-list (#10179)

* use CountedMap in pallet-bags-list

* Fix build

* Update frame/bags-list/src/list/mod.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* add a check as well

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This commit is contained in:
Kian Paimani
2021-11-10 09:33:14 +00:00
committed by GitHub
parent db59cfcf14
commit 60a50dabed
8 changed files with 94 additions and 44 deletions
@@ -31,6 +31,7 @@ use crate::{
Never,
};
use codec::{Decode, Encode, EncodeLike, FullCodec, MaxEncodedLen, Ref};
use sp_arithmetic::traits::Bounded;
use sp_runtime::traits::Saturating;
use sp_std::prelude::*;
@@ -262,9 +263,10 @@ where
}
/// Remove all value of the storage.
pub fn remove_all() {
CounterFor::<Prefix>::set(0u32);
<Self as MapWrapper>::Map::remove_all(None);
pub fn remove_all(maybe_limit: Option<u32>) {
let leftover = Self::count().saturating_sub(maybe_limit.unwrap_or_else(Bounded::max_value));
CounterFor::<Prefix>::set(leftover);
<Self as MapWrapper>::Map::remove_all(maybe_limit);
}
/// Iter over all value of the storage.
@@ -676,7 +678,7 @@ mod test {
assert_eq!(A::count(), 2);
// Remove all.
A::remove_all();
A::remove_all(None);
assert_eq!(A::count(), 0);
assert_eq!(A::initialize_counter(), 0);
@@ -907,7 +909,7 @@ mod test {
assert_eq!(B::count(), 2);
// Remove all.
B::remove_all();
B::remove_all(None);
assert_eq!(B::count(), 0);
assert_eq!(B::initialize_counter(), 0);