Skip to content

Commit

Permalink
unified syntax - tx hash refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Sep 20, 2024
1 parent 69d09a5 commit b8853bc
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 18 deletions.
3 changes: 3 additions & 0 deletions framework/base/src/types/interaction/tx_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ pub trait TxEnvMockDeployAddress: TxEnv {

pub trait TxEnvWithTxHash: TxEnv {
fn set_tx_hash(&mut self, tx_hash: H256);

/// Retrieves current tx hash, while resetting it in self.
fn take_tx_hash(&mut self) -> Option<H256>;
}
9 changes: 5 additions & 4 deletions framework/scenario/src/facade/world_tx/scenario_exec_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ where

fn run(self) -> Self::Returns {
let mut step_wrapper = self.tx_to_step();
step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash);
step_wrapper.env.world.sc_call(&mut step_wrapper.step);
step_wrapper.process_result()
}
Expand All @@ -111,16 +110,18 @@ where

fn run(self) -> Self::Returns {
let mut step_wrapper = self.tx_to_step();
step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash);
step_wrapper.env.world.sc_call(&mut step_wrapper.step);
step_wrapper.process_result()
}
}

impl<'w> TxEnvWithTxHash for ScenarioEnvExec<'w> {
fn set_tx_hash(&mut self, tx_hash: H256) {
assert!(self.data.tx_hash.is_none(), "tx hash set twice");
self.data.tx_hash = Some(tx_hash);
self.data.set_tx_hash(tx_hash);
}

fn take_tx_hash(&mut self) -> Option<H256> {
self.data.take_tx_hash()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ where

fn run(self) -> Self::Returns {
let mut step_wrapper = self.tx_to_step();
step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash);
step_wrapper.env.world.sc_deploy(&mut step_wrapper.step);
step_wrapper.process_result()
}
Expand Down
13 changes: 12 additions & 1 deletion framework/scenario/src/facade/world_tx/scenario_tx_env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_chain_scenario_format::interpret_trait::InterpreterContext;
use multiversx_sc::types::{ManagedAddress, ManagedBuffer, TxEnv, H256};
use multiversx_sc::types::{ManagedAddress, ManagedBuffer, TxEnv, TxEnvWithTxHash, H256};

use crate::{api::StaticApi, scenario_model::TxExpect, ScenarioWorld};

Expand Down Expand Up @@ -33,6 +33,17 @@ impl TxEnv for ScenarioTxEnvData {
}
}

impl TxEnvWithTxHash for ScenarioTxEnvData {
fn set_tx_hash(&mut self, tx_hash: H256) {
assert!(self.tx_hash.is_none(), "tx hash set twice");
self.tx_hash = Some(tx_hash);
}

fn take_tx_hash(&mut self) -> Option<H256> {
core::mem::take(&mut self.tx_hash)
}
}

impl ScenarioTxEnvData {
pub fn interpreter_context(&self) -> InterpreterContext {
self.interpreter_context.clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ where
let contract_obj = contract_obj_builder();

let mut step_wrapper = self.tx_to_step();
step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash);
let (new_address, tx_result) = step_wrapper
.env
.world
Expand Down Expand Up @@ -123,7 +122,6 @@ where
let contract_obj = contract_obj_builder();

let mut step_wrapper = self.tx_to_step();
step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash);

// no endpoint is called per se, but if it is empty, the VM thinks it is a simple transfer of value
if step_wrapper.step.tx.function.is_empty() {
Expand Down
5 changes: 4 additions & 1 deletion framework/scenario/src/scenario/model/step/sc_call_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ impl ScCallStep {
expect.update_from_response(&tx_response)
}
}
tx_response.tx_hash = self.explicit_tx_hash.as_ref().map(|vm_hash| vm_hash.as_array().into());
tx_response.tx_hash = self
.explicit_tx_hash
.as_ref()
.map(|vm_hash| vm_hash.as_array().into());
self.response = Some(tx_response);
}
}
Expand Down
5 changes: 4 additions & 1 deletion framework/scenario/src/scenario/model/step/sc_deploy_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ impl ScDeployStep {
expect.update_from_response(&tx_response)
}
}
tx_response.tx_hash = self.explicit_tx_hash.as_ref().map(|vm_hash| vm_hash.as_array().into());
tx_response.tx_hash = self
.explicit_tx_hash
.as_ref()
.map(|vm_hash| vm_hash.as_array().into());
self.response = Some(tx_response);
}
}
Expand Down
9 changes: 5 additions & 4 deletions framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use multiversx_sc::types::{
Code, FunctionCall, NotPayable, RHListExec, Tx, TxEnv, TxFromSpecified, TxGas, TxPayment,
TxToSpecified, UpgradeCall,
Code, FunctionCall, NotPayable, RHListExec, Tx, TxEnv, TxEnvWithTxHash, TxFromSpecified, TxGas,
TxPayment, TxToSpecified, UpgradeCall,
};

