diff --git a/contracts/examples/adder/tests/adder_blackbox_test.rs b/contracts/examples/adder/tests/adder_blackbox_test.rs index 1dafca181a..4b0f740343 100644 --- a/contracts/examples/adder/tests/adder_blackbox_test.rs +++ b/contracts/examples/adder/tests/adder_blackbox_test.rs @@ -61,11 +61,9 @@ fn adder_blackbox() { .returns(ExpectValue(6u32)) .run(); - world.check_state_step( - CheckStateStep::new() - .put_account(OWNER, CheckAccount::new()) - .put_account(SC_ADDER, CheckAccount::new().check_storage("str:sum", "6")), - ); + world.check_account(OWNER); + + world.check_account(SC_ADDER).check_storage("str:sum", "6"); world.write_scenario_trace("trace1.scen.json"); } diff --git a/contracts/examples/crowdfunding-esdt/tests/crowdfunding_esdt_blackbox_test.rs b/contracts/examples/crowdfunding-esdt/tests/crowdfunding_esdt_blackbox_test.rs index cc4235704c..c3a1e383f3 100644 --- a/contracts/examples/crowdfunding-esdt/tests/crowdfunding_esdt_blackbox_test.rs +++ b/contracts/examples/crowdfunding-esdt/tests/crowdfunding_esdt_blackbox_test.rs @@ -118,7 +118,7 @@ impl CrowdfundingESDTTestState { fn check_esdt_balance(&mut self, address: AddressExpr, balance_expr: &str) { self.world - .check_state_account(address) + .check_account(address) .esdt_balance(CF_TOKEN_ID_EXPR, balance_expr); } diff --git a/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs b/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs index ae3e29da45..de877fb76b 100644 --- a/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs +++ b/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs @@ -42,10 +42,10 @@ fn st_blackbox() { .commit(); world - .check_state_account(owner_address) + .check_account(owner_address) .nonce("1") .balance("100") - .check_state_account(other_address) + .check_account(other_address) .nonce("2") .balance("300") .esdt_balance("str:TOKEN-123456", "500") @@ -81,10 +81,10 @@ fn st_blackbox() { .run(); world - .check_state_account(owner_address) + .check_account(owner_address) .nonce("3") .balance("100") - .check_state_account(st_contract) + .check_account(st_contract) .check_storage("str:sum", "6") .commit(); @@ -122,10 +122,10 @@ fn set_state_test() { .commit(); world - .check_state_account(first) + .check_account(first) .nonce(1) .balance("100") - .check_state_account(second) + .check_account(second) .nonce(2) .balance("300") .esdt_balance("str:TOKEN-123456", "500") @@ -139,7 +139,7 @@ fn set_state_test() { .commit(); world - .check_state_account(third) + .check_account(third) .nonce(3) .balance("50") .esdt_nft_balance_and_attributes("str:NFT-123456", "2", "1", Some(Vec::::new())) @@ -156,10 +156,10 @@ fn set_state_test() { .esdt_balance("str:TOKEN-123456", "2"); world - .check_state_account(fourth) + .check_account(fourth) .nonce(4) .balance("400") - .check_state_account(fifth) + .check_account(fifth) .nonce(5) .balance("250") .esdt_balance("str:TOKEN-123456", "2"); @@ -171,7 +171,7 @@ fn set_state_test() { .esdt_balance("str:TOKEN-123456", "60"); world - .check_state_account(sixth) + .check_account(sixth) .nonce(6) .balance("600") .esdt_balance("str:TOKEN-123456", "60"); diff --git a/framework/scenario/src/facade/world_tx.rs b/framework/scenario/src/facade/world_tx.rs index 2fa69c744e..a2d7373a42 100644 --- a/framework/scenario/src/facade/world_tx.rs +++ b/framework/scenario/src/facade/world_tx.rs @@ -1,11 +1,11 @@ -#![allow(unused)] // TEMP - -mod scenario_env; -mod scenario_env_deploy; -mod scenario_env_exec; -mod scenario_env_query; +mod scenario_check_state; +mod scenario_exec_call; +mod scenario_exec_deploy; +mod scenario_query_call; mod scenario_rh_impl; +mod scenario_set_state; +mod scenario_tx_env; -pub use scenario_env::{ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun}; -pub use scenario_env_exec::ScenarioEnvExec; -pub use scenario_env_query::ScenarioEnvQuery; +pub use scenario_exec_call::ScenarioEnvExec; +pub use scenario_query_call::ScenarioEnvQuery; +pub use scenario_tx_env::{ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun}; diff --git a/framework/scenario/src/facade/world_tx/scenario_env_query.rs b/framework/scenario/src/facade/world_tx/scenario_check_state.rs similarity index 70% rename from framework/scenario/src/facade/world_tx/scenario_env_query.rs rename to framework/scenario/src/facade/world_tx/scenario_check_state.rs index 387ad0f444..ffb4a2754b 100644 --- a/framework/scenario/src/facade/world_tx/scenario_env_query.rs +++ b/framework/scenario/src/facade/world_tx/scenario_check_state.rs @@ -1,100 +1,20 @@ -use std::{ - collections::{btree_map::Entry, BTreeMap}, - path::PathBuf, -}; +use std::collections::{btree_map::Entry, BTreeMap}; use multiversx_chain_scenario_format::interpret_trait::{InterpretableFrom, InterpreterContext}; -use multiversx_sc::{ - codec::test_util::top_encode_to_vec_u8_or_panic, - proxy_imports::TopEncode, - tuple_util::NestedTupleFlatten, - types::{ - AnnotatedValue, FunctionCall, ManagedAddress, ManagedBuffer, RHListExec, Tx, TxBaseWithEnv, - TxEnv, TxFromSpecified, TxGas, TxPayment, TxToSpecified, - }, -}; +use multiversx_sc::{codec::test_util::top_encode_to_vec_u8_or_panic, proxy_imports::TopEncode}; use crate::{ - api::StaticApi, - scenario::{tx_to_step::TxToQueryStep, ScenarioRunner}, + scenario::ScenarioRunner, scenario_model::{ AddressKey, BigUintValue, BytesKey, BytesValue, CheckAccount, CheckEsdt, CheckEsdtData, CheckEsdtInstances, CheckEsdtMap, CheckEsdtMapContents, CheckStateStep, CheckStorage, - CheckStorageDetails, CheckValue, TxExpect, TxResponse, U64Value, + CheckStorageDetails, CheckValue, U64Value, }, - ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun, ScenarioWorld, + ScenarioWorld, }; -pub struct ScenarioEnvQuery<'w> { - pub world: &'w mut ScenarioWorld, - pub data: ScenarioTxEnvData, -} - -impl<'w> TxEnv for ScenarioEnvQuery<'w> { - type Api = StaticApi; - - type RHExpect = TxExpect; - - fn resolve_sender_address(&self) -> ManagedAddress { - panic!("Explicit sender address expected") - } - - fn default_gas_annotation(&self) -> ManagedBuffer { - self.data.default_gas_annotation() - } - - fn default_gas_value(&self) -> u64 { - self.data.default_gas_value() - } -} - -impl<'w> ScenarioTxEnv for ScenarioEnvQuery<'w> { - fn env_data(&self) -> &ScenarioTxEnvData { - &self.data - } -} - -impl<'w, To, RH> ScenarioTxRun - for Tx, (), To, (), (), FunctionCall, RH> -where - To: TxToSpecified>, - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - type Returns = ::Unpacked; - - fn run(self) -> Self::Returns { - let mut step_wrapper = self.tx_to_query_step(); - step_wrapper.env.world.sc_query(&mut step_wrapper.step); - step_wrapper.process_result() - } -} - impl ScenarioWorld { - pub fn query(&mut self) -> TxBaseWithEnv> { - let data = self.new_env_data(); - let env = ScenarioEnvQuery { world: self, data }; - Tx::new_with_env(env) - } - - pub fn chain_query(&mut self, f: F) -> &mut Self - where - To: TxToSpecified, - RH: RHListExec, - F: FnOnce( - TxBaseWithEnv, - ) -> Tx, RH>, - { - let env = self.new_env_data(); - let tx_base = TxBaseWithEnv::new_with_env(env); - let tx = f(tx_base); - let mut step_wrapper = tx.tx_to_query_step(); - self.sc_query(&mut step_wrapper.step); - step_wrapper.process_result(); - self - } - - pub fn check_state_account(&mut self, address: A) -> CheckStateBuilder<'_> + pub fn check_account(&mut self, address: A) -> CheckStateBuilder<'_> where AddressKey: From, { @@ -122,7 +42,7 @@ impl<'w> CheckStateBuilder<'w> { } /// Starts building of a new account. - pub fn check_state_account(mut self, address_expr: A) -> Self + pub fn check_account(mut self, address_expr: A) -> Self where AddressKey: From, { diff --git a/framework/scenario/src/facade/world_tx/scenario_exec_call.rs b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs new file mode 100644 index 0000000000..e92e38087c --- /dev/null +++ b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs @@ -0,0 +1,94 @@ +use multiversx_sc::{ + tuple_util::NestedTupleFlatten, + types::{ + FunctionCall, ManagedAddress, ManagedBuffer, RHListExec, Tx, TxBaseWithEnv, TxEnv, + TxFromSpecified, TxGas, TxPayment, TxToSpecified, + }, +}; + +use crate::{ + api::StaticApi, + scenario::tx_to_step::TxToStep, + scenario_model::{TxExpect, TxResponse}, + ScenarioTxEnv, ScenarioTxRun, ScenarioWorld, +}; + +use super::ScenarioTxEnvData; + +/// Environment for executing transactions. +pub struct ScenarioEnvExec<'w> { + pub world: &'w mut ScenarioWorld, + pub data: ScenarioTxEnvData, +} + +impl<'w> TxEnv for ScenarioEnvExec<'w> { + type Api = StaticApi; + + type RHExpect = TxExpect; + + fn resolve_sender_address(&self) -> ManagedAddress { + panic!("Explicit sender address expected") + } + + fn default_gas_annotation(&self) -> ManagedBuffer { + self.data.default_gas_annotation() + } + + fn default_gas_value(&self) -> u64 { + self.data.default_gas_value() + } +} + +impl<'w> ScenarioTxEnv for ScenarioEnvExec<'w> { + fn env_data(&self) -> &ScenarioTxEnvData { + &self.data + } +} + +impl<'w, From, To, Payment, Gas, RH> ScenarioTxRun + for Tx, From, To, Payment, Gas, FunctionCall, RH> +where + From: TxFromSpecified>, + To: TxToSpecified>, + Payment: TxPayment>, + Gas: TxGas>, + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + type Returns = ::Unpacked; + + fn run(self) -> Self::Returns { + let mut step_wrapper = self.tx_to_step(); + step_wrapper.env.world.sc_call(&mut step_wrapper.step); + step_wrapper.process_result() + } +} + +impl ScenarioWorld { + pub fn tx(&mut self) -> TxBaseWithEnv> { + let data = self.new_env_data(); + let env = ScenarioEnvExec { world: self, data }; + Tx::new_with_env(env) + } + + pub fn chain_call(&mut self, f: F) -> &mut Self + where + From: TxFromSpecified, + To: TxToSpecified, + Payment: TxPayment, + Gas: TxGas, + RH: RHListExec, + F: FnOnce( + TxBaseWithEnv, + ) + -> Tx, RH>, + { + let env = self.new_env_data(); + let tx_base = TxBaseWithEnv::new_with_env(env); + let tx = f(tx_base); + let mut step_wrapper = tx.tx_to_step(); + self.sc_call(&mut step_wrapper.step); + step_wrapper.process_result(); + self + } +} diff --git a/framework/scenario/src/facade/world_tx/scenario_env_deploy.rs b/framework/scenario/src/facade/world_tx/scenario_exec_deploy.rs similarity index 77% rename from framework/scenario/src/facade/world_tx/scenario_env_deploy.rs rename to framework/scenario/src/facade/world_tx/scenario_exec_deploy.rs index c218f06869..866d4f562f 100644 --- a/framework/scenario/src/facade/world_tx/scenario_env_deploy.rs +++ b/framework/scenario/src/facade/world_tx/scenario_exec_deploy.rs @@ -1,20 +1,14 @@ -use std::path::PathBuf; - -use multiversx_chain_scenario_format::serde_raw::ValueSubTree; use multiversx_sc::{ tuple_util::NestedTupleFlatten, types::{ - AnnotatedValue, Code, DeployCall, FunctionCall, ManagedAddress, ManagedBuffer, RHListExec, - Tx, TxBaseWithEnv, TxCodeSource, TxCodeSourceSpecified, TxCodeValue, TxEnv, - TxFromSpecified, TxGas, TxPayment, TxToSpecified, + Code, DeployCall, RHListExec, Tx, TxBaseWithEnv, TxCodeValue, TxFromSpecified, TxGas, + TxPayment, }, }; use crate::{ - api::StaticApi, - scenario::tx_to_step::TxToStep, - scenario_model::{AddressValue, BytesValue, ScCallStep, ScDeployStep, TxResponse}, - ScenarioEnvExec, ScenarioTxEnv, ScenarioTxRun, ScenarioWorld, + scenario::tx_to_step::TxToStep, scenario_model::TxResponse, ScenarioEnvExec, ScenarioTxRun, + ScenarioWorld, }; use super::ScenarioTxEnvData; diff --git a/framework/scenario/src/facade/world_tx/scenario_query_call.rs b/framework/scenario/src/facade/world_tx/scenario_query_call.rs new file mode 100644 index 0000000000..dbcf3130f5 --- /dev/null +++ b/framework/scenario/src/facade/world_tx/scenario_query_call.rs @@ -0,0 +1,84 @@ +use multiversx_sc::{ + tuple_util::NestedTupleFlatten, + types::{ + FunctionCall, ManagedAddress, ManagedBuffer, RHListExec, Tx, TxBaseWithEnv, TxEnv, + TxToSpecified, + }, +}; + +use crate::{ + api::StaticApi, + scenario::tx_to_step::TxToQueryStep, + scenario_model::{TxExpect, TxResponse}, + ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun, ScenarioWorld, +}; + +pub struct ScenarioEnvQuery<'w> { + pub world: &'w mut ScenarioWorld, + pub data: ScenarioTxEnvData, +} + +impl<'w> TxEnv for ScenarioEnvQuery<'w> { + type Api = StaticApi; + + type RHExpect = TxExpect; + + fn resolve_sender_address(&self) -> ManagedAddress { + panic!("Explicit sender address expected") + } + + fn default_gas_annotation(&self) -> ManagedBuffer { + self.data.default_gas_annotation() + } + + fn default_gas_value(&self) -> u64 { + self.data.default_gas_value() + } +} + +impl<'w> ScenarioTxEnv for ScenarioEnvQuery<'w> { + fn env_data(&self) -> &ScenarioTxEnvData { + &self.data + } +} + +impl<'w, To, RH> ScenarioTxRun + for Tx, (), To, (), (), FunctionCall, RH> +where + To: TxToSpecified>, + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + type Returns = ::Unpacked; + + fn run(self) -> Self::Returns { + let mut step_wrapper = self.tx_to_query_step(); + step_wrapper.env.world.sc_query(&mut step_wrapper.step); + step_wrapper.process_result() + } +} + +impl ScenarioWorld { + pub fn query(&mut self) -> TxBaseWithEnv> { + let data = self.new_env_data(); + let env = ScenarioEnvQuery { world: self, data }; + Tx::new_with_env(env) + } + + pub fn chain_query(&mut self, f: F) -> &mut Self + where + To: TxToSpecified, + RH: RHListExec, + F: FnOnce( + TxBaseWithEnv, + ) -> Tx, RH>, + { + let env = self.new_env_data(); + let tx_base = TxBaseWithEnv::new_with_env(env); + let tx = f(tx_base); + let mut step_wrapper = tx.tx_to_query_step(); + self.sc_query(&mut step_wrapper.step); + step_wrapper.process_result(); + self + } +} diff --git a/framework/scenario/src/facade/world_tx/scenario_rh_impl.rs b/framework/scenario/src/facade/world_tx/scenario_rh_impl.rs index bbf29a0d03..d040f0bc3a 100644 --- a/framework/scenario/src/facade/world_tx/scenario_rh_impl.rs +++ b/framework/scenario/src/facade/world_tx/scenario_rh_impl.rs @@ -1,18 +1,12 @@ use multiversx_sc::{ codec::{CodecFrom, TopDecodeMulti, TopEncodeMulti}, types::{ - ManagedAddress, RHList, RHListItem, RHListItemExec, ReturnsNewAddress, - ReturnsNewManagedAddress, ReturnsResult, ReturnsResultConv, TxEnv, WithNewAddress, - WithResultConv, + ManagedAddress, RHListItemExec, ReturnsNewAddress, ReturnsNewManagedAddress, ReturnsResult, + ReturnsResultConv, TxEnv, WithNewAddress, WithResultConv, }, }; -use crate::{ - api::StaticApi, - scenario_model::{TxResponse, TypedResponse}, -}; - -use super::ScenarioTxEnvData; +use crate::scenario_model::{TxResponse, TypedResponse}; impl RHListItemExec for ReturnsResult where diff --git a/framework/scenario/src/facade/world_tx/scenario_env_exec.rs b/framework/scenario/src/facade/world_tx/scenario_set_state.rs similarity index 66% rename from framework/scenario/src/facade/world_tx/scenario_env_exec.rs rename to framework/scenario/src/facade/world_tx/scenario_set_state.rs index 5eef522784..110b1b9692 100644 --- a/framework/scenario/src/facade/world_tx/scenario_env_exec.rs +++ b/framework/scenario/src/facade/world_tx/scenario_set_state.rs @@ -1,105 +1,15 @@ -use std::{collections::btree_map::Entry, ops::Add, path::PathBuf}; - -use multiversx_chain_scenario_format::serde_raw::ValueSubTree; -use multiversx_sc::{ - tuple_util::NestedTupleFlatten, - types::{ - Address, AddressExpr, AnnotatedValue, Code, DeployCall, FunctionCall, ManagedAddress, - ManagedBuffer, RHListExec, Tx, TxBaseWithEnv, TxCodeSource, TxCodeSourceSpecified, - TxCodeValue, TxEnv, TxFromSpecified, TxGas, TxPayment, TxTo, TxToSpecified, - }, -}; -use serde_json::map::OccupiedEntry; +use std::collections::btree_map::Entry; use crate::{ - api::StaticApi, - scenario::{tx_to_step::TxToStep, ScenarioRunner}, + scenario::ScenarioRunner, scenario_model::{ Account, AddressKey, AddressValue, BigUintValue, BytesKey, BytesValue, Esdt, EsdtObject, - NewAddress, ScCallStep, ScDeployStep, SetStateStep, TxExpect, TxResponse, U64Value, + SetStateStep, U64Value, }, - ScenarioTxEnv, ScenarioTxRun, ScenarioWorld, + ScenarioWorld, }; -use super::ScenarioTxEnvData; - -/// Environment for executing transactions. -pub struct ScenarioEnvExec<'w> { - pub world: &'w mut ScenarioWorld, - pub data: ScenarioTxEnvData, -} - -impl<'w> TxEnv for ScenarioEnvExec<'w> { - type Api = StaticApi; - - type RHExpect = TxExpect; - - fn resolve_sender_address(&self) -> ManagedAddress { - panic!("Explicit sender address expected") - } - - fn default_gas_annotation(&self) -> ManagedBuffer { - self.data.default_gas_annotation() - } - - fn default_gas_value(&self) -> u64 { - self.data.default_gas_value() - } -} - -impl<'w> ScenarioTxEnv for ScenarioEnvExec<'w> { - fn env_data(&self) -> &ScenarioTxEnvData { - &self.data - } -} - -impl<'w, From, To, Payment, Gas, RH> ScenarioTxRun - for Tx, From, To, Payment, Gas, FunctionCall, RH> -where - From: TxFromSpecified>, - To: TxToSpecified>, - Payment: TxPayment>, - Gas: TxGas>, - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - type Returns = ::Unpacked; - - fn run(self) -> Self::Returns { - let mut step_wrapper = self.tx_to_step(); - step_wrapper.env.world.sc_call(&mut step_wrapper.step); - step_wrapper.process_result() - } -} - impl ScenarioWorld { - pub fn tx(&mut self) -> TxBaseWithEnv> { - let data = self.new_env_data(); - let env = ScenarioEnvExec { world: self, data }; - Tx::new_with_env(env) - } - - pub fn chain_call(&mut self, f: F) -> &mut Self - where - From: TxFromSpecified, - To: TxToSpecified, - Payment: TxPayment, - Gas: TxGas, - RH: RHListExec, - F: FnOnce( - TxBaseWithEnv, - ) - -> Tx, RH>, - { - let env = self.new_env_data(); - let tx_base = TxBaseWithEnv::new_with_env(env); - let tx = f(tx_base); - let mut step_wrapper = tx.tx_to_step(); - self.sc_call(&mut step_wrapper.step); - step_wrapper.process_result(); - self - } - pub fn account(&mut self, address_expr: A) -> SetStateBuilder<'_> where AddressKey: From, diff --git a/framework/scenario/src/facade/world_tx/scenario_env.rs b/framework/scenario/src/facade/world_tx/scenario_tx_env.rs similarity index 86% rename from framework/scenario/src/facade/world_tx/scenario_env.rs rename to framework/scenario/src/facade/world_tx/scenario_tx_env.rs index aac1952a34..d195a7600d 100644 --- a/framework/scenario/src/facade/world_tx/scenario_env.rs +++ b/framework/scenario/src/facade/world_tx/scenario_tx_env.rs @@ -1,12 +1,8 @@ use std::path::PathBuf; -use multiversx_sc::types::{AnnotatedValue, ManagedAddress, ManagedBuffer, TxBaseWithEnv, TxEnv}; +use multiversx_sc::types::{ManagedAddress, ManagedBuffer, TxEnv}; -use crate::{ - api::StaticApi, - scenario_model::{TxExpect, TxResponse}, - ScenarioWorld, -}; +use crate::{api::StaticApi, scenario_model::TxExpect, ScenarioWorld}; /// Designates a tx environment suitable for running scenarios locally. pub trait ScenarioTxEnv: TxEnv {