Skip to content

Commit

Permalink
Merge branch 'rc/v0.53' into managed-decimal-var-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Sep 4, 2024
2 parents d2d2d15 + 4ae95f1 commit 29e5061
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 19 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/plotter-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
branches:
- master
pull_request:

permissions:
checks: write
pull-requests: write

jobs:
template_test_current:
name: Plotter tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown

- name: Run plotter tests
run: |
cd tools/plotter
cargo test
8 changes: 7 additions & 1 deletion contracts/feature-tests/basic-features/sc-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ kill_legacy_callback = true
[[proxy]]
path = "src/basic_features_proxy.rs"
add-unlabelled = false
add-endpoints = ["init", "store_bytes", "load_bytes", "returns_egld_decimal"]
add-endpoints = [
"init",
"store_bytes",
"load_bytes",
"returns_egld_decimal",
"echo_managed_option",
]
14 changes: 14 additions & 0 deletions contracts/feature-tests/basic-features/src/basic_features_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ where
To: TxTo<Env>,
Gas: TxGas<Env>,
{
/// This tests how is generated type name in proxy
pub fn echo_managed_option<
Arg0: ProxyArg<ManagedOption<Env::Api, BigUint<Env::Api>>>,
>(
self,
mo: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedOption<Env::Api, BigUint<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("echo_managed_option")
.argument(&mo)
.original_result()
}

pub fn load_bytes(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedBuffer<Env::Api>> {
Expand Down
8 changes: 7 additions & 1 deletion contracts/feature-tests/basic-features/src/echo_managed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
multiversx_sc::imports!();
use multiversx_sc::imports::*;

/// Test endpoint argument and result serialization.
#[multiversx_sc::module]
Expand All @@ -23,6 +23,12 @@ pub trait EchoManagedTypes {
ma
}

/// This tests how is generated type name in proxy
#[endpoint]
fn echo_managed_option(&self, mo: ManagedOption<BigUint>) -> ManagedOption<BigUint> {
mo
}

/// This tests that nested serialization of big ints within unmanaged types works.
#[endpoint]
fn echo_big_int_managed_vec(&self, x: ManagedVec<BigInt>) -> ManagedVec<BigInt> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use imports::{MxscPath, ReturnsResult, TestAddress, TestSCAddress};
use multiversx_sc::types::{BigUint, ManagedOption};
use multiversx_sc_scenario::{api::StaticApi, imports, ScenarioTxRun, ScenarioWorld};

const OWNER_ADDRESS: TestAddress = TestAddress::new("owner");
const BASIC_FEATURES_ADDRESS: TestSCAddress = TestSCAddress::new("basic-features");
const BASIC_FEATURES_PATH: MxscPath = MxscPath::new("output/basic-features.mxsc.json");

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

blockchain.register_contract(BASIC_FEATURES_PATH, basic_features::ContractBuilder);

blockchain.account(OWNER_ADDRESS).nonce(1);
blockchain
.account(BASIC_FEATURES_ADDRESS)
.nonce(1)
.code(BASIC_FEATURES_PATH);

blockchain
}

#[test]
fn managed_option_test() {
let mut world = world();

let type_number: BigUint<StaticApi> = BigUint::zero();
let expected_type_managed_option: ManagedOption<StaticApi, BigUint<StaticApi>> =
ManagedOption::some(type_number);

let output = world
.tx()
.from(OWNER_ADDRESS)
.to(BASIC_FEATURES_ADDRESS)
.typed(basic_features::basic_features_proxy::BasicFeaturesProxy)
.echo_managed_option(expected_type_managed_option.clone())
.returns(ReturnsResult)
.run();

assert_eq!(output, expected_type_managed_option);
}
1 change: 1 addition & 0 deletions contracts/feature-tests/basic-features/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ multiversx_sc_wasm_adapter::endpoints! {
echo_big_int => echo_big_int
echo_managed_buffer => echo_managed_buffer
echo_managed_address => echo_managed_address
echo_managed_option => echo_managed_option
echo_big_int_managed_vec => echo_big_int_managed_vec
echo_big_int_tuple => echo_big_int_tuple
echo_big_int_option => echo_big_int_option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,44 @@ fn test_multi_call_back_transfers() {
.check_account(PROMISES_FEATURE_ADDRESS)
.esdt_balance(TOKEN_ID_EXPR, token_amount);
}

#[test]
fn test_back_transfers_logs() {
let mut state = PromisesFeaturesTestState::new();
let token_amount = BigUint::from(1000u64);

let logs = state
.world
.tx()
.from(USER_ADDRESS)
.to(PROMISES_FEATURE_ADDRESS)
.typed(promises_feature_proxy::PromisesFeaturesProxy)
.forward_sync_retrieve_funds_bt(VAULT_ADDRESS, TOKEN_ID, 0u64, &token_amount)
.returns(ReturnsLogs)
.run();

assert!(!logs.is_empty() && !logs[0].topics.is_empty());
assert_eq!(logs[0].address, PROMISES_FEATURE_ADDRESS);
assert_eq!(logs[0].endpoint, "transferValueOnly");
}

#[test]
fn test_multi_call_back_transfers_logs() {
let mut state = PromisesFeaturesTestState::new();
let token_amount = BigUint::from(1000u64);
let half_token_amount = token_amount.clone() / 2u64;

let logs = state
.world
.tx()
.from(USER_ADDRESS)
.to(PROMISES_FEATURE_ADDRESS)
.typed(promises_feature_proxy::PromisesFeaturesProxy)
.forward_sync_retrieve_funds_bt_twice(VAULT_ADDRESS, TOKEN_ID, 0u64, &half_token_amount)
.returns(ReturnsLogs)
.run();

assert!(!logs.is_empty() && !logs[0].topics.is_empty());
assert_eq!(logs[0].address, PROMISES_FEATURE_ADDRESS);
assert_eq!(logs[0].endpoint, "transferValueOnly");
}
2 changes: 2 additions & 0 deletions framework/scenario/src/facade/result_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod expect_error;
mod expect_message;
mod expect_status;
mod expect_value;
mod returns_logs;
mod returns_message;
mod returns_new_bech32_address;
mod returns_new_token_identifier;
Expand All @@ -12,6 +13,7 @@ pub use expect_error::ExpectError;
pub use expect_message::ExpectMessage;
pub use expect_status::ExpectStatus;
pub use expect_value::ExpectValue;
pub use returns_logs::ReturnsLogs;
pub use returns_message::ReturnsMessage;
pub use returns_new_bech32_address::ReturnsNewBech32Address;
pub use returns_new_token_identifier::ReturnsNewTokenIdentifier;
Expand Down
24 changes: 24 additions & 0 deletions framework/scenario/src/facade/result_handlers/returns_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use multiversx_sc::types::RHListItemExec;

use crate::{
multiversx_sc::types::{RHListItem, TxEnv},
scenario_model::{Log, TxResponse},
};

pub struct ReturnsLogs;

impl<Env, Original> RHListItem<Env, Original> for ReturnsLogs
where
Env: TxEnv,
{
type Returns = Vec<Log>;
}

impl<Env, Original> RHListItemExec<TxResponse, Env, Original> for ReturnsLogs
where
Env: TxEnv,
{
fn item_process_result(self, raw_result: &TxResponse) -> Self::Returns {
raw_result.logs.clone()
}
}
10 changes: 5 additions & 5 deletions framework/scenario/src/scenario/model/transaction/log.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::scenario_model::BytesValue;
use multiversx_sc::types::Address;

#[derive(Debug, Clone)]
pub struct Log {
pub address: BytesValue,
pub endpoint: BytesValue,
pub topics: Vec<BytesValue>,
pub data: BytesValue,
pub address: Address,
pub endpoint: String,
pub topics: Vec<Vec<u8>>,
pub data: Vec<Vec<u8>>,
}
10 changes: 10 additions & 0 deletions framework/scenario/src/scenario/model/transaction/tx_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ impl TxResponse {
status: tx_result.result_status,
message: tx_result.result_message,
},
logs: tx_result
.result_logs
.iter()
.map(|tx_log| Log {
address: Address::from_slice(tx_log.address.as_bytes()),
endpoint: tx_log.endpoint.to_string(),
topics: tx_log.topics.clone(),
data: tx_log.data.clone(),
})
.collect(),
..Default::default()
}
}
Expand Down
50 changes: 44 additions & 6 deletions framework/snippets/src/network_response.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use multiversx_sc_scenario::{
imports::{Address, ESDTSystemSCAddress},
multiversx_chain_vm::crypto_functions::keccak256,
scenario_model::{TxResponse, TxResponseStatus},
scenario_model::{Log, TxResponse, TxResponseStatus},
};
use multiversx_sdk::{
data::transaction::{ApiSmartContractResult, Events, TransactionOnNetwork},
Expand Down Expand Up @@ -44,6 +44,7 @@ fn process_success(tx: &TransactionOnNetwork) -> TxResponse {
out: process_out(tx),
new_deployed_address: process_new_deployed_address(tx),
new_issued_token_identifier: process_new_issued_token_identifier(tx),
logs: process_logs(tx),
..Default::default()
}
}
Expand All @@ -58,18 +59,55 @@ fn process_out(tx: &TransactionOnNetwork) -> Vec<Vec<u8>> {
}
}

