1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// This file is part of Substrate.

// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Helpers for managing the different weights in various algorithmic branches.

use super::Config;
use crate::weights::WeightInfo;
use frame_support::weights::Weight;

/// Branches within the `begin_deciding` function.
pub enum BeginDecidingBranch {
	Passing,
	Failing,
}

/// Branches within the `service_referendum` function.
pub enum ServiceBranch {
	Fail,
	NoDeposit,
	Preparing,
	Queued,
	NotQueued,
	RequeuedInsertion,
	RequeuedSlide,
	BeginDecidingPassing,
	BeginDecidingFailing,
	BeginConfirming,
	ContinueConfirming,
	EndConfirming,
	ContinueNotConfirming,
	Approved,
	Rejected,
	TimedOut,
}

impl From<BeginDecidingBranch> for ServiceBranch {
	fn from(x: BeginDecidingBranch) -> Self {
		use BeginDecidingBranch::*;
		use ServiceBranch::*;
		match x {
			Passing => BeginDecidingPassing,
			Failing => BeginDecidingFailing,
		}
	}
}

impl ServiceBranch {
	/// Return the weight of the `nudge` function when it takes the branch denoted by `self`.
	pub fn weight_of_nudge<T: Config<I>, I: 'static>(self) -> frame_support::weights::Weight {
		use ServiceBranch::*;
		match self {
			NoDeposit => T::WeightInfo::nudge_referendum_no_deposit(),
			Preparing => T::WeightInfo::nudge_referendum_preparing(),
			Queued => T::WeightInfo::nudge_referendum_queued(),
			NotQueued => T::WeightInfo::nudge_referendum_not_queued(),
			RequeuedInsertion => T::WeightInfo::nudge_referendum_requeued_insertion(),
			RequeuedSlide => T::WeightInfo::nudge_referendum_requeued_slide(),
			BeginDecidingPassing => T::WeightInfo::nudge_referendum_begin_deciding_passing(),
			BeginDecidingFailing => T::WeightInfo::nudge_referendum_begin_deciding_failing(),
			BeginConfirming => T::WeightInfo::nudge_referendum_begin_confirming(),
			ContinueConfirming => T::WeightInfo::nudge_referendum_continue_confirming(),
			EndConfirming => T::WeightInfo::nudge_referendum_end_confirming(),
			ContinueNotConfirming => T::WeightInfo::nudge_referendum_continue_not_confirming(),
			Approved => T::WeightInfo::nudge_referendum_approved(),
			Rejected => T::WeightInfo::nudge_referendum_rejected(),
			TimedOut | Fail => T::WeightInfo::nudge_referendum_timed_out(),
		}
	}

	/// Return the maximum possible weight of the `nudge` function.
	pub fn max_weight_of_nudge<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
		Weight::zero()
			.max(T::WeightInfo::nudge_referendum_no_deposit())
			.max(T::WeightInfo::nudge_referendum_preparing())
			.max(T::WeightInfo::nudge_referendum_queued())
			.max(T::WeightInfo::nudge_referendum_not_queued())
			.max(T::WeightInfo::nudge_referendum_requeued_insertion())
			.max(T::WeightInfo::nudge_referendum_requeued_slide())
			.max(T::WeightInfo::nudge_referendum_begin_deciding_passing())
			.max(T::WeightInfo::nudge_referendum_begin_deciding_failing())
			.max(T::WeightInfo::nudge_referendum_begin_confirming())
			.max(T::WeightInfo::nudge_referendum_continue_confirming())
			.max(T::WeightInfo::nudge_referendum_end_confirming())
			.max(T::WeightInfo::nudge_referendum_continue_not_confirming())
			.max(T::WeightInfo::nudge_referendum_approved())
			.max(T::WeightInfo::nudge_referendum_rejected())
			.max(T::WeightInfo::nudge_referendum_timed_out())
	}

	/// Return the weight of the `place_decision_deposit` function when it takes the branch denoted
	/// by `self`.
	pub fn weight_of_deposit<T: Config<I>, I: 'static>(
		self,
	) -> Option<frame_support::weights::Weight> {
		use ServiceBranch::*;
		let ref_time_weight = match self {
			Preparing => T::WeightInfo::place_decision_deposit_preparing(),
			Queued => T::WeightInfo::place_decision_deposit_queued(),
			NotQueued => T::WeightInfo::place_decision_deposit_not_queued(),
			BeginDecidingPassing => T::WeightInfo::place_decision_deposit_passing(),
			BeginDecidingFailing => T::WeightInfo::place_decision_deposit_failing(),
			BeginConfirming |
			ContinueConfirming |
			EndConfirming |
			ContinueNotConfirming |
			Approved |
			Rejected |
			RequeuedInsertion |
			RequeuedSlide |
			TimedOut |
			Fail |
			NoDeposit => return None,
		};

		Some(ref_time_weight)
	}

	/// Return the maximum possible weight of the `place_decision_deposit` function.
	pub fn max_weight_of_deposit<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
		Weight::zero()
			.max(T::WeightInfo::place_decision_deposit_preparing())
			.max(T::WeightInfo::place_decision_deposit_queued())
			.max(T::WeightInfo::place_decision_deposit_not_queued())
			.max(T::WeightInfo::place_decision_deposit_passing())
			.max(T::WeightInfo::place_decision_deposit_failing())
	}
}

/// Branches that the `one_fewer_deciding` function may take.
pub enum OneFewerDecidingBranch {
	QueueEmpty,
	BeginDecidingPassing,
	BeginDecidingFailing,
}

impl From<BeginDecidingBranch> for OneFewerDecidingBranch {
	fn from(x: BeginDecidingBranch) -> Self {
		use BeginDecidingBranch::*;
		use OneFewerDecidingBranch::*;
		match x {
			Passing => BeginDecidingPassing,
			Failing => BeginDecidingFailing,
		}
	}
}

impl OneFewerDecidingBranch {
	/// Return the weight of the `one_fewer_deciding` function when it takes the branch denoted
	/// by `self`.
	pub fn weight<T: Config<I>, I: 'static>(self) -> frame_support::weights::Weight {
		use OneFewerDecidingBranch::*;
		match self {
			QueueEmpty => T::WeightInfo::one_fewer_deciding_queue_empty(),
			BeginDecidingPassing => T::WeightInfo::one_fewer_deciding_passing(),
			BeginDecidingFailing => T::WeightInfo::one_fewer_deciding_failing(),
		}
	}

	/// Return the maximum possible weight of the `one_fewer_deciding` function.
	pub fn max_weight<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
		Weight::zero()
			.max(T::WeightInfo::one_fewer_deciding_queue_empty())
			.max(T::WeightInfo::one_fewer_deciding_passing())
			.max(T::WeightInfo::one_fewer_deciding_failing())
	}
}