Fix over/under flow risk in OnDilution (#2548)

* fix over/under flow risk in OnDilution

* bump impl
This commit is contained in:
joe petrowski
2019-05-14 12:07:32 +02:00
committed by Gavin Wood
parent e5627e2480
commit 580432d822
2 changed files with 10 additions and 4 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 78,
impl_version: 78,
impl_version: 79,
apis: RUNTIME_API_VERSIONS,
};
+9 -3
View File
@@ -64,7 +64,9 @@ use serde::{Serialize, Deserialize};
use rstd::prelude::*;
use srml_support::{StorageValue, StorageMap, decl_module, decl_storage, decl_event, ensure};
use srml_support::traits::{Currency, ReservableCurrency, OnDilution, OnUnbalanced, Imbalance};
use runtime_primitives::{Permill, traits::{Zero, EnsureOrigin, StaticLookup}};
use runtime_primitives::{Permill,
traits::{Zero, EnsureOrigin, StaticLookup, Saturating, CheckedSub, CheckedMul}
};
use parity_codec::{Encode, Decode};
use system::ensure_signed;
@@ -291,8 +293,12 @@ impl<T: Trait> OnDilution<BalanceOf<T>> for Module<T> {
// pre dilution and post-dilution.
if !minted.is_zero() && !portion.is_zero() {
let total_issuance = T::Currency::total_issuance();
let funding = (total_issuance - portion) / portion * minted;
<Pot<T>>::mutate(|x| *x += funding);
if let Some(funding) = total_issuance.checked_sub(&portion) {
let funding = funding / portion;
if let Some(funding) = funding.checked_mul(&minted) {
<Pot<T>>::mutate(|x| *x = x.saturating_add(funding));
}
}
}
}
}