Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referendum threshold #16

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions substrate/frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@
use frame_system::{pallet_prelude::*, RawOrigin};

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]

Check warning on line 421 in substrate/frame/democracy/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

trait `Store` is never used
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

Expand Down Expand Up @@ -780,6 +780,12 @@
recipient: T::AccountId,
deposit: BalanceOf<T>,
},
/// Referendum threshold was updated.
ReferendumThresholdUpdated {
ref_index: ReferendumIndex,
old_threshold: VoteThreshold,
new_threshold: VoteThreshold,
},
}

#[pallet::error]
Expand Down Expand Up @@ -1610,6 +1616,37 @@
}
.into())
}

/// Sets new threshold for the ongoing referendum.
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_referendum_threshold(
origin: OriginFor<T>,
ref_index: ReferendumIndex,
new_threshold: VoteThreshold,
) -> DispatchResult {
ensure_root(origin)?;

ReferendumInfoOf::<T>::try_mutate(ref_index, |info_opt| {
let status = info_opt
.as_mut()
.and_then(|info| match info {
ReferendumInfo::Ongoing(status) => Some(status),
_ => None,
})
.ok_or(Error::<T>::ReferendumInvalid)?;

let old_threshold = status.threshold;
status.threshold = new_threshold;

Self::deposit_event(Event::<T>::ReferendumThresholdUpdated {
ref_index,
old_threshold,
new_threshold,
});

Ok(())
})
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions substrate/frame/democracy/src/tests/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

//! The tests for normal voting functionality.

use frame_system::RawOrigin;

use super::*;

#[test]
Expand Down Expand Up @@ -155,6 +157,50 @@ fn controversial_voting_should_work() {
});
}

#[test]
fn set_threshold_should_work() {
new_test_ext().execute_with(|| {
let r = Democracy::inject_referendum(
2,
set_balance_proposal_hash_and_note(2),
VoteThreshold::SuperMajorityAgainst,
0,
);

assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
assert_ok!(Democracy::vote(Origin::signed(2), r, nay(2)));
assert_ok!(Democracy::vote(Origin::signed(3), r, nay(3)));
assert_ok!(Democracy::vote(Origin::signed(4), r, aye(4)));

assert_eq!(
tally(r),
Tally {
ayes: 5,
nays: 5,
turnout: 100
}
);
assert_ok!(Democracy::set_referendum_threshold(
RawOrigin::Root.into(),
r,
VoteThreshold::SuperMajorityApprove
));

next_block();
next_block();

assert_eq!(Balances::free_balance(42), 0);
assert_noop!(
Democracy::set_referendum_threshold(
RawOrigin::Root.into(),
r,
VoteThreshold::SuperMajorityApprove
),
Error::<Test>::ReferendumInvalid
);
});
}

#[test]
fn controversial_low_turnout_voting_should_work() {
new_test_ext().execute_with(|| {
Expand Down
Loading