1
// Copyright 2019-2022 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! # Parachain Staking
18
//! Minimal staking pallet that implements collator selection by total backed stake.
19
//! The main difference between this pallet and `frame/pallet-staking` is that this pallet
20
//! uses direct delegation. Delegators choose exactly who they delegate and with what stake.
21
//! This is different from `frame/pallet-staking` where delegators approval vote and run Phragmen.
22
//!
23
//! ### Rules
24
//! There is a new round every `<Round<T>>::get().length` blocks.
25
//!
26
//! At the start of every round,
27
//! * issuance is calculated for collators (and their delegators) for block authoring
28
//! `T::RewardPaymentDelay` rounds ago
29
//! * a new set of collators is chosen from the candidates
30
//!
31
//! Immediately following a round change, payments are made once-per-block until all payments have
32
//! been made. In each such block, one collator is chosen for a rewards payment and is paid along
33
//! with each of its top `T::MaxTopDelegationsPerCandidate` delegators.
34
//!
35
//! To join the set of candidates, call `join_candidates` with `bond >= MinCandidateStk`.
36
//! To leave the set of candidates, call `schedule_leave_candidates`. If the call succeeds,
37
//! the collator is removed from the pool of candidates so they cannot be selected for future
38
//! collator sets, but they are not unbonded until their exit request is executed. Any signed
39
//! account may trigger the exit `T::LeaveCandidatesDelay` rounds after the round in which the
40
//! original request was made.
41
//!
42
//! To join the set of delegators, call `delegate` and pass in an account that is
43
//! already a collator candidate and `bond >= MinDelegation`. Each delegator can delegate up to
44
//! `T::MaxDelegationsPerDelegator` collator candidates by calling `delegate`.
45
//!
46
//! To revoke a delegation, call `revoke_delegation` with the collator candidate's account.
47
//! To leave the set of delegators and revoke all delegations, call `leave_delegators`.
48

            
49
#![cfg_attr(not(feature = "std"), no_std)]
50

            
51
mod auto_compound;
52
mod delegation_requests;
53
pub mod inflation;
54
pub mod migrations;
55
pub mod traits;
56
pub mod types;
57
pub mod weights;
58

            
59
#[cfg(any(test, feature = "runtime-benchmarks"))]
60
mod benchmarks;
61
#[cfg(test)]
62
mod mock;
63
mod set;
64
#[cfg(test)]
65
mod tests;
66

            
67
use frame_support::pallet;
68
pub use inflation::{InflationInfo, Range};
69
pub use weights::WeightInfo;
70

            
71
pub use auto_compound::{AutoCompoundConfig, AutoCompoundDelegations};
72
pub use delegation_requests::{CancelledScheduledRequest, DelegationAction, ScheduledRequest};
73
pub use pallet::*;
74
pub use traits::*;
75
pub use types::*;
76
pub use RoundIndex;
77

            
78
10233
#[pallet]
79
pub mod pallet {
80
	use crate::delegation_requests::{
81
		CancelledScheduledRequest, DelegationAction, ScheduledRequest,
82
	};
83
	use crate::{set::BoundedOrderedSet, traits::*, types::*, InflationInfo, Range, WeightInfo};
84
	use crate::{AutoCompoundConfig, AutoCompoundDelegations};
85
	use frame_support::fail;
86
	use frame_support::pallet_prelude::*;
87
	use frame_support::traits::{
88
		tokens::WithdrawReasons, Currency, Get, Imbalance, LockIdentifier, LockableCurrency,
89
		ReservableCurrency,
90
	};
91
	use frame_system::pallet_prelude::*;
92
	use sp_consensus_slots::Slot;
93
	use sp_runtime::{
94
		traits::{Saturating, Zero},
95
		DispatchErrorWithPostInfo, Perbill, Percent,
96
	};
97
	use sp_std::{collections::btree_map::BTreeMap, prelude::*};
98

            
99
	/// Pallet for parachain staking
100
32
	#[pallet::pallet]
101
	#[pallet::without_storage_info]
102
	pub struct Pallet<T>(PhantomData<T>);
103

            
104
	pub type RoundIndex = u32;
105
	type RewardPoint = u32;
106
	pub type BalanceOf<T> =
107
		<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
108

            
109
	pub const COLLATOR_LOCK_ID: LockIdentifier = *b"stkngcol";
110
	pub const DELEGATOR_LOCK_ID: LockIdentifier = *b"stkngdel";
111

            
112
	/// A hard limit for weight computation purposes for the max candidates that _could_
113
	/// theoretically exist.
114
	pub const MAX_CANDIDATES: u32 = 200;
115

            
116
	/// Configuration trait of this pallet.
117
	#[pallet::config]
118
	pub trait Config: frame_system::Config {
119
		/// Overarching event type
120
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
121
		/// The currency type
122
		type Currency: Currency<Self::AccountId>
123
			+ ReservableCurrency<Self::AccountId>
124
			+ LockableCurrency<Self::AccountId>;
125
		/// The origin for monetary governance
126
		type MonetaryGovernanceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
127
		/// Minimum number of blocks per round
128
		#[pallet::constant]
129
		type MinBlocksPerRound: Get<u32>;
130
		/// If a collator doesn't produce any block on this number of rounds, it is notified as inactive.
131
		/// This value must be less than or equal to RewardPaymentDelay.
132
		#[pallet::constant]
133
		type MaxOfflineRounds: Get<u32>;
134
		/// Number of rounds that candidates remain bonded before exit request is executable
135
		#[pallet::constant]
136
		type LeaveCandidatesDelay: Get<RoundIndex>;
137
		/// Number of rounds candidate requests to decrease self-bond must wait to be executable
138
		#[pallet::constant]
139
		type CandidateBondLessDelay: Get<RoundIndex>;
140
		/// Number of rounds that delegators remain bonded before exit request is executable
141
		#[pallet::constant]
142
		type LeaveDelegatorsDelay: Get<RoundIndex>;
143
		/// Number of rounds that delegations remain bonded before revocation request is executable
144
		#[pallet::constant]
145
		type RevokeDelegationDelay: Get<RoundIndex>;
146
		/// Number of rounds that delegation less requests must wait before executable
147
		#[pallet::constant]
148
		type DelegationBondLessDelay: Get<RoundIndex>;
149
		/// Number of rounds after which block authors are rewarded
150
		#[pallet::constant]
151
		type RewardPaymentDelay: Get<RoundIndex>;
152
		/// Minimum number of selected candidates every round
153
		#[pallet::constant]
154
		type MinSelectedCandidates: Get<u32>;
155
		/// Maximum top delegations counted per candidate
156
		#[pallet::constant]
157
		type MaxTopDelegationsPerCandidate: Get<u32>;
158
		/// Maximum bottom delegations (not counted) per candidate
159
		#[pallet::constant]
160
		type MaxBottomDelegationsPerCandidate: Get<u32>;
161
		/// Maximum delegations per delegator
162
		#[pallet::constant]
163
		type MaxDelegationsPerDelegator: Get<u32>;
164
		/// Minimum stake required for any account to be a collator candidate
165
		#[pallet::constant]
166
		type MinCandidateStk: Get<BalanceOf<Self>>;
167
		/// Minimum stake for any registered on-chain account to delegate
168
		#[pallet::constant]
169
		type MinDelegation: Get<BalanceOf<Self>>;
170
		/// Get the current block author
171
		type BlockAuthor: Get<Self::AccountId>;
172
		/// Handler to notify the runtime when a collator is paid.
173
		/// If you don't need it, you can specify the type `()`.
174
		type OnCollatorPayout: OnCollatorPayout<Self::AccountId, BalanceOf<Self>>;
175
		/// Handler to distribute a collator's reward.
176
		/// To use the default implementation of minting rewards, specify the type `()`.
177
		type PayoutCollatorReward: PayoutCollatorReward<Self>;
178
		/// Handler to notify the runtime when a collator is inactive.
179
		/// The default behavior is to mark the collator as offline.
180
		/// If you need to use the default implementation, specify the type `()`.
181
		type OnInactiveCollator: OnInactiveCollator<Self>;
182
		/// Handler to notify the runtime when a new round begin.
183
		/// If you don't need it, you can specify the type `()`.
184
		type OnNewRound: OnNewRound;
185
		/// Get the current slot number
186
		type SlotProvider: Get<Slot>;
187
		/// Get the slot duration in milliseconds
188
		#[pallet::constant]
189
		type SlotDuration: Get<u64>;
190
		/// Get the average time beetween 2 blocks in milliseconds
191
		#[pallet::constant]
192
		type BlockTime: Get<u64>;
193
		/// Maximum candidates
194
		#[pallet::constant]
195
		type MaxCandidates: Get<u32>;
196
		/// Weight information for extrinsics in this pallet.
197
		type WeightInfo: WeightInfo;
198
	}
199

            
200
8797
	#[pallet::error]
201
	pub enum Error<T> {
202
		DelegatorDNE,
203
		DelegatorDNEinTopNorBottom,
204
		DelegatorDNEInDelegatorSet,
205
		CandidateDNE,
206
		DelegationDNE,
207
		DelegatorExists,
208
		CandidateExists,
209
		CandidateBondBelowMin,
210
		InsufficientBalance,
211
		DelegatorBondBelowMin,
212
		DelegationBelowMin,
213
		AlreadyOffline,
214
		AlreadyActive,
215
		DelegatorAlreadyLeaving,
216
		DelegatorNotLeaving,
217
		DelegatorCannotLeaveYet,
218
		CannotDelegateIfLeaving,
219
		CandidateAlreadyLeaving,
220
		CandidateNotLeaving,
221
		CandidateCannotLeaveYet,
222
		CannotGoOnlineIfLeaving,
223
		ExceedMaxDelegationsPerDelegator,
224
		AlreadyDelegatedCandidate,
225
		InvalidSchedule,
226
		CannotSetBelowMin,
227
		RoundLengthMustBeGreaterThanTotalSelectedCollators,
228
		NoWritingSameValue,
229
		TotalInflationDistributionPercentExceeds100,
230
		TooLowCandidateCountWeightHintJoinCandidates,
231
		TooLowCandidateCountWeightHintCancelLeaveCandidates,
232
		TooLowCandidateCountToLeaveCandidates,
233
		TooLowDelegationCountToDelegate,
234
		TooLowCandidateDelegationCountToDelegate,
235
		TooLowCandidateDelegationCountToLeaveCandidates,
236
		TooLowDelegationCountToLeaveDelegators,
237
		PendingCandidateRequestsDNE,
238
		PendingCandidateRequestAlreadyExists,
239
		PendingCandidateRequestNotDueYet,
240
		PendingDelegationRequestDNE,
241
		PendingDelegationRequestAlreadyExists,
242
		PendingDelegationRequestNotDueYet,
243
		CannotDelegateLessThanOrEqualToLowestBottomWhenFull,
244
		PendingDelegationRevoke,
245
		TooLowDelegationCountToAutoCompound,
246
		TooLowCandidateAutoCompoundingDelegationCountToAutoCompound,
247
		TooLowCandidateAutoCompoundingDelegationCountToDelegate,
248
		TooLowCollatorCountToNotifyAsInactive,
249
		CannotBeNotifiedAsInactive,
250
		TooLowCandidateAutoCompoundingDelegationCountToLeaveCandidates,
251
		TooLowCandidateCountWeightHint,
252
		TooLowCandidateCountWeightHintGoOffline,
253
		CandidateLimitReached,
254
		CannotSetAboveMaxCandidates,
255
		RemovedCall,
256
		MarkingOfflineNotEnabled,
257
		CurrentRoundTooLow,
258
	}
259

            
260
	#[pallet::event]
261
3474
	#[pallet::generate_deposit(pub(crate) fn deposit_event)]
262
	pub enum Event<T: Config> {
263
100
		/// Started new round.
264
		NewRound {
265
			starting_block: BlockNumberFor<T>,
266
			round: RoundIndex,
267
			selected_collators_number: u32,
268
			total_balance: BalanceOf<T>,
269
		},
270
9
		/// Account joined the set of collator candidates.
271
		JoinedCollatorCandidates {
272
			account: T::AccountId,
273
			amount_locked: BalanceOf<T>,
274
			new_total_amt_locked: BalanceOf<T>,
275
		},
276
208
		/// Candidate selected for collators. Total Exposed Amount includes all delegations.
277
		CollatorChosen {
278
			round: RoundIndex,
279
			collator_account: T::AccountId,
280
			total_exposed_amount: BalanceOf<T>,
281
		},
282
5
		/// Candidate requested to decrease a self bond.
283
		CandidateBondLessRequested {
284
			candidate: T::AccountId,
285
			amount_to_decrease: BalanceOf<T>,
286
			execute_round: RoundIndex,
287
		},
288
2
		/// Candidate has increased a self bond.
289
		CandidateBondedMore {
290
			candidate: T::AccountId,
291
			amount: BalanceOf<T>,
292
			new_total_bond: BalanceOf<T>,
293
		},
294
2
		/// Candidate has decreased a self bond.
295
		CandidateBondedLess {
296
			candidate: T::AccountId,
297
			amount: BalanceOf<T>,
298
			new_bond: BalanceOf<T>,
299
		},
300
4
		/// Candidate temporarily leave the set of collator candidates without unbonding.
301
		CandidateWentOffline { candidate: T::AccountId },
302
2
		/// Candidate rejoins the set of collator candidates.
303
		CandidateBackOnline { candidate: T::AccountId },
304
8
		/// Candidate has requested to leave the set of candidates.
305
		CandidateScheduledExit {
306
			exit_allowed_round: RoundIndex,
307
			candidate: T::AccountId,
308
			scheduled_exit: RoundIndex,
309
		},
310
2
		/// Cancelled request to leave the set of candidates.
311
		CancelledCandidateExit { candidate: T::AccountId },
312
2
		/// Cancelled request to decrease candidate's bond.
313
		CancelledCandidateBondLess {
314
			candidate: T::AccountId,
315
			amount: BalanceOf<T>,
316
			execute_round: RoundIndex,
317
		},
318
5
		/// Candidate has left the set of candidates.
319
		CandidateLeft {
320
			ex_candidate: T::AccountId,
321
			unlocked_amount: BalanceOf<T>,
322
			new_total_amt_locked: BalanceOf<T>,
323
		},
324
9
		/// Delegator requested to decrease a bond for the collator candidate.
325
		DelegationDecreaseScheduled {
326
			delegator: T::AccountId,
327
			candidate: T::AccountId,
328
			amount_to_decrease: BalanceOf<T>,
329
			execute_round: RoundIndex,
330
		},
331
		// Delegation increased.
332
17
		DelegationIncreased {
333
			delegator: T::AccountId,
334
			candidate: T::AccountId,
335
			amount: BalanceOf<T>,
336
			in_top: bool,
337
		},
338
		// Delegation decreased.
339
5
		DelegationDecreased {
340
			delegator: T::AccountId,
341
			candidate: T::AccountId,
342
			amount: BalanceOf<T>,
343
			in_top: bool,
344
		},
345
		/// Delegator requested to leave the set of delegators.
346
		DelegatorExitScheduled {
347
			round: RoundIndex,
348
			delegator: T::AccountId,
349
			scheduled_exit: RoundIndex,
350
		},
351
14
		/// Delegator requested to revoke delegation.
352
		DelegationRevocationScheduled {
353
			round: RoundIndex,
354
			delegator: T::AccountId,
355
			candidate: T::AccountId,
356
			scheduled_exit: RoundIndex,
357
		},
358
10
		/// Delegator has left the set of delegators.
359
		DelegatorLeft {
360
			delegator: T::AccountId,
361
			unstaked_amount: BalanceOf<T>,
362
		},
363
9
		/// Delegation revoked.
364
		DelegationRevoked {
365
			delegator: T::AccountId,
366
			candidate: T::AccountId,
367
			unstaked_amount: BalanceOf<T>,
368
		},
369
5
		/// Delegation kicked.
370
		DelegationKicked {
371
			delegator: T::AccountId,
372
			candidate: T::AccountId,
373
			unstaked_amount: BalanceOf<T>,
374
		},
375
		/// Cancelled a pending request to exit the set of delegators.
376
		DelegatorExitCancelled { delegator: T::AccountId },
377
4
		/// Cancelled request to change an existing delegation.
378
		CancelledDelegationRequest {
379
			delegator: T::AccountId,
380
			cancelled_request: CancelledScheduledRequest<BalanceOf<T>>,
381
			collator: T::AccountId,
382
		},
383
30
		/// New delegation (increase of the existing one).
384
		Delegation {
385
			delegator: T::AccountId,
386
			locked_amount: BalanceOf<T>,
387
			candidate: T::AccountId,
388
			delegator_position: DelegatorAdded<BalanceOf<T>>,
389
			auto_compound: Percent,
390
		},
391
9
		/// Delegation from candidate state has been remove.
392
		DelegatorLeftCandidate {
393
			delegator: T::AccountId,
394
			candidate: T::AccountId,
395
			unstaked_amount: BalanceOf<T>,
396
			total_candidate_staked: BalanceOf<T>,
397
		},
398
128
		/// Paid the account (delegator or collator) the balance as liquid rewards.
399
		Rewarded {
400
			account: T::AccountId,
401
			rewards: BalanceOf<T>,
402
		},
403
		/// Transferred to account which holds funds reserved for parachain bond.
404
		InflationDistributed {
405
			index: u32,
406
			account: T::AccountId,
407
			value: BalanceOf<T>,
408
		},
409
4
		InflationDistributionConfigUpdated {
410
			old: InflationDistributionConfig<T::AccountId>,
411
			new: InflationDistributionConfig<T::AccountId>,
412
		},
413
1
		/// Annual inflation input (first 3) was used to derive new per-round inflation (last 3)
414
		InflationSet {
415
			annual_min: Perbill,
416
			annual_ideal: Perbill,
417
			annual_max: Perbill,
418
			round_min: Perbill,
419
			round_ideal: Perbill,
420
			round_max: Perbill,
421
		},
422
1
		/// Staking expectations set.
423
		StakeExpectationsSet {
424
			expect_min: BalanceOf<T>,
425
			expect_ideal: BalanceOf<T>,
426
			expect_max: BalanceOf<T>,
427
		},
428
1
		/// Set total selected candidates to this value.
429
		TotalSelectedSet { old: u32, new: u32 },
430
1
		/// Set collator commission to this value.
431
		CollatorCommissionSet { old: Perbill, new: Perbill },
432
1
		/// Set blocks per round
433
		BlocksPerRoundSet {
434
			current_round: RoundIndex,
435
			first_block: BlockNumberFor<T>,
436
			old: u32,
437
			new: u32,
438
			new_per_round_inflation_min: Perbill,
439
			new_per_round_inflation_ideal: Perbill,
440
			new_per_round_inflation_max: Perbill,
441
		},
442
6
		/// Auto-compounding reward percent was set for a delegation.
443
		AutoCompoundSet {
444
			candidate: T::AccountId,
445
			delegator: T::AccountId,
446
			value: Percent,
447
		},
448
3
		/// Compounded a portion of rewards towards the delegation.
449
		Compounded {
450
			candidate: T::AccountId,
451
			delegator: T::AccountId,
452
			amount: BalanceOf<T>,
453
		},
454
	}
455

            
456
60156
	#[pallet::hooks]
457
	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
458
30673
		fn on_initialize(n: BlockNumberFor<T>) -> Weight {
459
30673
			let mut weight = <T as Config>::WeightInfo::base_on_initialize();
460
30673

            
461
30673
			let mut round = <Round<T>>::get();
462
30673
			if round.should_update(n) {
463
258
				// fetch current slot number
464
258
				let current_slot: u64 = T::SlotProvider::get().into();
465
258

            
466
258
				// account for SlotProvider read
467
258
				weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 0));
468
258

            
469
258
				// Compute round duration in slots
470
258
				let round_duration = (current_slot.saturating_sub(round.first_slot))
471
258
					.saturating_mul(T::SlotDuration::get());
472
258

            
473
258
				// mutate round
474
258
				round.update(n, current_slot);
475
258
				// notify that new round begin
476
258
				weight = weight.saturating_add(T::OnNewRound::on_new_round(round.current));
477
258
				// pay all stakers for T::RewardPaymentDelay rounds ago
478
258
				weight =
479
258
					weight.saturating_add(Self::prepare_staking_payouts(round, round_duration));
480
258
				// select top collator candidates for next round
481
258
				let (extra_weight, collator_count, _delegation_count, total_staked) =
482
258
					Self::select_top_candidates(round.current);
483
258
				weight = weight.saturating_add(extra_weight);
484
258
				// start next round
485
258
				<Round<T>>::put(round);
486
258
				Self::deposit_event(Event::NewRound {
487
258
					starting_block: round.first,
488
258
					round: round.current,
489
258
					selected_collators_number: collator_count,
490
258
					total_balance: total_staked,
491
258
				});
492
258
				// account for Round write
493
258
				weight = weight.saturating_add(T::DbWeight::get().reads_writes(0, 1));
494
30415
			} else {
495
30415
				weight = weight.saturating_add(Self::handle_delayed_payouts(round.current));
496
30415
			}
