Skip to content

Commit

Permalink
Merge pull request #1649 from multiversx/llr-unified
Browse files Browse the repository at this point in the history
unified llr
  • Loading branch information
andrei-marinica authored May 27, 2024
2 parents c2aab74 + 40ae22a commit 18b63e6
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[proxy]]
path = "src/linked_list_repeat_proxy.rs"
[[proxy.path-rename]]
from = "benchmark_common::example_struct"
to = "benchmark_common"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
multiversx_sc::imports!();

use benchmark_common::ExampleStruct;
pub mod linked_list_repeat_proxy;

#[multiversx_sc::contract]
pub trait LinkedListRepeat: benchmark_common::BenchmarkCommon {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Code generated by the multiversx-sc proxy generator. DO NOT EDIT.

////////////////////////////////////////////////////
////////////////// AUTO-GENERATED //////////////////
////////////////////////////////////////////////////

#![allow(dead_code)]
#![allow(clippy::all)]

use multiversx_sc::proxy_imports::*;

pub struct LinkedListRepeatProxy;

impl<Env, From, To, Gas> TxProxyTrait<Env, From, To, Gas> for LinkedListRepeatProxy
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
type TxProxyMethods = LinkedListRepeatProxyMethods<Env, From, To, Gas>;

fn proxy_methods(self, tx: Tx<Env, From, To, (), Gas, (), ()>) -> Self::TxProxyMethods {
LinkedListRepeatProxyMethods { wrapped_tx: tx }
}
}

pub struct LinkedListRepeatProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
wrapped_tx: Tx<Env, From, To, (), Gas, (), ()>,
}

#[rustfmt::skip]
impl<Env, From, Gas> LinkedListRepeatProxyMethods<Env, From, (), Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
Gas: TxGas<Env>,
{
pub fn init(
self,
) -> TxTypedDeploy<Env, From, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_deploy()
.original_result()
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> LinkedListRepeatProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn add<
Arg0: ProxyArg<usize>,
Arg1: ProxyArg<ManagedBuffer<Env::Api>>,
>(
self,
num_repeats: Arg0,
value: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("add")
.argument(&num_repeats)
.argument(&value)
.original_result()
}

pub fn count<
Arg0: ProxyArg<ManagedBuffer<Env::Api>>,
>(
self,
value: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, usize> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("count")
.argument(&value)
.original_result()
}

pub fn remove<
Arg0: ProxyArg<usize>,
>(
self,
num_repeats: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("remove")
.argument(&num_repeats)
.original_result()
}

pub fn bench(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, MultiValueEncoded<Env::Api, ManagedBuffer<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("bench")
.original_result()
}

pub fn add_struct<
Arg0: ProxyArg<usize>,
Arg1: ProxyArg<benchmark_common::ExampleStruct<Env::Api>>,
>(
self,
num_repeats: Arg0,
value: Arg1,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("add_struct")
.argument(&num_repeats)
.argument(&value)
.original_result()
}

pub fn count_struct<
Arg0: ProxyArg<benchmark_common::ExampleStruct<Env::Api>>,
>(
self,
value: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, usize> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("count_struct")
.argument(&value)
.original_result()
}

pub fn remove_struct<
Arg0: ProxyArg<usize>,
>(
self,
num_repeats: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("remove_struct")
.argument(&num_repeats)
.original_result()
}

pub fn bench_struct(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, MultiValueEncoded<Env::Api, benchmark_common::ExampleStruct<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("bench_struct")
.original_result()
}
}
Original file line number Diff line number Diff line change
@@ -1,78 +1,76 @@
#![allow(deprecated)] // TODO: unified syntax

use benchmark_common::ExampleStruct;
use linked_list_repeat::ProxyTrait;
use linked_list_repeat::linked_list_repeat_proxy;
use multiversx_sc_scenario::imports::*;

const WASM_PATH_EXPR: &str = "mxsc:output/linked-list-repeat.mxsc.json";
const WASM_PATH: MxscPath = MxscPath::new("output/linked-list-repeat.mxsc.json");
const LLR_ADDRESS: TestSCAddress = TestSCAddress::new("llr");
const OWNER_ADDRESS: TestAddress = TestAddress::new("owner");

fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();

blockchain.register_contract(WASM_PATH_EXPR, linked_list_repeat::ContractBuilder);
blockchain.register_contract(WASM_PATH, linked_list_repeat::ContractBuilder);
blockchain
}

