Skip to content

Commit c8f9a75

Browse files
authored
Set balance below existential (#1983)
* Added check to panic when set_account_balance receives a value less than the existential minimum * Restored comment * Used ChainSpec::default().minimum_balance as minimum * Removed the 0 from the check to reap the account * Removed tabs and spaces * Added comment for the 0 balance option * Updated CHANGELOG.md * Added tests for set_account_balance * cargo fmt and cargo clippy
1 parent 9bc8b3a commit c8f9a75

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ See [the compatibility section](https://use.ink/faq/migrating-from-ink-4-to-5/#c
200200
- Split up `ink_linting` to mandatory and extra libraries - [#2032](https://github.com/use-ink/ink/pull/2032)
201201
- [E2E] resolve DispatchError error details for dry-runs - [#1994](https://github.com/use-ink/ink/pull/1994)
202202
- [E2E] update to new `drink` API - [#2005](https://github.com/use-ink/ink/pull/2005)
203+
- [E2E] `set_account_balance` now can't set balance below existential deposit - [#1983](https://github.com/paritytech/ink/pull/1983)
203204

204205

205206
## Version 5.0.0-alpha

crates/env/src/engine/off_chain/test_api.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ use ink_engine::test_api::RecordedDebugMessages;
2727
use std::panic::UnwindSafe;
2828

2929
pub use super::call_data::CallData;
30-
pub use ink_engine::ChainExtension;
30+
pub use ink_engine::{
31+
ext::ChainSpec,
32+
ChainExtension,
33+
};
3134

3235
/// Record for an emitted event.
3336
#[derive(Clone)]
@@ -45,15 +48,27 @@ pub struct EmittedEvent {
4548
/// Note that account could refer to either a user account or
4649
/// a smart contract account.
4750
///
51+
/// If a 0 balance is set, this would not fail. This is useful for
52+
/// reaping an account.
53+
///
4854
/// # Errors
4955
///
5056
/// - If `account` does not exist.
5157
/// - If the underlying `account` type does not match.
5258
/// - If the underlying `new_balance` type does not match.
59+
/// - If the `new_balance` is less than the existential minimum.
5360
pub fn set_account_balance<T>(account_id: T::AccountId, new_balance: T::Balance)
5461
where
5562
T: Environment<Balance = u128>, // Just temporary for the MVP!
5663
{
64+
let min = ChainSpec::default().minimum_balance;
65+
if new_balance < min && new_balance != 0u128 {
66+
panic!(
67+
"Balance must be at least [{}]. Use 0 as balance to reap the account.",
68+
min
69+
);
70+
}
71+
5772
<EnvInstance as OnInstance>::on_instance(|instance| {
5873
instance
5974
.engine

crates/env/src/engine/off_chain/tests.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
// limitations under the License.
1414

1515
use crate::{
16-
engine::off_chain::impls::TopicsBuilder,
16+
engine::off_chain::{
17+
impls::TopicsBuilder,
18+
test_api::set_account_balance,
19+
},
1720
event::TopicsBuilderBackend,
21+
types::Environment,
22+
DefaultEnvironment,
1823
Result,
1924
};
2025

@@ -41,3 +46,32 @@ fn topics_builder() -> Result<()> {
4146
Ok(())
4247
})
4348
}
49+
#[test]
50+
fn test_set_account_balance() -> Result<()> {
51+
pub use ink_engine::ext::ChainSpec;
52+
53+
crate::test::run_test::<DefaultEnvironment, _>(|_| {
54+
let minimum_balance = ChainSpec::default().minimum_balance;
55+
56+
let result = std::panic::catch_unwind(|| {
57+
set_account_balance::<DefaultEnvironment>(
58+
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
59+
<DefaultEnvironment as Environment>::Balance::from(minimum_balance - 1),
60+
)
61+
});
62+
63+
assert!(result.is_err());
64+
65+
set_account_balance::<DefaultEnvironment>(
66+
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
67+
<DefaultEnvironment as Environment>::Balance::from(0u128),
68+
);
69+
70+
set_account_balance::<DefaultEnvironment>(
71+
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
72+
<DefaultEnvironment as Environment>::Balance::from(minimum_balance + 1),
73+
);
74+
75+
Ok(())
76+
})
77+
}

0 commit comments

Comments
 (0)