@@ -54,7 +54,7 @@ use frame_support::{
54
54
use scale_info:: TypeInfo ;
55
55
use sp_io:: storage;
56
56
use sp_runtime:: traits:: Dispatchable ;
57
- use sp_runtime:: { traits:: Hash , RuntimeDebug } ;
57
+ use sp_runtime:: { traits:: Hash , RuntimeDebug , Saturating } ;
58
58
use sp_std:: { marker:: PhantomData , prelude:: * , result} ;
59
59
60
60
#[ cfg( test) ]
@@ -120,7 +120,7 @@ impl DefaultVote for MoreThanMajorityThenPrimeDefaultVote {
120
120
_no_votes : MemberCount ,
121
121
len : MemberCount ,
122
122
) -> bool {
123
- let more_than_majority = yes_votes * 2 > len;
123
+ let more_than_majority = yes_votes. saturating_mul ( 2 ) > len;
124
124
more_than_majority || prime_vote. unwrap_or ( false )
125
125
}
126
126
}
@@ -547,7 +547,9 @@ pub mod pallet {
547
547
Error :: <T , I >:: DurationLowerThanConfiguredMotionDuration
548
548
) ;
549
549
550
- let threshold = ( T :: GetVotingMembers :: get_count ( ) / 2 ) + 1 ;
550
+ let threshold = T :: GetVotingMembers :: get_count ( )
551
+ . saturating_div ( 2 )
552
+ . saturating_add ( 1 ) ;
551
553
552
554
let members = Self :: members ( ) ;
553
555
let ( proposal_len, active_proposals) =
@@ -718,10 +720,15 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
718
720
} ) ?;
719
721
720
722
let index = Self :: proposal_count ( ) ;
721
- <ProposalCount < T , I > >:: mutate ( |i| * i += 1 ) ;
723
+ <ProposalCount < T , I > >:: try_mutate ( |i| {
724
+ * i = i
725
+ . checked_add ( 1 )
726
+ . ok_or ( Error :: < T , I > :: TooManyActiveProposals ) ?;
727
+ Ok :: < ( ) , Error < T , I > > ( ( ) )
728
+ } ) ?;
722
729
<ProposalOf < T , I > >:: insert ( proposal_hash, proposal) ;
723
730
let votes = {
724
- let end = frame_system:: Pallet :: < T > :: block_number ( ) + duration;
731
+ let end = frame_system:: Pallet :: < T > :: block_number ( ) . saturating_add ( duration) ;
725
732
Votes {
726
733
index,
727
734
threshold,
@@ -864,10 +871,10 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
864
871
// default voting strategy.
865
872
let default = T :: DefaultVote :: default_vote ( prime_vote, yes_votes, no_votes, seats) ;
866
873
867
- let abstentions = seats - ( yes_votes + no_votes) ;
874
+ let abstentions = seats. saturating_sub ( yes_votes. saturating_add ( no_votes) ) ;
868
875
match default {
869
- true => yes_votes += abstentions,
870
- false => no_votes += abstentions,
876
+ true => yes_votes = yes_votes . saturating_add ( abstentions) ,
877
+ false => no_votes = no_votes . saturating_add ( abstentions) ,
871
878
}
872
879
let approved = yes_votes >= voting. threshold ;
873
880
@@ -983,7 +990,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
983
990
Voting :: < T , I > :: remove ( proposal_hash) ;
984
991
let num_proposals = Proposals :: < T , I > :: mutate ( |proposals| {
985
992
proposals. retain ( |h| h != & proposal_hash) ;
986
- proposals. len ( ) + 1 // calculate weight based on original length
993
+ proposals. len ( ) . saturating_add ( 1 ) // calculate weight based on original length
987
994
} ) ;
988
995
num_proposals as u32
989
996
}
@@ -1156,7 +1163,7 @@ impl<
1156
1163
type Success = ( ) ;
1157
1164
fn try_origin ( o : O ) -> Result < Self :: Success , O > {
1158
1165
o. into ( ) . and_then ( |o| match o {
1159
- RawOrigin :: Members ( n, m) if n * D > N * m => Ok ( ( ) ) ,
1166
+ RawOrigin :: Members ( n, m) if n. saturating_mul ( D ) > N . saturating_mul ( m ) => Ok ( ( ) ) ,
1160
1167
r => Err ( O :: from ( r) ) ,
1161
1168
} )
1162
1169
}
@@ -1181,7 +1188,7 @@ impl<
1181
1188
type Success = ( ) ;
1182
1189
fn try_origin ( o : O ) -> Result < Self :: Success , O > {
1183
1190
o. into ( ) . and_then ( |o| match o {
1184
- RawOrigin :: Members ( n, m) if n * D >= N * m => Ok ( ( ) ) ,
1191
+ RawOrigin :: Members ( n, m) if n. saturating_mul ( D ) >= N . saturating_mul ( m ) => Ok ( ( ) ) ,
1185
1192
r => Err ( O :: from ( r) ) ,
1186
1193
} )
1187
1194
}
0 commit comments