fn setup() -> ScenarioWorld {
let mut world = world();
let llr_code = world.code_expression(WASM_PATH_EXPR);
world
.account(OWNER_ADDRESS)
.nonce(1)
.new_address(OWNER_ADDRESS, 1, LLR_ADDRESS);

world
.set_state_step(
SetStateStep::new()
.put_account("address:owner", Account::new().nonce(1))
.new_address("address:owner", 1, "sc:llr"),
)
.sc_deploy(
ScDeployStep::new()
.from("address:owner")
.code(llr_code)
.expect(TxExpect::ok().no_result()),
);
.tx()
.from(OWNER_ADDRESS)
.typed(linked_list_repeat_proxy::LinkedListRepeatProxy)
.init()
.code(WASM_PATH)
.run();
world
}

#[test]
fn linked_list_repeat_blackbox_raw() {
let mut world = setup();
let mut contract = ContractInfo::<linked_list_repeat::Proxy<StaticApi>>::new("sc:llr");

let num_repeats = 5usize;

world
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:llr")
.call(contract.add(num_repeats, "test--"))
.expect(TxExpect::ok().no_result()),
)
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:llr")
.call(contract.count("test--\x00\x00\x00\x04"))
.expect(TxExpect::ok().result("1")),
)
.sc_query_use_result(
ScQueryStep::new().to("sc:llr").call(contract.bench()),
|tr: TypedResponse<MultiValueEncoded<StaticApi, String>>| {
let result = tr.result.unwrap().into_iter().collect::<Vec<String>>();
assert_eq!(result.len(), num_repeats);
for (index, item) in result.iter().enumerate() {
let index_str =
String::from_utf8((index as u32).to_be_bytes().to_vec()).unwrap();
let expected = format!("test--{}", index_str);
assert_eq!(item, &expected);
}
},
);
.tx()
.from(OWNER_ADDRESS)
.to(LLR_ADDRESS)
.typed(linked_list_repeat_proxy::LinkedListRepeatProxy)
.add(num_repeats, "test--")
.run();

world
.tx()
.from(OWNER_ADDRESS)
.to(LLR_ADDRESS)
.typed(linked_list_repeat_proxy::LinkedListRepeatProxy)
.count("test--\x00\x00\x00\x04")
.returns(ExpectValue(1u32))
.run();

let items = world
.query()
.to(LLR_ADDRESS)
.typed(linked_list_repeat_proxy::LinkedListRepeatProxy)
.bench()
.returns(ReturnsResult)
.run();

for (index, item) in items.into_iter().enumerate() {
let index_str = String::from_utf8((index as u32).to_be_bytes().to_vec()).unwrap();
let expected = format!("test--{}", index_str);
assert_eq!(item.to_string(), expected);
}
}

#[test]
fn linked_list_repeat_struct_blackbox_raw() {
let mut world = setup();
let mut contract = ContractInfo::<linked_list_repeat::Proxy<StaticApi>>::new("sc:llr");

let mut example = ExampleStruct {
first_token_id: TokenIdentifier::from_esdt_bytes(b"str:TESTTOK-1234"),
Expand All @@ -82,20 +80,22 @@ fn linked_list_repeat_struct_blackbox_raw() {
second_token_nonce: 0,
second_token_amount: multiversx_sc::types::BigUint::from(1_000_000_000_000_000_000u64),
};
world.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:llr")
.call(contract.add_struct(5u32, example.clone()))
.expect(TxExpect::ok().no_result()),
);

world
.tx()
.from(OWNER_ADDRESS)
.to(LLR_ADDRESS)
.typed(linked_list_repeat_proxy::LinkedListRepeatProxy)
.add_struct(5u32, example.clone())
.run();
example.first_token_nonce = 3;
example.second_token_nonce = 3;
world.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:llr")
.call(contract.count_struct(example))
.expect(TxExpect::ok().result("1")),
);
world
.tx()
.from(OWNER_ADDRESS)
.to(LLR_ADDRESS)
.typed(linked_list_repeat_proxy::LinkedListRepeatProxy)
.count_struct(example)
.returns(ExpectValue(1u32))
.run();
}
2 changes: 1 addition & 1 deletion framework/base/src/types/interaction/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ where
///
/// Whenever possible, use proxies instead.
///
/// Doesa not serialize, does not enforce type safety.
/// Does not serialize, does not enforce type safety.
#[inline]
pub fn arguments_raw(mut self, raw: ManagedArgBuffer<Env::Api>) -> Self {
self.data.arg_buffer = raw;
Expand Down

0 comments on commit 18b63e6

Please sign in to comment.