Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed multiversx-sdk dependency from multiversx-sc-scenario #1684

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
445 changes: 381 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contracts/feature-tests/abi-tester/src/abi_test_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ pub struct OnlyShowsUpInEsdtAttr {

#[derive(TypeAbi)]
pub struct ManagedDecimalWrapper<M: ManagedTypeApi> {
#[allow(dead_code)]
pub field: ManagedDecimal<M, ConstDecimals<2>>,
}
2 changes: 1 addition & 1 deletion framework/base/src/storage/mappers/vec_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
storage::{storage_clear, storage_set, StorageKey},
types::{ManagedAddress, ManagedType, MultiValueEncoded},
};
use core::{marker::PhantomData, usize};
use core::marker::PhantomData;

const ITEM_SUFFIX: &[u8] = b".item";
const LEN_SUFFIX: &[u8] = b".len";
Expand Down
4 changes: 0 additions & 4 deletions framework/scenario/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,3 @@ version = "0.2.0"
[dependencies.multiversx-chain-vm]
version = "=0.8.4"
path = "../../vm"

[dependencies.multiversx-sdk]
version = "=0.4.1"
path = "../../sdk/core"
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use multiversx_chain_vm::tx_mock::TxResult;
use multiversx_sc::types::Address;
use multiversx_sdk::data::transaction::{ApiLogs, ApiSmartContractResult};

use super::{Log, TxExpect, TxResponseStatus};

Expand All @@ -21,10 +20,6 @@ pub struct TxResponse {
pub gas: u64,
/// The refund of the transaction.
pub refund: u64,
/// The smart contract results of the transaction.
pub api_scrs: Vec<ApiSmartContractResult>,
/// The api logs of the transaction.
pub api_logs: Option<ApiLogs>,
}