fn process_logs(tx: &TransactionOnNetwork) -> Vec<Log> {
if let Some(api_logs) = &tx.logs {
return api_logs
.events
.iter()
.map(|event| Log {
address: Address::from_slice(&event.address.to_bytes()),
endpoint: event.identifier.clone(),
topics: extract_topics(event),
data: extract_data(event),
})
.collect::<Vec<Log>>();
}

Vec::new()
}

fn extract_data(event: &Events) -> Vec<Vec<u8>> {
let mut out: Vec<Vec<u8>> = Vec::new();
event
.data
.for_each(|data_field| out.push(data_field.clone().into_bytes()));
out
}

fn extract_topics(event: &Events) -> Vec<Vec<u8>> {
event
.topics
.clone()
.unwrap_or_default()
.into_iter()
.map(|s| s.into_bytes())
.collect()
}

fn process_out_from_log(tx: &TransactionOnNetwork) -> Option<Vec<Vec<u8>>> {
if let Some(logs) = &tx.logs {
logs.events.iter().rev().find_map(|event| {
if event.identifier == "writeLog" {
if let Some(data) = &event.data {
let decoded_data = String::from_utf8(base64_decode(data)).unwrap();
let mut out = Vec::new();
event.data.for_each(|data_member| {
let decoded_data = String::from_utf8(base64_decode(data_member)).unwrap();

if decoded_data.starts_with('@') {
let out = decode_scr_data_or_panic(decoded_data.as_str());
return Some(out);
let out_content = decode_scr_data_or_panic(decoded_data.as_str());
out.extend(out_content);
}
}
});
return Some(out);
}

None
Expand Down
Loading

0 comments on commit 29e5061

Please sign in to comment.