Skip to content

Commit

Permalink
Merge pull request #1572 from multiversx/set-check-refactor
Browse files Browse the repository at this point in the history
Scenario set/check refactor
  • Loading branch information
andrei-marinica authored Apr 17, 2024
2 parents 9542a5a + 9549b7e commit b9bd798
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 231 deletions.
8 changes: 3 additions & 5 deletions contracts/examples/adder/tests/adder_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
20 changes: 10 additions & 10 deletions contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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")
Expand All @@ -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::<u8>::new()))
Expand All @@ -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");
Expand All @@ -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");
Expand Down
18 changes: 9 additions & 9 deletions framework/scenario/src/facade/world_tx.rs
Original file line number Diff line number Diff line change
@@ -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};
Original file line number Diff line number Diff line change
@@ -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<Self::Api> {
panic!("Explicit sender address expected")
}

fn default_gas_annotation(&self) -> ManagedBuffer<Self::Api> {
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<ScenarioEnvQuery<'w>, (), To, (), (), FunctionCall<StaticApi>, RH>
where
To: TxToSpecified<ScenarioEnvQuery<'w>>,
RH: RHListExec<TxResponse, ScenarioEnvQuery<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
type Returns = <RH::ListReturns as NestedTupleFlatten>::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<ScenarioEnvQuery<'_>> {
let data = self.new_env_data();
let env = ScenarioEnvQuery { world: self, data };
Tx::new_with_env(env)
}

pub fn chain_query<To, RH, F>(&mut self, f: F) -> &mut Self
where
To: TxToSpecified<ScenarioTxEnvData>,
RH: RHListExec<TxResponse, ScenarioTxEnvData, ListReturns = ()>,
F: FnOnce(
TxBaseWithEnv<ScenarioTxEnvData>,
) -> Tx<ScenarioTxEnvData, (), To, (), (), FunctionCall<StaticApi>, 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<A>(&mut self, address: A) -> CheckStateBuilder<'_>
pub fn check_account<A>(&mut self, address: A) -> CheckStateBuilder<'_>
where
AddressKey: From<A>,
{
Expand Down Expand Up @@ -122,7 +42,7 @@ impl<'w> CheckStateBuilder<'w> {
}

/// Starts building of a new account.
pub fn check_state_account<A>(mut self, address_expr: A) -> Self
pub fn check_account<A>(mut self, address_expr: A) -> Self
where
AddressKey: From<A>,
{
Expand Down
94 changes: 94 additions & 0 deletions framework/scenario/src/facade/world_tx/scenario_exec_call.rs
Original file line number Diff line number Diff line change
@@ -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<Self::Api> {
panic!("Explicit sender address expected")
}

fn default_gas_annotation(&self) -> ManagedBuffer<Self::Api> {
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<ScenarioEnvExec<'w>, From, To, Payment, Gas, FunctionCall<StaticApi>, RH>
where
From: TxFromSpecified<ScenarioEnvExec<'w>>,
To: TxToSpecified<ScenarioEnvExec<'w>>,
Payment: TxPayment<ScenarioEnvExec<'w>>,
Gas: TxGas<ScenarioEnvExec<'w>>,
RH: RHListExec<TxResponse, ScenarioEnvExec<'w>>,
RH::ListReturns: NestedTupleFlatten,
{
type Returns = <RH::ListReturns as NestedTupleFlatten>::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<ScenarioEnvExec<'_>> {
let data = self.new_env_data();
let env = ScenarioEnvExec { world: self, data };
Tx::new_with_env(env)
}

pub fn chain_call<From, To, Payment, Gas, RH, F>(&mut self, f: F) -> &mut Self
where
From: TxFromSpecified<ScenarioTxEnvData>,
To: TxToSpecified<ScenarioTxEnvData>,
Payment: TxPayment<ScenarioTxEnvData>,
Gas: TxGas<ScenarioTxEnvData>,
RH: RHListExec<TxResponse, ScenarioTxEnvData, ListReturns = ()>,
F: FnOnce(
TxBaseWithEnv<ScenarioTxEnvData>,
)
-> Tx<ScenarioTxEnvData, From, To, Payment, Gas, FunctionCall<StaticApi>, 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
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit b9bd798

Please sign in to comment.