use crate::{
Expand All @@ -14,7 +14,7 @@ use super::{address_annotated, gas_annotated, StepWrapper, TxToStep};
impl<Env, From, To, Payment, Gas, RH> TxToStep<Env, RH>
for Tx<Env, From, To, Payment, Gas, FunctionCall<Env::Api>, RH>
where
Env: TxEnv<RHExpect = TxExpect>,
Env: TxEnvWithTxHash<RHExpect = TxExpect>,
From: TxFromSpecified<Env>,
To: TxToSpecified<Env>,
Payment: TxPayment<Env>,
Expand All @@ -23,7 +23,7 @@ where
{
type Step = ScCallStep;

fn tx_to_step(self) -> StepWrapper<Env, Self::Step, RH> {
fn tx_to_step(mut self) -> StepWrapper<Env, Self::Step, RH> {
let mut step = tx_to_sc_call_step(
&self.env,
self.from,
Expand All @@ -32,6 +32,7 @@ where
self.gas,
self.data,
);
step.explicit_tx_hash = self.env.take_tx_hash();
step.expect = Some(self.result_handler.list_tx_expect());

StepWrapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use multiversx_sc::types::{
Code, DeployCall, RHListExec, Tx, TxCodeValue, TxEnv, TxFromSpecified, TxGas, TxPayment,
Code, DeployCall, RHListExec, Tx, TxCodeValue, TxEnv, TxEnvWithTxHash, TxFromSpecified, TxGas,
TxPayment,
};

use crate::scenario_model::{ScDeployStep, TxExpect, TxResponse};
Expand All @@ -9,7 +10,7 @@ use super::{address_annotated, code_annotated, gas_annotated, StepWrapper, TxToS
impl<Env, From, Payment, Gas, CodeValue, RH> TxToStep<Env, RH>
for Tx<Env, From, (), Payment, Gas, DeployCall<Env, Code<CodeValue>>, RH>
where
Env: TxEnv<RHExpect = TxExpect>,
Env: TxEnvWithTxHash<RHExpect = TxExpect>,
From: TxFromSpecified<Env>,
Payment: TxPayment<Env>,
Gas: TxGas<Env>,
Expand All @@ -18,9 +19,10 @@ where
{
type Step = ScDeployStep;

fn tx_to_step(self) -> StepWrapper<Env, Self::Step, RH> {
fn tx_to_step(mut self) -> StepWrapper<Env, Self::Step, RH> {
let mut step =
tx_to_sc_deploy_step(&self.env, self.from, self.payment, self.gas, self.data);
step.explicit_tx_hash = self.env.take_tx_hash();
step.expect = Some(self.result_handler.list_tx_expect());

StepWrapper {
Expand Down
14 changes: 13 additions & 1 deletion framework/snippets/src/interactor_tx/interactor_exec_env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use multiversx_sc_scenario::{
api::StaticApi,
multiversx_sc::types::{ManagedAddress, ManagedBuffer, Tx, TxBaseWithEnv, TxEnv},
multiversx_sc::types::{
ManagedAddress, ManagedBuffer, Tx, TxBaseWithEnv, TxEnv, TxEnvWithTxHash, H256,
},
scenario_model::TxExpect,
ScenarioTxEnv, ScenarioTxEnvData,
};
Expand Down Expand Up @@ -44,3 +46,13 @@ impl<'w> ScenarioTxEnv for InteractorEnvExec<'w> {
&self.data
}
}

impl<'w> TxEnvWithTxHash for InteractorEnvExec<'w> {
fn set_tx_hash(&mut self, tx_hash: H256) {
self.data.set_tx_hash(tx_hash);
}

fn take_tx_hash(&mut self) -> Option<H256> {
self.data.take_tx_hash()
}
}

0 comments on commit b8853bc

Please sign in to comment.