From 1f4faf2967e2f6c4dfde021cf6465a7f40bed9b7 Mon Sep 17 00:00:00 2001 From: Victor Yanev Date: Tue, 10 Dec 2024 15:06:40 +0200 Subject: [PATCH] chore: reduce code duplication in metricService.spec.ts Signed-off-by: Victor Yanev --- .../metricService/metricService.spec.ts | 188 ++++++------------ 1 file changed, 61 insertions(+), 127 deletions(-) diff --git a/packages/relay/tests/lib/services/metricService/metricService.spec.ts b/packages/relay/tests/lib/services/metricService/metricService.spec.ts index b61aa5e0c3..3462192000 100644 --- a/packages/relay/tests/lib/services/metricService/metricService.spec.ts +++ b/packages/relay/tests/lib/services/metricService/metricService.spec.ts @@ -60,7 +60,6 @@ describe('Metric Service', function () { const mockedTxFee = 36900000; const operatorAccountId = `0.0.1022`; const mockedCallerName = 'caller_name'; - const mockedExecutionMode = 'exection_mode'; const mockedConstructorName = 'constructor_name'; const mockedInteractingEntity = 'interacting_entity'; const mockedTransactionId = '0.0.1022@1681130064.409933500'; @@ -103,19 +102,57 @@ describe('Metric Service', function () { ], } as unknown as TransactionRecord; + const verifyConsensusNodeClientHistogramGasFee = async () => { + // @ts-ignore + const gasMetricObject = (await metricService['consensusNodeClientHistogramGasFee'].get()).values.find( + (metric) => metric.metricName === metricHistogramGasFeeSumTitle, + )!; + + expect(gasMetricObject.metricName).to.eq(metricHistogramGasFeeSumTitle); + expect(gasMetricObject.labels.caller).to.eq(mockedCallerName); + expect(gasMetricObject.labels.interactingEntity).to.eq(mockedInteractingEntity); + expect(gasMetricObject.value).to.eq( + mockedConsensusNodeTransactionRecord.contractFunctionResult?.gasUsed.toNumber(), + ); + }; + + const verifyConsensusNodeClientHistogramCost = async (executionMode: string, expectedTxRecordFee: number = 0) => { + const metricObjects = await metricService['consensusNodeClientHistogramCost'].get(); + + if (expectedTxRecordFee) { + const txRecordFeeMetricObject = metricObjects.values.find((metric) => { + return ( + metric.labels.mode === constants.EXECUTION_MODE.RECORD && metric.metricName === metricHistogramCostSumTitle + ); + }); + expect(txRecordFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle); + expect(txRecordFeeMetricObject?.labels.caller).to.eq(mockedCallerName); + expect(txRecordFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity); + expect(txRecordFeeMetricObject?.value).to.eq(expectedTxRecordFee); + } + + const transactionFeeMetricObject = metricObjects.values.find((metric) => { + return metric.labels.mode === executionMode && metric.metricName === metricHistogramCostSumTitle; + }); + expect(transactionFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle); + expect(transactionFeeMetricObject?.labels.caller).to.eq(mockedCallerName); + expect(transactionFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity); + expect(transactionFeeMetricObject?.value).to.eq(mockedTxFee); + }; + overrideEnvsInMochaDescribe({ OPERATOR_KEY_FORMAT: 'DER' }); before(() => { // consensus node client - const hederaNetwork = ConfigService.get('HEDERA_NETWORK')!; + const hederaNetwork = ConfigService.get('HEDERA_NETWORK')! as string; if (hederaNetwork in constants.CHAIN_IDS) { client = Client.forName(hederaNetwork); } else { client = Client.forNetwork(JSON.parse(hederaNetwork)); } client = client.setOperator( - AccountId.fromString(ConfigService.get('OPERATOR_ID_MAIN')!), - Utils.createPrivateKeyBasedOnFormat(ConfigService.get('OPERATOR_KEY_MAIN')!), + AccountId.fromString(ConfigService.get('OPERATOR_ID_MAIN')! as string), + Utils.createPrivateKeyBasedOnFormat(ConfigService.get('OPERATOR_KEY_MAIN')! as string), ); // mirror node client @@ -128,7 +165,7 @@ describe('Metric Service', function () { timeout: 20 * 1000, }); mirrorNodeClient = new MirrorNodeClient( - ConfigService.get('MIRROR_NODE_URL') || '', + (ConfigService.get('MIRROR_NODE_URL') as string) || '', logger.child({ name: `mirror-node` }), registry, new CacheService(logger.child({ name: `cache` }), registry), @@ -183,6 +220,18 @@ describe('Metric Service', function () { originalCallerAddress: mockedOriginalCallerAddress, }; + const verifyMetrics = async (originalBudget: Hbar, expectedTxRecordFee: number) => { + // validate hbarLimitService + // note: since the query is made to consensus node, the total charged amount = txFee + txRecordFee + const updatedBudget = await hbarLimitService['getRemainingBudget'](requestDetails); + expect(originalBudget.toTinybars().toNumber() - updatedBudget.toTinybars().toNumber()).to.eq( + mockedTxFee + expectedTxRecordFee, + ); + + await verifyConsensusNodeClientHistogramCost(constants.EXECUTION_MODE.TRANSACTION, expectedTxRecordFee); + await verifyConsensusNodeClientHistogramGasFee(); + }; + withOverriddenEnvsInMochaTest({ GET_RECORD_DEFAULT_TO_CONSENSUS_NODE: false }, () => { it('Should execute captureTransactionMetrics() by retrieving transaction record from MIRROR NODE client', async () => { mock @@ -199,14 +248,7 @@ describe('Metric Service', function () { expect(originalBudget.toTinybars().toNumber() - updatedBudget.toTinybars().toNumber()).to.eq(mockedTxFee); // validate cost metrics - const costMetricObject = (await metricService['consensusNodeClientHistogramCost'].get()).values.find( - (metric) => metric.metricName === metricHistogramCostSumTitle, - ); - expect(costMetricObject).to.not.be.undefined; - expect(costMetricObject!.metricName).to.eq(metricHistogramCostSumTitle); - expect(costMetricObject!.labels.caller).to.eq(mockedCallerName); - expect(costMetricObject!.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(costMetricObject!.value).to.eq(mockedTxFee); + await verifyConsensusNodeClientHistogramCost(constants.EXECUTION_MODE.TRANSACTION); }); }); @@ -222,52 +264,9 @@ describe('Metric Service', function () { const originalBudget = await hbarLimitService['getRemainingBudget'](requestDetails); await metricService.captureTransactionMetrics(mockedExecuteTransactionEventPayload); - expect(transactionRecordStub.called).to.be.true; - - // validate hbarLimitService - // note: since the query is made to consensus node, the total charged amount = txFee + txRecordFee - const updatedBudget = await hbarLimitService['getRemainingBudget'](requestDetails); - expect(originalBudget.toTinybars().toNumber() - updatedBudget.toTinybars().toNumber()).to.eq( - mockedTxFee + expectedTxRecordFee, - ); - // validate cost metric - // @ts-ignore - const metricObjects = await metricService['consensusNodeClientHistogramCost'].get(); - const txRecordFeeMetricObject = metricObjects.values.find((metric) => { - return ( - metric.labels.mode === constants.EXECUTION_MODE.RECORD && metric.metricName === metricHistogramCostSumTitle - ); - }); - const transactionFeeMetricObject = metricObjects.values.find((metric) => { - return ( - metric.labels.mode === constants.EXECUTION_MODE.TRANSACTION && - metric.metricName === metricHistogramCostSumTitle - ); - }); - - expect(txRecordFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle); - expect(txRecordFeeMetricObject?.labels.caller).to.eq(mockedCallerName); - expect(txRecordFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(txRecordFeeMetricObject?.value).to.eq(expectedTxRecordFee); - - expect(transactionFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle); - expect(transactionFeeMetricObject?.labels.caller).to.eq(mockedCallerName); - expect(transactionFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(transactionFeeMetricObject?.value).to.eq(mockedTxFee); - - // validate gas metric - // @ts-ignore - const gasMetricObject = (await metricService['consensusNodeClientHistogramGasFee'].get()).values.find( - (metric) => metric.metricName === metricHistogramGasFeeSumTitle, - )!; - - expect(gasMetricObject.metricName).to.eq(metricHistogramGasFeeSumTitle); - expect(gasMetricObject.labels.caller).to.eq(mockedCallerName); - expect(gasMetricObject.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(gasMetricObject.value).to.eq( - mockedConsensusNodeTransactionRecord.contractFunctionResult?.gasUsed.toNumber(), - ); + expect(transactionRecordStub.called).to.be.true; + await verifyMetrics(originalBudget, expectedTxRecordFee); }); }); @@ -289,52 +288,7 @@ describe('Metric Service', function () { await new Promise((r) => setTimeout(r, 100)); expect(transactionRecordStub.called).to.be.true; - - // validate hbarLimitService - // note: since the query is made to consensus node, the total charged amount = txFee + txRecordFee - const updatedBudget = await hbarLimitService['getRemainingBudget'](requestDetails); - - expect(originalBudget.toTinybars().toNumber() - updatedBudget.toTinybars().toNumber()).to.eq( - mockedTxFee + expectedTxRecordFee, - ); - - // validate cost metric - // @ts-ignore - const metricObjects = await metricService['consensusNodeClientHistogramCost'].get(); - const txRecordFeeMetricObject = metricObjects.values.find((metric) => { - return ( - metric.labels.mode === constants.EXECUTION_MODE.RECORD && metric.metricName === metricHistogramCostSumTitle - ); - }); - const transactionFeeMetricObject = metricObjects.values.find((metric) => { - return ( - metric.labels.mode === constants.EXECUTION_MODE.TRANSACTION && - metric.metricName === metricHistogramCostSumTitle - ); - }); - - expect(txRecordFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle); - expect(txRecordFeeMetricObject?.labels.caller).to.eq(mockedCallerName); - expect(txRecordFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(txRecordFeeMetricObject?.value).to.eq(expectedTxRecordFee); - - expect(transactionFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle); - expect(transactionFeeMetricObject?.labels.caller).to.eq(mockedCallerName); - expect(transactionFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(transactionFeeMetricObject?.value).to.eq(mockedTxFee); - - // validate gas metric - // @ts-ignore - const gasMetricObject = (await metricService['consensusNodeClientHistogramGasFee'].get()).values.find( - (metric) => metric.metricName === metricHistogramGasFeeSumTitle, - )!; - - expect(gasMetricObject.metricName).to.eq(metricHistogramGasFeeSumTitle); - expect(gasMetricObject.labels.caller).to.eq(mockedCallerName); - expect(gasMetricObject.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(gasMetricObject.value).to.eq( - mockedConsensusNodeTransactionRecord.contractFunctionResult?.gasUsed.toNumber(), - ); + await verifyMetrics(originalBudget, expectedTxRecordFee); }); }); }); @@ -342,7 +296,7 @@ describe('Metric Service', function () { describe('addExpenseAndCaptureMetrics', () => { const mockedGasUsed = mockedConsensusNodeTransactionRecord.contractFunctionResult!.gasUsed.toNumber(); const mockedExecuteQueryEventPayload: IExecuteQueryEventPayload = { - executionMode: mockedExecutionMode, + executionMode: constants.EXECUTION_MODE.QUERY, transactionId: mockedTransactionId, txConstructorName: mockedConstructorName, callerName: mockedCallerName, @@ -358,28 +312,8 @@ describe('Metric Service', function () { const updatedBudget = await hbarLimitService['getRemainingBudget'](requestDetails); expect(originalBudget.toTinybars().toNumber() - updatedBudget.toTinybars().toNumber()).to.eq(mockedTxFee); - // validate cost metrics - // @ts-ignore - const costMetricObject = (await metricService['consensusNodeClientHistogramCost'].get()).values.find( - (metric) => metric.metricName === metricHistogramCostSumTitle, - )!; - expect(costMetricObject.metricName).to.eq(metricHistogramCostSumTitle); - expect(costMetricObject.labels.caller).to.eq(mockedCallerName); - expect(costMetricObject.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(costMetricObject.value).to.eq(mockedTxFee); - - // validate gas metric - // @ts-ignore - const gasMetricObject = (await metricService['consensusNodeClientHistogramGasFee'].get()).values.find( - (metric) => metric.metricName === metricHistogramGasFeeSumTitle, - )!; - - expect(gasMetricObject.metricName).to.eq(metricHistogramGasFeeSumTitle); - expect(gasMetricObject.labels.caller).to.eq(mockedCallerName); - expect(gasMetricObject.labels.interactingEntity).to.eq(mockedInteractingEntity); - expect(gasMetricObject.value).to.eq( - mockedConsensusNodeTransactionRecord.contractFunctionResult?.gasUsed.toNumber(), - ); + await verifyConsensusNodeClientHistogramCost(constants.EXECUTION_MODE.QUERY); + await verifyConsensusNodeClientHistogramGasFee(); }; it('should execute addExpenseAndCaptureMetrics() to capture metrics in HBAR limiter and metric registry', async () => {