From 03bc4463b165abd93aa13a491e63fa783565a807 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Tue, 4 Jun 2024 16:00:17 +0300 Subject: [PATCH] param tests - deploy unified syntax --- .../parametric-tests/adder-pt/src/adder_pt.rs | 21 +++---- framework/base/src/types/interaction/tx.rs | 23 ++++++- .../interaction/tx_data/tx_code_source.rs | 30 +++++++++ .../interaction/tx_exec/tx_exec_test_call.rs | 63 ++++++++++++++++++- 4 files changed, 120 insertions(+), 17 deletions(-) diff --git a/contracts/parametric-tests/adder-pt/src/adder_pt.rs b/contracts/parametric-tests/adder-pt/src/adder_pt.rs index 40cb87f897..7239963b4f 100644 --- a/contracts/parametric-tests/adder-pt/src/adder_pt.rs +++ b/contracts/parametric-tests/adder-pt/src/adder_pt.rs @@ -28,18 +28,15 @@ pub trait TestAdder { let adder = ManagedAddress::from(b"adder___________________________"); self.test_raw().register_new_address(&owner, 1, &adder); - // deploy the adder contract - let mut adder_init_args = ManagedArgBuffer::new(); - adder_init_args.push_arg(INIT_SUM); // initial sum - - // deploy a contract from `owner` - let adder = self.test_raw().deploy_contract( - &owner, - 5000000000000, - &BigUint::zero(), - &code_path, - &adder_init_args, - ); + let adder = self + .tx() + .from(&owner) + .typed(adder_proxy::AdderProxy) + .init(INIT_SUM) + .code_path(code_path) + .gas(5000000000000) + .returns(ReturnsNewManagedAddress) + .test_deploy(); // save the deployed contract's address self.adder_address().set(&adder); diff --git a/framework/base/src/types/interaction/tx.rs b/framework/base/src/types/interaction/tx.rs index 905b9af8fc..db0fcb9c5b 100644 --- a/framework/base/src/types/interaction/tx.rs +++ b/framework/base/src/types/interaction/tx.rs @@ -11,10 +11,10 @@ use crate::{ use multiversx_sc_codec::TopEncodeMulti; use super::{ - AnnotatedValue, Code, ContractCallBase, ContractCallNoPayment, ContractCallWithEgld, + AnnotatedValue, Code, CodePath, ContractCallBase, ContractCallNoPayment, ContractCallWithEgld, ContractDeploy, DeployCall, Egld, EgldPayment, ExplicitGas, FromSource, FunctionCall, ManagedArgBuffer, OriginalResultMarker, RHList, RHListAppendNoRet, RHListAppendRet, RHListItem, - TxCodeSource, TxCodeValue, TxData, TxDataFunctionCall, TxEgldValue, TxEnv, + TxCodePathValue, TxCodeSource, TxCodeValue, TxData, TxDataFunctionCall, TxEgldValue, TxEnv, TxEnvMockDeployAddress, TxEnvWithTxHash, TxFrom, TxFromSourceValue, TxFromSpecified, TxGas, TxGasValue, TxPayment, TxPaymentEgldOnly, TxProxyTrait, TxResultHandler, TxScEnv, TxTo, TxToSpecified, UpgradeCall, UNSPECIFIED_GAS_LIMIT, @@ -784,6 +784,25 @@ where result_handler: self.result_handler, } } + + /// Sets the path to the code file. Only works in parametric tests. + pub fn code_path( + self, + code: CodePathValue, + ) -> Tx>, RH> + where + CodePathValue: TxCodePathValue, + { + Tx { + env: self.env, + from: self.from, + to: self.to, + payment: self.payment, + gas: self.gas, + data: self.data.code_source(CodePath(code)), + result_handler: self.result_handler, + } + } } impl diff --git a/framework/base/src/types/interaction/tx_data/tx_code_source.rs b/framework/base/src/types/interaction/tx_data/tx_code_source.rs index 0d3c180e16..0b8e23fb48 100644 --- a/framework/base/src/types/interaction/tx_data/tx_code_source.rs +++ b/framework/base/src/types/interaction/tx_data/tx_code_source.rs @@ -73,3 +73,33 @@ where FromSourceValue: TxFromSourceValue, { } + +#[diagnostic::on_unimplemented( + message = "Type `{Self}` cannot be used as code path (does not implement `TxCodePathValue<{Env}>`)", + label = "not a valid smart contract code path", + note = "there are multiple ways to specify SC code path, but `{Self}` is not one of them" +)] +pub trait TxCodePathValue: AnnotatedValue> +where + Env: TxEnv, +{ +} + +impl TxCodePathValue for ManagedBuffer where Env: TxEnv {} + +/// Contains code for a deploy or upgrade. +pub struct CodePath(pub CodePathValue); + +impl TxCodeSource for CodePath +where + Env: TxEnv, + CodePathValue: TxCodePathValue, +{ +} + +impl TxCodeSourceSpecified for CodePath +where + Env: TxEnv, + CodePathValue: TxCodePathValue, +{ +} diff --git a/framework/base/src/types/interaction/tx_exec/tx_exec_test_call.rs b/framework/base/src/types/interaction/tx_exec/tx_exec_test_call.rs index 93f7f9331a..2159729ae8 100644 --- a/framework/base/src/types/interaction/tx_exec/tx_exec_test_call.rs +++ b/framework/base/src/types/interaction/tx_exec/tx_exec_test_call.rs @@ -1,13 +1,16 @@ use crate::api::{CallTypeApi, TestApi}; - +use crate::tuple_util::NestedTupleFlatten; use crate::{ contract_base::TestRawWrapper, - proxy_imports::TxFromSpecified, types::{ - FunctionCall, Tx, TxData, TxEmptyResultHandler, TxGas, TxPayment, TxScEnv, TxToSpecified, + CodePath, DeployCall, FunctionCall, ManagedAddress, ManagedVec, RHListExec, Tx, + TxCodePathValue, TxData, TxEmptyResultHandler, TxFromSpecified, TxGas, TxPayment, + TxPaymentEgldOnly, TxResultHandler, TxScEnv, TxToSpecified, }, }; +use super::DeployRawResult; + impl Tx, From, To, Payment, Gas, FC, RH> where Api: CallTypeApi + TestApi, @@ -31,3 +34,57 @@ where TestRawWrapper::::new().stop_prank(); } } + +impl + Tx, From, (), Payment, Gas, DeployCall, CodePath>, RH> +where + Api: CallTypeApi + TestApi, + From: TxFromSpecified>, + Payment: TxPaymentEgldOnly>, + Gas: TxGas>, + CodePathValue: TxCodePathValue>, + RH: TxResultHandler>, +{ + fn execute_test_deploy_raw(self) -> (ManagedAddress, RH) { + let gas_limit = self.gas.gas_value(&self.env); + let new_address = self.from.with_value_ref(&self.env, |from| { + self.payment.with_egld_value(&self.env, |egld_value| { + TestRawWrapper::::new().deploy_contract( + from, + gas_limit, + egld_value, + &self.data.code_source.0.into_value(&self.env), + &self.data.arg_buffer, + ) + }) + }); + (new_address, self.result_handler) + } +} + +impl + Tx, From, (), Payment, Gas, DeployCall, CodePath>, RH> +where + Api: CallTypeApi + TestApi, + From: TxFromSpecified>, + Payment: TxPaymentEgldOnly>, + Gas: TxGas>, + CodePathValue: TxCodePathValue>, + RH: RHListExec, TxScEnv>, + RH::ListReturns: NestedTupleFlatten, +{ + /// Synchronously deploys a contract. + pub fn test_deploy(self) -> ::Unpacked { + let (new_address, result_handler) = self.execute_test_deploy_raw(); + + // TODO: results currently not retrieved + let raw_results = ManagedVec::new(); + + let deploy_raw_result = DeployRawResult { + new_address, + raw_results, + }; + let tuple_result = result_handler.list_process_result(&deploy_raw_result); + tuple_result.flatten_unpack() + } +}