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

Jba khalifa/issue424 1 #426

Merged
merged 2 commits into from
Jul 30, 2021
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
3 changes: 1 addition & 2 deletions lib-serml/democracy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
]
16 changes: 5 additions & 11 deletions lib-serml/democracy/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# Setheum Democracy Pallet
# Democracy Pallet

## Overview

In Setheum Democracy, it is a multi-currency democracy where a participant can
lock not just one type of currency but a multiple.
This provides multiple governance tokens not just one.
Supports MultiCurrency Governance Model. In Setheum, it is three
(DNAR, SETT and DRAM).
But only the native currency if allowed for voting while the others are allowed for proposing referendum..
- [`democracy::Trait`](https://docs.rs/pallet-democracy/latest/pallet_democracy/trait.Trait.html)
- [`Call`](https://docs.rs/pallet-democracy/latest/pallet_democracy/enum.Call.html)

The Democracy pallet handles the administration of general stakeholder voting. It is based on the [FRAME Democracy Pallet](https://github.com/paritytech/substrate/frame/democracy).
## Overview

## FRAME Democracy Pallet Overview
The Democracy pallet handles the administration of general stakeholder voting.

There are two different queues that a proposal can be added to before it
becomes a referendum, 1) the proposal queue consisting of all public proposals
Expand Down
53 changes: 20 additions & 33 deletions lib-serml/democracy/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Setheum Democracy benchmarking.
// TODO: Update!
//! Democracy pallet benchmarking.

use super::*;

use frame_benchmarking::{benchmarks, account, whitelist_account, impl_benchmark_test_suite};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelist_account};
use frame_support::{
assert_noop, assert_ok,
traits::{Currency, Get, EnsureOrigin, OnInitialize, UnfilteredDispatchable, schedule::DispatchTime},
traits::{
schedule::DispatchTime, Currency, EnsureOrigin, Get, OnInitialize, UnfilteredDispatchable,
},
};
use frame_system::{RawOrigin, Pallet as System, self};
use sp_runtime::traits::{Bounded, One};
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::{BadOrigin, Bounded, One};

use crate::Pallet as Democracy;

Expand All @@ -50,11 +51,7 @@ fn add_proposal<T: Config>(n: u32) -> Result<T::Hash, &'static str> {
let value = T::MinimumDeposit::get();
let proposal_hash: T::Hash = T::Hashing::hash_of(&n);

Democracy::<T>::propose(
RawOrigin::Signed(other).into(),
proposal_hash,
value.into(),
)?;
Democracy::<T>::propose(RawOrigin::Signed(other).into(), proposal_hash, value.into())?;

Ok(proposal_hash)
}
Expand All @@ -77,20 +74,15 @@ fn add_referendum<T: Config>(n: u32) -> Result<ReferendumIndex, &'static str> {
63,
frame_system::RawOrigin::Root.into(),
Call::enact_proposal(proposal_hash, referendum_index).into(),
).map_err(|_| "failed to schedule named")?;
)
.map_err(|_| "failed to schedule named")?;
Ok(referendum_index)
}

fn account_vote<T: Config>(b: BalanceOf<T>) -> AccountVote<BalanceOf<T>> {
let v = Vote {
aye: true,
conviction: Conviction::Locked1x,
};

AccountVote::Standard {
vote: v,
balance: b,
}
let v = Vote { aye: true, conviction: Conviction::Locked1x };

AccountVote::Standard { vote: v, balance: b }
}

