Skip to content

Commit

Permalink
Merge pull request #1803 from multiversx/gateway-refactor-9
Browse files Browse the repository at this point in the history
Gateway refactor - StepBuffer - removed trait object
  • Loading branch information
andrei-marinica authored Oct 4, 2024
2 parents 2f7d9de + 01bb8b1 commit 16a8588
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion framework/snippets/src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ mod interactor_step;
mod step_buffer;

pub use homogenous_tx_buffer::HomogenousTxBuffer;
pub use interactor_step::InteractorStep;
pub use interactor_step::{InteractorStep, InteractorStepRef};
pub use step_buffer::StepBuffer;
6 changes: 3 additions & 3 deletions framework/snippets/src/multi/homogenous_tx_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multiversx_sc_scenario::{
tuple_util::NestedTupleFlatten,
types::{RHListExec, TxBaseWithEnv},
},
scenario::tx_to_step::{StepWithResponse, StepWrapper, TxToStep},
scenario::tx_to_step::{StepWrapper, TxToStep},
scenario_model::TxResponse,
ScenarioTxEnvData,
};
Expand Down Expand Up @@ -33,7 +33,7 @@ impl InteractorBase<GatewayHttpProxy> {

impl<'w, Step, RH> HomogenousTxBuffer<'w, Step, RH>
where
Step: InteractorStep + StepWithResponse,
Step: InteractorStep,
RH: RHListExec<TxResponse, ScenarioTxEnvData>,
RH::ListReturns: NestedTupleFlatten,
{
Expand All @@ -54,7 +54,7 @@ where
pub async fn run(mut self) -> Vec<<RH::ListReturns as NestedTupleFlatten>::Unpacked> {
let mut step_buffer = StepBuffer::default();
for step in &mut self.steps {
step_buffer.refs.push(&mut step.step);
step_buffer.refs.push(step.step.as_interactor_step());
}
self.env.world.multi_sc_exec(step_buffer).await;

Expand Down
13 changes: 8 additions & 5 deletions framework/snippets/src/multi/interactor_multi_sc_exec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use multiversx_sdk_http::GatewayHttpProxy;

use super::interactor_multi_sc_process::{update_nonces_and_sign_tx, SenderSet, Txs};
use super::InteractorStepRef;
use crate::sdk::data::transaction::Transaction;
use crate::{network_response, InteractorBase, InteractorStep, StepBuffer};
use crate::sdk::gateway::GatewayAsyncService;
use crate::{network_response, InteractorBase, StepBuffer};

impl InteractorBase<GatewayHttpProxy> {
impl<GatewayProxy> InteractorBase<GatewayProxy>
where
GatewayProxy: GatewayAsyncService,
{
pub async fn multi_sc_exec(&mut self, mut buffer: StepBuffer<'_>) {
for step in buffer.refs.iter_mut() {
step.run_step(&mut self.pre_runners);
Expand Down Expand Up @@ -45,7 +48,7 @@ impl InteractorBase<GatewayHttpProxy> {
}
}

fn retrieve_senders(sc_call_steps: &[&mut dyn InteractorStep]) -> SenderSet {
fn retrieve_senders(sc_call_steps: &[InteractorStepRef]) -> SenderSet {
let mut senders = SenderSet::new();

for sc_call_step in sc_call_steps {
Expand Down
74 changes: 42 additions & 32 deletions framework/snippets/src/multi/interactor_step.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,66 @@
use crate::sdk::data::transaction::Transaction;
use multiversx_sc_scenario::{
mandos_system::ScenarioRunner,
scenario::tx_to_step::StepWithResponse,
scenario_model::{AddressValue, ScCallStep, ScDeployStep, TxResponse},
};
use multiversx_sdk_http::GatewayHttpProxy;
use multiversx_sdk::gateway::GatewayAsyncService;

use crate::InteractorBase;

/// Describes a scenario step that can be executed in an interactor.
pub trait InteractorStep {
fn to_transaction(&self, interactor: &InteractorBase<GatewayHttpProxy>) -> Transaction;

fn sender_address(&self) -> &AddressValue;

fn run_step(&mut self, step_runner: &mut dyn ScenarioRunner);

fn set_response(&mut self, tx_response: TxResponse);
pub enum InteractorStepRef<'a> {
ScCall(&'a mut ScCallStep),
ScDeploy(&'a mut ScDeployStep),
}

impl InteractorStep for ScCallStep {
fn to_transaction(&self, interactor: &InteractorBase<GatewayHttpProxy>) -> Transaction {
interactor.tx_call_to_blockchain_tx(&self.tx)
impl<'a> InteractorStepRef<'a> {
pub fn to_transaction<GatewayProxy: GatewayAsyncService>(
&self,
interactor: &InteractorBase<GatewayProxy>,
) -> Transaction {
match self {
InteractorStepRef::ScCall(sc_call) => interactor.tx_call_to_blockchain_tx(&sc_call.tx),
InteractorStepRef::ScDeploy(sc_deploy) => {
interactor.sc_deploy_to_blockchain_tx(sc_deploy)
},
}
}

fn sender_address(&self) -> &AddressValue {
&self.tx.from
pub fn sender_address(&self) -> &AddressValue {
match self {
InteractorStepRef::ScCall(sc_call) => &sc_call.tx.from,
InteractorStepRef::ScDeploy(sc_deploy) => &sc_deploy.tx.from,
}
}

fn run_step(&mut self, step_runner: &mut dyn ScenarioRunner) {
let mut clone = self.clone();
step_runner.run_sc_call_step(&mut clone); // TODO: make mutability uniform
pub fn run_step(&mut self, step_runner: &mut dyn ScenarioRunner) {
match self {
InteractorStepRef::ScCall(sc_call) => step_runner.run_sc_call_step(sc_call),
InteractorStepRef::ScDeploy(sc_deploy) => step_runner.run_sc_deploy_step(sc_deploy),
}
}

fn set_response(&mut self, response: TxResponse) {
self.save_response(response);
pub fn set_response(&mut self, tx_response: TxResponse) {
match self {
InteractorStepRef::ScCall(sc_call) => sc_call.save_response(tx_response),
InteractorStepRef::ScDeploy(sc_deploy) => sc_deploy.save_response(tx_response),
}
}
}

impl InteractorStep for ScDeployStep {
fn to_transaction(&self, interactor: &InteractorBase<GatewayHttpProxy>) -> Transaction {
interactor.sc_deploy_to_blockchain_tx(self)
}

fn sender_address(&self) -> &AddressValue {
&self.tx.from
}
/// Describes a scenario step that can be executed in an interactor.
pub trait InteractorStep: StepWithResponse {
fn as_interactor_step(&mut self) -> InteractorStepRef<'_>;
}

fn run_step(&mut self, step_runner: &mut dyn ScenarioRunner) {
step_runner.run_sc_deploy_step(self);
impl InteractorStep for ScCallStep {
fn as_interactor_step(&mut self) -> InteractorStepRef<'_> {
InteractorStepRef::ScCall(self)
}
}

fn set_response(&mut self, response: TxResponse) {
self.save_response(response);
impl InteractorStep for ScDeployStep {
fn as_interactor_step(&mut self) -> InteractorStepRef<'_> {
InteractorStepRef::ScDeploy(self)
}
}
16 changes: 6 additions & 10 deletions framework/snippets/src/multi/step_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use multiversx_sc_scenario::scenario_model::{ScCallStep, ScDeployStep};

use crate::InteractorStep;
use super::InteractorStepRef;

#[derive(Default)]
pub struct StepBuffer<'a> {
pub refs: Vec<&'a mut dyn InteractorStep>,
pub refs: Vec<InteractorStepRef<'a>>,
}

impl<'a> StepBuffer<'a> {
Expand All @@ -17,7 +17,7 @@ impl<'a> StepBuffer<'a> {
'b: 'a,
S: AsMut<ScCallStep>,
{
self.refs.push(step.as_mut());
self.refs.push(InteractorStepRef::ScCall(step.as_mut()));
}

pub fn add_sc_call_vec<'b, S>(&'a mut self, steps: &'b mut Vec<S>)
Expand All @@ -26,7 +26,7 @@ impl<'a> StepBuffer<'a> {
S: AsMut<ScCallStep>,
{
for step in steps {
self.refs.push(step.as_mut());
self.refs.push(InteractorStepRef::ScCall(step.as_mut()));
}
}

Expand All @@ -37,7 +37,7 @@ impl<'a> StepBuffer<'a> {
{
let mut buffer = Self::default();
for step in steps {
buffer.refs.push(step.as_mut());
buffer.refs.push(InteractorStepRef::ScCall(step.as_mut()));
}
buffer
}
Expand All @@ -49,12 +49,8 @@ impl<'a> StepBuffer<'a> {
{
let mut buffer = Self::default();
for step in steps {
buffer.refs.push(step.as_mut());
buffer.refs.push(InteractorStepRef::ScDeploy(step.as_mut()));
}
buffer
}

pub fn to_refs_vec(&'a self) -> Vec<&'a dyn InteractorStep> {
self.refs.iter().map(|r| &**r).collect()
}
}

0 comments on commit 16a8588

Please sign in to comment.