From 757c15ac98ced7cc2d4afbed78b59b2c92bba893 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Wed, 8 Nov 2023 14:41:19 -0300 Subject: [PATCH 1/3] Fixed balance default value in the integration tests Now is the same as in the e2e tests! --- crates/env/src/engine/off_chain/test_api.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index b36c58d225..c312553d4e 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -14,14 +14,8 @@ //! Operations on the off-chain testing environment. -use super::{ - EnvInstance, - OnInstance, -}; -use crate::{ - Environment, - Result, -}; +use super::{EnvInstance, OnInstance}; +use crate::{Environment, Result}; use core::fmt::Debug; use ink_engine::test_api::RecordedDebugMessages; use std::panic::UnwindSafe; @@ -306,7 +300,8 @@ where instance.engine.set_callee(encoded_alice.clone()); // set up the funds for the default accounts - let substantial = 1_000_000; + // the 1_000_000_000 is the same value as in the e2e tests + let substantial = 1_000_000_000; let some = 1_000; instance.engine.set_balance(encoded_alice, substantial); instance From 2a165543eed2e0bf48d7b2682d7ef4ede974afe4 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Thu, 9 Nov 2023 09:25:31 -0300 Subject: [PATCH 2/3] Restored import formatting --- crates/env/src/engine/off_chain/test_api.rs | 10 ++++++++-- 1 file changed, 8 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 c312553d4e..fa3ae00524 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -14,8 +14,14 @@ //! Operations on the off-chain testing environment. -use super::{EnvInstance, OnInstance}; -use crate::{Environment, Result}; +use super::{ + EnvInstance, + OnInstance, +}; +use crate::{ + Environment, + Result, +}; use core::fmt::Debug; use ink_engine::test_api::RecordedDebugMessages; use std::panic::UnwindSafe; From 8ec3b370c25375394d7619a8562e4a68725acf81 Mon Sep 17 00:00:00 2001 From: Facundo Lerena Date: Wed, 15 Nov 2023 11:40:19 -0300 Subject: [PATCH 3/3] Changed the default account balance to the testnet genesis default This is the same value as substrate-contracts-node/node/src/chain_spec.rs `test_genesis` function --- crates/env/src/engine/off_chain/test_api.rs | 113 +++++++++++--------- 1 file changed, 61 insertions(+), 52 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index fa3ae00524..d869c82f20 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. @@ -45,15 +46,24 @@ 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(account_id: T::AccountId, new_balance: T::Balance) -where - T: Environment, // Just temporary for the MVP! + 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(){ + panic!("Balance must be at least [{}]. Use 0 as balance to reap the account.", min); + } + ::on_instance(|instance| { instance .engine @@ -74,8 +84,8 @@ where /// - If `account` does not exist. /// - If the underlying `account` type does not match. pub fn get_account_balance(account_id: T::AccountId) -> Result -where - T: Environment, // Just temporary for the MVP! + where + T: Environment, // Just temporary for the MVP! { ::on_instance(|instance| { instance @@ -87,8 +97,8 @@ where /// Registers a new chain extension. pub fn register_chain_extension(extension: E) -where - E: ink_engine::ChainExtension + 'static, + where + E: ink_engine::ChainExtension + 'static, { ::on_instance(|instance| { instance @@ -120,8 +130,8 @@ pub fn set_clear_storage_disabled(_disable: bool) { /// Advances the chain by a single block. pub fn advance_block() -where - T: Environment, + where + T: Environment, { ::on_instance(|instance| { instance.engine.advance_block(); @@ -130,9 +140,9 @@ where /// Sets a caller for the next call. pub fn set_caller(caller: T::AccountId) -where - T: Environment, - ::AccountId: From<[u8; 32]>, + where + T: Environment, + ::AccountId: From<[u8; 32]>, { ::on_instance(|instance| { instance.engine.set_caller(scale::Encode::encode(&caller)); @@ -141,9 +151,9 @@ where /// Sets the callee for the next call. pub fn set_callee(callee: T::AccountId) -where - T: Environment, - ::AccountId: From<[u8; 32]>, + where + T: Environment, + ::AccountId: From<[u8; 32]>, { ::on_instance(|instance| { instance.engine.set_callee(scale::Encode::encode(&callee)); @@ -152,9 +162,9 @@ where /// Sets an account as a contract pub fn set_contract(contract: T::AccountId) -where - T: Environment, - ::AccountId: From<[u8; 32]>, + where + T: Environment, + ::AccountId: From<[u8; 32]>, { ::on_instance(|instance| { instance @@ -165,9 +175,9 @@ where /// Returns a boolean to indicate whether an account is a contract pub fn is_contract(contract: T::AccountId) -> bool -where - T: Environment, - ::AccountId: From<[u8; 32]>, + where + T: Environment, + ::AccountId: From<[u8; 32]>, { ::on_instance(|instance| { instance @@ -180,8 +190,8 @@ where /// /// This is account id of the currently executing contract. pub fn callee() -> T::AccountId -where - T: Environment, + where + T: Environment, { ::on_instance(|instance| { let callee = instance.engine.get_callee(); @@ -192,8 +202,8 @@ where /// Returns the total number of reads and writes of the contract's storage. pub fn get_contract_storage_rw(account_id: &T::AccountId) -> (usize, usize) -where - T: Environment, + where + T: Environment, { ::on_instance(|instance| { instance @@ -207,8 +217,8 @@ where /// Please note that the acting accounts should be set with [`set_caller()`] and /// [`set_callee()`] beforehand. pub fn set_value_transferred(value: T::Balance) -where - T: Environment, // Just temporary for the MVP! + where + T: Environment, // Just temporary for the MVP! { ::on_instance(|instance| { instance.engine.set_value_transferred(value); @@ -220,8 +230,8 @@ where /// Please note that the acting accounts should be set with [`set_caller()`] and /// [`set_callee()`] beforehand. pub fn transfer_in(value: T::Balance) -where - T: Environment, // Just temporary for the MVP! + where + T: Environment, // Just temporary for the MVP! { ::on_instance(|instance| { let caller = instance @@ -258,8 +268,8 @@ where /// /// Returns `None` if the `account_id` is non-existent. pub fn count_used_storage_cells(account_id: &T::AccountId) -> Result -where - T: Environment, + where + T: Environment, { ::on_instance(|instance| { instance @@ -271,8 +281,8 @@ where /// Sets the block timestamp for the next [`advance_block`] invocation. pub fn set_block_timestamp(value: T::Timestamp) -where - T: Environment, + where + T: Environment, { ::on_instance(|instance| { instance.engine.set_block_timestamp(value); @@ -281,8 +291,8 @@ where /// Sets the block number for the next [`advance_block`] invocation. pub fn set_block_number(value: T::BlockNumber) -where - T: Environment, + where + T: Environment, { ::on_instance(|instance| { instance.engine.set_block_number(value); @@ -292,10 +302,10 @@ where /// Runs the given closure test function with the default configuration /// for the off-chain environment. pub fn run_test(f: F) -> Result<()> -where - T: Environment, - F: FnOnce(DefaultAccounts) -> Result<()>, - ::AccountId: From<[u8; 32]>, + where + T: Environment, + F: FnOnce(DefaultAccounts) -> Result<()>, + ::AccountId: From<[u8; 32]>, { let default_accounts = default_accounts::(); ::on_instance(|instance| { @@ -306,25 +316,24 @@ where instance.engine.set_callee(encoded_alice.clone()); // set up the funds for the default accounts - // the 1_000_000_000 is the same value as in the e2e tests - let substantial = 1_000_000_000; - let some = 1_000; - instance.engine.set_balance(encoded_alice, substantial); + // this is the same as substrate-contracts-node/node/src/chain_spec.rs `test_genesis` function. + let value = (1 << 60); + instance.engine.set_balance(encoded_alice, value); instance .engine - .set_balance(scale::Encode::encode(&default_accounts.bob), some); + .set_balance(scale::Encode::encode(&default_accounts.bob), value); instance .engine - .set_balance(scale::Encode::encode(&default_accounts.charlie), some); + .set_balance(scale::Encode::encode(&default_accounts.charlie), value); instance .engine - .set_balance(scale::Encode::encode(&default_accounts.django), 0); + .set_balance(scale::Encode::encode(&default_accounts.django), value); instance .engine - .set_balance(scale::Encode::encode(&default_accounts.eve), 0); + .set_balance(scale::Encode::encode(&default_accounts.eve), value); instance .engine - .set_balance(scale::Encode::encode(&default_accounts.frank), 0); + .set_balance(scale::Encode::encode(&default_accounts.frank), value); }); f(default_accounts) } @@ -332,9 +341,9 @@ where /// Returns the default accounts for testing purposes: /// Alice, Bob, Charlie, Django, Eve and Frank. pub fn default_accounts() -> DefaultAccounts -where - T: Environment, - ::AccountId: From<[u8; 32]>, + where + T: Environment, + ::AccountId: From<[u8; 32]>, { DefaultAccounts { alice: T::AccountId::from([0x01; 32]), @@ -348,8 +357,8 @@ where /// The default accounts. pub struct DefaultAccounts -where - T: Environment, + where + T: Environment, { /// The predefined `ALICE` account holding substantial amounts of value. pub alice: T::AccountId,