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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
// This file is part of Substrate.

// Copyright (C) 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.

//! The signed phase implementation.

use crate::{
	unsigned::MinerConfig, Config, ElectionCompute, Pallet, QueuedSolution, RawSolution,
	ReadySolution, SignedSubmissionIndices, SignedSubmissionNextIndex, SignedSubmissionsMap,
	SolutionOf, SolutionOrSnapshotSize, Weight, WeightInfo,
};
use codec::{Decode, Encode, HasCompact};
use frame_election_provider_support::NposSolution;
use frame_support::traits::{
	defensive_prelude::*, Currency, Get, OnUnbalanced, ReservableCurrency,
};
use frame_system::pallet_prelude::BlockNumberFor;
use sp_arithmetic::traits::SaturatedConversion;
use sp_core::bounded::BoundedVec;
use sp_npos_elections::ElectionScore;
use sp_runtime::{
	traits::{Saturating, Zero},
	RuntimeDebug,
};
use sp_std::{
	cmp::Ordering,
	collections::{btree_map::BTreeMap, btree_set::BTreeSet},
	vec::Vec,
};

/// A raw, unchecked signed submission.
///
/// This is just a wrapper around [`RawSolution`] and some additional info.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, scale_info::TypeInfo)]
pub struct SignedSubmission<AccountId, Balance: HasCompact, Solution> {
	/// Who submitted this solution.
	pub who: AccountId,
	/// The deposit reserved for storing this solution.
	pub deposit: Balance,
	/// The raw solution itself.
	pub raw_solution: RawSolution<Solution>,
	// The estimated fee `who` paid to submit the solution.
	pub call_fee: Balance,
}

impl<AccountId, Balance, Solution> Ord for SignedSubmission<AccountId, Balance, Solution>
where
	AccountId: Ord,
	Balance: Ord + HasCompact,
	Solution: Ord,
	RawSolution<Solution>: Ord,
{
	fn cmp(&self, other: &Self) -> Ordering {
		self.raw_solution
			.score
			.cmp(&other.raw_solution.score)
			.then_with(|| self.raw_solution.cmp(&other.raw_solution))
			.then_with(|| self.deposit.cmp(&other.deposit))
			.then_with(|| self.who.cmp(&other.who))
	}
}

impl<AccountId, Balance, Solution> PartialOrd for SignedSubmission<AccountId, Balance, Solution>
where
	AccountId: Ord,
	Balance: Ord + HasCompact,
	Solution: Ord,
	RawSolution<Solution>: Ord,
{
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}

pub type BalanceOf<T> =
	<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type PositiveImbalanceOf<T> = <<T as Config>::Currency as Currency<
	<T as frame_system::Config>::AccountId,
>>::PositiveImbalance;
pub type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
	<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
pub type SignedSubmissionOf<T> = SignedSubmission<
	<T as frame_system::Config>::AccountId,
	BalanceOf<T>,
	<<T as crate::Config>::MinerConfig as MinerConfig>::Solution,
>;

/// Always sorted vector of a score, submitted at the given block number, which can be found at the
/// given index (`u32`) of the `SignedSubmissionsMap`.
pub type SubmissionIndicesOf<T> =
	BoundedVec<(ElectionScore, BlockNumberFor<T>, u32), <T as Config>::SignedMaxSubmissions>;

/// Outcome of [`SignedSubmissions::insert`].
pub enum InsertResult<T: Config> {
	/// The submission was not inserted because the queue was full and the submission had
	/// insufficient score to eject a prior solution from the queue.
	NotInserted,
	/// The submission was inserted successfully without ejecting a solution.
	Inserted,
	/// The submission was inserted successfully. As the queue was full, this operation ejected a
	/// prior solution, contained in this variant.
	InsertedEjecting(SignedSubmissionOf<T>),
}

/// Mask type which pretends to be a set of `SignedSubmissionOf<T>`, while in fact delegating to the
/// actual implementations in `SignedSubmissionIndices<T>`, `SignedSubmissionsMap<T>`, and
/// `SignedSubmissionNextIndex<T>`.
#[cfg_attr(feature = "std", derive(frame_support::DebugNoBound))]
pub struct SignedSubmissions<T: Config> {
	indices: SubmissionIndicesOf<T>,
	next_idx: u32,
	insertion_overlay: BTreeMap<u32, SignedSubmissionOf<T>>,
	deletion_overlay: BTreeSet<u32>,
}

impl<T: Config> SignedSubmissions<T> {
	/// `true` if the structure is empty.
	pub fn is_empty(&self) -> bool {
		self.indices.is_empty()
	}

	/// Get the length of submitted solutions.
	pub fn len(&self) -> usize {
		self.indices.len()
	}

