From df65a92c9147954deef1df356d1bb37be5172fb1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 21 Mar 2019 13:56:31 +0100 Subject: [PATCH] Rebuild, add a couple of tests and fix theoretical issue (#2056) * Rebuild, add a couple of tests and fix theoretical issue * Update lib.rs --- substrate/node/runtime/src/lib.rs | 2 +- .../release/node_runtime.compact.wasm | Bin 900932 -> 901302 bytes substrate/srml/balances/src/lib.rs | 16 +++++++++- substrate/srml/balances/src/tests.rs | 30 ++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index 6ef50ef61f..1f52620cd4 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -59,7 +59,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { impl_name: create_runtime_str!("substrate-node"), authoring_version: 10, spec_version: 38, - impl_version: 38, + impl_version: 39, apis: RUNTIME_API_VERSIONS, }; diff --git a/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm b/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm index 22b953faab7d9a741ca31373208ea792a66918f2..446e9eaad9b7c42c8435e38f28f298bd48fa373d 100644 GIT binary patch delta 1698 zcma)6O=u)V6z;0m-PJwQJ(~dht!Un2@w~;it30|M_dU(WpJxU6!Up$fX>9PvDtXM| zqsU9Xn8rMc^Ch3`(|Mmc+lAq0C`J+SISpn07LLon%e$zM148MlDNre7w6d=m?D zY?DV;K{=q8lb>($h0sAe=y=w36b3)S$SapU?g@vbtD2WjS3~T?LIb-n*YJzjRrBrb zhTnxY{pqZ!iD=m+uag{$NJbYzRU?LlCK1imwwr!I8BITjltuyb$#lNt(y0pE;E8)q ze*7VSuE+zd`y7p@TrHhOz^)2$p|vx{D?B)`e-pq%Sm2?Yi=e+sdHV*%H7I-EL};L< zEP=e?TPSM2K~x1A6yL0=iKrSBR6~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%~VQTN9MSIc{NDN8bVb zkVwC=<{rCubCjO{*iol>wDS6*qgp&lRGZgZ{UwhillGUyUXL&Lq(}P1R_+HV20$t= z$ob~YvUtucZxi(-3|t=t^&xsl4TX?vGwX=Tvud{@ZqXXe8fD13k*1_*u)K(24PGSW z)nF36ODfAp(HjrxOZ1B~m3z%j5@BY`DjqQt79y=zjj z0PO6ABmuFaJ`6AuV~Ad?yD@BNpIR(LpH6}XhUP}UxcTTt?52%apPoT8;Mw5_(6e)! zAt;Apph>CZc5XduG^tzJ1bK#?#oUA9_?U<8_`_i)5FH}_0+v{?7&qBFIBr^>C68u_ zuo`wT-)XUhdFDo+*kh*Yap`f}N00r@weuen+F{Q$DMLiFNi@l5DykDb{C{4s%N(kR lstjr%(Lt|urXtca6}h;`?rt5vB}uX*T_k>b_*O9a&0mwnGBf}H diff --git a/substrate/srml/balances/src/lib.rs b/substrate/srml/balances/src/lib.rs index 8eee7ad130..775d83e33b 100644 --- a/substrate/srml/balances/src/lib.rs +++ b/substrate/srml/balances/src/lib.rs @@ -283,8 +283,9 @@ impl, I: Instance> Module { /// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that /// the caller will do this. fn set_free_balance(who: &T::AccountId, balance: T::Balance) -> UpdateBalanceOutcome { - // Commented out for no - but consider it instructive. + // Commented out for now - but consider it instructive. // assert!(!Self::total_balance(who).is_zero()); + // assert!(Self::free_balance(who) > Self::existential_deposit()); if balance < Self::existential_deposit() { >::insert(who, balance); Self::on_free_too_low(who); @@ -657,6 +658,19 @@ where UpdateBalanceOutcome ) { let original = Self::free_balance(who); + if balance < Self::existential_deposit() && original.is_zero() { + // If we're attempting to set an existing account to less than ED, then + // bypass the entire operation. It's a no-op if you follow it through, but + // since this is an instance where we might account for a negative imbalance + // (in the dust cleaner of set_free_balance) before we account for its actual + // equal and opposite cause (returned as an Imbalance), then in the + // instance that there's no other accounts on the system at all, we might + // underflow the issuance and our arithmetic will be off. + return ( + SignedImbalance::Positive(Self::PositiveImbalance::zero()), + UpdateBalanceOutcome::AccountKilled, + ) + } let imbalance = if original <= balance { SignedImbalance::Positive(PositiveImbalance(balance - original)) } else { diff --git a/substrate/srml/balances/src/tests.rs b/substrate/srml/balances/src/tests.rs index 802f184aca..123a04524e 100644 --- a/substrate/srml/balances/src/tests.rs +++ b/substrate/srml/balances/src/tests.rs @@ -440,6 +440,36 @@ fn transferring_too_high_value_should_not_panic() { }); } +#[test] +fn account_create_on_free_too_low_with_other() { + with_externalities( + &mut ExtBuilder::default().existential_deposit(100).build(), + || { + let _ = Balances::deposit_creating(&1, 100); + assert_eq!(>::get(), 100); + + // No-op. + let _ = Balances::deposit_creating(&2, 50); + assert_eq!(Balances::free_balance(&2), 0); + assert_eq!(>::get(), 100); + } + ) +} + + +#[test] +fn account_create_on_free_too_low() { + with_externalities( + &mut ExtBuilder::default().existential_deposit(100).build(), + || { + // No-op. + let _ = Balances::deposit_creating(&2, 50); + assert_eq!(Balances::free_balance(&2), 0); + assert_eq!(>::get(), 0); + } + ) +} + #[test] fn account_removal_on_free_too_low() { with_externalities(