Fuzzer for Pallet Bags List (#9851)

* Fuzzer for Pallet Bags List

* Some small updates

* Fuzzer for Pallet Bags List

This PR adds a fuzzer for the `SortedListProvider` API exposed by pallet-bags-list.

* Feature gate code NOT used by fuzz feature

* Create Enum for list actions

* fix some small mistakes

* try and make CI happy

* fmt

* Do not insert before updating

* clean up some misc. comments

* marginally improve Node::sanity_check

* Change ID_RANGE to 25_000

* comma

* try improve correct feature gating so no unused code

Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Zeke Mostov
2021-10-31 22:10:13 +01:00
committed by GitHub
parent 47c7447106
commit 21c8d18c23
9 changed files with 144 additions and 10 deletions
+2 -2
View File
@@ -59,8 +59,8 @@ use sp_std::prelude::*;
mod benchmarks;
mod list;
#[cfg(test)]
mod mock;
#[cfg(any(test, feature = "fuzz"))]
pub mod mock;
#[cfg(test)]
mod tests;
pub mod weights;
+8 -6
View File
@@ -391,8 +391,8 @@ impl<T: Config> List<T> {
///
/// * there are no duplicate ids,
/// * length of this list is in sync with `CounterForListNodes`,
/// * and sanity-checks all bags. This will cascade down all the checks and makes sure all bags
/// are checked per *any* update to `List`.
/// * 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")]
pub(crate) fn sanity_check() -> Result<(), &'static str> {
use frame_support::ensure;
@@ -414,7 +414,6 @@ impl<T: Config> List<T> {
let thresholds = T::BagThresholds::get().iter().copied();
let thresholds: Vec<u64> = if thresholds.clone().last() == Some(VoteWeight::MAX) {
// in the event that they included it, we don't need to make any changes
// Box::new(thresholds.collect()
thresholds.collect()
} else {
// otherwise, insert it here.
@@ -774,10 +773,13 @@ impl<T: Config> Node<T> {
"node does not exist in the expected bag"
);
let non_terminal_check = !self.is_terminal() &&
expected_bag.head.as_ref() != Some(id) &&
expected_bag.tail.as_ref() != Some(id);
let terminal_check =
expected_bag.head.as_ref() == Some(id) || expected_bag.tail.as_ref() == Some(id);
frame_support::ensure!(
!self.is_terminal() ||
expected_bag.head.as_ref() == Some(id) ||
expected_bag.tail.as_ref() == Some(id),
non_terminal_check || terminal_check,
"a terminal node is neither its bag head or tail"
);
+5 -2
View File
@@ -101,12 +101,13 @@ pub(crate) const GENESIS_IDS: [(AccountId, VoteWeight); 4] =
[(1, 10), (2, 1_000), (3, 1_000), (4, 1_000)];
#[derive(Default)]
pub(crate) struct ExtBuilder {
pub struct ExtBuilder {
ids: Vec<(AccountId, VoteWeight)>,
}
impl ExtBuilder {
/// Add some AccountIds to insert into `List`.
#[cfg(test)]
pub(crate) fn add_ids(mut self, ids: Vec<(AccountId, VoteWeight)>) -> Self {
self.ids = ids;
self
@@ -126,18 +127,20 @@ impl ExtBuilder {
ext
}
pub(crate) fn build_and_execute(self, test: impl FnOnce() -> ()) {
pub fn build_and_execute(self, test: impl FnOnce() -> ()) {
self.build().execute_with(|| {
test();
List::<Runtime>::sanity_check().expect("Sanity check post condition failed")
})
}
#[cfg(test)]
pub(crate) fn build_and_execute_no_post_check(self, test: impl FnOnce() -> ()) {
self.build().execute_with(test)
}
}
#[cfg(test)]
pub(crate) mod test_utils {
use super::*;
use list::Bag;