Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Cleave voting from council
  • Loading branch information
gavofyork committed Sep 10, 2018
commit aef688fb824c2025d641ba03d4768c189347fb03
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions substrate/runtime/council/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ serde_derive = { version = "1.0", optional = true }
safe-mix = { version = "1.0", default_features = false}
substrate-keyring = { path = "../../keyring", optional = true }
substrate-codec = { path = "../../codec", default_features = false }
substrate-codec-derive = { path = "../../codec/derive", default_features = false }
substrate-primitives = { path = "../../primitives", default_features = false }
substrate-runtime-std = { path = "../../runtime-std", default_features = false }
substrate-runtime-io = { path = "../../runtime-io", default_features = false }
Expand All @@ -29,6 +30,7 @@ std = [
"safe-mix/std",
"substrate-keyring",
"substrate-codec/std",
"substrate-codec-derive/std",
"substrate-primitives/std",
"substrate-runtime-std/std",
"substrate-runtime-io/std",
Expand Down
47 changes: 40 additions & 7 deletions substrate/runtime/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern crate serde_derive;

extern crate integer_sqrt;
extern crate substrate_codec as codec;
#[macro_use] extern crate substrate_codec_derive;
extern crate substrate_primitives;
#[cfg(feature = "std")] extern crate substrate_keyring as keyring;
#[macro_use] extern crate substrate_runtime_std as rstd;
Expand All @@ -40,9 +41,9 @@ extern crate substrate_runtime_system as system;
use rstd::prelude::*;
#[cfg(feature = "std")]
use std::collections::HashMap;
use primitives::traits::{Zero, One, As, Lookup};
use substrate_runtime_support::{StorageValue, StorageMap};
use substrate_runtime_support::dispatch::Result;
use primitives::traits::{Zero, One, As, Lookup, OnFinalise};
use runtime_io::print;
use substrate_runtime_support::{StorageValue, StorageMap, dispatch::Result};
use balances::address::Address;
use system::{ensure_signed, ensure_root};

Expand Down Expand Up @@ -104,7 +105,9 @@ pub mod voting;

pub type VoteIndex = u32;

pub trait Trait: democracy::Trait {}
pub trait Trait: democracy::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
Expand Down Expand Up @@ -176,8 +179,29 @@ decl_storage! {
}
}

pub type Event<T> = RawEvent<<T as system::Trait>::Hash>;

/// An event in this module.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawEvent<Hash> {
/// A voting tally has happened for a referendum cancelation vote. Last three are yes, no, abstain counts.
TallyCancelation(Hash, u32, u32, u32),
/// A voting tally has happened for a referendum vote. Last three are yes, no, abstain counts.
TallyReferendum(Hash, u32, u32, u32),
}

impl<N> From<RawEvent<N>> for () {
fn from(_: RawEvent<N>) -> () { () }
}

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.

/// True if we're currently in a presentation period.
Expand Down Expand Up @@ -552,6 +576,15 @@ impl<T: Trait> Module<T> {
}
}

impl<T: Trait> OnFinalise<T::BlockNumber> for Module<T> {
fn on_finalise(n: T::BlockNumber) {
if let Err(e) = Self::end_block(n) {
print("Guru meditation");
print(e);
}
}
}

#[cfg(feature = "std")]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -569,14 +602,13 @@ pub struct GenesisConfig<T: Trait> {
pub term_duration: T::BlockNumber,
pub inactive_grace_period: T::BlockNumber,


// for the council's votes.
pub cooloff_period: T::BlockNumber,
pub voting_period: T::BlockNumber,
}

#[cfg(feature = "std")]
impl<T: Trait> Default for GenesisConfig<T> {
impl<T: Trait + voting::Trait> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
candidacy_bond: T::Balance::sa(9),
Expand All @@ -596,7 +628,7 @@ impl<T: Trait> Default for GenesisConfig<T> {
}

#[cfg(feature = "std")]
impl<T: Trait> primitives::BuildStorage for GenesisConfig<T>
impl<T: Trait + voting::Trait> primitives::BuildStorage for GenesisConfig<T>
{
fn build_storage(self) -> ::std::result::Result<HashMap<Vec<u8>, Vec<u8>>, String> {
use codec::Encode;
Expand Down Expand Up @@ -714,6 +746,7 @@ mod tests {
pub type Balances = balances::Module<Test>;
pub type Democracy = democracy::Module<Test>;
pub type Council = Module<Test>;
pub type CouncilVoting = voting::Module<Test>;

#[test]
fn params_should_work() {
Expand Down
23 changes: 12 additions & 11 deletions substrate/runtime/council/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ decl_storage! {
}
}

pub type Event<T> = RawEvent<<T as system::Trait>::BlockNumber>;
pub type Event<T> = RawEvent<<T as system::Trait>::Hash>;

/// An event in this module.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawEvent<BlockNumber> {
pub enum RawEvent<Hash> {
/// A voting tally has happened for a referendum cancelation vote. Last three are yes, no, abstain counts.
TallyCancelation(Hash, u32, u32, u32),
/// A voting tally has happened for a referendum vote. Last three are yes, no, abstain counts.
TallyReferendum(Hash, u32, u32, u32),
}

impl<N> From<RawEvent<N>> for () {
Expand Down Expand Up @@ -113,17 +117,16 @@ impl<T: Trait> Module<T> {

<ProposalOf<T>>::insert(proposal_hash, *proposal);
<ProposalVoters<T>>::insert(proposal_hash, vec![who.clone()]);
<CouncilVoteOf<T>>::insert((proposal_hash, who), true);
<CouncilVoteOf<T>>::insert((proposal_hash, who.clone()), true);

Ok(())
}

fn vote(origin: T::Origin, proposal: T::Hash, approve: bool) -> Result {
let who = ensure_signed(origin)?;

if Self::vote_of((proposal, who.clone())).is_none() {
let mut voters = Self::proposal_voters(&proposal);
voters.push(who.clone());
<ProposalVoters<T>>::insert(proposal, voters);
<ProposalVoters<T>>::mutate(proposal, |voters| voters.push(who.clone()));
}
<CouncilVoteOf<T>>::insert((proposal, who), approve);
Ok(())
Expand Down Expand Up @@ -208,10 +211,12 @@ impl<T: Trait> Module<T> {
while let Some((proposal, proposal_hash)) = Self::take_proposal_if_expiring_at(now) {
let tally = Self::take_tally(&proposal_hash);
if let Some(&democracy::Call::cancel_referendum(ref_index)) = IsSubType::<democracy::Module<T>>::is_aux_sub_type(&proposal) {
Self::deposit_event(RawEvent::TallyCancelation(proposal_hash, tally.0, tally.1, tally.2));
if let (_, 0, 0) = tally {
<democracy::Module<T>>::internal_cancel_referendum(ref_index);
}
} else {
Self::deposit_event(RawEvent::TallyReferendum(proposal_hash.clone(), tally.0, tally.1, tally.2));
if tally.0 > tally.1 + tally.2 {
Self::kill_veto_of(&proposal_hash);
match tally {
Expand All @@ -225,16 +230,12 @@ impl<T: Trait> Module<T> {
}
}

impl<T: Trait> OnFinalise<T::BlockNumber> for Council<T> {
impl<T: Trait> OnFinalise<T::BlockNumber> for Module<T> {
fn on_finalise(n: T::BlockNumber) {
if let Err(e) = Self::end_block(n) {
print("Guru meditation");
print(e);
}
if let Err(e) = <Module<T>>::end_block(n) {
print("Guru meditation");
print(e);
}
}
}

Expand Down