Skip to content

Commit

Permalink
added support for SCResults without a data field, avoid unnecessary p…
Browse files Browse the repository at this point in the history
…anic
  • Loading branch information
mihaicalinluca committed Sep 6, 2024
1 parent 49a82cb commit 2cb5e42
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 13 deletions.
37 changes: 25 additions & 12 deletions framework/snippets/src/network_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ fn process_success(tx: &TransactionOnNetwork) -> TxResponse {
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 {
decode_scr_data_or_panic(&out_scr.data)
} else {
process_out_from_log(tx).unwrap_or_default()
match out_scr {
Some(out_scr) => match &out_scr.data {
Some(data) => decode_scr_data_or_panic(data),
None => Vec::new(),
},
None => process_out_from_log(tx).unwrap_or_default(),
}
}

Expand Down Expand Up @@ -153,7 +155,10 @@ fn process_new_issued_token_identifier(tx: &TransactionOnNetwork) -> Option<Stri
.iter()
.find(|e| e.hash == scr.prev_tx_hash)
{
prev_tx.data.as_ref()
match &prev_tx.data {
Some(data) => data.as_ref(),
None => "",
}
} else if &scr.prev_tx_hash == tx.hash.as_ref().unwrap() {
&original_tx_data
} else {
Expand All @@ -176,12 +181,17 @@ fn process_new_issued_token_identifier(tx: &TransactionOnNetwork) -> Option<Stri
continue;
}

if scr.data.starts_with("ESDTTransfer@") {
let encoded_tid = scr.data.split('@').nth(1);
return Some(String::from_utf8(hex::decode(encoded_tid?).unwrap()).unwrap());
} else if scr.data.starts_with("@00@") || scr.data.starts_with("@6f6b@") {
let encoded_tid = scr.data.split('@').nth(2);
return Some(String::from_utf8(hex::decode(encoded_tid?).unwrap()).unwrap());
match &scr.data {
Some(data) => {
if data.starts_with("ESDTTransfer@") {
let encoded_tid = data.split('@').nth(1);
return Some(String::from_utf8(hex::decode(encoded_tid?).unwrap()).unwrap());
} else if data.starts_with("@00@") || data.starts_with("@6f6b@") {
let encoded_tid = data.split('@').nth(2);
return Some(String::from_utf8(hex::decode(encoded_tid?).unwrap()).unwrap());
}
},
None => return Some(String::new()),
}
}
None
Expand Down Expand Up @@ -228,5 +238,8 @@ pub fn decode_scr_data_or_panic(data: &str) -> Vec<Vec<u8>> {

/// 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('@')
match &scr.data {
Some(data) => scr.nonce != 0 && data.starts_with('@'),
None => scr.nonce != 0,
}
}
118 changes: 118 additions & 0 deletions framework/snippets/tests/test_tx_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use multiversx_sc_snippets::network_response;
use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork};

