-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: reduce code duplication in metricService.spec.ts
Signed-off-by: Victor Yanev <[email protected]>
- Loading branch information
1 parent
4dd94b6
commit 1f4faf2
Showing
1 changed file
with
61 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = '[email protected]'; | ||
|
@@ -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,60 +288,15 @@ 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); | ||
}); | ||
}); | ||
}); | ||
|
||
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 () => { | ||
|