From a274b902e01a55c3d1e58c137819c825396abba2 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Thu, 9 Nov 2023 13:48:06 -0300 Subject: [PATCH 1/9] Added check to panic when set_account_balance receives a value less than the existential minimum --- crates/env/src/engine/off_chain/test_api.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index b36c58d2252..d96389159ea 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -49,18 +49,21 @@ pub struct EmittedEvent { /// /// - 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(account_id: T::AccountId, new_balance: T::Balance) where T: Environment, // Just temporary for the MVP! { + if new_balance < T::Balance::from(1_000_000u64) { + panic!("balance must be at least 1_000_000"); + } + ::on_instance(|instance| { instance .engine .set_balance(scale::Encode::encode(&account_id), new_balance); }) } - /// Returns the balance of the account. /// /// # Note From 95be1da6f1661d1cdaeda35183c8e0457e2559ca Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Thu, 9 Nov 2023 13:48:50 -0300 Subject: [PATCH 2/9] Restored comment --- crates/env/src/engine/off_chain/test_api.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index d96389159ea..826a468542a 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -49,6 +49,7 @@ pub struct EmittedEvent { /// /// - 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(account_id: T::AccountId, new_balance: T::Balance) where From f0a03541f000c3ba9ecaae3890aeaec2ae1c02be Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Thu, 9 Nov 2023 14:38:18 -0300 Subject: [PATCH 3/9] Used ChainSpec::default().minimum_balance as minimum --- crates/env/src/engine/off_chain/test_api.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 826a468542a..24a102d318a 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -27,6 +27,7 @@ use ink_engine::test_api::RecordedDebugMessages; use std::panic::UnwindSafe; pub use super::call_data::CallData; +pub use ink_engine::ext::ChainSpec; pub use ink_engine::ChainExtension; /// Record for an emitted event. @@ -55,8 +56,9 @@ pub fn set_account_balance(account_id: T::AccountId, new_balance: T::Balance) where T: Environment, // Just temporary for the MVP! { - if new_balance < T::Balance::from(1_000_000u64) { - panic!("balance must be at least 1_000_000"); + let min = ChainSpec::default().minimum_balance; + if new_balance < T::Balance::from(min) { + panic!("balance must be at least [{}]", min); } ::on_instance(|instance| { From a31fcc544fdfaee1e939b65ea380bfb3db01d912 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Tue, 14 Nov 2023 12:57:18 -0300 Subject: [PATCH 4/9] Removed the 0 from the check to reap the account --- crates/env/src/engine/off_chain/test_api.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 24a102d318a..e0f67c8a9ba 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -1,4 +1,4 @@ -// Copyright (C) Parity Technologies (UK) Ltd. + // Copyright (C) Parity Technologies (UK) Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -57,8 +57,8 @@ where T: Environment, // Just temporary for the MVP! { let min = ChainSpec::default().minimum_balance; - if new_balance < T::Balance::from(min) { - panic!("balance must be at least [{}]", min); + if new_balance < T::Balance::from(min) && new_balance != 0.into(){ + panic!("Balance must be at least [{}]. Use 0 as balance to reap the account.", min); } ::on_instance(|instance| { From 6bbb3c0d72c1e3b10ac5f3a5dfd395c5e702e8b6 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Tue, 14 Nov 2023 12:58:19 -0300 Subject: [PATCH 5/9] Removed tabs and spaces --- crates/env/src/engine/off_chain/test_api.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index e0f67c8a9ba..bc88743dd9b 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -1,4 +1,4 @@ - // Copyright (C) Parity Technologies (UK) Ltd. +// Copyright (C) Parity Technologies (UK) Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -67,6 +67,7 @@ where .set_balance(scale::Encode::encode(&account_id), new_balance); }) } + /// Returns the balance of the account. /// /// # Note From 81a9ae9766405f55eed6a08c1f93262207e191b4 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Tue, 14 Nov 2023 12:59:36 -0300 Subject: [PATCH 6/9] Added comment for the 0 balance option --- crates/env/src/engine/off_chain/test_api.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index bc88743dd9b..9b67a5bc972 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -46,6 +46,9 @@ 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. From 0db0abedf2d581cecad70cb1bf7dd08f50dce081 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Tue, 21 Nov 2023 11:40:50 -0300 Subject: [PATCH 7/9] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17f08c0858d..9944b541680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Messages return `TypeSpec` directly - #[1999](https://github.com/paritytech/ink/pull/1999) - Fail when decoding from storage and not all bytes consumed - [#1897](https://github.com/paritytech/ink/pull/1897) - [E2E] resolve DispatchError error details for dry-runs - [#1944](https://github.com/paritytech/ink/pull/1994) +- [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 From 8c38310e64787ad7b910455304536f0e9420bea2 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Wed, 22 Nov 2023 12:46:54 -0300 Subject: [PATCH 8/9] Added tests for set_account_balance --- crates/env/src/engine/off_chain/test_api.rs | 2 +- crates/env/src/engine/off_chain/tests.rs | 36 ++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 9b67a5bc972..0f015110e29 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -60,7 +60,7 @@ where T: Environment, // Just temporary for the MVP! { let min = ChainSpec::default().minimum_balance; - if new_balance < T::Balance::from(min) && new_balance != 0.into(){ + if new_balance < T::Balance::from(min) && new_balance != 0u128.into(){ panic!("Balance must be at least [{}]. Use 0 as balance to reap the account.", min); } diff --git a/crates/env/src/engine/off_chain/tests.rs b/crates/env/src/engine/off_chain/tests.rs index 3127e7850ce..4ec2b4a91e4 100644 --- a/crates/env/src/engine/off_chain/tests.rs +++ b/crates/env/src/engine/off_chain/tests.rs @@ -12,11 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::{ - engine::off_chain::impls::TopicsBuilder, - event::TopicsBuilderBackend, - Result, -}; +use crate::{engine::off_chain::impls::TopicsBuilder, + event::TopicsBuilderBackend, + Result, + engine::off_chain::test_api::set_account_balance, + types::Environment, + DefaultEnvironment}; #[test] fn topics_builder() -> Result<()> { @@ -41,3 +42,28 @@ fn topics_builder() -> Result<()> { Ok(()) }) } +#[test] +fn test_set_account_balance() -> Result<()> { + pub use ink_engine::ext::ChainSpec; + + crate::test::run_test::(|_| { + + let minimum_balance = ChainSpec::default().minimum_balance; + + let result = std::panic::catch_unwind(|| {set_account_balance::( + ::AccountId::from([0x1; 32]), + ::Balance::from(minimum_balance - 1))}); + + assert!(result.is_err()); + + set_account_balance::( + ::AccountId::from([0x1; 32]), + ::Balance::from(0u128)); + + set_account_balance::( + ::AccountId::from([0x1; 32]), + ::Balance::from(minimum_balance + 1)); + + Ok(()) + }) +} \ No newline at end of file From dae6d32536eda915063f85c15cc860405913d491 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Thu, 7 Nov 2024 10:13:35 -0300 Subject: [PATCH 9/9] cargo fmt and cargo clippy --- crates/env/src/engine/off_chain/test_api.rs | 13 +++++--- crates/env/src/engine/off_chain/tests.rs | 34 +++++++++++++-------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 930ffe3f57a..5c1e00bfeb2 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -27,8 +27,10 @@ use ink_engine::test_api::RecordedDebugMessages; use std::panic::UnwindSafe; pub use super::call_data::CallData; -pub use ink_engine::ext::ChainSpec; -pub use ink_engine::ChainExtension; +pub use ink_engine::{ + ext::ChainSpec, + ChainExtension, +}; /// Record for an emitted event. #[derive(Clone)] @@ -60,8 +62,11 @@ where T: Environment, // Just temporary for the MVP! { let min = ChainSpec::default().minimum_balance; - if new_balance < T::Balance::from(min) && new_balance != 0u128.into(){ - panic!("Balance must be at least [{}]. Use 0 as balance to reap the account.", min); + if new_balance < min && new_balance != 0u128 { + panic!( + "Balance must be at least [{}]. Use 0 as balance to reap the account.", + min + ); } ::on_instance(|instance| { diff --git a/crates/env/src/engine/off_chain/tests.rs b/crates/env/src/engine/off_chain/tests.rs index ad6e426d00e..9b9848576cc 100644 --- a/crates/env/src/engine/off_chain/tests.rs +++ b/crates/env/src/engine/off_chain/tests.rs @@ -12,12 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::{engine::off_chain::impls::TopicsBuilder, - event::TopicsBuilderBackend, - Result, - engine::off_chain::test_api::set_account_balance, - types::Environment, - DefaultEnvironment}; +use crate::{ + engine::off_chain::{ + impls::TopicsBuilder, + test_api::set_account_balance, + }, + event::TopicsBuilderBackend, + types::Environment, + DefaultEnvironment, + Result, +}; #[test] fn topics_builder() -> Result<()> { @@ -47,23 +51,27 @@ fn test_set_account_balance() -> Result<()> { pub use ink_engine::ext::ChainSpec; crate::test::run_test::(|_| { - let minimum_balance = ChainSpec::default().minimum_balance; - let result = std::panic::catch_unwind(|| {set_account_balance::( - ::AccountId::from([0x1; 32]), - ::Balance::from(minimum_balance - 1))}); + let result = std::panic::catch_unwind(|| { + set_account_balance::( + ::AccountId::from([0x1; 32]), + ::Balance::from(minimum_balance - 1), + ) + }); assert!(result.is_err()); set_account_balance::( ::AccountId::from([0x1; 32]), - ::Balance::from(0u128)); + ::Balance::from(0u128), + ); set_account_balance::( ::AccountId::from([0x1; 32]), - ::Balance::from(minimum_balance + 1)); + ::Balance::from(minimum_balance + 1), + ); Ok(()) }) -} \ No newline at end of file +}