497

            
498
			// add on_finalize weight
499
			//   read:  Author, Points, AwardedPts
500
			//   write: Points, AwardedPts
501
30673
			weight = weight.saturating_add(T::DbWeight::get().reads_writes(3, 2));
502
30673
			weight
503
30673
		}
504
29483
		fn on_finalize(_n: BlockNumberFor<T>) {
505
29483
			Self::award_points_to_block_author();
506
29483
		}
507
	}
508

            
509
1188
	#[pallet::storage]
510
	#[pallet::getter(fn collator_commission)]
511
	/// Commission percent taken off of rewards for all collators
512
	type CollatorCommission<T: Config> = StorageValue<_, Perbill, ValueQuery>;
513

            
514
2642
	#[pallet::storage]
515
	#[pallet::getter(fn total_selected)]
516
	/// The total candidates selected every round
517
	pub(crate) type TotalSelected<T: Config> = StorageValue<_, u32, ValueQuery>;
518

            
519
1322
	#[pallet::storage]
520
	#[pallet::getter(fn inflation_distribution_info)]
521
	/// Inflation distribution configuration, including accounts that should receive inflation
522
	/// before it is distributed to collators and delegators.
523
	///
524
	/// The sum of the distribution percents must be less than or equal to 100.
525
	pub(crate) type InflationDistributionInfo<T: Config> =
526
		StorageValue<_, InflationDistributionConfig<T::AccountId>, ValueQuery>;
527

            
528
122590
	#[pallet::storage]
529
	#[pallet::getter(fn round)]
530
	/// Current round index and next round scheduled transition
531
	pub type Round<T: Config> = StorageValue<_, RoundInfo<BlockNumberFor<T>>, ValueQuery>;
532

            
533
29506
	#[pallet::storage]
534
	#[pallet::getter(fn delegator_state)]
535
	/// Get delegator state associated with an account if account is delegating else None
536
	pub(crate) type DelegatorState<T: Config> = StorageMap<
537
		_,
538
		Twox64Concat,
539
		T::AccountId,
540
		Delegator<T::AccountId, BalanceOf<T>>,
541
		OptionQuery,
542
	>;
543

            
544
22703
	#[pallet::storage]
545
	#[pallet::getter(fn candidate_info)]
546
	/// Get collator candidate info associated with an account if account is candidate else None
547
	pub(crate) type CandidateInfo<T: Config> =
548
		StorageMap<_, Twox64Concat, T::AccountId, CandidateMetadata<BalanceOf<T>>, OptionQuery>;
549

            
550
	pub struct AddGet<T, R> {
551
		_phantom: PhantomData<(T, R)>,
552
	}
553
	impl<T, R> Get<u32> for AddGet<T, R>
554
	where
555
		T: Get<u32>,
556
		R: Get<u32>,
557
	{
558
377
		fn get() -> u32 {
559
377
			T::get() + R::get()
560
377
		}
561
	}
562

            
563
	/// Stores outstanding delegation requests per collator.
564
1361
	#[pallet::storage]
565
	#[pallet::getter(fn delegation_scheduled_requests)]
566
	pub(crate) type DelegationScheduledRequests<T: Config> = StorageMap<
567
		_,
568
		Blake2_128Concat,
569
		T::AccountId,
570
		BoundedVec<
571
			ScheduledRequest<T::AccountId, BalanceOf<T>>,
572
			AddGet<T::MaxTopDelegationsPerCandidate, T::MaxBottomDelegationsPerCandidate>,
573
		>,
574
		ValueQuery,
575
	>;
576

            
577
	/// Stores auto-compounding configuration per collator.
578
1121
	#[pallet::storage]
579
	#[pallet::getter(fn auto_compounding_delegations)]
580
	pub(crate) type AutoCompoundingDelegations<T: Config> = StorageMap<
581
		_,
582
		Blake2_128Concat,
583
		T::AccountId,
584
		BoundedVec<
585
			AutoCompoundConfig<T::AccountId>,
586
			AddGet<T::MaxTopDelegationsPerCandidate, T::MaxBottomDelegationsPerCandidate>,