	/// Get the signed submissions from storage.
	pub fn get() -> Self {
		let submissions = SignedSubmissions {
			indices: SignedSubmissionIndices::<T>::get(),
			next_idx: SignedSubmissionNextIndex::<T>::get(),
			insertion_overlay: BTreeMap::new(),
			deletion_overlay: BTreeSet::new(),
		};

		// validate that the stored state is sane
		debug_assert!(submissions
			.indices
			.iter()
			.map(|(_, _, index)| index)
			.copied()
			.max()
			.map_or(true, |max_idx| submissions.next_idx > max_idx,));
		submissions
	}

	/// Put the signed submissions back into storage.
	pub fn put(mut self) {
		// validate that we're going to write only sane things to storage
		debug_assert!(self
			.insertion_overlay
			.keys()
			.copied()
			.max()
			.map_or(true, |max_idx| self.next_idx > max_idx,));
		debug_assert!(self
			.indices
			.iter()
			.map(|(_, _, index)| index)
			.copied()
			.max()
			.map_or(true, |max_idx| self.next_idx > max_idx,));

		SignedSubmissionIndices::<T>::put(self.indices);
		SignedSubmissionNextIndex::<T>::put(self.next_idx);
		for key in self.deletion_overlay {
			self.insertion_overlay.remove(&key);
			SignedSubmissionsMap::<T>::remove(key);
		}
		for (key, value) in self.insertion_overlay {
			SignedSubmissionsMap::<T>::insert(key, value);
		}
	}

	/// Get the submission at a particular index.
	fn get_submission(&self, index: u32) -> Option<SignedSubmissionOf<T>> {
		if self.deletion_overlay.contains(&index) {
			// Note: can't actually remove the item from the insertion overlay (if present) because
			// we don't want to use `&mut self` here. There may be some kind of `RefCell`
			// optimization possible here in the future.
			None
		} else {
			self.insertion_overlay
				.get(&index)
				.cloned()
				.or_else(|| SignedSubmissionsMap::<T>::get(index))
		}
	}

	/// Perform three operations:
	///
	/// - Remove the solution at the given position of `self.indices`.
	/// - Insert a new submission (identified by score and insertion index), if provided.
	/// - Return the submission which was removed, if any.
	///
	/// The call site must ensure that `remove_pos` is a valid index. If otherwise, `None` is
	/// silently returned.
	///
	/// Note: this doesn't insert into `insertion_overlay`, the optional new insertion must be
	/// inserted into `insertion_overlay` to keep the variable `self` in a valid state.
	fn swap_out_submission(
		&mut self,
		remove_pos: usize,
		insert: Option<(ElectionScore, BlockNumberFor<T>, u32)>,
	) -> Option<SignedSubmissionOf<T>> {
		if remove_pos >= self.indices.len() {
			return None
		}

		// safe: index was just checked in the line above.
		let (_, _, remove_index) = self.indices.remove(remove_pos);

		if let Some((insert_score, block_number, insert_idx)) = insert {
			self.indices
				.try_push((insert_score, block_number, insert_idx))
				.expect("just removed an item, we must be under capacity; qed");
		}

		self.insertion_overlay.remove(&remove_index).or_else(|| {
			(!self.deletion_overlay.contains(&remove_index))
				.then(|| {
					self.deletion_overlay.insert(remove_index);
					SignedSubmissionsMap::<T>::get(remove_index)
				})
				.flatten()
		})
	}

	/// Remove the signed submission with the highest score from the set.
	pub fn pop_last(&mut self) -> Option<SignedSubmissionOf<T>> {
		let best_index = self.indices.len().checked_sub(1)?;
		self.swap_out_submission(best_index, None)
	}

