mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-16 12:01:12 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -17,12 +17,12 @@
|
||||
|
||||
//! Voting thresholds.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Deserialize};
|
||||
use codec::{Encode, Decode};
|
||||
use sp_runtime::traits::{Zero, IntegerSquareRoot};
|
||||
use sp_std::ops::{Add, Mul, Div, Rem};
|
||||
use crate::Tally;
|
||||
use codec::{Decode, Encode};
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sp_runtime::traits::{IntegerSquareRoot, Zero};
|
||||
use sp_std::ops::{Add, Div, Mul, Rem};
|
||||
|
||||
/// A means of determining if a vote is past pass threshold.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, sp_runtime::RuntimeDebug)]
|
||||
@@ -43,25 +43,32 @@ pub trait Approved<Balance> {
|
||||
}
|
||||
|
||||
/// Return `true` iff `n1 / d1 < n2 / d2`. `d1` and `d2` may not be zero.
|
||||
fn compare_rationals<T: Zero + Mul<T, Output = T> + Div<T, Output = T> + Rem<T, Output = T> + Ord + Copy>(mut n1: T, mut d1: T, mut n2: T, mut d2: T) -> bool {
|
||||
fn compare_rationals<
|
||||
T: Zero + Mul<T, Output = T> + Div<T, Output = T> + Rem<T, Output = T> + Ord + Copy,
|
||||
>(
|
||||
mut n1: T,
|
||||
mut d1: T,
|
||||
mut n2: T,
|
||||
mut d2: T,
|
||||
) -> bool {
|
||||
// Uses a continued fractional representation for a non-overflowing compare.
|
||||
// Detailed at https://janmr.com/blog/2014/05/comparing-rational-numbers-without-overflow/.
|
||||
loop {
|
||||
let q1 = n1 / d1;
|
||||
let q2 = n2 / d2;
|
||||
if q1 < q2 {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
if q2 < q1 {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
let r1 = n1 % d1;
|
||||
let r2 = n2 % d2;
|
||||
if r2.is_zero() {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
if r1.is_zero() {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
n1 = d2;
|
||||
n2 = d1;
|
||||
@@ -71,14 +78,22 @@ fn compare_rationals<T: Zero + Mul<T, Output = T> + Div<T, Output = T> + Rem<T,
|
||||
}
|
||||
|
||||
impl<
|
||||
Balance: IntegerSquareRoot + Zero + Ord + Add<Balance, Output = Balance>
|
||||
+ Mul<Balance, Output = Balance> + Div<Balance, Output = Balance>
|
||||
+ Rem<Balance, Output = Balance> + Copy,
|
||||
> Approved<Balance> for VoteThreshold {
|
||||
Balance: IntegerSquareRoot
|
||||
+ Zero
|
||||
+ Ord
|
||||
+ Add<Balance, Output = Balance>
|
||||
+ Mul<Balance, Output = Balance>
|
||||
+ Div<Balance, Output = Balance>
|
||||
+ Rem<Balance, Output = Balance>
|
||||
+ Copy,
|
||||
> Approved<Balance> for VoteThreshold
|
||||
{
|
||||
fn approved(&self, tally: Tally<Balance>, electorate: Balance) -> bool {
|
||||
let sqrt_voters = tally.turnout.integer_sqrt();
|
||||
let sqrt_electorate = electorate.integer_sqrt();
|
||||
if sqrt_voters.is_zero() { return false; }
|
||||
if sqrt_voters.is_zero() {
|
||||
return false
|
||||
}
|
||||
match *self {
|
||||
VoteThreshold::SuperMajorityApprove =>
|
||||
compare_rationals(tally.nays, sqrt_voters, tally.ayes, sqrt_electorate),
|
||||
@@ -95,7 +110,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn should_work() {
|
||||
assert!(!VoteThreshold::SuperMajorityApprove.approved(Tally{ayes: 60, nays: 50, turnout: 110}, 210));
|
||||
assert!(VoteThreshold::SuperMajorityApprove.approved(Tally{ayes: 100, nays: 50, turnout: 150}, 210));
|
||||
assert!(!VoteThreshold::SuperMajorityApprove
|
||||
.approved(Tally { ayes: 60, nays: 50, turnout: 110 }, 210));
|
||||
assert!(VoteThreshold::SuperMajorityApprove
|
||||
.approved(Tally { ayes: 100, nays: 50, turnout: 150 }, 210));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user