referrerpolicy=no-referrer-when-downgrade

bags_list/
main.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Running
19//! Running this fuzzer can be done with `cargo hfuzz run bags-list`. `honggfuzz` CLI options can
20//! be used by setting `HFUZZ_RUN_ARGS`, such as `-n 4` to use 4 threads.
21//!
22//! # Debugging a panic
23//! Once a panic is found, it can be debugged with
24//! `cargo hfuzz run-debug fixed_point hfuzz_workspace/bags_list/*.fuzz`.
25//!
26//! # More information
27//! More information about `honggfuzz` can be found
28//! [here](https://docs.rs/honggfuzz/).
29
30use frame_election_provider_support::{SortedListProvider, VoteWeight};
31use honggfuzz::fuzz;
32use pallet_bags_list::mock::{AccountId, BagsList, ExtBuilder};
33
34const ID_RANGE: AccountId = 25_000;
35
36/// Actions of a `SortedListProvider` that we fuzz.
37enum Action {
38	Insert,
39	Update,
40	Remove,
41}
42
43impl From<u32> for Action {
44	fn from(v: u32) -> Self {
45		let num_variants = Self::Remove as u32 + 1;
46		match v % num_variants {
47			_x if _x == Action::Insert as u32 => Action::Insert,
48			_x if _x == Action::Update as u32 => Action::Update,
49			_x if _x == Action::Remove as u32 => Action::Remove,
50			_ => unreachable!(),
51		}
52	}
53}
54
55fn main() {
56	ExtBuilder::default().build_and_execute(|| loop {
57		fuzz!(|data: (AccountId, VoteWeight, u32)| {
58			let (account_id_seed, vote_weight, action_seed) = data;
59
60			let id = account_id_seed % ID_RANGE;
61			let action = Action::from(action_seed);
62
63			match action {
64				Action::Insert => {
65					if BagsList::on_insert(id, vote_weight).is_err() {
66						// this was a duplicate id, which is ok. We can just update it.
67						BagsList::on_update(&id, vote_weight).unwrap();
68					}
69					assert!(BagsList::contains(&id));
70				},
71				Action::Update => {
72					let already_contains = BagsList::contains(&id);
73					if already_contains {
74						BagsList::on_update(&id, vote_weight).unwrap();
75						assert!(BagsList::contains(&id));
76					} else {
77						BagsList::on_update(&id, vote_weight).unwrap_err();
78					}
79				},
80				Action::Remove => {
81					let already_contains = BagsList::contains(&id);
82					if already_contains {
83						BagsList::on_remove(&id).unwrap();
84					} else {
85						BagsList::on_remove(&id).unwrap_err();
86					}
87					assert!(!BagsList::contains(&id));
88				},
89			}
90
91			assert!(BagsList::do_try_state().is_ok());
92		})
93	});
94}