Skip to content

Commit

Permalink
fix: updated precheck.nonce() to handle higher nonce cases
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Nov 17, 2024
1 parent 27a10dc commit 2835433
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 54 deletions.
17 changes: 10 additions & 7 deletions packages/relay/src/lib/precheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*
*/

import { JsonRpcError, predefined } from './errors/JsonRpcError';
import { MirrorNodeClient } from './clients';
import { EthImpl } from './eth';
import { Logger } from 'pino';
import constants from './constants';
import { ethers, Transaction } from 'ethers';
import { Logger } from 'pino';

import { prepend0x } from '../formatters';
import { MirrorNodeClient } from './clients';
import constants from './constants';
import { JsonRpcError, predefined } from './errors/JsonRpcError';
import { EthImpl } from './eth';
import { RequestDetails } from './types';

/**
Expand Down Expand Up @@ -124,8 +125,10 @@ export class Precheck {
);
}

if (accountInfoNonce > tx.nonce) {
throw predefined.NONCE_TOO_LOW(tx.nonce, accountInfoNonce);
if (tx.nonce !== accountInfoNonce) {
throw tx.nonce < accountInfoNonce
? predefined.NONCE_TOO_LOW(tx.nonce, accountInfoNonce)
: predefined.NONCE_TOO_HIGH(tx.nonce, accountInfoNonce);
}
}

Expand Down
26 changes: 14 additions & 12 deletions packages/relay/tests/lib/eth/eth_sendRawTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { expect, use } from 'chai';
import sinon from 'sinon';
import chaiAsPromised from 'chai-as-promised';
import {
FileAppendTransaction,
FileId,
Expand All @@ -32,21 +29,25 @@ import {
TransactionId,
TransactionResponse,
} from '@hashgraph/sdk';
import { HbarLimitService } from '../../../src/lib/services/hbarLimitService';
import MockAdapter from 'axios-mock-adapter';
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { EventEmitter } from 'events';
import pino from 'pino';
import { SDKClient } from '../../../src/lib/clients';
import { ACCOUNT_ADDRESS_1, DEFAULT_NETWORK_FEES, MAX_GAS_LIMIT_HEX, NO_TRANSACTIONS } from './eth-config';
import { Counter } from 'prom-client';
import sinon from 'sinon';

import { Eth, JsonRpcError, predefined } from '../../../src';
import { SDKClient } from '../../../src/lib/clients';
import { SDKClientError } from '../../../src/lib/errors/SDKClientError';
import { CacheService } from '../../../src/lib/services/cacheService/cacheService';
import HAPIService from '../../../src/lib/services/hapiService/hapiService';
import { HbarLimitService } from '../../../src/lib/services/hbarLimitService';
import { RequestDetails } from '../../../src/lib/types';
import RelayAssertions from '../../assertions';
import { getRequestId, mockData, overrideEnvsInMochaDescribe, signTransaction } from '../../helpers';
import { ACCOUNT_ADDRESS_1, DEFAULT_NETWORK_FEES, MAX_GAS_LIMIT_HEX, NO_TRANSACTIONS } from './eth-config';
import { generateEthTestEnv } from './eth-helpers';
import { SDKClientError } from '../../../src/lib/errors/SDKClientError';
import { RequestDetails } from '../../../src/lib/types';
import MockAdapter from 'axios-mock-adapter';
import HAPIService from '../../../src/lib/services/hapiService/hapiService';
import { CacheService } from '../../../src/lib/services/cacheService/cacheService';
import { Counter } from 'prom-client';

use(chaiAsPromised);

Expand Down Expand Up @@ -115,6 +116,7 @@ describe('@ethSendRawTransaction eth_sendRawTransaction spec', async function ()
balance: {
balance: Hbar.from(100_000_000_000, HbarUnit.Hbar).to(HbarUnit.Tinybar),
},
ethereum_nonce: 0,
};

beforeEach(() => {
Expand Down
37 changes: 20 additions & 17 deletions packages/relay/tests/lib/openrpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,34 @@
*/

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { AccountInfo, Hbar } from '@hashgraph/sdk';
import { parseOpenRPCDocument, validateOpenRPCDocument } from '@open-rpc/schema-utils-js';
import Ajv from 'ajv';
import pino from 'pino';
import Long from 'long';
import axios from 'axios';
import sinon from 'sinon';
import { expect } from 'chai';
import EventEmitter from 'events';
import { AccountInfo, Hbar } from '@hashgraph/sdk';
import MockAdapter from 'axios-mock-adapter';
import { BigNumber } from 'bignumber.js';
import { EthImpl } from '../../src/lib/eth';
import constants from '../../src/lib/constants';
import { RelayImpl } from '../../src/lib/relay';
import { expect } from 'chai';
import { ethers } from 'ethers';
import EventEmitter from 'events';
import Long from 'long';
import pino from 'pino';
import { register, Registry } from 'prom-client';
import { NOT_FOUND_RES } from './eth/eth-config';
import sinon from 'sinon';

