lib.rs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. //! # Proposals engine module
  2. //! Proposals `engine` module for the Joystream platform. Version 2.
  3. //! The main component of the proposals system. Provides methods and extrinsics to create and
  4. //! vote for proposals, inspired by Parity **Democracy module**.
  5. //!
  6. //! ## Overview
  7. //! Proposals `engine` module provides an abstract mechanism to work with proposals: creation, voting,
  8. //! execution, canceling, etc. Proposal execution demands serialized _Dispatchable_ proposal code.
  9. //! It could be any _Dispatchable_ + _Parameter_ type, but most likely, it would be serialized (via
  10. //! Parity _codec_ crate) extrisic call. A proposal stage can be described by its [status](./enum.ProposalStatus.html).
  11. //!
  12. //! ## Proposal lifecycle
  13. //! When a proposal passes [checks](./struct.Module.html#method.ensure_create_proposal_parameters_are_valid)
  14. //! for its [parameters](./struct.ProposalParameters.html) - it can be [created](./struct.Module.html#method.create_proposal).
  15. //! The newly created proposal has _Active_ status. The proposal can be voted on or canceled during its
  16. //! _voting period_. Votes can be [different](./enum.VoteKind.html). When the proposal gets enough votes
  17. //! to be slashed or approved or _voting period_ ends - the proposal becomes _Finalized_. If the proposal
  18. //! got approved and _grace period_ passed - the `engine` module tries to execute the proposal.
  19. //! The final [approved status](./enum.ApprovedProposalStatus.html) of the proposal defines
  20. //! an overall proposal outcome.
  21. //!
  22. //! ### Notes
  23. //!
  24. //! - The proposal can be [vetoed](./struct.Module.html#method.veto_proposal)
  25. //! anytime before the proposal execution by the _sudo_.
  26. //! - When the proposal is created with some stake - refunding on proposal finalization with
  27. //! different statuses should be accomplished from the external handler from the _stake module_
  28. //! (_StakingEventsHandler_). Such a handler should call
  29. //! [refund_proposal_stake](./struct.Module.html#method.refund_proposal_stake) callback function.
  30. //! - If the _council_ got reelected during the proposal _voting period_ the external handler calls
  31. //! [reset_active_proposals](./trait.Module.html#method.reset_active_proposals) function and
  32. //! all voting results get cleared.
  33. //!
  34. //! ### Important abstract types to be implemented
  35. //! Proposals `engine` module has several abstractions to be implemented in order to work correctly.
  36. //! - _VoterOriginValidator_ - ensure valid voter identity. Voters should have permissions to vote:
  37. //! they should be council members.
  38. //! - [VotersParameters](./trait.VotersParameters.html) - defines total voter number, which is
  39. //! the council size
  40. //! - _ProposerOriginValidator_ - ensure valid proposer identity. Proposers should have permissions
  41. //! to create a proposal: they should be members of the Joystream.
  42. //! - [StakeHandlerProvider](./trait.StakeHandlerProvider.html) - defines an interface for the staking.
  43. //!
  44. //! A full list of the abstractions can be found [here](./trait.Trait.html).
  45. //!
  46. //! ### Supported extrinsics
  47. //! - [vote](./struct.Module.html#method.vote) - registers a vote for the proposal
  48. //! - [cancel_proposal](./struct.Module.html#method.cancel_proposal) - cancels the proposal (can be canceled only by owner)
  49. //! - [veto_proposal](./struct.Module.html#method.veto_proposal) - vetoes the proposal
  50. //!
  51. //! ### Public API
  52. //! - [create_proposal](./struct.Module.html#method.create_proposal) - creates proposal using provided parameters
  53. //! - [ensure_create_proposal_parameters_are_valid](./struct.Module.html#method.ensure_create_proposal_parameters_are_valid) - ensures that we can create the proposal
  54. //! - [refund_proposal_stake](./struct.Module.html#method.refund_proposal_stake) - a callback for _StakingHandlerEvents_
  55. //! - [reset_active_proposals](./trait.Module.html#method.reset_active_proposals) - resets voting results for active proposals
  56. //!
  57. //! ## Usage
  58. //!
  59. //! ```
  60. //! use frame_support::{decl_module, print};
  61. //! use system::ensure_signed;
  62. //! use codec::Encode;
  63. //! use pallet_proposals_engine::{self as engine, ProposalParameters};
  64. //!
  65. //! pub trait Trait: engine::Trait + membership::Trait {}
  66. //!
  67. //! decl_module! {
  68. //! pub struct Module<T: Trait> for enum Call where origin: T::Origin {
  69. //! #[weight = 10_000_000]
  70. //! fn executable_proposal(origin) {
  71. //! print("executed!");
  72. //! }
  73. //!
  74. //! #[weight = 10_000_000]
  75. //! pub fn create_spending_proposal(
  76. //! origin,
  77. //! proposer_id: T::MemberId,
  78. //! ) {
  79. //! let account_id = ensure_signed(origin)?;
  80. //! let parameters = ProposalParameters::default();
  81. //! let title = b"Spending proposal".to_vec();
  82. //! let description = b"We need to spend some tokens to support the working group lead."
  83. //! .to_vec();
  84. //! let encoded_proposal_code = <Call<T>>::executable_proposal().encode();
  85. //!
  86. //! <engine::Module<T>>::ensure_create_proposal_parameters_are_valid(
  87. //! &parameters,
  88. //! &title,
  89. //! &description,
  90. //! None
  91. //! )?;
  92. //! <engine::Module<T>>::create_proposal(
  93. //! account_id,
  94. //! proposer_id,
  95. //! parameters,
  96. //! title,
  97. //! description,
  98. //! None,
  99. //! encoded_proposal_code
  100. //! )?;
  101. //! }
  102. //! }
  103. //! }
  104. //! # fn main() {}
  105. //! ```
  106. // Ensure we're `no_std` when compiling for Wasm.
  107. #![cfg_attr(not(feature = "std"), no_std)]
  108. // Do not delete! Cannot be uncommented by default, because of Parity decl_module! issue.
  109. //#![warn(missing_docs)]
  110. use crate::types::ApprovedProposalData;
  111. use types::FinalizedProposalData;
  112. use types::ProposalStakeManager;
  113. pub use types::{
  114. ActiveStake, ApprovedProposalStatus, FinalizationData, Proposal, ProposalDecisionStatus,
  115. ProposalParameters, ProposalStatus, VotingResults,
  116. };
  117. pub use types::{BalanceOf, CurrencyOf, NegativeImbalance};
  118. pub use types::{DefaultStakeHandlerProvider, StakeHandler, StakeHandlerProvider};
  119. pub use types::{ProposalCodeDecoder, ProposalExecutable};
  120. pub use types::{VoteKind, VotersParameters};
  121. pub(crate) mod types;
  122. #[cfg(test)]
  123. mod tests;
  124. use codec::Decode;
  125. use frame_support::dispatch::{DispatchError, DispatchResult, UnfilteredDispatchable};
  126. use frame_support::storage::IterableStorageMap;
  127. use frame_support::traits::{Currency, Get};
  128. use frame_support::{
  129. decl_error, decl_event, decl_module, decl_storage, ensure, print, Parameter, StorageDoubleMap,
  130. };
  131. use sp_arithmetic::traits::Zero;
  132. use sp_std::vec::Vec;
  133. use system::{ensure_root, RawOrigin};
  134. use common::origin::ActorOriginValidator;
  135. type MemberId<T> = <T as membership::Trait>::MemberId;
  136. /// Proposals engine trait.
  137. pub trait Trait:
  138. system::Trait + pallet_timestamp::Trait + stake::Trait + membership::Trait
  139. {
  140. /// Engine event type.
  141. type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
  142. /// Validates proposer id and origin combination
  143. type ProposerOriginValidator: ActorOriginValidator<
  144. Self::Origin,
  145. MemberId<Self>,
  146. Self::AccountId,
  147. >;
  148. /// Validates voter id and origin combination
  149. type VoterOriginValidator: ActorOriginValidator<Self::Origin, MemberId<Self>, Self::AccountId>;
  150. /// Provides data for voting. Defines maximum voters count for the proposal.
  151. type TotalVotersCounter: VotersParameters;
  152. /// Proposal Id type
  153. type ProposalId: From<u32> + Parameter + Default + Copy;
  154. /// Provides stake logic implementation. Can be used to mock stake logic.
  155. type StakeHandlerProvider: StakeHandlerProvider<Self>;
  156. /// The fee is applied when cancel the proposal. A fee would be slashed (burned).
  157. type CancellationFee: Get<BalanceOf<Self>>;
  158. /// The fee is applied when the proposal gets rejected. A fee would be slashed (burned).
  159. type RejectionFee: Get<BalanceOf<Self>>;
  160. /// Defines max allowed proposal title length.
  161. type TitleMaxLength: Get<u32>;
  162. /// Defines max allowed proposal description length.
  163. type DescriptionMaxLength: Get<u32>;
  164. /// Defines max simultaneous active proposals number.
  165. type MaxActiveProposalLimit: Get<u32>;
  166. /// Proposals executable code. Can be instantiated by external module Call enum members.
  167. type DispatchableCallCode: Parameter + UnfilteredDispatchable<Origin = Self::Origin> + Default;
  168. }
  169. decl_event!(
  170. /// Proposals engine events
  171. pub enum Event<T>
  172. where
  173. <T as Trait>::ProposalId,
  174. MemberId = MemberId<T>,
  175. <T as system::Trait>::BlockNumber,
  176. <T as system::Trait>::AccountId,
  177. <T as stake::Trait>::StakeId,
  178. {
  179. /// Emits on proposal creation.
  180. /// Params:
  181. /// - Member id of a proposer.
  182. /// - Id of a newly created proposal after it was saved in storage.
  183. ProposalCreated(MemberId, ProposalId),
  184. /// Emits on proposal status change.
  185. /// Params:
  186. /// - Id of a updated proposal.
  187. /// - New proposal status
  188. ProposalStatusUpdated(ProposalId, ProposalStatus<BlockNumber, StakeId, AccountId>),
  189. /// Emits on voting for the proposal
  190. /// Params:
  191. /// - Voter - member id of a voter.
  192. /// - Id of a proposal.
  193. /// - Kind of vote.
  194. Voted(MemberId, ProposalId, VoteKind),
  195. }
  196. );
  197. decl_error! {
  198. /// Engine module predefined errors
  199. pub enum Error for Module<T: Trait>{
  200. /// Proposal cannot have an empty title"
  201. EmptyTitleProvided,
  202. /// Proposal cannot have an empty body
  203. EmptyDescriptionProvided,
  204. /// Title is too long
  205. TitleIsTooLong,
  206. /// Description is too long
  207. DescriptionIsTooLong,
  208. /// The proposal does not exist
  209. ProposalNotFound,
  210. /// Proposal is finalized already
  211. ProposalFinalized,
  212. /// The proposal have been already voted on
  213. AlreadyVoted,
  214. /// Not an author
  215. NotAuthor,
  216. /// Max active proposals number exceeded
  217. MaxActiveProposalNumberExceeded,
  218. /// Stake cannot be empty with this proposal
  219. EmptyStake,
  220. /// Stake should be empty for this proposal
  221. StakeShouldBeEmpty,
  222. /// Stake differs from the proposal requirements
  223. StakeDiffersFromRequired,
  224. /// Approval threshold cannot be zero
  225. InvalidParameterApprovalThreshold,
  226. /// Slashing threshold cannot be zero
  227. InvalidParameterSlashingThreshold,
  228. /// Require root origin in extrinsics
  229. RequireRootOrigin,
  230. }
  231. }
  232. // Storage for the proposals engine module
  233. decl_storage! {
  234. pub trait Store for Module<T: Trait> as ProposalEngine{
  235. /// Map proposal by its id.
  236. pub Proposals get(fn proposals): map hasher(blake2_128_concat)
  237. T::ProposalId => ProposalOf<T>;
  238. /// Count of all proposals that have been created.
  239. pub ProposalCount get(fn proposal_count): u32;
  240. /// Map proposal executable code by proposal id.
  241. pub DispatchableCallCode get(fn proposal_codes): map hasher(blake2_128_concat)
  242. T::ProposalId => Vec<u8>;
  243. /// Count of active proposals.
  244. pub ActiveProposalCount get(fn active_proposal_count): u32;
  245. /// Ids of proposals that are open for voting (have not been finalized yet).
  246. pub ActiveProposalIds get(fn active_proposal_ids): map hasher(blake2_128_concat)
  247. T::ProposalId=> ();
  248. /// Ids of proposals that were approved and theirs grace period was not expired.
  249. pub PendingExecutionProposalIds get(fn pending_proposal_ids): map hasher(blake2_128_concat)
  250. T::ProposalId=> ();
  251. /// Double map for preventing duplicate votes. Should be cleaned after usage.
  252. pub VoteExistsByProposalByVoter get(fn vote_by_proposal_by_voter):
  253. double_map hasher(blake2_128_concat) T::ProposalId, hasher(blake2_128_concat) MemberId<T> => VoteKind;
  254. /// Map proposal id by stake id. Required by StakingEventsHandler callback call
  255. pub StakesProposals get(fn stakes_proposals): map hasher(blake2_128_concat)
  256. T::StakeId => T::ProposalId;
  257. }
  258. }
  259. decl_module! {
  260. /// 'Proposal engine' substrate module
  261. pub struct Module<T: Trait> for enum Call where origin: T::Origin {
  262. /// Predefined errors
  263. type Error = Error<T>;
  264. /// Emits an event. Default substrate implementation.
  265. fn deposit_event() = default;
  266. /// Exports const - the fee is applied when cancel the proposal. A fee would be slashed (burned).
  267. const CancellationFee: BalanceOf<T> = T::CancellationFee::get();
  268. /// Exports const - the fee is applied when the proposal gets rejected. A fee would be slashed (burned).
  269. const RejectionFee: BalanceOf<T> = T::RejectionFee::get();
  270. /// Exports const - max allowed proposal title length.
  271. const TitleMaxLength: u32 = T::TitleMaxLength::get();
  272. /// Exports const - max allowed proposal description length.
  273. const DescriptionMaxLength: u32 = T::DescriptionMaxLength::get();
  274. /// Exports const - max simultaneous active proposals number.
  275. const MaxActiveProposalLimit: u32 = T::MaxActiveProposalLimit::get();
  276. /// Vote extrinsic. Conditions: origin must allow votes.
  277. #[weight = 10_000_000] // TODO: adjust weight
  278. pub fn vote(origin, voter_id: MemberId<T>, proposal_id: T::ProposalId, vote: VoteKind) {
  279. T::VoterOriginValidator::ensure_actor_origin(
  280. origin,
  281. voter_id,
  282. )?;
  283. ensure!(<Proposals<T>>::contains_key(proposal_id), Error::<T>::ProposalNotFound);
  284. let mut proposal = Self::proposals(proposal_id);
  285. ensure!(matches!(proposal.status, ProposalStatus::Active{..}), Error::<T>::ProposalFinalized);
  286. let did_not_vote_before = !<VoteExistsByProposalByVoter<T>>::contains_key(
  287. proposal_id,
  288. voter_id,
  289. );
  290. ensure!(did_not_vote_before, Error::<T>::AlreadyVoted);
  291. proposal.voting_results.add_vote(vote.clone());
  292. // mutation
  293. <Proposals<T>>::insert(proposal_id, proposal);
  294. <VoteExistsByProposalByVoter<T>>::insert(proposal_id, voter_id, vote.clone());
  295. Self::deposit_event(RawEvent::Voted(voter_id, proposal_id, vote));
  296. }
  297. /// Cancel a proposal by its original proposer.
  298. #[weight = 10_000_000] // TODO: adjust weight
  299. pub fn cancel_proposal(origin, proposer_id: MemberId<T>, proposal_id: T::ProposalId) {
  300. T::ProposerOriginValidator::ensure_actor_origin(
  301. origin,
  302. proposer_id,
  303. )?;
  304. ensure!(<Proposals<T>>::contains_key(proposal_id), Error::<T>::ProposalNotFound);
  305. let proposal = Self::proposals(proposal_id);
  306. ensure!(proposer_id == proposal.proposer_id, Error::<T>::NotAuthor);
  307. ensure!(matches!(proposal.status, ProposalStatus::Active{..}), Error::<T>::ProposalFinalized);
  308. // mutation
  309. Self::finalize_proposal(proposal_id, ProposalDecisionStatus::Canceled);
  310. }
  311. /// Veto a proposal. Must be root.
  312. #[weight = 10_000_000] // TODO: adjust weight
  313. pub fn veto_proposal(origin, proposal_id: T::ProposalId) {
  314. ensure_root(origin)?;
  315. ensure!(<Proposals<T>>::contains_key(proposal_id), Error::<T>::ProposalNotFound);
  316. let proposal = Self::proposals(proposal_id);
  317. // mutation
  318. if <PendingExecutionProposalIds<T>>::contains_key(proposal_id) {
  319. Self::veto_pending_execution_proposal(proposal_id, proposal);
  320. } else {
  321. ensure!(matches!(proposal.status, ProposalStatus::Active{..}), Error::<T>::ProposalFinalized);
  322. Self::finalize_proposal(proposal_id, ProposalDecisionStatus::Vetoed);
  323. }
  324. }
  325. /// Block finalization. Perform voting period check, vote result tally, approved proposals
  326. /// grace period checks, and proposal execution.
  327. fn on_finalize(_n: T::BlockNumber) {
  328. let finalized_proposals = Self::get_finalized_proposals();
  329. // mutation
  330. // Check vote results. Approved proposals with zero grace period will be
  331. // transitioned to the PendingExecution status.
  332. for proposal_data in finalized_proposals {
  333. <Proposals<T>>::insert(proposal_data.proposal_id, proposal_data.proposal);
  334. Self::finalize_proposal(proposal_data.proposal_id, proposal_data.status);
  335. }
  336. let executable_proposals =
  337. Self::get_approved_proposal_with_expired_grace_period();
  338. // Execute approved proposals with expired grace period
  339. for approved_proosal in executable_proposals {
  340. Self::execute_proposal(approved_proosal);
  341. }
  342. }
  343. }
  344. }
  345. impl<T: Trait> Module<T> {
  346. /// Create proposal. Requires 'proposal origin' membership.
  347. pub fn create_proposal(
  348. account_id: T::AccountId,
  349. proposer_id: MemberId<T>,
  350. parameters: ProposalParameters<T::BlockNumber, types::BalanceOf<T>>,
  351. title: Vec<u8>,
  352. description: Vec<u8>,
  353. stake_balance: Option<types::BalanceOf<T>>,
  354. encoded_dispatchable_call_code: Vec<u8>,
  355. ) -> Result<T::ProposalId, DispatchError> {
  356. Self::ensure_create_proposal_parameters_are_valid(
  357. &parameters,
  358. &title,
  359. &description,
  360. stake_balance,
  361. )?;
  362. // checks passed
  363. // mutation
  364. let next_proposal_count_value = Self::proposal_count() + 1;
  365. let new_proposal_id = next_proposal_count_value;
  366. let proposal_id = T::ProposalId::from(new_proposal_id);
  367. // Check stake_balance for value and create stake if value exists, else take None
  368. // If create_stake() returns error - return error from extrinsic
  369. let stake_id_result = stake_balance
  370. .map(|stake_amount| {
  371. ProposalStakeManager::<T>::create_stake(stake_amount, account_id.clone())
  372. })
  373. .transpose()?;
  374. let mut stake_data = None;
  375. if let Some(stake_id) = stake_id_result {
  376. stake_data = Some(ActiveStake {
  377. stake_id,
  378. source_account_id: account_id,
  379. });
  380. <StakesProposals<T>>::insert(stake_id, proposal_id);
  381. }
  382. let new_proposal = Proposal {
  383. created_at: Self::current_block(),
  384. parameters,
  385. title,
  386. description,
  387. proposer_id,
  388. status: ProposalStatus::Active(stake_data),
  389. voting_results: VotingResults::default(),
  390. };
  391. <Proposals<T>>::insert(proposal_id, new_proposal);
  392. <DispatchableCallCode<T>>::insert(proposal_id, encoded_dispatchable_call_code);
  393. <ActiveProposalIds<T>>::insert(proposal_id, ());
  394. ProposalCount::put(next_proposal_count_value);
  395. Self::increase_active_proposal_counter();
  396. Self::deposit_event(RawEvent::ProposalCreated(proposer_id, proposal_id));
  397. Ok(proposal_id)
  398. }
  399. /// Performs all checks for the proposal creation:
  400. /// - title, body lengths
  401. /// - max active proposal
  402. /// - provided parameters: approval_threshold_percentage and slashing_threshold_percentage > 0
  403. /// - provided stake balance and parameters.required_stake are valid
  404. pub fn ensure_create_proposal_parameters_are_valid(
  405. parameters: &ProposalParameters<T::BlockNumber, types::BalanceOf<T>>,
  406. title: &[u8],
  407. description: &[u8],
  408. stake_balance: Option<types::BalanceOf<T>>,
  409. ) -> DispatchResult {
  410. ensure!(!title.is_empty(), Error::<T>::EmptyTitleProvided);
  411. ensure!(
  412. title.len() as u32 <= T::TitleMaxLength::get(),
  413. Error::<T>::TitleIsTooLong
  414. );
  415. ensure!(
  416. !description.is_empty(),
  417. Error::<T>::EmptyDescriptionProvided
  418. );
  419. ensure!(
  420. description.len() as u32 <= T::DescriptionMaxLength::get(),
  421. Error::<T>::DescriptionIsTooLong
  422. );
  423. ensure!(
  424. (Self::active_proposal_count()) < T::MaxActiveProposalLimit::get(),
  425. Error::<T>::MaxActiveProposalNumberExceeded
  426. );
  427. ensure!(
  428. parameters.approval_threshold_percentage > 0,
  429. Error::<T>::InvalidParameterApprovalThreshold
  430. );
  431. ensure!(
  432. parameters.slashing_threshold_percentage > 0,
  433. Error::<T>::InvalidParameterSlashingThreshold
  434. );
  435. // check stake parameters
  436. if let Some(required_stake) = parameters.required_stake {
  437. if let Some(staked_balance) = stake_balance {
  438. ensure!(
  439. required_stake == staked_balance,
  440. Error::<T>::StakeDiffersFromRequired
  441. );
  442. } else {
  443. return Err(Error::<T>::EmptyStake.into());
  444. }
  445. }
  446. if stake_balance.is_some() && parameters.required_stake.is_none() {
  447. return Err(Error::<T>::StakeShouldBeEmpty.into());
  448. }
  449. Ok(())
  450. }
  451. /// Callback from StakingEventsHandler. Refunds unstaked imbalance back to the source account.
  452. /// There can be a lot of invariant breaks in the scope of this proposal.
  453. /// Such situations are handled by adding error messages to the log.
  454. pub fn refund_proposal_stake(stake_id: T::StakeId, imbalance: NegativeImbalance<T>) {
  455. if <StakesProposals<T>>::contains_key(stake_id) {
  456. let proposal_id = Self::stakes_proposals(stake_id);
  457. if <Proposals<T>>::contains_key(proposal_id) {
  458. let proposal = Self::proposals(proposal_id);
  459. if let ProposalStatus::Active(active_stake_result) = proposal.status {
  460. if let Some(active_stake) = active_stake_result {
  461. let refunding_result = CurrencyOf::<T>::resolve_into_existing(
  462. &active_stake.source_account_id,
  463. imbalance,
  464. );
  465. if refunding_result.is_err() {
  466. print("Broken invariant: cannot refund");
  467. }
  468. }
  469. } else {
  470. print("Broken invariant: proposal status is not Active");
  471. }
  472. } else {
  473. print("Broken invariant: proposal doesn't exist");
  474. }
  475. } else {
  476. print("Broken invariant: stake doesn't exist");
  477. }
  478. }
  479. /// Resets voting results for active proposals.
  480. /// Possible application includes new council elections.
  481. pub fn reset_active_proposals() {
  482. <ActiveProposalIds<T>>::iter().for_each(|(proposal_id, _)| {
  483. <Proposals<T>>::mutate(proposal_id, |proposal| {
  484. proposal.reset_proposal();
  485. <VoteExistsByProposalByVoter<T>>::remove_prefix(&proposal_id);
  486. });
  487. });
  488. }
  489. }
  490. impl<T: Trait> Module<T> {
  491. // Wrapper-function over system::block_number()
  492. fn current_block() -> T::BlockNumber {
  493. <system::Module<T>>::block_number()
  494. }
  495. // Enumerates through active proposals. Tally Voting results.
  496. // Returns proposals with finalized status and id
  497. fn get_finalized_proposals() -> Vec<FinalizedProposal<T>> {
  498. // Enumerate active proposals id and gather finalization data.
  499. // Skip proposals with unfinished voting.
  500. <ActiveProposalIds<T>>::iter()
  501. .filter_map(|(proposal_id, _)| {
  502. // load current proposal
  503. let proposal = Self::proposals(proposal_id);
  504. // Calculates votes, takes in account voting period expiration.
  505. // If voting process is in progress, then decision status is None.
  506. let decision_status = proposal.define_proposal_decision_status(
  507. T::TotalVotersCounter::total_voters_count(),
  508. Self::current_block(),
  509. );
  510. // map to FinalizedProposalData if decision for the proposal is made or return None
  511. decision_status.map(|status| FinalizedProposalData {
  512. proposal_id,
  513. proposal,
  514. status,
  515. finalized_at: Self::current_block(),
  516. })
  517. })
  518. .collect() // compose output vector
  519. }
  520. // Veto approved proposal during its grace period. Saves a new proposal status and removes
  521. // proposal id from the 'PendingExecutionProposalIds'
  522. fn veto_pending_execution_proposal(proposal_id: T::ProposalId, proposal: ProposalOf<T>) {
  523. <PendingExecutionProposalIds<T>>::remove(proposal_id);
  524. let vetoed_proposal_status = ProposalStatus::finalized(
  525. ProposalDecisionStatus::Vetoed,
  526. None,
  527. None,
  528. Self::current_block(),
  529. );
  530. <Proposals<T>>::insert(
  531. proposal_id,
  532. Proposal {
  533. status: vetoed_proposal_status,
  534. ..proposal
  535. },
  536. );
  537. }
  538. // Executes approved proposal code
  539. fn execute_proposal(approved_proposal: ApprovedProposal<T>) {
  540. let proposal_code = Self::proposal_codes(approved_proposal.proposal_id);
  541. let proposal_code_result = T::DispatchableCallCode::decode(&mut &proposal_code[..]);
  542. let approved_proposal_status = match proposal_code_result {
  543. Ok(proposal_code) => {
  544. if let Err(dispatch_error) =
  545. proposal_code.dispatch_bypass_filter(T::Origin::from(RawOrigin::Root))
  546. {
  547. ApprovedProposalStatus::failed_execution(Self::parse_dispatch_error(
  548. dispatch_error.error,
  549. ))
  550. } else {
  551. ApprovedProposalStatus::Executed
  552. }
  553. }
  554. Err(error) => ApprovedProposalStatus::failed_execution(error.what()),
  555. };
  556. let proposal_execution_status = approved_proposal
  557. .finalisation_status_data
  558. .create_approved_proposal_status(approved_proposal_status);
  559. let mut proposal = approved_proposal.proposal;
  560. proposal.status = proposal_execution_status.clone();
  561. <Proposals<T>>::insert(approved_proposal.proposal_id, proposal);
  562. Self::deposit_event(RawEvent::ProposalStatusUpdated(
  563. approved_proposal.proposal_id,
  564. proposal_execution_status,
  565. ));
  566. <PendingExecutionProposalIds<T>>::remove(&approved_proposal.proposal_id);
  567. }
  568. // Performs all actions on proposal finalization:
  569. // - clean active proposal cache
  570. // - update proposal status fields (status, finalized_at)
  571. // - add to pending execution proposal cache if approved
  572. // - slash and unstake proposal stake if stake exists
  573. // - decrease active proposal counter
  574. // - fire an event
  575. // It prints an error message in case of an attempt to finalize the non-active proposal.
  576. fn finalize_proposal(proposal_id: T::ProposalId, decision_status: ProposalDecisionStatus) {
  577. Self::decrease_active_proposal_counter();
  578. <ActiveProposalIds<T>>::remove(&proposal_id.clone());
  579. let mut proposal = Self::proposals(proposal_id);
  580. if let ProposalStatus::Active(active_stake) = proposal.status.clone() {
  581. if let ProposalDecisionStatus::Approved { .. } = decision_status {
  582. <PendingExecutionProposalIds<T>>::insert(proposal_id, ());
  583. }
  584. // deal with stakes if necessary
  585. let slash_balance =
  586. Self::calculate_slash_balance(&decision_status, &proposal.parameters);
  587. let slash_and_unstake_result =
  588. Self::slash_and_unstake(active_stake.clone(), slash_balance);
  589. // create finalized proposal status with error if any
  590. let new_proposal_status = ProposalStatus::finalized(
  591. decision_status,
  592. slash_and_unstake_result.err(),
  593. active_stake,
  594. Self::current_block(),
  595. );
  596. proposal.status = new_proposal_status.clone();
  597. <Proposals<T>>::insert(proposal_id, proposal);
  598. Self::deposit_event(RawEvent::ProposalStatusUpdated(
  599. proposal_id,
  600. new_proposal_status,
  601. ));
  602. } else {
  603. print("Broken invariant: proposal cannot be non-active during the finalisation");
  604. }
  605. }
  606. // Slashes the stake and perform unstake only in case of existing stake
  607. fn slash_and_unstake(
  608. current_stake_data: Option<ActiveStake<T::StakeId, T::AccountId>>,
  609. slash_balance: BalanceOf<T>,
  610. ) -> Result<(), &'static str> {
  611. // only if stake exists
  612. if let Some(stake_data) = current_stake_data {
  613. if !slash_balance.is_zero() {
  614. ProposalStakeManager::<T>::slash(stake_data.stake_id, slash_balance)?;
  615. }
  616. ProposalStakeManager::<T>::remove_stake(stake_data.stake_id)?;
  617. }
  618. Ok(())
  619. }
  620. // Calculates required slash based on finalization ProposalDecisionStatus and proposal parameters.
  621. // Method visibility allows testing.
  622. pub(crate) fn calculate_slash_balance(
  623. decision_status: &ProposalDecisionStatus,
  624. proposal_parameters: &ProposalParameters<T::BlockNumber, types::BalanceOf<T>>,
  625. ) -> types::BalanceOf<T> {
  626. match decision_status {
  627. ProposalDecisionStatus::Rejected | ProposalDecisionStatus::Expired => {
  628. T::RejectionFee::get()
  629. }
  630. ProposalDecisionStatus::Approved { .. } | ProposalDecisionStatus::Vetoed => {
  631. BalanceOf::<T>::zero()
  632. }
  633. ProposalDecisionStatus::Canceled => T::CancellationFee::get(),
  634. ProposalDecisionStatus::Slashed => proposal_parameters
  635. .required_stake
  636. .clone()
  637. .unwrap_or_else(BalanceOf::<T>::zero), // stake if set or zero
  638. }
  639. }
  640. // Enumerates approved proposals and checks their grace period expiration
  641. fn get_approved_proposal_with_expired_grace_period() -> Vec<ApprovedProposal<T>> {
  642. <PendingExecutionProposalIds<T>>::iter()
  643. .filter_map(|(proposal_id, _)| {
  644. let proposal = Self::proposals(proposal_id);
  645. if proposal.is_grace_period_expired(Self::current_block()) {
  646. // this should be true, because it was tested inside is_grace_period_expired()
  647. if let ProposalStatus::Finalized(finalisation_data) = proposal.status.clone() {
  648. Some(ApprovedProposalData {
  649. proposal_id,
  650. proposal,
  651. finalisation_status_data: finalisation_data,
  652. })
  653. } else {
  654. None
  655. }
  656. } else {
  657. None
  658. }
  659. })
  660. .collect()
  661. }
  662. // Increases active proposal counter.
  663. fn increase_active_proposal_counter() {
  664. let next_active_proposal_count_value = Self::active_proposal_count() + 1;
  665. ActiveProposalCount::put(next_active_proposal_count_value);
  666. }
  667. // Decreases active proposal counter down to zero. Decreasing below zero has no effect.
  668. fn decrease_active_proposal_counter() {
  669. let current_active_proposal_counter = Self::active_proposal_count();
  670. if current_active_proposal_counter > 0 {
  671. let next_active_proposal_count_value = current_active_proposal_counter - 1;
  672. ActiveProposalCount::put(next_active_proposal_count_value);
  673. };
  674. }
  675. // Parse dispatchable execution result.
  676. fn parse_dispatch_error(error: DispatchError) -> &'static str {
  677. match error {
  678. DispatchError::BadOrigin => error.into(),
  679. DispatchError::Other(msg) => msg,
  680. DispatchError::CannotLookup => error.into(),
  681. DispatchError::Module {
  682. index: _,
  683. error: _,
  684. message: msg,
  685. } => msg.unwrap_or("Dispatch error."),
  686. }
  687. }
  688. }
  689. // Simplification of the 'FinalizedProposalData' type
  690. type FinalizedProposal<T> = FinalizedProposalData<
  691. <T as Trait>::ProposalId,
  692. <T as system::Trait>::BlockNumber,
  693. MemberId<T>,
  694. types::BalanceOf<T>,
  695. <T as stake::Trait>::StakeId,
  696. <T as system::Trait>::AccountId,
  697. >;
  698. // Simplification of the 'ApprovedProposalData' type
  699. type ApprovedProposal<T> = ApprovedProposalData<
  700. <T as Trait>::ProposalId,
  701. <T as system::Trait>::BlockNumber,
  702. MemberId<T>,
  703. types::BalanceOf<T>,
  704. <T as stake::Trait>::StakeId,
  705. <T as system::Trait>::AccountId,
  706. >;
  707. // Simplification of the 'Proposal' type
  708. type ProposalOf<T> = Proposal<
  709. <T as system::Trait>::BlockNumber,
  710. MemberId<T>,
  711. types::BalanceOf<T>,
  712. <T as stake::Trait>::StakeId,
  713. <T as system::Trait>::AccountId,
  714. >;