Skip to content

Commit

Permalink
Use Alias When Available and No sender_id On Contract Results Response (
Browse files Browse the repository at this point in the history
#9708)

*  use alias when available and no sender_id on contract_result

---------

Signed-off-by: Jesse Nelson <[email protected]>
  • Loading branch information
jnels124 authored Nov 6, 2024
1 parent 069412c commit 4542640
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 44 deletions.
4 changes: 4 additions & 0 deletions hedera-mirror-rest/__tests__/service/contractService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('ContractService.getContractResultsByIdAndFiltersQuery tests', () => {
cr.error_message,
cr.failed_initcode,
cr.function_parameters,
case when cr.sender_id is null then cr.function_result else '' end as function_result,
cr.gas_consumed,
cr.gas_limit,
cr.gas_used,
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('ContractService.getContractResultsByIdAndFiltersQuery tests', () => {
cr.error_message,
cr.failed_initcode,
cr.function_parameters,
case when cr.sender_id is null then cr.function_result else '' end as function_result,
cr.gas_consumed,
cr.gas_limit,
cr.gas_used,
Expand Down Expand Up @@ -121,6 +123,7 @@ describe('ContractService.getContractResultsByIdAndFiltersQuery tests', () => {
cr.error_message,
cr.failed_initcode,
cr.function_parameters,
case when cr.sender_id is null then cr.function_result else '' end as function_result,
cr.gas_consumed,
cr.gas_limit,
cr.gas_used,
Expand Down Expand Up @@ -160,6 +163,7 @@ describe('ContractService.getContractResultsByIdAndFiltersQuery tests', () => {
cr.error_message,
cr.failed_initcode,
cr.function_parameters,
case when cr.sender_id is null then cr.function_result else '' end as function_result,
cr.gas_consumed,
cr.gas_limit,
cr.gas_used,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"consensus_timestamp": "1676540001234390005",
"contract_id": 5001,
"created_contract_ids": [],
"error_message": "Not enough gas",
"error_message": "Invalid account",
"function_parameters": [3, 3],
"function_result": [4, 4],
"function_result": null,
"gas_limit": 1234556,
"gas_used": 987,
"sender_id": null,
Expand All @@ -31,6 +31,68 @@
"transaction_hash": "0x160502030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"transaction_nonce": 0,
"transaction_result": "15"
},
{
"amount": 20,
"bloom": [1, 1],
"call_result": [2, 2],
"consensus_timestamp": "1676540001234390006",
"contract_id": 5001,
"created_contract_ids": [],
"error_message": "Invalid account",
"function_parameters": [3, 3],
"function_result": [2, 2],
"gas_limit": 1234556,
"gas_used": 987,
"sender_id": null,
"gas_consumed": 1000,
"payer_account_id": 5001,
"transaction_hash": "0xc6a2902a66d4c919a59fad466de4063bad35263a24c10144446bccead8cef0c8",
"transaction_nonce": 0,
"transaction_result": "15"
},
{
"amount": 20,
"bloom": [1, 1],
"call_result": [2, 2],
"consensus_timestamp": "1676540001234390007",
"contract_id": 5001,
"created_contract_ids": [],
"error_message": "Invalid account",
"function_parameters": [3, 3],
"function_result": [
106,
22,
34,
20,
88,
89,
138,
112,
194,
52,
135,
209,
103,
53,
209,
129,
39,
229,
222,
239,
115,
201,
76,
13],
"gas_limit": 1234556,
"gas_used": 987,
"sender_id": null,
"gas_consumed": 1000,
"payer_account_id": 5001,
"transaction_hash": "0x1a8770893d8a39b87abc7f13ecb34796fe1623a13590322b2b06e7cba252c263",
"transaction_nonce": 0,
"transaction_result": "15"
}
],
"recordFiles": [
Expand Down Expand Up @@ -61,50 +123,170 @@
"recovery_id": 1,
"nonce": 5,
"value": ["0x14"]
}
]
},
"urls": ["/api/v1/contracts/results?timestamp=1676540001.234390005"],
"responseStatus": 200,
"responseJson": {
"results": [
},
{
"access_list": "0x",
"address": "0x70f2b2914a2a4b783faefb75f459a580616fcb5e",
"amount": 20,
"block_gas_used": 400000,
"block_hash": "0xfbd921184e229e2051280d827ba3b31599117af7eafba65dc0e5a998b70c48c0492bf793a150769b1b4fb2c9b7cb4c1c",
"block_number": 10,
"bloom": "0x0101",
"call_result": "0x0202",
"chain_id": "0x12a",
"contract_id": "0.0.5001",
"created_contract_ids": [],
"error_message": "Not enough gas",
"failed_initcode": null,
"from": null,
"function_parameters": "0x0303",
"gas_limit": 1234556,
"gas_price": "0x4a817c80",
"gas_used": 987,
"hash": "0x160502030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"consensus_timestamp": "1676540001234390006",
"hash": "0xc6a2902a66d4c919a59fad466de4063bad35263a24c10144446bccead8cef0c8",
"chain_id": "0x012a",
"max_fee_per_gas": "0x59",
"max_priority_fee_per_gas": "0x33",
"payer_account_id": 5001,
"signature_r": "0xb5c21ab4dfd336e30ac2106cad4aa8888b1873a99bce35d50f64d2ec2cc5f6d9",
"signature_s": "0x1092806a99727a20c31836959133301b65a2bfa980f9795522d21a254e629110",
"recovery_id": 1,
"nonce": 5,
"r": "0xb5c21ab4dfd336e30ac2106cad4aa8888b1873a99bce35d50f64d2ec2cc5f6d9",
"result": "INVALID_ACCOUNT_ID",
"s": "0x1092806a99727a20c31836959133301b65a2bfa980f9795522d21a254e629110",
"status": "0x0",
"timestamp": "1676540001.234390005",
"to": "0x0000000000000000000000000000000000001389",
"transaction_index": 1,
"gas_consumed": 1000,
"type": 2,
"v": 1
"value": ["0x14"]
},
{
"consensus_timestamp": "1676540001234390007",
"hash": "0x1a8770893d8a39b87abc7f13ecb34796fe1623a13590322b2b06e7cba252c263",
"chain_id": "0x012a",
"max_fee_per_gas": "0x59",
"max_priority_fee_per_gas": "0x33",
"payer_account_id": 5001,
"signature_r": "0xb5c21ab4dfd336e30ac2106cad4aa8888b1873a99bce35d50f64d2ec2cc5f6d9",
"signature_s": "0x1092806a99727a20c31836959133301b65a2bfa980f9795522d21a254e629110",
"recovery_id": 1,
"nonce": 5,
"value": ["0x14"]
}
]
},
"tests": [
{
"urls": ["/api/v1/contracts/results?timestamp=1676540001.234390005"],
"responseStatus": 200,
"responseJson": {
"results": [
{
"access_list": "0x",
"address": "0x70f2b2914a2a4b783faefb75f459a580616fcb5e",
"amount": 20,
"block_gas_used": 400000,
"block_hash": "0xfbd921184e229e2051280d827ba3b31599117af7eafba65dc0e5a998b70c48c0492bf793a150769b1b4fb2c9b7cb4c1c",
"block_number": 10,
"bloom": "0x0101",
"call_result": "0x0202",
"chain_id": "0x12a",
"contract_id": "0.0.5001",
"created_contract_ids": [],
"error_message": "Invalid account",
"failed_initcode": null,
"from": null,
"function_parameters": "0x0303",
"gas_limit": 1234556,
"gas_price": "0x4a817c80",
"gas_used": 987,
"hash": "0x160502030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"max_fee_per_gas": "0x59",
"max_priority_fee_per_gas": "0x33",
"nonce": 5,
"r": "0xb5c21ab4dfd336e30ac2106cad4aa8888b1873a99bce35d50f64d2ec2cc5f6d9",
"result": "INVALID_ACCOUNT_ID",
"s": "0x1092806a99727a20c31836959133301b65a2bfa980f9795522d21a254e629110",
"status": "0x0",
"timestamp": "1676540001.234390005",
"to": "0x0000000000000000000000000000000000001389",
"transaction_index": 1,
"gas_consumed": 1000,
"type": 2,
"v": 1
}
],
"links": {
"next": null
}
}
},
{
"urls": ["/api/v1/contracts/results?timestamp=1676540001.234390006"],
"responseStatus": 200,
"responseJson": {
"results": [
{
"access_list": "0x",
"address": "0x70f2b2914a2a4b783faefb75f459a580616fcb5e",
"amount": 20,
"block_gas_used": 400000,
"block_hash": "0xfbd921184e229e2051280d827ba3b31599117af7eafba65dc0e5a998b70c48c0492bf793a150769b1b4fb2c9b7cb4c1c",
"block_number": 10,
"bloom": "0x0101",
"call_result": "0x0202",
"chain_id": "0x12a",
"contract_id": "0.0.5001",
"created_contract_ids": [],
"error_message": "Invalid account",
"failed_initcode": null,
"from": null,
"function_parameters": "0x0303",
"gas_limit": 1234556,
"gas_price": "0x4a817c80",
"gas_used": 987,
"hash": "0xc6a2902a66d4c919a59fad466de4063bad35263a24c10144446bccead8cef0c8",
"max_fee_per_gas": "0x59",
"max_priority_fee_per_gas": "0x33",
"nonce": 5,
"r": "0xb5c21ab4dfd336e30ac2106cad4aa8888b1873a99bce35d50f64d2ec2cc5f6d9",
"result": "INVALID_ACCOUNT_ID",
"s": "0x1092806a99727a20c31836959133301b65a2bfa980f9795522d21a254e629110",
"status": "0x0",
"timestamp": "1676540001.234390006",
"to": "0x0000000000000000000000000000000000001389",
"transaction_index": 1,
"gas_consumed": 1000,
"type": 2,
"v": 1
}
],
"links": {
"next": null
}
}
},
{
"urls": ["/api/v1/contracts/results?timestamp=1676540001.234390007"],
"responseStatus": 200,
"responseJson": {
"results": [
{
"access_list": "0x",
"address": "0x70f2b2914a2a4b783faefb75f459a580616fcb5e",
"amount": 20,
"block_gas_used": 400000,
"block_hash": "0xfbd921184e229e2051280d827ba3b31599117af7eafba65dc0e5a998b70c48c0492bf793a150769b1b4fb2c9b7cb4c1c",
"block_number": 10,
"bloom": "0x0101",
"call_result": "0x0202",
"chain_id": "0x12a",
"contract_id": "0.0.5001",
"created_contract_ids": [],
"error_message": "Invalid account",
"failed_initcode": null,
"from": "0x58598a70c23487d16735d18127e5deef73c94c0d",
"function_parameters": "0x0303",
"gas_limit": 1234556,
"gas_price": "0x4a817c80",
"gas_used": 987,
"hash": "0x1a8770893d8a39b87abc7f13ecb34796fe1623a13590322b2b06e7cba252c263",
"max_fee_per_gas": "0x59",
"max_priority_fee_per_gas": "0x33",
"nonce": 5,
"r": "0xb5c21ab4dfd336e30ac2106cad4aa8888b1873a99bce35d50f64d2ec2cc5f6d9",
"result": "INVALID_ACCOUNT_ID",
"s": "0x1092806a99727a20c31836959133301b65a2bfa980f9795522d21a254e629110",
"status": "0x0",
"timestamp": "1676540001.234390007",
"to": "0x0000000000000000000000000000000000001389",
"transaction_index": 1,
"gas_consumed": 1000,
"type": 2,
"v": 1
}
],
"links": {
"next": null
}
}
],
"links": {
"next": null
}
}
]
}
5 changes: 4 additions & 1 deletion hedera-mirror-rest/service/contractService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import _ from 'lodash';

import BaseService from './baseService';
import {getResponseLimit} from '../config';
import {MAX_LONG, filterKeys, orderFilterValues} from '../constants';
import {filterKeys, MAX_LONG, orderFilterValues} from '../constants';
import EntityId from '../entityId';
import {NotFoundError} from '../errors';
import {OrderSpec} from '../sql';
Expand Down Expand Up @@ -60,6 +60,9 @@ ${ContractResult.getFullName(ContractResult.CREATED_CONTRACT_IDS)},
${ContractResult.getFullName(ContractResult.ERROR_MESSAGE)},
${ContractResult.getFullName(ContractResult.FAILED_INITCODE)},
${ContractResult.getFullName(ContractResult.FUNCTION_PARAMETERS)},
case when ${ContractResult.getFullName(ContractResult.SENDER_ID)} is null
then ${ContractResult.getFullName(ContractResult.FUNCTION_RESULT)}
else '' end as ${ContractResult.FUNCTION_RESULT},
${ContractResult.getFullName(ContractResult.GAS_CONSUMED)},
${ContractResult.getFullName(ContractResult.GAS_LIMIT)},
${ContractResult.getFullName(ContractResult.GAS_USED)},
Expand Down
16 changes: 15 additions & 1 deletion hedera-mirror-rest/viewmodel/contractResultViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import _ from 'lodash';
import EntityId from '../entityId';
import {nsToSecNs, toHexString} from '../utils';
import {proto} from '@hashgraph/proto';

/**
* Contract results view model
Expand All @@ -38,7 +39,7 @@ class ContractResultViewModel {
this.contract_id = contractId.toString();
this.created_contract_ids = _.toArray(contractResult.createdContractIds).map((id) => EntityId.parse(id).toString());
this.error_message = _.isEmpty(contractResult.errorMessage) ? null : contractResult.errorMessage;
this.from = EntityId.parse(contractResult.senderId, {isNullable: true}).toEvmAddress();
this.from = EntityId.parse(contractResult.senderId, {isNullable: true}).toEvmAddress() || this.#extractSenderFromFunctionResult(contractResult);
this.function_parameters = toHexString(contractResult.functionParameters, true);
this.gas_consumed = contractResult.gasConsumed;
this.gas_limit = contractResult.gasLimit;
Expand All @@ -47,6 +48,19 @@ class ContractResultViewModel {
this.to = contractId.toEvmAddress();
this.hash = toHexString(contractResult.transactionHash, true);
}

#extractSenderFromFunctionResult(contractResult) {
if (!contractResult.sender_id && contractResult.functionResult) {
try {
const functionResult = proto.ContractFunctionResult.decode(contractResult.functionResult);
return functionResult?.senderId?.alias?.length ? toHexString(functionResult.senderId.alias, true) : null;
} catch (error) {
logger.warn('Error decoding function result', error);
}
}

return null;
}
}

export default ContractResultViewModel;

0 comments on commit 4542640

Please sign in to comment.