587
		>,
588
		ValueQuery,
589
	>;
590

            
591
2626
	#[pallet::storage]
592
	#[pallet::getter(fn top_delegations)]
593
	/// Top delegations for collator candidate
594
	pub(crate) type TopDelegations<T: Config> = StorageMap<
595
		_,
596
		Twox64Concat,
597
		T::AccountId,
598
		Delegations<T::AccountId, BalanceOf<T>>,
599
		OptionQuery,
600
	>;
601

            
602
1089
	#[pallet::storage]
603
	#[pallet::getter(fn bottom_delegations)]
604
	/// Bottom delegations for collator candidate
605
	pub(crate) type BottomDelegations<T: Config> = StorageMap<
606
		_,
607
		Twox64Concat,
608
		T::AccountId,
609
		Delegations<T::AccountId, BalanceOf<T>>,
610
		OptionQuery,
611
	>;
612

            
613
1761
	#[pallet::storage]
614
	#[pallet::getter(fn selected_candidates)]
615
	/// The collator candidates selected for the current round
616
	type SelectedCandidates<T: Config> =
617
		StorageValue<_, BoundedVec<T::AccountId, T::MaxCandidates>, ValueQuery>;
618

            
619
5354
	#[pallet::storage]
620
	#[pallet::getter(fn total)]
621
	/// Total capital locked by this staking pallet
622
	pub(crate) type Total<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
623

            
624
6578
	#[pallet::storage]
625
	#[pallet::getter(fn candidate_pool)]
626
	/// The pool of collator candidates, each with their total backing stake
627
	pub(crate) type CandidatePool<T: Config> = StorageValue<
628
		_,
629
		BoundedOrderedSet<Bond<T::AccountId, BalanceOf<T>>, T::MaxCandidates>,
630
		ValueQuery,
631
	>;
632

            
633
1421
	#[pallet::storage]
634
	#[pallet::getter(fn at_stake)]
635
	/// Snapshot of collator delegation stake at the start of the round
636
	pub type AtStake<T: Config> = StorageDoubleMap<
637
		_,
638
		Twox64Concat,
639
		RoundIndex,
640
		Twox64Concat,
641
		T::AccountId,
642
		CollatorSnapshot<T::AccountId, BalanceOf<T>>,
643
		OptionQuery,
644
	>;
645

            
646
14022
	#[pallet::storage]
647
	#[pallet::getter(fn delayed_payouts)]
648
	/// Delayed payouts
649
	pub type DelayedPayouts<T: Config> =
650
		StorageMap<_, Twox64Concat, RoundIndex, DelayedPayout<BalanceOf<T>>, OptionQuery>;
651

            
652
1262
	#[pallet::storage]
653
	#[pallet::getter(fn inflation_config)]
654
	/// Inflation configuration
655
	pub type InflationConfig<T: Config> = StorageValue<_, InflationInfo<BalanceOf<T>>, ValueQuery>;
656

            
657
29969
	#[pallet::storage]
658
	#[pallet::getter(fn points)]
659
	/// Total points awarded to collators for block production in the round
660
	pub type Points<T: Config> = StorageMap<_, Twox64Concat, RoundIndex, RewardPoint, ValueQuery>;
661

            
662
59180
	#[pallet::storage]
663
	#[pallet::getter(fn awarded_pts)]
664
	/// Points for each collator per round
665
	pub type AwardedPts<T: Config> = StorageDoubleMap<
666
		_,
667
		Twox64Concat,
668
		RoundIndex,
669
		Twox64Concat,
670
		T::AccountId,
671
		RewardPoint,
672
		ValueQuery,
673
	>;
674

            
675
30
	#[pallet::storage]
676
	#[pallet::getter(fn marking_offline)]
677
	/// Killswitch to enable/disable marking offline feature.
678
	pub type EnableMarkingOffline<T: Config> = StorageValue<_, bool, ValueQuery>;
679

            
680
	#[pallet::genesis_config]
681
	pub struct GenesisConfig<T: Config> {
682
		/// Initialize balance and register all as collators: `(collator AccountId, balance Amount)`
683
		pub candidates: Vec<(T::AccountId, BalanceOf<T>)>,
684
		/// Initialize balance and make delegations:
685
		/// `(delegator AccountId, collator AccountId, delegation Amount, auto-compounding Percent)`
686
		pub delegations: Vec<(T::AccountId, T::AccountId, BalanceOf<T>, Percent)>,
687
		/// Inflation configuration
688
		pub inflation_config: InflationInfo<BalanceOf<T>>,
689
		/// Default fixed percent a collator takes off the top of due rewards
690
		pub collator_commission: Perbill,
691
		/// Default percent of inflation set aside for parachain bond every round
692
		pub parachain_bond_reserve_percent: Percent,
693
		/// Default number of blocks in a round
694
		pub blocks_per_round: u32,
695
		/// Number of selected candidates every round. Cannot be lower than MinSelectedCandidates
696
		pub num_selected_candidates: u32,
697
	}
698

            
699
	impl<T: Config> Default for GenesisConfig<T> {
700
5
		fn default() -> Self {
701
5
			Self {
702
5
				candidates: vec![],
703
5
				delegations: vec![],
704
5
				inflation_config: Default::default(),
705
5
				collator_commission: Default::default(),
706
5
				parachain_bond_reserve_percent: Default::default(),
707
5
				blocks_per_round: 1u32,
708
5
				num_selected_candidates: T::MinSelectedCandidates::get(),
709
5
			}
710
5
		}
711
	}
712

            
713
504
	#[pallet::genesis_build]
714
	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
715
509
		fn build(&self) {
716
509
			assert!(self.blocks_per_round > 0, "Blocks per round must be > 0");
717
509
			<InflationConfig<T>>::put(self.inflation_config.clone());
718
509
			let mut candidate_count = 0u32;
719
			// Initialize the candidates
720
1145
			for &(ref candidate, balance) in &self.candidates {
721
636
				assert!(
722
636
					<Pallet<T>>::get_collator_stakable_free_balance(candidate) >= balance,
723
					"Account does not have enough balance to bond as a candidate."
724
				);
725
636
				if let Err(error) = <Pallet<T>>::join_candidates(
726
636
					T::RuntimeOrigin::from(Some(candidate.clone()).into()),
727
636
					balance,
728
636
					candidate_count,
729
636
				) {
730
13
					log::warn!("Join candidates failed in genesis with error {:?}", error);
731
623
				} else {
732
623
					candidate_count = candidate_count.saturating_add(1u32);
733
623
				}
734
			}
735

            
736
509
			let mut col_delegator_count: BTreeMap<T::AccountId, u32> = BTreeMap::new();
737
509
			let mut col_auto_compound_delegator_count: BTreeMap<T::AccountId, u32> =
738
509
				BTreeMap::new();
739
509
			let mut del_delegation_count: BTreeMap<T::AccountId, u32> = BTreeMap::new();
740
			// Initialize the delegations
741
9515
			for &(ref delegator, ref target, balance, auto_compound) in &self.delegations {
742
9006
				assert!(
743
9006
					<Pallet<T>>::get_delegator_stakable_balance(delegator) >= balance,
744
					"Account does not have enough balance to place delegation."
745
				);
746
9006
				let cd_count = if let Some(x) = col_delegator_count.get(target) {
747
8780
					*x
748
				} else {
749
226
					0u32
750
				};
751
9006
				let dd_count = if let Some(x) = del_delegation_count.get(delegator) {
752
44
					*x
753
				} else {
754
8962
					0u32
755
				};
756
9006
				let cd_auto_compound_count = col_auto_compound_delegator_count
757
9006
					.get(target)
758
9006
					.cloned()
759
9006
					.unwrap_or_default();
760
9006
				if let Err(error) = <Pallet<T>>::delegate_with_auto_compound(
761
9006
					T::RuntimeOrigin::from(Some(delegator.clone()).into()),
762
9006
					target.clone(),
763
9006
					balance,
764
9006
					auto_compound,
765
9006
					cd_count,
766
9006
					cd_auto_compound_count,
767
9006
					dd_count,
768
9006
				) {
769
8472
					log::warn!("Delegate failed in genesis with error {:?}", error);
770
				} else {
771
534
					if let Some(x) = col_delegator_count.get_mut(target) {
772
312
						*x = x.saturating_add(1u32);
773
354
					} else {
774
222
						col_delegator_count.insert(target.clone(), 1u32);
775
222
					};
776
534
					if let Some(x) = del_delegation_count.get_mut(delegator) {
777
44
						*x = x.saturating_add(1u32);
778
490
					} else {
779
490
						del_delegation_count.insert(delegator.clone(), 1u32);
780
490
					};
781
534
					if !auto_compound.is_zero() {
782
4
						col_auto_compound_delegator_count
783
4
							.entry(target.clone())
784
4
							.and_modify(|x| *x = x.saturating_add(1))
785
4
							.or_insert(1);
786
530
					}
787
				}
788
			}
789
			// Set collator commission to default config
790
509
			<CollatorCommission<T>>::put(self.collator_commission);
791
509
			// Set parachain bond config to default config
792
509
			let pbr = InflationDistributionAccount {
793
509
				// must be set soon; if not => due inflation will be sent to collators/delegators
794
509
				account: T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
795
509
					.expect("infinite length input; no invalid inputs for type; qed"),
796
509
				percent: self.parachain_bond_reserve_percent,
797
509
			};
798
509
			let zeroed_account = InflationDistributionAccount {
799
509
				account: T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
800
509
					.expect("infinite length input; no invalid inputs for type; qed"),
801
509
				percent: Percent::zero(),
802
509
			};
803
509
			<InflationDistributionInfo<T>>::put::<InflationDistributionConfig<T::AccountId>>(
804
509
				[pbr, zeroed_account].into(),
805
509
			);
806
509
			// Set total selected candidates to value from config
807
509
			assert!(
808
509
				self.num_selected_candidates >= T::MinSelectedCandidates::get(),
809
				"{:?}",
810
				Error::<T>::CannotSetBelowMin
811
			);
812
509
			assert!(
813
509
				self.num_selected_candidates <= T::MaxCandidates::get(),
814
				"{:?}",
815
				Error::<T>::CannotSetAboveMaxCandidates
816
			);
817
509
			<TotalSelected<T>>::put(self.num_selected_candidates);
818
509
			// Choose top TotalSelected collator candidates
819
509
			let (_, v_count, _, total_staked) = <Pallet<T>>::select_top_candidates(1u32);
820
509
			// Start Round 1 at Block 0
821
509
			let round: RoundInfo<BlockNumberFor<T>> =
822
509
				RoundInfo::new(1u32, Zero::zero(), self.blocks_per_round, 0);
823
509
			<Round<T>>::put(round);
824
509
			<Pallet<T>>::deposit_event(Event::NewRound {
825
509
				starting_block: Zero::zero(),
826
509
				round: 1u32,
827
509
				selected_collators_number: v_count,
828
509
				total_balance: total_staked,
829
509
			});
830
509
		}
831
	}
832

            
833
84
	#[pallet::call]
