Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Ban ED of zero unless feature enabled
  • Loading branch information
gavofyork committed Mar 21, 2023
commit f97b850aebd809a0827e409eff9f7b368b05ca23
1 change: 1 addition & 0 deletions frame/balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ std = [
"sp-runtime/std",
"sp-std/std",
]
zero_ed = []
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
12 changes: 11 additions & 1 deletion frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ const LOG_TARGET: &str = "runtime::balances";

type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;

#[cfg(feature = "zero_ed")]
fn assert_valid_ed<Balance: Zero>(_: Balance) {}

#[cfg(not(feature = "zero_ed"))]
fn assert_valid_ed<Balance: Zero>(existential_deposit: Balance) {
assert!(!existential_deposit.is_zero(), "The existential deposit must be greater than zero!");
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -452,12 +460,14 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
fn build(&self) {
let ed = <T as Config<I>>::ExistentialDeposit::get();
assert_valid_ed(ed);
let total = self.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n);
<TotalIssuance<T, I>>::put(total);

for (_, balance) in &self.balances {
assert!(
*balance >= <T as Config<I>>::ExistentialDeposit::get(),
*balance >= ed,
"the balance of any account should always be at least the existential deposit.",
)
}
Expand Down
10 changes: 9 additions & 1 deletion frame/balances/src/tests/currency_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ fn slash_reserved_on_non_existant_works() {
fn operations_on_dead_account_should_not_change_state() {
// These functions all use `mutate_account` which may introduce a storage change when
// the account never existed to begin with, and shouldn't exist in the end.
ExtBuilder::default().existential_deposit(0).build_and_execute_with(|| {
ExtBuilder::default().existential_deposit(1).build_and_execute_with(|| {
assert!(!frame_system::Account::<Test>::contains_key(&1337));

// Unreserve
Expand All @@ -993,6 +993,14 @@ fn operations_on_dead_account_should_not_change_state() {
});
}

#[test]
#[should_panic = "The existential deposit must be greater than zero!"]
fn zero_ed_is_prohibited() {
// These functions all use `mutate_account` which may introduce a storage change when
// the account never existed to begin with, and shouldn't exist in the end.
ExtBuilder::default().existential_deposit(0).build_and_execute_with(|| {});
}

#[test]
fn named_reserve_should_work() {
ExtBuilder::default().build_and_execute_with(|| {
Expand Down