Skip to content

Commit

Permalink
feat: integrated HbarLimitService instance into HapiService 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 0dc82e6 commit 7d76233
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 54 deletions.
11 changes: 10 additions & 1 deletion packages/relay/src/lib/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ export class RelayImpl implements Relay {

this.eventEmitter = new EventEmitter();
this.cacheService = new CacheService(logger.child({ name: 'cache-service' }), register);
const hapiService = new HAPIService(logger, register, hbarLimiter, this.cacheService, this.eventEmitter);

const hbarSpendingPlanRepository = new HbarSpendingPlanRepository(this.cacheService, logger);
const ethAddressHbarSpendingPlanRepository = new EthAddressHbarSpendingPlanRepository(this.cacheService, logger);
Expand All @@ -143,6 +142,16 @@ export class RelayImpl implements Relay {
register,
total,
);

const hapiService = new HAPIService(
logger,
register,
hbarLimiter,
this.cacheService,
this.eventEmitter,
hbarLimitService,
);

this.clientMain = hapiService.getMainClientInstance();

this.web3Impl = new Web3Impl(this.clientMain);
Expand Down
11 changes: 11 additions & 0 deletions packages/relay/src/lib/services/hapiService/hapiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Utils } from './../../../utils';
import HbarLimit from '../../hbarlimiter';
import { Registry, Counter } from 'prom-client';
import { SDKClient } from '../../clients/sdkClient';
import { HbarLimitService } from '../hbarLimitService';
import { CacheService } from '../cacheService/cacheService';
import { AccountId, Client, PrivateKey } from '@hashgraph/sdk';
import fs from 'fs';
Expand Down Expand Up @@ -141,6 +142,13 @@ export default class HAPIService {
*/
private readonly hbarLimiter: HbarLimit;

/**
* An instance of the HbarLimitService that tracks hbar expenses and limits.
* @private
* @readonly
* @type {HbarLimitService}
*/
private readonly hbarLimitService: HbarLimitService;
/**
* An instance of EventEmitter used for emitting and handling events within the class.
* @private
Expand Down Expand Up @@ -186,13 +194,15 @@ export default class HAPIService {
* @param {HbarLimit} hbarLimiter - The Hbar rate limiter instance.
* @param {CacheService} cacheService - The cache service instance.
* @param {EventEmitter} eventEmitter - The event emitter instance used for emitting events.
* @param {HbarLimitService} hbarLimitService - An HBAR Rate Limit service that tracks hbar expenses and limits.
*/
constructor(
logger: Logger,
register: Registry,
hbarLimiter: HbarLimit,
cacheService: CacheService,
eventEmitter: EventEmitter,
hbarLimitService: HbarLimitService,
) {
dotenv.config({ path: findConfig('.env') || '' });
if (fs.existsSync(findConfig('.env') || '')) {
Expand All @@ -204,6 +214,7 @@ export default class HAPIService {
this.logger = logger;
this.hbarLimiter = hbarLimiter;

this.hbarLimitService = hbarLimitService;
this.eventEmitter = eventEmitter;
this.hederaNetwork = (process.env.HEDERA_NETWORK || this.config.HEDERA_NETWORK || '{}').toLowerCase();
this.clientMain = this.initClient(logger, this.hederaNetwork);
Expand Down
27 changes: 25 additions & 2 deletions packages/relay/tests/lib/eth/eth-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@

import { CacheService } from '../../../src/lib/services/cacheService/cacheService';
import pino from 'pino';
import { Registry } from 'prom-client';
import { register, Registry } from 'prom-client';
import constants from '../../../src/lib/constants';
import HbarLimit from '../../../src/lib/hbarlimiter';
import HAPIService from '../../../src/lib/services/hapiService/hapiService';
import MockAdapter from 'axios-mock-adapter';
import { MirrorNodeClient } from '../../../src/lib/clients/mirrorNodeClient';
import { EthImpl } from '../../../src/lib/eth';
import { EventEmitter } from 'stream';
import { EthAddressHbarSpendingPlanRepository } from '../../../src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository';
import { HbarSpendingPlanRepository } from '../../../src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository';
import { IPAddressHbarSpendingPlanRepository } from '../../../src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository';
import { HbarLimitService } from '../../../src/lib/services/hbarLimitService';

export function contractResultsByNumberByIndexURL(number: number, index: number): string {
return `contracts/results?block.number=${number}&transaction.index=${index}&limit=100&order=asc`;
Expand Down Expand Up @@ -65,7 +69,26 @@ export function generateEthTestEnv(fixedFeeHistory = false) {
const hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration, registry);
const eventEmitter = new EventEmitter();

const hapiServiceInstance = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
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 hapiServiceInstance = new HAPIService(
logger,
registry,
hbarLimiter,
cacheService,
eventEmitter,
hbarLimitService,
);

// @ts-ignore
const ethImpl = new EthImpl(hapiServiceInstance, mirrorNodeInstance, logger, '0x12a', registry, cacheService);
Expand Down
27 changes: 22 additions & 5 deletions packages/relay/tests/lib/ethGetBlockBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ import dotenv from 'dotenv';
import MockAdapter from 'axios-mock-adapter';
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { Registry } from 'prom-client';
import { register, Registry } from 'prom-client';

dotenv.config({ path: path.resolve(__dirname, '../test.env') });
import { EthImpl } from '../../src/lib/eth';
import { MirrorNodeClient } from '../../src/lib/clients/mirrorNodeClient';

import pino from 'pino';
import { EventEmitter } from 'events';
import constants from '../../src/lib/constants';
import HAPIService from '../../src/lib/services/hapiService/hapiService';
import HbarLimit from '../../src/lib/hbarlimiter';
import { Log, Transaction } from '../../src/lib/model';
import { nullableNumberTo0x, numberTo0x, nanOrNumberTo0x, toHash32 } from '../../../../packages/relay/src/formatters';
import HAPIService from '../../src/lib/services/hapiService/hapiService';
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
import { defaultDetailedContractResults, useInMemoryRedisServer } from '../helpers';
import { EventEmitter } from 'events';
import { HbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository';
import { nullableNumberTo0x, numberTo0x, nanOrNumberTo0x, toHash32 } from '../../../../packages/relay/src/formatters';
import { IPAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository';
import { EthAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository';

use(chaiAsPromised);

Expand Down Expand Up @@ -137,7 +141,20 @@ describe('eth_getBlockBy', async function () {
const total = constants.HBAR_RATE_LIMIT_TINYBAR();
const hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration, registry);
const eventEmitter = new EventEmitter();
hapiServiceInstance = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);

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,
);

hapiServiceInstance = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);

process.env.ETH_FEE_HISTORY_FIXED = 'false';

Expand Down
33 changes: 25 additions & 8 deletions packages/relay/tests/lib/hapiService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ import path from 'path';
import dotenv from 'dotenv';
import { expect } from 'chai';
import EventEmitter from 'events';
import { Registry } from 'prom-client';
import { register, Registry } from 'prom-client';
import { Client } from '@hashgraph/sdk';
import { SDKClient } from '../../src/lib/clients';
import HbarLimit from '../../src/lib/hbarlimiter';
import HAPIService from '../../src/lib/services/hapiService/hapiService';
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
import { EthAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository';
import { HbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository';
import { IPAddressHbarSpendingPlanRepository } from '../../src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository';
import { HbarLimitService } from '../../src/lib/services/hbarLimitService';

dotenv.config({ path: path.resolve(__dirname, '../test.env') });

Expand All @@ -41,6 +45,7 @@ describe('HAPI Service', async function () {
let cacheService: CacheService;
let eventEmitter: EventEmitter;
let hapiService: HAPIService;
let hbarLimitService: HbarLimitService;

const errorStatus = 50;

Expand All @@ -50,6 +55,18 @@ describe('HAPI Service', async function () {
eventEmitter = new EventEmitter();
cacheService = new CacheService(logger.child({ name: `cache` }), registry);
hbarLimiter = new HbarLimit(logger.child({ name: 'hbar-rate-limit' }), Date.now(), total, duration, 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 All @@ -59,7 +76,7 @@ describe('HAPI Service', async function () {
});

it('should be able to initialize SDK instance', async function () {
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);
const client = hapiService.getMainClientInstance();
const sdkClient = hapiService.getSDKClient();

Expand All @@ -69,7 +86,7 @@ describe('HAPI Service', async function () {

it('should be able to reinitialise SDK instance upon reaching transaction limit', async function () {
process.env.HAPI_CLIENT_TRANSACTION_RESET = '2';
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getTransactionCount()).to.eq(parseInt(process.env.HAPI_CLIENT_TRANSACTION_RESET!));

const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -86,7 +103,7 @@ describe('HAPI Service', async function () {

it('should be able to reinitialise SDK instance upon reaching time limit', async function () {
process.env.HAPI_CLIENT_DURATION_RESET = '100';
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getTimeUntilReset()).to.eq(parseInt(process.env.HAPI_CLIENT_DURATION_RESET!));

const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -102,7 +119,7 @@ describe('HAPI Service', async function () {

it('should be able to reinitialise SDK instance upon error status code encounter', async function () {
process.env.HAPI_CLIENT_ERROR_RESET = '[50]';
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getErrorCodes()[0]).to.eq(JSON.parse(process.env.HAPI_CLIENT_ERROR_RESET!)[0]);

const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -120,7 +137,7 @@ describe('HAPI Service', async function () {
process.env.HAPI_CLIENT_ERROR_RESET = '[50]';
process.env.HAPI_CLIENT_TRANSACTION_RESET = '50';
process.env.HAPI_CLIENT_DURATION_RESET = '36000';
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);

expect(hapiService.getErrorCodes()[0]).to.eq(JSON.parse(process.env.HAPI_CLIENT_ERROR_RESET!)[0]);
const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -141,7 +158,7 @@ describe('HAPI Service', async function () {
process.env.HAPI_CLIENT_TRANSACTION_RESET = '50';
process.env.HAPI_CLIENT_DURATION_RESET = '36000';
const costAmount = 10000;
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);

const hbarLimiterBudgetBefore = hbarLimiter.getRemainingBudget();
const oldClientInstance = hapiService.getMainClientInstance();
Expand All @@ -164,7 +181,7 @@ describe('HAPI Service', async function () {
process.env.HAPI_CLIENT_DURATION_RESET = '0';
process.env.HAPI_CLIENT_ERROR_RESET = '[]';

hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter);
hapiService = new HAPIService(logger, registry, hbarLimiter, cacheService, eventEmitter, hbarLimitService);
expect(hapiService.getTransactionCount()).to.eq(parseInt(process.env.HAPI_CLIENT_TRANSACTION_RESET!));

const oldClientInstance = hapiService.getMainClientInstance();
Expand Down
Loading

0 comments on commit 7d76233

Please sign in to comment.