834
	impl<T: Config> Pallet<T> {
835
		/// Set the expectations for total staked. These expectations determine the issuance for
836
		/// the round according to logic in `fn compute_issuance`
837
		#[pallet::call_index(0)]
838
		#[pallet::weight(<T as Config>::WeightInfo::set_staking_expectations())]
839
		pub fn set_staking_expectations(
840
			origin: OriginFor<T>,
841
			expectations: Range<BalanceOf<T>>,
842
6
		) -> DispatchResultWithPostInfo {
843
6
			T::MonetaryGovernanceOrigin::ensure_origin(origin)?;
844
5
			ensure!(expectations.is_valid(), Error::<T>::InvalidSchedule);
845
4
			let mut config = <InflationConfig<T>>::get();
846
4
			ensure!(
847
4
				config.expect != expectations,
848
1
				Error::<T>::NoWritingSameValue
849
			);
850
3
			config.set_expectations(expectations);
851
3
			Self::deposit_event(Event::StakeExpectationsSet {
852
3
				expect_min: config.expect.min,
853
3
				expect_ideal: config.expect.ideal,
854
3
				expect_max: config.expect.max,
855
3
			});
856
3
			<InflationConfig<T>>::put(config);
857
3
			Ok(().into())
858
		}
859

            
860
		/// Set the annual inflation rate to derive per-round inflation
861
		#[pallet::call_index(1)]
862
		#[pallet::weight(<T as Config>::WeightInfo::set_inflation())]
863
		pub fn set_inflation(
864
			origin: OriginFor<T>,
865
			schedule: Range<Perbill>,
866
7
		) -> DispatchResultWithPostInfo {
867
7
			T::MonetaryGovernanceOrigin::ensure_origin(origin)?;
868
5
			ensure!(schedule.is_valid(), Error::<T>::InvalidSchedule);
869
4
			let mut config = <InflationConfig<T>>::get();
870
4
			ensure!(config.annual != schedule, Error::<T>::NoWritingSameValue);
871
3
			config.annual = schedule;
872
3
			config.set_round_from_annual::<T>(schedule);
873
3
			Self::deposit_event(Event::InflationSet {
874
3
				annual_min: config.annual.min,
875
3
				annual_ideal: config.annual.ideal,
876
3
				annual_max: config.annual.max,
877
3
				round_min: config.round.min,
878
3
				round_ideal: config.round.ideal,
879
3
				round_max: config.round.max,
880
3
			});
881
3
			<InflationConfig<T>>::put(config);
882
3
			Ok(().into())
883
		}
884

            
885
		/// Deprecated: please use `set_inflation_distribution_config` instead.
886
		///
887
		///  Set the account that will hold funds set aside for parachain bond
888
		#[pallet::call_index(2)]
889
		#[pallet::weight(<T as Config>::WeightInfo::set_parachain_bond_account())]
890
		pub fn set_parachain_bond_account(
891
			origin: OriginFor<T>,
892
			new: T::AccountId,
893
6
		) -> DispatchResultWithPostInfo {
894
6
			T::MonetaryGovernanceOrigin::ensure_origin(origin.clone())?;
895
5
			let old = <InflationDistributionInfo<T>>::get().0;
896
5
			let new = InflationDistributionAccount {
897
5
				account: new,
898
5
				percent: old[0].percent.clone(),
899
5
			};
900
5
			Pallet::<T>::set_inflation_distribution_config(origin, [new, old[1].clone()].into())
901
		}
902

            
903
		/// Deprecated: please use `set_inflation_distribution_config` instead.
904
		///
905
		/// Set the percent of inflation set aside for parachain bond
906
		#[pallet::call_index(3)]
907
		#[pallet::weight(<T as Config>::WeightInfo::set_parachain_bond_reserve_percent())]
908
		pub fn set_parachain_bond_reserve_percent(
909
			origin: OriginFor<T>,
910
			new: Percent,
911
4
		) -> DispatchResultWithPostInfo {
912
4
			T::MonetaryGovernanceOrigin::ensure_origin(origin.clone())?;
913
3
			let old = <InflationDistributionInfo<T>>::get().0;
914
3
			let new = InflationDistributionAccount {
915
3
				account: old[0].account.clone(),
916
3
				percent: new,
917
3
			};
918
3
			Pallet::<T>::set_inflation_distribution_config(origin, [new, old[1].clone()].into())
919
		}
920

            
921
		/// Set the total number of collator candidates selected per round
922
		/// - changes are not applied until the start of the next round
923
		#[pallet::call_index(4)]
924
		#[pallet::weight(<T as Config>::WeightInfo::set_total_selected())]
925
11
		pub fn set_total_selected(origin: OriginFor<T>, new: u32) -> DispatchResultWithPostInfo {
926
11
			frame_system::ensure_root(origin)?;
927
10
			ensure!(
928
10
				new >= T::MinSelectedCandidates::get(),
929
1
				Error::<T>::CannotSetBelowMin
930
			);
931
9
			ensure!(
932
9
				new <= T::MaxCandidates::get(),
933
1
				Error::<T>::CannotSetAboveMaxCandidates
934
			);
935
8
			let old = <TotalSelected<T>>::get();
936
8
			ensure!(old != new, Error::<T>::NoWritingSameValue);
937
7
			ensure!(
938
7
				new < <Round<T>>::get().length,
939
2
				Error::<T>::RoundLengthMustBeGreaterThanTotalSelectedCollators,
940
			);
941
5
			<TotalSelected<T>>::put(new);
942
5
			Self::deposit_event(Event::TotalSelectedSet { old, new });
943
5
			Ok(().into())
944
		}
945

            
946
		/// Set the commission for all collators
947
		#[pallet::call_index(5)]
948
		#[pallet::weight(<T as Config>::WeightInfo::set_collator_commission())]
949
		pub fn set_collator_commission(
950
			origin: OriginFor<T>,
951
			new: Perbill,
952
4
		) -> DispatchResultWithPostInfo {
953
4
			frame_system::ensure_root(origin)?;
954
3
			let old = <CollatorCommission<T>>::get();
955
3
			ensure!(old != new, Error::<T>::NoWritingSameValue);
956
2
			<CollatorCommission<T>>::put(new);
957
2
			Self::deposit_event(Event::CollatorCommissionSet { old, new });
958
2
			Ok(().into())
959
		}
960

            
961
		/// Set blocks per round
962
		/// - if called with `new` less than length of current round, will transition immediately
963
		/// in the next block
964
		/// - also updates per-round inflation config
965
		#[pallet::call_index(6)]
966
		#[pallet::weight(<T as Config>::WeightInfo::set_blocks_per_round())]
967
16
		pub fn set_blocks_per_round(origin: OriginFor<T>, new: u32) -> DispatchResultWithPostInfo {
968
16
			frame_system::ensure_root(origin)?;
969
15
			ensure!(
970
15
				new >= T::MinBlocksPerRound::get(),
971
1
				Error::<T>::CannotSetBelowMin
972
			);
973
14
			let mut round = <Round<T>>::get();
974
14
			let (now, first, old) = (round.current, round.first, round.length);
975
14
			ensure!(old != new, Error::<T>::NoWritingSameValue);
976
13
			ensure!(
977
13
				new > <TotalSelected<T>>::get(),
978
2
				Error::<T>::RoundLengthMustBeGreaterThanTotalSelectedCollators,
979
			);
980
11
			round.length = new;
981
11
			// update per-round inflation given new rounds per year
982
11
			let mut inflation_config = <InflationConfig<T>>::get();
983
11
			inflation_config.reset_round::<T>(new);
984
11
			<Round<T>>::put(round);
985
11
			Self::deposit_event(Event::BlocksPerRoundSet {
986
11
				current_round: now,
987
11
				first_block: first,
988
11
				old: old,
989
11
				new: new,
990
11
				new_per_round_inflation_min: inflation_config.round.min,
991
11
				new_per_round_inflation_ideal: inflation_config.round.ideal,
992
11
				new_per_round_inflation_max: inflation_config.round.max,
993
11
			});
994
11
			<InflationConfig<T>>::put(inflation_config);
995
11
			Ok(().into())
996
		}
997

            
998
		/// Join the set of collator candidates
999
		#[pallet::call_index(7)]
		#[pallet::weight(<T as Config>::WeightInfo::join_candidates(*candidate_count))]
		pub fn join_candidates(
			origin: OriginFor<T>,
			bond: BalanceOf<T>,
			candidate_count: u32,
676
		) -> DispatchResultWithPostInfo {
676
			let acc = ensure_signed(origin)?;
676
			ensure!(
676
				bond >= T::MinCandidateStk::get(),
14
				Error::<T>::CandidateBondBelowMin
			);
662
			Self::join_candidates_inner(acc, bond, candidate_count)
		}
		/// Request to leave the set of candidates. If successful, the account is immediately
		/// removed from the candidate pool to prevent selection as a collator.
		#[pallet::call_index(8)]
		#[pallet::weight(<T as Config>::WeightInfo::schedule_leave_candidates(*candidate_count))]
		pub fn schedule_leave_candidates(
			origin: OriginFor<T>,
			candidate_count: u32,
47
		) -> DispatchResultWithPostInfo {
47
			let collator = ensure_signed(origin)?;
47
			let mut state = <CandidateInfo<T>>::get(&collator).ok_or(Error::<T>::CandidateDNE)?;
46
			let (now, when) = state.schedule_leave::<T>()?;
45
			let mut candidates = <CandidatePool<T>>::get();
45
			ensure!(
45
				candidate_count >= candidates.0.len() as u32,
5
				Error::<T>::TooLowCandidateCountToLeaveCandidates
			);
40
			if candidates.remove(&Bond::from_owner(collator.clone())) {
40
				<CandidatePool<T>>::put(candidates);
40
			}
40
			<CandidateInfo<T>>::insert(&collator, state);
40
			Self::deposit_event(Event::CandidateScheduledExit {
40
				exit_allowed_round: now,
40
				candidate: collator,
40
				scheduled_exit: when,
40
			});
40
			Ok(().into())
		}
		/// Execute leave candidates request
		#[pallet::call_index(9)]
		#[pallet::weight(
			<T as Config>::WeightInfo::execute_leave_candidates_worst_case(*candidate_delegation_count)
		)]
		pub fn execute_leave_candidates(
			origin: OriginFor<T>,
			candidate: T::AccountId,
			candidate_delegation_count: u32,
26
		) -> DispatchResultWithPostInfo {
26
			ensure_signed(origin)?;
26
			let state = <CandidateInfo<T>>::get(&candidate).ok_or(Error::<T>::CandidateDNE)?;
26
			ensure!(
26
				state.delegation_count <= candidate_delegation_count,
3
				Error::<T>::TooLowCandidateDelegationCountToLeaveCandidates
			);
23
			<Pallet<T>>::execute_leave_candidates_inner(candidate)
		}
		/// Cancel open request to leave candidates
		/// - only callable by collator account
		/// - result upon successful call is the candidate is active in the candidate pool
		#[pallet::call_index(10)]
		#[pallet::weight(<T as Config>::WeightInfo::cancel_leave_candidates(*candidate_count))]
		pub fn cancel_leave_candidates(
			origin: OriginFor<T>,
			candidate_count: u32,
4
		) -> DispatchResultWithPostInfo {
4
			let collator = ensure_signed(origin)?;
4
			let mut state = <CandidateInfo<T>>::get(&collator).ok_or(Error::<T>::CandidateDNE)?;
4
			ensure!(state.is_leaving(), Error::<T>::CandidateNotLeaving);
4
			state.go_online();
4
			let mut candidates = <CandidatePool<T>>::get();
4
			ensure!(
4
				candidates.0.len() as u32 <= candidate_count,
				Error::<T>::TooLowCandidateCountWeightHintCancelLeaveCandidates
			);
4
			let maybe_inserted_candidate = candidates
4
				.try_insert(Bond {
4
					owner: collator.clone(),
4
					amount: state.total_counted,
4
				})
4
				.map_err(|_| Error::<T>::CandidateLimitReached)?;
4
			ensure!(maybe_inserted_candidate, Error::<T>::AlreadyActive);
4
			<CandidatePool<T>>::put(candidates);
4
			<CandidateInfo<T>>::insert(&collator, state);
4
			Self::deposit_event(Event::CancelledCandidateExit {
4
				candidate: collator,
4
			});
4
			Ok(().into())
		}
		/// Temporarily leave the set of collator candidates without unbonding
		#[pallet::call_index(11)]
		#[pallet::weight(<T as Config>::WeightInfo::go_offline(MAX_CANDIDATES))]
14
		pub fn go_offline(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
14
			let collator = ensure_signed(origin)?;
14
			<Pallet<T>>::go_offline_inner(collator)
		}
		/// Rejoin the set of collator candidates if previously had called `go_offline`
		#[pallet::call_index(12)]
		#[pallet::weight(<T as Config>::WeightInfo::go_online(MAX_CANDIDATES))]
7
		pub fn go_online(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
7
			let collator = ensure_signed(origin)?;
7
			<Pallet<T>>::go_online_inner(collator)
		}
		/// Increase collator candidate self bond by `more`
		#[pallet::call_index(13)]
		#[pallet::weight(<T as Config>::WeightInfo::candidate_bond_more(MAX_CANDIDATES))]
		pub fn candidate_bond_more(
			origin: OriginFor<T>,
			more: BalanceOf<T>,
6
		) -> DispatchResultWithPostInfo {
6
			let candidate = ensure_signed(origin)?;
6
			<Pallet<T>>::candidate_bond_more_inner(candidate, more)
		}
		/// Request by collator candidate to decrease self bond by `less`
		#[pallet::call_index(14)]
		#[pallet::weight(<T as Config>::WeightInfo::schedule_candidate_bond_less())]
		pub fn schedule_candidate_bond_less(
			origin: OriginFor<T>,
			less: BalanceOf<T>,
19
		) -> DispatchResultWithPostInfo {
19
			let collator = ensure_signed(origin)?;
19
			let mut state = <CandidateInfo<T>>::get(&collator).ok_or(Error::<T>::CandidateDNE)?;
17
			let when = state.schedule_bond_less::<T>(less)?;
15
			<CandidateInfo<T>>::insert(&collator, state);
15
			Self::deposit_event(Event::CandidateBondLessRequested {
15
				candidate: collator,
15
				amount_to_decrease: less,
15
				execute_round: when,
15
			});
15
			Ok(().into())
		}
		/// Execute pending request to adjust the collator candidate self bond
		#[pallet::call_index(15)]
		#[pallet::weight(<T as Config>::WeightInfo::execute_candidate_bond_less(MAX_CANDIDATES))]
		pub fn execute_candidate_bond_less(
			origin: OriginFor<T>,
			candidate: T::AccountId,
6
		) -> DispatchResultWithPostInfo {
6
			ensure_signed(origin)?; // we may want to reward this if caller != candidate
6
			<Pallet<T>>::execute_candidate_bond_less_inner(candidate)
		}
		/// Cancel pending request to adjust the collator candidate self bond
		#[pallet::call_index(16)]
		#[pallet::weight(<T as Config>::WeightInfo::cancel_candidate_bond_less())]
