Skip to content

Commit

Permalink
optimization to eliminate assumptions that a valid weight can never be 0
Browse files Browse the repository at this point in the history
  • Loading branch information
abdbee committed Dec 23, 2024
1 parent f2137bd commit 33032b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
7 changes: 7 additions & 0 deletions substrate/frame/ranked-collective/abdbee
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAi5QJ2BscjgPR0k07UOfItiYSgK/E7M1Ajve7oWYNmEwAAAJi3C7+Gtwu/
hgAAAAtzc2gtZWQyNTUxOQAAACAi5QJ2BscjgPR0k07UOfItiYSgK/E7M1Ajve7oWYNmEw
AAAEAjTuk+3MWUYJpi938ze18PPjR70U1nIPlO/CxyuSqH0CLlAnYGxyOA9HSTTtQ58i2J
hKAr8TszUCO97uhZg2YTAAAAFGFtb2thdmF1bHRAZ21haWwuY29tAQ==
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions substrate/frame/ranked-collective/abdbee.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICLlAnYGxyOA9HSTTtQ58i2JhKAr8TszUCO97uhZg2YT [email protected]
39 changes: 19 additions & 20 deletions substrate/frame/ranked-collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,18 @@ impl MemberRecord {
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum VoteRecord {
/// Vote was an aye with given vote weight.
Aye(Votes),
Aye(Option<Votes>),
/// Vote was a nay with given vote weight.
Nay(Votes),
Nay(Option<Votes>),
}

impl From<(bool, Votes)> for VoteRecord {
fn from((aye, votes): (bool, Votes)) -> Self {
match aye {
true => VoteRecord::Aye(votes),
false => VoteRecord::Nay(votes),
impl From<(bool, Option<Votes>)> for VoteRecord {
fn from((aye, votes): (bool, Option<Votes>)) -> Self {
match (aye, votes) {
(true, Some(votes)) => VoteRecord::Aye(Some(votes)),
(true, None) => VoteRecord::Aye(None),
(false, Some(votes)) => VoteRecord::Nay(Some(votes)),
(false, None) => VoteRecord::Nay(None),
}
}
}
Expand Down Expand Up @@ -631,29 +633,26 @@ pub mod pallet {
Err(Error::<T, I>::NotPolling)?,
PollStatus::Ongoing(ref mut tally, class) => {
match Voting::<T, I>::get(&poll, &who) {
Some(Aye(votes)) => {
if votes > 0 {
Some(Aye(Some(votes))) => {
tally.bare_ayes.saturating_dec();
tally.ayes.saturating_reduce(votes);
} else {
tally.out_of_rank_ayes.saturating_dec();
}

Some(Aye(None)) => {
tally.out_of_rank_ayes.saturating_dec();
},
Some(Nay(votes)) => {
if votes > 0 {
Some(Nay(Some(votes))) => {
tally.nays.saturating_reduce(votes)
} else {
tally.out_of_rank_nays.saturating_dec();
}
},
Some(Nay(None)) => {
tally.out_of_rank_nays.saturating_dec();
},
None => pays = Pays::No,
}
let min_rank = T::MinRankOfClass::convert(class);
let votes = Self::rank_to_votes(record.rank, min_rank).unwrap_or(0);
let votes = Self::rank_to_votes(record.rank, min_rank);
let vote = VoteRecord::from((aye, votes));
match votes {
0 => {
None => {
match aye {
true => {
tally.out_of_rank_ayes.saturating_inc();
Expand All @@ -663,7 +662,7 @@ pub mod pallet {
},
}
},
_ => {
Some(votes) => {
match aye {
true => {
tally.bare_ayes.saturating_inc();
Expand Down

0 comments on commit 33032b3

Please sign in to comment.