[Fix] Try-state feature-gated for BagsList (#13296)

* [Fix] Try-state feature-gated for BagsList

* fix comment

* fix try_state remote-tests

* feature-gate try-state remote test for bags-list

* remove try-state from a migration

* more SortedListProvider fixes

* more fixes

* more fixes to allow do_try_state usage in other crates

* do-try-state for fuzz

* more fixes

* more fixes

* remove feature-flag

* do-try-state

* fix review comments

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

Co-authored-by: Anton <anton.kalyaev@gmail.com>

---------

Co-authored-by: parity-processbot <>
Co-authored-by: Anton <anton.kalyaev@gmail.com>
This commit is contained in:
Roman Useinov
2023-02-09 12:47:39 +01:00
committed by GitHub
parent 214d1c0cc3
commit 03e9f43f1d
13 changed files with 46 additions and 30 deletions
+9 -1
View File
@@ -274,6 +274,13 @@ pub mod pallet {
}
}
#[cfg(any(test, feature = "try-runtime", feature = "fuzz"))]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn do_try_state() -> Result<(), &'static str> {
List::<T, I>::do_try_state()
}
}
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Move an account from one bag to another, depositing an event on success.
///
@@ -348,8 +355,9 @@ impl<T: Config<I>, I: 'static> SortedListProvider<T::AccountId> for Pallet<T, I>
List::<T, I>::unsafe_regenerate(all, score_of)
}
#[cfg(feature = "try-runtime")]
fn try_state() -> Result<(), &'static str> {
List::<T, I>::try_state()
Self::do_try_state()
}
fn unsafe_clear() {
+9 -8
View File
@@ -220,9 +220,6 @@ impl<T: Config<I>, I: 'static> List<T, I> {
crate::ListBags::<T, I>::remove(removed_bag);
}
#[cfg(feature = "std")]
debug_assert_eq!(Self::try_state(), Ok(()));
num_affected
}
@@ -514,7 +511,8 @@ impl<T: Config<I>, I: 'static> List<T, I> {
/// * 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`.
pub(crate) fn try_state() -> Result<(), &'static str> {
#[cfg(any(test, feature = "try-runtime", feature = "fuzz"))]
pub(crate) fn do_try_state() -> Result<(), &'static str> {
let mut seen_in_list = BTreeSet::new();
ensure!(
Self::iter().map(|node| node.id).all(|id| seen_in_list.insert(id)),
@@ -542,7 +540,7 @@ impl<T: Config<I>, I: 'static> List<T, I> {
thresholds.into_iter().filter_map(|t| Bag::<T, I>::get(t))
};
let _ = active_bags.clone().try_for_each(|b| b.try_state())?;
let _ = active_bags.clone().try_for_each(|b| b.do_try_state())?;
let nodes_in_bags_count =
active_bags.clone().fold(0u32, |acc, cur| acc + cur.iter().count() as u32);
@@ -553,7 +551,7 @@ impl<T: Config<I>, I: 'static> List<T, I> {
// check that all nodes are sane. We check the `ListNodes` storage item directly in case we
// have some "stale" nodes that are not in a bag.
for (_id, node) in crate::ListNodes::<T, I>::iter() {
node.try_state()?
node.do_try_state()?
}
Ok(())
@@ -751,7 +749,8 @@ impl<T: Config<I>, I: 'static> Bag<T, I> {
/// * Ensures head has no prev.
/// * Ensures tail has no next.
/// * Ensures there are no loops, traversal from head to tail is correct.
fn try_state(&self) -> Result<(), &'static str> {
#[cfg(any(test, feature = "try-runtime", feature = "fuzz"))]
fn do_try_state(&self) -> Result<(), &'static str> {
frame_support::ensure!(
self.head()
.map(|head| head.prev().is_none())
@@ -790,6 +789,7 @@ impl<T: Config<I>, I: 'static> Bag<T, I> {
}
/// Check if the bag contains a node with `id`.
#[cfg(any(test, feature = "try-runtime", feature = "fuzz"))]
fn contains(&self, id: &T::AccountId) -> bool {
self.iter().any(|n| n.id() == id)
}
@@ -894,7 +894,8 @@ impl<T: Config<I>, I: 'static> Node<T, I> {
self.bag_upper
}
fn try_state(&self) -> Result<(), &'static str> {
#[cfg(any(test, feature = "try-runtime", feature = "fuzz"))]
fn do_try_state(&self) -> Result<(), &'static str> {
let expected_bag = Bag::<T, I>::get(self.bag_upper).ok_or("bag not found for node")?;
let id = self.id();
+13 -12
View File
@@ -137,6 +137,7 @@ fn migrate_works() {
BagThresholds::set(NEW_THRESHOLDS);
// and we call
List::<Runtime>::migrate(old_thresholds);
assert_eq!(List::<Runtime>::do_try_state(), Ok(()));
// then
assert_eq!(
@@ -352,13 +353,13 @@ mod list {
#[test]
fn try_state_works() {
ExtBuilder::default().build_and_execute_no_post_check(|| {
assert_ok!(List::<Runtime>::try_state());
assert_ok!(List::<Runtime>::do_try_state());
});
// make sure there are no duplicates.
ExtBuilder::default().build_and_execute_no_post_check(|| {
Bag::<Runtime>::get(10).unwrap().insert_unchecked(2, 10);
assert_eq!(List::<Runtime>::try_state(), Err("duplicate identified"));
assert_eq!(List::<Runtime>::do_try_state(), Err("duplicate identified"));
});
// ensure count is in sync with `ListNodes::count()`.
@@ -372,7 +373,7 @@ mod list {
CounterForListNodes::<Runtime>::mutate(|counter| *counter += 1);
assert_eq!(crate::ListNodes::<Runtime>::count(), 5);
assert_eq!(List::<Runtime>::try_state(), Err("iter_count != stored_count"));
assert_eq!(List::<Runtime>::do_try_state(), Err("iter_count != stored_count"));
});
}
@@ -804,7 +805,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_1000), vec![2, 3, 13, 14]);
assert_ok!(bag_1000.try_state());
assert_ok!(bag_1000.do_try_state());
// and the node isn't mutated when its removed
assert_eq!(node_4, node_4_pre_remove);
@@ -814,7 +815,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_1000), vec![3, 13, 14]);
assert_ok!(bag_1000.try_state());
assert_ok!(bag_1000.do_try_state());
// when removing a tail that is not pointing at the head
let node_14 = Node::<Runtime>::get(&14).unwrap();
@@ -822,7 +823,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_1000), vec![3, 13]);
assert_ok!(bag_1000.try_state());
assert_ok!(bag_1000.do_try_state());
// when removing a tail that is pointing at the head
let node_13 = Node::<Runtime>::get(&13).unwrap();
@@ -830,7 +831,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_1000), vec![3]);
assert_ok!(bag_1000.try_state());
assert_ok!(bag_1000.do_try_state());
// when removing a node that is both the head & tail
let node_3 = Node::<Runtime>::get(&3).unwrap();
@@ -846,7 +847,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_10), vec![1, 12]);
assert_ok!(bag_10.try_state());
assert_ok!(bag_10.do_try_state());
// when removing a head that is pointing at the tail
let node_1 = Node::<Runtime>::get(&1).unwrap();
@@ -854,7 +855,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_10), vec![12]);
assert_ok!(bag_10.try_state());
assert_ok!(bag_10.do_try_state());
// and since we updated the bag's head/tail, we need to write this storage so we
// can correctly `get` it again in later checks
bag_10.put();
@@ -865,7 +866,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_2000), vec![15, 17, 18, 19]);
assert_ok!(bag_2000.try_state());
assert_ok!(bag_2000.do_try_state());
// when removing a node that is pointing at tail, but not head
let node_18 = Node::<Runtime>::get(&18).unwrap();
@@ -873,7 +874,7 @@ mod bags {
// then
assert_eq!(bag_as_ids(&bag_2000), vec![15, 17, 19]);
assert_ok!(bag_2000.try_state());
assert_ok!(bag_2000.do_try_state());
// finally, when reading from storage, the state of all bags is as expected
assert_eq!(
@@ -905,7 +906,7 @@ mod bags {
// .. and the bag it was removed from
let bag_1000 = Bag::<Runtime>::get(1_000).unwrap();
// is sane
assert_ok!(bag_1000.try_state());
assert_ok!(bag_1000.do_try_state());
// and has the correct head and tail.
assert_eq!(bag_1000.head, Some(3));
assert_eq!(bag_1000.tail, Some(4));
+1 -1
View File
@@ -148,7 +148,7 @@ impl ExtBuilder {
pub fn build_and_execute(self, test: impl FnOnce() -> ()) {
self.build().execute_with(|| {
test();
List::<Runtime>::try_state().expect("Try-state post condition failed")
List::<Runtime>::do_try_state().expect("do_try_state post condition failed")
})
}