From 8b905afcfaf102dd8cb5c11241139b86ab68f909 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 3 Apr 2024 00:52:43 +0300 Subject: [PATCH 1/3] unified syntax - interactor refactor --- framework/snippets/src/interactor_tx.rs | 17 +++ .../src/interactor_tx/interactor_exec_call.rs | 76 +++++++++++ .../interactor_exec_deploy.rs} | 52 +++----- .../src/interactor_tx/interactor_exec_env.rs | 46 +++++++ .../src/interactor_tx/interactor_exec_step.rs | 15 +++ .../interactor_tx/interactor_exec_transf.rs | 34 +++++ .../interactor_prepare_async.rs} | 9 +- .../interactor_tx/interactor_query_call.rs | 65 ++++++++++ .../src/interactor_tx/interactor_query_env.rs | 45 +++++++ .../interactor_tx/interactor_query_step.rs | 15 +++ framework/snippets/src/itx.rs | 13 -- .../snippets/src/itx/interactor_env_exec.rs | 121 ------------------ .../snippets/src/itx/interactor_env_query.rs | 116 ----------------- .../snippets/src/itx/interactor_env_transf.rs | 51 -------- framework/snippets/src/lib.rs | 4 +- 15 files changed, 336 insertions(+), 343 deletions(-) create mode 100644 framework/snippets/src/interactor_tx.rs create mode 100644 framework/snippets/src/interactor_tx/interactor_exec_call.rs rename framework/snippets/src/{itx/interactor_env_deploy.rs => interactor_tx/interactor_exec_deploy.rs} (64%) create mode 100644 framework/snippets/src/interactor_tx/interactor_exec_env.rs create mode 100644 framework/snippets/src/interactor_tx/interactor_exec_step.rs create mode 100644 framework/snippets/src/interactor_tx/interactor_exec_transf.rs rename framework/snippets/src/{itx/interactor_env.rs => interactor_tx/interactor_prepare_async.rs} (56%) create mode 100644 framework/snippets/src/interactor_tx/interactor_query_call.rs create mode 100644 framework/snippets/src/interactor_tx/interactor_query_env.rs create mode 100644 framework/snippets/src/interactor_tx/interactor_query_step.rs delete mode 100644 framework/snippets/src/itx.rs delete mode 100644 framework/snippets/src/itx/interactor_env_exec.rs delete mode 100644 framework/snippets/src/itx/interactor_env_query.rs delete mode 100644 framework/snippets/src/itx/interactor_env_transf.rs diff --git a/framework/snippets/src/interactor_tx.rs b/framework/snippets/src/interactor_tx.rs new file mode 100644 index 0000000000..2c8bed0fda --- /dev/null +++ b/framework/snippets/src/interactor_tx.rs @@ -0,0 +1,17 @@ +#![allow(unused_imports)] // TEMP + +mod interactor_exec_call; +mod interactor_exec_deploy; +mod interactor_exec_env; +mod interactor_exec_step; +mod interactor_exec_transf; +mod interactor_prepare_async; +mod interactor_query_call; +mod interactor_query_env; +mod interactor_query_step; + +pub use interactor_exec_env::InteractorExecEnv; +pub use interactor_exec_step::InteractorExecStep; +pub use interactor_prepare_async::InteractorPrepareAsync; +pub use interactor_query_env::InteractorQueryEnv; +pub use interactor_query_step::InteractorQueryStep; diff --git a/framework/snippets/src/interactor_tx/interactor_exec_call.rs b/framework/snippets/src/interactor_tx/interactor_exec_call.rs new file mode 100644 index 0000000000..71ff47c043 --- /dev/null +++ b/framework/snippets/src/interactor_tx/interactor_exec_call.rs @@ -0,0 +1,76 @@ +use multiversx_sc_scenario::{ + api::StaticApi, + multiversx_sc::{ + tuple_util::NestedTupleFlatten, + types::{ + FunctionCall, RHListExec, Tx, TxBaseWithEnv, TxFromSpecified, TxGas, TxPayment, + TxToSpecified, + }, + }, + scenario::tx_to_step::TxToStep, + scenario_model::{ScCallStep, TxResponse}, + ScenarioTxEnvData, +}; + +use crate::Interactor; + +use super::{InteractorExecEnv, InteractorExecStep, InteractorPrepareAsync}; + +impl<'w, From, To, Payment, Gas, RH> InteractorPrepareAsync + for Tx, From, To, Payment, Gas, FunctionCall, RH> +where + From: TxFromSpecified>, + To: TxToSpecified>, + Payment: TxPayment>, + Gas: TxGas>, + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + type Exec = InteractorExecStep<'w, ScCallStep, RH>; + + fn prepare_async(self) -> Self::Exec { + InteractorExecStep { + step_wrapper: self.tx_to_step(), + } + } +} + +impl<'w, RH> InteractorExecStep<'w, ScCallStep, RH> +where + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + pub async fn run(mut self) -> ::Unpacked { + self.step_wrapper + .env + .world + .sc_call(&mut self.step_wrapper.step) + .await; + self.step_wrapper.process_result() + } +} + +impl Interactor { + pub async 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).await; + step_wrapper.process_result(); + + self + } +} diff --git a/framework/snippets/src/itx/interactor_env_deploy.rs b/framework/snippets/src/interactor_tx/interactor_exec_deploy.rs similarity index 64% rename from framework/snippets/src/itx/interactor_env_deploy.rs rename to framework/snippets/src/interactor_tx/interactor_exec_deploy.rs index bf5e8e61bc..30e30a34ab 100644 --- a/framework/snippets/src/itx/interactor_env_deploy.rs +++ b/framework/snippets/src/interactor_tx/interactor_exec_deploy.rs @@ -1,62 +1,50 @@ -use std::path::PathBuf; - use multiversx_sc_scenario::{ - api::StaticApi, 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, }, }, - scenario::tx_to_step::{StepWrapper, TxToStep}, - scenario_model::{AddressValue, BytesValue, ScCallStep, ScDeployStep, TxResponse}, - ScenarioEnvExec, ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun, ScenarioWorld, + scenario::tx_to_step::TxToStep, + scenario_model::{ScDeployStep, TxResponse}, + ScenarioTxEnvData, }; -use crate::{Interactor, InteractorPrepareAsync}; - -use super::InteractorEnvExec; +use crate::Interactor; -pub struct InteractorDeployStep<'w, RH> -where - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - step_wrapper: StepWrapper, ScDeployStep, RH>, -} +use super::{InteractorExecEnv, InteractorExecStep, InteractorPrepareAsync}; impl<'w, From, Payment, Gas, CodeValue, RH> InteractorPrepareAsync for Tx< - InteractorEnvExec<'w>, + InteractorExecEnv<'w>, From, (), Payment, Gas, - DeployCall, Code>, + DeployCall, Code>, RH, > where - From: TxFromSpecified>, - Payment: TxPayment>, - Gas: TxGas>, - CodeValue: TxCodeValue>, - RH: RHListExec>, + From: TxFromSpecified>, + Payment: TxPayment>, + Gas: TxGas>, + CodeValue: TxCodeValue>, + RH: RHListExec>, RH::ListReturns: NestedTupleFlatten, { - type Exec = InteractorDeployStep<'w, RH>; + type Exec = InteractorExecStep<'w, ScDeployStep, RH>; fn prepare_async(self) -> Self::Exec { - InteractorDeployStep { + InteractorExecStep { step_wrapper: self.tx_to_step(), } } } -impl<'w, RH> InteractorDeployStep<'w, RH> +impl<'w, RH> InteractorExecStep<'w, ScDeployStep, RH> where - RH: RHListExec>, + RH: RHListExec>, RH::ListReturns: NestedTupleFlatten, { pub async fn run(mut self) -> ::Unpacked { @@ -94,7 +82,7 @@ impl Interactor { let tx = f(tx_base); let mut step_wrapper = tx.tx_to_step(); - self.sc_deploy(&mut step_wrapper.step); + self.sc_deploy(&mut step_wrapper.step).await; step_wrapper.process_result(); self @@ -128,7 +116,7 @@ impl Interactor { let tx = f(tx_base); let mut step_wrapper = tx.tx_to_step(); - self.sc_deploy(&mut step_wrapper.step); + self.sc_deploy(&mut step_wrapper.step).await; step_wrapper.process_result() } } diff --git a/framework/snippets/src/interactor_tx/interactor_exec_env.rs b/framework/snippets/src/interactor_tx/interactor_exec_env.rs new file mode 100644 index 0000000000..935164f7eb --- /dev/null +++ b/framework/snippets/src/interactor_tx/interactor_exec_env.rs @@ -0,0 +1,46 @@ +use multiversx_sc_scenario::{ + api::StaticApi, + multiversx_sc::types::{ManagedAddress, ManagedBuffer, Tx, TxBaseWithEnv, TxEnv}, + scenario_model::TxExpect, + ScenarioTxEnv, ScenarioTxEnvData, +}; + +use crate::Interactor; + +impl Interactor { + pub fn tx(&mut self) -> TxBaseWithEnv> { + let data = self.new_env_data(); + let env = InteractorExecEnv { world: self, data }; + Tx::new_with_env(env) + } +} + +/// Environment for executing transactions. +pub struct InteractorExecEnv<'w> { + pub world: &'w mut Interactor, + pub data: ScenarioTxEnvData, +} + +impl<'w> TxEnv for InteractorExecEnv<'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 InteractorExecEnv<'w> { + fn env_data(&self) -> &ScenarioTxEnvData { + &self.data + } +} diff --git a/framework/snippets/src/interactor_tx/interactor_exec_step.rs b/framework/snippets/src/interactor_tx/interactor_exec_step.rs new file mode 100644 index 0000000000..3becfe81d9 --- /dev/null +++ b/framework/snippets/src/interactor_tx/interactor_exec_step.rs @@ -0,0 +1,15 @@ +use multiversx_sc_scenario::{ + multiversx_sc::{tuple_util::NestedTupleFlatten, types::RHListExec}, + scenario::tx_to_step::StepWrapper, + scenario_model::TxResponse, +}; + +use super::InteractorExecEnv; + +pub struct InteractorExecStep<'w, Step, RH> +where + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + pub(crate) step_wrapper: StepWrapper, Step, RH>, +} diff --git a/framework/snippets/src/interactor_tx/interactor_exec_transf.rs b/framework/snippets/src/interactor_tx/interactor_exec_transf.rs new file mode 100644 index 0000000000..a19d54548d --- /dev/null +++ b/framework/snippets/src/interactor_tx/interactor_exec_transf.rs @@ -0,0 +1,34 @@ +use multiversx_sc_scenario::{ + multiversx_sc::types::{Tx, TxFromSpecified, TxGas, TxPayment, TxToSpecified}, + scenario::tx_to_step::TxToStep, + scenario_model::TransferStep, +}; + +use super::{InteractorExecEnv, InteractorExecStep, InteractorPrepareAsync}; + +impl<'w, From, To, Payment, Gas> InteractorPrepareAsync + for Tx, From, To, Payment, Gas, (), ()> +where + From: TxFromSpecified>, + To: TxToSpecified>, + Payment: TxPayment>, + Gas: TxGas>, +{ + type Exec = InteractorExecStep<'w, TransferStep, ()>; + + fn prepare_async(self) -> Self::Exec { + InteractorExecStep { + step_wrapper: self.tx_to_step(), + } + } +} + +impl<'w> InteractorExecStep<'w, TransferStep, ()> { + pub async fn run(self) { + self.step_wrapper + .env + .world + .transfer(self.step_wrapper.step) + .await; + } +} diff --git a/framework/snippets/src/itx/interactor_env.rs b/framework/snippets/src/interactor_tx/interactor_prepare_async.rs similarity index 56% rename from framework/snippets/src/itx/interactor_env.rs rename to framework/snippets/src/interactor_tx/interactor_prepare_async.rs index 521389a713..167c7d9f27 100644 --- a/framework/snippets/src/itx/interactor_env.rs +++ b/framework/snippets/src/interactor_tx/interactor_prepare_async.rs @@ -1,11 +1,4 @@ -use std::path::PathBuf; - -use multiversx_sc_scenario::{ - api::StaticApi, - multiversx_sc::types::{AnnotatedValue, ManagedAddress, TxBaseWithEnv, TxEnv}, - scenario_model::TxResponse, - ScenarioTxEnvData, ScenarioWorld, -}; +use multiversx_sc_scenario::ScenarioTxEnvData; use crate::Interactor; diff --git a/framework/snippets/src/interactor_tx/interactor_query_call.rs b/framework/snippets/src/interactor_tx/interactor_query_call.rs new file mode 100644 index 0000000000..55421743c5 --- /dev/null +++ b/framework/snippets/src/interactor_tx/interactor_query_call.rs @@ -0,0 +1,65 @@ +use multiversx_sc_scenario::{ + api::StaticApi, + multiversx_sc::{ + tuple_util::NestedTupleFlatten, + types::{FunctionCall, RHListExec, Tx, TxBaseWithEnv, TxToSpecified}, + }, + scenario::tx_to_step::TxToQueryStep, + scenario_model::TxResponse, + ScenarioTxEnvData, +}; + +use crate::Interactor; + +use super::{InteractorPrepareAsync, InteractorQueryEnv, InteractorQueryStep}; + +impl<'w, To, RH> InteractorPrepareAsync + for Tx, (), To, (), (), FunctionCall, RH> +where + To: TxToSpecified>, + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + type Exec = InteractorQueryStep<'w, RH>; + + fn prepare_async(self) -> Self::Exec { + InteractorQueryStep { + step_wrapper: self.tx_to_query_step(), + } + } +} + +impl<'w, RH> InteractorQueryStep<'w, RH> +where + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + pub async fn run(mut self) -> ::Unpacked { + self.step_wrapper + .env + .world + .sc_query(&mut self.step_wrapper.step) + .await; + self.step_wrapper.process_result() + } +} + +impl Interactor { + pub async 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).await; + step_wrapper.process_result(); + self + } +} diff --git a/framework/snippets/src/interactor_tx/interactor_query_env.rs b/framework/snippets/src/interactor_tx/interactor_query_env.rs new file mode 100644 index 0000000000..1fa1142111 --- /dev/null +++ b/framework/snippets/src/interactor_tx/interactor_query_env.rs @@ -0,0 +1,45 @@ +use multiversx_sc_scenario::{ + api::StaticApi, + multiversx_sc::types::{ManagedAddress, ManagedBuffer, Tx, TxBaseWithEnv, TxEnv}, + scenario_model::TxExpect, + ScenarioTxEnv, ScenarioTxEnvData, +}; + +use crate::Interactor; + +impl Interactor { + pub fn query(&mut self) -> TxBaseWithEnv> { + let data = self.new_env_data(); + let env = InteractorQueryEnv { world: self, data }; + Tx::new_with_env(env) + } +} + +pub struct InteractorQueryEnv<'w> { + pub world: &'w mut Interactor, + pub data: ScenarioTxEnvData, +} + +impl<'w> TxEnv for InteractorQueryEnv<'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 InteractorQueryEnv<'w> { + fn env_data(&self) -> &ScenarioTxEnvData { + &self.data + } +} diff --git a/framework/snippets/src/interactor_tx/interactor_query_step.rs b/framework/snippets/src/interactor_tx/interactor_query_step.rs new file mode 100644 index 0000000000..81c7542add --- /dev/null +++ b/framework/snippets/src/interactor_tx/interactor_query_step.rs @@ -0,0 +1,15 @@ +use multiversx_sc_scenario::{ + multiversx_sc::{tuple_util::NestedTupleFlatten, types::RHListExec}, + scenario::tx_to_step::StepWrapper, + scenario_model::{ScQueryStep, TxResponse}, +}; + +use super::InteractorQueryEnv; + +pub struct InteractorQueryStep<'w, RH> +where + RH: RHListExec>, + RH::ListReturns: NestedTupleFlatten, +{ + pub(crate) step_wrapper: StepWrapper, ScQueryStep, RH>, +} diff --git a/framework/snippets/src/itx.rs b/framework/snippets/src/itx.rs deleted file mode 100644 index 8cef20e699..0000000000 --- a/framework/snippets/src/itx.rs +++ /dev/null @@ -1,13 +0,0 @@ -#![allow(unused)] // TEMP - -mod interactor_env; -mod interactor_env_deploy; -mod interactor_env_exec; -mod interactor_env_query; -mod interactor_env_transf; - -pub use interactor_env::*; -pub use interactor_env_deploy::*; -pub use interactor_env_exec::InteractorEnvExec; -pub use interactor_env_query::InteractorEnvQuery; -pub use interactor_env_transf::*; diff --git a/framework/snippets/src/itx/interactor_env_exec.rs b/framework/snippets/src/itx/interactor_env_exec.rs deleted file mode 100644 index 8e6b4dc69c..0000000000 --- a/framework/snippets/src/itx/interactor_env_exec.rs +++ /dev/null @@ -1,121 +0,0 @@ -use std::path::PathBuf; - -use multiversx_sc_scenario::{ - api::StaticApi, - multiversx_sc::{ - tuple_util::NestedTupleFlatten, - types::{ - AnnotatedValue, Code, DeployCall, FunctionCall, ManagedAddress, ManagedBuffer, - RHListExec, Tx, TxBaseWithEnv, TxCodeSource, TxCodeSourceSpecified, TxCodeValue, TxEnv, - TxFromSpecified, TxGas, TxPayment, TxToSpecified, - }, - }, - scenario::tx_to_step::{StepWrapper, TxToStep}, - scenario_model::{AddressValue, BytesValue, ScCallStep, ScDeployStep, TxExpect, TxResponse}, - ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun, ScenarioWorld, -}; - -use crate::{Interactor, InteractorPrepareAsync}; - -/// Environment for executing transactions. -pub struct InteractorEnvExec<'w> { - pub world: &'w mut Interactor, - pub data: ScenarioTxEnvData, -} - -impl<'w> TxEnv for InteractorEnvExec<'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 InteractorEnvExec<'w> { - fn env_data(&self) -> &ScenarioTxEnvData { - &self.data - } -} - -pub struct InteractorCallStep<'w, RH> -where - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - step_wrapper: StepWrapper, ScCallStep, RH>, -} - -impl<'w, From, To, Payment, Gas, RH> InteractorPrepareAsync - for Tx, From, To, Payment, Gas, FunctionCall, RH> -where - From: TxFromSpecified>, - To: TxToSpecified>, - Payment: TxPayment>, - Gas: TxGas>, - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - type Exec = InteractorCallStep<'w, RH>; - - fn prepare_async(self) -> Self::Exec { - InteractorCallStep { - step_wrapper: self.tx_to_step(), - } - } -} - -impl<'w, RH> InteractorCallStep<'w, RH> -where - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - pub async fn run(mut self) -> ::Unpacked { - self.step_wrapper - .env - .world - .sc_call(&mut self.step_wrapper.step) - .await; - self.step_wrapper.process_result() - } -} - -impl Interactor { - pub fn tx(&mut self) -> TxBaseWithEnv> { - let data = self.new_env_data(); - let env = InteractorEnvExec { world: self, data }; - Tx::new_with_env(env) - } - - pub async 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).await; - step_wrapper.process_result(); - - self - } -} diff --git a/framework/snippets/src/itx/interactor_env_query.rs b/framework/snippets/src/itx/interactor_env_query.rs deleted file mode 100644 index 881fc6b861..0000000000 --- a/framework/snippets/src/itx/interactor_env_query.rs +++ /dev/null @@ -1,116 +0,0 @@ -use std::path::PathBuf; - -use multiversx_sc_scenario::{ - api::StaticApi, - multiversx_sc::{ - tuple_util::NestedTupleFlatten, - types::{ - AnnotatedValue, FunctionCall, ManagedAddress, ManagedBuffer, RHListExec, Tx, - TxBaseWithEnv, TxEnv, TxFromSpecified, TxGas, TxPayment, TxToSpecified, - }, - }, - scenario::tx_to_step::{StepWrapper, TxToQueryStep}, - scenario_model::{ScQueryStep, TxExpect, TxResponse}, - ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun, ScenarioWorld, -}; - -use crate::{Interactor, InteractorPrepareAsync}; - -pub struct InteractorEnvQuery<'w> { - pub world: &'w mut Interactor, - pub data: ScenarioTxEnvData, -} - -impl<'w> TxEnv for InteractorEnvQuery<'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 InteractorEnvQuery<'w> { - fn env_data(&self) -> &ScenarioTxEnvData { - &self.data - } -} - -pub struct InteractorQueryStep<'w, RH> -where - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - step_wrapper: StepWrapper, ScQueryStep, RH>, -} - -impl<'w, To, RH> InteractorPrepareAsync - for Tx, (), To, (), (), FunctionCall, RH> -where - To: TxToSpecified>, - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - type Exec = InteractorQueryStep<'w, RH>; - - fn prepare_async(self) -> Self::Exec { - InteractorQueryStep { - step_wrapper: self.tx_to_query_step(), - } - } -} - -impl<'w, RH> InteractorQueryStep<'w, RH> -where - RH: RHListExec>, - RH::ListReturns: NestedTupleFlatten, -{ - pub async fn run(mut self) -> ::Unpacked { - self.step_wrapper - .env - .world - .sc_query(&mut self.step_wrapper.step) - .await; - self.step_wrapper.process_result() - } -} - -impl Interactor { - pub fn query(&mut self) -> TxBaseWithEnv> { - let data = self.new_env_data(); - let env = InteractorEnvQuery { world: self, data }; - Tx::new_with_env(env) - } - - pub async 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).await; - step_wrapper.process_result(); - - // let mut step = tx_to_sc_query_step(&tx.env, tx.to, tx.data); - // step.expect = Some(tx.result_handler.list_tx_expect()); - // self.sc_query(&mut step).await; - // process_result(step.response, tx.result_handler); - self - } -} diff --git a/framework/snippets/src/itx/interactor_env_transf.rs b/framework/snippets/src/itx/interactor_env_transf.rs deleted file mode 100644 index 33b977dbac..0000000000 --- a/framework/snippets/src/itx/interactor_env_transf.rs +++ /dev/null @@ -1,51 +0,0 @@ -use std::path::PathBuf; - -use multiversx_sc_scenario::{ - api::StaticApi, - multiversx_sc::{ - tuple_util::NestedTupleFlatten, - types::{ - AnnotatedValue, Code, DeployCall, FunctionCall, ManagedAddress, ManagedBuffer, Tx, - TxBaseWithEnv, TxCodeSource, TxCodeSourceSpecified, TxCodeValue, TxEnv, - TxFromSpecified, TxGas, TxPayment, TxToSpecified, - }, - }, - scenario::tx_to_step::{StepWrapper, TxToStep}, - scenario_model::{ - AddressValue, BytesValue, ScCallStep, ScDeployStep, TransferStep, TxResponse, - }, - ScenarioTxEnv, ScenarioTxEnvData, ScenarioTxRun, ScenarioWorld, -}; - -use crate::{Interactor, InteractorEnvExec, InteractorPrepareAsync}; - -pub struct InteractorTransferStep<'w> { - step_wrapper: StepWrapper, TransferStep, ()>, -} - -impl<'w, From, To, Payment, Gas> InteractorPrepareAsync - for Tx, From, To, Payment, Gas, (), ()> -where - From: TxFromSpecified>, - To: TxToSpecified>, - Payment: TxPayment>, - Gas: TxGas>, -{ - type Exec = InteractorTransferStep<'w>; - - fn prepare_async(self) -> Self::Exec { - InteractorTransferStep { - step_wrapper: self.tx_to_step(), - } - } -} - -impl<'w> InteractorTransferStep<'w> { - pub async fn run(mut self) { - self.step_wrapper - .env - .world - .transfer(self.step_wrapper.step) - .await; - } -} diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index 9bf2c07e33..1a30aaa23a 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -8,9 +8,9 @@ mod interactor_sc_deploy; mod interactor_sc_extra; mod interactor_sc_transfer; mod interactor_sender; +mod interactor_tx; mod interactor_tx_spec; mod interactor_vm_query; -mod itx; mod step_buffer; pub use env_logger; @@ -18,8 +18,8 @@ pub use hex; pub use interactor::*; pub use interactor_dns::*; pub use interactor_sender::*; +pub use interactor_tx::*; pub use interactor_tx_spec::*; -pub use itx::*; pub use log; pub use multiversx_sc_scenario::{self, multiversx_sc}; pub use multiversx_sdk as erdrs; // TODO: remove From 00b390f08533568f44e45f9c5ba83dfbd0e95308 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 3 Apr 2024 01:41:11 +0300 Subject: [PATCH 2/3] interactor refactor --- framework/snippets/src/interactor_multi_sc_exec.rs | 8 ++++---- .../{interactor_tx_spec.rs => interactor_step.rs} | 13 +++++++------ framework/snippets/src/lib.rs | 4 ++-- framework/snippets/src/step_buffer.rs | 6 +++--- 4 files changed, 16 insertions(+), 15 deletions(-) rename framework/snippets/src/{interactor_tx_spec.rs => interactor_step.rs} (79%) diff --git a/framework/snippets/src/interactor_multi_sc_exec.rs b/framework/snippets/src/interactor_multi_sc_exec.rs index b8035fbae6..08c4f9c6cd 100644 --- a/framework/snippets/src/interactor_multi_sc_exec.rs +++ b/framework/snippets/src/interactor_multi_sc_exec.rs @@ -1,6 +1,6 @@ use crate::{ interactor_multi_sc_process::{update_nonces_and_sign_tx, SenderSet, Txs}, - Interactor, StepBuffer, TransactionSpec, + Interactor, InteractorStep, StepBuffer, }; use multiversx_sc_scenario::scenario_model::TxResponse; @@ -32,7 +32,7 @@ impl Interactor { for sc_call_step in &mut buffer.refs { let mut transaction = sc_call_step.to_transaction(self); - let sender_address = &sc_call_step.to_address().value; + let sender_address = &sc_call_step.sender_address().value; let sender = self .sender_map .get_mut(sender_address) @@ -45,11 +45,11 @@ impl Interactor { } } -fn retrieve_senders(sc_call_steps: &[&mut dyn TransactionSpec]) -> SenderSet { +fn retrieve_senders(sc_call_steps: &[&mut dyn InteractorStep]) -> SenderSet { let mut senders = SenderSet::new(); for sc_call_step in sc_call_steps { - let sender_address = &sc_call_step.to_address().value; + let sender_address = &sc_call_step.sender_address().value; senders.insert(sender_address.clone()); } senders diff --git a/framework/snippets/src/interactor_tx_spec.rs b/framework/snippets/src/interactor_step.rs similarity index 79% rename from framework/snippets/src/interactor_tx_spec.rs rename to framework/snippets/src/interactor_step.rs index 7463d37c5e..be172bbb82 100644 --- a/framework/snippets/src/interactor_tx_spec.rs +++ b/framework/snippets/src/interactor_step.rs @@ -6,22 +6,23 @@ use multiversx_sdk::data::transaction::Transaction; use crate::Interactor; -pub trait TransactionSpec { +/// Describes a scenario step that can be executed in an interactor. +pub trait InteractorStep { fn to_transaction(&self, interactor: &Interactor) -> Transaction; - fn to_address(&self) -> &AddressValue; + fn sender_address(&self) -> &AddressValue; fn run_step(&mut self, step_runner: &mut dyn ScenarioRunner); fn set_response(&mut self, tx_response: TxResponse); } -impl TransactionSpec for ScCallStep { +impl InteractorStep for ScCallStep { fn to_transaction(&self, interactor: &Interactor) -> Transaction { interactor.tx_call_to_blockchain_tx(&self.tx) } - fn to_address(&self) -> &AddressValue { + fn sender_address(&self) -> &AddressValue { &self.tx.from } @@ -35,12 +36,12 @@ impl TransactionSpec for ScCallStep { } } -impl TransactionSpec for ScDeployStep { +impl InteractorStep for ScDeployStep { fn to_transaction(&self, interactor: &Interactor) -> Transaction { interactor.sc_deploy_to_blockchain_tx(self) } - fn to_address(&self) -> &AddressValue { + fn sender_address(&self) -> &AddressValue { &self.tx.from } diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index 1a30aaa23a..e05bbf5f01 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -8,8 +8,8 @@ mod interactor_sc_deploy; mod interactor_sc_extra; mod interactor_sc_transfer; mod interactor_sender; +mod interactor_step; mod interactor_tx; -mod interactor_tx_spec; mod interactor_vm_query; mod step_buffer; @@ -18,8 +18,8 @@ pub use hex; pub use interactor::*; pub use interactor_dns::*; pub use interactor_sender::*; +pub use interactor_step::InteractorStep; pub use interactor_tx::*; -pub use interactor_tx_spec::*; pub use log; pub use multiversx_sc_scenario::{self, multiversx_sc}; pub use multiversx_sdk as erdrs; // TODO: remove diff --git a/framework/snippets/src/step_buffer.rs b/framework/snippets/src/step_buffer.rs index f4956ff3a0..529b790ff5 100644 --- a/framework/snippets/src/step_buffer.rs +++ b/framework/snippets/src/step_buffer.rs @@ -1,10 +1,10 @@ use multiversx_sc_scenario::scenario_model::{ScCallStep, ScDeployStep}; -use crate::TransactionSpec; +use crate::InteractorStep; #[derive(Default)] pub struct StepBuffer<'a> { - pub refs: Vec<&'a mut dyn TransactionSpec>, + pub refs: Vec<&'a mut dyn InteractorStep>, } impl<'a> StepBuffer<'a> { @@ -54,7 +54,7 @@ impl<'a> StepBuffer<'a> { buffer } - pub fn to_refs_vec(&'a self) -> Vec<&'a dyn TransactionSpec> { + pub fn to_refs_vec(&'a self) -> Vec<&'a dyn InteractorStep> { self.refs.iter().map(|r| &**r).collect() } } From 43934fd9ec5810ff2431351875ac193e2811ca07 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 3 Apr 2024 01:58:34 +0300 Subject: [PATCH 3/3] interactor refactor --- framework/snippets/src/interactor_scenario.rs | 5 +++++ .../interactor_sc_call.rs | 0 .../interactor_sc_deploy.rs | 0 .../interactor_sc_extra.rs | 0 .../interactor_transfer.rs} | 0 .../interactor_vm_query.rs | 0 framework/snippets/src/lib.rs | 14 +++----------- framework/snippets/src/multi.rs | 7 +++++++ .../src/{ => multi}/interactor_multi_sc_exec.rs | 7 ++----- .../src/{ => multi}/interactor_multi_sc_process.rs | 0 .../snippets/src/{ => multi}/interactor_step.rs | 0 framework/snippets/src/{ => multi}/step_buffer.rs | 0 12 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 framework/snippets/src/interactor_scenario.rs rename framework/snippets/src/{ => interactor_scenario}/interactor_sc_call.rs (100%) rename framework/snippets/src/{ => interactor_scenario}/interactor_sc_deploy.rs (100%) rename framework/snippets/src/{ => interactor_scenario}/interactor_sc_extra.rs (100%) rename framework/snippets/src/{interactor_sc_transfer.rs => interactor_scenario/interactor_transfer.rs} (100%) rename framework/snippets/src/{ => interactor_scenario}/interactor_vm_query.rs (100%) create mode 100644 framework/snippets/src/multi.rs rename framework/snippets/src/{ => multi}/interactor_multi_sc_exec.rs (92%) rename framework/snippets/src/{ => multi}/interactor_multi_sc_process.rs (100%) rename framework/snippets/src/{ => multi}/interactor_step.rs (100%) rename framework/snippets/src/{ => multi}/step_buffer.rs (100%) diff --git a/framework/snippets/src/interactor_scenario.rs b/framework/snippets/src/interactor_scenario.rs new file mode 100644 index 0000000000..8449bf9edc --- /dev/null +++ b/framework/snippets/src/interactor_scenario.rs @@ -0,0 +1,5 @@ +mod interactor_sc_call; +mod interactor_sc_deploy; +mod interactor_sc_extra; +mod interactor_transfer; +mod interactor_vm_query; diff --git a/framework/snippets/src/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs similarity index 100% rename from framework/snippets/src/interactor_sc_call.rs rename to framework/snippets/src/interactor_scenario/interactor_sc_call.rs diff --git a/framework/snippets/src/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs similarity index 100% rename from framework/snippets/src/interactor_sc_deploy.rs rename to framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs diff --git a/framework/snippets/src/interactor_sc_extra.rs b/framework/snippets/src/interactor_scenario/interactor_sc_extra.rs similarity index 100% rename from framework/snippets/src/interactor_sc_extra.rs rename to framework/snippets/src/interactor_scenario/interactor_sc_extra.rs diff --git a/framework/snippets/src/interactor_sc_transfer.rs b/framework/snippets/src/interactor_scenario/interactor_transfer.rs similarity index 100% rename from framework/snippets/src/interactor_sc_transfer.rs rename to framework/snippets/src/interactor_scenario/interactor_transfer.rs diff --git a/framework/snippets/src/interactor_vm_query.rs b/framework/snippets/src/interactor_scenario/interactor_vm_query.rs similarity index 100% rename from framework/snippets/src/interactor_vm_query.rs rename to framework/snippets/src/interactor_scenario/interactor_vm_query.rs diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index e05bbf5f01..70f3f8e792 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -1,28 +1,20 @@ mod interactor; mod interactor_dns; -mod interactor_multi_sc_exec; -mod interactor_multi_sc_process; mod interactor_retrieve; -mod interactor_sc_call; -mod interactor_sc_deploy; -mod interactor_sc_extra; -mod interactor_sc_transfer; +mod interactor_scenario; mod interactor_sender; -mod interactor_step; mod interactor_tx; -mod interactor_vm_query; -mod step_buffer; +mod multi; pub use env_logger; pub use hex; pub use interactor::*; pub use interactor_dns::*; pub use interactor_sender::*; -pub use interactor_step::InteractorStep; pub use interactor_tx::*; pub use log; +pub use multi::*; pub use multiversx_sc_scenario::{self, multiversx_sc}; pub use multiversx_sdk as erdrs; // TODO: remove pub use multiversx_sdk as sdk; -pub use step_buffer::*; pub use tokio; diff --git a/framework/snippets/src/multi.rs b/framework/snippets/src/multi.rs new file mode 100644 index 0000000000..0353b8931b --- /dev/null +++ b/framework/snippets/src/multi.rs @@ -0,0 +1,7 @@ +mod interactor_multi_sc_exec; +mod interactor_multi_sc_process; +mod interactor_step; +mod step_buffer; + +pub use interactor_step::InteractorStep; +pub use step_buffer::StepBuffer; diff --git a/framework/snippets/src/interactor_multi_sc_exec.rs b/framework/snippets/src/multi/interactor_multi_sc_exec.rs similarity index 92% rename from framework/snippets/src/interactor_multi_sc_exec.rs rename to framework/snippets/src/multi/interactor_multi_sc_exec.rs index 08c4f9c6cd..d99df9c807 100644 --- a/framework/snippets/src/interactor_multi_sc_exec.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_exec.rs @@ -1,8 +1,5 @@ -use crate::{ - interactor_multi_sc_process::{update_nonces_and_sign_tx, SenderSet, Txs}, - Interactor, InteractorStep, StepBuffer, -}; - +use super::interactor_multi_sc_process::{update_nonces_and_sign_tx, SenderSet, Txs}; +use crate::{Interactor, InteractorStep, StepBuffer}; use multiversx_sc_scenario::scenario_model::TxResponse; use multiversx_sdk::data::transaction::Transaction; diff --git a/framework/snippets/src/interactor_multi_sc_process.rs b/framework/snippets/src/multi/interactor_multi_sc_process.rs similarity index 100% rename from framework/snippets/src/interactor_multi_sc_process.rs rename to framework/snippets/src/multi/interactor_multi_sc_process.rs diff --git a/framework/snippets/src/interactor_step.rs b/framework/snippets/src/multi/interactor_step.rs similarity index 100% rename from framework/snippets/src/interactor_step.rs rename to framework/snippets/src/multi/interactor_step.rs diff --git a/framework/snippets/src/step_buffer.rs b/framework/snippets/src/multi/step_buffer.rs similarity index 100% rename from framework/snippets/src/step_buffer.rs rename to framework/snippets/src/multi/step_buffer.rs