From c0ce0751bc054524492c44f6124aa3f4c7ef7468 Mon Sep 17 00:00:00 2001 From: muharem Date: Tue, 14 Jun 2022 11:54:59 +0200 Subject: [PATCH 1/6] Democracy.fast_track not allowed with zero voting period --- frame/democracy/src/lib.rs | 4 +++- frame/democracy/src/tests/fast_tracking.rs | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/frame/democracy/src/lib.rs b/frame/democracy/src/lib.rs index 4e11c207657f0..7a485c8998aa6 100644 --- a/frame/democracy/src/lib.rs +++ b/frame/democracy/src/lib.rs @@ -600,6 +600,8 @@ pub mod pallet { MaxVotesReached, /// Maximum number of proposals reached. TooManyProposals, + /// Voting period too low + VotingPeriodLow, } #[pallet::hooks] @@ -830,7 +832,7 @@ pub mod pallet { T::InstantOrigin::ensure_origin(ensure_instant)?; ensure!(T::InstantAllowed::get(), Error::::InstantNotAllowed); } - + ensure!(voting_period != T::BlockNumber::zero(), Error::::VotingPeriodLow); let (e_proposal_hash, threshold) = >::get().ok_or(Error::::ProposalMissing)?; ensure!( diff --git a/frame/democracy/src/tests/fast_tracking.rs b/frame/democracy/src/tests/fast_tracking.rs index 7a15bb8de4dac..7c876ed0ed0ca 100644 --- a/frame/democracy/src/tests/fast_tracking.rs +++ b/frame/democracy/src/tests/fast_tracking.rs @@ -66,7 +66,11 @@ fn instant_referendum_works() { Democracy::fast_track(Origin::signed(6), h, 1, 0), Error::::InstantNotAllowed ); - INSTANT_ALLOWED.with(|v| *v.borrow_mut() = true); + InstantAllowed::set(true); + assert_noop!( + Democracy::fast_track(Origin::signed(6), h, 0, 0), + Error::::VotingPeriodLow + ); assert_ok!(Democracy::fast_track(Origin::signed(6), h, 1, 0)); assert_eq!( Democracy::referendum_status(0), From 601cbfb2ca8e7753d4d163d4c8822032cc5f9c46 Mon Sep 17 00:00:00 2001 From: muharem Date: Wed, 15 Jun 2022 00:56:12 +0200 Subject: [PATCH 2/6] revert static parameter alter line --- frame/democracy/src/tests/fast_tracking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/democracy/src/tests/fast_tracking.rs b/frame/democracy/src/tests/fast_tracking.rs index 7c876ed0ed0ca..f38458a5c4639 100644 --- a/frame/democracy/src/tests/fast_tracking.rs +++ b/frame/democracy/src/tests/fast_tracking.rs @@ -66,7 +66,7 @@ fn instant_referendum_works() { Democracy::fast_track(Origin::signed(6), h, 1, 0), Error::::InstantNotAllowed ); - InstantAllowed::set(true); + INSTANT_ALLOWED.with(|v| *v.borrow_mut() = true); assert_noop!( Democracy::fast_track(Origin::signed(6), h, 0, 0), Error::::VotingPeriodLow From 6a91df31478e607dd22d99b7736964d3ac98086d Mon Sep 17 00:00:00 2001 From: muharem Date: Fri, 17 Jun 2022 15:50:15 +0200 Subject: [PATCH 3/6] ensure voting period greater zero --- frame/democracy/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/democracy/src/lib.rs b/frame/democracy/src/lib.rs index 7a485c8998aa6..9ab50829ff1af 100644 --- a/frame/democracy/src/lib.rs +++ b/frame/democracy/src/lib.rs @@ -832,7 +832,7 @@ pub mod pallet { T::InstantOrigin::ensure_origin(ensure_instant)?; ensure!(T::InstantAllowed::get(), Error::::InstantNotAllowed); } - ensure!(voting_period != T::BlockNumber::zero(), Error::::VotingPeriodLow); + ensure!(voting_period > T::BlockNumber::zero(), Error::::VotingPeriodLow); let (e_proposal_hash, threshold) = >::get().ok_or(Error::::ProposalMissing)?; ensure!( From ebb2b75bf5f4c23182de0a1256e23f13ef6374b0 Mon Sep 17 00:00:00 2001 From: muharem Date: Mon, 20 Jun 2022 13:31:20 +0200 Subject: [PATCH 4/6] update doc for fast_track --- frame/democracy/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frame/democracy/src/lib.rs b/frame/democracy/src/lib.rs index 9ab50829ff1af..443b8579116d0 100644 --- a/frame/democracy/src/lib.rs +++ b/frame/democracy/src/lib.rs @@ -802,8 +802,9 @@ pub mod pallet { /// The dispatch of this call must be `FastTrackOrigin`. /// /// - `proposal_hash`: The hash of the current external proposal. - /// - `voting_period`: The period that is allowed for voting on this proposal. Increased to - /// `FastTrackVotingPeriod` if too low. + /// - `voting_period`: The period that is allowed for voting on this proposal. + /// Must be always greater than zero. + /// For `FastTrackOrigin` must be equal or greater than `FastTrackVotingPeriod`. /// - `delay`: The number of block after voting has ended in approval and this should be /// enacted. This doesn't have a minimum amount. /// From 2e06e88f26c5ea482c9deffcd0ca79ecd67aae97 Mon Sep 17 00:00:00 2001 From: muharem Date: Wed, 22 Jun 2022 10:36:55 +0200 Subject: [PATCH 5/6] unit test: instant fast track to the next block referendum is backed --- frame/democracy/src/tests/fast_tracking.rs | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/frame/democracy/src/tests/fast_tracking.rs b/frame/democracy/src/tests/fast_tracking.rs index f38458a5c4639..ff7d1d094a2af 100644 --- a/frame/democracy/src/tests/fast_tracking.rs +++ b/frame/democracy/src/tests/fast_tracking.rs @@ -85,6 +85,60 @@ fn instant_referendum_works() { }); } +#[test] +fn instant_next_block_referendum_backed() { + new_test_ext().execute_with(|| { + // arrange + let start_block_number = 10; + let majority_origin_id = 3; + let instant_origin_id = 6; + let voting_period = 1; + let proposal_hash = set_balance_proposal_hash_and_note(2); + let delay = 2; // has no affect on test + + // init + System::set_block_number(start_block_number); + InstantAllowed::set(true); + + // propose with majority origin + assert_ok!(Democracy::external_propose_majority( + Origin::signed(majority_origin_id), + proposal_hash + )); + + // fast track with instant origin and voting period pointing to the next block + assert_ok!(Democracy::fast_track( + Origin::signed(instant_origin_id), + proposal_hash, + voting_period, + delay + )); + + // fetch the status of the only referendum at index 0 + assert_eq!( + Democracy::referendum_status(0), + Ok(ReferendumStatus { + end: start_block_number + voting_period, + proposal_hash, + threshold: VoteThreshold::SimpleMajority, + delay, + tally: Tally { ayes: 0, nays: 0, turnout: 0 }, + }) + ); + + // referendum expected to be backed with the start of the next block + next_block(); + + // assert no active referendums + assert_noop!(Democracy::referendum_status(0), Error::::ReferendumInvalid); + // the only referendum in the storage is finished and not approved + assert_eq!( + ReferendumInfoOf::::get(0).unwrap(), + ReferendumInfo::Finished { approved: false, end: start_block_number + voting_period } + ); + }); +} + #[test] fn fast_track_referendum_fails_when_no_simple_majority() { new_test_ext().execute_with(|| { From 88dbf432f63d9eb0e4bb13fa1e686fd953aaf7bc Mon Sep 17 00:00:00 2001 From: muharem Date: Wed, 22 Jun 2022 11:15:15 +0200 Subject: [PATCH 6/6] fix typos in comments --- frame/democracy/src/tests/fast_tracking.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/democracy/src/tests/fast_tracking.rs b/frame/democracy/src/tests/fast_tracking.rs index ff7d1d094a2af..caf83c6d46120 100644 --- a/frame/democracy/src/tests/fast_tracking.rs +++ b/frame/democracy/src/tests/fast_tracking.rs @@ -94,7 +94,7 @@ fn instant_next_block_referendum_backed() { let instant_origin_id = 6; let voting_period = 1; let proposal_hash = set_balance_proposal_hash_and_note(2); - let delay = 2; // has no affect on test + let delay = 2; // has no effect on test // init System::set_block_number(start_block_number); @@ -126,7 +126,7 @@ fn instant_next_block_referendum_backed() { }) ); - // referendum expected to be backed with the start of the next block + // referendum expected to be baked with the start of the next block next_block(); // assert no active referendums