#[test]
fn test_tx_multiple_logs() {
let data = r#"
{
"data": {
"transaction": {
"type": "normal",
"processingTypeOnSource": "SCInvoking",
"processingTypeOnDestination": "SCInvoking",
"hash": "4c554d060e1b489d403759e445c4a4d80b0daa5a8eceafc7b9093eb8a7dd4b7a",
"nonce": 6768,
"round": 5269269,
"epoch": 2169,
"value": "0",
"receiver": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"sender": "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
"gasPrice": 1000000000,
"gasLimit": 30000000,
"gasUsed": 30000000,
"data": "cG9uZw==",
"signature": "dccb8bf68defef89e938e768fea872d34d6a1ba716813fe78c583233e418b6c800973bb353c61c1babf816bae2200c2ff24197e7c7748c007d72560e2ce16108",
"sourceShard": 1,
"destinationShard": 1,
"blockNonce": 5200682,
"blockHash": "b70f33fd98a8cd465cf1a679977d4c0c27f38db6a7634cab57d82e3fd8bd4841",
"notarizedAtSourceInMetaNonce": 5204138,
"NotarizedAtSourceInMetaHash": "1b908cfc413beb0e4d89812e2d4430d7cfbf67f7c65098aa476a2cdf2a892ac8",
"notarizedAtDestinationInMetaNonce": 5204138,
"notarizedAtDestinationInMetaHash": "1b908cfc413beb0e4d89812e2d4430d7cfbf67f7c65098aa476a2cdf2a892ac8",
"miniblockType": "TxBlock",
"miniblockHash": "3217265a72ce8cf40b900a8467b797610fb80eb92158143aac6e9f85e8177945",
"hyperblockNonce": 5204138,
"hyperblockHash": "1b908cfc413beb0e4d89812e2d4430d7cfbf67f7c65098aa476a2cdf2a892ac8",
"timestamp": 1725615614,
"smartContractResults": [
{
"hash": "66debb4f02a735f00bd7da565069f7d26412341d6fae56bbab9b98696c8701e9",
"nonce": 0,
"value": 1000000000000000,
"receiver": "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
"sender": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"prevTxHash": "4c554d060e1b489d403759e445c4a4d80b0daa5a8eceafc7b9093eb8a7dd4b7a",
"originalTxHash": "4c554d060e1b489d403759e445c4a4d80b0daa5a8eceafc7b9093eb8a7dd4b7a",
"gasLimit": 0,
"gasPrice": 1000000000,
"callType": 0,
"originalSender": "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
"operation": "transfer"
}
],
"logs": {
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"events": [
{
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"identifier": "transferValueOnly",
"topics": [
"A41+pMaAAA==",
"ATlHLv9ohncamC8wg9pdQh8kwpGB5jiIIo3IHKYNaeE="
],
"data": "RGlyZWN0Q2FsbA==",
"additionalData": [
"RGlyZWN0Q2FsbA==",
""
]
},
{
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"identifier": "writeLog",
"topics": [
"ATlHLv9ohncamC8wg9pdQh8kwpGB5jiIIo3IHKYNaeE=",
"QHRvbyBtdWNoIGdhcyBwcm92aWRlZCBmb3IgcHJvY2Vzc2luZzogZ2FzIHByb3ZpZGVkID0gMjk5NDQwMDAsIGdhcyB1c2VkID0gMjE3MTk0OA=="
],
"data": "QDZmNmI=",
"additionalData": [
"QDZmNmI="
]
},
{
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"identifier": "completedTxEvent",
"topics": [
"TFVNBg4bSJ1AN1nkRcSk2AsNqlqOzq/HuQk+uKfdS3o="
],
"data": null,
"additionalData": null
}
]
},
"status": "success",
"operation": "transfer",
"function": "pong",
"initiallyPaidFee": "355440000000000",
"fee": "355440000000000",
"chainID": "D",
"version": 1,
"options": 0
}
},
"error": "",
"code": "successful"
}"#;

let tx_on_network: TransactionOnNetwork = serde_json::from_str::<TransactionInfo>(data)
.unwrap()
.data
.unwrap()
.transaction;
let tx_response = network_response::parse_tx_response(tx_on_network);

assert!(tx_response.logs.len() == 3);
assert_eq!(tx_response.logs[0].endpoint, "transferValueOnly");
assert_eq!(tx_response.logs[1].endpoint, "writeLog");
assert_eq!(tx_response.logs[2].endpoint, "completedTxEvent");
}
113 changes: 113 additions & 0 deletions framework/snippets/tests/test_tx_sc_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,116 @@ fn test_with_tx_that_has_no_sc_result() {

assert_eq!(tx_response.out, expected)
}

