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
+33 -11
View File
@@ -260,10 +260,7 @@ mod tests {
construct_block(
1,
[69u8; 32].into(),
// Blake
// hex!("3437bf4b182ab17bb322af5c67e55f6be487a77084ad2b4e27ddac7242e4ad21").into(),
// Keccak
hex!("508a68a0918f614b86b2ccfd0975754f6d2abe1026a34e42d6d8d5abdf4db010").into(),
hex!("ddfc4d60889b25215f4fe6ead4e38b7522fa20809a793476eae3ad5ab2d9c399").into(),
vec![CheckedExtrinsic {
signed: Some(alice()),
index: 0,
@@ -276,10 +273,7 @@ mod tests {
construct_block(
2,
block1().1,
// Blake
// hex!("741fcb660e6fa9f625fbcd993b49f6c1cc4040f5e0cc8727afdedf11fd3c464b").into(),
// Keccak
hex!("a72ec570c7642d9ad06ef0e5dd37be65fb04b71e0ab52b3927d760ed6c777a1f").into(),
hex!("2b4464c7e0d51325505663ae2ebd2246fcefc5cb998e9c29d8030c559cbbf27f").into(),
vec![
CheckedExtrinsic {
signed: Some(bob()),
@@ -299,9 +293,6 @@ mod tests {
construct_block(
1,
[69u8; 32].into(),
// Blake
// hex!("2c7231a9c210a7aa4bea169d944bc4aaacd517862b244b8021236ffa7f697991").into(),
// Keccak
hex!("e45221804da3a3609454d4e09debe6364cc6af63c2ff067d802d1af62fea32ae").into(),
vec![CheckedExtrinsic {
signed: Some(alice()),
@@ -325,6 +316,15 @@ mod tests {
phase: Phase::ApplyExtrinsic(0),
event: Event::balances(balances::RawEvent::NewAccount(bob(), 1, balances::NewAccountOutcome::NoHint))
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::balances(balances::RawEvent::Transfer(
hex!["d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f"].into(),
hex!["d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9"].into(),
69,
0
))
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::system(system::Event::ExtrinsicSuccess)
@@ -338,10 +338,32 @@ mod tests {
assert_eq!(Balances::total_balance(&alice()), 30);
assert_eq!(Balances::total_balance(&bob()), 78);
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::balances(
balances::RawEvent::Transfer(
hex!["d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9"].into(),
hex!["d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f"].into(),
5,
0
)
)
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::system(system::Event::ExtrinsicSuccess)
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::balances(
balances::RawEvent::Transfer(
hex!["d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f"].into(),
hex!["d7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9"].into(),
15,
0
)
)
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::system(system::Event::ExtrinsicSuccess)
+2 -1
View File
@@ -148,6 +148,7 @@ pub type Staking = staking::Module<Runtime>;
impl democracy::Trait for Runtime {
type Proposal = Call;
type Event = Event;
}
/// Democracy module for this concrete runtime.
@@ -162,7 +163,7 @@ pub type CouncilVoting = council::voting::Module<Runtime>;
impl_outer_event! {
pub enum Event for Runtime {
balances, session, staking
balances, session, staking, democracy
}
}
@@ -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> {