Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions substrate/runtime/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ impl<T: Trait> Module<T> {
if normal_rotation {
// reward
let ideal_elapsed = <session::Module<T>>::ideal_session_duration();
let percent: usize = (T::Moment::sa(65536usize) * ideal_elapsed.clone() / actual_elapsed.max(ideal_elapsed)).as_();
let reward = Self::session_reward() * T::Balance::sa(percent) / T::Balance::sa(65536usize);
let per65536: u64 = (T::Moment::sa(65536u64) * ideal_elapsed.clone() / actual_elapsed.max(ideal_elapsed)).as_();
let reward = Self::session_reward() * T::Balance::sa(per65536) / T::Balance::sa(65536u64);
// apply good session reward
for v in <session::Module<T>>::validators().iter() {
let noms = Self::current_nominators_for(v);
Expand All @@ -578,11 +578,11 @@ impl<T: Trait> Module<T> {
if let Some(rem) = Self::slash(v, early_era_slash) {
let noms = Self::current_nominators_for(v);
let total = noms.iter().map(Self::voting_balance).fold(Zero::zero(), |acc, x| acc + x);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting:

   --> /home/svyatonik/dev/polkadot/substrate/runtime/staking/src/lib.rs:580:76
    |
580 |                     let total = noms.iter().map(Self::voting_balance).fold(Zero::zero(), |acc, x| acc + x);
    |                                                                                           ^^^ consider giving this closure parameter a type
    |
    = note: type must be known at this point

on this line when trying to run build.sh

for n in noms.iter() {
//let r = Self::voting_balance(n) * reward / total; // correct formula, but might overflow with large slash * total.
let quant = T::Balance::sa(1usize << 31);
let s = (Self::voting_balance(n) * quant / total) * rem / quant; // avoid overflow by using quant as a denominator.
let _ = Self::slash(n, s); // best effort - not much that can be done on fail.
if !total.is_zero() {
let safe_mul_rational = |b| b * rem / total;// TODO: avoid overflow
for n in noms.iter() {
let _ = Self::slash(n, safe_mul_rational(Self::voting_balance(n))); // best effort - not much that can be done on fail.
}
}
}
}
Expand Down