Skip to content

Commit c9ac873

Browse files
committed
Merge branch 'referendum-threshold'
2 parents 49c162e + 133a54a commit c9ac873

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

substrate/frame/democracy/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,12 @@ pub mod pallet {
780780
recipient: T::AccountId,
781781
deposit: BalanceOf<T>,
782782
},
783+
/// Referendum threshold was updated.
784+
ReferendumThresholdUpdated {
785+
ref_index: ReferendumIndex,
786+
old_threshold: VoteThreshold,
787+
new_threshold: VoteThreshold,
788+
},
783789
}
784790

785791
#[pallet::error]
@@ -1610,6 +1616,37 @@ pub mod pallet {
16101616
}
16111617
.into())
16121618
}
1619+
1620+
/// Sets new threshold for the ongoing referendum.
1621+
#[pallet::weight(T::DbWeight::get().writes(1))]
1622+
pub fn set_referendum_threshold(
1623+
origin: OriginFor<T>,
1624+
ref_index: ReferendumIndex,
1625+
new_threshold: VoteThreshold,
1626+
) -> DispatchResult {
1627+
ensure_root(origin)?;
1628+
1629+
ReferendumInfoOf::<T>::try_mutate(ref_index, |info_opt| {
1630+
let status = info_opt
1631+
.as_mut()
1632+
.and_then(|info| match info {
1633+
ReferendumInfo::Ongoing(status) => Some(status),
1634+
_ => None,
1635+
})
1636+
.ok_or(Error::<T>::ReferendumInvalid)?;
1637+
1638+
let old_threshold = status.threshold;
1639+
status.threshold = new_threshold;
1640+
1641+
Self::deposit_event(Event::<T>::ReferendumThresholdUpdated {
1642+
ref_index,
1643+
old_threshold,
1644+
new_threshold,
1645+
});
1646+
1647+
Ok(())
1648+
})
1649+
}
16131650
}
16141651
}
16151652

substrate/frame/democracy/src/tests/voting.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
//! The tests for normal voting functionality.
1919
20+
use frame_system::RawOrigin;
21+
2022
use super::*;
2123

2224
#[test]
@@ -155,6 +157,50 @@ fn controversial_voting_should_work() {
155157
});
156158
}
157159

160+
#[test]
161+
fn set_threshold_should_work() {
162+
new_test_ext().execute_with(|| {
163+
let r = Democracy::inject_referendum(
164+
2,
165+
set_balance_proposal_hash_and_note(2),
166+
VoteThreshold::SuperMajorityAgainst,
167+
0,
168+
);
169+
170+
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
171+
assert_ok!(Democracy::vote(Origin::signed(2), r, nay(2)));
172+
assert_ok!(Democracy::vote(Origin::signed(3), r, nay(3)));
173+
assert_ok!(Democracy::vote(Origin::signed(4), r, aye(4)));
174+
175+
assert_eq!(
176+
tally(r),
177+
Tally {
178+
ayes: 5,
179+
nays: 5,
180+
turnout: 100
181+
}
182+
);
183+
assert_ok!(Democracy::set_referendum_threshold(
184+
RawOrigin::Root.into(),
185+
r,
186+
VoteThreshold::SuperMajorityApprove
187+
));
188+
189+
next_block();
190+
next_block();
191+
192+
assert_eq!(Balances::free_balance(42), 0);
193+
assert_noop!(
194+
Democracy::set_referendum_threshold(
195+
RawOrigin::Root.into(),
196+
r,
197+
VoteThreshold::SuperMajorityApprove
198+
),
199+
Error::<Test>::ReferendumInvalid
200+
);
201+
});
202+
}
203+
158204
#[test]
159205
fn controversial_low_turnout_voting_should_work() {
160206
new_test_ext().execute_with(|| {

0 commit comments

Comments
 (0)