4
		pub fn cancel_candidate_bond_less(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
4
			let collator = ensure_signed(origin)?;
4
			let mut state = <CandidateInfo<T>>::get(&collator).ok_or(Error::<T>::CandidateDNE)?;
3
			state.cancel_bond_less::<T>(collator.clone())?;
3
			<CandidateInfo<T>>::insert(&collator, state);
3
			Ok(().into())
		}
		/// DEPRECATED use delegateWithAutoCompound
		/// If caller is not a delegator and not a collator, then join the set of delegators
		/// If caller is a delegator, then makes delegation to change their delegation state
		#[pallet::call_index(17)]
		#[pallet::weight(
			<T as Config>::WeightInfo::delegate_with_auto_compound_worst()
		)]
		pub fn delegate(
			origin: OriginFor<T>,
			candidate: T::AccountId,
			amount: BalanceOf<T>,
			candidate_delegation_count: u32,
			delegation_count: u32,
59
		) -> DispatchResultWithPostInfo {
59
			let delegator = ensure_signed(origin)?;
59
			<AutoCompoundDelegations<T>>::delegate_with_auto_compound(
59
				candidate,
59
				delegator,
59
				amount,
59
				Percent::zero(),
59
				candidate_delegation_count,
59
				0,
59
				delegation_count,
59
			)
		}
		/// If caller is not a delegator and not a collator, then join the set of delegators
		/// If caller is a delegator, then makes delegation to change their delegation state
		/// Sets the auto-compound config for the delegation
		#[pallet::call_index(18)]
		#[pallet::weight(
			<T as Config>::WeightInfo::delegate_with_auto_compound(
				*candidate_delegation_count,
				*candidate_auto_compounding_delegation_count,
				*delegation_count,
			)
		)]
		pub fn delegate_with_auto_compound(
			origin: OriginFor<T>,
			candidate: T::AccountId,
			amount: BalanceOf<T>,
			auto_compound: Percent,
			candidate_delegation_count: u32,
			candidate_auto_compounding_delegation_count: u32,
			delegation_count: u32,
9025
		) -> DispatchResultWithPostInfo {
9025
			let delegator = ensure_signed(origin)?;
9025
			<AutoCompoundDelegations<T>>::delegate_with_auto_compound(
9025
				candidate,
9025
				delegator,
9025
				amount,
9025
				auto_compound,
9025
				candidate_delegation_count,
9025
				candidate_auto_compounding_delegation_count,
9025
				delegation_count,
9025
			)
		}
		/// REMOVED, was schedule_leave_delegators
		#[pallet::call_index(19)]
		#[pallet::weight(<T as Config>::WeightInfo::set_staking_expectations())]
1
		pub fn removed_call_19(_origin: OriginFor<T>) -> DispatchResultWithPostInfo {
1
			fail!(Error::<T>::RemovedCall)
		}
		/// REMOVED, was execute_leave_delegators
		#[pallet::call_index(20)]
		#[pallet::weight(<T as Config>::WeightInfo::set_staking_expectations())]
1
		pub fn removed_call_20(_origin: OriginFor<T>) -> DispatchResultWithPostInfo {
1
			fail!(Error::<T>::RemovedCall)
		}
		/// REMOVED, was cancel_leave_delegators
		#[pallet::call_index(21)]
		#[pallet::weight(<T as Config>::WeightInfo::set_staking_expectations())]
1
		pub fn removed_call_21(_origin: OriginFor<T>) -> DispatchResultWithPostInfo {
1
			fail!(Error::<T>::RemovedCall)
		}
		/// Request to revoke an existing delegation. If successful, the delegation is scheduled
		/// to be allowed to be revoked via the `execute_delegation_request` extrinsic.
		/// The delegation receives no rewards for the rounds while a revoke is pending.
		/// A revoke may not be performed if any other scheduled request is pending.
		#[pallet::call_index(22)]
		#[pallet::weight(<T as Config>::WeightInfo::schedule_revoke_delegation(
			T::MaxTopDelegationsPerCandidate::get() + T::MaxBottomDelegationsPerCandidate::get()
		))]
		pub fn schedule_revoke_delegation(
			origin: OriginFor<T>,
			collator: T::AccountId,
51
		) -> DispatchResultWithPostInfo {
51
			let delegator = ensure_signed(origin)?;
51
			Self::delegation_schedule_revoke(collator, delegator)
		}
		/// Bond more for delegators wrt a specific collator candidate.
		#[pallet::call_index(23)]
		#[pallet::weight(<T as Config>::WeightInfo::delegator_bond_more(
			T::MaxTopDelegationsPerCandidate::get() + T::MaxBottomDelegationsPerCandidate::get()
		))]
		pub fn delegator_bond_more(
			origin: OriginFor<T>,
			candidate: T::AccountId,
			more: BalanceOf<T>,
19
		) -> DispatchResultWithPostInfo {
19
			let delegator = ensure_signed(origin)?;
19
			let (in_top, weight) = Self::delegation_bond_more_without_event(
19
				delegator.clone(),
19
				candidate.clone(),
19
				more.clone(),
19
			)?;
18
			Pallet::<T>::deposit_event(Event::DelegationIncreased {
18
				delegator,
18
				candidate,
18
				amount: more,
18
				in_top,
18
			});
18

            
18
			Ok(Some(weight).into())
		}
		/// Request bond less for delegators wrt a specific collator candidate. The delegation's
		/// rewards for rounds while the request is pending use the reduced bonded amount.
		/// A bond less may not be performed if any other scheduled request is pending.
		#[pallet::call_index(24)]
		#[pallet::weight(<T as Config>::WeightInfo::schedule_delegator_bond_less(
			T::MaxTopDelegationsPerCandidate::get() + T::MaxBottomDelegationsPerCandidate::get()
		))]
		pub fn schedule_delegator_bond_less(
			origin: OriginFor<T>,
			candidate: T::AccountId,
			less: BalanceOf<T>,
31
		) -> DispatchResultWithPostInfo {
31
			let delegator = ensure_signed(origin)?;
31
			Self::delegation_schedule_bond_decrease(candidate, delegator, less)
		}
		/// Execute pending request to change an existing delegation
		#[pallet::call_index(25)]
		#[pallet::weight(<T as Config>::WeightInfo::execute_delegator_revoke_delegation_worst())]
		pub fn execute_delegation_request(
			origin: OriginFor<T>,
			delegator: T::AccountId,
			candidate: T::AccountId,
37
		) -> DispatchResultWithPostInfo {
37
			ensure_signed(origin)?; // we may want to reward caller if caller != delegator
37
			Self::delegation_execute_scheduled_request(candidate, delegator)
		}
		/// Cancel request to change an existing delegation.
		#[pallet::call_index(26)]
		#[pallet::weight(<T as Config>::WeightInfo::cancel_delegation_request(350))]
		pub fn cancel_delegation_request(
			origin: OriginFor<T>,
			candidate: T::AccountId,
10
		) -> DispatchResultWithPostInfo {
10
			let delegator = ensure_signed(origin)?;
10
			Self::delegation_cancel_request(candidate, delegator)
		}
		/// Sets the auto-compounding reward percentage for a delegation.
		#[pallet::call_index(27)]
		#[pallet::weight(<T as Config>::WeightInfo::set_auto_compound(
			*candidate_auto_compounding_delegation_count_hint,
			*delegation_count_hint,
		))]
		pub fn set_auto_compound(
			origin: OriginFor<T>,
			candidate: T::AccountId,
			value: Percent,
			candidate_auto_compounding_delegation_count_hint: u32,
			delegation_count_hint: u32,
21
		) -> DispatchResultWithPostInfo {
21
			let delegator = ensure_signed(origin)?;
21
			<AutoCompoundDelegations<T>>::set_auto_compound(
21
				candidate,
21
				delegator,
21
				value,
21
				candidate_auto_compounding_delegation_count_hint,
21
				delegation_count_hint,
21
			)
		}
		/// Hotfix to remove existing empty entries for candidates that have left.
		#[pallet::call_index(28)]
		#[pallet::weight(
			T::DbWeight::get().reads_writes(2 * candidates.len() as u64, candidates.len() as u64)
		)]
		pub fn hotfix_remove_delegation_requests_exited_candidates(
			origin: OriginFor<T>,
			candidates: Vec<T::AccountId>,
4
		) -> DispatchResult {
4
			ensure_signed(origin)?;
4
			ensure!(candidates.len() < 100, <Error<T>>::InsufficientBalance);
9
			for candidate in &candidates {
7
				ensure!(
7
					<CandidateInfo<T>>::get(&candidate).is_none(),
1
					<Error<T>>::CandidateNotLeaving
				);
6
				ensure!(
6
					<DelegationScheduledRequests<T>>::get(&candidate).is_empty(),
1
					<Error<T>>::CandidateNotLeaving
				);
			}
6
			for candidate in candidates {
4
				<DelegationScheduledRequests<T>>::remove(candidate);
4
			}
2
			Ok(().into())
		}
		/// Notify a collator is inactive during MaxOfflineRounds
		#[pallet::call_index(29)]
		#[pallet::weight(<T as Config>::WeightInfo::notify_inactive_collator())]
		pub fn notify_inactive_collator(
			origin: OriginFor<T>,
			collator: T::AccountId,
5
		) -> DispatchResult {
5
			ensure!(
5
				<EnableMarkingOffline<T>>::get(),
				<Error<T>>::MarkingOfflineNotEnabled
			);
5
			ensure_signed(origin)?;
5
			let mut collators_len = 0usize;
5
			let max_collators = <TotalSelected<T>>::get();
5
			if let Some(len) = <SelectedCandidates<T>>::decode_len() {
5
				collators_len = len;
5
			};
			// Check collators length is not below or eq to 66% of max_collators.
			// We use saturating logic here with (2/3)
			// as it is dangerous to use floating point numbers directly.
5
			ensure!(
5
				collators_len * 3 > (max_collators * 2) as usize,
1
				<Error<T>>::TooLowCollatorCountToNotifyAsInactive
			);
4
			let round_info = <Round<T>>::get();
4
			let max_offline_rounds = T::MaxOfflineRounds::get();
4

            
4
			ensure!(
4
				round_info.current > max_offline_rounds,
1
				<Error<T>>::CurrentRoundTooLow
			);
			// Have rounds_to_check = [8,9]
			// in case we are in round 10 for instance
			// with MaxOfflineRounds = 2
3
			let first_round_to_check = round_info.current.saturating_sub(max_offline_rounds);
3
			let rounds_to_check = first_round_to_check..round_info.current;
3

            
3
			// If this counter is eq to max_offline_rounds,
3
			// the collator should be notified as inactive
3
			let mut inactive_counter: RoundIndex = 0u32;
			// Iter rounds to check
			//
			// - The collator has AtStake associated and their AwardedPts are zero
			//
			// If the previous condition is met in all rounds of rounds_to_check,
			// the collator is notified as inactive
6
			for r in rounds_to_check {
3
				let stake = <AtStake<T>>::get(r, &collator);
3
				let pts = <AwardedPts<T>>::get(r, &collator);
3

            
3
				if stake.is_some() && pts.is_zero() {
1
					inactive_counter = inactive_counter.saturating_add(1);
2
				}
			}
3
			if inactive_counter == max_offline_rounds {
1
				let _ = T::OnInactiveCollator::on_inactive_collator(
1
					collator.clone(),
1
					round_info.current.saturating_sub(1),
1
				);
1
			} else {
2
				return Err(<Error<T>>::CannotBeNotifiedAsInactive.into());
			}
1
			Ok(().into())
		}
		/// Enable/Disable marking offline feature
		#[pallet::call_index(30)]
		#[pallet::weight(
			Weight::from_parts(3_000_000u64, 4_000u64)
				.saturating_add(T::DbWeight::get().writes(1u64))
		)]
