Skip to content

Commit

Permalink
Small optimization and comment about underflow/overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Feb 12, 2024
1 parent e2bbb64 commit 3bb16c0
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions crates/pallet-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,27 +435,26 @@ impl<T: Config> Pallet<T> {
let is = params.initial_subsidy;
let mdi = max_decay_issuance;

let term1 = is * is * t / mdi;
let term2 = term1 * is * t / mdi;
let term3 = term2 * is * t / mdi;
let term4 = term3 * is * t / mdi;
let term5 = term4 * is * t / mdi;
let maybe_subsidy = try {
is.checked_sub(&(is * is * t / mdi))?
.checked_add(&(is * is * t / mdi * is * t / mdi / 2_u32.into()))?
.checked_sub(&(is * is * t / mdi * is * t / mdi * is * t / mdi / 6_u32.into()))?
.checked_add(
&(is * is * t / mdi * is * t / mdi * is * t / mdi * is * t
/ mdi
/ 24_u32.into()),
)?
.checked_sub(
&(is * is * t / mdi * is * t / mdi * is * t / mdi * is * t / mdi * is * t
/ mdi
/ 120_u32.into()),
)?
is.checked_sub(&term1)?
.checked_add(&(term2 / 2_u32.into()))?
.checked_sub(&(term3 / 6_u32.into()))?
.checked_add(&(term4 / 24_u32.into()))?
.checked_sub(&(term5 / 120_u32.into()))?
};

match maybe_subsidy {
Some(subsidy) => subsidy,
None => {
// Due to use of Taylor series above this can eventually underflow, but not any
// time soon
warn!(
"Underflow or overflow when calculating reference subsidy, incorrect \
"Underflow or overflow when calculating reference subsidy, likely incorrect \
issuance parameters"
);

Expand Down

0 comments on commit 3bb16c0

Please sign in to comment.