Skip to content

Commit b769116

Browse files
committed
chore: edit check
Signed-off-by: nikolay <[email protected]>
1 parent c20b8f7 commit b769116

File tree

8 files changed

+55
-22
lines changed

8 files changed

+55
-22
lines changed

packages/relay/src/lib/eth.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ export class EthImpl implements Eth {
137137
this.filterService = new FilterService(mirrorNodeClient, logger, cacheService, this.common);
138138
this.feeService = new FeeService(mirrorNodeClient, this.common, logger);
139139
this.contractService = new ContractService(cacheService, this.common, hapiService, logger, mirrorNodeClient);
140-
this.accountService = new AccountService(cacheService, this.common, logger, mirrorNodeClient);
141140
this.blockService = new BlockService(cacheService, chain, this.common, mirrorNodeClient, logger);
142141
const storage = this.redisClient
143142
? new RedisPendingTransactionStorage(this.redisClient)
@@ -153,6 +152,13 @@ export class EthImpl implements Eth {
153152
mirrorNodeClient,
154153
transactionPoolService,
155154
);
155+
this.accountService = new AccountService(
156+
cacheService,
157+
this.common,
158+
logger,
159+
mirrorNodeClient,
160+
transactionPoolService,
161+
);
156162
}
157163

158164
/**

packages/relay/src/lib/precheck.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { prepend0x } from '../formatters';
88
import { MirrorNodeClient } from './clients';
99
import constants from './constants';
1010
import { JsonRpcError, predefined } from './errors/JsonRpcError';
11-
import { CommonService } from './services';
11+
import { CommonService, TransactionPoolService } from './services';
1212
import { RequestDetails } from './types';
1313

1414
/**
@@ -18,17 +18,24 @@ export class Precheck {
1818
private readonly mirrorNodeClient: MirrorNodeClient;
1919
private readonly chain: string;
2020
private readonly logger: Logger;
21+
private readonly transactionPoolService: TransactionPoolService;
2122

2223
/**
2324
* Creates an instance of Precheck.
2425
* @param {MirrorNodeClient} mirrorNodeClient - The MirrorNodeClient instance.
2526
* @param {Logger} logger - The logger instance.
2627
* @param {string} chainId - The chain ID.
2728
*/
28-
constructor(mirrorNodeClient: MirrorNodeClient, logger: Logger, chainId: string) {
29+
constructor(
30+
mirrorNodeClient: MirrorNodeClient,
31+
logger: Logger,
32+
chainId: string,
33+
transactionPoolService: TransactionPoolService,
34+
) {
2935
this.mirrorNodeClient = mirrorNodeClient;
3036
this.logger = logger;
3137
this.chain = chainId;
38+
this.transactionPoolService = transactionPoolService;
3239
}
3340

3441
/**
@@ -70,7 +77,10 @@ export class Precheck {
7077
this.transactionType(parsedTx);
7178
this.gasLimit(parsedTx);
7279
const mirrorAccountInfo = await this.verifyAccount(parsedTx, requestDetails);
73-
this.nonce(parsedTx, mirrorAccountInfo.ethereum_nonce);
80+
this.nonce(
81+
parsedTx,
82+
mirrorAccountInfo.ethereum_nonce + (await this.transactionPoolService.getPendingCount(parsedTx.from!)),
83+
);
7484
this.chainId(parsedTx);
7585
this.value(parsedTx);
7686
this.gasPrice(parsedTx, networkGasPriceInWeiBars);
@@ -105,17 +115,15 @@ export class Precheck {
105115
/**
106116
* Checks the nonce of the transaction.
107117
* @param tx - The transaction.
108-
* @param accountInfoNonce - The nonce of the account.
118+
* @param signerNonce - The nonce of the account.
109119
*/
110-
nonce(tx: Transaction, accountInfoNonce: number): void {
120+
nonce(tx: Transaction, signerNonce: number): void {
111121
if (this.logger.isLevelEnabled('trace')) {
112-
this.logger.trace(
113-
`Nonce precheck for sendRawTransaction(tx.nonce=${tx.nonce}, accountInfoNonce=${accountInfoNonce})`,
114-
);
122+
this.logger.trace(`Nonce precheck for sendRawTransaction(tx.nonce=${tx.nonce}, signerNonce=${signerNonce})`);
115123
}
116124

117-
if (accountInfoNonce > tx.nonce) {
118-
throw predefined.NONCE_TOO_LOW(tx.nonce, accountInfoNonce);
125+
if (signerNonce > tx.nonce) {
126+
throw predefined.NONCE_TOO_LOW(tx.nonce, signerNonce);
119127
}
120128
}
121129

packages/relay/src/lib/services/ethService/accountService/AccountService.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { JsonRpcError, predefined } from '../../../errors/JsonRpcError';
99
import { RequestDetails } from '../../../types';
1010
import { LatestBlockNumberTimestamp } from '../../../types/mirrorNode';
1111
import { CacheService } from '../../cacheService/cacheService';
12+
import { TransactionPoolService } from '../../transactionPoolService/transactionPoolService';
1213
import { ICommonService } from '../ethCommonService/ICommonService';
1314
import { IAccountService } from './IAccountService';
1415

@@ -70,18 +71,31 @@ export class AccountService implements IAccountService {
7071
*/
7172
private readonly mirrorNodeClient: MirrorNodeClient;
7273

74+
/**
75+
* The interface through which we interact with the transaction pool.
76+
* @private
77+
*/
78+
private readonly transactionPoolService: TransactionPoolService;
79+
7380
/**
7481
* @constructor
7582
* @param cacheService
7683
* @param common
7784
* @param logger
7885
* @param mirrorNodeClient
7986
*/
80-
constructor(cacheService: CacheService, common: ICommonService, logger: Logger, mirrorNodeClient: MirrorNodeClient) {
87+
constructor(
88+
cacheService: CacheService,
89+
common: ICommonService,
90+
logger: Logger,
91+
mirrorNodeClient: MirrorNodeClient,
92+
transactionPoolService: TransactionPoolService,
93+
) {
8194
this.cacheService = cacheService;
8295
this.common = common;
8396
this.logger = logger;
8497
this.mirrorNodeClient = mirrorNodeClient;
98+
this.transactionPoolService = transactionPoolService;
8599
}
86100

87101
/**
@@ -303,8 +317,11 @@ export class AccountService implements IAccountService {
303317
// previewnet and testnet bug have a genesis blockNumber of 1 but non system account were yet to be created
304318
return constants.ZERO_HEX;
305319
} else if (this.common.blockTagIsLatestOrPending(blockNumOrTag)) {
306-
// if latest or pending, get latest ethereumNonce from mirror node account API
307-
return await this.getAccountLatestEthereumNonce(address, requestDetails);
320+
const mnNonce = await this.getAccountLatestEthereumNonce(address, requestDetails);
321+
if (blockNumOrTag == constants.BLOCK_PENDING) {
322+
return numberTo0x(Number(mnNonce) + (await this.transactionPoolService.getPendingCount(address)));
323+
}
324+
return mnNonce;
308325
} else if (blockNumOrTag === constants.BLOCK_EARLIEST) {
309326
return await this.getAccountNonceForEarliestBlock(requestDetails);
310327
} else if (!isNaN(blockNum) && blockNumOrTag.length != constants.BLOCK_HASH_LENGTH && blockNum > 0) {

packages/relay/src/lib/services/ethService/transactionService/TransactionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class TransactionService implements ITransactionService {
9494
this.hapiService = hapiService;
9595
this.logger = logger;
9696
this.mirrorNodeClient = mirrorNodeClient;
97-
this.precheck = new Precheck(mirrorNodeClient, logger, chain);
97+
this.precheck = new Precheck(mirrorNodeClient, logger, chain, transactionPoolService);
9898
this.transactionPoolService = transactionPoolService;
9999
}
100100

packages/relay/tests/lib/eth/eth_getTransactionCount.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import MockAdapter from 'axios-mock-adapter';
44
import { expect, use } from 'chai';
55
import chaiAsPromised from 'chai-as-promised';
6-
import sinon from 'sinon';
6+
import sinon, { stub } from 'sinon';
77

88
import { Eth, predefined } from '../../../src';
99
import { numberTo0x } from '../../../src/formatters';
@@ -141,11 +141,13 @@ describe('@ethGetTransactionCount eth_getTransactionCount spec', async function
141141
expect(nonce).to.equal(numberTo0x(mockData.account.ethereum_nonce));
142142
});
143143

144-
it('should return latest nonce for pending block', async () => {
144+
it('should return pending nonce for pending block', async () => {
145+
const pendingTxs: number = 2;
146+
stub(ethImpl['accountService']['transactionPoolService'], 'getPendingCount').returns(pendingTxs);
145147
restMock.onGet(accountPath).reply(200, JSON.stringify(mockData.account));
146148
const nonce = await ethImpl.getTransactionCount(MOCK_ACCOUNT_ADDR, constants.BLOCK_PENDING, requestDetails);
147149
expect(nonce).to.exist;
148-
expect(nonce).to.equal(numberTo0x(mockData.account.ethereum_nonce));
150+
expect(nonce).to.equal(numberTo0x(mockData.account.ethereum_nonce + pendingTxs));
149151
});
150152

151153
it('should return 0x0 nonce for earliest block with valid block', async () => {

packages/relay/tests/lib/precheck.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ describe('Precheck', async function () {
605605
mock.onGet(`accounts/${parsedTx.from}${limitOrderPostFix}`).reply(200, JSON.stringify(mirrorAccount));
606606

607607
try {
608-
precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce, requestDetails);
608+
precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce);
609609
expectedError();
610610
} catch (e: any) {
611611
expect(e).to.eql(predefined.NONCE_TOO_LOW(parsedTx.nonce, mirrorAccount.ethereum_nonce));
@@ -622,7 +622,7 @@ describe('Precheck', async function () {
622622

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

625-
precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce, requestDetails);
625+
precheck.nonce(parsedTx, mirrorAccount.ethereum_nonce);
626626
});
627627
});
628628

packages/relay/tests/lib/services/transactionPoolService/LocalPendingTransactionStorage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as sinon from 'sinon';
55

66
import { LocalPendingTransactionStorage } from '../../../../src/lib/services/transactionPoolService/LocalPendingTransactionStorage';
77

8-
describe.only('LocalPendingTransactionStorage Test Suite', function () {
8+
describe('LocalPendingTransactionStorage Test Suite', function () {
99
let storage: LocalPendingTransactionStorage;
1010

1111
const testAddress1 = '0x742d35cc6db9027d0e0ba7d3c9e5a96f';

packages/relay/tests/lib/services/transactionPoolService/transactionPoolService.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { TransactionPoolService } from '../../../../src/lib/services/transaction
99
import { IExecuteTransactionEventPayload } from '../../../../src/lib/types/events';
1010
import { AddToListResult, PendingTransactionStorage } from '../../../../src/lib/types/transactionPool';
1111

12-
describe.only('TransactionPoolService Test Suite', function () {
12+
describe('TransactionPoolService Test Suite', function () {
1313
this.timeout(10000);
1414

1515
let logger: Logger;

0 commit comments

Comments
 (0)