Skip to content

Commit

Permalink
param tests - deploy unified syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Jun 4, 2024
1 parent e2b889e commit 03bc446
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 17 deletions.
21 changes: 9 additions & 12 deletions contracts/parametric-tests/adder-pt/src/adder_pt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 21 additions & 2 deletions framework/base/src/types/interaction/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<CodePathValue>(
self,
code: CodePathValue,
) -> Tx<Env, From, To, Payment, Gas, DeployCall<Env, CodePath<CodePathValue>>, RH>
where
CodePathValue: TxCodePathValue<Env>,
{
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<Env, From, To, Payment, Gas, CodeSource, RH>
Expand Down
30 changes: 30 additions & 0 deletions framework/base/src/types/interaction/tx_data/tx_code_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,33 @@ where
FromSourceValue: TxFromSourceValue<Env>,
{
}

#[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<Env>: AnnotatedValue<Env, ManagedBuffer<Env::Api>>
where
Env: TxEnv,
{
}

impl<Env> TxCodePathValue<Env> for ManagedBuffer<Env::Api> where Env: TxEnv {}

/// Contains code for a deploy or upgrade.
pub struct CodePath<CodePathValue>(pub CodePathValue);

impl<Env, CodePathValue> TxCodeSource<Env> for CodePath<CodePathValue>
where
Env: TxEnv,
CodePathValue: TxCodePathValue<Env>,
{
}

impl<Env, CodePathValue> TxCodeSourceSpecified<Env> for CodePath<CodePathValue>
where
Env: TxEnv,
CodePathValue: TxCodePathValue<Env>,
{
}
63 changes: 60 additions & 3 deletions framework/base/src/types/interaction/tx_exec/tx_exec_test_call.rs
Original file line number Diff line number Diff line change
@@ -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<Api, From, To, Payment, Gas, FC, RH> Tx<TxScEnv<Api>, From, To, Payment, Gas, FC, RH>
where
Api: CallTypeApi + TestApi,
Expand All @@ -31,3 +34,57 @@ where
TestRawWrapper::<Api>::new().stop_prank();
}
}

impl<Api, From, Payment, Gas, CodePathValue, RH>
Tx<TxScEnv<Api>, From, (), Payment, Gas, DeployCall<TxScEnv<Api>, CodePath<CodePathValue>>, RH>
where
Api: CallTypeApi + TestApi,
From: TxFromSpecified<TxScEnv<Api>>,
Payment: TxPaymentEgldOnly<TxScEnv<Api>>,
Gas: TxGas<TxScEnv<Api>>,
CodePathValue: TxCodePathValue<TxScEnv<Api>>,
RH: TxResultHandler<TxScEnv<Api>>,
{
fn execute_test_deploy_raw(self) -> (ManagedAddress<Api>, 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::<Api>::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<Api, From, Payment, Gas, CodePathValue, RH>
Tx<TxScEnv<Api>, From, (), Payment, Gas, DeployCall<TxScEnv<Api>, CodePath<CodePathValue>>, RH>
where
Api: CallTypeApi + TestApi,
From: TxFromSpecified<TxScEnv<Api>>,
Payment: TxPaymentEgldOnly<TxScEnv<Api>>,
Gas: TxGas<TxScEnv<Api>>,
CodePathValue: TxCodePathValue<TxScEnv<Api>>,
RH: RHListExec<DeployRawResult<Api>, TxScEnv<Api>>,
RH::ListReturns: NestedTupleFlatten,
{
/// Synchronously deploys a contract.
pub fn test_deploy(self) -> <RH::ListReturns as NestedTupleFlatten>::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()
}
}

0 comments on commit 03bc446

Please sign in to comment.