Skip to content

Commit 0091f24

Browse files
authored
Update eth_transactioncount to use mirror (0.8) (#557)
Update eth_transactionaccount to use mirror node Signed-off-by: Nana Essilfie-Conduah <[email protected]>
1 parent a880c87 commit 0091f24

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

packages/relay/src/lib/clients/sdkClient.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ export class SDKClient {
261261
const requestIdPrefix = formatRequestIdMessage(requestId);
262262
try {
263263
const resp = await query.execute(client);
264-
this.logger.info(`${requestIdPrefix} Consensus Node query response: ${query.constructor.name} ${Status.Success._code}`);
264+
this.logger.info(`${requestIdPrefix} Consensus Node ${query.constructor.name} response: ${query.paymentTransactionId} ${Status.Success._code}`);
265265
// local free queries will have a '0.0.0' accountId on transactionId
266-
this.logger.trace(`${requestIdPrefix} ${query.paymentTransactionId} ${callerName} query cost: ${query._queryPayment}`);
266+
this.logger.trace(`${requestIdPrefix} ${callerName} query cost: ${query._queryPayment}`);
267267

268268
this.captureMetrics(
269269
SDKClient.queryMode,
@@ -310,14 +310,15 @@ export class SDKClient {
310310
}
311311
};
312312

313-
executeGetTransactionRecord = async (resp: TransactionResponse, transactionName: string, callerName: string, requestId?: string): Promise<TransactionRecord> => {
313+
async executeGetTransactionRecord(resp: TransactionResponse, transactionName: string, callerName: string, requestId?: string): Promise<TransactionRecord> {
314314
const requestIdPrefix = formatRequestIdMessage(requestId);
315315
try {
316316
if (!resp.getRecord) {
317317
throw new SDKClientError({}, `${requestIdPrefix} Invalid response format, expected record availability: ${JSON.stringify(resp)}`);
318318
}
319319

320320
const transactionRecord: TransactionRecord = await resp.getRecord(this.clientMain);
321+
this.logger.info(`${requestIdPrefix} Consensus Node ${transactionName} record response: ${resp.transactionId.toString()} ${Status.Success._code}`);
321322
this.logger.trace(`${requestIdPrefix} ${resp.transactionId.toString()} ${callerName} transaction cost: ${transactionRecord.transactionFee}`);
322323
this.captureMetrics(
323324
SDKClient.transactionMode,
@@ -330,14 +331,14 @@ export class SDKClient {
330331
catch (e: any) {
331332
// capture sdk record retrieval errors and shorten familiar stack trace
332333
const sdkClientError = new SDKClientError(e);
333-
if(sdkClientError.isValidNetworkError()) {
334-
this.captureMetrics(
335-
SDKClient.transactionMode,
336-
transactionName,
337-
sdkClientError.status,
338-
0,
339-
callerName);
340-
}
334+
this.captureMetrics(
335+
SDKClient.transactionMode,
336+
transactionName,
337+
sdkClientError.status,
338+
0,
339+
callerName);
340+
341+
this.logger.debug(`${requestIdPrefix} Consensus Node ${transactionName} record response: ${resp.transactionId.toString()} ${sdkClientError.status}`);
341342

342343
throw sdkClientError;
343344
}

packages/relay/src/lib/eth.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export class EthImpl implements Eth {
320320

321321
return EthImpl.numberTo0x(gasPrice);
322322
} catch (error) {
323-
this.logger.trace(error);
323+
this.logger.error(error, `${requestIdPrefix} Failed to retrieve gasPrice`);
324324
throw error;
325325
}
326326
}
@@ -688,24 +688,31 @@ export class EthImpl implements Eth {
688688
this.logger.trace(`${requestIdPrefix} getTransactionCount(address=${address}, blockNumOrTag=${blockNumOrTag})`);
689689
const blockNumber = await this.translateBlockTag(blockNumOrTag, requestId);
690690
if (blockNumber === 0) {
691-
return '0x0';
692-
} else {
693-
try {
694-
const result = await this.mirrorNodeClient.resolveEntityType(address, requestId);
695-
if (result?.type === constants.TYPE_ACCOUNT) {
696-
const accountInfo = await this.sdkClient.getAccountInfo(result?.entity.account, EthImpl.ethGetTransactionCount, requestId);
697-
return EthImpl.numberTo0x(Number(accountInfo.ethereumNonce));
698-
}
699-
else if (result?.type === constants.TYPE_CONTRACT) {
700-
return EthImpl.numberTo0x(1);
701-
}
702-
} catch (e: any) {
703-
this.logger.error(e, `${requestIdPrefix} Error raised during getTransactionCount for address ${address}, block number or tag ${blockNumOrTag}`);
704-
return predefined.INTERNAL_ERROR;
691+
return EthImpl.zeroHex;
692+
} else if (address && !blockNumOrTag) {
693+
// get latest ethereumNonce
694+
const mirrorAccount = await this.mirrorNodeClient.getAccount(address, requestId);
695+
if (mirrorAccount && mirrorAccount.ethereum_nonce) {
696+
return EthImpl.numberTo0x(mirrorAccount.ethereum_nonce);
705697
}
706-
707-
return EthImpl.numberTo0x(0);
708698
}
699+
700+
// check consensus node as back up
701+
try {
702+
const result = await this.mirrorNodeClient.resolveEntityType(address, requestId);
703+
if (result?.type === constants.TYPE_ACCOUNT) {
704+
const accountInfo = await this.sdkClient.getAccountInfo(result?.entity.account, EthImpl.ethGetTransactionCount, requestId);
705+
return EthImpl.numberTo0x(Number(accountInfo.ethereumNonce));
706+
}
707+
else if (result?.type === constants.TYPE_CONTRACT) {
708+
return EthImpl.numberTo0x(1)
709+
}
710+
711+
return EthImpl.zeroHex;
712+
} catch (e: any) {
713+
this.logger.error(e, `${requestIdPrefix} Error raised during getTransactionCount for address ${address}, block number or tag ${blockNumOrTag}`);
714+
return predefined.INTERNAL_ERROR;
715+
}
709716
}
710717

711718
/**
@@ -735,9 +742,16 @@ export class EthImpl implements Eth {
735742
try {
736743
// Wait for the record from the execution.
737744
const record = await this.sdkClient.executeGetTransactionRecord(contractExecuteResponse, EthereumTransaction.name, EthImpl.ethSendRawTransaction, requestId);
745+
if (!record) {
746+
this.logger.warn(`${requestIdPrefix} No record retrieved`);
747+
throw predefined.INTERNAL_ERROR;
748+
}
749+
738750
if (record.ethereumHash == null) {
739-
throw new Error('The ethereumHash can never be null for an ethereum transaction, and yet it was!!');
751+
this.logger.error(`${requestIdPrefix} The ethereumHash can never be null for an ethereum transaction, and yet it was!!`);
752+
throw predefined.INTERNAL_ERROR;
740753
}
754+
741755
return EthImpl.prepend0x(Buffer.from(record.ethereumHash).toString('hex'));
742756
} catch (e) {
743757
this.logger.error(e,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { Registry } from 'prom-client';
2727
import sinon from 'sinon';
2828
import cache from 'js-cache';
2929
dotenv.config({ path: path.resolve(__dirname, '../test.env') });
30-
import {RelayImpl, predefined, MirrorNodeClientError} from '@hashgraph/json-rpc-relay';
30+
import {RelayImpl, MirrorNodeClientError} from '@hashgraph/json-rpc-relay';
3131
import { EthImpl } from '../../src/lib/eth';
3232
import { MirrorNodeClient } from '../../src/lib/clients/mirrorNodeClient';
3333
import { expectUnsupportedMethod } from '../helpers';
@@ -120,7 +120,6 @@ describe('Eth calls using MirrorNode', async function () {
120120
const blockTimestamp = '1651560386';
121121
const blockTimestampHex = EthImpl.numberTo0x(Number(blockTimestamp));
122122
const firstTransactionTimestampSeconds = '1653077547';
123-
const firstTransactionTimestampSecondsHex = EthImpl.numberTo0x(Number(firstTransactionTimestampSeconds));
124123
const contractAddress1 = '0x000000000000000000000000000000000000055f';
125124
const contractTimestamp1 = `${firstTransactionTimestampSeconds}.983983199`;
126125
const contractHash1 = '0x4a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb6392';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import { expect } from 'chai';
2222
import { Registry } from 'prom-client';
23-
import { BigNumber } from 'ethers';
2423
import { Hbar, HbarUnit } from '@hashgraph/sdk';
2524
const registry = new Registry();
2625

@@ -33,6 +32,7 @@ import axios from "axios";
3332
import MockAdapter from "axios-mock-adapter";
3433
import { ethers } from "ethers";
3534
import constants from '../../src/lib/constants';
35+
import { predefined } from '../../src';
3636
const logger = pino();
3737

3838
describe('Precheck', async function() {

0 commit comments

Comments
 (0)