Refactor CurrencyToVote (#6896)

* Refactor CurrencyToVote to avoid calls to total_issuance.

* Update frame/support/src/traits.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Some grumbles

* Fix last grumbles.

* Fix comment

* Final fix

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Kian Paimani
2020-10-08 16:50:54 +02:00
committed by GitHub
parent aba0128f6f
commit ba229c629f
14 changed files with 157 additions and 199 deletions
+1 -20
View File
@@ -17,8 +17,6 @@
//! Some configurable implementations as associated type for the substrate runtime.
use node_primitives::Balance;
use sp_runtime::traits::Convert;
use frame_support::traits::{OnUnbalanced, Currency};
use crate::{Balances, Authorship, NegativeImbalance};
@@ -29,26 +27,9 @@ impl OnUnbalanced<NegativeImbalance> for Author {
}
}
/// Struct that handles the conversion of Balance -> `u64`. This is used for staking's election
/// calculation.
pub struct CurrencyToVoteHandler;
impl CurrencyToVoteHandler {
fn factor() -> Balance { (Balances::total_issuance() / u64::max_value() as Balance).max(1) }
}
impl Convert<Balance, u64> for CurrencyToVoteHandler {
fn convert(x: Balance) -> u64 { (x / Self::factor()) as u64 }
}
impl Convert<u128, Balance> for CurrencyToVoteHandler {
fn convert(x: u128) -> Balance { x * Self::factor() }
}
#[cfg(test)]
mod multiplier_tests {
use super::*;
use sp_runtime::{assert_eq_error_rate, FixedPointNumber};
use sp_runtime::{assert_eq_error_rate, FixedPointNumber, traits::Convert};
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
use crate::{