#[test]
fn test_tx_sc_results_with_no_data() {
let data = r#"
{
"data": {
"transaction": {
"type": "normal",
"processingTypeOnSource": "SCInvoking",
"processingTypeOnDestination": "SCInvoking",
"hash": "4c554d060e1b489d403759e445c4a4d80b0daa5a8eceafc7b9093eb8a7dd4b7a",
"nonce": 6768,
"round": 5269269,
"epoch": 2169,
"value": "0",
"receiver": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"sender": "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
"gasPrice": 1000000000,
"gasLimit": 30000000,
"gasUsed": 30000000,
"data": "cG9uZw==",
"signature": "dccb8bf68defef89e938e768fea872d34d6a1ba716813fe78c583233e418b6c800973bb353c61c1babf816bae2200c2ff24197e7c7748c007d72560e2ce16108",
"sourceShard": 1,
"destinationShard": 1,
"blockNonce": 5200682,
"blockHash": "b70f33fd98a8cd465cf1a679977d4c0c27f38db6a7634cab57d82e3fd8bd4841",
"notarizedAtSourceInMetaNonce": 5204138,
"NotarizedAtSourceInMetaHash": "1b908cfc413beb0e4d89812e2d4430d7cfbf67f7c65098aa476a2cdf2a892ac8",
"notarizedAtDestinationInMetaNonce": 5204138,
"notarizedAtDestinationInMetaHash": "1b908cfc413beb0e4d89812e2d4430d7cfbf67f7c65098aa476a2cdf2a892ac8",
"miniblockType": "TxBlock",
"miniblockHash": "3217265a72ce8cf40b900a8467b797610fb80eb92158143aac6e9f85e8177945",
"hyperblockNonce": 5204138,
"hyperblockHash": "1b908cfc413beb0e4d89812e2d4430d7cfbf67f7c65098aa476a2cdf2a892ac8",
"timestamp": 1725615614,
"smartContractResults": [
{
"hash": "66debb4f02a735f00bd7da565069f7d26412341d6fae56bbab9b98696c8701e9",
"nonce": 0,
"value": 1000000000000000,
"receiver": "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
"sender": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"prevTxHash": "4c554d060e1b489d403759e445c4a4d80b0daa5a8eceafc7b9093eb8a7dd4b7a",
"originalTxHash": "4c554d060e1b489d403759e445c4a4d80b0daa5a8eceafc7b9093eb8a7dd4b7a",
"gasLimit": 0,
"gasPrice": 1000000000,
"callType": 0,
"originalSender": "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
"operation": "transfer"
}
],
"logs": {
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"events": [
{
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"identifier": "transferValueOnly",
"topics": [
"A41+pMaAAA==",
"ATlHLv9ohncamC8wg9pdQh8kwpGB5jiIIo3IHKYNaeE="
],
"data": "RGlyZWN0Q2FsbA==",
"additionalData": [
"RGlyZWN0Q2FsbA==",
""
]
},
{
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"identifier": "writeLog",
"topics": [
"ATlHLv9ohncamC8wg9pdQh8kwpGB5jiIIo3IHKYNaeE=",
"QHRvbyBtdWNoIGdhcyBwcm92aWRlZCBmb3IgcHJvY2Vzc2luZzogZ2FzIHByb3ZpZGVkID0gMjk5NDQwMDAsIGdhcyB1c2VkID0gMjE3MTk0OA=="
],
"data": "QDZmNmI=",
"additionalData": [
"QDZmNmI="
]
},
{
"address": "erd1qqqqqqqqqqqqqpgqqnw862rla67qnm7qwcxnkaw42kpg2t7ld8sssw0vgu",
"identifier": "completedTxEvent",
"topics": [
"TFVNBg4bSJ1AN1nkRcSk2AsNqlqOzq/HuQk+uKfdS3o="
],
"data": null,
"additionalData": null
}
]
},
"status": "success",
"operation": "transfer",
"function": "pong",
"initiallyPaidFee": "355440000000000",
"fee": "355440000000000",
"chainID": "D",
"version": 1,
"options": 0
}
},
"error": "",
"code": "successful"
}"#;

let tx_on_network: TransactionOnNetwork = serde_json::from_str::<TransactionInfo>(data)
.unwrap()
.data
.unwrap()
.transaction;
let tx_response = network_response::parse_tx_response(tx_on_network);

assert!(tx_response.out.is_empty());
}
2 changes: 1 addition & 1 deletion sdk/core/src/data/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub struct ApiSmartContractResult {
pub value: u64,
pub receiver: Address,
pub sender: Address,
pub data: String,
pub data: Option<String>,
pub prev_tx_hash: String,
pub original_tx_hash: String,
pub gas_limit: u64,
Expand Down

0 comments on commit 2cb5e42

Please sign in to comment.