referrerpolicy=no-referrer-when-downgrade

pallet_referenda/
branch.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//! Helpers for managing the different weights in various algorithmic branches.
19
20use super::Config;
21use crate::weights::WeightInfo;
22use frame_support::weights::Weight;
23
24/// Branches within the `begin_deciding` function.
25pub enum BeginDecidingBranch {
26	Passing,
27	Failing,
28}
29
30/// Branches within the `service_referendum` function.
31pub enum ServiceBranch {
32	Fail,
33	NoDeposit,
34	Preparing,
35	Queued,
36	NotQueued,
37	RequeuedInsertion,
38	RequeuedSlide,
39	BeginDecidingPassing,
40	BeginDecidingFailing,
41	BeginConfirming,
42	ContinueConfirming,
43	EndConfirming,
44	ContinueNotConfirming,
45	Approved,
46	Rejected,
47	TimedOut,
48}
49
50impl From<BeginDecidingBranch> for ServiceBranch {
51	fn from(x: BeginDecidingBranch) -> Self {
52		use BeginDecidingBranch::*;
53		use ServiceBranch::*;
54		match x {
55			Passing => BeginDecidingPassing,
56			Failing => BeginDecidingFailing,
57		}
58	}
59}
60
61impl ServiceBranch {
62	/// Return the weight of the `nudge` function when it takes the branch denoted by `self`.
63	pub fn weight_of_nudge<T: Config<I>, I: 'static>(self) -> frame_support::weights::Weight {
64		use ServiceBranch::*;
65		match self {
66			NoDeposit => T::WeightInfo::nudge_referendum_no_deposit(),
67			Preparing => T::WeightInfo::nudge_referendum_preparing(),
68			Queued => T::WeightInfo::nudge_referendum_queued(),
69			NotQueued => T::WeightInfo::nudge_referendum_not_queued(),
70			RequeuedInsertion => T::WeightInfo::nudge_referendum_requeued_insertion(),
71			RequeuedSlide => T::WeightInfo::nudge_referendum_requeued_slide(),
72			BeginDecidingPassing => T::WeightInfo::nudge_referendum_begin_deciding_passing(),
73			BeginDecidingFailing => T::WeightInfo::nudge_referendum_begin_deciding_failing(),
74			BeginConfirming => T::WeightInfo::nudge_referendum_begin_confirming(),
75			ContinueConfirming => T::WeightInfo::nudge_referendum_continue_confirming(),
76			EndConfirming => T::WeightInfo::nudge_referendum_end_confirming(),
77			ContinueNotConfirming => T::WeightInfo::nudge_referendum_continue_not_confirming(),
78			Approved => T::WeightInfo::nudge_referendum_approved(),
79			Rejected => T::WeightInfo::nudge_referendum_rejected(),
80			TimedOut | Fail => T::WeightInfo::nudge_referendum_timed_out(),
81		}
82	}
83
84	/// Return the maximum possible weight of the `nudge` function.
85	pub fn max_weight_of_nudge<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
86		Weight::zero()
87			.max(T::WeightInfo::nudge_referendum_no_deposit())
88			.max(T::WeightInfo::nudge_referendum_preparing())
89			.max(T::WeightInfo::nudge_referendum_queued())
90			.max(T::WeightInfo::nudge_referendum_not_queued())
91			.max(T::WeightInfo::nudge_referendum_requeued_insertion())
92			.max(T::WeightInfo::nudge_referendum_requeued_slide())
93			.max(T::WeightInfo::nudge_referendum_begin_deciding_passing())
94			.max(T::WeightInfo::nudge_referendum_begin_deciding_failing())
95			.max(T::WeightInfo::nudge_referendum_begin_confirming())
96			.max(T::WeightInfo::nudge_referendum_continue_confirming())
97			.max(T::WeightInfo::nudge_referendum_end_confirming())
98			.max(T::WeightInfo::nudge_referendum_continue_not_confirming())
99			.max(T::WeightInfo::nudge_referendum_approved())
100			.max(T::WeightInfo::nudge_referendum_rejected())
101			.max(T::WeightInfo::nudge_referendum_timed_out())
102	}
103
104	/// Return the weight of the `place_decision_deposit` function when it takes the branch denoted
105	/// by `self`.
106	pub fn weight_of_deposit<T: Config<I>, I: 'static>(
107		self,
108	) -> Option<frame_support::weights::Weight> {
109		use ServiceBranch::*;
110		let ref_time_weight = match self {
111			Preparing => T::WeightInfo::place_decision_deposit_preparing(),
112			Queued => T::WeightInfo::place_decision_deposit_queued(),
113			NotQueued => T::WeightInfo::place_decision_deposit_not_queued(),
114			BeginDecidingPassing => T::WeightInfo::place_decision_deposit_passing(),
115			BeginDecidingFailing => T::WeightInfo::place_decision_deposit_failing(),
116			BeginConfirming |
117			ContinueConfirming |
118			EndConfirming |
119			ContinueNotConfirming |
120			Approved |
121			Rejected |
122			RequeuedInsertion |
123			RequeuedSlide |
124			TimedOut |
125			Fail |
126			NoDeposit => return None,
127		};
128
129		Some(ref_time_weight)
130	}
131
132	/// Return the maximum possible weight of the `place_decision_deposit` function.
133	pub fn max_weight_of_deposit<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
134		Weight::zero()
135			.max(T::WeightInfo::place_decision_deposit_preparing())
136			.max(T::WeightInfo::place_decision_deposit_queued())
137			.max(T::WeightInfo::place_decision_deposit_not_queued())
138			.max(T::WeightInfo::place_decision_deposit_passing())
139			.max(T::WeightInfo::place_decision_deposit_failing())
140	}
141}
142
143/// Branches that the `one_fewer_deciding` function may take.
144pub enum OneFewerDecidingBranch {
145	QueueEmpty,
146	BeginDecidingPassing,
147	BeginDecidingFailing,
148}
149
150impl From<BeginDecidingBranch> for OneFewerDecidingBranch {
151	fn from(x: BeginDecidingBranch) -> Self {
152		use BeginDecidingBranch::*;
153		use OneFewerDecidingBranch::*;
154		match x {
155			Passing => BeginDecidingPassing,
156			Failing => BeginDecidingFailing,
157		}
158	}
159}
160
161impl OneFewerDecidingBranch {
162	/// Return the weight of the `one_fewer_deciding` function when it takes the branch denoted
163	/// by `self`.
164	pub fn weight<T: Config<I>, I: 'static>(self) -> frame_support::weights::Weight {
165		use OneFewerDecidingBranch::*;
166		match self {
167			QueueEmpty => T::WeightInfo::one_fewer_deciding_queue_empty(),
168			BeginDecidingPassing => T::WeightInfo::one_fewer_deciding_passing(),
169			BeginDecidingFailing => T::WeightInfo::one_fewer_deciding_failing(),
170		}
171	}
172
173	/// Return the maximum possible weight of the `one_fewer_deciding` function.
174	pub fn max_weight<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
175		Weight::zero()
176			.max(T::WeightInfo::one_fewer_deciding_queue_empty())
177			.max(T::WeightInfo::one_fewer_deciding_passing())
178			.max(T::WeightInfo::one_fewer_deciding_failing())
179	}
180}