	/// Iterate through the set of signed submissions in order of increasing score.
	pub fn iter(&self) -> impl '_ + Iterator<Item = SignedSubmissionOf<T>> {
		self.indices
			.iter()
			.filter_map(move |(_score, _bn, idx)| self.get_submission(*idx).defensive())
	}

	/// Empty the set of signed submissions, returning an iterator of signed submissions in
	/// order of submission.
	///
	/// Note that if the iterator is dropped without consuming all elements, not all may be removed
	/// from the underlying `SignedSubmissionsMap`, putting the storages into an invalid state.
	///
	/// Note that, like `put`, this function consumes `Self` and modifies storage.
	fn drain_submitted_order(mut self) -> impl Iterator<Item = SignedSubmissionOf<T>> {
		let mut keys = SignedSubmissionsMap::<T>::iter_keys()
			.filter(|k| {
				if self.deletion_overlay.contains(k) {
					// Remove submissions that should be deleted.
					SignedSubmissionsMap::<T>::remove(k);
					false
				} else {
					true
				}
			})
			.chain(self.insertion_overlay.keys().copied())
			.collect::<Vec<_>>();
		keys.sort();

		SignedSubmissionIndices::<T>::kill();
		SignedSubmissionNextIndex::<T>::kill();

		keys.into_iter().filter_map(move |index| {
			SignedSubmissionsMap::<T>::take(index).or_else(|| self.insertion_overlay.remove(&index))
		})
	}

	/// Decode the length of the signed submissions without actually reading the entire struct into
	/// memory.
	///
	/// Note that if you hold an instance of `SignedSubmissions`, this function does _not_
	/// track its current length. This only decodes what is currently stored in memory.
	pub fn decode_len() -> Option<usize> {
		SignedSubmissionIndices::<T>::decode_len()
	}

	/// Insert a new signed submission into the set.
	///
	/// In the event that the new submission is not better than the current weakest according
	/// to `is_score_better`, we do not change anything.
	pub fn insert(&mut self, submission: SignedSubmissionOf<T>) -> InsertResult<T> {
		// verify the expectation that we never reuse an index
		debug_assert!(!self.indices.iter().map(|(_, _, x)| x).any(|&idx| idx == self.next_idx));
		let block_number = frame_system::Pallet::<T>::block_number();

		let maybe_weakest = match self.indices.try_push((
			submission.raw_solution.score,
			block_number,
			self.next_idx,
		)) {
			Ok(_) => None,
			Err(_) => {
				// the queue is full -- if this is better, insert it.
				let weakest_score = match self.indices.iter().next().defensive() {
					None => return InsertResult::NotInserted,
					Some((score, _, _)) => *score,
				};
				let threshold = T::BetterSignedThreshold::get();

				// if we haven't improved on the weakest score, don't change anything.
				if !submission.raw_solution.score.strict_threshold_better(weakest_score, threshold)
				{
					return InsertResult::NotInserted
				}

				self.swap_out_submission(
					0, // swap out the worse one, which is always index 0.
					Some((submission.raw_solution.score, block_number, self.next_idx)),
				)
			},
		};

		// this is the ONLY place that we insert, and we sort post insertion. If scores are the
		// same, we sort based on reverse of submission block number.
		self.indices
			.sort_by(|(score1, bn1, _), (score2, bn2, _)| match score1.cmp(score2) {
				Ordering::Equal => bn1.cmp(&bn2).reverse(),
				x => x,
			});

		// we've taken out the weakest, so update the storage map and the next index
		debug_assert!(!self.insertion_overlay.contains_key(&self.next_idx));
		self.insertion_overlay.insert(self.next_idx, submission);
		debug_assert!(!self.deletion_overlay.contains(&self.next_idx));
		self.next_idx += 1;
		match maybe_weakest {
			Some(weakest) => InsertResult::InsertedEjecting(weakest),
			None => InsertResult::Inserted,
		}
	}
}

impl<T: Config> Pallet<T> {
	/// `Self` accessor for `SignedSubmission<T>`.
	pub fn signed_submissions() -> SignedSubmissions<T> {
		SignedSubmissions::<T>::get()
	}

	/// Finish the signed phase. Process the signed submissions from best to worse until a valid one
	/// is found, rewarding the best one and slashing the invalid ones along the way.
	///
	/// Returns true if we have a good solution in the signed phase.
	///
	/// This drains the [`SignedSubmissions`], potentially storing the best valid one in
	/// [`QueuedSolution`].
	///
	/// This is a *self-weighing* function, it automatically registers its weight internally when
	/// being called.
	pub fn finalize_signed_phase() -> bool {
		let (weight, found_solution) = Self::finalize_signed_phase_internal();
		Self::register_weight(weight);
		found_solution
	}

