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: remove transactions with status INVALID_ACCOUNT_ID from block-specific routes #3177

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,
},
EXCLUDED_TRANSACTION_STATUSES: {
envName: 'EXCLUDED_TRANSACTION_STATUSES',
type: 'string',
required: false,
defaultValue: '["WRONG_NONCE", "INVALID_ACCOUNT_ID"]',
},
FEE_HISTORY_MAX_RESULTS: {
envName: 'FEE_HISTORY_MAX_RESULTS',
type: 'number',
Expand Down
12 changes: 3 additions & 9 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2298,18 +2298,12 @@ 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)) {
Nana-EC marked this conversation as resolved.
Show resolved Hide resolved
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
quiet-node marked this conversation as resolved.
Show resolved Hide resolved
const statuses = JSON.parse(ConfigService.get('EXCLUDED_TRANSACTION_STATUSES'));
natanasow marked this conversation as resolved.
Show resolved Hide resolved
natanasow marked this conversation as resolved.
Show resolved Hide resolved
return (
statuses.indexOf(contractResult.result) > -1 ||
natanasow marked this conversation as resolved.
Show resolved Hide resolved
statuses.indexOf(hexToASCII(strip0x(contractResult.error_message ?? ''))) > -1
);
}
}
56 changes: 29 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,36 @@ 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 },
{ ...defaultContractResults.results[0], error_message: prepend0x(ASCIIToHex(status)) },
],
});
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
Nana-EC marked this conversation as resolved.
Show resolved Hide resolved
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('EXCLUDED_TRANSACTION_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