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

            
20
use super::Config;
21
use crate::weights::WeightInfo;
22
use frame_support::weights::Weight;
23

            
24
/// Branches within the `begin_deciding` function.
25
pub enum BeginDecidingBranch {
26
	Passing,
27
	Failing,
28
}
29

            
30
/// Branches within the `service_referendum` function.
31
pub 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

            
50
impl From<BeginDecidingBranch> for ServiceBranch {
51
6
	fn from(x: BeginDecidingBranch) -> Self {
52
		use BeginDecidingBranch::*;
53
		use ServiceBranch::*;
54
6
		match x {
55
			Passing => BeginDecidingPassing,
56
6
			Failing => BeginDecidingFailing,
57
		}
58
6
	}
59
}
60

            
61
impl 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
240
	pub fn max_weight_of_nudge<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
86
240
		Weight::zero()
87
240
			.max(T::WeightInfo::nudge_referendum_no_deposit())
88
240
			.max(T::WeightInfo::nudge_referendum_preparing())
89
240
			.max(T::WeightInfo::nudge_referendum_queued())
90
240
			.max(T::WeightInfo::nudge_referendum_not_queued())
91
240
			.max(T::WeightInfo::nudge_referendum_requeued_insertion())
92
240
			.max(T::WeightInfo::nudge_referendum_requeued_slide())
93
240
			.max(T::WeightInfo::nudge_referendum_begin_deciding_passing())
94
240
			.max(T::WeightInfo::nudge_referendum_begin_deciding_failing())
95
240
			.max(T::WeightInfo::nudge_referendum_begin_confirming())
96
240
			.max(T::WeightInfo::nudge_referendum_continue_confirming())
97
240
			.max(T::WeightInfo::nudge_referendum_end_confirming())
98
240
			.max(T::WeightInfo::nudge_referendum_continue_not_confirming())
99
240
			.max(T::WeightInfo::nudge_referendum_approved())
100
240
			.max(T::WeightInfo::nudge_referendum_rejected())
101
240
			.max(T::WeightInfo::nudge_referendum_timed_out())
102
240
	}
103

            
104
	/// Return the weight of the `place_decision_deposit` function when it takes the branch denoted
105
	/// by `self`.
106
6
	pub fn weight_of_deposit<T: Config<I>, I: 'static>(
107
6
		self,
108
6
	) -> Option<frame_support::weights::Weight> {
109
		use ServiceBranch::*;
110
6
		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
6
			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
6
		Some(ref_time_weight)
130
6
	}
131

            
132
	/// Return the maximum possible weight of the `place_decision_deposit` function.
133
711
	pub fn max_weight_of_deposit<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
134
711
		Weight::zero()
135
711
			.max(T::WeightInfo::place_decision_deposit_preparing())
136
711
			.max(T::WeightInfo::place_decision_deposit_queued())
137
711
			.max(T::WeightInfo::place_decision_deposit_not_queued())
138
711
			.max(T::WeightInfo::place_decision_deposit_passing())
139
711
			.max(T::WeightInfo::place_decision_deposit_failing())
140
711
	}
141
}
142

            
143
/// Branches that the `one_fewer_deciding` function may take.
144
pub enum OneFewerDecidingBranch {
145
	QueueEmpty,
146
	BeginDecidingPassing,
147
	BeginDecidingFailing,
148
}
149

            
150
impl 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

            
161
impl 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
330
	pub fn max_weight<T: Config<I>, I: 'static>() -> frame_support::weights::Weight {
175
330
		Weight::zero()
176
330
			.max(T::WeightInfo::one_fewer_deciding_queue_empty())
177
330
			.max(T::WeightInfo::one_fewer_deciding_passing())
178
330
			.max(T::WeightInfo::one_fewer_deciding_failing())
179
330
	}
180
}