forked from interlay/interbtc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
1302 lines (1165 loc) · 51.4 KB
/
lib.rs
File metadata and controls
1302 lines (1165 loc) · 51.4 KB
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
//! # Democracy Pallet
//!
//! - [`Config`]
//! - [`Call`]
//!
//! ## Overview
//!
//! The democracy pallet handles the administration of general stakeholder voting.
//!
//! Proposals made by the community are added to a queue before they become a referendum.
//!
//! Every launch period - a length defined in the runtime - this pallet will launch a
//! referendum from the proposal queue. Any token holder in the system can vote on this.
//!
//! ### Terminology
//!
//! - **Enactment Period:** The period between a proposal being approved and enacted.
//! - **Vote:** A value that can either be in approval ("Aye") or rejection ("Nay") of a particular referendum.
//! - **Proposal:** A submission to the chain that represents an action that a proposer (either an
//! account or an external origin) suggests that the system adopt.
//! - **Referendum:** A proposal that is in the process of being voted on for either acceptance or rejection as a change
//! to the system.
//!
//! ### Adaptive Quorum Biasing
//!
//! A _referendum_ can be either simple majority-carries in which 50%+1 of the
//! votes decide the outcome or _adaptive quorum biased_. Adaptive quorum biasing
//! makes the threshold for passing or rejecting a referendum higher or lower
//! depending on how the referendum was originally proposed. There are two types of
//! adaptive quorum biasing: 1) _positive turnout bias_ makes a referendum
//! require a super-majority to pass that decreases as turnout increases and
//! 2) _negative turnout bias_ makes a referendum require a super-majority to
//! reject that decreases as turnout increases. Another way to think about the
//! quorum biasing is that _positive bias_ referendums will be rejected by
//! default and _negative bias_ referendums get passed by default.
//!
//! ## Interface
//!
//! ### Dispatchable Functions
//!
//! #### Public
//!
//! These calls can be made from any externally held account capable of creating
//! a signed extrinsic.
//!
//! Basic actions:
//! - `propose` - Submits a sensitive action, represented as a hash. Requires a deposit.
//! - `second` - Signals agreement with a proposal, moves it higher on the proposal queue, and requires a matching
//! deposit to the original.
//! - `vote` - Votes in a referendum, either the vote is "Aye" to enact the proposal or "Nay" to keep the status quo.
//! - `unvote` - Cancel a previous vote, this must be done by the voter before the vote ends.
//!
//! Administration actions that can be done to any account:
//! - `reap_vote` - Remove some account's expired votes.
//!
//! Preimage actions:
//! - `note_preimage` - Registers the preimage for an upcoming proposal, requires a deposit that is returned once the
//! proposal is enacted.
//! - `note_imminent_preimage` - Registers the preimage for an upcoming proposal. Does not require a deposit, but the
//! proposal must be in the dispatch queue.
//! - `reap_preimage` - Removes the preimage for an expired proposal. Will only work under the condition that it's the
//! same account that noted it and after the voting period, OR it's a different account after the enactment period.
//!
//! #### Fast Track Origin
//!
//! This call can only be made by the `FastTrackOrigin`.
//!
//! - `fast_track` - Schedules the current externally proposed proposal that is "majority-carries" to become a
//! referendum immediately.
//! - `fast_track_referendum` - Schedules an active referendum to end in `FastTrackVotingPeriod`
//! blocks.
//!
//! #### Root
//!
//! - `cancel_referendum` - Removes a referendum.
//! - `cancel_queued` - Cancels a proposal that is queued for enactment.
//! - `clear_public_proposal` - Removes all public proposals.
#![deny(warnings)]
#![recursion_limit = "256"]
#![cfg_attr(not(feature = "std"), no_std)]
use core::time::Duration;
use chrono::Days;
use codec::{Decode, DecodeLimit, Encode, Input, MaxEncodedLen};
use frame_support::{
ensure,
traits::{
schedule::{DispatchTime, Named as ScheduleNamed},
BalanceStatus, Currency, Get, LockIdentifier, OnUnbalanced, ReservableCurrency, UnfilteredDispatchable,
UnixTime,
},
transactional,
weights::Weight,
};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{Hash, Saturating, Zero},
ArithmeticError, DispatchError, DispatchResult, RuntimeDebug,
};
use sp_std::prelude::*;
mod types;
mod vote_threshold;
pub mod weights;
pub use pallet::*;
pub use types::{ReferendumInfo, ReferendumStatus, Tally, Vote, Voting};
pub use vote_threshold::{Approved, VoteThreshold};
pub use weights::WeightInfo;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
const DEMOCRACY_ID: LockIdentifier = *b"democrac";
/// A proposal index.
pub type PropIndex = u32;
/// A referendum index.
pub type ReferendumIndex = u32;
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type NegativeImbalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum PreimageStatus<AccountId, Balance, BlockNumber> {
/// The preimage is imminently needed at the argument.
Missing(BlockNumber),
/// The preimage is available.
Available {
data: Vec<u8>,
provider: AccountId,
deposit: Balance,
since: BlockNumber,
/// None if it's not imminent.
expiry: Option<BlockNumber>,
},
}
impl<AccountId, Balance, BlockNumber> PreimageStatus<AccountId, Balance, BlockNumber> {
fn to_missing_expiry(self) -> Option<BlockNumber> {
match self {
PreimageStatus::Missing(expiry) => Some(expiry),
_ => None,
}
}
}
// A value placed in storage that represents the current version of the Democracy storage.
// This value is used by the `on_runtime_upgrade` logic to determine whether we run
// storage migration logic.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
enum Releases {
V1,
}
#[frame_support::pallet]
pub mod pallet {
use core::num::TryFromIntError;
use super::*;
use frame_support::{
dispatch::{DispatchClass, DispatchResultWithPostInfo, Pays},
pallet_prelude::*,
traits::EnsureOrigin,
Parameter,
};
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
use sp_runtime::DispatchResult;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config + Sized {
type Proposal: DecodeLimit
+ Parameter
+ UnfilteredDispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ From<Call<Self>>;
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Currency type for this pallet.
type Currency: ReservableCurrency<Self::AccountId>;
/// The period between a proposal being approved and enacted.
///
/// It should generally be a little more than the unstake period to ensure that
/// voting stakers have an opportunity to remove themselves from the system in the case
/// where they are on the losing side of a vote.
#[pallet::constant]
type EnactmentPeriod: Get<Self::BlockNumber>;
/// How often (in blocks) to check for new votes.
#[pallet::constant]
type VotingPeriod: Get<Self::BlockNumber>;
/// The minimum amount to be used as a deposit for a public referendum proposal.
#[pallet::constant]
type MinimumDeposit: Get<BalanceOf<Self>>;
/// Origin from which the next majority-carries (or more permissive) referendum may be
/// tabled to vote according to the `FastTrackVotingPeriod` asynchronously in a similar
/// manner to the emergency origin. It retains its threshold method.
type FastTrackOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// Minimum voting period allowed for a fast-track referendum.
#[pallet::constant]
type FastTrackVotingPeriod: Get<Self::BlockNumber>;
/// The amount of balance that must be deposited per byte of preimage stored.
#[pallet::constant]
type PreimageByteDeposit: Get<BalanceOf<Self>>;
/// Handler for the unbalanced reduction when slashing a preimage deposit.
type Slash: OnUnbalanced<NegativeImbalanceOf<Self>>;
/// The Scheduler.
type Scheduler: ScheduleNamed<Self::BlockNumber, Self::Proposal, Self::PalletsOrigin>;
/// Overarching type of all pallets origins.
type PalletsOrigin: From<frame_system::RawOrigin<Self::AccountId>>;
/// The maximum number of votes for an account.
///
/// Also used to compute weight, an overly big value can
/// lead to extrinsic with very big weight.
#[pallet::constant]
type MaxVotes: Get<u32>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// The maximum number of public proposals that can exist at any time.
#[pallet::constant]
type MaxProposals: Get<u32>;
/// Unix time
type UnixTime: UnixTime;
/// Duration
type Moment: TryInto<i64>;
type LaunchOffsetMillis: Get<Self::Moment>;
}
/// The number of (public) proposals that have been made so far.
#[pallet::storage]
#[pallet::getter(fn public_prop_count)]
pub type PublicPropCount<T> = StorageValue<_, PropIndex, ValueQuery>;
/// The public proposals. Unsorted. The second item is the proposal's hash.
#[pallet::storage]
#[pallet::getter(fn public_props)]
pub type PublicProps<T: Config> = StorageValue<_, Vec<(PropIndex, T::Hash, T::AccountId)>, ValueQuery>;
/// Those who have locked a deposit.
///
/// TWOX-NOTE: Safe, as increasing integer keys are safe.
#[pallet::storage]
#[pallet::getter(fn deposit_of)]
pub type DepositOf<T: Config> = StorageMap<_, Twox64Concat, PropIndex, (Vec<T::AccountId>, BalanceOf<T>)>;
/// Map of hashes to the proposal preimage, along with who registered it and their deposit.
/// The block number is the block at which it was deposited.
// TODO: Refactor Preimages into its own pallet.
// https://github.com/paritytech/substrate/issues/5322
#[pallet::storage]
pub type Preimages<T: Config> =
StorageMap<_, Identity, T::Hash, PreimageStatus<T::AccountId, BalanceOf<T>, T::BlockNumber>>;
/// The next free referendum index, aka the number of referenda started so far.
#[pallet::storage]
#[pallet::getter(fn referendum_count)]
pub type ReferendumCount<T> = StorageValue<_, ReferendumIndex, ValueQuery>;
/// The lowest referendum index representing an unbaked referendum. Equal to
/// `ReferendumCount` if there isn't a unbaked referendum.
#[pallet::storage]
#[pallet::getter(fn lowest_unbaked)]
pub type LowestUnbaked<T> = StorageValue<_, ReferendumIndex, ValueQuery>;
/// Information concerning any given referendum.
///
/// TWOX-NOTE: SAFE as indexes are not under an attacker’s control.
#[pallet::storage]
#[pallet::getter(fn referendum_info)]
pub type ReferendumInfoOf<T: Config> =
StorageMap<_, Twox64Concat, ReferendumIndex, ReferendumInfo<T::BlockNumber, T::Hash, BalanceOf<T>>>;
/// All votes for a particular voter. We store the balance for the number of votes that we
/// have recorded.
///
/// TWOX-NOTE: SAFE as `AccountId`s are crypto hashes anyway.
#[pallet::storage]
pub type VotingOf<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, Voting<BalanceOf<T>>, ValueQuery>;
/// Storage version of the pallet.
///
/// New networks start with last version.
#[pallet::storage]
pub(crate) type StorageVersion<T> = StorageValue<_, Releases>;
#[pallet::storage]
pub type NextLaunchTimestamp<T: Config> = StorageValue<_, u64, ValueQuery>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
_phantom: sp_std::marker::PhantomData<T>,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
_phantom: Default::default(),
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
PublicPropCount::<T>::put(0 as PropIndex);
ReferendumCount::<T>::put(0 as ReferendumIndex);
LowestUnbaked::<T>::put(0 as ReferendumIndex);
StorageVersion::<T>::put(Releases::V1);
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// A motion has been proposed by a public account. \[proposal_index, deposit\]
Proposed(PropIndex, BalanceOf<T>),
/// A public proposal has been tabled for referendum vote. \[proposal_index, deposit,
/// depositors\]
Tabled(PropIndex, BalanceOf<T>, Vec<T::AccountId>),
/// A referendum has begun. \[ref_index, threshold\]
Started(ReferendumIndex, VoteThreshold),
/// A proposal has been fast tracked. \[ref_index\]
FastTrack(ReferendumIndex),
/// A referendum has been fast tracked. \[ref_index\]
FastTrackReferendum(ReferendumIndex),
/// A proposal has been approved by referendum. \[ref_index\]
Passed(ReferendumIndex),
/// A proposal has been rejected by referendum. \[ref_index\]
NotPassed(ReferendumIndex),
/// A referendum has been cancelled. \[ref_index\]
Cancelled(ReferendumIndex),
/// A proposal has been enacted. \[ref_index, result\]
Executed(ReferendumIndex, DispatchResult),
/// A proposal's preimage was noted, and the deposit taken. \[proposal_hash, who, deposit\]
PreimageNoted(T::Hash, T::AccountId, BalanceOf<T>),
/// A proposal preimage was removed and used (the deposit was returned).
/// \[proposal_hash, provider, deposit\]
PreimageUsed(T::Hash, T::AccountId, BalanceOf<T>),
/// A proposal could not be executed because its preimage was invalid.
/// \[proposal_hash, ref_index\]
PreimageInvalid(T::Hash, ReferendumIndex),
/// A proposal could not be executed because its preimage was missing.
/// \[proposal_hash, ref_index\]
PreimageMissing(T::Hash, ReferendumIndex),
/// A registered preimage was removed and the deposit collected by the reaper.
/// \[proposal_hash, provider, deposit, reaper\]
PreimageReaped(T::Hash, T::AccountId, BalanceOf<T>, T::AccountId),
}
#[pallet::error]
pub enum Error<T> {
/// Value too low
ValueLow,
/// Proposal does not exist
ProposalMissing,
/// Proposal already made
DuplicateProposal,
/// Preimage already noted
DuplicatePreimage,
/// Not imminent
NotImminent,
/// Too early
TooEarly,
/// Imminent
Imminent,
/// Preimage not found
PreimageMissing,
/// Vote given for invalid referendum
ReferendumInvalid,
/// Fast tracking failed, because the referendum is
/// ending sooner than the fast track voting period.
ReferendumFastTrackFailed,
/// Invalid preimage
PreimageInvalid,
/// No proposals waiting
NoneWaiting,
/// The given account did not make this proposal.
NotProposer,
/// The given account did not vote on the referendum.
NotVoter,
/// Too high a balance was provided that the account cannot afford.
InsufficientFunds,
/// Invalid upper bound.
WrongUpperBound,
/// Maximum number of votes reached.
MaxVotesReached,
/// Maximum number of proposals reached.
TooManyProposals,
/// Unable to convert value.
TryIntoIntError,
}
impl<T> From<TryFromIntError> for Error<T> {
fn from(_err: TryFromIntError) -> Self {
Error::<T>::TryIntoIntError
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// Weight: see `begin_block`
fn on_initialize(n: T::BlockNumber) -> Weight {
Self::begin_block(n).unwrap_or_else(|e| {
sp_runtime::print(e);
Weight::from_ref_time(0 as u64)
})
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Propose a sensitive action to be taken.
///
/// The dispatch origin of this call must be _Signed_ and the sender must
/// have funds to cover the deposit.
///
/// - `proposal_hash`: The hash of the proposal preimage.
/// - `value`: The amount of deposit (must be at least `MinimumDeposit`).
///
/// Emits `Proposed`.
///
/// Weight: `O(p)`
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::propose())]
pub fn propose(
origin: OriginFor<T>,
proposal_hash: T::Hash,
#[pallet::compact] value: BalanceOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(value >= T::MinimumDeposit::get(), Error::<T>::ValueLow);
let index = Self::public_prop_count();
let real_prop_count = PublicProps::<T>::decode_len().unwrap_or(0) as u32;
let max_proposals = T::MaxProposals::get();
ensure!(real_prop_count < max_proposals, Error::<T>::TooManyProposals);
T::Currency::reserve(&who, value)?;
PublicPropCount::<T>::put(index + 1);
<DepositOf<T>>::insert(index, (&[&who][..], value));
<PublicProps<T>>::append((index, proposal_hash, who));
Self::deposit_event(Event::<T>::Proposed(index, value));
Ok(())
}
/// Signals agreement with a particular proposal.
///
/// The dispatch origin of this call must be _Signed_ and the sender
/// must have funds to cover the deposit, equal to the original deposit.
///
/// - `proposal`: The index of the proposal to second.
/// - `seconds_upper_bound`: an upper bound on the current number of seconds on this proposal. Extrinsic is
/// weighted according to this value with no refund.
///
/// Weight: `O(S)` where S is the number of seconds a proposal already has.
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::second(*seconds_upper_bound))]
pub fn second(
origin: OriginFor<T>,
#[pallet::compact] proposal: PropIndex,
#[pallet::compact] seconds_upper_bound: u32,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let seconds = Self::len_of_deposit_of(proposal).ok_or_else(|| Error::<T>::ProposalMissing)?;
ensure!(seconds <= seconds_upper_bound, Error::<T>::WrongUpperBound);
let mut deposit = Self::deposit_of(proposal).ok_or(Error::<T>::ProposalMissing)?;
T::Currency::reserve(&who, deposit.1)?;
deposit.0.push(who);
<DepositOf<T>>::insert(proposal, deposit);
Ok(())
}
/// Vote in a referendum. If `vote.is_aye()`, the vote is to enact the proposal;
/// otherwise it is a vote to keep the status quo.
///
/// The dispatch origin of this call must be _Signed_.
///
/// - `ref_index`: The index of the referendum to vote for.
/// - `vote`: The vote configuration.
///
/// Weight: `O(R)` where R is the number of referendums the voter has voted on.
#[pallet::call_index(2)]
#[pallet::weight(
T::WeightInfo::vote_new(T::MaxVotes::get())
.max(T::WeightInfo::vote_existing(T::MaxVotes::get()))
)]
pub fn vote(
origin: OriginFor<T>,
#[pallet::compact] ref_index: ReferendumIndex,
vote: Vote<BalanceOf<T>>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::try_vote(&who, ref_index, vote)
}
/// Schedule a proposal to be tabled immediately with the `FastTrackVotingPeriod`.
///
/// The dispatch of this call must be `FastTrackOrigin`.
///
/// - `prop_index`: The index of the current external proposal.
/// - `delay`: The number of blocks to wait after approval before execution.
///
/// Emits `Started` and `FastTrack`.
///
/// Weight: `O(1)`
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::fast_track())]
pub fn fast_track(
origin: OriginFor<T>,
#[pallet::compact] prop_index: PropIndex,
delay: T::BlockNumber,
) -> DispatchResult {
T::FastTrackOrigin::ensure_origin(origin)?;
Self::fast_track_with_voting_period(prop_index, delay, T::FastTrackVotingPeriod::get())
}
/// Same as `fast_track` but with the default `VotingPeriod`.
///
/// The dispatch of this call must be `FastTrackOrigin`.
///
/// - `prop_index`: The index of the proposal.
/// - `delay`: The number of blocks to wait after approval before execution.
///
/// Emits `Started` and `FastTrack`.
///
/// Weight: `O(1)`
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::fast_track())]
pub fn fast_track_default(
origin: OriginFor<T>,
#[pallet::compact] prop_index: PropIndex,
delay: T::BlockNumber,
) -> DispatchResult {
T::FastTrackOrigin::ensure_origin(origin)?;
Self::fast_track_with_voting_period(prop_index, delay, T::VotingPeriod::get())
}
/// Reduces the voting period of an existing referendum.
///
/// The dispatch of this call must be `FastTrackOrigin`.
///
/// - `ref_index`: The index of the referendum.
///
/// Emits `FastTrackReferendum`.
///
/// Weight: `O(1)`
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::fast_track_referendum())]
pub fn fast_track_referendum(origin: OriginFor<T>, #[pallet::compact] ref_index: PropIndex) -> DispatchResult {
T::FastTrackOrigin::ensure_origin(origin)?;
let mut status = Self::referendum_status(ref_index)?;
let now = <frame_system::Pallet<T>>::block_number();
let voting_period = T::FastTrackVotingPeriod::get();
let end_block = now.saturating_add(voting_period);
ensure!(status.end > end_block, Error::<T>::ReferendumFastTrackFailed);
status.end = end_block;
ReferendumInfoOf::<T>::insert(ref_index, ReferendumInfo::Ongoing(status));
Self::deposit_event(Event::<T>::FastTrackReferendum(ref_index));
Ok(())
}
/// Remove a referendum.
///
/// The dispatch origin of this call must be _Root_.
///
/// - `ref_index`: The index of the referendum to cancel.
///
/// # Weight: `O(1)`.
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::cancel_referendum())]
pub fn cancel_referendum(
origin: OriginFor<T>,
#[pallet::compact] ref_index: ReferendumIndex,
) -> DispatchResult {
ensure_root(origin)?;
Self::internal_cancel_referendum(ref_index);
Ok(())
}
/// Cancel a proposal queued for enactment.
///
/// The dispatch origin of this call must be _Root_.
///
/// - `which`: The index of the referendum to cancel.
///
/// Weight: `O(D)` where `D` is the items in the dispatch queue. Weighted as `D = 10`.
#[pallet::call_index(7)]
#[pallet::weight((T::WeightInfo::cancel_queued(10), DispatchClass::Operational))]
pub fn cancel_queued(origin: OriginFor<T>, which: ReferendumIndex) -> DispatchResult {
ensure_root(origin)?;
T::Scheduler::cancel_named((DEMOCRACY_ID, which).encode()).map_err(|_| Error::<T>::ProposalMissing)?;
Ok(())
}
/// Clears all public proposals.
///
/// The dispatch origin of this call must be _Root_.
///
/// Weight: `O(1)`.
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::clear_public_proposals())]
pub fn clear_public_proposals(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
<PublicProps<T>>::kill();
Ok(())
}
/// Remove a proposal.
///
/// - `prop_index`: The index of the proposal to cancel.
///
/// Weight: `O(p)` where `p = PublicProps::<T>::decode_len()`
#[pallet::call_index(9)]
#[pallet::weight(T::WeightInfo::cancel_proposal(T::MaxProposals::get()))]
#[transactional]
pub fn cancel_proposal(origin: OriginFor<T>, #[pallet::compact] prop_index: PropIndex) -> DispatchResult {
let who = ensure_signed(origin.clone())
.map(Some)
.or_else(|_| ensure_root(origin).map(|_| None))?;
PublicProps::<T>::try_mutate(|props| {
if let Some(i) = props.iter().position(|p| p.0 == prop_index) {
let (_, _, proposer) = props.remove(i);
if let Some(account_id) = who {
ensure!(proposer == account_id, Error::<T>::NotProposer);
}
Ok(())
} else {
Err(Error::<T>::ProposalMissing)
}
})?;
if let Some((whos, amount)) = DepositOf::<T>::take(prop_index) {
for who in whos.into_iter() {
T::Currency::unreserve(&who, amount);
}
}
Ok(())
}
/// Register the preimage for an upcoming proposal. This doesn't require the proposal to be
/// in the dispatch queue but does require a deposit, returned once enacted.
///
/// The dispatch origin of this call must be _Signed_.
///
/// - `encoded_proposal`: The preimage of a proposal.
///
/// Emits `PreimageNoted`.
///
/// Weight: `O(E)` with E size of `encoded_proposal` (protected by a required deposit).
#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::note_preimage(encoded_proposal.len() as u32))]
pub fn note_preimage(origin: OriginFor<T>, encoded_proposal: Vec<u8>) -> DispatchResult {
Self::note_preimage_inner(ensure_signed(origin)?, encoded_proposal)?;
Ok(())
}
/// Register the preimage for an upcoming proposal. This requires the proposal to be
/// in the dispatch queue. No deposit is needed. When this call is successful, i.e.
/// the preimage has not been uploaded before and matches some imminent proposal,
/// no fee is paid.
///
/// The dispatch origin of this call must be _Signed_.
///
/// - `encoded_proposal`: The preimage of a proposal.
///
/// Emits `PreimageNoted`.
///
/// Weight: `O(E)` with E size of `encoded_proposal` (protected by a required deposit).
#[pallet::call_index(11)]
#[pallet::weight(T::WeightInfo::note_imminent_preimage(encoded_proposal.len() as u32))]
pub fn note_imminent_preimage(origin: OriginFor<T>, encoded_proposal: Vec<u8>) -> DispatchResultWithPostInfo {
Self::note_imminent_preimage_inner(ensure_signed(origin)?, encoded_proposal)?;
// We check that this preimage was not uploaded before in
// `note_imminent_preimage_inner`, thus this call can only be successful once. If
// successful, user does not pay a fee.
Ok(Pays::No.into())
}
/// Remove an expired proposal preimage and collect the deposit.
///
/// The dispatch origin of this call must be _Signed_.
///
/// - `proposal_hash`: The preimage hash of a proposal.
/// - `proposal_length_upper_bound`: an upper bound on length of the proposal. Extrinsic is weighted according
/// to this value with no refund.
///
/// This will only work after `VotingPeriod` blocks from the time that the preimage was
/// noted, if it's the same account doing it. If it's a different account, then it'll only
/// work an additional `EnactmentPeriod` later.
///
/// Emits `PreimageReaped`.
///
/// Weight: `O(D)` where D is length of proposal.
#[pallet::call_index(12)]
#[pallet::weight(T::WeightInfo::reap_preimage(*proposal_len_upper_bound))]
pub fn reap_preimage(
origin: OriginFor<T>,
proposal_hash: T::Hash,
#[pallet::compact] proposal_len_upper_bound: u32,
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(
Self::pre_image_data_len(proposal_hash)? <= proposal_len_upper_bound,
Error::<T>::WrongUpperBound,
);
let (provider, deposit, since, expiry) = <Preimages<T>>::get(&proposal_hash)
.and_then(|m| match m {
PreimageStatus::Available {
provider,
deposit,
since,
expiry,
..
} => Some((provider, deposit, since, expiry)),
_ => None,
})
.ok_or(Error::<T>::PreimageMissing)?;
let now = <frame_system::Pallet<T>>::block_number();
let (voting, enactment) = (T::VotingPeriod::get(), T::EnactmentPeriod::get());
let additional = if who == provider { Zero::zero() } else { enactment };
ensure!(
now >= since.saturating_add(voting).saturating_add(additional),
Error::<T>::TooEarly
);
ensure!(expiry.map_or(true, |e| now > e), Error::<T>::Imminent);
let res = T::Currency::repatriate_reserved(&provider, &who, deposit, BalanceStatus::Free);
debug_assert!(res.is_ok());
<Preimages<T>>::remove(&proposal_hash);
Self::deposit_event(Event::<T>::PreimageReaped(proposal_hash, provider, deposit, who));
Ok(())
}
/// Remove a vote for an ongoing referendum.
///
/// The dispatch origin of this call must be _Signed_, and the signer must have a vote
/// registered for referendum `index`.
///
/// - `index`: The index of referendum of the vote to be removed.
///
/// Weight: `O(R + log R)` where R is the number of referenda that `target` has voted on.
/// Weight is calculated for the maximum number of vote.
#[pallet::call_index(13)]
#[pallet::weight(T::WeightInfo::remove_vote(T::MaxVotes::get()))]
pub fn remove_vote(origin: OriginFor<T>, index: ReferendumIndex) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::try_remove_vote(&who, index)
}
/// Enact a proposal from a referendum. For now we just make the weight be the maximum.
#[pallet::call_index(14)]
#[pallet::weight(1000)]
pub fn enact_proposal(origin: OriginFor<T>, proposal_hash: T::Hash, index: ReferendumIndex) -> DispatchResult {
ensure_root(origin)?;
Self::do_enact_proposal(proposal_hash, index)
}
}
}
impl<T: Config> Pallet<T> {
// exposed immutables.
/// Get the amount locked in support of `proposal`; `None` if proposal isn't a valid proposal
/// index.
pub fn backing_for(proposal: PropIndex) -> Option<BalanceOf<T>> {
Self::deposit_of(proposal).map(|(l, d)| d.saturating_mul((l.len() as u32).into()))
}
/// Get all referenda ready for tally at block `n`.
pub fn maturing_referenda_at(
n: T::BlockNumber,
) -> Vec<(ReferendumIndex, ReferendumStatus<T::BlockNumber, T::Hash, BalanceOf<T>>)> {
let next = Self::lowest_unbaked();
let last = Self::referendum_count();
Self::maturing_referenda_at_inner(n, next..last)
}
fn maturing_referenda_at_inner(
n: T::BlockNumber,
range: core::ops::Range<PropIndex>,
) -> Vec<(ReferendumIndex, ReferendumStatus<T::BlockNumber, T::Hash, BalanceOf<T>>)> {
range
.into_iter()
.map(|i| (i, Self::referendum_info(i)))
.filter_map(|(i, maybe_info)| match maybe_info {
Some(ReferendumInfo::Ongoing(status)) => Some((i, status)),
_ => None,
})
.filter(|(_, status)| status.end == n)
.collect()
}
// Exposed mutables.
/// Start a referendum.
pub fn internal_start_referendum(
proposal_hash: T::Hash,
threshold: VoteThreshold,
delay: T::BlockNumber,
) -> ReferendumIndex {
<Pallet<T>>::inject_referendum(
<frame_system::Pallet<T>>::block_number().saturating_add(T::VotingPeriod::get()),
proposal_hash,
threshold,
delay,
)
}
/// Remove a referendum.
pub fn internal_cancel_referendum(ref_index: ReferendumIndex) {
Self::deposit_event(Event::<T>::Cancelled(ref_index));
ReferendumInfoOf::<T>::remove(ref_index);
}
// private.
fn fast_track_with_voting_period(
prop_index: PropIndex,
delay: T::BlockNumber,
voting_period: T::BlockNumber,
) -> DispatchResult {
let mut public_props = <PublicProps<T>>::get();
let (winner_index, _) = public_props
.iter()
.enumerate()
.find(|(_, (i, ..))| *i == prop_index)
.ok_or(Error::<T>::ProposalMissing)?;
let (_, proposal_hash, _) = public_props.swap_remove(winner_index);
<PublicProps<T>>::put(public_props);
if let Some((depositors, deposit)) = <DepositOf<T>>::take(prop_index) {
// refund depositors
for d in &depositors {
T::Currency::unreserve(d, deposit);
}
}
let now = <frame_system::Pallet<T>>::block_number();
let ref_index = Self::inject_referendum(
now.saturating_add(voting_period),
proposal_hash,
VoteThreshold::SuperMajorityAgainst,
delay,
);
Self::deposit_event(Event::<T>::FastTrack(ref_index));
Ok(())
}
/// Ok if the given referendum is active, Err otherwise
fn ensure_ongoing(
r: ReferendumInfo<T::BlockNumber, T::Hash, BalanceOf<T>>,
) -> Result<ReferendumStatus<T::BlockNumber, T::Hash, BalanceOf<T>>, DispatchError> {
match r {
ReferendumInfo::Ongoing(s) => Ok(s),
_ => Err(Error::<T>::ReferendumInvalid.into()),
}
}
fn referendum_status(
ref_index: ReferendumIndex,
) -> Result<ReferendumStatus<T::BlockNumber, T::Hash, BalanceOf<T>>, DispatchError> {
let info = ReferendumInfoOf::<T>::get(ref_index).ok_or(Error::<T>::ReferendumInvalid)?;
Self::ensure_ongoing(info)
}
/// Actually enact a vote, if legit.
fn try_vote(who: &T::AccountId, ref_index: ReferendumIndex, vote: Vote<BalanceOf<T>>) -> DispatchResult {
let mut status = Self::referendum_status(ref_index)?;
ensure!(
vote.balance <= T::Currency::free_balance(who),
Error::<T>::InsufficientFunds
);
VotingOf::<T>::try_mutate(who, |voting| -> DispatchResult {
let Voting { ref mut votes, .. } = voting;
match votes.binary_search_by_key(&ref_index, |i| i.0) {
Ok(i) => {
// Shouldn't be possible to fail, but we handle it gracefully.
status.tally.remove(votes[i].1).ok_or(ArithmeticError::Underflow)?;
votes[i].1 = vote;
}
Err(i) => {
ensure!(votes.len() as u32 <= T::MaxVotes::get(), Error::<T>::MaxVotesReached);
votes.insert(i, (ref_index, vote));
}
}
// Shouldn't be possible to fail, but we handle it gracefully.
status.tally.add(vote).ok_or(ArithmeticError::Overflow)?;
Ok(())
})?;
ReferendumInfoOf::<T>::insert(ref_index, ReferendumInfo::Ongoing(status));
Ok(())
}
/// Remove the account's vote for the given referendum.
fn try_remove_vote(who: &T::AccountId, ref_index: ReferendumIndex) -> DispatchResult {
let info = ReferendumInfoOf::<T>::get(ref_index);
VotingOf::<T>::try_mutate(who, |voting| -> DispatchResult {
let Voting { ref mut votes, .. } = voting;
let i = votes
.binary_search_by_key(&ref_index, |i| i.0)
.map_err(|_| Error::<T>::NotVoter)?;
match info {
Some(ReferendumInfo::Ongoing(mut status)) => {
// Shouldn't be possible to fail, but we handle it gracefully.
status.tally.remove(votes[i].1).ok_or(ArithmeticError::Underflow)?;
ReferendumInfoOf::<T>::insert(ref_index, ReferendumInfo::Ongoing(status));
}
Some(ReferendumInfo::Finished { .. }) => {}
None => {} // Referendum was cancelled.
}
votes.remove(i);
Ok(())
})?;
Ok(())
}
/// Start a referendum
fn inject_referendum(
end: T::BlockNumber,
proposal_hash: T::Hash,
threshold: VoteThreshold,
delay: T::BlockNumber,
) -> ReferendumIndex {
let ref_index = Self::referendum_count();
ReferendumCount::<T>::put(ref_index + 1);
let status = ReferendumStatus {
end,
proposal_hash,
threshold,
delay,
tally: Default::default(),
};
let item = ReferendumInfo::Ongoing(status);
<ReferendumInfoOf<T>>::insert(ref_index, item);
Self::deposit_event(Event::<T>::Started(ref_index, threshold));
ref_index
}
/// Table the next waiting proposal for a vote.
fn launch_next(now: T::BlockNumber) -> DispatchResult {
Self::launch_public(now).map_err(|_| Error::<T>::NoneWaiting.into())
}
/// Table the waiting public proposal with the highest backing for a vote.
fn launch_public(now: T::BlockNumber) -> DispatchResult {
let mut public_props = Self::public_props();
if let Some((winner_index, _)) = public_props.iter().enumerate().max_by_key(
// defensive only: All current public proposals have an amount locked
|x| Self::backing_for((x.1).0).unwrap_or_else(Zero::zero),
) {
let (prop_index, proposal, _) = public_props.swap_remove(winner_index);
<PublicProps<T>>::put(public_props);
if let Some((depositors, deposit)) = <DepositOf<T>>::take(prop_index) {
// refund depositors
for d in &depositors {
T::Currency::unreserve(d, deposit);
}
Self::deposit_event(Event::<T>::Tabled(prop_index, deposit, depositors));
Self::inject_referendum(
now.saturating_add(T::VotingPeriod::get()),
proposal,