diff --git a/packages/relay/src/lib/clients/sdkClient.ts b/packages/relay/src/lib/clients/sdkClient.ts index 1478530eb4..db525611f9 100644 --- a/packages/relay/src/lib/clients/sdkClient.ts +++ b/packages/relay/src/lib/clients/sdkClient.ts @@ -58,6 +58,7 @@ import HbarLimit from '../hbarlimiter'; import constants from './../constants'; import { BigNumber } from '@hashgraph/sdk/lib/Transfer'; import { SDKClientError } from './../errors/SDKClientError'; +import { HbarLimitService } from '../services/hbarLimitService'; import { JsonRpcError, predefined } from './../errors/JsonRpcError'; import { CacheService } from '../services/cacheService/cacheService'; import { formatRequestIdMessage, weibarHexToTinyBarInt } from '../../formatters'; @@ -114,6 +115,14 @@ export class SDKClient { */ private readonly eventEmitter: EventEmitter; + /** + * An instance of the HbarLimitService that tracks hbar expenses and limits. + * @private + * @readonly + * @type {HbarLimitService} + */ + private readonly hbarLimitService: HbarLimitService; + /** * Constructs an instance of the SDKClient and initializes various services and settings. * @@ -129,6 +138,7 @@ export class SDKClient { hbarLimiter: HbarLimit, cacheService: CacheService, eventEmitter: EventEmitter, + hbarLimitService: HbarLimitService, ) { this.clientMain = clientMain; @@ -142,6 +152,7 @@ export class SDKClient { this.hbarLimiter = hbarLimiter; this.cacheService = cacheService; this.eventEmitter = eventEmitter; + this.hbarLimitService = hbarLimitService; this.maxChunks = Number(process.env.FILE_APPEND_MAX_CHUNKS) || 20; this.fileAppendChunkSize = Number(process.env.FILE_APPEND_CHUNK_SIZE) || 5120; } diff --git a/packages/relay/src/lib/services/hapiService/hapiService.ts b/packages/relay/src/lib/services/hapiService/hapiService.ts index 0f534d3a46..0306ba5ad2 100644 --- a/packages/relay/src/lib/services/hapiService/hapiService.ts +++ b/packages/relay/src/lib/services/hapiService/hapiService.ts @@ -322,6 +322,7 @@ export default class HAPIService { this.hbarLimiter, this.cacheService, this.eventEmitter, + this.hbarLimitService, ); } diff --git a/packages/relay/tests/lib/sdkClient.spec.ts b/packages/relay/tests/lib/sdkClient.spec.ts index cb3b93f417..b79cdf81fe 100644 --- a/packages/relay/tests/lib/sdkClient.spec.ts +++ b/packages/relay/tests/lib/sdkClient.spec.ts @@ -77,8 +77,10 @@ describe('SdkClient', async function () { let hbarLimiter: HbarLimit; let instance: AxiosInstance; let eventEmitter: EventEmitter; + let cacheService: CacheService; let metricService: MetricService; let mirrorNodeClient: MirrorNodeClient; + let hbarLimitService: HbarLimitService; const feeSchedules = { current: { @@ -113,12 +115,27 @@ describe('SdkClient', async function () { const total = constants.HBAR_RATE_LIMIT_TINYBAR(); hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration, registry); eventEmitter = new EventEmitter(); + + cacheService = new CacheService(logger, registry); + const hbarSpendingPlanRepository = new HbarSpendingPlanRepository(cacheService, logger); + const ethAddressHbarSpendingPlanRepository = new EthAddressHbarSpendingPlanRepository(cacheService, logger); + const ipAddressHbarSpendingPlanRepository = new IPAddressHbarSpendingPlanRepository(cacheService, logger); + hbarLimitService = new HbarLimitService( + hbarSpendingPlanRepository, + ethAddressHbarSpendingPlanRepository, + ipAddressHbarSpendingPlanRepository, + logger, + register, + total, + ); + sdkClient = new SDKClient( client, logger.child({ name: `consensus-node` }), hbarLimiter, new CacheService(logger.child({ name: `cache` }), registry), eventEmitter, + hbarLimitService, ); process.env.GET_RECORD_DEFAULT_TO_CONSENSUS_NODE = 'true'; @@ -235,9 +252,7 @@ describe('SdkClient', async function () { describe('HAPIService', async () => { let hapiService: HAPIService; - let cacheService: CacheService; let originalEnv: NodeJS.ProcessEnv; - let hbarLimitService: HbarLimitService; let initialOperatorKeyFormat: string | undefined; const OPERATOR_KEY_ED25519 = { @@ -268,20 +283,6 @@ describe('SdkClient', async function () { }, }); } - - const total = constants.HBAR_RATE_LIMIT_TINYBAR(); - cacheService = new CacheService(logger, registry); - const hbarSpendingPlanRepository = new HbarSpendingPlanRepository(cacheService, logger); - const ethAddressHbarSpendingPlanRepository = new EthAddressHbarSpendingPlanRepository(cacheService, logger); - const ipAddressHbarSpendingPlanRepository = new IPAddressHbarSpendingPlanRepository(cacheService, logger); - hbarLimitService = new HbarLimitService( - hbarSpendingPlanRepository, - ethAddressHbarSpendingPlanRepository, - ipAddressHbarSpendingPlanRepository, - logger, - register, - total, - ); }); this.beforeEach(() => { diff --git a/packages/relay/tests/lib/services/metricService/metricService.spec.ts b/packages/relay/tests/lib/services/metricService/metricService.spec.ts index 69c868e882..8941a26675 100644 --- a/packages/relay/tests/lib/services/metricService/metricService.spec.ts +++ b/packages/relay/tests/lib/services/metricService/metricService.spec.ts @@ -24,18 +24,22 @@ import { resolve } from 'path'; import * as sinon from 'sinon'; import { config } from 'dotenv'; import EventEmitter from 'events'; -import { Registry } from 'prom-client'; import axios, { AxiosInstance } from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { Utils } from '../../../../src/utils'; +import { register, Registry } from 'prom-client'; import constants from '../../../../src/lib/constants'; import HbarLimit from '../../../../src/lib/hbarlimiter'; import { MirrorNodeClient, SDKClient } from '../../../../src/lib/clients'; import { calculateTxRecordChargeAmount, getRequestId } from '../../../helpers'; +import { HbarLimitService } from '../../../../src/lib/services/hbarLimitService'; import MetricService from '../../../../src/lib/services/metricService/metricService'; import { CacheService } from '../../../../src/lib/services/cacheService/cacheService'; import { IExecuteQueryEventPayload, IExecuteTransactionEventPayload } from '../../../../src/lib/types/events'; import { Hbar, Long, Status, Client, AccountId, TransactionRecord, TransactionRecordQuery } from '@hashgraph/sdk'; +import { HbarSpendingPlanRepository } from '../../../../src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository'; +import { IPAddressHbarSpendingPlanRepository } from '../../../../src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository'; +import { EthAddressHbarSpendingPlanRepository } from '../../../../src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository'; config({ path: resolve(__dirname, '../../../test.env') }); const registry = new Registry(); @@ -137,12 +141,26 @@ describe('Metric Service', function () { hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration, registry); eventEmitter = new EventEmitter(); + const cacheService = new CacheService(logger, registry); + const hbarSpendingPlanRepository = new HbarSpendingPlanRepository(cacheService, logger); + const ethAddressHbarSpendingPlanRepository = new EthAddressHbarSpendingPlanRepository(cacheService, logger); + const ipAddressHbarSpendingPlanRepository = new IPAddressHbarSpendingPlanRepository(cacheService, logger); + const hbarLimitService = new HbarLimitService( + hbarSpendingPlanRepository, + ethAddressHbarSpendingPlanRepository, + ipAddressHbarSpendingPlanRepository, + logger, + register, + total, + ); + const sdkClient = new SDKClient( client, logger.child({ name: `consensus-node` }), hbarLimiter, new CacheService(logger.child({ name: `cache` }), registry), eventEmitter, + hbarLimitService, ); // Init new MetricService instance metricService = new MetricService(logger, sdkClient, mirrorNodeClient, hbarLimiter, registry, eventEmitter);