Democracy module events (#697)

* Democracy module events

* Fix demo runtime

* Fix

* Extra event in balances

* Missing event

* Fix test

* Fix council

* Fix test
This commit is contained in:
Gav Wood
2018-09-10 14:40:35 +02:00
committed by GitHub
parent 393c3b5af7
commit 0aefb50689
11 changed files with 85 additions and 24 deletions
@@ -69,7 +69,8 @@ pub type Address<T> = RawAddress<<T as system::Trait>::AccountId, <T as Trait>::
pub type Event<T> = RawEvent<
<T as system::Trait>::AccountId,
<T as Trait>::AccountIndex
<T as Trait>::AccountIndex,
<T as Trait>::Balance,
>;
/// The account with the given id was killed.
@@ -142,15 +143,17 @@ decl_module! {
/// An event in this module.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawEvent<AccountId, AccountIndex> {
pub enum RawEvent<AccountId, AccountIndex, Balance> {
/// A new account was created.
NewAccount(AccountId, AccountIndex, NewAccountOutcome),
/// An account was reaped.
ReapedAccount(AccountId),
/// Transfer succeeded (from, to, value, fees).
Transfer(AccountId, AccountId, Balance, Balance),
}
impl<A, I> From<RawEvent<A, I>> for () {
fn from(_: RawEvent<A, I>) -> () { () }
impl<A, I, B> From<RawEvent<A, I, B>> for () {
fn from(_: RawEvent<A, I, B>) -> () { () }
}
decl_storage! {
@@ -312,6 +315,7 @@ impl<T: Trait> Module<T> {
Self::set_free_balance(&transactor, new_from_balance);
Self::decrease_total_stake_by(fee);
Self::set_free_balance_creating(&dest, new_to_balance);
Self::deposit_event(RawEvent::Transfer(transactor, dest, value, fee));
}
Ok(())
@@ -665,6 +665,7 @@ mod tests {
}
impl democracy::Trait for Test {
type Proposal = Call;
type Event = ();
}
impl Trait for Test {}
@@ -61,6 +61,8 @@ pub type ReferendumIndex = u32;
pub trait Trait: balances::Trait + Sized {
type Proposal: Parameter + Dispatchable<Origin=Self::Origin> + IsSubType<Module<Self>> + MaybeSerializeDebug;
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_module! {
@@ -106,8 +108,34 @@ decl_storage! {
}
}
/// An event in this module.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawEvent<Balance, AccountId> {
Tabled(PropIndex, Balance, Vec<AccountId>),
Started(ReferendumIndex, VoteThreshold),
Passed(ReferendumIndex),
NotPassed(ReferendumIndex),
Cancelled(ReferendumIndex),
Executed(ReferendumIndex, bool),
}
impl<B, A> From<RawEvent<B, A>> for () {
fn from(_: RawEvent<B, A>) -> () { () }
}
pub type Event<T> = RawEvent<
<T as balances::Trait>::Balance,
<T as system::Trait>::AccountId,
>;
impl<T: Trait> Module<T> {
/// Deposit one of this module's events.
fn deposit_event(event: Event<T>) {
<system::Module<T>>::deposit_event(<T as Trait>::Event::from(event).into());
}
// exposed immutables.
/// Get the amount locked in support of `proposal`; `None` if proposal isn't a valid proposal
@@ -180,16 +208,14 @@ impl<T: Trait> Module<T> {
}
/// Vote in a referendum. If `approve_proposal` is true, the vote is to enact the proposal;
/// false would be a vote to keep the status quo..
/// false would be a vote to keep the status quo.
fn vote(origin: T::Origin, ref_index: ReferendumIndex, approve_proposal: bool) -> Result {
let who = ensure_signed(origin)?;
ensure!(Self::is_active_referendum(ref_index), "vote given for invalid referendum.");
ensure!(!<balances::Module<T>>::total_balance(&who).is_zero(),
"transactor must have balance to signal approval.");
if !<VoteOf<T>>::exists(&(ref_index, who.clone())) {
let mut voters = Self::voters_for(ref_index);
voters.push(who.clone());
<VotersFor<T>>::insert(ref_index, voters);
<VotersFor<T>>::mutate(ref_index, |voters| voters.push(who.clone()));
}
<VoteOf<T>>::insert(&(ref_index, who), approve_proposal);
Ok(())
@@ -221,6 +247,7 @@ impl<T: Trait> Module<T> {
/// Remove a referendum. Can be called directly by the council.
pub fn internal_cancel_referendum(ref_index: ReferendumIndex) {
Self::deposit_event(RawEvent::Cancelled(ref_index));
<Module<T>>::clear_referendum(ref_index);
}
@@ -239,6 +266,7 @@ impl<T: Trait> Module<T> {
<ReferendumCount<T>>::put(ref_index + 1);
<ReferendumInfoOf<T>>::insert(ref_index, (end, proposal, vote_threshold));
Self::deposit_event(RawEvent::Started(ref_index, vote_threshold));
Ok(ref_index)
}
@@ -261,15 +289,15 @@ impl<T: Trait> Module<T> {
.max_by_key(|x| Self::locked_for((x.1).0).unwrap_or_else(Zero::zero)/*defensive only: All current public proposals have an amount locked*/)
{
let (prop_index, proposal, _) = public_props.swap_remove(winner_index);
<PublicProps<T>>::put(public_props);
if let Some((deposit, depositors)) = <DepositOf<T>>::take(prop_index) {//: (T::Balance, Vec<T::AccountId>) =
// refund depositors
for d in &depositors {
<balances::Module<T>>::unreserve(d, deposit);
}
<PublicProps<T>>::put(public_props);
Self::deposit_event(RawEvent::Tabled(prop_index, deposit, depositors));
Self::inject_referendum(now + Self::voting_period(), proposal, VoteThreshold::SuperMajorityApprove)?;
} else {
return Err("depositors always exist for current proposals")
}
}
}
@@ -280,7 +308,11 @@ impl<T: Trait> Module<T> {
let total_stake = <balances::Module<T>>::total_stake();
Self::clear_referendum(index);
if vote_threshold.approved(approve, against, total_stake) {
proposal.dispatch(system::RawOrigin::Root.into())?;
Self::deposit_event(RawEvent::Passed(index));
let ok = proposal.dispatch(system::RawOrigin::Root.into()).is_ok();
Self::deposit_event(RawEvent::Executed(index, ok));
} else {
Self::deposit_event(RawEvent::NotPassed(index));
}
<NextTally<T>>::put(index + 1);
}
@@ -388,6 +420,7 @@ mod tests {
}
impl Trait for Test {
type Proposal = Call;
type Event = ();
}
fn new_test_ext() -> runtime_io::TestExternalities<KeccakHasher> {