Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cherry-pick #3177 remove transactions with status INVALID_ACCOUNT_ID from block… #3217

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 62 additions & 61 deletions docs/configuration.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/config-service/src/services/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ export class GlobalConfig {
required: false,
defaultValue: null,
},
HEDERA_SPECIFIC_REVERT_STATUSES: {
envName: 'HEDERA_SPECIFIC_REVERT_STATUSES',
type: 'string',
required: false,
defaultValue: '["WRONG_NONCE", "INVALID_ACCOUNT_ID"]',
},
FEE_HISTORY_MAX_RESULTS: {
envName: 'FEE_HISTORY_MAX_RESULTS',
type: 'number',
Expand Down
17 changes: 6 additions & 11 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ import {
formatContractResult,
formatTransactionIdWithoutQueryParams,
getFunctionSelector,
hexToASCII,
isHex,
isValidEthereumAddress,
nanOrNumberTo0x,
nullableNumberTo0x,
numberTo0x,
parseNumericEnvVar,
prepend0x,
strip0x,
toHash32,
trimPrecedingZeros,
weibarHexToTinyBarInt,
Expand Down Expand Up @@ -2298,18 +2296,15 @@ export class EthImpl implements Eth {
throw predefined.MAX_BLOCK_SIZE(blockResponse.count);
}

const isWrongNonce = (contractResult: { result: string; error_message: any }) => {
return (
contractResult.result === constants.TRANSACTION_RESULT_STATUS.WRONG_NONCE ||
hexToASCII(strip0x(contractResult.error_message ?? '')) === constants.TRANSACTION_RESULT_STATUS.WRONG_NONCE
);
};

// prepare transactionArray
let transactionArray: any[] = [];
for (const contractResult of contractResults) {
if (isWrongNonce(contractResult)) {
// skip wrong nonce transactions as they are not valid transactions which should be included in the block
// there are several hedera-specific validations that occur right before entering the evm
// if a transaction has reverted there, we should not include that tx in the block response
if (Utils.isRevertedDueToHederaSpecificValidation(contractResult)) {
this.logger.debug(
`${requestDetails.formattedRequestId} Transaction with hash ${contractResult.hash} is skipped due to hedera-specific validation failure (${contractResult.result})`,
);
continue;
}
contractResult.from = await this.resolveEvmAddress(contractResult.from, requestDetails, [constants.TYPE_ACCOUNT]);
Expand Down
18 changes: 18 additions & 0 deletions packages/relay/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { PrivateKey } from '@hashgraph/sdk';
import constants from './lib/constants';
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import crypto from 'crypto';
import { hexToASCII, strip0x } from './formatters';

export class Utils {
public static readonly IP_ADDRESS_REGEX = /\b((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}\b/g;
Expand Down Expand Up @@ -107,4 +108,21 @@ export class Utils {

return estimatedTxFee;
}

/**
* Check whether the transaction has reverted by a hedera-specific validation before the actual evm execution
* @param contractResult
* @returns {boolean}
*/
public static isRevertedDueToHederaSpecificValidation(contractResult: {
result: string;
error_message: any;
}): boolean {
// @ts-ignore
const statuses = JSON.parse(ConfigService.get('HEDERA_SPECIFIC_REVERT_STATUSES'));
return (
statuses.includes(contractResult.result) ||
statuses.includes(hexToASCII(strip0x(contractResult.error_message ?? '')))
);
}
}
65 changes: 38 additions & 27 deletions packages/relay/tests/lib/eth/eth_getBlockByNumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,34 +568,45 @@ describe('@ethGetBlockByNumber using MirrorNode', async function () {
});

[false, true].forEach((showDetails) => {
it(`eth_getBlockByNumber should skip wrong nonce transactions when showDetails = ${showDetails}`, async () => {
// mirror node request mocks
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(200, DEFAULT_BLOCK);
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, MOST_RECENT_BLOCK);
restMock.onGet(CONTRACT_RESULTS_WITH_FILTER_URL).reply(200, {
results: [
...defaultContractResults.results,
{ ...defaultContractResults.results[0], result: 'WRONG_NONCE' },
{ ...defaultContractResults.results[0], error_message: prepend0x(ASCIIToHex('WRONG_NONCE')) },
],
['WRONG_NONCE', 'INVALID_ACCOUNT_ID'].forEach((status) => {
it(`eth_getBlockByNumber should skip ${status} transactions when showDetails = ${showDetails}`, async () => {
// mirror node request mocks
restMock.onGet(`blocks/${BLOCK_NUMBER}`).reply(200, DEFAULT_BLOCK);
restMock.onGet(BLOCKS_LIMIT_ORDER_URL).reply(200, MOST_RECENT_BLOCK);
restMock.onGet(CONTRACT_RESULTS_WITH_FILTER_URL).reply(200, {
results: [
...defaultContractResults.results,
{
...defaultContractResults.results[0],
result: status,
hash: '0xf84b9a38205131431901ca6a945046369f5be81bb579167458d4992427d03bb1',
},
{
...defaultContractResults.results[0],
error_message: prepend0x(ASCIIToHex(status)),
hash: '0x9c8d9d99e033c56bec1669a0ea68887b7df69ec1bac55899150b6ed5bc3f4b79',
},
],
});
restMock.onGet(CONTRACT_RESULTS_LOGS_WITH_FILTER_URL).reply(200, DEFAULT_ETH_GET_BLOCK_BY_LOGS);

const result = await ethImpl.getBlockByNumber(numberTo0x(BLOCK_NUMBER), showDetails, requestDetails);

RelayAssertions.assertBlock(
result,
{
hash: BLOCK_HASH_TRIMMED,
gasUsed: TOTAL_GAS_USED,
number: BLOCK_NUMBER_HEX,
parentHash: BLOCK_HASH_PREV_TRIMMED,
timestamp: BLOCK_TIMESTAMP_HEX,
// should not include the transaction with wrong nonce or invalid account id
transactions: [CONTRACT_HASH_1, CONTRACT_HASH_2],
receiptsRoot: DEFAULT_BLOCK_RECEIPTS_ROOT_HASH,
},
showDetails,
);
});
restMock.onGet(CONTRACT_RESULTS_LOGS_WITH_FILTER_URL).reply(200, DEFAULT_ETH_GET_BLOCK_BY_LOGS);

const result = await ethImpl.getBlockByNumber(numberTo0x(BLOCK_NUMBER), showDetails, requestDetails);

RelayAssertions.assertBlock(
result,
{
hash: BLOCK_HASH_TRIMMED,
gasUsed: TOTAL_GAS_USED,
number: BLOCK_NUMBER_HEX,
parentHash: BLOCK_HASH_PREV_TRIMMED,
timestamp: BLOCK_TIMESTAMP_HEX,
transactions: [CONTRACT_HASH_1, CONTRACT_HASH_2], // should not include the transaction with wrong nonce
receiptsRoot: DEFAULT_BLOCK_RECEIPTS_ROOT_HASH,
},
showDetails,
);
});
});
});
34 changes: 32 additions & 2 deletions packages/relay/tests/lib/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import { expect } from 'chai';
import { Utils } from '../../src/utils';
import constants from '../../src/lib/constants';
import { overrideEnvsInMochaDescribe } from '../helpers';
import { estimateFileTransactionsFee } from '../helpers';
import { estimateFileTransactionsFee, overrideEnvsInMochaDescribe } from '../helpers';
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { ASCIIToHex, prepend0x } from '../../src/formatters';

describe('Utils', () => {
describe('addPercentageBufferToGasPrice', () => {
Expand Down Expand Up @@ -63,4 +63,34 @@ describe('Utils', () => {
expect(result).to.eq(expectedResult);
});
});

describe('isRevertedDueToHederaSpecificValidation', () => {
it('should not exclude transaction with status SUCCESS', () => {
expect(Utils.isRevertedDueToHederaSpecificValidation({ result: 'SUCCESS', error_message: null })).to.be.false;
});

it('should not exclude evm reverted transaction', () => {
expect(
Utils.isRevertedDueToHederaSpecificValidation({
result: 'CONTRACT_REVERT_EXECUTED',
error_message: 'Error',
}),
).to.be.false;
});

// @ts-ignore
JSON.parse(ConfigService.get('HEDERA_SPECIFIC_REVERT_STATUSES')).forEach((status) => {
it(`should exclude transaction with result ${status}`, () => {
expect(Utils.isRevertedDueToHederaSpecificValidation({ result: status, error_message: null })).to.be.true;
});
it(`should exclude transaction with error_message ${status}`, () => {
expect(
Utils.isRevertedDueToHederaSpecificValidation({
result: '',
error_message: prepend0x(ASCIIToHex(status)),
}),
).to.be.true;
});
});
});
});
Loading