	/// The guts of [`finalized_signed_phase`], that does everything except registering its weight.
	pub(crate) fn finalize_signed_phase_internal() -> (Weight, bool) {
		let mut all_submissions = Self::signed_submissions();
		let mut found_solution = false;
		let mut weight = T::DbWeight::get().reads(1);

		let SolutionOrSnapshotSize { voters, targets } =
			Self::snapshot_metadata().unwrap_or_default();

		while let Some(best) = all_submissions.pop_last() {
			log!(
				debug,
				"finalized_signed: trying to verify from {:?} score {:?}",
				best.who,
				best.raw_solution.score
			);
			let SignedSubmission { raw_solution, who, deposit, call_fee } = best;
			let active_voters = raw_solution.solution.voter_count() as u32;
			let feasibility_weight = {
				// defensive only: at the end of signed phase, snapshot will exits.
				let desired_targets = Self::desired_targets().defensive_unwrap_or_default();
				T::WeightInfo::feasibility_check(voters, targets, active_voters, desired_targets)
			};

			// the feasibility check itself has some weight
			weight = weight.saturating_add(feasibility_weight);
			match Self::feasibility_check(raw_solution, ElectionCompute::Signed) {
				Ok(ready_solution) => {
					Self::finalize_signed_phase_accept_solution(
						ready_solution,
						&who,
						deposit,
						call_fee,
					);
					found_solution = true;
					log!(debug, "finalized_signed: found a valid solution");

					weight = weight
						.saturating_add(T::WeightInfo::finalize_signed_phase_accept_solution());
					break
				},
				Err(_) => {
					log!(warn, "finalized_signed: invalid signed submission found, slashing.");
					Self::finalize_signed_phase_reject_solution(&who, deposit);
					weight = weight
						.saturating_add(T::WeightInfo::finalize_signed_phase_reject_solution());
				},
			}
		}

		// Any unprocessed solution is pointless to even consider. Feasible or malicious,
		// they didn't end up being used. Unreserve the bonds.
		let discarded = all_submissions.len();
		let mut refund_count = 0;
		let max_refunds = T::SignedMaxRefunds::get();

		for SignedSubmission { who, deposit, call_fee, .. } in
			all_submissions.drain_submitted_order()
		{
			if refund_count < max_refunds {
				// Refund fee
				let positive_imbalance = T::Currency::deposit_creating(&who, call_fee);
				T::RewardHandler::on_unbalanced(positive_imbalance);
				refund_count += 1;
			}

			// Unreserve deposit
			let _remaining = T::Currency::unreserve(&who, deposit);
			debug_assert!(_remaining.is_zero());
			weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2));
		}

		debug_assert!(!SignedSubmissionIndices::<T>::exists());
		debug_assert!(!SignedSubmissionNextIndex::<T>::exists());
		debug_assert!(SignedSubmissionsMap::<T>::iter().next().is_none());

		log!(
			debug,
			"closed signed phase, found solution? {}, discarded {}",
			found_solution,
			discarded
		);

		(weight, found_solution)
	}
	/// Helper function for the case where a solution is accepted in the signed phase.
	///
	/// Extracted to facilitate with weight calculation.
	///
	/// Infallible
	pub fn finalize_signed_phase_accept_solution(
		ready_solution: ReadySolution<T::AccountId, T::MaxWinners>,
		who: &T::AccountId,
		deposit: BalanceOf<T>,
		call_fee: BalanceOf<T>,
	) {
		// write this ready solution.
		<QueuedSolution<T>>::put(ready_solution);

		let reward = T::SignedRewardBase::get();
		// emit reward event
		Self::deposit_event(crate::Event::Rewarded { account: who.clone(), value: reward });

		// Unreserve deposit.
		let _remaining = T::Currency::unreserve(who, deposit);
		debug_assert!(_remaining.is_zero());

		// Reward and refund the call fee.
		let positive_imbalance =
			T::Currency::deposit_creating(who, reward.saturating_add(call_fee));
		T::RewardHandler::on_unbalanced(positive_imbalance);
	}

	/// Helper function for the case where a solution is accepted in the rejected phase.
	///
	/// Extracted to facilitate with weight calculation.
	///
	/// Infallible
	pub fn finalize_signed_phase_reject_solution(who: &T::AccountId, deposit: BalanceOf<T>) {
		Self::deposit_event(crate::Event::Slashed { account: who.clone(), value: deposit });
		let (negative_imbalance, _remaining) = T::Currency::slash_reserved(who, deposit);
		debug_assert!(_remaining.is_zero());
		T::SlashHandler::on_unbalanced(negative_imbalance);
	}

	/// The weight of the given raw solution.
	pub fn solution_weight_of(
		raw_solution: &RawSolution<SolutionOf<T::MinerConfig>>,
		size: SolutionOrSnapshotSize,
	) -> Weight {
		T::MinerConfig::solution_weight(
			size.voters,
			size.targets,
			raw_solution.solution.voter_count() as u32,
			raw_solution.solution.unique_targets().len() as u32,
		)
	}

	/// Collect a sufficient deposit to store this solution.
	///
	/// The deposit is composed of 3 main elements:
	///
	/// 1. base deposit, fixed for all submissions.
	/// 2. a per-byte deposit, for renting the state usage.
	/// 3. a per-weight deposit, for the potential weight usage in an upcoming on_initialize
	pub fn deposit_for(
		raw_solution: &RawSolution<SolutionOf<T::MinerConfig>>,
		size: SolutionOrSnapshotSize,
	) -> BalanceOf<T> {
		let encoded_len: u32 = raw_solution.encoded_size().saturated_into();
		let encoded_len: BalanceOf<T> = encoded_len.into();
		let feasibility_weight = Self::solution_weight_of(raw_solution, size);

		let len_deposit = T::SignedDepositByte::get().saturating_mul(encoded_len);
		let weight_deposit = T::SignedDepositWeight::get()
			.saturating_mul(feasibility_weight.ref_time().saturated_into());

		T::SignedDepositBase::get()
			.saturating_add(len_deposit)
			.saturating_add(weight_deposit)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::{
		mock::*, ElectionBoundsBuilder, ElectionCompute, ElectionError, Error, Event, Perbill,
		Phase,
	};
	use frame_support::{assert_noop, assert_ok, assert_storage_noop};

	#[test]
	fn cannot_submit_too_early() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to(2);
			assert_eq!(MultiPhase::current_phase(), Phase::Off);

			// create a temp snapshot only for this test.
			MultiPhase::create_snapshot().unwrap();
			let solution = raw_solution();

			assert_noop!(
				MultiPhase::submit(RuntimeOrigin::signed(10), Box::new(solution)),
				Error::<Runtime>::PreDispatchEarlySubmission,
			);

			// make sure invariants hold true and post-test try state checks to pass.
			<crate::Snapshot<Runtime>>::kill();
			<crate::SnapshotMetadata<Runtime>>::kill();
			<crate::DesiredTargets<Runtime>>::kill();
		})
	}

	#[test]
	fn data_provider_should_respect_target_limits() {
		ExtBuilder::default().build_and_execute(|| {
			// given a reduced expectation of maximum electable targets
			let new_bounds = ElectionBoundsBuilder::default().targets_count(2.into()).build();
			ElectionsBounds::set(new_bounds);
			// and a data provider that does not respect limits
			DataProviderAllowBadData::set(true);

			assert_noop!(
				MultiPhase::create_snapshot(),
				ElectionError::DataProvider("Ensure targets bounds: bounds exceeded."),
			);
		})
	}

	#[test]
	fn data_provider_should_respect_voter_limits() {
		ExtBuilder::default().build_and_execute(|| {
			// given a reduced expectation of maximum electing voters
			let new_bounds = ElectionBoundsBuilder::default().voters_count(2.into()).build();
			ElectionsBounds::set(new_bounds);
			// and a data provider that does not respect limits
			DataProviderAllowBadData::set(true);

			assert_noop!(
				MultiPhase::create_snapshot(),
				ElectionError::DataProvider("Ensure voters bounds: bounds exceeded."),
			);
		})
	}

	#[test]
	fn desired_targets_greater_than_max_winners() {
		ExtBuilder::default().build_and_execute(|| {
			// given desired_targets bigger than MaxWinners
			DesiredTargets::set(4);
			MaxWinners::set(3);

			// snapshot not created because data provider returned an unexpected number of
			// desired_targets
			assert_noop!(
				MultiPhase::create_snapshot_external(),
				ElectionError::DataProvider("desired_targets must not be greater than MaxWinners."),
			);
		})
	}

	#[test]
	fn should_pay_deposit() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			let solution = raw_solution();
			assert_eq!(balances(&99), (100, 0));

			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));

			assert_eq!(balances(&99), (95, 5));
			assert_eq!(MultiPhase::signed_submissions().iter().next().unwrap().deposit, 5);

			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(99),
						prev_ejected: false
					}
				]
			);
		})
	}

	#[test]
	fn good_solution_is_rewarded() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			let solution = raw_solution();
			assert_eq!(balances(&99), (100, 0));

			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
			assert_eq!(balances(&99), (95, 5));

			assert!(MultiPhase::finalize_signed_phase());
			assert_eq!(balances(&99), (100 + 7 + 8, 0));

			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(99),
						prev_ejected: false
					},
					Event::Rewarded { account: 99, value: 7 }
				]
			);
		})
	}

	#[test]
	fn bad_solution_is_slashed() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			let mut solution = raw_solution();
			assert_eq!(balances(&99), (100, 0));

			// make the solution invalid.
			solution.score.minimal_stake += 1;

			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
			assert_eq!(balances(&99), (95, 5));

			// no good solution was stored.
			assert!(!MultiPhase::finalize_signed_phase());
			// and the bond is gone.
			assert_eq!(balances(&99), (95, 0));

			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(99),
						prev_ejected: false
					},
					Event::Slashed { account: 99, value: 5 }
				]
			);
		})
	}

	#[test]
	fn suppressed_solution_gets_bond_back() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			let mut solution = raw_solution();
			assert_eq!(balances(&99), (100, 0));
			assert_eq!(balances(&999), (100, 0));

			// submit as correct.
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution.clone())));

			// make the solution invalid and weaker.
			solution.score.minimal_stake -= 1;
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(999), Box::new(solution)));
			assert_eq!(balances(&99), (95, 5));
			assert_eq!(balances(&999), (95, 5));

			// _some_ good solution was stored.
			assert!(MultiPhase::finalize_signed_phase());

			// 99 is rewarded.
			assert_eq!(balances(&99), (100 + 7 + 8, 0));
			// 999 gets everything back, including the call fee.
			assert_eq!(balances(&999), (100 + 8, 0));
			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(99),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(999),
						prev_ejected: false
					},
					Event::Rewarded { account: 99, value: 7 }
				]
			);
		})
	}

	#[test]
	fn cannot_submit_worse_with_full_queue() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			for s in 0..SignedMaxSubmissions::get() {
				// score is always getting better
				let solution = RawSolution {
					score: ElectionScore { minimal_stake: (5 + s).into(), ..Default::default() },
					..Default::default()
				};
				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
			}

			// weaker.
			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 4, ..Default::default() },
				..Default::default()
			};

			assert_noop!(
				MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)),
				Error::<Runtime>::SignedQueueFull,
			);
		})
	}

	#[test]
	fn call_fee_refund_is_limited_by_signed_max_refunds() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());
			assert_eq!(SignedMaxRefunds::get(), 1);
			assert!(SignedMaxSubmissions::get() > 2);

			for s in 0..SignedMaxSubmissions::get() {
				let account = 99 + s as u64;
				Balances::make_free_balance_be(&account, 100);
				// score is always decreasing
				let mut solution = raw_solution();
				solution.score.minimal_stake -= s as u128;

				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(account), Box::new(solution)));
				assert_eq!(balances(&account), (95, 5));
			}

			assert_ok!(MultiPhase::do_elect());

			for s in 0..SignedMaxSubmissions::get() {
				let account = 99 + s as u64;
				// lower accounts have higher scores
				if s == 0 {
					// winning solution always gets call fee + reward
					assert_eq!(balances(&account), (100 + 8 + 7, 0))
				} else if s == 1 {
					// 1 runner up gets their call fee refunded
					assert_eq!(balances(&account), (100 + 8, 0))
				} else {
					// all other solutions don't get a call fee refund
					assert_eq!(balances(&account), (100, 0));
				}
			}
			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(99),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(100),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(101),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(102),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(103),
						prev_ejected: false
					},
					Event::Rewarded { account: 99, value: 7 },
					Event::ElectionFinalized {
						compute: ElectionCompute::Signed,
						score: ElectionScore {
							minimal_stake: 40,
							sum_stake: 100,
							sum_stake_squared: 5200
						}
					}
				]
			);
		});
	}

	#[test]
	fn cannot_submit_worse_with_full_queue_depends_on_threshold() {
		ExtBuilder::default()
			.signed_max_submission(1)
			.better_signed_threshold(Perbill::from_percent(20))
			.build_and_execute(|| {
				roll_to_signed();
				assert!(MultiPhase::current_phase().is_signed());

				let mut solution = RawSolution {
					score: ElectionScore {
						minimal_stake: 5u128,
						sum_stake: 0u128,
						sum_stake_squared: 10u128,
					},
					..Default::default()
				};
				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));

				// This is 10% better, so does not meet the 20% threshold and is therefore rejected.
				solution = RawSolution {
					score: ElectionScore {
						minimal_stake: 5u128,
						sum_stake: 0u128,
						sum_stake_squared: 9u128,
					},
					..Default::default()
				};

				assert_noop!(
					MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)),
					Error::<Runtime>::SignedQueueFull,
				);

				// This is however 30% better and should therefore be accepted.
				solution = RawSolution {
					score: ElectionScore {
						minimal_stake: 5u128,
						sum_stake: 0u128,
						sum_stake_squared: 7u128,
					},
					..Default::default()
				};

				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
				assert_eq!(
					multi_phase_events(),
					vec![
						Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
						Event::SolutionStored {
							compute: ElectionCompute::Signed,
							origin: Some(99),
							prev_ejected: false
						},
						Event::SolutionStored {
							compute: ElectionCompute::Signed,
							origin: Some(99),
							prev_ejected: true
						}
					]
				);
			})
	}

	#[test]
	fn weakest_is_removed_if_better_provided() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			for s in 0..SignedMaxSubmissions::get() {
				let account = 99 + s as u64;
				Balances::make_free_balance_be(&account, 100);
				// score is always getting better
				let solution = RawSolution {
					score: ElectionScore { minimal_stake: (5 + s).into(), ..Default::default() },
					..Default::default()
				};
				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(account), Box::new(solution)));
				assert_eq!(balances(&account), (95, 5));
			}

			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| s.raw_solution.score.minimal_stake)
					.collect::<Vec<_>>(),
				vec![5, 6, 7, 8, 9]
			);

			// better.
			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 20, ..Default::default() },
				..Default::default()
			};
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(999), Box::new(solution)));

			// the one with score 5 was rejected, the new one inserted.
			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| s.raw_solution.score.minimal_stake)
					.collect::<Vec<_>>(),
				vec![6, 7, 8, 9, 20]
			);

			// the submitter of the ejected solution does *not* get a call fee refund
			assert_eq!(balances(&(99 + 0)), (100, 0));
		})
	}

	#[test]
	fn replace_weakest_by_score_works() {
		ExtBuilder::default().signed_max_submission(3).build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			for s in 1..SignedMaxSubmissions::get() {
				// score is always getting better
				let solution = RawSolution {
					score: ElectionScore { minimal_stake: (5 + s).into(), ..Default::default() },
					..Default::default()
				};
				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
			}

			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 4, ..Default::default() },
				..Default::default()
			};
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));

			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| s.raw_solution.score.minimal_stake)
					.collect::<Vec<_>>(),
				vec![4, 6, 7],
			);

			// better.
			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 5, ..Default::default() },
				..Default::default()
			};
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));

			// the one with score 5 was rejected, the new one inserted.
			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| s.raw_solution.score.minimal_stake)
					.collect::<Vec<_>>(),
				vec![5, 6, 7],
			);
		})
	}

	#[test]
	fn early_ejected_solution_gets_bond_back() {
		ExtBuilder::default().signed_deposit(2, 0, 0).build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			for s in 0..SignedMaxSubmissions::get() {
				// score is always getting better
				let solution = RawSolution {
					score: ElectionScore { minimal_stake: (5 + s).into(), ..Default::default() },
					..Default::default()
				};
				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
			}

			assert_eq!(balances(&99).1, 2 * 5);
			assert_eq!(balances(&999).1, 0);

			// better.
			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 20, ..Default::default() },
				..Default::default()
			};
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(999), Box::new(solution)));

			// got one bond back.
			assert_eq!(balances(&99).1, 2 * 4);
			assert_eq!(balances(&999).1, 2);
		})
	}

	#[test]
	fn equally_good_solution_is_not_accepted_when_queue_full() {
		// because in ordering of solutions, an older solution has higher priority and should stay.
		ExtBuilder::default().signed_max_submission(3).build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			for i in 0..SignedMaxSubmissions::get() {
				let solution = RawSolution {
					score: ElectionScore { minimal_stake: (5 + i).into(), ..Default::default() },
					..Default::default()
				};
				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
			}

			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| s.raw_solution.score.minimal_stake)
					.collect::<Vec<_>>(),
				vec![5, 6, 7]
			);

			// 5 is not accepted. This will only cause processing with no benefit.
			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 5, ..Default::default() },
				..Default::default()
			};
			assert_noop!(
				MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)),
				Error::<Runtime>::SignedQueueFull,
			);
		})
	}

	#[test]
	fn equally_good_solution_is_accepted_when_queue_not_full() {
		// because in ordering of solutions, an older solution has higher priority and should stay.
		ExtBuilder::default().signed_max_submission(3).build_and_execute(|| {
			roll_to(15);
			assert!(MultiPhase::current_phase().is_signed());

			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 5, ..Default::default() },
				..Default::default()
			};
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));

			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| (s.who, s.raw_solution.score.minimal_stake,))
					.collect::<Vec<_>>(),
				vec![(99, 5)]
			);

			roll_to(16);
			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 5, ..Default::default() },
				..Default::default()
			};
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(999), Box::new(solution)));

			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| (s.who, s.raw_solution.score.minimal_stake,))
					.collect::<Vec<_>>(),
				vec![(999, 5), (99, 5)]
			);

			let solution = RawSolution {
				score: ElectionScore { minimal_stake: 6, ..Default::default() },
				..Default::default()
			};
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(9999), Box::new(solution)));

			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| (s.who, s.raw_solution.score.minimal_stake,))
					.collect::<Vec<_>>(),
				vec![(999, 5), (99, 5), (9999, 6)]
			);
		})
	}

	#[test]
	fn all_equal_score() {
		// because in ordering of solutions, an older solution has higher priority and should stay.
		ExtBuilder::default().signed_max_submission(3).build_and_execute(|| {
			roll_to(15);
			assert!(MultiPhase::current_phase().is_signed());

			for i in 0..SignedMaxSubmissions::get() {
				roll_to((15 + i).into());
				let solution = raw_solution();
				assert_ok!(MultiPhase::submit(
					RuntimeOrigin::signed(100 + i as AccountId),
					Box::new(solution)
				));
			}

			assert_eq!(
				MultiPhase::signed_submissions()
					.iter()
					.map(|s| (s.who, s.raw_solution.score.minimal_stake))
					.collect::<Vec<_>>(),
				vec![(102, 40), (101, 40), (100, 40)]
			);

			roll_to(25);

			// The first one that will actually get verified is the last one.
			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(100),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(101),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(102),
						prev_ejected: false
					},
					Event::Rewarded { account: 100, value: 7 },
					Event::PhaseTransitioned {
						from: Phase::Signed,
						to: Phase::Unsigned((true, 25)),
						round: 1
					},
				]
			);
		})
	}

	#[test]
	fn all_in_one_signed_submission_scenario() {
		// a combination of:
		// - good_solution_is_rewarded
		// - bad_solution_is_slashed
		// - suppressed_solution_gets_bond_back
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			assert_eq!(balances(&99), (100, 0));
			assert_eq!(balances(&999), (100, 0));
			assert_eq!(balances(&9999), (100, 0));

			let solution = raw_solution();

			// submit a correct one.
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution.clone())));

			// make the solution invalidly better and submit. This ought to be slashed.
			let mut solution_999 = solution.clone();
			solution_999.score.minimal_stake += 1;
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(999), Box::new(solution_999)));

			// make the solution invalidly worse and submit. This ought to be suppressed and
			// returned.
			let mut solution_9999 = solution.clone();
			solution_9999.score.minimal_stake -= 1;
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(9999), Box::new(solution_9999)));

			assert_eq!(
				MultiPhase::signed_submissions().iter().map(|x| x.who).collect::<Vec<_>>(),
				vec![9999, 99, 999]
			);

			// _some_ good solution was stored.
			assert!(MultiPhase::finalize_signed_phase());

			// 99 is rewarded.
			assert_eq!(balances(&99), (100 + 7 + 8, 0));
			// 999 is slashed.
			assert_eq!(balances(&999), (95, 0));
			// 9999 gets everything back, including the call fee.
			assert_eq!(balances(&9999), (100 + 8, 0));
			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(99),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(999),
						prev_ejected: false
					},
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(9999),
						prev_ejected: false
					},
					Event::Slashed { account: 999, value: 5 },
					Event::Rewarded { account: 99, value: 7 }
				]
			);
		})
	}

	#[test]
	fn cannot_consume_too_much_future_weight() {
		ExtBuilder::default()
			.signed_weight(Weight::from_parts(40, u64::MAX))
			.mock_weight_info(MockedWeightInfo::Basic)
			.build_and_execute(|| {
				roll_to_signed();
				assert!(MultiPhase::current_phase().is_signed());

				let (raw, witness) = MultiPhase::mine_solution().unwrap();
				let solution_weight = <Runtime as MinerConfig>::solution_weight(
					witness.voters,
					witness.targets,
					raw.solution.voter_count() as u32,
					raw.solution.unique_targets().len() as u32,
				);
				// default solution will have 5 edges (5 * 5 + 10)
				assert_eq!(solution_weight, Weight::from_parts(35, 0));
				assert_eq!(raw.solution.voter_count(), 5);
				assert_eq!(
					<Runtime as Config>::SignedMaxWeight::get(),
					Weight::from_parts(40, u64::MAX)
				);

				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(raw.clone())));

				<SignedMaxWeight>::set(Weight::from_parts(30, u64::MAX));

				// note: resubmitting the same solution is technically okay as long as the queue has
				// space.
				assert_noop!(
					MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(raw)),
					Error::<Runtime>::SignedTooMuchWeight,
				);
			})
	}

	#[test]
	fn insufficient_deposit_does_not_store_submission() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			let solution = raw_solution();

			assert_eq!(balances(&123), (0, 0));
			assert_noop!(
				MultiPhase::submit(RuntimeOrigin::signed(123), Box::new(solution)),
				Error::<Runtime>::SignedCannotPayDeposit,
			);

			assert_eq!(balances(&123), (0, 0));
		})
	}

	// given a full queue, and a solution which _should_ be allowed in, but the proposer of this
	// new solution has insufficient deposit, we should not modify storage at all
	#[test]
	fn insufficient_deposit_with_full_queue_works_properly() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			for s in 0..SignedMaxSubmissions::get() {
				// score is always getting better
				let solution = RawSolution {
					score: ElectionScore { minimal_stake: (5 + s).into(), ..Default::default() },
					..Default::default()
				};
				assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
			}

			// this solution has a higher score than any in the queue
			let solution = RawSolution {
				score: ElectionScore {
					minimal_stake: (5 + SignedMaxSubmissions::get()).into(),
					..Default::default()
				},
				..Default::default()
			};

			assert_eq!(balances(&123), (0, 0));
			assert_noop!(
				MultiPhase::submit(RuntimeOrigin::signed(123), Box::new(solution)),
				Error::<Runtime>::SignedCannotPayDeposit,
			);

			assert_eq!(balances(&123), (0, 0));
		})
	}

	#[test]
	fn finalize_signed_phase_is_idempotent_given_no_submissions() {
		ExtBuilder::default().build_and_execute(|| {
			for block_number in 0..25 {
				roll_to(block_number);

				assert_eq!(SignedSubmissions::<Runtime>::decode_len().unwrap_or_default(), 0);
				assert_storage_noop!(MultiPhase::finalize_signed_phase_internal());
			}
		})
	}

	#[test]
	fn finalize_signed_phase_is_idempotent_given_submissions() {
		ExtBuilder::default().build_and_execute(|| {
			roll_to_signed();
			assert!(MultiPhase::current_phase().is_signed());

			let solution = raw_solution();

			// submit a correct one.
			assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));

			// _some_ good solution was stored.
			assert!(MultiPhase::finalize_signed_phase());

			// calling it again doesn't change anything
			assert_storage_noop!(MultiPhase::finalize_signed_phase());

			assert_eq!(
				multi_phase_events(),
				vec![
					Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 },
					Event::SolutionStored {
						compute: ElectionCompute::Signed,
						origin: Some(99),
						prev_ejected: false
					},
					Event::Rewarded { account: 99, value: 7 }
				]
			);
		})
	}
}