@@ -299,7 +299,7 @@ use frame_support::{
299
299
} ,
300
300
traits:: {
301
301
Currency , LockIdentifier , LockableCurrency , WithdrawReasons , OnUnbalanced , Imbalance , Get ,
302
- UnixTime , EstimateNextNewSession , EnsureOrigin ,
302
+ UnixTime , EstimateNextNewSession , EnsureOrigin , CurrencyToVote ,
303
303
}
304
304
} ;
305
305
use pallet_session:: historical;
@@ -811,7 +811,7 @@ pub trait Trait: frame_system::Trait + SendTransactionTypes<Call<Self>> {
811
811
/// [`sp_npos_elections`] crate which accepts u64 numbers and does operations in 128.
812
812
/// Consequently, the backward convert is used convert the u128s from sp-elections back to a
813
813
/// [`BalanceOf`].
814
- type CurrencyToVote : Convert < BalanceOf < Self > , VoteWeight > + Convert < u128 , BalanceOf < Self > > ;
814
+ type CurrencyToVote : CurrencyToVote < BalanceOf < Self > > ;
815
815
816
816
/// Tokens have been minted and are unused for validator-reward.
817
817
/// See [Era payout](./index.html#era-payout).
@@ -2192,11 +2192,22 @@ impl<T: Trait> Module<T> {
2192
2192
Self :: bonded ( stash) . and_then ( Self :: ledger) . map ( |l| l. active ) . unwrap_or_default ( )
2193
2193
}
2194
2194
2195
- /// internal impl of [`slashable_balance_of`] that returns [`VoteWeight`].
2196
- pub fn slashable_balance_of_vote_weight ( stash : & T :: AccountId ) -> VoteWeight {
2197
- <T :: CurrencyToVote as Convert < BalanceOf < T > , VoteWeight > >:: convert (
2198
- Self :: slashable_balance_of ( stash)
2199
- )
2195
+ /// Internal impl of [`slashable_balance_of`] that returns [`VoteWeight`].
2196
+ pub fn slashable_balance_of_vote_weight ( stash : & T :: AccountId , issuance : BalanceOf < T > ) -> VoteWeight {
2197
+ T :: CurrencyToVote :: to_vote ( Self :: slashable_balance_of ( stash) , issuance)
2198
+ }
2199
+
2200
+ /// Returns a closure around `slashable_balance_of_vote_weight` that can be passed around.
2201
+ ///
2202
+ /// This prevents call sites from repeatedly requesting `total_issuance` from backend. But it is
2203
+ /// important to be only used while the total issuance is not changing.
2204
+ pub fn slashable_balance_of_fn ( ) -> Box < dyn Fn ( & T :: AccountId ) -> VoteWeight > {
2205
+ // NOTE: changing this to unboxed `impl Fn(..)` return type and the module will still
2206
+ // compile, while some types in mock fail to resolve.
2207
+ let issuance = T :: Currency :: total_issuance ( ) ;
2208
+ Box :: new ( move |who : & T :: AccountId | -> VoteWeight {
2209
+ Self :: slashable_balance_of_vote_weight ( who, issuance)
2210
+ } )
2200
2211
}
2201
2212
2202
2213
/// Dump the list of validators and nominators into vectors and keep them on-chain.
@@ -2601,7 +2612,7 @@ impl<T: Trait> Module<T> {
2601
2612
// convert into staked assignments.
2602
2613
let staked_assignments = sp_npos_elections:: assignment_ratio_to_staked (
2603
2614
assignments,
2604
- Self :: slashable_balance_of_vote_weight ,
2615
+ Self :: slashable_balance_of_fn ( ) ,
2605
2616
) ;
2606
2617
2607
2618
// build the support map thereof in order to evaluate.
@@ -2852,7 +2863,7 @@ impl<T: Trait> Module<T> {
2852
2863
/// `PrimitiveElectionResult` into `ElectionResult`.
2853
2864
///
2854
2865
/// No storage item is updated.
2855
- fn do_on_chain_phragmen ( ) -> Option < ElectionResult < T :: AccountId , BalanceOf < T > > > {
2866
+ pub fn do_on_chain_phragmen ( ) -> Option < ElectionResult < T :: AccountId , BalanceOf < T > > > {
2856
2867
if let Some ( phragmen_result) = Self :: do_phragmen :: < ChainAccuracy > ( 0 ) {
2857
2868
let elected_stashes = phragmen_result. winners . iter ( )
2858
2869
. map ( |( s, _) | s. clone ( ) )
@@ -2861,7 +2872,7 @@ impl<T: Trait> Module<T> {
2861
2872
2862
2873
let staked_assignments = sp_npos_elections:: assignment_ratio_to_staked (
2863
2874
assignments,
2864
- Self :: slashable_balance_of_vote_weight ,
2875
+ Self :: slashable_balance_of_fn ( ) ,
2865
2876
) ;
2866
2877
2867
2878
let supports = build_support_map :: < T :: AccountId > (
@@ -2903,16 +2914,16 @@ impl<T: Trait> Module<T> {
2903
2914
/// Self votes are added and nominations before the most recent slashing span are ignored.
2904
2915
///
2905
2916
/// No storage item is updated.
2906
- pub fn do_phragmen < Accuracy : PerThing > (
2907
- iterations : usize ,
2908
- ) -> Option < PrimitiveElectionResult < T :: AccountId , Accuracy > >
2917
+ pub fn do_phragmen < Accuracy : PerThing > ( iterations : usize )
2918
+ -> Option < PrimitiveElectionResult < T :: AccountId , Accuracy > >
2909
2919
where ExtendedBalance : From < InnerOf < Accuracy > >
2910
2920
{
2921
+ let weight_of = Self :: slashable_balance_of_fn ( ) ;
2911
2922
let mut all_nominators: Vec < ( T :: AccountId , VoteWeight , Vec < T :: AccountId > ) > = Vec :: new ( ) ;
2912
2923
let mut all_validators = Vec :: new ( ) ;
2913
2924
for ( validator, _) in <Validators < T > >:: iter ( ) {
2914
2925
// append self vote
2915
- let self_vote = ( validator. clone ( ) , Self :: slashable_balance_of_vote_weight ( & validator) , vec ! [ validator. clone( ) ] ) ;
2926
+ let self_vote = ( validator. clone ( ) , weight_of ( & validator) , vec ! [ validator. clone( ) ] ) ;
2916
2927
all_nominators. push ( self_vote) ;
2917
2928
all_validators. push ( validator) ;
2918
2929
}
@@ -2932,7 +2943,7 @@ impl<T: Trait> Module<T> {
2932
2943
( nominator, targets)
2933
2944
} ) ;
2934
2945
all_nominators. extend ( nominator_votes. map ( |( n, ns) | {
2935
- let s = Self :: slashable_balance_of_vote_weight ( & n) ;
2946
+ let s = weight_of ( & n) ;
2936
2947
( n, s, ns)
2937
2948
} ) ) ;
2938
2949
@@ -2956,8 +2967,8 @@ impl<T: Trait> Module<T> {
2956
2967
fn collect_exposure (
2957
2968
supports : SupportMap < T :: AccountId > ,
2958
2969
) -> Vec < ( T :: AccountId , Exposure < T :: AccountId , BalanceOf < T > > ) > {
2959
- let to_balance = | e : ExtendedBalance |
2960
- < T :: CurrencyToVote as Convert < ExtendedBalance , BalanceOf < T > > > :: convert ( e ) ;
2970
+ let total_issuance = T :: Currency :: total_issuance ( ) ;
2971
+ let to_currency = | e : ExtendedBalance | T :: CurrencyToVote :: to_currency ( e , total_issuance ) ;
2961
2972
2962
2973
supports. into_iter ( ) . map ( |( validator, support) | {
2963
2974
// build `struct exposure` from `support`
@@ -2966,7 +2977,7 @@ impl<T: Trait> Module<T> {
2966
2977
let mut total: BalanceOf < T > = Zero :: zero ( ) ;
2967
2978
support. voters
2968
2979
. into_iter ( )
2969
- . map ( |( nominator, weight) | ( nominator, to_balance ( weight) ) )
2980
+ . map ( |( nominator, weight) | ( nominator, to_currency ( weight) ) )
2970
2981
. for_each ( |( nominator, stake) | {
2971
2982
if nominator == validator {
2972
2983
own = own. saturating_add ( stake) ;
0 commit comments