-
Notifications
You must be signed in to change notification settings - Fork 2.7k
use CountedMap in pallet-bags-list #10179
Changes from 1 commit
37771d1
8df8b8f
6f42e81
8cb4b38
7c218eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| //! Implementation of a "bags list": a semi-sorted list where ordering granularity is dictated by | ||
| //! configurable thresholds that delineate the boundaries of bags. It uses a pattern of composite | ||
| //! data structures, where multiple storage items are masked by one outer API. See [`ListNodes`], | ||
| //! [`CounterForListNodes`] and [`ListBags`] for more information. | ||
| //! [`ListBags`] for more information. | ||
| //! | ||
| //! The outer API of this module is the [`List`] struct. It wraps all acceptable operations on top | ||
| //! of the aggregate linked list. All operations with the bags list should happen through this | ||
|
|
@@ -77,17 +77,18 @@ pub struct List<T: Config>(PhantomData<T>); | |
|
|
||
| impl<T: Config> List<T> { | ||
| /// Remove all data associated with the list from storage. Parameter `items` is the number of | ||
| /// items to clear from the list. WARNING: `None` will clear all items and should generally not | ||
| /// be used in production as it could lead to an infinite number of storage accesses. | ||
| /// items to clear from the list. | ||
| /// | ||
| /// ## WARNING | ||
| /// | ||
| /// `None` will clear all items and should generally not be used in production as it could lead | ||
| /// to a vary large number of storage accesses. | ||
| pub(crate) fn clear(maybe_count: Option<u32>) -> u32 { | ||
| crate::ListBags::<T>::remove_all(maybe_count); | ||
| let pre = crate::ListNodes::<T>::count(); | ||
| crate::ListNodes::<T>::remove_all(maybe_count); | ||
| if let Some(count) = maybe_count { | ||
| crate::CounterForListNodes::<T>::mutate(|items| *items - count); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this previous code also doesn't seems correct, it is not ensure that |
||
| count | ||
| } else { | ||
| crate::CounterForListNodes::<T>::take() | ||
| } | ||
| let post = crate::ListNodes::<T>::count(); | ||
| pre.saturating_sub(post) | ||
| } | ||
|
|
||
| /// Regenerate all of the data from the given ids. | ||
|
|
@@ -274,17 +275,13 @@ impl<T: Config> List<T> { | |
| // new inserts are always the tail, so we must write the bag. | ||
| bag.put(); | ||
|
|
||
| crate::CounterForListNodes::<T>::mutate(|prev_count| { | ||
| *prev_count = prev_count.saturating_add(1) | ||
| }); | ||
|
|
||
| crate::log!( | ||
| debug, | ||
| "inserted {:?} with weight {} into bag {:?}, new count is {}", | ||
| id, | ||
| weight, | ||
| bag_weight, | ||
| crate::CounterForListNodes::<T>::get(), | ||
| crate::ListNodes::<T>::count(), | ||
| ); | ||
|
|
||
| Ok(()) | ||
|
|
@@ -331,10 +328,6 @@ impl<T: Config> List<T> { | |
| bag.put(); | ||
| } | ||
|
|
||
| crate::CounterForListNodes::<T>::mutate(|prev_count| { | ||
| *prev_count = prev_count.saturating_sub(count) | ||
| }); | ||
|
|
||
| count | ||
| } | ||
|
|
||
|
|
@@ -390,7 +383,7 @@ impl<T: Config> List<T> { | |
| /// is being used, after all other staking data (such as counter) has been updated. It checks: | ||
| /// | ||
| /// * there are no duplicate ids, | ||
| /// * length of this list is in sync with `CounterForListNodes`, | ||
| /// * length of this list is in sync with `ListNodes::count()`, | ||
| /// * and sanity-checks all bags and nodes. This will cascade down all the checks and makes sure | ||
| /// all bags and nodes are checked per *any* update to `List`. | ||
| #[cfg(feature = "std")] | ||
|
|
@@ -403,7 +396,7 @@ impl<T: Config> List<T> { | |
| ); | ||
|
|
||
| let iter_count = Self::iter().count() as u32; | ||
| let stored_count = crate::CounterForListNodes::<T>::get(); | ||
| let stored_count = crate::ListNodes::<T>::count(); | ||
| let nodes_count = crate::ListNodes::<T>::iter().count() as u32; | ||
| ensure!(iter_count == stored_count, "iter_count != stored_count"); | ||
| ensure!(stored_count == nodes_count, "stored_count != nodes_count"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the only logical change here.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not correct, the documentation of remove_all for the storage map is indeed missing, but underneath it calls sp-io::storage::clear_prefix Thus we cannot keep an accurate leftover here, we need to change sp-io::storage::clear_prefix to return here the number of key deleted in the overlay.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kianenigma wat happens nao?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. follow up is #10231 |
||
| CounterFor::<Prefix>::set(leftover); | ||
| <Self as MapWrapper>::Map::remove_all(maybe_limit); | ||
| } | ||
|
|
||
| /// Iter over all value of the storage. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.