3
		pub fn enable_marking_offline(origin: OriginFor<T>, value: bool) -> DispatchResult {
3
			ensure_root(origin)?;
2
			<EnableMarkingOffline<T>>::set(value);
2
			Ok(())
		}
		/// Force join the set of collator candidates.
		/// It will skip the minimum required bond check.
		#[pallet::call_index(31)]
		#[pallet::weight(<T as Config>::WeightInfo::join_candidates(*candidate_count))]
		pub fn force_join_candidates(
			origin: OriginFor<T>,
			account: T::AccountId,
			bond: BalanceOf<T>,
			candidate_count: u32,
1
		) -> DispatchResultWithPostInfo {
1
			T::MonetaryGovernanceOrigin::ensure_origin(origin.clone())?;
1
			Self::join_candidates_inner(account, bond, candidate_count)
		}
		/// Set the inflation distribution configuration.
		#[pallet::call_index(32)]
		#[pallet::weight(<T as Config>::WeightInfo::set_inflation_distribution_config())]
		pub fn set_inflation_distribution_config(
			origin: OriginFor<T>,
			new: InflationDistributionConfig<T::AccountId>,
35
		) -> DispatchResultWithPostInfo {
35
			T::MonetaryGovernanceOrigin::ensure_origin(origin)?;
34
			let old = <InflationDistributionInfo<T>>::get().0;
34
			let new = new.0;
34
			ensure!(old != new, Error::<T>::NoWritingSameValue);
64
			let total_percent = new.iter().fold(0, |acc, x| acc + x.percent.deconstruct());
32
			ensure!(
32
				total_percent <= 100,
8
				Error::<T>::TotalInflationDistributionPercentExceeds100,
			);
24
			<InflationDistributionInfo<T>>::put::<InflationDistributionConfig<T::AccountId>>(
24
				new.clone().into(),
24
			);
24
			Self::deposit_event(Event::InflationDistributionConfigUpdated {
24
				old: old.into(),
24
				new: new.into(),
24
			});
24
			Ok(().into())
		}
	}
	/// Represents a payout made via `pay_one_collator_reward`.
	pub(crate) enum RewardPayment {
		/// A collator was paid
		Paid,
		/// A collator was skipped for payment. This can happen if they haven't been awarded any
		/// points, that is, they did not produce any blocks.
		Skipped,
		/// All collator payments have been processed.
		Finished,
	}
	impl<T: Config> Pallet<T> {
		pub fn set_candidate_bond_to_zero(acc: &T::AccountId) -> Weight {
			let actual_weight =
				<T as Config>::WeightInfo::set_candidate_bond_to_zero(T::MaxCandidates::get());
			if let Some(mut state) = <CandidateInfo<T>>::get(&acc) {
				state.bond_less::<T>(acc.clone(), state.bond);
				<CandidateInfo<T>>::insert(&acc, state);
			}
			actual_weight
		}
688
		pub fn is_delegator(acc: &T::AccountId) -> bool {
688
			<DelegatorState<T>>::get(acc).is_some()
688
		}
9680
		pub fn is_candidate(acc: &T::AccountId) -> bool {
9680
			<CandidateInfo<T>>::get(acc).is_some()
9680
		}
2
		pub fn is_selected_candidate(acc: &T::AccountId) -> bool {
2
			<SelectedCandidates<T>>::get().binary_search(acc).is_ok()
2
		}
663
		pub fn join_candidates_inner(
663
			acc: T::AccountId,
663
			bond: BalanceOf<T>,
663
			candidate_count: u32,
663
		) -> DispatchResultWithPostInfo {
663
			ensure!(!Self::is_candidate(&acc), Error::<T>::CandidateExists);
659
			ensure!(!Self::is_delegator(&acc), Error::<T>::DelegatorExists);
655
			let mut candidates = <CandidatePool<T>>::get();
655
			let old_count = candidates.0.len() as u32;
655
			ensure!(
655
				candidate_count >= old_count,
5
				Error::<T>::TooLowCandidateCountWeightHintJoinCandidates
			);
650
			let maybe_inserted_candidate = candidates
650
				.try_insert(Bond {
650
					owner: acc.clone(),
650
					amount: bond,
650
				})
650
				.map_err(|_| Error::<T>::CandidateLimitReached)?;
649
			ensure!(maybe_inserted_candidate, Error::<T>::CandidateExists);
649
			ensure!(
649
				Self::get_collator_stakable_free_balance(&acc) >= bond,
4
				Error::<T>::InsufficientBalance,
			);
645
			T::Currency::set_lock(COLLATOR_LOCK_ID, &acc, bond, WithdrawReasons::all());
645
			let candidate = CandidateMetadata::new(bond);
645
			<CandidateInfo<T>>::insert(&acc, candidate);
645
			let empty_delegations: Delegations<T::AccountId, BalanceOf<T>> = Default::default();
645
			// insert empty top delegations
645
			<TopDelegations<T>>::insert(&acc, empty_delegations.clone());
645
			// insert empty bottom delegations
645
			<BottomDelegations<T>>::insert(&acc, empty_delegations);
645
			<CandidatePool<T>>::put(candidates);
645
			let new_total = <Total<T>>::get().saturating_add(bond);
645
			<Total<T>>::put(new_total);
645
			Self::deposit_event(Event::JoinedCollatorCandidates {
645
				account: acc,
645
				amount_locked: bond,
645
				new_total_amt_locked: new_total,
645
			});
645
			Ok(().into())
663
		}
15
		pub fn go_offline_inner(collator: T::AccountId) -> DispatchResultWithPostInfo {
15
			let mut state = <CandidateInfo<T>>::get(&collator).ok_or(Error::<T>::CandidateDNE)?;
14
			let mut candidates = <CandidatePool<T>>::get();
14
			let actual_weight = <T as Config>::WeightInfo::go_offline(candidates.0.len() as u32);
14

            
14
			ensure!(
14
				state.is_active(),
1
				DispatchErrorWithPostInfo {
1
					post_info: Some(actual_weight).into(),
1
					error: <Error<T>>::AlreadyOffline.into(),
1
				}
			);
13
			state.go_offline();
13

            
13
			if candidates.remove(&Bond::from_owner(collator.clone())) {
13
				<CandidatePool<T>>::put(candidates);
13
			}
13
			<CandidateInfo<T>>::insert(&collator, state);
13
			Self::deposit_event(Event::CandidateWentOffline {
13
				candidate: collator,
13
			});
13
			Ok(Some(actual_weight).into())
15
		}
7
		pub fn go_online_inner(collator: T::AccountId) -> DispatchResultWithPostInfo {
7
			let mut state = <CandidateInfo<T>>::get(&collator).ok_or(Error::<T>::CandidateDNE)?;
6
			let mut candidates = <CandidatePool<T>>::get();
6
			let actual_weight = <T as Config>::WeightInfo::go_online(candidates.0.len() as u32);
6

            
6
			ensure!(
6
				!state.is_active(),
1
				DispatchErrorWithPostInfo {
1
					post_info: Some(actual_weight).into(),
1
					error: <Error<T>>::AlreadyActive.into(),
1
				}
			);
5
			ensure!(
5
				!state.is_leaving(),
1
				DispatchErrorWithPostInfo {
1
					post_info: Some(actual_weight).into(),
1
					error: <Error<T>>::CannotGoOnlineIfLeaving.into(),
1
				}
			);
4
			state.go_online();
4
			let maybe_inserted_candidate = candidates
4
				.try_insert(Bond {
4
					owner: collator.clone(),
4
					amount: state.total_counted,
4
				})
4
				.map_err(|_| Error::<T>::CandidateLimitReached)?;
4
			ensure!(
4
				maybe_inserted_candidate,
				DispatchErrorWithPostInfo {
					post_info: Some(actual_weight).into(),
					error: <Error<T>>::AlreadyActive.into(),
				},
			);
4
			<CandidatePool<T>>::put(candidates);
4
			<CandidateInfo<T>>::insert(&collator, state);
4
			Self::deposit_event(Event::CandidateBackOnline {
4
				candidate: collator,
4
			});
4
			Ok(Some(actual_weight).into())
7
		}
6
		pub fn candidate_bond_more_inner(
6
			collator: T::AccountId,
6
			more: BalanceOf<T>,
6
		) -> DispatchResultWithPostInfo {
6
			let mut state = <CandidateInfo<T>>::get(&collator).ok_or(Error::<T>::CandidateDNE)?;
6
			let actual_weight =
6
				<T as Config>::WeightInfo::candidate_bond_more(T::MaxCandidates::get());
6

            
6
			state
6
				.bond_more::<T>(collator.clone(), more)
6
				.map_err(|err| DispatchErrorWithPostInfo {
					post_info: Some(actual_weight).into(),
					error: err,
6
				})?;
6
			let (is_active, total_counted) = (state.is_active(), state.total_counted);
6
			<CandidateInfo<T>>::insert(&collator, state);
6
			if is_active {
6
				Self::update_active(collator, total_counted);
6
			}
6
			Ok(Some(actual_weight).into())
6
		}
6
		pub fn execute_candidate_bond_less_inner(
6
			candidate: T::AccountId,
6
		) -> DispatchResultWithPostInfo {
6
			let mut state = <CandidateInfo<T>>::get(&candidate).ok_or(Error::<T>::CandidateDNE)?;
6
			let actual_weight =
6
				<T as Config>::WeightInfo::execute_candidate_bond_less(T::MaxCandidates::get());
6

            
6
			state
6
				.execute_bond_less::<T>(candidate.clone())
6
				.map_err(|err| DispatchErrorWithPostInfo {
					post_info: Some(actual_weight).into(),
					error: err,
6
				})?;
6
			<CandidateInfo<T>>::insert(&candidate, state);
6
			Ok(Some(actual_weight).into())
6
		}
23
		pub fn execute_leave_candidates_inner(
23
			candidate: T::AccountId,
23
		) -> DispatchResultWithPostInfo {
23
			let state = <CandidateInfo<T>>::get(&candidate).ok_or(Error::<T>::CandidateDNE)?;
23
			let actual_auto_compound_delegation_count =
23
				<AutoCompoundingDelegations<T>>::decode_len(&candidate).unwrap_or_default() as u32;
23

            
23
			// TODO use these to return actual weight used via `execute_leave_candidates_ideal`
23
			let actual_delegation_count = state.delegation_count;
23
			let actual_weight = <T as Config>::WeightInfo::execute_leave_candidates_ideal(
23
				actual_delegation_count,
23
				actual_auto_compound_delegation_count,
23
			);
23

            
23
			state
23
				.can_leave::<T>()
23
				.map_err(|err| DispatchErrorWithPostInfo {
2
					post_info: Some(actual_weight).into(),
2
					error: err,
23
				})?;
21
			let return_stake = |bond: Bond<T::AccountId, BalanceOf<T>>| {
15
				// remove delegation from delegator state
15
				let mut delegator = DelegatorState::<T>::get(&bond.owner).expect(
15
					"Collator state and delegator state are consistent. 
15
						Collator state has a record of this delegation. Therefore, 
15
						Delegator state also has a record. qed.",
15
				);
15
				if let Some(remaining) = delegator.rm_delegation::<T>(&candidate) {
15
					Self::delegation_remove_request_with_state(
15
						&candidate,
15
						&bond.owner,
15
						&mut delegator,
15
					);
15
					<AutoCompoundDelegations<T>>::remove_auto_compound(&candidate, &bond.owner);
15

            
15
					if remaining.is_zero() {
9
						// we do not remove the scheduled delegation requests from other collators
9
						// since it is assumed that they were removed incrementally before only the
9
						// last delegation was left.
9
						<DelegatorState<T>>::remove(&bond.owner);
9
						T::Currency::remove_lock(DELEGATOR_LOCK_ID, &bond.owner);
9
					} else {
6
						<DelegatorState<T>>::insert(&bond.owner, delegator);
6
					}
				} else {
					// TODO: review. we assume here that this delegator has no remaining staked
					// balance, so we ensure the lock is cleared
					T::Currency::remove_lock(DELEGATOR_LOCK_ID, &bond.owner);
				}
15
			};
			// total backing stake is at least the candidate self bond
21
			let mut total_backing = state.bond;
21
			// return all top delegations
21
			let top_delegations =
21
				<TopDelegations<T>>::take(&candidate).expect("CandidateInfo existence checked");
35
			for bond in top_delegations.delegations {
14
				return_stake(bond);
14
			}
21
			total_backing = total_backing.saturating_add(top_delegations.total);
21
			// return all bottom delegations
21
			let bottom_delegations =
21
				<BottomDelegations<T>>::take(&candidate).expect("CandidateInfo existence checked");
22
			for bond in bottom_delegations.delegations {
1
				return_stake(bond);
1
			}
21
			total_backing = total_backing.saturating_add(bottom_delegations.total);
21
			// return stake to collator
21
			T::Currency::remove_lock(COLLATOR_LOCK_ID, &candidate);
21
			<CandidateInfo<T>>::remove(&candidate);
21
			<DelegationScheduledRequests<T>>::remove(&candidate);
21
			<AutoCompoundingDelegations<T>>::remove(&candidate);
21
			<TopDelegations<T>>::remove(&candidate);
21
			<BottomDelegations<T>>::remove(&candidate);
21
			let new_total_staked = <Total<T>>::get().saturating_sub(total_backing);
21
			<Total<T>>::put(new_total_staked);
21
			Self::deposit_event(Event::CandidateLeft {
21
				ex_candidate: candidate,
21
				unlocked_amount: total_backing,
21
				new_total_amt_locked: new_total_staked,
21
			});
21
			Ok(Some(actual_weight).into())
23
		}
		/// Returns an account's stakable balance which is not locked in delegation staking
18728
		pub fn get_delegator_stakable_balance(acc: &T::AccountId) -> BalanceOf<T> {
18728
			let mut stakable_balance =
18728
				T::Currency::free_balance(acc).saturating_add(T::Currency::reserved_balance(acc));
18728
			if let Some(state) = <DelegatorState<T>>::get(acc) {
226
				stakable_balance = stakable_balance.saturating_sub(state.total());
18502
			}
18728
			stakable_balance
18728
		}
		/// Returns an account's free balance which is not locked in collator staking
1309
		pub fn get_collator_stakable_free_balance(acc: &T::AccountId) -> BalanceOf<T> {
1309
			let mut balance = T::Currency::free_balance(acc);
1309
			if let Some(info) = <CandidateInfo<T>>::get(acc) {
19
				balance = balance.saturating_sub(info.bond);
1290
			}
1309
			balance
1309
		}
		/// Returns a delegations auto-compound value.
5
		pub fn delegation_auto_compound(
5
			candidate: &T::AccountId,
5
			delegator: &T::AccountId,
5
		) -> Percent {
5
			<AutoCompoundDelegations<T>>::auto_compound(candidate, delegator)
5
		}
		/// Caller must ensure candidate is active before calling
514
		pub(crate) fn update_active(candidate: T::AccountId, total: BalanceOf<T>) {
514
			let mut candidates = <CandidatePool<T>>::get();
514
			candidates.remove(&Bond::from_owner(candidate.clone()));
514
			candidates
514
				.try_insert(Bond {
514
					owner: candidate,
514
					amount: total,
514
				})
514
				.expect(
514
					"the candidate is removed in previous step so the length cannot increase; qed",
514
				);
514
			<CandidatePool<T>>::put(candidates);
514
		}
		/// Compute round issuance based on duration of the given round
77
		fn compute_issuance(round_duration: u64, round_length: u32) -> BalanceOf<T> {
77
			let ideal_duration: BalanceOf<T> = round_length
77
				.saturating_mul(T::BlockTime::get() as u32)
77
				.into();
77
			let config = <InflationConfig<T>>::get();
77
			let round_issuance = crate::inflation::round_issuance_range::<T>(config.round);
77

            
77
			// Initial formula: (round_duration / ideal_duration) * ideal_issuance
77
			// We multiply before the division to reduce rounding effects
77
			BalanceOf::<T>::from(round_duration as u32).saturating_mul(round_issuance.ideal)
77
				/ (ideal_duration)
77
		}
		/// Remove delegation from candidate state
		/// Amount input should be retrieved from delegator and it informs the storage lookups
23
		pub(crate) fn delegator_leaves_candidate(
23
			candidate: T::AccountId,
23
			delegator: T::AccountId,
23
			amount: BalanceOf<T>,
23
		) -> DispatchResult {
23
			let mut state = <CandidateInfo<T>>::get(&candidate).ok_or(Error::<T>::CandidateDNE)?;
23
			state.rm_delegation_if_exists::<T>(&candidate, delegator.clone(), amount)?;
23
			let new_total_locked = <Total<T>>::get().saturating_sub(amount);
23
			<Total<T>>::put(new_total_locked);
23
			let new_total = state.total_counted;
23
			<CandidateInfo<T>>::insert(&candidate, state);
23
			Self::deposit_event(Event::DelegatorLeftCandidate {
23
				delegator: delegator,
23
				candidate: candidate,
23
				unstaked_amount: amount,
23
				total_candidate_staked: new_total,
23
			});
23
			Ok(())
23
		}
258
		pub(crate) fn prepare_staking_payouts(
258
			round_info: RoundInfo<BlockNumberFor<T>>,
258
			round_duration: u64,
258
		) -> Weight {
258
			let RoundInfo {
258
				current: now,
258
				length: round_length,
258
				..
258
			} = round_info;
258

            
258
			// This function is called right after the round index increment,
258
			// and the goal is to compute the payout informations for the round that just ended.
258
			// We don't need to saturate here because the genesis round is 1.
258
			let prepare_payout_for_round = now - 1;
258

            
258
			// Return early if there is no blocks for this round
258
			if <Points<T>>::get(prepare_payout_for_round).is_zero() {
181
				return Weight::zero();
77
			}
77

            
77
			// Compute total issuance based on round duration
77
			let total_issuance = Self::compute_issuance(round_duration, round_length);
77
			// reserve portion of issuance for parachain bond account
77
			let mut left_issuance = total_issuance;
77

            
77
			let configs = <InflationDistributionInfo<T>>::get().0;
154
			for (index, config) in configs.iter().enumerate() {
154
				if config.percent.is_zero() {
77
					continue;
77
				}
77
				let reserve = config.percent * total_issuance;
77
				if let Ok(imb) = T::Currency::deposit_into_existing(&config.account, reserve) {
15
					// update round issuance if transfer succeeds
15
					left_issuance = left_issuance.saturating_sub(imb.peek());
15
					Self::deposit_event(Event::InflationDistributed {
15
						index: index as u32,
15
						account: config.account.clone(),
15
						value: imb.peek(),
15
					});
63
				}
			}
77
			let payout = DelayedPayout {
77
				round_issuance: total_issuance,
77
				total_staking_reward: left_issuance,
77
				collator_commission: <CollatorCommission<T>>::get(),
77
			};
77

            
77
			<DelayedPayouts<T>>::insert(prepare_payout_for_round, payout);
77

            
77
			<T as Config>::WeightInfo::prepare_staking_payouts()
258
		}
		/// Wrapper around pay_one_collator_reward which handles the following logic:
		/// * whether or not a payout needs to be made
		/// * cleaning up when payouts are done
		/// * returns the weight consumed by pay_one_collator_reward if applicable
30415
		fn handle_delayed_payouts(now: RoundIndex) -> Weight {
30415
			let delay = T::RewardPaymentDelay::get();
30415

            
30415
			// don't underflow uint
30415
			if now < delay {
16492
				return Weight::from_parts(0u64, 0);
13923
			}
13923

            
13923
			let paid_for_round = now.saturating_sub(delay);
13923
			if let Some(payout_info) = <DelayedPayouts<T>>::get(paid_for_round) {
129
				let result = Self::pay_one_collator_reward(paid_for_round, payout_info);
				// clean up storage items that we no longer need
129
				if matches!(result.0, RewardPayment::Finished) {
16
					<DelayedPayouts<T>>::remove(paid_for_round);
16
					<Points<T>>::remove(paid_for_round);
120
				}
129
				result.1 // weight consumed by pay_one_collator_reward
			} else {
13794
				Weight::from_parts(0u64, 0)
			}
30415
		}
		/// Payout a single collator from the given round.
		///
		/// Returns an optional tuple of (Collator's AccountId, total paid)
		/// or None if there were no more payouts to be made for the round.
129
		pub(crate) fn pay_one_collator_reward(
129
			paid_for_round: RoundIndex,
129
			payout_info: DelayedPayout<BalanceOf<T>>,
129
		) -> (RewardPayment, Weight) {
129
			// 'early_weight' tracks weight used for reads/writes done early in this fn before its
129
			// early-exit codepaths.
129
			let mut early_weight = Weight::zero();
129

            
129
			// TODO: it would probably be optimal to roll Points into the DelayedPayouts storage
129
			// item so that we do fewer reads each block
129
			let total_points = <Points<T>>::get(paid_for_round);
129
			early_weight = early_weight.saturating_add(T::DbWeight::get().reads_writes(1, 0));
129

            
129
			if total_points.is_zero() {
				// TODO: this case is obnoxious... it's a value query, so it could mean one of two
				// different logic errors:
				// 1. we removed it before we should have
				// 2. we called pay_one_collator_reward when we were actually done with deferred
				//    payouts
				log::warn!("pay_one_collator_reward called with no <Points<T>> for the round!");
				return (RewardPayment::Finished, early_weight);
129
			}
129

            
129
			let collator_fee = payout_info.collator_commission;
129
			let collator_issuance = collator_fee * payout_info.round_issuance;
113
			if let Some((collator, state)) =
129
				<AtStake<T>>::iter_prefix(paid_for_round).drain().next()
			{
				// read and kill AtStake
113
				early_weight = early_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
113

            
113
				// Take the awarded points for the collator
113
				let pts = <AwardedPts<T>>::take(paid_for_round, &collator);
113
				// read and kill AwardedPts
113
				early_weight = early_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
113
				if pts == 0 {
63
					return (RewardPayment::Skipped, early_weight);
50
				}
50

            
50
				// 'extra_weight' tracks weight returned from fns that we delegate to which can't be
50
				// known ahead of time.
50
				let mut extra_weight = Weight::zero();
50
				let pct_due = Perbill::from_rational(pts, total_points);
50
				let total_paid = pct_due * payout_info.total_staking_reward;
50
				let mut amt_due = total_paid;
50

            
50
				let num_delegators = state.delegations.len();
50
				let mut num_paid_delegations = 0u32;
50
				let mut num_auto_compounding = 0u32;
50
				let num_scheduled_requests =
50
					<DelegationScheduledRequests<T>>::decode_len(&collator).unwrap_or_default();
50
				if state.delegations.is_empty() {
15
					// solo collator with no delegators
15
					extra_weight = extra_weight
15
						.saturating_add(T::PayoutCollatorReward::payout_collator_reward(
15
							paid_for_round,
15
							collator.clone(),
15
							amt_due,
15
						))
15
						.saturating_add(T::OnCollatorPayout::on_collator_payout(
15
							paid_for_round,
15
							collator.clone(),
15
							amt_due,
15
						));
15
				} else {
					// pay collator first; commission + due_portion
35
					let collator_pct = Perbill::from_rational(state.bond, state.total);
35
					let commission = pct_due * collator_issuance;
35
					amt_due = amt_due.saturating_sub(commission);
35
					let collator_reward = (collator_pct * amt_due).saturating_add(commission);
35
					extra_weight = extra_weight
35
						.saturating_add(T::PayoutCollatorReward::payout_collator_reward(
35
							paid_for_round,
35
							collator.clone(),
35
							collator_reward,
35
						))
35
						.saturating_add(T::OnCollatorPayout::on_collator_payout(
35
							paid_for_round,
35
							collator.clone(),
35
							collator_reward,
35
						));
					// pay delegators due portion
					for BondWithAutoCompound {
61
						owner,
61
						amount,
61
						auto_compound,
96
					} in state.delegations
					{
61
						let percent = Perbill::from_rational(amount, state.total);
61
						let due = percent * amt_due;
61
						if !due.is_zero() {
55
							num_auto_compounding += if auto_compound.is_zero() { 0 } else { 1 };
55
							num_paid_delegations += 1u32;
55
							Self::mint_and_compound(
55
								due,
55
								auto_compound.clone(),
55
								collator.clone(),
55
								owner.clone(),
55
							);
6
						}
					}
				}
50
				extra_weight = extra_weight.saturating_add(
50
					<T as Config>::WeightInfo::pay_one_collator_reward_best(
50
						num_paid_delegations,
50
						num_auto_compounding,
50
						num_scheduled_requests as u32,
50
					),
50
				);
50

            
50
				(
50
					RewardPayment::Paid,
50
					<T as Config>::WeightInfo::pay_one_collator_reward(num_delegators as u32)
50
						.saturating_add(extra_weight),
50
				)
			} else {
				// Note that we don't clean up storage here; it is cleaned up in
				// handle_delayed_payouts()
16
				(RewardPayment::Finished, Weight::from_parts(0u64, 0))
			}
129
		}
		/// Compute the top `TotalSelected` candidates in the CandidatePool and return
		/// a vec of their AccountIds (sorted by AccountId).
		///
		/// If the returned vec is empty, the previous candidates should be used.
768
		pub fn compute_top_candidates() -> Vec<T::AccountId> {
768
			let top_n = <TotalSelected<T>>::get() as usize;
768
			if top_n == 0 {
				return vec![];
768
			}
768

            
768
			let candidates = <CandidatePool<T>>::get().0;
768

            
768
			// If the number of candidates is greater than top_n, select the candidates with higher
768
			// amount. Otherwise, return all the candidates.
768
			if candidates.len() > top_n {
				// Partially sort candidates such that element at index `top_n - 1` is sorted, and
				// all the elements in the range 0..top_n are the top n elements.
15
				let sorted_candidates = candidates
15
					.try_mutate(|inner| {
745
						inner.select_nth_unstable_by(top_n - 1, |a, b| {
745
							// Order by amount, then owner. The owner is needed to ensure a stable order
745
							// when two accounts have the same amount.
745
							a.amount
745
								.cmp(&b.amount)
745
								.then_with(|| a.owner.cmp(&b.owner))
745
								.reverse()
745
						});
15
					})
15
					.expect("sort cannot increase item count; qed");
15

            
15
				let mut collators = sorted_candidates
15
					.into_iter()
15
					.take(top_n)
75
					.map(|x| x.owner)
15
					.collect::<Vec<_>>();
15

            
15
				// Sort collators by AccountId
15
				collators.sort();
15

            
15
				collators
			} else {
				// Return all candidates
				// The candidates are already sorted by AccountId, so no need to sort again
1014
				candidates.into_iter().map(|x| x.owner).collect::<Vec<_>>()
			}
768
		}
		/// Best as in most cumulatively supported in terms of stake
		/// Returns [collator_count, delegation_count, total staked]
767
		pub(crate) fn select_top_candidates(now: RoundIndex) -> (Weight, u32, u32, BalanceOf<T>) {
767
			let (mut collator_count, mut delegation_count, mut total) =
767
				(0u32, 0u32, BalanceOf::<T>::zero());
767
			// choose the top TotalSelected qualified candidates, ordered by stake
767
			let collators = Self::compute_top_candidates();
767
			if collators.is_empty() {
				// SELECTION FAILED TO SELECT >=1 COLLATOR => select collators from previous round
290
				let last_round = now.saturating_sub(1u32);
290
				let mut total_per_candidate: BTreeMap<T::AccountId, BalanceOf<T>> = BTreeMap::new();
				// set this round AtStake to last round AtStake
290
				for (account, snapshot) in <AtStake<T>>::iter_prefix(last_round) {
43
					collator_count = collator_count.saturating_add(1u32);
43
					delegation_count =
43
						delegation_count.saturating_add(snapshot.delegations.len() as u32);
43
					total = total.saturating_add(snapshot.total);
43
					total_per_candidate.insert(account.clone(), snapshot.total);
43
					<AtStake<T>>::insert(now, account, snapshot);
43
				}
				// `SelectedCandidates` remains unchanged from last round
				// emit CollatorChosen event for tools that use this event
333
				for candidate in <SelectedCandidates<T>>::get() {
43
					let snapshot_total = total_per_candidate
43
						.get(&candidate)
43
						.expect("all selected candidates have snapshots");
43
					Self::deposit_event(Event::CollatorChosen {
43
						round: now,
43
						collator_account: candidate,
43
						total_exposed_amount: *snapshot_total,
43
					})
				}
290
				let weight = <T as Config>::WeightInfo::select_top_candidates(0, 0);
290
				return (weight, collator_count, delegation_count, total);
477
			}
			// snapshot exposure for round for weighting reward distribution
913
			for account in collators.iter() {
913
				let state = <CandidateInfo<T>>::get(account)
913
					.expect("all members of CandidateQ must be candidates");
913

            
913
				collator_count = collator_count.saturating_add(1u32);
913
				delegation_count = delegation_count.saturating_add(state.delegation_count);
913
				total = total.saturating_add(state.total_counted);
913
				let CountedDelegations {
913
					uncounted_stake,
913
					rewardable_delegations,
913
				} = Self::get_rewardable_delegators(&account);
913
				let total_counted = state.total_counted.saturating_sub(uncounted_stake);
913

            
913
				let auto_compounding_delegations = <AutoCompoundingDelegations<T>>::get(&account)
913
					.into_iter()
913
					.map(|x| (x.delegator, x.value))
913
					.collect::<BTreeMap<_, _>>();
913
				let rewardable_delegations = rewardable_delegations
913
					.into_iter()
913
					.map(|d| BondWithAutoCompound {
763
						owner: d.owner.clone(),
763
						amount: d.amount,
763
						auto_compound: auto_compounding_delegations
763
							.get(&d.owner)
763
							.cloned()
763
							.unwrap_or_else(|| Percent::zero()),
913
					})
913
					.collect();
913

            
913
				let snapshot = CollatorSnapshot {
913
					bond: state.bond,
913
					delegations: rewardable_delegations,
913
					total: total_counted,
913
				};
913
				<AtStake<T>>::insert(now, account, snapshot);
913
				Self::deposit_event(Event::CollatorChosen {
913
					round: now,
913
					collator_account: account.clone(),
913
					total_exposed_amount: state.total_counted,
913
				});
913
			}
			// insert canonical collator set
477
			<SelectedCandidates<T>>::put(
477
				BoundedVec::try_from(collators)
477
					.expect("subset of collators is always less than or equal to max candidates"),
477
			);
477

            
477
			let avg_delegator_count = delegation_count.checked_div(collator_count).unwrap_or(0);
477
			let weight = <T as Config>::WeightInfo::select_top_candidates(
477
				collator_count,
477
				avg_delegator_count,
477
			);
477
			(weight, collator_count, delegation_count, total)
767
		}
		/// Apply the delegator intent for revoke and decrease in order to build the
		/// effective list of delegators with their intended bond amount.
		///
		/// This will:
		/// - if [DelegationChange::Revoke] is outstanding, set the bond amount to 0.
		/// - if [DelegationChange::Decrease] is outstanding, subtract the bond by specified amount.
		/// - else, do nothing
		///
		/// The intended bond amounts will be used while calculating rewards.
913
		pub(crate) fn get_rewardable_delegators(collator: &T::AccountId) -> CountedDelegations<T> {
913
			let requests = <DelegationScheduledRequests<T>>::get(collator)
913
				.into_iter()
913
				.map(|x| (x.delegator, x.action))
913
				.collect::<BTreeMap<_, _>>();
913
			let mut uncounted_stake = BalanceOf::<T>::zero();
913
			let rewardable_delegations = <TopDelegations<T>>::get(collator)
913
				.expect("all members of CandidateQ must be candidates")
913
				.delegations
913
				.into_iter()
913
				.map(|mut bond| {
763
					bond.amount = match requests.get(&bond.owner) {
652
						None => bond.amount,
						Some(DelegationAction::Revoke(_)) => {
75
							uncounted_stake = uncounted_stake.saturating_add(bond.amount);
75
							BalanceOf::<T>::zero()
						}
36
						Some(DelegationAction::Decrease(amount)) => {
36
							uncounted_stake = uncounted_stake.saturating_add(*amount);
36
							bond.amount.saturating_sub(*amount)
						}
					};
763
					bond
913
				})
913
				.collect();
913
			CountedDelegations {
913
				uncounted_stake,
913
				rewardable_delegations,
913
			}
913
		}
		/// This function exists as a helper to delegator_bond_more & auto_compound functionality.
		/// Any changes to this function must align with both user-initiated bond increases and
		/// auto-compounding bond increases.
		/// Any feature-specific preconditions should be validated before this function is invoked.
		/// Any feature-specific events must be emitted after this function is invoked.
23
		pub fn delegation_bond_more_without_event(
23
			delegator: T::AccountId,
23
			candidate: T::AccountId,
23
			more: BalanceOf<T>,
23
		) -> Result<
23
			(bool, Weight),
23
			DispatchErrorWithPostInfo<frame_support::dispatch::PostDispatchInfo>,
23
		> {
23
			let mut state = <DelegatorState<T>>::get(&delegator).ok_or(Error::<T>::DelegatorDNE)?;
23
			ensure!(
23
				!Self::delegation_request_revoke_exists(&candidate, &delegator),
2
				Error::<T>::PendingDelegationRevoke
			);
21
			let actual_weight = <T as Config>::WeightInfo::delegator_bond_more(
21
				<DelegationScheduledRequests<T>>::decode_len(&candidate).unwrap_or_default() as u32,
21
			);
21
			let in_top = state
21
				.increase_delegation::<T>(candidate.clone(), more)
21
				.map_err(|err| DispatchErrorWithPostInfo {
					post_info: Some(actual_weight).into(),
					error: err,
21
				})?;
21
			Ok((in_top, actual_weight))
23
		}
		/// Mint a specified reward amount to the beneficiary account. Emits the [Rewarded] event.
		pub fn mint(amt: BalanceOf<T>, to: T::AccountId) {
			if let Ok(amount_transferred) = T::Currency::deposit_into_existing(&to, amt) {
				Self::deposit_event(Event::Rewarded {
					account: to.clone(),
					rewards: amount_transferred.peek(),
				});
			}
		}
		/// Mint a specified reward amount to the collator's account. Emits the [Rewarded] event.
88
		pub fn mint_collator_reward(
88
			_paid_for_round: RoundIndex,
88
			collator_id: T::AccountId,
88
			amt: BalanceOf<T>,
88
		) -> Weight {
88
			if let Ok(amount_transferred) = T::Currency::deposit_into_existing(&collator_id, amt) {
88
				Self::deposit_event(Event::Rewarded {
88
					account: collator_id.clone(),
88
					rewards: amount_transferred.peek(),
88
				});
88
			}
88
			<T as Config>::WeightInfo::mint_collator_reward()
88
		}
		/// Mint and compound delegation rewards. The function mints the amount towards the
		/// delegator and tries to compound a specified percent of it back towards the delegation.
		/// If a scheduled delegation revoke exists, then the amount is only minted, and nothing is
		/// compounded. Emits the [Compounded] event.
55
		pub fn mint_and_compound(
55
			amt: BalanceOf<T>,
55
			compound_percent: Percent,
55
			candidate: T::AccountId,
55
			delegator: T::AccountId,
55
		) {
55
			if let Ok(amount_transferred) =
55
				T::Currency::deposit_into_existing(&delegator, amt.clone())
			{
55
				Self::deposit_event(Event::Rewarded {
55
					account: delegator.clone(),
55
					rewards: amount_transferred.peek(),
55
				});
55

            
55
				let compound_amount = compound_percent.mul_ceil(amount_transferred.peek());
55
				if compound_amount.is_zero() {
51
					return;
4
				}
4
				if let Err(err) = Self::delegation_bond_more_without_event(
4
					delegator.clone(),
4
					candidate.clone(),
4
					compound_amount.clone(),
4
				) {
1
					log::debug!(
						"skipped compounding staking reward towards candidate '{:?}' for delegator '{:?}': {:?}",
						candidate,
						delegator,
						err
					);
1
					return;
3
				};
3

            
3
				Pallet::<T>::deposit_event(Event::Compounded {
3
					delegator,
3
					candidate,
3
					amount: compound_amount.clone(),
3
				});
			};
55
		}
	}
	/// Add reward points to block authors:
	/// * 20 points to the block producer for producing a block in the chain
	impl<T: Config> Pallet<T> {
29483
		fn award_points_to_block_author() {
29483
			let author = T::BlockAuthor::get();
29483
			let now = <Round<T>>::get().current;
29483
			let score_plus_20 = <AwardedPts<T>>::get(now, &author).saturating_add(20);
29483
			<AwardedPts<T>>::insert(now, author, score_plus_20);
29483
			<Points<T>>::mutate(now, |x| *x = x.saturating_add(20));
29483
		}
	}
	impl<T: Config> nimbus_primitives::CanAuthor<T::AccountId> for Pallet<T> {
		fn can_author(account: &T::AccountId, _slot: &u32) -> bool {
			Self::is_selected_candidate(account)
		}
	}
	impl<T: Config> Get<Vec<T::AccountId>> for Pallet<T> {
66
		fn get() -> Vec<T::AccountId> {
66
			Self::selected_candidates().into_inner()
66
		}
	}
}