Skip to content

Commit

Permalink
feat: integrated HbarLimitService instance into SDKClient class
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
  • Loading branch information
quiet-node committed Sep 30, 2024
1 parent 7d76233 commit b8d83c2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
11 changes: 11 additions & 0 deletions packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
*
Expand All @@ -129,6 +138,7 @@ export class SDKClient {
hbarLimiter: HbarLimit,
cacheService: CacheService,
eventEmitter: EventEmitter,
hbarLimitService: HbarLimitService,
) {
this.clientMain = clientMain;

Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions packages/relay/src/lib/services/hapiService/hapiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export default class HAPIService {
this.hbarLimiter,
this.cacheService,
this.eventEmitter,
this.hbarLimitService,
);
}

Expand Down
33 changes: 17 additions & 16 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b8d83c2

Please sign in to comment.