impl TxResponse {
Expand Down
111 changes: 39 additions & 72 deletions framework/snippets/src/network_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,19 @@ const LOG_IDENTIFIER_SIGNAL_ERROR: &str = "signalError";

/// Creates a [`TxResponse`] from a [`TransactionOnNetwork`].
pub fn parse_tx_response(tx: TransactionOnNetwork) -> TxResponse {
let mut response = TxResponse {
api_scrs: tx.smart_contract_results.unwrap_or_default(),
api_logs: tx.logs,
..Default::default()
};

response.tx_error = process_signal_error(&response);
if !response.tx_error.is_success() {
return response;
let tx_error = process_signal_error(&tx);
if !tx_error.is_success() {
return TxResponse {
tx_error,
..Default::default()
};
}

process(
&mut response,
tx.sender.to_bytes(),
tx.nonce,
tx.processing_type_on_destination,
);

response
process_success(&tx)
}

fn process_signal_error(tx_response: &TxResponse) -> TxResponseStatus {
if let Some(event) = find_log(tx_response, LOG_IDENTIFIER_SIGNAL_ERROR) {
fn process_signal_error(tx: &TransactionOnNetwork) -> TxResponseStatus {
if let Some(event) = find_log(tx, LOG_IDENTIFIER_SIGNAL_ERROR) {
let topics = event.topics.as_ref();
if let Some(error) = process_topics_error(topics) {
return TxResponseStatus::signal_error(&error);
Expand All @@ -49,34 +39,27 @@ fn process_signal_error(tx_response: &TxResponse) -> TxResponseStatus {
TxResponseStatus::default()
}

fn process(
tx_response: &mut TxResponse,
sender_address: [u8; 32],
nonce: u64,
processing_type_on_destination: String,
) {
process_out(tx_response);
process_new_deployed_address(
tx_response,
sender_address,
nonce,
processing_type_on_destination,
);
process_new_issued_token_identifier(tx_response);
fn process_success(tx: &TransactionOnNetwork) -> TxResponse {
TxResponse {
out: process_out(tx),
new_deployed_address: process_new_deployed_address(tx),
new_issued_token_identifier: process_new_issued_token_identifier(tx),
..Default::default()
}
}

fn process_out(tx_response: &mut TxResponse) {
let out_scr = tx_response.api_scrs.iter().find(is_out_scr);
fn process_out(tx: &TransactionOnNetwork) -> Vec<Vec<u8>> {
let out_scr = tx.smart_contract_results.iter().find(is_out_scr);

if let Some(out_scr) = out_scr {
tx_response.out = decode_scr_data_or_panic(&out_scr.data);
} else if let Some(data) = process_out_from_log(tx_response) {
tx_response.out = data
decode_scr_data_or_panic(&out_scr.data)
} else {
process_out_from_log(tx).unwrap_or_default()
}
}

fn process_out_from_log(tx_response: &TxResponse) -> Option<Vec<Vec<u8>>> {
if let Some(logs) = &tx_response.api_logs {
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 {
Expand All @@ -96,17 +79,13 @@ fn process_out_from_log(tx_response: &TxResponse) -> Option<Vec<Vec<u8>>> {
}
}

fn process_new_deployed_address(
tx_response: &mut TxResponse,
sender_address_bytes: [u8; 32],
nonce: u64,
processing_type_on_destination: String,
) {
if processing_type_on_destination != SC_DEPLOY_PROCESSING_TYPE {
return;
fn process_new_deployed_address(tx: &TransactionOnNetwork) -> Option<Address> {
if tx.processing_type_on_destination != SC_DEPLOY_PROCESSING_TYPE {
return None;
}

let sender_nonce_bytes = nonce.to_le_bytes();
let sender_address_bytes = tx.sender.to_bytes();
let sender_nonce_bytes = tx.nonce.to_le_bytes();
let mut bytes_to_hash: Vec<u8> = Vec::new();
bytes_to_hash.extend_from_slice(&sender_address_bytes);
bytes_to_hash.extend_from_slice(&sender_nonce_bytes);
Expand All @@ -120,17 +99,17 @@ fn process_new_deployed_address(
address[10..30].copy_from_slice(&address_keccak[10..30]);
address[30..32].copy_from_slice(&sender_address_bytes[30..32]);

tx_response.new_deployed_address = Some(Address::from(address));
Some(Address::from(address))
}

fn process_new_issued_token_identifier(tx_response: &mut TxResponse) {
for scr in tx_response.api_scrs.iter() {
fn process_new_issued_token_identifier(tx: &TransactionOnNetwork) -> Option<String> {
for scr in tx.smart_contract_results.iter() {
if scr.sender.to_bech32_string().unwrap() != ESDTSystemSCAddress.to_bech32_string() {
continue;
}

let Some(prev_tx) = tx_response
.api_scrs
let Some(prev_tx) = tx
.smart_contract_results
.iter()
.find(|e| e.hash == scr.prev_tx_hash)
else {
Expand All @@ -155,30 +134,18 @@ fn process_new_issued_token_identifier(tx_response: &mut TxResponse) {

if scr.data.starts_with("ESDTTransfer@") {
let encoded_tid = scr.data.split('@').nth(1);
if encoded_tid.is_none() {
return;
}

tx_response.new_issued_token_identifier =
Some(String::from_utf8(hex::decode(encoded_tid.unwrap()).unwrap()).unwrap());

break;
return Some(String::from_utf8(hex::decode(encoded_tid?).unwrap()).unwrap());
} else if scr.data.starts_with("@00@") {
let encoded_tid = scr.data.split('@').nth(2);
if encoded_tid.is_none() {
return;
}

tx_response.new_issued_token_identifier =
Some(String::from_utf8(hex::decode(encoded_tid.unwrap()).unwrap()).unwrap());

break;
return Some(String::from_utf8(hex::decode(encoded_tid?).unwrap()).unwrap());
}
}

None
}

fn find_log<'a>(tx_response: &'a TxResponse, log_identifier: &str) -> Option<&'a Events> {
if let Some(logs) = &tx_response.api_logs {
fn find_log<'a>(tx: &'a TransactionOnNetwork, log_identifier: &str) -> Option<&'a Events> {
if let Some(logs) = &tx.logs {
logs.events
.iter()
.find(|event| event.identifier == log_identifier)
Expand Down
8 changes: 5 additions & 3 deletions framework/snippets/tests/test_tx_multiple_sc_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ fn test_transaction_multiple_sc_results() {
.data
.unwrap()
.transaction;
let tx_response = network_response::parse_tx_response(tx_on_network);
assert_eq!(tx_response.api_scrs.len(), 4usize);
assert!(is_out_scr(&tx_response.api_scrs.get(2).unwrap()));
assert_eq!(tx_on_network.smart_contract_results.len(), 4usize);
assert!(is_out_scr(
&tx_on_network.smart_contract_results.get(2).unwrap()
));
let _ = network_response::parse_tx_response(tx_on_network);
}
3 changes: 2 additions & 1 deletion sdk/core/src/data/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ pub struct TransactionOnNetwork {
pub status: String,
pub hyperblock_nonce: Option<u64>,
pub hyperblock_hash: Option<String>,
pub smart_contract_results: Option<Vec<ApiSmartContractResult>>,
#[serde(default)]
pub smart_contract_results: Vec<ApiSmartContractResult>,
pub logs: Option<ApiLogs>,
}

Expand Down
Loading