From c7e6c327bf203aec9774c732a1032c0ca4343e0b Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 31 Jan 2022 03:54:00 +0000 Subject: [PATCH 1/3] basic fixes --- frame/conviction-voting/src/conviction.rs | 14 ++++++++- frame/conviction-voting/src/lib.rs | 14 +++++++-- frame/conviction-voting/src/types.rs | 10 +++--- frame/conviction-voting/src/vote.rs | 37 +++++++++++++++-------- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/frame/conviction-voting/src/conviction.rs b/frame/conviction-voting/src/conviction.rs index a273a0a8273fe..129f2771124b5 100644 --- a/frame/conviction-voting/src/conviction.rs +++ b/frame/conviction-voting/src/conviction.rs @@ -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, diff --git a/frame/conviction-voting/src/lib.rs b/frame/conviction-voting/src/lib.rs index 16c31efd7772d..8e7e0d91b1cf4 100644 --- a/frame/conviction-voting/src/lib.rs +++ b/frame/conviction-voting/src/lib.rs @@ -379,7 +379,8 @@ impl Pallet { votes[i].1 = vote; }, Err(i) => { - votes.try_insert(i, (poll_index, vote)) + votes + .try_insert(i, (poll_index, vote)) .map_err(|()| Error::::MaxVotesReached)?; }, } @@ -433,7 +434,9 @@ impl Pallet { }, 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::::block_number(); if now < unlock_at { ensure!( @@ -576,7 +579,12 @@ impl Pallet { ); let now = frame_system::Pallet::::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) diff --git a/frame/conviction-voting/src/types.rs b/frame/conviction-voting/src/types.rs index 432bd7b97e6d4..2ad1a164dd143 100644 --- a/frame/conviction-voting/src/types.rs +++ b/frame/conviction-voting/src/types.rs @@ -170,28 +170,28 @@ impl< } /// Increment some amount of votes. - pub fn increase(&mut self, approve: bool, delegations: Delegations) -> Option<()> { + pub fn increase(&mut self, approve: bool, delegations: Delegations) { 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) -> Option<()> { + pub fn reduce(&mut self, approve: bool, delegations: Delegations) { 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 { /// The number of votes (this is post-conviction). pub votes: Balance, diff --git a/frame/conviction-voting/src/vote.rs b/frame/conviction-voting/src/vote.rs index 38785ac78d60f..d7ca931de35a1 100644 --- a/frame/conviction-voting/src/vote.rs +++ b/frame/conviction-voting/src/vote.rs @@ -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)] @@ -109,7 +109,18 @@ impl AccountVote { /// 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); @@ -152,8 +163,8 @@ pub struct Delegating { #[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] #[scale_info(skip_type_params(MaxVotes))] pub struct Casting - where - MaxVotes: Get, +where + MaxVotes: Get, { /// The current votes of the account. pub votes: BoundedVec<(PollIndex, AccountVote), MaxVotes>, @@ -167,8 +178,8 @@ pub struct Casting #[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] #[scale_info(skip_type_params(MaxVotes))] pub enum Voting - where - MaxVotes: Get, +where + MaxVotes: Get, { /// The account is voting directly. Casting(Casting), @@ -178,8 +189,8 @@ pub enum Voting impl Default for Voting - where - MaxVotes: Get, +where + MaxVotes: Get, { fn default() -> Self { Voting::Casting(Casting { @@ -192,8 +203,8 @@ impl Defaul impl AsMut> for Voting - where - MaxVotes: Get, +where + MaxVotes: Get, { fn as_mut(&mut self) -> &mut PriorLock { match self { @@ -210,8 +221,8 @@ impl< PollIndex, MaxVotes, > Voting - where - MaxVotes: Get, +where + MaxVotes: Get, { pub fn rejig(&mut self, now: BlockNumber) { AsMut::>::as_mut(self).rejig(now); From c26b2e8b047f298d174fcd4a51360301a28e447f Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 31 Jan 2022 04:12:24 +0000 Subject: [PATCH 2/3] fix benchmarking --- frame/conviction-voting/src/benchmarking.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frame/conviction-voting/src/benchmarking.rs b/frame/conviction-voting/src/benchmarking.rs index 040287f41bdb5..2beee4f3b49d2 100644 --- a/frame/conviction-voting/src/benchmarking.rs +++ b/frame/conviction-voting/src/benchmarking.rs @@ -61,6 +61,8 @@ fn account_vote(b: BalanceOf) -> AccountVote> { } benchmarks! { + where_clause { where T::MaxVotes: core::fmt::Debug } + vote_new { let caller = funded_account::("caller", 0); whitelist_account!(caller); From 9318b09fa85491158086dd46c99c9455445ad6de Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 31 Jan 2022 04:29:12 +0000 Subject: [PATCH 3/3] fix license --- frame/referenda/src/branch.rs | 4 +++- frame/referenda/src/lib.rs | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/frame/referenda/src/branch.rs b/frame/referenda/src/branch.rs index a8185f5efd84b..6a4efa31e15e2 100644 --- a/frame/referenda/src/branch.rs +++ b/frame/referenda/src/branch.rs @@ -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 diff --git a/frame/referenda/src/lib.rs b/frame/referenda/src/lib.rs index 734bd9c0da709..fb8e9aa6a6db7 100644 --- a/frame/referenda/src/lib.rs +++ b/frame/referenda/src/lib.rs @@ -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 @@ -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::::insert(index, ReferendumInfo::Ongoing(status));