From 5a57586c6700aa66d2db92b85039dbb7420c6419 Mon Sep 17 00:00:00 2001 From: quentin Date: Wed, 23 Oct 2024 16:51:04 +0200 Subject: [PATCH] handled "execution failed error signaled by smart contract" error in simulated calls --- executor/src/error/simulation.rs | 4 +++- executor/src/network/simulate.rs | 26 ++++++++++++++++++----- executor/src/utils/transaction/results.rs | 10 ++++++++- tester/core/tests/network_simulate.rs | 13 ++++++++++-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/executor/src/error/simulation.rs b/executor/src/error/simulation.rs index e856a6d..77dadb8 100644 --- a/executor/src/error/simulation.rs +++ b/executor/src/error/simulation.rs @@ -16,7 +16,9 @@ pub enum SimulationError { code: String }, - NoSmartContractResult + NoSmartContractResult, + SmartContractExecutionError { status: u64, message: String }, + CannotDecodeSmartContractResult } impl From for ExecutorError { diff --git a/executor/src/network/simulate.rs b/executor/src/network/simulate.rs index 8cccb30..8a76a11 100644 --- a/executor/src/network/simulate.rs +++ b/executor/src/network/simulate.rs @@ -11,12 +11,11 @@ use novax_request::gateway::client::GatewayClient; use crate::{ExecutorError, GatewayError, SimulationError, SimulationGatewayRequest, SimulationGatewayResponse, TransactionExecutor, TransactionOnNetwork, TransactionOnNetworkTransactionSmartContractResult}; use crate::call_result::CallResult; -use crate::error::transaction::TransactionError; use crate::network::models::simulate::request::SimulationGatewayRequestBody; use crate::network::utils::address::get_address_info; use crate::network::utils::network::get_network_config; use crate::utils::transaction::normalization::NormalizationInOut; -use crate::utils::transaction::results::find_smart_contract_result; +use crate::utils::transaction::results::{find_sc_error, find_smart_contract_result}; use crate::utils::transaction::token_transfer::TokenTransfer; /// Type alias for `BaseSimulationNetworkExecutor` with the `String` type as the generic `Client`. @@ -184,11 +183,28 @@ impl TransactionExecutor for BaseSimulationNetworkExecuto }) .collect(); - let mut raw_result = find_smart_contract_result(&Some(scrs), data.logs.as_ref())? - .unwrap_or_default(); + let opt_smart_contract_results = find_smart_contract_result(&Some(scrs), data.logs.as_ref())?; + + let mut raw_result = match opt_smart_contract_results { + None => { + if let Some(logs) = data.logs.as_ref() { + if let Ok(Some(error_log)) = find_sc_error(logs) { + return Err(SimulationError::SmartContractExecutionError { // TODO add tests for this + status: error_log.status, + message: error_log.message + }.into()); + } + } + + vec![] + } + Some(results) => { + results + } + }; let Ok(output_managed) = OutputManaged::multi_decode(&mut raw_result) else { - return Err(TransactionError::CannotDecodeSmartContractResult.into()) + return Err(SimulationError::CannotDecodeSmartContractResult.into()) }; let mut response = TransactionOnNetwork::default(); diff --git a/executor/src/utils/transaction/results.rs b/executor/src/utils/transaction/results.rs index 20afbbb..31b3fbe 100644 --- a/executor/src/utils/transaction/results.rs +++ b/executor/src/utils/transaction/results.rs @@ -5,6 +5,8 @@ use multiversx_sdk::utils::base64_decode; use crate::{ExecutorError, TransactionOnNetworkTransactionLogs, TransactionOnNetworkTransactionLogsEvents, TransactionOnNetworkTransactionSmartContractResult}; use crate::error::transaction::TransactionError; +const ERROR_SIGNALLED_BY_SMART_CONTRACT: &str = "error signalled by smartcontract"; + #[derive(Clone, Debug)] pub(crate) struct SmartContractError { pub status: u64, @@ -49,8 +51,14 @@ pub(crate) fn find_sc_error(logs: &TransactionOnNetworkTransactionLogs) -> Resul } let error = decode_topic(topics.get(1).unwrap())?; + let status = if error.contains(ERROR_SIGNALLED_BY_SMART_CONTRACT) { + 10 + } else { + 4 + }; + let result = SmartContractError { - status: 4, + status, message: error, }; return Ok(Some(result)); diff --git a/tester/core/tests/network_simulate.rs b/tester/core/tests/network_simulate.rs index 69036d6..ae7f6e6 100644 --- a/tester/core/tests/network_simulate.rs +++ b/tester/core/tests/network_simulate.rs @@ -10,7 +10,7 @@ use tokio::sync::Mutex; use novax::Address; use novax::errors::NovaXError; -use novax::executor::{BaseSimulationNetworkExecutor, SimulationNetworkExecutor, TokenTransfer}; +use novax::executor::{BaseSimulationNetworkExecutor, ExecutorError, SimulationError, SimulationNetworkExecutor, TokenTransfer}; use novax::tester::tester::TesterContract; use novax_request::error::request::RequestError; use novax_request::gateway::client::GatewayClient; @@ -362,7 +362,16 @@ async fn test_autoscale_zap_in_error_signaled_by_smart_contract() -> Result<(), .err() .unwrap(); - todo!(); + let expected = NovaXError::Executor( + ExecutorError::Simulation( + SimulationError::SmartContractExecutionError { + status: 10, + message: "error signalled by smartcontract".to_string() + } + ) + ); + + assert_eq!(result, expected); Ok(()) }