Skip to content

Commit

Permalink
Set balance below existential (#1983)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
faculerena authored Nov 13, 2024
1 parent 9bc8b3a commit c8f9a75
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ See [the compatibility section](https://use.ink/faq/migrating-from-ink-4-to-5/#c
- Split up `ink_linting` to mandatory and extra libraries - [#2032](https://github.com/use-ink/ink/pull/2032)
- [E2E] resolve DispatchError error details for dry-runs - [#1994](https://github.com/use-ink/ink/pull/1994)
- [E2E] update to new `drink` API - [#2005](https://github.com/use-ink/ink/pull/2005)
- [E2E] `set_account_balance` now can't set balance below existential deposit - [#1983](https://github.com/paritytech/ink/pull/1983)


## Version 5.0.0-alpha
Expand Down
17 changes: 16 additions & 1 deletion crates/env/src/engine/off_chain/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use ink_engine::test_api::RecordedDebugMessages;
use std::panic::UnwindSafe;

pub use super::call_data::CallData;
pub use ink_engine::ChainExtension;
pub use ink_engine::{
ext::ChainSpec,
ChainExtension,
};

/// Record for an emitted event.
#[derive(Clone)]
Expand All @@ -45,15 +48,27 @@ pub struct EmittedEvent {
/// Note that account could refer to either a user account or
/// a smart contract account.
///
/// If a 0 balance is set, this would not fail. This is useful for
/// reaping an account.
///
/// # Errors
///
/// - If `account` does not exist.
/// - If the underlying `account` type does not match.
/// - If the underlying `new_balance` type does not match.
/// - If the `new_balance` is less than the existential minimum.
pub fn set_account_balance<T>(account_id: T::AccountId, new_balance: T::Balance)
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
{
let min = ChainSpec::default().minimum_balance;
if new_balance < min && new_balance != 0u128 {
panic!(
"Balance must be at least [{}]. Use 0 as balance to reap the account.",
min
);
}

<EnvInstance as OnInstance>::on_instance(|instance| {
instance
.engine
Expand Down
36 changes: 35 additions & 1 deletion crates/env/src/engine/off_chain/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
// limitations under the License.

use crate::{
engine::off_chain::impls::TopicsBuilder,
engine::off_chain::{
impls::TopicsBuilder,
test_api::set_account_balance,
},
event::TopicsBuilderBackend,
types::Environment,
DefaultEnvironment,
Result,
};

Expand All @@ -41,3 +46,32 @@ fn topics_builder() -> Result<()> {
Ok(())
})
}
#[test]
fn test_set_account_balance() -> Result<()> {
pub use ink_engine::ext::ChainSpec;

crate::test::run_test::<DefaultEnvironment, _>(|_| {
let minimum_balance = ChainSpec::default().minimum_balance;

let result = std::panic::catch_unwind(|| {
set_account_balance::<DefaultEnvironment>(
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
<DefaultEnvironment as Environment>::Balance::from(minimum_balance - 1),
)
});

assert!(result.is_err());

set_account_balance::<DefaultEnvironment>(
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
<DefaultEnvironment as Environment>::Balance::from(0u128),
);

set_account_balance::<DefaultEnvironment>(
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
<DefaultEnvironment as Environment>::Balance::from(minimum_balance + 1),
);

Ok(())
})
}

0 comments on commit c8f9a75

Please sign in to comment.