Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Balance integration fix #1982

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 61 additions & 51 deletions crates/env/src/engine/off_chain/test_api.rs
Expand Up @@ -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.
Expand All @@ -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<T>(account_id: T::AccountId, new_balance: T::Balance)
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
where
T: Environment<Balance = u128>, // 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);
}

<EnvInstance as OnInstance>::on_instance(|instance| {
instance
.engine
Expand All @@ -74,8 +84,8 @@ where
/// - If `account` does not exist.
/// - If the underlying `account` type does not match.
pub fn get_account_balance<T>(account_id: T::AccountId) -> Result<T::Balance>
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance
Expand All @@ -87,8 +97,8 @@ where

/// Registers a new chain extension.
pub fn register_chain_extension<E>(extension: E)
where
E: ink_engine::ChainExtension + 'static,
where
E: ink_engine::ChainExtension + 'static,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance
Expand Down Expand Up @@ -120,8 +130,8 @@ pub fn set_clear_storage_disabled(_disable: bool) {

/// Advances the chain by a single block.
pub fn advance_block<T>()
where
T: Environment,
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.advance_block();
Expand All @@ -130,9 +140,9 @@ where

/// Sets a caller for the next call.
pub fn set_caller<T>(caller: T::AccountId)
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.set_caller(scale::Encode::encode(&caller));
Expand All @@ -141,9 +151,9 @@ where

/// Sets the callee for the next call.
pub fn set_callee<T>(callee: T::AccountId)
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.set_callee(scale::Encode::encode(&callee));
Expand All @@ -152,9 +162,9 @@ where

/// Sets an account as a contract
pub fn set_contract<T>(contract: T::AccountId)
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance
Expand All @@ -165,9 +175,9 @@ where

/// Returns a boolean to indicate whether an account is a contract
pub fn is_contract<T>(contract: T::AccountId) -> bool
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance
Expand All @@ -180,8 +190,8 @@ where
///
/// This is account id of the currently executing contract.
pub fn callee<T>() -> T::AccountId
where
T: Environment,
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
let callee = instance.engine.get_callee();
Expand All @@ -192,8 +202,8 @@ where

/// Returns the total number of reads and writes of the contract's storage.
pub fn get_contract_storage_rw<T>(account_id: &T::AccountId) -> (usize, usize)
where
T: Environment,
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance
Expand All @@ -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<T>(value: T::Balance)
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.set_value_transferred(value);
Expand All @@ -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<T>(value: T::Balance)
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
{
<EnvInstance as OnInstance>::on_instance(|instance| {
let caller = instance
Expand Down Expand Up @@ -258,8 +268,8 @@ where
///
/// Returns `None` if the `account_id` is non-existent.
pub fn count_used_storage_cells<T>(account_id: &T::AccountId) -> Result<usize>
where
T: Environment,
where
T: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance
Expand All @@ -271,8 +281,8 @@ where

/// Sets the block timestamp for the next [`advance_block`] invocation.
pub fn set_block_timestamp<T>(value: T::Timestamp)
where
T: Environment<Timestamp = u64>,
where
T: Environment<Timestamp = u64>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.set_block_timestamp(value);
Expand All @@ -281,8 +291,8 @@ where

/// Sets the block number for the next [`advance_block`] invocation.
pub fn set_block_number<T>(value: T::BlockNumber)
where
T: Environment<BlockNumber = u32>,
where
T: Environment<BlockNumber = u32>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.engine.set_block_number(value);
Expand All @@ -292,10 +302,10 @@ where
/// Runs the given closure test function with the default configuration
/// for the off-chain environment.
pub fn run_test<T, F>(f: F) -> Result<()>
where
T: Environment,
F: FnOnce(DefaultAccounts<T>) -> Result<()>,
<T as Environment>::AccountId: From<[u8; 32]>,
where
T: Environment,
F: FnOnce(DefaultAccounts<T>) -> Result<()>,
<T as Environment>::AccountId: From<[u8; 32]>,
{
let default_accounts = default_accounts::<T>();
<EnvInstance as OnInstance>::on_instance(|instance| {
Expand All @@ -306,34 +316,34 @@ where
instance.engine.set_callee(encoded_alice.clone());

// set up the funds for the default accounts
let substantial = 1_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)
}

/// Returns the default accounts for testing purposes:
/// Alice, Bob, Charlie, Django, Eve and Frank.
pub fn default_accounts<T>() -> DefaultAccounts<T>
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
{
DefaultAccounts {
alice: T::AccountId::from([0x01; 32]),
Expand All @@ -347,8 +357,8 @@ where

/// The default accounts.
pub struct DefaultAccounts<T>
where
T: Environment,
where
T: Environment,
{
/// The predefined `ALICE` account holding substantial amounts of value.
pub alice: T::AccountId,
Expand Down