Skip to content

Commit

Permalink
Merge branch 'rc/v0.51' into feat/managed-decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed May 30, 2024
2 parents 0ffc965 + 7fe9f04 commit 4be9da5
Show file tree
Hide file tree
Showing 45 changed files with 1,177 additions and 810 deletions.
30 changes: 16 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions framework/scenario/src/scenario/model/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
186 changes: 4 additions & 182 deletions framework/scenario/src/scenario/model/transaction/tx_response.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Vec<u8>>) -> Self {
TxResponse {
Expand Down Expand Up @@ -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<Vec<Vec<u8>>> {
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<u8> = 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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(),
}
}

/// 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)
}

Expand Down

This file was deleted.

Loading

0 comments on commit 4be9da5

Please sign in to comment.