import openRpcSchema from '../../../../docs/openrpc.json';
import { numberTo0x } from '../../src/formatters';
import { SDKClient } from '../../src/lib/clients';
import { RequestDetails } from '../../src/lib/types';
import openRpcSchema from '../../../../docs/openrpc.json';
import { MirrorNodeClient } from '../../src/lib/clients/mirrorNodeClient';
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';
import ClientService from '../../src/lib/services/hapiService/hapiService';
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
import { parseOpenRPCDocument, validateOpenRPCDocument } from '@open-rpc/schema-utils-js';
import constants from '../../src/lib/constants';
import { EthAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository';
import { HbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository';
import { IPAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository';
import { EthAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository';
import { EthImpl } from '../../src/lib/eth';
import { RelayImpl } from '../../src/lib/relay';
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
import ClientService from '../../src/lib/services/hapiService/hapiService';
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';
import { RequestDetails } from '../../src/lib/types';
import {
blockHash,
blockNumber,
Expand Down Expand Up @@ -75,6 +76,7 @@ import {
overrideEnvsInMochaDescribe,
signedTransactionHash,
} from '../helpers';
import { NOT_FOUND_RES } from './eth/eth-config';

const logger = pino();
const registry = new Registry();
Expand Down Expand Up @@ -199,6 +201,7 @@ describe('Open RPC Specification', function () {
balance: {
balance: 100000000000,
},
ethereum_nonce: ethers.Transaction.from(signedTransactionHash).nonce,
});
mock
.onGet(`accounts/0xbC989b7b17d18702663F44A6004cB538b9DfcBAc?limit=100`)
Expand Down
49 changes: 31 additions & 18 deletions packages/relay/tests/lib/precheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@
*/

import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
import { expect } from 'chai';
import { Registry } from 'prom-client';
import { Hbar, HbarUnit } from '@hashgraph/sdk';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { expect } from 'chai';
import { ethers, Transaction } from 'ethers';
import pino from 'pino';
import { Registry } from 'prom-client';

import { JsonRpcError, predefined } from '../../src';
import { MirrorNodeClient } from '../../src/lib/clients';
import constants from '../../src/lib/constants';
import { Precheck } from '../../src/lib/precheck';
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
import {
blobVersionedHash,
contractAddress1,
Expand All @@ -32,13 +40,6 @@ import {
overrideEnvsInMochaDescribe,
signTransaction,
} from '../helpers';
import { MirrorNodeClient } from '../../src/lib/clients';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { ethers, Transaction } from 'ethers';
import constants from '../../src/lib/constants';
import { JsonRpcError, predefined } from '../../src';
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
import { ONE_TINYBAR_IN_WEI_HEX } from './eth/eth-config';

const registry = new Registry();
Expand Down Expand Up @@ -402,7 +403,7 @@ describe('Precheck', async function () {
ethereum_nonce: defaultNonce,
};

it(`should fail for low nonce`, async function () {
it(`should fail for lowwer nonce`, async function () {
const tx = {
...defaultTx,
nonce: 1,
Expand All @@ -412,15 +413,12 @@ describe('Precheck', async function () {

mock.onGet(`accounts/${parsedTx.from}${limitOrderPostFix}`).reply(200, mirrorAccount);

try {
precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce, requestDetails);
expectedError();
} catch (e: any) {
expect(e).to.eql(predefined.NONCE_TOO_LOW(parsedTx.nonce, mirrorAccount.ethereum_nonce));
}
expect(() => precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce, requestDetails))
.to.throw()
.and.eql(predefined.NONCE_TOO_LOW(parsedTx.nonce, mirrorAccount.ethereum_nonce));
});

it(`should not fail for next nonce`, async function () {
it(`should fail for higher nonce`, async function () {
const tx = {
...defaultTx,
nonce: 4,
Expand All @@ -430,7 +428,22 @@ describe('Precheck', async function () {

mock.onGet(`accounts/${parsedTx.from}${limitOrderPostFix}`).reply(200, mirrorAccount);

precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce, requestDetails);
expect(() => precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce, requestDetails))
.to.throw()
.and.eql(predefined.NONCE_TOO_HIGH(parsedTx.nonce, mirrorAccount.ethereum_nonce));
});

it(`should not fail for correct nonce`, async function () {
const tx = {
...defaultTx,
nonce: defaultNonce,
};
const signed = await signTransaction(tx);
const parsedTx = ethers.Transaction.from(signed);

mock.onGet(`accounts/${parsedTx.from}${limitOrderPostFix}`).reply(200, mirrorAccount);

expect(() => precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce, requestDetails)).to.not.throw();
});
});

Expand Down

0 comments on commit 2835433

Please sign in to comment.