From 28de5b53652fc1972347f38a14dabe94aa58b1de Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 29 May 2024 21:39:05 +0300 Subject: [PATCH 1/3] moved tx response parser to multiversx-sc-snippets --- Cargo.lock | 29 +-- .../src/scenario/model/transaction.rs | 2 - .../scenario/model/transaction/tx_response.rs | 186 +------------- .../model/transaction/tx_response_status.rs | 4 +- .../model/transaction/tx_response_utils.rs | 35 --- framework/snippets/Cargo.toml | 3 + framework/snippets/src/account_tool.rs | 2 +- .../interactor_scenario/interactor_sc_call.rs | 6 +- .../interactor_sc_deploy.rs | 6 +- framework/snippets/src/lib.rs | 1 + .../src/multi/interactor_multi_sc_exec.rs | 7 +- .../snippets/src/tx_response_from_network.rs | 228 ++++++++++++++++++ .../tests/test_tx_deployed_address.rs | 8 +- .../tests/test_tx_issued_token_identifier.rs | 14 +- .../tests/test_tx_multi_contract_sc_result.rs | 10 +- .../tests/test_tx_multiple_sc_results.rs | 4 +- .../tests/test_tx_sc_result.rs | 6 +- 17 files changed, 285 insertions(+), 266 deletions(-) delete mode 100644 framework/scenario/src/scenario/model/transaction/tx_response_utils.rs create mode 100644 framework/snippets/src/tx_response_from_network.rs rename framework/{scenario => snippets}/tests/test_tx_deployed_address.rs (99%) rename framework/{scenario => snippets}/tests/test_tx_issued_token_identifier.rs (99%) rename framework/{scenario => snippets}/tests/test_tx_multi_contract_sc_result.rs (98%) rename framework/{scenario => snippets}/tests/test_tx_multiple_sc_results.rs (98%) rename framework/{scenario => snippets}/tests/test_tx_sc_result.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index a7bc64efd5..fb08c18c3e 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,9 +47,9 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" dependencies = [ "gimli", ] @@ -182,9 +182,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.71" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11" dependencies = [ "addr2line", "cc", @@ -1286,9 +1286,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.28.1" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" [[package]] name = "globset" @@ -1449,9 +1449,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d8d52be92d09acc2e01dddb7fde3ad983fc6489c7db4837e605bc3fca4cb63e" +checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56" dependencies = [ "bytes", "futures-channel", @@ -2054,6 +2054,7 @@ dependencies = [ "multiversx-chain-scenario-format", "multiversx-sc-scenario", "multiversx-sdk", + "serde_json", "tokio", ] @@ -2232,9 +2233,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.2" +version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e" dependencies = [ "memchr", ] @@ -3885,9 +3886,9 @@ checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "winnow" -version = "0.6.8" +version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c52e9c97a68071b23e836c9380edae937f17b9c4667bd021973efc689f618d" +checksum = "86c949fede1d13936a99f14fafd3e76fd642b556dd2ce96287fbe2e0151bfac6" dependencies = [ "memchr", ] @@ -3944,9 +3945,9 @@ dependencies = [ [[package]] name = "zip" -version = "2.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2568cd0f20e86cd9a7349fe05178f7bd22f22724678448ae5a9bac266df2689" +checksum = "1dd56a4d5921bc2f99947ac5b3abe5f510b1be7376fdc5e9fce4a23c6a93e87c" dependencies = [ "arbitrary", "crc32fast", diff --git a/framework/scenario/src/scenario/model/transaction.rs b/framework/scenario/src/scenario/model/transaction.rs index c1a6d6a271..3ffa8a542d 100644 --- a/framework/scenario/src/scenario/model/transaction.rs +++ b/framework/scenario/src/scenario/model/transaction.rs @@ -9,7 +9,6 @@ mod tx_interpret_util; mod tx_query; mod tx_response; mod tx_response_status; -mod tx_response_utils; mod tx_transfer; mod tx_validator_reward; mod typed_response; @@ -24,7 +23,6 @@ pub use tx_expect::*; pub use tx_query::*; pub use tx_response::TxResponse; pub use tx_response_status::TxResponseStatus; -pub use tx_response_utils::*; pub use tx_transfer::*; pub use tx_validator_reward::*; pub use typed_response::TypedResponse; diff --git a/framework/scenario/src/scenario/model/transaction/tx_response.rs b/framework/scenario/src/scenario/model/transaction/tx_response.rs index 1465885476..46806b1183 100644 --- a/framework/scenario/src/scenario/model/transaction/tx_response.rs +++ b/framework/scenario/src/scenario/model/transaction/tx_response.rs @@ -1,16 +1,8 @@ -use multiversx_chain_vm::{crypto_functions::keccak256, tx_mock::TxResult}; -use multiversx_sc::types::{Address, ESDTSystemSCAddress}; -use multiversx_sdk::{ - data::transaction::{ApiLogs, ApiSmartContractResult, Events, TransactionOnNetwork}, - utils::base64_decode, -}; +use multiversx_chain_vm::tx_mock::TxResult; +use multiversx_sc::types::Address; +use multiversx_sdk::data::transaction::{ApiLogs, ApiSmartContractResult}; -use super::{ - decode_scr_data_or_panic, is_out_scr, process_topics_error, Log, TxExpect, TxResponseStatus, -}; - -const SC_DEPLOY_PROCESSING_TYPE: &str = "SCDeployment"; -const LOG_IDENTIFIER_SIGNAL_ERROR: &str = "signalError"; +use super::{Log, TxExpect, TxResponseStatus}; #[derive(Debug, Default, Clone)] /// The response of a transaction. @@ -48,26 +40,6 @@ impl TxResponse { } } - /// Creates a [`TxResponse`] from a [`TransactionOnNetwork`]. - pub fn from_network_tx(tx: TransactionOnNetwork) -> Self { - let mut response = Self { - api_scrs: tx.smart_contract_results.unwrap_or_default(), - api_logs: tx.logs, - ..Default::default() - }; - - response.tx_error = response.process_signal_error(); - if !response.tx_error.is_success() { - return response; - } - - response.process( - tx.sender.to_bytes(), - tx.nonce, - tx.processing_type_on_destination, - ) - } - /// Creates a [`TxResponse`] from raw results. pub fn from_raw_results(raw_results: Vec>) -> Self { TxResponse { @@ -103,154 +75,4 @@ impl TxResponse { pub fn is_success(&self) -> bool { self.tx_error.is_success() } - - fn process_signal_error(&self) -> TxResponseStatus { - if let Some(event) = self.find_log(LOG_IDENTIFIER_SIGNAL_ERROR) { - let topics = event.topics.as_ref(); - if let Some(error) = process_topics_error(topics) { - return TxResponseStatus::signal_error(&error); - } - - let error_raw = base64_decode(topics.unwrap().get(1).unwrap()); - let error = String::from_utf8(error_raw).unwrap(); - return TxResponseStatus::signal_error(&error); - } - - TxResponseStatus::default() - } - - fn process( - self, - sender_address: [u8; 32], - nonce: u64, - processing_type_on_destination: String, - ) -> Self { - self.process_out() - .process_new_deployed_address(sender_address, nonce, processing_type_on_destination) - .process_new_issued_token_identifier() - } - - fn process_out(mut self) -> Self { - let out_scr = self.api_scrs.iter().find(is_out_scr); - - if let Some(out_scr) = out_scr { - self.out = decode_scr_data_or_panic(&out_scr.data); - } else if let Some(data) = self.process_out_from_log() { - self.out = data - } - - self - } - - fn process_out_from_log(&self) -> Option>> { - if let Some(logs) = &self.api_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(); - - if decoded_data.starts_with('@') { - let out = decode_scr_data_or_panic(decoded_data.as_str()); - return Some(out); - } - } - } - - None - }) - } else { - None - } - } - - fn process_new_deployed_address( - mut self, - sender_address_bytes: [u8; 32], - nonce: u64, - processing_type_on_destination: String, - ) -> Self { - if processing_type_on_destination != SC_DEPLOY_PROCESSING_TYPE { - return self; - } - - let sender_nonce_bytes = nonce.to_le_bytes(); - let mut bytes_to_hash: Vec = Vec::new(); - bytes_to_hash.extend_from_slice(&sender_address_bytes); - bytes_to_hash.extend_from_slice(&sender_nonce_bytes); - - let address_keccak = keccak256(&bytes_to_hash); - - let mut address = [0u8; 32]; - - address[0..8].copy_from_slice(&[0u8; 8]); - address[8..10].copy_from_slice(&[5, 0]); - address[10..30].copy_from_slice(&address_keccak[10..30]); - address[30..32].copy_from_slice(&sender_address_bytes[30..32]); - - self.new_deployed_address = Some(Address::from(address)); - - self - } - - fn process_new_issued_token_identifier(mut self) -> Self { - for scr in self.api_scrs.iter() { - if scr.sender.to_bech32_string().unwrap() != ESDTSystemSCAddress.to_bech32_string() { - continue; - } - - let Some(prev_tx) = self.api_scrs.iter().find(|e| e.hash == scr.prev_tx_hash) else { - continue; - }; - - let is_issue_fungible = prev_tx.data.starts_with("issue@"); - let is_issue_semi_fungible = prev_tx.data.starts_with("issueSemiFungible@"); - let is_issue_non_fungible = prev_tx.data.starts_with("issueNonFungible@"); - let is_register_meta_esdt = prev_tx.data.starts_with("registerMetaESDT@"); - let is_register_and_set_all_roles_esdt = - prev_tx.data.starts_with("registerAndSetAllRoles@"); - - if !is_issue_fungible - && !is_issue_semi_fungible - && !is_issue_non_fungible - && !is_register_meta_esdt - && !is_register_and_set_all_roles_esdt - { - continue; - } - - if scr.data.starts_with("ESDTTransfer@") { - let encoded_tid = scr.data.split('@').nth(1); - if encoded_tid.is_none() { - return self; - } - - self.new_issued_token_identifier = - Some(String::from_utf8(hex::decode(encoded_tid.unwrap()).unwrap()).unwrap()); - - break; - } else if scr.data.starts_with("@00@") { - let encoded_tid = scr.data.split('@').nth(2); - if encoded_tid.is_none() { - return self; - } - - self.new_issued_token_identifier = - Some(String::from_utf8(hex::decode(encoded_tid.unwrap()).unwrap()).unwrap()); - - break; - } - } - - self - } - - fn find_log(&self, log_identifier: &str) -> Option<&Events> { - if let Some(logs) = &self.api_logs { - logs.events - .iter() - .find(|event| event.identifier == log_identifier) - } else { - None - } - } } diff --git a/framework/scenario/src/scenario/model/transaction/tx_response_status.rs b/framework/scenario/src/scenario/model/transaction/tx_response_status.rs index 5c9d057615..e7902047c6 100644 --- a/framework/scenario/src/scenario/model/transaction/tx_response_status.rs +++ b/framework/scenario/src/scenario/model/transaction/tx_response_status.rs @@ -9,7 +9,7 @@ pub struct TxResponseStatus { impl TxResponseStatus { /// Creates a [`TxResponseStatus`] - pub(crate) fn new(status: u64, message: &str) -> Self { + pub fn new(status: u64, message: &str) -> Self { Self { status, message: message.to_string(), @@ -17,7 +17,7 @@ impl TxResponseStatus { } /// Creates a [`TxResponseStatus`] that signals an error. - pub(crate) fn signal_error(message: &str) -> Self { + pub fn signal_error(message: &str) -> Self { Self::new(4, message) } diff --git a/framework/scenario/src/scenario/model/transaction/tx_response_utils.rs b/framework/scenario/src/scenario/model/transaction/tx_response_utils.rs deleted file mode 100644 index 36c86e920a..0000000000 --- a/framework/scenario/src/scenario/model/transaction/tx_response_utils.rs +++ /dev/null @@ -1,35 +0,0 @@ -use multiversx_sdk::data::transaction::ApiSmartContractResult; - -/// Checks for invalid topics. -pub fn process_topics_error(topics: Option<&Vec>) -> Option { - if topics.is_none() { - return Some("missing topics".to_string()); - } - - let topics = topics.unwrap(); - if topics.len() != 2 { - Some(format!( - "expected to have 2 topics, found {} instead", - topics.len() - )) - } else { - None - } -} - -/// Decodes the data of a smart contract result. -pub fn decode_scr_data_or_panic(data: &str) -> Vec> { - let mut split = data.split('@'); - let _ = split.next().expect("SCR data should start with '@'"); - let result_code = split.next().expect("missing result code"); - assert_eq!(result_code, "6f6b", "result code is not 'ok'"); - - split - .map(|encoded_arg| hex::decode(encoded_arg).expect("error hex-decoding result")) - .collect() -} - -/// Checks if the given smart contract result is an out smart contract result. -pub fn is_out_scr(scr: &&ApiSmartContractResult) -> bool { - scr.nonce != 0 && scr.data.starts_with('@') -} diff --git a/framework/snippets/Cargo.toml b/framework/snippets/Cargo.toml index 495e98425e..4d4b3d4917 100644 --- a/framework/snippets/Cargo.toml +++ b/framework/snippets/Cargo.toml @@ -32,3 +32,6 @@ path = "../../sdk/scenario-format" [dependencies.multiversx-sdk] version = "=0.4.1" path = "../../sdk/core" + +[dev-dependencies] +serde_json = "1.0" diff --git a/framework/snippets/src/account_tool.rs b/framework/snippets/src/account_tool.rs index db1f14b4af..70eaafdace 100644 --- a/framework/snippets/src/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -10,7 +10,7 @@ use multiversx_sdk::{ use std::collections::{BTreeMap, HashMap}; /// Called directly from CLI, from `sc-meta`. -/// +/// /// Retrieves an account data via the API, /// then formats it as a scenario set state step. pub async fn print_account_as_scenario_set_state( diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index d01e4af590..0e2a800596 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -1,9 +1,9 @@ -use crate::{address_h256_to_erdrs, mandos_to_erdrs_address, Interactor}; +use crate::{address_h256_to_erdrs, mandos_to_erdrs_address, tx_response_from_network, Interactor}; use log::info; use multiversx_sc_scenario::{ api::StaticApi, scenario::ScenarioRunner, - scenario_model::{ScCallStep, SetStateStep, TxCall, TxResponse}, + scenario_model::{ScCallStep, SetStateStep, TxCall}, }; use multiversx_sdk::{data::transaction::Transaction, utils::base64_encode}; @@ -16,7 +16,7 @@ impl Interactor { let tx_hash = self.launch_sc_call(sc_call_step).await; let tx = self.retrieve_tx_on_network(tx_hash.clone()).await; - sc_call_step.save_response(TxResponse::from_network_tx(tx)); + sc_call_step.save_response(tx_response_from_network::from_network_tx(tx)); if let Some(token_identifier) = sc_call_step.response().new_issued_token_identifier.clone() { diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index 87d17dff7f..f92da9c070 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -1,9 +1,9 @@ -use crate::{mandos_to_erdrs_address, Interactor}; +use crate::{mandos_to_erdrs_address, tx_response_from_network, Interactor}; use log::info; use multiversx_sc_scenario::{ imports::Bech32Address, mandos_system::ScenarioRunner, - scenario_model::{ScDeployStep, SetStateStep, TxResponse}, + scenario_model::{ScDeployStep, SetStateStep}, }; use multiversx_sdk::{ data::{address::Address as ErdrsAddress, transaction::Transaction}, @@ -57,7 +57,7 @@ impl Interactor { let addr = sc_deploy_step.tx.from.clone(); let nonce = tx.nonce; - sc_deploy_step.save_response(TxResponse::from_network_tx(tx)); + sc_deploy_step.save_response(tx_response_from_network::from_network_tx(tx)); let deploy_address = sc_deploy_step .response() diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index 07c5789d8c..ae20ca432a 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -7,6 +7,7 @@ mod interactor_sender; mod interactor_tx; mod multi; pub mod test_wallets; +pub mod tx_response_from_network; pub use env_logger; pub use hex; diff --git a/framework/snippets/src/multi/interactor_multi_sc_exec.rs b/framework/snippets/src/multi/interactor_multi_sc_exec.rs index d99df9c807..d827f09ddd 100644 --- a/framework/snippets/src/multi/interactor_multi_sc_exec.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_exec.rs @@ -1,6 +1,5 @@ use super::interactor_multi_sc_process::{update_nonces_and_sign_tx, SenderSet, Txs}; -use crate::{Interactor, InteractorStep, StepBuffer}; -use multiversx_sc_scenario::scenario_model::TxResponse; +use crate::{tx_response_from_network, Interactor, InteractorStep, StepBuffer}; use multiversx_sdk::data::transaction::Transaction; impl Interactor { @@ -16,7 +15,9 @@ impl Interactor { let results = self.process_txs(txs).await; for (i, sc_call_step) in buffer.refs.iter_mut().enumerate() { - sc_call_step.set_response(TxResponse::from_network_tx(results.get(i).unwrap().clone())); + sc_call_step.set_response(tx_response_from_network::from_network_tx( + results.get(i).unwrap().clone(), + )); } for step in buffer.refs.iter_mut() { diff --git a/framework/snippets/src/tx_response_from_network.rs b/framework/snippets/src/tx_response_from_network.rs new file mode 100644 index 0000000000..93a7a40ae5 --- /dev/null +++ b/framework/snippets/src/tx_response_from_network.rs @@ -0,0 +1,228 @@ +use multiversx_sc_scenario::{ + imports::{Address, ESDTSystemSCAddress}, + multiversx_chain_vm::crypto_functions::keccak256, + scenario_model::{TxResponse, TxResponseStatus}, +}; +use multiversx_sdk::{ + data::transaction::{ApiSmartContractResult, Events, TransactionOnNetwork}, + utils::base64_decode, +}; + +const SC_DEPLOY_PROCESSING_TYPE: &str = "SCDeployment"; +const LOG_IDENTIFIER_SIGNAL_ERROR: &str = "signalError"; + +/// Creates a [`TxResponse`] from a [`TransactionOnNetwork`]. +pub fn from_network_tx(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; + } + + process( + response, + tx.sender.to_bytes(), + tx.nonce, + tx.processing_type_on_destination, + ) +} + +fn process_signal_error(tx_response: &TxResponse) -> TxResponseStatus { + if let Some(event) = find_log(tx_response, LOG_IDENTIFIER_SIGNAL_ERROR) { + let topics = event.topics.as_ref(); + if let Some(error) = process_topics_error(topics) { + return TxResponseStatus::signal_error(&error); + } + + let error_raw = base64_decode(topics.unwrap().get(1).unwrap()); + let error = String::from_utf8(error_raw).unwrap(); + return TxResponseStatus::signal_error(&error); + } + + TxResponseStatus::default() +} + +fn process( + mut tx_response: TxResponse, + sender_address: [u8; 32], + nonce: u64, + processing_type_on_destination: String, +) -> TxResponse { + tx_response = process_out(tx_response); + + tx_response = process_new_deployed_address( + tx_response, + sender_address, + nonce, + processing_type_on_destination, + ); + tx_response = process_new_issued_token_identifier(tx_response); + tx_response +} + +fn process_out(mut tx_response: TxResponse) -> TxResponse { + let out_scr = tx_response.api_scrs.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 + } + + tx_response +} + +fn process_out_from_log(tx_response: &TxResponse) -> Option>> { + if let Some(logs) = &tx_response.api_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(); + + if decoded_data.starts_with('@') { + let out = decode_scr_data_or_panic(decoded_data.as_str()); + return Some(out); + } + } + } + + None + }) + } else { + None + } +} + +fn process_new_deployed_address( + mut tx_response: TxResponse, + sender_address_bytes: [u8; 32], + nonce: u64, + processing_type_on_destination: String, +) -> TxResponse { + if processing_type_on_destination != SC_DEPLOY_PROCESSING_TYPE { + return tx_response; + } + + let sender_nonce_bytes = nonce.to_le_bytes(); + let mut bytes_to_hash: Vec = Vec::new(); + bytes_to_hash.extend_from_slice(&sender_address_bytes); + bytes_to_hash.extend_from_slice(&sender_nonce_bytes); + + let address_keccak = keccak256(&bytes_to_hash); + + let mut address = [0u8; 32]; + + address[0..8].copy_from_slice(&[0u8; 8]); + address[8..10].copy_from_slice(&[5, 0]); + 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)); + + tx_response +} + +fn process_new_issued_token_identifier(mut tx_response: TxResponse) -> TxResponse { + for scr in tx_response.api_scrs.iter() { + if scr.sender.to_bech32_string().unwrap() != ESDTSystemSCAddress.to_bech32_string() { + continue; + } + + let Some(prev_tx) = tx_response + .api_scrs + .iter() + .find(|e| e.hash == scr.prev_tx_hash) + else { + continue; + }; + + let is_issue_fungible = prev_tx.data.starts_with("issue@"); + let is_issue_semi_fungible = prev_tx.data.starts_with("issueSemiFungible@"); + let is_issue_non_fungible = prev_tx.data.starts_with("issueNonFungible@"); + let is_register_meta_esdt = prev_tx.data.starts_with("registerMetaESDT@"); + let is_register_and_set_all_roles_esdt = + prev_tx.data.starts_with("registerAndSetAllRoles@"); + + if !is_issue_fungible + && !is_issue_semi_fungible + && !is_issue_non_fungible + && !is_register_meta_esdt + && !is_register_and_set_all_roles_esdt + { + continue; + } + + if scr.data.starts_with("ESDTTransfer@") { + let encoded_tid = scr.data.split('@').nth(1); + if encoded_tid.is_none() { + return tx_response; + } + + tx_response.new_issued_token_identifier = + Some(String::from_utf8(hex::decode(encoded_tid.unwrap()).unwrap()).unwrap()); + + break; + } else if scr.data.starts_with("@00@") { + let encoded_tid = scr.data.split('@').nth(2); + if encoded_tid.is_none() { + return tx_response; + } + + tx_response.new_issued_token_identifier = + Some(String::from_utf8(hex::decode(encoded_tid.unwrap()).unwrap()).unwrap()); + + break; + } + } + + tx_response +} + +fn find_log<'a>(tx_response: &'a TxResponse, log_identifier: &str) -> Option<&'a Events> { + if let Some(logs) = &tx_response.api_logs { + logs.events + .iter() + .find(|event| event.identifier == log_identifier) + } else { + None + } +} + +/// Checks for invalid topics. +pub fn process_topics_error(topics: Option<&Vec>) -> Option { + if topics.is_none() { + return Some("missing topics".to_string()); + } + + let topics = topics.unwrap(); + if topics.len() != 2 { + Some(format!( + "expected to have 2 topics, found {} instead", + topics.len() + )) + } else { + None + } +} + +/// Decodes the data of a smart contract result. +pub fn decode_scr_data_or_panic(data: &str) -> Vec> { + let mut split = data.split('@'); + let _ = split.next().expect("SCR data should start with '@'"); + let result_code = split.next().expect("missing result code"); + assert_eq!(result_code, "6f6b", "result code is not 'ok'"); + + split + .map(|encoded_arg| hex::decode(encoded_arg).expect("error hex-decoding result")) + .collect() +} + +/// Checks if the given smart contract result is an out smart contract result. +pub fn is_out_scr(scr: &&ApiSmartContractResult) -> bool { + scr.nonce != 0 && scr.data.starts_with('@') +} diff --git a/framework/scenario/tests/test_tx_deployed_address.rs b/framework/snippets/tests/test_tx_deployed_address.rs similarity index 99% rename from framework/scenario/tests/test_tx_deployed_address.rs rename to framework/snippets/tests/test_tx_deployed_address.rs index b040ace618..f7a4b8e25e 100644 --- a/framework/scenario/tests/test_tx_deployed_address.rs +++ b/framework/snippets/tests/test_tx_deployed_address.rs @@ -1,5 +1,5 @@ -use multiversx_sc::types::Address; -use multiversx_sc_scenario::scenario_model::TxResponse; +use multiversx_sc_scenario::imports::Address; +use multiversx_sc_snippets::tx_response_from_network; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -53,7 +53,7 @@ fn test_deployed_address() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let opt_address = tx_response.new_deployed_address.map(|e| { multiversx_sdk::data::address::Address::from_bytes(*e.as_array()) .to_bech32_string() @@ -124,7 +124,7 @@ fn test_deployed_address_should_be_none_if_not_a_sc_deployment_tx() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let opt_address = tx_response.new_deployed_address; let expected: Option
= None; diff --git a/framework/scenario/tests/test_tx_issued_token_identifier.rs b/framework/snippets/tests/test_tx_issued_token_identifier.rs similarity index 99% rename from framework/scenario/tests/test_tx_issued_token_identifier.rs rename to framework/snippets/tests/test_tx_issued_token_identifier.rs index 2d0ccffa84..71859fddf0 100644 --- a/framework/scenario/tests/test_tx_issued_token_identifier.rs +++ b/framework/snippets/tests/test_tx_issued_token_identifier.rs @@ -1,4 +1,4 @@ -use multiversx_sc_scenario::scenario_model::TxResponse; +use multiversx_sc_snippets::tx_response_from_network; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -203,7 +203,7 @@ fn test_process_issued_token_identifier_fungible() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Option = Some("EGLDMEX-95c6d5".to_string()); @@ -362,7 +362,7 @@ fn test_process_issued_token_identifier_semi_fungible() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Option = Some("DOPETEST-77200c".to_string()); @@ -618,7 +618,7 @@ fn test_process_issued_token_identifier_non_fungible() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Option = Some("GEN-868593".to_string()); @@ -919,7 +919,7 @@ fn test_process_issued_token_identifier_meta_esdt() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Option = Some("AVASH-7d8b5d".to_string()); @@ -1152,7 +1152,7 @@ fn test_set_special_roles_should_not_process_issued_token_identifier() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Option = None; @@ -1414,7 +1414,7 @@ fn test_multisig_issue_nft_and_set_all_roles() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected = Some("TESTCOLL1-5aa80c".to_string()); diff --git a/framework/scenario/tests/test_tx_multi_contract_sc_result.rs b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs similarity index 98% rename from framework/scenario/tests/test_tx_multi_contract_sc_result.rs rename to framework/snippets/tests/test_tx_multi_contract_sc_result.rs index 8d45dfff0c..43cd750ba3 100644 --- a/framework/scenario/tests/test_tx_multi_contract_sc_result.rs +++ b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs @@ -1,4 +1,4 @@ -use multiversx_sc_scenario::scenario_model::TxResponse; +use multiversx_sc_snippets::tx_response_from_network; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -79,7 +79,7 @@ fn test_with_multi_contract_same_shard_tx_that_has_no_sc_result() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Vec> = vec![ hex::decode("0a").unwrap(), @@ -211,7 +211,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_no_callback() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Vec> = vec![]; @@ -340,7 +340,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_non_returning_callback() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Vec> = vec![]; @@ -469,7 +469,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_returning_callback() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Vec> = vec![]; diff --git a/framework/scenario/tests/test_tx_multiple_sc_results.rs b/framework/snippets/tests/test_tx_multiple_sc_results.rs similarity index 98% rename from framework/scenario/tests/test_tx_multiple_sc_results.rs rename to framework/snippets/tests/test_tx_multiple_sc_results.rs index 6322b0a7bb..b56e85b06c 100644 --- a/framework/scenario/tests/test_tx_multiple_sc_results.rs +++ b/framework/snippets/tests/test_tx_multiple_sc_results.rs @@ -1,4 +1,4 @@ -use multiversx_sc_scenario::scenario_model::{is_out_scr, TxResponse}; +use multiversx_sc_snippets::tx_response_from_network::{self, is_out_scr}; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -281,7 +281,7 @@ fn test_transaction_multiple_sc_results() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); assert_eq!(tx_response.api_scrs.len(), 4usize); assert!(is_out_scr(&tx_response.api_scrs.get(2).unwrap())); } diff --git a/framework/scenario/tests/test_tx_sc_result.rs b/framework/snippets/tests/test_tx_sc_result.rs similarity index 98% rename from framework/scenario/tests/test_tx_sc_result.rs rename to framework/snippets/tests/test_tx_sc_result.rs index 561712300c..a86371aad0 100644 --- a/framework/scenario/tests/test_tx_sc_result.rs +++ b/framework/snippets/tests/test_tx_sc_result.rs @@ -1,4 +1,4 @@ -use multiversx_sc_scenario::scenario_model::TxResponse; +use multiversx_sc_snippets::tx_response_from_network; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -251,7 +251,7 @@ fn test_with_tx_that_has_sc_result() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Vec> = vec![ hex::decode("0000000c5745474c442d64376336626200000000000000000000000803856446ff9a304b") @@ -339,7 +339,7 @@ fn test_with_tx_that_has_no_sc_result() { .data .unwrap() .transaction; - let tx_response = TxResponse::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::from_network_tx(tx_on_network); let expected: Vec> = vec![ hex::decode("0a").unwrap(), From 9dc06b6adb2d01dbbcd336887a3a7aec0e9d1d76 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 29 May 2024 21:42:49 +0300 Subject: [PATCH 2/3] tx response parser refactor --- .../interactor_scenario/interactor_sc_call.rs | 2 +- .../interactor_sc_deploy.rs | 2 +- .../src/multi/interactor_multi_sc_exec.rs | 2 +- .../snippets/src/tx_response_from_network.rs | 42 ++++++++----------- .../tests/test_tx_deployed_address.rs | 4 +- .../tests/test_tx_issued_token_identifier.rs | 12 +++--- .../tests/test_tx_multi_contract_sc_result.rs | 8 ++-- .../tests/test_tx_multiple_sc_results.rs | 2 +- framework/snippets/tests/test_tx_sc_result.rs | 4 +- 9 files changed, 36 insertions(+), 42 deletions(-) diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index 0e2a800596..930ff054fc 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -16,7 +16,7 @@ impl Interactor { let tx_hash = self.launch_sc_call(sc_call_step).await; let tx = self.retrieve_tx_on_network(tx_hash.clone()).await; - sc_call_step.save_response(tx_response_from_network::from_network_tx(tx)); + sc_call_step.save_response(tx_response_from_network::parse_tx_response(tx)); if let Some(token_identifier) = sc_call_step.response().new_issued_token_identifier.clone() { diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index f92da9c070..e7d391a190 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -57,7 +57,7 @@ impl Interactor { let addr = sc_deploy_step.tx.from.clone(); let nonce = tx.nonce; - sc_deploy_step.save_response(tx_response_from_network::from_network_tx(tx)); + sc_deploy_step.save_response(tx_response_from_network::parse_tx_response(tx)); let deploy_address = sc_deploy_step .response() diff --git a/framework/snippets/src/multi/interactor_multi_sc_exec.rs b/framework/snippets/src/multi/interactor_multi_sc_exec.rs index d827f09ddd..c592fd21d8 100644 --- a/framework/snippets/src/multi/interactor_multi_sc_exec.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_exec.rs @@ -15,7 +15,7 @@ impl Interactor { let results = self.process_txs(txs).await; for (i, sc_call_step) in buffer.refs.iter_mut().enumerate() { - sc_call_step.set_response(tx_response_from_network::from_network_tx( + sc_call_step.set_response(tx_response_from_network::parse_tx_response( results.get(i).unwrap().clone(), )); } diff --git a/framework/snippets/src/tx_response_from_network.rs b/framework/snippets/src/tx_response_from_network.rs index 93a7a40ae5..f7f0b962df 100644 --- a/framework/snippets/src/tx_response_from_network.rs +++ b/framework/snippets/src/tx_response_from_network.rs @@ -12,7 +12,7 @@ const SC_DEPLOY_PROCESSING_TYPE: &str = "SCDeployment"; const LOG_IDENTIFIER_SIGNAL_ERROR: &str = "signalError"; /// Creates a [`TxResponse`] from a [`TransactionOnNetwork`]. -pub fn from_network_tx(tx: TransactionOnNetwork) -> TxResponse { +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, @@ -25,11 +25,13 @@ pub fn from_network_tx(tx: TransactionOnNetwork) -> TxResponse { } process( - response, + &mut response, tx.sender.to_bytes(), tx.nonce, tx.processing_type_on_destination, - ) + ); + + response } fn process_signal_error(tx_response: &TxResponse) -> TxResponseStatus { @@ -48,33 +50,29 @@ fn process_signal_error(tx_response: &TxResponse) -> TxResponseStatus { } fn process( - mut tx_response: TxResponse, + tx_response: &mut TxResponse, sender_address: [u8; 32], nonce: u64, processing_type_on_destination: String, -) -> TxResponse { - tx_response = process_out(tx_response); - - tx_response = process_new_deployed_address( +) { + process_out(tx_response); + process_new_deployed_address( tx_response, sender_address, nonce, processing_type_on_destination, ); - tx_response = process_new_issued_token_identifier(tx_response); - tx_response + process_new_issued_token_identifier(tx_response); } -fn process_out(mut tx_response: TxResponse) -> TxResponse { +fn process_out(tx_response: &mut TxResponse) { let out_scr = tx_response.api_scrs.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) { + } else if let Some(data) = process_out_from_log(tx_response) { tx_response.out = data } - - tx_response } fn process_out_from_log(tx_response: &TxResponse) -> Option>> { @@ -99,13 +97,13 @@ fn process_out_from_log(tx_response: &TxResponse) -> Option>> { } fn process_new_deployed_address( - mut tx_response: TxResponse, + tx_response: &mut TxResponse, sender_address_bytes: [u8; 32], nonce: u64, processing_type_on_destination: String, -) -> TxResponse { +) { if processing_type_on_destination != SC_DEPLOY_PROCESSING_TYPE { - return tx_response; + return; } let sender_nonce_bytes = nonce.to_le_bytes(); @@ -123,11 +121,9 @@ fn process_new_deployed_address( address[30..32].copy_from_slice(&sender_address_bytes[30..32]); tx_response.new_deployed_address = Some(Address::from(address)); - - tx_response } -fn process_new_issued_token_identifier(mut tx_response: TxResponse) -> TxResponse { +fn process_new_issued_token_identifier(tx_response: &mut TxResponse) { for scr in tx_response.api_scrs.iter() { if scr.sender.to_bech32_string().unwrap() != ESDTSystemSCAddress.to_bech32_string() { continue; @@ -160,7 +156,7 @@ fn process_new_issued_token_identifier(mut tx_response: TxResponse) -> TxRespons if scr.data.starts_with("ESDTTransfer@") { let encoded_tid = scr.data.split('@').nth(1); if encoded_tid.is_none() { - return tx_response; + return; } tx_response.new_issued_token_identifier = @@ -170,7 +166,7 @@ fn process_new_issued_token_identifier(mut tx_response: TxResponse) -> TxRespons } else if scr.data.starts_with("@00@") { let encoded_tid = scr.data.split('@').nth(2); if encoded_tid.is_none() { - return tx_response; + return; } tx_response.new_issued_token_identifier = @@ -179,8 +175,6 @@ fn process_new_issued_token_identifier(mut tx_response: TxResponse) -> TxRespons break; } } - - tx_response } fn find_log<'a>(tx_response: &'a TxResponse, log_identifier: &str) -> Option<&'a Events> { diff --git a/framework/snippets/tests/test_tx_deployed_address.rs b/framework/snippets/tests/test_tx_deployed_address.rs index f7a4b8e25e..905da88568 100644 --- a/framework/snippets/tests/test_tx_deployed_address.rs +++ b/framework/snippets/tests/test_tx_deployed_address.rs @@ -53,7 +53,7 @@ fn test_deployed_address() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let opt_address = tx_response.new_deployed_address.map(|e| { multiversx_sdk::data::address::Address::from_bytes(*e.as_array()) .to_bech32_string() @@ -124,7 +124,7 @@ fn test_deployed_address_should_be_none_if_not_a_sc_deployment_tx() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let opt_address = tx_response.new_deployed_address; let expected: Option
= None; diff --git a/framework/snippets/tests/test_tx_issued_token_identifier.rs b/framework/snippets/tests/test_tx_issued_token_identifier.rs index 71859fddf0..a1a235c06f 100644 --- a/framework/snippets/tests/test_tx_issued_token_identifier.rs +++ b/framework/snippets/tests/test_tx_issued_token_identifier.rs @@ -203,7 +203,7 @@ fn test_process_issued_token_identifier_fungible() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Option = Some("EGLDMEX-95c6d5".to_string()); @@ -362,7 +362,7 @@ fn test_process_issued_token_identifier_semi_fungible() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Option = Some("DOPETEST-77200c".to_string()); @@ -618,7 +618,7 @@ fn test_process_issued_token_identifier_non_fungible() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Option = Some("GEN-868593".to_string()); @@ -919,7 +919,7 @@ fn test_process_issued_token_identifier_meta_esdt() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Option = Some("AVASH-7d8b5d".to_string()); @@ -1152,7 +1152,7 @@ fn test_set_special_roles_should_not_process_issued_token_identifier() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Option = None; @@ -1414,7 +1414,7 @@ fn test_multisig_issue_nft_and_set_all_roles() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected = Some("TESTCOLL1-5aa80c".to_string()); diff --git a/framework/snippets/tests/test_tx_multi_contract_sc_result.rs b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs index 43cd750ba3..2812d50844 100644 --- a/framework/snippets/tests/test_tx_multi_contract_sc_result.rs +++ b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs @@ -79,7 +79,7 @@ fn test_with_multi_contract_same_shard_tx_that_has_no_sc_result() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Vec> = vec![ hex::decode("0a").unwrap(), @@ -211,7 +211,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_no_callback() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Vec> = vec![]; @@ -340,7 +340,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_non_returning_callback() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Vec> = vec![]; @@ -469,7 +469,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_returning_callback() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Vec> = vec![]; diff --git a/framework/snippets/tests/test_tx_multiple_sc_results.rs b/framework/snippets/tests/test_tx_multiple_sc_results.rs index b56e85b06c..b3107db82c 100644 --- a/framework/snippets/tests/test_tx_multiple_sc_results.rs +++ b/framework/snippets/tests/test_tx_multiple_sc_results.rs @@ -281,7 +281,7 @@ fn test_transaction_multiple_sc_results() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::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())); } diff --git a/framework/snippets/tests/test_tx_sc_result.rs b/framework/snippets/tests/test_tx_sc_result.rs index a86371aad0..69513b7ba5 100644 --- a/framework/snippets/tests/test_tx_sc_result.rs +++ b/framework/snippets/tests/test_tx_sc_result.rs @@ -251,7 +251,7 @@ fn test_with_tx_that_has_sc_result() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Vec> = vec![ hex::decode("0000000c5745474c442d64376336626200000000000000000000000803856446ff9a304b") @@ -339,7 +339,7 @@ fn test_with_tx_that_has_no_sc_result() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::from_network_tx(tx_on_network); + let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); let expected: Vec> = vec![ hex::decode("0a").unwrap(), From 113346d2f5b781b7bbccf8526e2eacf6c0210284 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 29 May 2024 21:49:22 +0300 Subject: [PATCH 3/3] tx response parser refactor --- .../src/interactor_scenario/interactor_sc_call.rs | 4 ++-- .../interactor_scenario/interactor_sc_deploy.rs | 4 ++-- framework/snippets/src/lib.rs | 2 +- .../snippets/src/multi/interactor_multi_sc_exec.rs | 4 ++-- ...esponse_from_network.rs => network_response.rs} | 0 .../snippets/tests/test_tx_deployed_address.rs | 6 +++--- .../tests/test_tx_issued_token_identifier.rs | 14 +++++++------- .../tests/test_tx_multi_contract_sc_result.rs | 10 +++++----- .../snippets/tests/test_tx_multiple_sc_results.rs | 4 ++-- framework/snippets/tests/test_tx_sc_result.rs | 6 +++--- 10 files changed, 27 insertions(+), 27 deletions(-) rename framework/snippets/src/{tx_response_from_network.rs => network_response.rs} (100%) diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index 930ff054fc..061005ea13 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -1,4 +1,4 @@ -use crate::{address_h256_to_erdrs, mandos_to_erdrs_address, tx_response_from_network, Interactor}; +use crate::{address_h256_to_erdrs, mandos_to_erdrs_address, network_response, Interactor}; use log::info; use multiversx_sc_scenario::{ api::StaticApi, @@ -16,7 +16,7 @@ impl Interactor { let tx_hash = self.launch_sc_call(sc_call_step).await; let tx = self.retrieve_tx_on_network(tx_hash.clone()).await; - sc_call_step.save_response(tx_response_from_network::parse_tx_response(tx)); + sc_call_step.save_response(network_response::parse_tx_response(tx)); if let Some(token_identifier) = sc_call_step.response().new_issued_token_identifier.clone() { diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index e7d391a190..529efc38bb 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -1,4 +1,4 @@ -use crate::{mandos_to_erdrs_address, tx_response_from_network, Interactor}; +use crate::{mandos_to_erdrs_address, network_response, Interactor}; use log::info; use multiversx_sc_scenario::{ imports::Bech32Address, @@ -57,7 +57,7 @@ impl Interactor { let addr = sc_deploy_step.tx.from.clone(); let nonce = tx.nonce; - sc_deploy_step.save_response(tx_response_from_network::parse_tx_response(tx)); + sc_deploy_step.save_response(network_response::parse_tx_response(tx)); let deploy_address = sc_deploy_step .response() diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index ae20ca432a..6240a9f152 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -6,8 +6,8 @@ mod interactor_scenario; mod interactor_sender; mod interactor_tx; mod multi; +pub mod network_response; pub mod test_wallets; -pub mod tx_response_from_network; pub use env_logger; pub use hex; diff --git a/framework/snippets/src/multi/interactor_multi_sc_exec.rs b/framework/snippets/src/multi/interactor_multi_sc_exec.rs index c592fd21d8..8ac6a4eba9 100644 --- a/framework/snippets/src/multi/interactor_multi_sc_exec.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_exec.rs @@ -1,5 +1,5 @@ use super::interactor_multi_sc_process::{update_nonces_and_sign_tx, SenderSet, Txs}; -use crate::{tx_response_from_network, Interactor, InteractorStep, StepBuffer}; +use crate::{network_response, Interactor, InteractorStep, StepBuffer}; use multiversx_sdk::data::transaction::Transaction; impl Interactor { @@ -15,7 +15,7 @@ impl Interactor { let results = self.process_txs(txs).await; for (i, sc_call_step) in buffer.refs.iter_mut().enumerate() { - sc_call_step.set_response(tx_response_from_network::parse_tx_response( + sc_call_step.set_response(network_response::parse_tx_response( results.get(i).unwrap().clone(), )); } diff --git a/framework/snippets/src/tx_response_from_network.rs b/framework/snippets/src/network_response.rs similarity index 100% rename from framework/snippets/src/tx_response_from_network.rs rename to framework/snippets/src/network_response.rs diff --git a/framework/snippets/tests/test_tx_deployed_address.rs b/framework/snippets/tests/test_tx_deployed_address.rs index 905da88568..377a0e779f 100644 --- a/framework/snippets/tests/test_tx_deployed_address.rs +++ b/framework/snippets/tests/test_tx_deployed_address.rs @@ -1,5 +1,5 @@ use multiversx_sc_scenario::imports::Address; -use multiversx_sc_snippets::tx_response_from_network; +use multiversx_sc_snippets::network_response; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -53,7 +53,7 @@ fn test_deployed_address() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let opt_address = tx_response.new_deployed_address.map(|e| { multiversx_sdk::data::address::Address::from_bytes(*e.as_array()) .to_bech32_string() @@ -124,7 +124,7 @@ fn test_deployed_address_should_be_none_if_not_a_sc_deployment_tx() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let opt_address = tx_response.new_deployed_address; let expected: Option
= None; diff --git a/framework/snippets/tests/test_tx_issued_token_identifier.rs b/framework/snippets/tests/test_tx_issued_token_identifier.rs index a1a235c06f..3539c4e757 100644 --- a/framework/snippets/tests/test_tx_issued_token_identifier.rs +++ b/framework/snippets/tests/test_tx_issued_token_identifier.rs @@ -1,4 +1,4 @@ -use multiversx_sc_snippets::tx_response_from_network; +use multiversx_sc_snippets::network_response; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -203,7 +203,7 @@ fn test_process_issued_token_identifier_fungible() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Option = Some("EGLDMEX-95c6d5".to_string()); @@ -362,7 +362,7 @@ fn test_process_issued_token_identifier_semi_fungible() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Option = Some("DOPETEST-77200c".to_string()); @@ -618,7 +618,7 @@ fn test_process_issued_token_identifier_non_fungible() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Option = Some("GEN-868593".to_string()); @@ -919,7 +919,7 @@ fn test_process_issued_token_identifier_meta_esdt() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Option = Some("AVASH-7d8b5d".to_string()); @@ -1152,7 +1152,7 @@ fn test_set_special_roles_should_not_process_issued_token_identifier() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Option = None; @@ -1414,7 +1414,7 @@ fn test_multisig_issue_nft_and_set_all_roles() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected = Some("TESTCOLL1-5aa80c".to_string()); diff --git a/framework/snippets/tests/test_tx_multi_contract_sc_result.rs b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs index 2812d50844..a9b64d8941 100644 --- a/framework/snippets/tests/test_tx_multi_contract_sc_result.rs +++ b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs @@ -1,4 +1,4 @@ -use multiversx_sc_snippets::tx_response_from_network; +use multiversx_sc_snippets::network_response; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -79,7 +79,7 @@ fn test_with_multi_contract_same_shard_tx_that_has_no_sc_result() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Vec> = vec![ hex::decode("0a").unwrap(), @@ -211,7 +211,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_no_callback() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Vec> = vec![]; @@ -340,7 +340,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_non_returning_callback() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Vec> = vec![]; @@ -469,7 +469,7 @@ fn test_with_multi_contract_cross_shard_tx_that_has_returning_callback() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Vec> = vec![]; diff --git a/framework/snippets/tests/test_tx_multiple_sc_results.rs b/framework/snippets/tests/test_tx_multiple_sc_results.rs index b3107db82c..25e6fc477a 100644 --- a/framework/snippets/tests/test_tx_multiple_sc_results.rs +++ b/framework/snippets/tests/test_tx_multiple_sc_results.rs @@ -1,4 +1,4 @@ -use multiversx_sc_snippets::tx_response_from_network::{self, is_out_scr}; +use multiversx_sc_snippets::network_response::{self, is_out_scr}; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -281,7 +281,7 @@ fn test_transaction_multiple_sc_results() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + 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())); } diff --git a/framework/snippets/tests/test_tx_sc_result.rs b/framework/snippets/tests/test_tx_sc_result.rs index 69513b7ba5..b3809eb2f8 100644 --- a/framework/snippets/tests/test_tx_sc_result.rs +++ b/framework/snippets/tests/test_tx_sc_result.rs @@ -1,4 +1,4 @@ -use multiversx_sc_snippets::tx_response_from_network; +use multiversx_sc_snippets::network_response; use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] @@ -251,7 +251,7 @@ fn test_with_tx_that_has_sc_result() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Vec> = vec![ hex::decode("0000000c5745474c442d64376336626200000000000000000000000803856446ff9a304b") @@ -339,7 +339,7 @@ fn test_with_tx_that_has_no_sc_result() { .data .unwrap() .transaction; - let tx_response = tx_response_from_network::parse_tx_response(tx_on_network); + let tx_response = network_response::parse_tx_response(tx_on_network); let expected: Vec> = vec![ hex::decode("0a").unwrap(),