Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

More Referenda Patches #10760

Merged
merged 3 commits into from
Feb 1, 2022
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
2 changes: 2 additions & 0 deletions frame/conviction-voting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ fn account_vote<T: Config>(b: BalanceOf<T>) -> AccountVote<BalanceOf<T>> {
}

benchmarks! {
where_clause { where T::MaxVotes: core::fmt::Debug }

vote_new {
let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);
Expand Down
14 changes: 13 additions & 1 deletion frame/conviction-voting/src/conviction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ use sp_runtime::{
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, TypeInfo, MaxEncodedLen)]
#[derive(
Encode,
Decode,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub enum Conviction {
/// 0.1x votes, unlocked.
None,
Expand Down
14 changes: 11 additions & 3 deletions frame/conviction-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ impl<T: Config> Pallet<T> {
votes[i].1 = vote;
},
Err(i) => {
votes.try_insert(i, (poll_index, vote))
votes
.try_insert(i, (poll_index, vote))
.map_err(|()| Error::<T>::MaxVotesReached)?;
},
}
Expand Down Expand Up @@ -433,7 +434,9 @@ impl<T: Config> Pallet<T> {
},
PollStatus::Completed(end, approved) => {
if let Some((lock_periods, balance)) = v.1.locked_if(approved) {
let unlock_at = end + T::VoteLockingPeriod::get() * lock_periods.into();
let unlock_at = end.saturating_add(
T::VoteLockingPeriod::get().saturating_mul(lock_periods.into()),
);
let now = frame_system::Pallet::<T>::block_number();
if now < unlock_at {
ensure!(
Expand Down Expand Up @@ -576,7 +579,12 @@ impl<T: Config> Pallet<T> {
);
let now = frame_system::Pallet::<T>::block_number();
let lock_periods = conviction.lock_periods().into();
prior.accumulate(now + T::VoteLockingPeriod::get() * lock_periods, balance);
prior.accumulate(
now.saturating_add(
T::VoteLockingPeriod::get().saturating_mul(lock_periods),
),
balance,
);
voting.set_common(delegations, prior);

Ok(votes)
Expand Down
10 changes: 5 additions & 5 deletions frame/conviction-voting/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,28 @@ impl<
}

/// Increment some amount of votes.
pub fn increase(&mut self, approve: bool, delegations: Delegations<Votes>) -> Option<()> {
pub fn increase(&mut self, approve: bool, delegations: Delegations<Votes>) {
self.turnout = self.turnout.saturating_add(delegations.capital);
match approve {
true => self.ayes = self.ayes.saturating_add(delegations.votes),
false => self.nays = self.nays.saturating_add(delegations.votes),
}
Some(())
}

/// Decrement some amount of votes.
pub fn reduce(&mut self, approve: bool, delegations: Delegations<Votes>) -> Option<()> {
pub fn reduce(&mut self, approve: bool, delegations: Delegations<Votes>) {
self.turnout = self.turnout.saturating_sub(delegations.capital);
match approve {
true => self.ayes = self.ayes.saturating_sub(delegations.votes),
false => self.nays = self.nays.saturating_sub(delegations.votes),
}
Some(())
}
}

/// Amount of votes and capital placed in delegation for an account.
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[derive(
Encode, Decode, Default, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen,
)]
pub struct Delegations<Balance> {
/// The number of votes (this is post-conviction).
pub votes: Balance,
Expand Down
37 changes: 24 additions & 13 deletions frame/conviction-voting/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
//! The vote datatype.

use crate::{Conviction, Delegations};
use codec::{Decode, Encode, EncodeLike, Input, Output, MaxEncodedLen};
use codec::{Decode, Encode, EncodeLike, Input, MaxEncodedLen, Output};
use frame_support::{pallet_prelude::Get, BoundedVec};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{Saturating, Zero},
RuntimeDebug,
};
use sp_std::{convert::TryFrom, prelude::*, result::Result};
use frame_support::{BoundedVec, pallet_prelude::Get};

/// A number of lock periods, plus a vote, one way or the other.
#[derive(Copy, Clone, Eq, PartialEq, Default, RuntimeDebug, MaxEncodedLen)]
Expand Down Expand Up @@ -109,7 +109,18 @@ impl<Balance: Saturating> AccountVote<Balance> {

/// A "prior" lock, i.e. a lock for some now-forgotten reason.
#[derive(
Encode, Decode, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug, TypeInfo, MaxEncodedLen,
Encode,
Decode,
Default,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct PriorLock<BlockNumber, Balance>(BlockNumber, Balance);

Expand Down Expand Up @@ -152,8 +163,8 @@ pub struct Delegating<Balance, AccountId, BlockNumber> {
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxVotes))]
pub struct Casting<Balance, BlockNumber, PollIndex, MaxVotes>
where
MaxVotes: Get<u32>,
where
MaxVotes: Get<u32>,
{
/// The current votes of the account.
pub votes: BoundedVec<(PollIndex, AccountVote<Balance>), MaxVotes>,
Expand All @@ -167,8 +178,8 @@ pub struct Casting<Balance, BlockNumber, PollIndex, MaxVotes>
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxVotes))]
pub enum Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
where
MaxVotes: Get<u32>,
where
MaxVotes: Get<u32>,
{
/// The account is voting directly.
Casting(Casting<Balance, BlockNumber, PollIndex, MaxVotes>),
Expand All @@ -178,8 +189,8 @@ pub enum Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>

impl<Balance: Default, AccountId, BlockNumber: Zero, PollIndex, MaxVotes> Default
for Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
where
MaxVotes: Get<u32>,
where
MaxVotes: Get<u32>,
{
fn default() -> Self {
Voting::Casting(Casting {
Expand All @@ -192,8 +203,8 @@ impl<Balance: Default, AccountId, BlockNumber: Zero, PollIndex, MaxVotes> Defaul

impl<Balance, AccountId, BlockNumber, PollIndex, MaxVotes> AsMut<PriorLock<BlockNumber, Balance>>
for Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
where
MaxVotes: Get<u32>,
where
MaxVotes: Get<u32>,
{
fn as_mut(&mut self) -> &mut PriorLock<BlockNumber, Balance> {
match self {
Expand All @@ -210,8 +221,8 @@ impl<
PollIndex,
MaxVotes,
> Voting<Balance, AccountId, BlockNumber, PollIndex, MaxVotes>
where
MaxVotes: Get<u32>,
where
MaxVotes: Get<u32>,
{
pub fn rejig(&mut self, now: BlockNumber) {
AsMut::<PriorLock<BlockNumber, Balance>>::as_mut(self).rejig(now);
Expand Down
4 changes: 3 additions & 1 deletion frame/referenda/src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// 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.0sp_runtime::{DispatchResult, traits::One}asp_runtime::{DispatchResult, traits::AtLeast32BitUnsigned} in writing, software
// 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
Expand Down
6 changes: 4 additions & 2 deletions frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// 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.0sp_runtime::{DispatchResult, traits::One}asp_runtime::{DispatchResult, traits::AtLeast32BitUnsigned} in writing, software
// 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
Expand Down Expand Up @@ -360,7 +362,7 @@ pub mod pallet {
deciding: None,
tally: Default::default(),
in_queue: false,
alarm: Self::set_alarm(nudge_call, now + T::UndecidingTimeout::get()),
alarm: Self::set_alarm(nudge_call, now.saturating_add(T::UndecidingTimeout::get())),
};
ReferendumInfoFor::<T>::insert(index, ReferendumInfo::Ongoing(status));

Expand Down