Skip to content

Commit 2d7cbfa

Browse files
authored
Add InteractingEntity label to ConsensusNode Metrics (#910) (#918)
* - Added interactingEntity to metrics collector for consensusNodeClientHistorgram * - refactored precheck.sendRawTransactionCheck, to receive an already parsedTx instead of the string format, so we avoid double parse --------- Signed-off-by: Alfredo Gutierrez <[email protected]>
1 parent c9e7a68 commit 2d7cbfa

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class SDKClient {
113113
this.consensusNodeClientHistorgram = new Histogram({
114114
name: metricHistogramName,
115115
help: 'Relay consensusnode mode type status cost histogram',
116-
labelNames: ['mode', 'type', 'status', 'caller'],
116+
labelNames: ['mode', 'type', 'status', 'caller', 'interactingEntity'],
117117
registers: [register]
118118
});
119119

@@ -146,7 +146,7 @@ export class SDKClient {
146146

147147
async getAccountBalance(account: string, callerName: string, requestId?: string): Promise<AccountBalance> {
148148
return this.executeQuery(new AccountBalanceQuery()
149-
.setAccountId(AccountId.fromString(account)), this.clientMain, callerName, requestId);
149+
.setAccountId(AccountId.fromString(account)), this.clientMain, callerName, account, requestId);
150150
}
151151

152152
async getAccountBalanceInTinyBar(account: string, callerName: string, requestId?: string): Promise<BigNumber> {
@@ -161,17 +161,17 @@ export class SDKClient {
161161

162162
async getAccountInfo(address: string, callerName: string, requestId?: string): Promise<AccountInfo> {
163163
return this.executeQuery(new AccountInfoQuery()
164-
.setAccountId(AccountId.fromString(address)), this.clientMain, callerName, requestId);
164+
.setAccountId(AccountId.fromString(address)), this.clientMain, callerName, address, requestId);
165165
}
166166

167167
async getContractByteCode(shard: number | Long, realm: number | Long, address: string, callerName: string, requestId?: string): Promise<Uint8Array> {
168168
return this.executeQuery(new ContractByteCodeQuery()
169-
.setContractId(ContractId.fromEvmAddress(shard, realm, address)), this.clientMain, callerName, requestId);
169+
.setContractId(ContractId.fromEvmAddress(shard, realm, address)), this.clientMain, callerName, address, requestId);
170170
}
171171

172172
async getContractBalance(contract: string, callerName: string, requestId?: string): Promise<AccountBalance> {
173173
return this.executeQuery(new AccountBalanceQuery()
174-
.setContractId(ContractId.fromString(contract)), this.clientMain, callerName, requestId);
174+
.setContractId(ContractId.fromString(contract)), this.clientMain, callerName, contract, requestId);
175175
}
176176

177177
async getContractBalanceInWeiBar(account: string, callerName: string, requestId?: string): Promise<BigNumber> {
@@ -211,7 +211,7 @@ export class SDKClient {
211211

212212
async getFileIdBytes(address: string, callerName: string, requestId?: string): Promise<Uint8Array> {
213213
return this.executeQuery(new FileContentsQuery()
214-
.setFileId(address), this.clientMain, callerName, requestId);
214+
.setFileId(address), this.clientMain, callerName, address, requestId);
215215
}
216216

217217
async getRecord(transactionResponse: TransactionResponse) {
@@ -221,6 +221,7 @@ export class SDKClient {
221221
async submitEthereumTransaction(transactionBuffer: Uint8Array, callerName: string, requestId?: string): Promise<TransactionResponse> {
222222
const ethereumTransactionData: EthereumTransactionData = EthereumTransactionData.fromBytes(transactionBuffer);
223223
const ethereumTransaction = new EthereumTransaction();
224+
const interactingEntity = ethereumTransactionData.toJSON()['to'].toString();
224225

225226
if (ethereumTransactionData.toBytes().length <= 5120) {
226227
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes());
@@ -238,7 +239,7 @@ export class SDKClient {
238239
const tinybarsGasFee = await this.getTinyBarGasFee('eth_sendRawTransaction');
239240
ethereumTransaction.setMaxTransactionFee(Hbar.fromTinybars(Math.floor(tinybarsGasFee * constants.BLOCK_GAS_LIMIT)));
240241

241-
return this.executeTransaction(ethereumTransaction, callerName, requestId);
242+
return this.executeTransaction(ethereumTransaction, callerName, interactingEntity, requestId);
242243
}
243244

244245
async submitContractCallQuery(to: string, data: string, gas: number, from: string, callerName: string, requestId?: string): Promise<ContractFunctionResult> {
@@ -265,7 +266,7 @@ export class SDKClient {
265266
.setPaymentTransactionId(TransactionId.generate(this.clientMain.operatorAccountId));
266267
}
267268

268-
return this.executeQuery(contractCallQuery, this.clientMain, callerName, requestId);
269+
return this.executeQuery(contractCallQuery, this.clientMain, callerName, to, requestId);
269270
}
270271

271272
async increaseCostAndRetryExecution(query: Query<any>, baseCost: Hbar, client: Client, maxRetries: number, currentRetry: number, requestId?: string) {
@@ -304,7 +305,7 @@ export class SDKClient {
304305
);
305306
};
306307

307-
private executeQuery = async (query: Query<any>, client: Client, callerName: string, requestId?: string) => {
308+
private executeQuery = async (query: Query<any>, client: Client, callerName: string, interactingEntity: string, requestId?: string) => {
308309
const requestIdPrefix = formatRequestIdMessage(requestId);
309310
const currentDateNow = Date.now();
310311
try {
@@ -332,7 +333,8 @@ export class SDKClient {
332333
query.constructor.name,
333334
Status.Success,
334335
cost,
335-
callerName);
336+
callerName,
337+
interactingEntity);
336338
return resp;
337339
}
338340
catch (e: any) {
@@ -343,7 +345,8 @@ export class SDKClient {
343345
query.constructor.name,
344346
sdkClientError.status,
345347
cost,
346-
callerName);
348+
callerName,
349+
interactingEntity);
347350
this.logger.trace(`${requestIdPrefix} ${query.paymentTransactionId} ${callerName} ${query.constructor.name} status: ${sdkClientError.status} (${sdkClientError.status._code}), cost: ${query._queryPayment}`);
348351
if (cost) {
349352
this.hbarLimiter.addExpense(cost, currentDateNow);
@@ -365,7 +368,7 @@ export class SDKClient {
365368
}
366369
};
367370

368-
private executeTransaction = async (transaction: Transaction, callerName: string, requestId?: string): Promise<TransactionResponse> => {
371+
private executeTransaction = async (transaction: Transaction, callerName: string, interactingEntity: string, requestId?: string): Promise<TransactionResponse> => {
369372
const transactionType = transaction.constructor.name;
370373
const requestIdPrefix = formatRequestIdMessage(requestId);
371374
const currentDateNow = Date.now();
@@ -400,7 +403,8 @@ export class SDKClient {
400403
transactionType,
401404
sdkClientError.status,
402405
transactionFee.toTinybars().toNumber(),
403-
callerName);
406+
callerName,
407+
interactingEntity);
404408

405409
this.hbarLimiter.addExpense(transactionFee.toTinybars().toNumber(), currentDateNow);
406410
} catch (err: any) {
@@ -417,7 +421,7 @@ export class SDKClient {
417421
}
418422
};
419423

420-
async executeGetTransactionRecord(resp: TransactionResponse, transactionName: string, callerName: string, requestId?: string): Promise<TransactionRecord> {
424+
async executeGetTransactionRecord(resp: TransactionResponse, transactionName: string, callerName: string, interactingEntity: string, requestId?: string): Promise<TransactionRecord> {
421425
const requestIdPrefix = formatRequestIdMessage(requestId);
422426
const currentDateNow = Date.now();
423427
try {
@@ -438,7 +442,8 @@ export class SDKClient {
438442
transactionName,
439443
transactionRecord.receipt.status,
440444
cost,
441-
callerName);
445+
callerName,
446+
interactingEntity);
442447

443448
this.hbarLimiter.addExpense(cost, currentDateNow);
444449

@@ -463,7 +468,8 @@ export class SDKClient {
463468
transactionName,
464469
sdkClientError.status,
465470
transactionFee.toTinybars().toNumber(),
466-
callerName);
471+
callerName,
472+
interactingEntity);
467473

468474
this.hbarLimiter.addExpense(transactionFee.toTinybars().toNumber(), currentDateNow);
469475
} catch (err: any) {
@@ -481,13 +487,14 @@ export class SDKClient {
481487
}
482488
};
483489

484-
private captureMetrics = (mode, type, status, cost, caller) => {
490+
private captureMetrics = (mode, type, status, cost, caller, interactingEntity) => {
485491
const resolvedCost = cost ? cost : 0;
486492
this.consensusNodeClientHistorgram.labels(
487493
mode,
488494
type,
489495
status,
490-
caller)
496+
caller,
497+
interactingEntity)
491498
.observe(resolvedCost);
492499
};
493500

packages/relay/src/lib/eth.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,13 @@ export class EthImpl implements Eth {
891891
*/
892892
async sendRawTransaction(transaction: string, requestId?: string): Promise<string | JsonRpcError> {
893893
const requestIdPrefix = formatRequestIdMessage(requestId);
894+
let interactingEntity = '';
894895
this.logger.trace(`${requestIdPrefix} sendRawTransaction(transaction=${transaction})`);
895-
896896
try {
897+
const parsedTx = Precheck.parseTxIfNeeded(transaction);
898+
interactingEntity = parsedTx.to ? parsedTx.to.toString() : '';
897899
const gasPrice = await this.getFeeWeibars(EthImpl.ethSendRawTransaction, requestId);
898-
await this.precheck.sendRawTransactionCheck(transaction, gasPrice, requestId);
900+
await this.precheck.sendRawTransactionCheck(parsedTx, gasPrice, requestId);
899901
} catch (e: any) {
900902
throw this.genericErrorHandler(e);
901903
}
@@ -906,7 +908,7 @@ export class EthImpl implements Eth {
906908

907909
try {
908910
// Wait for the record from the execution.
909-
const record = await this.sdkClient.executeGetTransactionRecord(contractExecuteResponse, EthereumTransaction.name, EthImpl.ethSendRawTransaction, requestId);
911+
const record = await this.sdkClient.executeGetTransactionRecord(contractExecuteResponse, EthereumTransaction.name, EthImpl.ethSendRawTransaction, interactingEntity, requestId);
910912
if (!record) {
911913
this.logger.warn(`${requestIdPrefix} No record retrieved`);
912914
throw predefined.INTERNAL_ERROR();

packages/relay/src/lib/precheck.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class Precheck {
3939
this.logger = logger;
4040
}
4141

42-
private static parseTxIfNeeded(transaction: string | Transaction): Transaction {
42+
public static parseTxIfNeeded(transaction: string | Transaction): Transaction {
4343
return typeof transaction === 'string'
4444
? ethers.utils.parseTransaction(transaction)
4545
: transaction;
@@ -55,9 +55,8 @@ export class Precheck {
5555
* @param transaction
5656
* @param gasPrice
5757
*/
58-
async sendRawTransactionCheck(transaction: string, gasPrice: number, requestId?: string) {
59-
const parsedTx = Precheck.parseTxIfNeeded(transaction);
60-
58+
async sendRawTransactionCheck(parsedTx: ethers.Transaction, gasPrice: number, requestId?: string) {
59+
6160
this.gasLimit(parsedTx, requestId);
6261
const mirrorAccountInfo = await this.verifyAccount(parsedTx, requestId);
6362
await this.nonce(parsedTx, mirrorAccountInfo.ethereum_nonce, requestId);

0 commit comments

Comments
 (0)