benchmarks! {
Expand Down Expand Up @@ -225,8 +217,8 @@ benchmarks! {
// Place our proposal in the external queue, too.
let hash = T::Hashing::hash_of(&0);
assert_ok!(
Democracy::<T>::external_propose(T::ExternalOrigin::successful_origin(), hash.clone())
);
Democracy::<T>::external_propose(T::ExternalOrigin::successful_origin(), hash.clone())
);

// Add a referendum of our proposal.
let referendum_index = add_referendum::<T>(0)?;
Expand All @@ -238,9 +230,9 @@ benchmarks! {
verify {
// Referendum has been canceled
assert_noop!(
Democracy::<T>::referendum_status(referendum_index),
Error::<T>::ReferendumInvalid
);
Democracy::<T>::referendum_status(referendum_index),
Error::<T>::ReferendumInvalid
);
}

// Worst case scenario, we external propose a previously blacklisted proposal
Expand Down Expand Up @@ -760,7 +752,7 @@ benchmarks! {
}: enact_proposal(RawOrigin::Root, proposal_hash, 0)
verify {
// Fails due to mismatched origin
assert_last_event::<T>(Event::<T>::Executed(0, false).into());
assert_last_event::<T>(Event::<T>::Executed(0, Err(BadOrigin.into())).into());
}

#[extra]
Expand All @@ -786,9 +778,4 @@ benchmarks! {
}
}


impl_benchmark_test_suite!(
Democracy,
crate::tests::new_test_ext(),
crate::tests::Test,
);
impl_benchmark_test_suite!(Democracy, crate::tests::new_test_ext(), crate::tests::Test);
118 changes: 118 additions & 0 deletions lib-serml/democracy/src/conviction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// This file is part of Substrate.

// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The conviction datatype.

use crate::types::Delegations;
use codec::{Decode, Encode};
use sp_runtime::{
traits::{Bounded, CheckedDiv, CheckedMul, Zero},
RuntimeDebug,
};
use sp_std::{convert::TryFrom, result::Result};

/// A value denoting the strength of conviction of a vote.
#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug)]
pub enum Conviction {
/// 0.1x votes, unlocked.
None,
/// 1x votes, locked for an enactment period following a successful vote.
Locked1x,
/// 2x votes, locked for 2x enactment periods following a successful vote.
Locked2x,
/// 3x votes, locked for 4x...
Locked3x,
/// 4x votes, locked for 8x...
Locked4x,
/// 5x votes, locked for 16x...
Locked5x,
/// 6x votes, locked for 32x...
Locked6x,
}

impl Default for Conviction {
fn default() -> Self {
Conviction::None
}
}

impl From<Conviction> for u8 {
fn from(c: Conviction) -> u8 {
match c {
Conviction::None => 0,
Conviction::Locked1x => 1,
Conviction::Locked2x => 2,
Conviction::Locked3x => 3,
Conviction::Locked4x => 4,
Conviction::Locked5x => 5,
Conviction::Locked6x => 6,
}
}
}

impl TryFrom<u8> for Conviction {
type Error = ();
fn try_from(i: u8) -> Result<Conviction, ()> {
Ok(match i {
0 => Conviction::None,
1 => Conviction::Locked1x,
2 => Conviction::Locked2x,
3 => Conviction::Locked3x,
4 => Conviction::Locked4x,
5 => Conviction::Locked5x,
6 => Conviction::Locked6x,
_ => return Err(()),
})
}
}

impl Conviction {
/// The amount of time (in number of periods) that our conviction implies a successful voter's
/// balance should be locked for.
pub fn lock_periods(self) -> u32 {
match self {
Conviction::None => 0,
Conviction::Locked1x => 1,
Conviction::Locked2x => 2,
Conviction::Locked3x => 4,
Conviction::Locked4x => 8,
Conviction::Locked5x => 16,
Conviction::Locked6x => 32,
}
}

/// The votes of a voter of the given `balance` with our conviction.
pub fn votes<B: From<u8> + Zero + Copy + CheckedMul + CheckedDiv + Bounded>(
self,
capital: B,
) -> Delegations<B> {
let votes = match self {
Conviction::None => capital.checked_div(&10u8.into()).unwrap_or_else(Zero::zero),
x => capital.checked_mul(&u8::from(x).into()).unwrap_or_else(B::max_value),
};
Delegations { votes, capital }
}
}

impl Bounded for Conviction {
fn min_value() -> Self {
Conviction::None
}
fn max_value() -> Self {
Conviction::Locked6x
}
}
Loading