Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replaced HbarLimit module with the new HbarLimitService class #3024

Open
wants to merge 35 commits into
base: 2739-Redesign-of-the-hbar-rate-limiter
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
916b17a
fix: converted constant HBAR_RATE_LIMIT_DURATION from var into function
quiet-node Sep 19, 2024
ce2463f
chore: added jsdoc to private vars in hapiService module
quiet-node Sep 19, 2024
51e2a99
feat: initialized an instance of HbarLimitService in relay.ts
quiet-node Sep 19, 2024
faf053d
feat: integrated HbarLimitService instance into HapiService class
quiet-node Sep 19, 2024
cc9973f
feat: integrated HbarLimitService instance into SDKClient class
quiet-node Sep 19, 2024
5397689
feat: integrated HbarLimitService instance into MetricService class
quiet-node Sep 19, 2024
8e6de47
fix: modified addExpense to turn ethAddress to be optional
quiet-node Sep 19, 2024
a93232d
feat: added getRemainingBudget() getter
quiet-node Sep 19, 2024
03a0f52
feat: added originalCallerAddress to IExecuteTransactionEventPayload …
quiet-node Sep 19, 2024
b88c6d9
feat: removed metricService instance in relay
quiet-node Sep 19, 2024
921a587
feat: replaced hbarLimitter with hbarLimitService in MetricService class
quiet-node Sep 19, 2024
b5d3b63
fix: added estimateFileTransactionsFee to Utils
quiet-node Sep 20, 2024
39dceda
feat: added txConstructorName and updated log messages for shouldLimit
quiet-node Sep 20, 2024
4fc106e
fix: rework logic for estimateFileTransactionsFee
quiet-node Sep 20, 2024
391b7f2
feat: removed hbarLimiter instance in SDKClient classes
quiet-node Sep 20, 2024
98550c5
feat: deleted HbarLimit module from codease
quiet-node Sep 20, 2024
065f2cc
feat: reverted logic reworked on estimateFileTransactionsFee
quiet-node Sep 20, 2024
ee9b413
feat: added preemptive rate limit logic to createFile() method
quiet-node Sep 20, 2024
06a6b80
test: updated hbarLimiter.spec.ts
quiet-node Sep 20, 2024
5e0c9a9
fix: added names for child loggers for spending plan repo
quiet-node Sep 30, 2024
75e036c
chore: reverted "feat: added getRemainingBudget() getter"
quiet-node Sep 30, 2024
f9a98c8
fix: updated log message
quiet-node Sep 30, 2024
0db87e3
test: added HBAR_DAILY_LIMIT_BASIC to localTestEnv
quiet-node Sep 30, 2024
b402ece
fix: converted function HBAR_RATE_LIMIT_DURATION from function into var
quiet-node Oct 2, 2024
c540c97
fix: reverted "feat: removed metricService instance in relay"
quiet-node Oct 2, 2024
58e9afc
chore: updated log message
quiet-node Oct 2, 2024
b54f4a7
fix: fixed failing test in hapiService
quiet-node Oct 3, 2024
f47dc27
fix: reverted ethAddress back to be a required param for addExpense
quiet-node Oct 3, 2024
330f1fb
fix: fixed failing test in hapiService.spec.ts
quiet-node Oct 8, 2024
eb39c8d
fix: loaded env into prcess.env for constant module
quiet-node Oct 8, 2024
c001c47
chore: removed duplicating doc for estimateFileTransactionsFee
quiet-node Oct 8, 2024
650e420
chore: sort imports
victor-yanev Oct 10, 2024
c64b64c
fix: removed ipAddresses from log in hbarLimitService
quiet-node Oct 10, 2024
c32adfc
fix: renamed isDailyBudgetExceeded -> isTotalBudgetExceeded
quiet-node Oct 10, 2024
99603f7
fix: fixed conflicts after rebased
quiet-node Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 59 additions & 37 deletions packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ import {
TransactionResponse,
} from '@hashgraph/sdk';
import { Logger } from 'pino';
import { Utils } from '../../utils';
import { EventEmitter } from 'events';
import HbarLimit from '../hbarlimiter';
import constants from './../constants';
import { BigNumber } from '@hashgraph/sdk/lib/Transfer';
import { SDKClientError } from '../errors/SDKClientError';
import { JsonRpcError, predefined } from '../errors/JsonRpcError';
import { CacheService } from '../services/cacheService/cacheService';
import { weibarHexToTinyBarInt } from '../../formatters';
import { SDKClientError } from './../errors/SDKClientError';
import { HbarLimitService } from '../services/hbarLimitService';
import { JsonRpcError, predefined } from './../errors/JsonRpcError';
import { CacheService } from '../services/cacheService/cacheService';
import {
IExecuteQueryEventPayload,
IExecuteTransactionEventPayload,
Expand All @@ -85,12 +86,6 @@ export class SDKClient {
*/
private readonly logger: Logger;

/**
* This limiter tracks hbar expenses and limits.
* @private
*/
private readonly hbarLimiter: HbarLimit;

/**
* LRU cache container.
* @private
Expand Down Expand Up @@ -118,21 +113,28 @@ 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.
*
* @param {Client} clientMain - The primary Hedera client instance used for executing transactions and queries.
* @param {Logger} logger - The logger instance for logging information, warnings, and errors.
* @param {HbarLimit} hbarLimiter - The Hbar rate limiter instance for managing Hbar transaction budgets.
* @param {CacheService} cacheService - The cache service instance used for caching and retrieving data.
* @param {EventEmitter} eventEmitter - The eventEmitter used for emitting and handling events within the class.
*/
constructor(
clientMain: Client,
logger: Logger,
hbarLimiter: HbarLimit,
cacheService: CacheService,
eventEmitter: EventEmitter,
hbarLimitService: HbarLimitService,
) {
this.clientMain = clientMain;

Expand All @@ -143,9 +145,9 @@ export class SDKClient {
}

this.logger = logger;
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 Expand Up @@ -417,30 +419,14 @@ export class SDKClient {
if (ethereumTransactionData.callData.length <= this.fileAppendChunkSize) {
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes());
} else {
const isPreemptiveCheckOn = process.env.HBAR_RATE_LIMIT_PREEMPTIVE_CHECK === 'true';

if (isPreemptiveCheckOn) {
const hexCallDataLength = Buffer.from(ethereumTransactionData.callData).toString('hex').length;
const shouldPreemptivelyLimit = this.hbarLimiter.shouldPreemptivelyLimitFileTransactions(
originalCallerAddress,
hexCallDataLength,
this.fileAppendChunkSize,
currentNetworkExchangeRateInCents,
requestDetails,
);

if (shouldPreemptivelyLimit) {
throw predefined.HBAR_RATE_LIMIT_PREEMPTIVE_EXCEEDED;
}
}

fileId = await this.createFile(
ethereumTransactionData.callData,
this.clientMain,
requestDetails,
callerName,
interactingEntity,
originalCallerAddress,
currentNetworkExchangeRateInCents,
);
if (!fileId) {
throw new SDKClientError({}, `${requestDetails.formattedRequestId} No fileId created for transaction. `);
Expand Down Expand Up @@ -506,7 +492,7 @@ export class SDKClient {
contractCallQuery.setPaymentTransactionId(TransactionId.generate(this.clientMain.operatorAccountId));
}

return this.executeQuery(contractCallQuery, this.clientMain, callerName, to, requestDetails);
return this.executeQuery(contractCallQuery, this.clientMain, callerName, to, requestDetails, from);
}

/**
Expand Down Expand Up @@ -603,6 +589,7 @@ export class SDKClient {
* @param {string} callerName - The name of the caller executing the query.
* @param {string} interactingEntity - The entity interacting with the query.
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
* @param {string} [originalCallerAddress] - The optional address of the original caller making the request.
* @returns {Promise<T>} A promise resolving to the query response.
* @throws {Error} Throws an error if the query fails or if rate limits are exceeded.
* @template T - The type of the query response.
Expand All @@ -613,6 +600,7 @@ export class SDKClient {
callerName: string,
interactingEntity: string,
requestDetails: RequestDetails,
originalCallerAddress?: string,
): Promise<T> {
const queryConstructorName = query.constructor.name;
const requestIdPrefix = requestDetails.formattedRequestId;
Expand Down Expand Up @@ -669,6 +657,7 @@ export class SDKClient {
interactingEntity,
status,
requestDetails,
originalCallerAddress,
} as IExecuteQueryEventPayload);
}
}
Expand All @@ -683,6 +672,7 @@ export class SDKClient {
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
* @param {boolean} shouldThrowHbarLimit - Flag to indicate whether to check HBAR limits.
* @param {string} originalCallerAddress - The address of the original caller making the request.
* @param {number} [estimatedTxFee] - The optioanl total estimated transaction fee.
* @returns {Promise<TransactionResponse>} - A promise that resolves to the transaction response.
* @throws {SDKClientError} - Throws if an error occurs during transaction execution.
*/
Expand All @@ -693,19 +683,22 @@ export class SDKClient {
requestDetails: RequestDetails,
shouldThrowHbarLimit: boolean,
originalCallerAddress: string,
estimatedTxFee?: number,
): Promise<TransactionResponse> {
const txConstructorName = transaction.constructor.name;
let transactionId: string = '';
let transactionResponse: TransactionResponse | null = null;

if (shouldThrowHbarLimit) {
const shouldLimit = this.hbarLimiter.shouldLimit(
Date.now(),
const shouldLimit = await this.hbarLimitService.shouldLimit(
constants.EXECUTION_MODE.TRANSACTION,
callerName,
txConstructorName,
originalCallerAddress,
requestDetails,
estimatedTxFee,
);

if (shouldLimit) {
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
}
Expand Down Expand Up @@ -756,6 +749,7 @@ export class SDKClient {
txConstructorName,
operatorAccountId: this.clientMain.operatorAccountId!.toString(),
interactingEntity,
originalCallerAddress,
} as IExecuteTransactionEventPayload);
}
}
Expand All @@ -770,6 +764,7 @@ export class SDKClient {
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
* @param {boolean} shouldThrowHbarLimit - Flag to indicate whether to check HBAR limits.
* @param {string} originalCallerAddress - The address of the original caller making the request.
* @param {number} [estimatedTxFee] - The optioanl total estimated transaction fee.
* @returns {Promise<void>} - A promise that resolves when the batch execution is complete.
* @throws {SDKClientError} - Throws if an error occurs during batch transaction execution.
*/
Expand All @@ -780,18 +775,21 @@ export class SDKClient {
requestDetails: RequestDetails,
shouldThrowHbarLimit: boolean,
originalCallerAddress: string,
estimatedTxFee?: number,
): Promise<void> {
const txConstructorName = transaction.constructor.name;
let transactionResponses: TransactionResponse[] | null = null;

if (shouldThrowHbarLimit) {
const shouldLimit = this.hbarLimiter.shouldLimit(
Date.now(),
const shouldLimit = await this.hbarLimitService.shouldLimit(
constants.EXECUTION_MODE.TRANSACTION,
callerName,
txConstructorName,
originalCallerAddress,
requestDetails,
estimatedTxFee,
);

if (shouldLimit) {
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
}
Expand Down Expand Up @@ -822,6 +820,7 @@ export class SDKClient {
txConstructorName,
operatorAccountId: this.clientMain.operatorAccountId!.toString(),
interactingEntity,
originalCallerAddress,
} as IExecuteTransactionEventPayload);
}
}
Expand All @@ -837,6 +836,7 @@ export class SDKClient {
* @param {string} callerName - The name of the caller creating the file.
* @param {string} interactingEntity - The entity interacting with the transaction.
* @param {string} originalCallerAddress - The address of the original caller making the request.
* @param {number} currentNetworkExchangeRateInCents - The current network exchange rate in cents per HBAR.
* @returns {Promise<FileId | null>} A promise that resolves to the created file ID or null if the creation failed.
* @throws Will throw an error if the created file is empty or if any transaction fails during execution.
*/
Expand All @@ -847,9 +847,29 @@ export class SDKClient {
callerName: string,
interactingEntity: string,
originalCallerAddress: string,
currentNetworkExchangeRateInCents: number,
): Promise<FileId | null> {
const hexedCallData = Buffer.from(callData).toString('hex');

const estimatedTxFee = Utils.estimateFileTransactionsFee(
quiet-node marked this conversation as resolved.
Show resolved Hide resolved
hexedCallData.length,
this.fileAppendChunkSize,
currentNetworkExchangeRateInCents,
);

const shouldPreemptivelyLimit = await this.hbarLimitService.shouldLimit(
constants.EXECUTION_MODE.TRANSACTION,
callerName,
this.createFile.name,
originalCallerAddress,
requestDetails,
estimatedTxFee,
);

if (shouldPreemptivelyLimit) {
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
}

const fileCreateTx = new FileCreateTransaction()
.setContents(hexedCallData.substring(0, this.fileAppendChunkSize))
.setKeys(client.operatorPublicKey ? [client.operatorPublicKey] : []);
Expand All @@ -859,7 +879,7 @@ export class SDKClient {
callerName,
interactingEntity,
requestDetails,
true,
false,
originalCallerAddress,
);
victor-yanev marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -877,7 +897,7 @@ export class SDKClient {
callerName,
interactingEntity,
requestDetails,
true,
false,
victor-yanev marked this conversation as resolved.
Show resolved Hide resolved
originalCallerAddress,
);
}
Expand All @@ -889,6 +909,7 @@ export class SDKClient {
callerName,
interactingEntity,
requestDetails,
originalCallerAddress,
);

if (fileInfo.size.isZero()) {
Expand Down Expand Up @@ -942,6 +963,7 @@ export class SDKClient {
callerName,
interactingEntity,
requestDetails,
originalCallerAddress,
);

if (fileInfo.isDeleted) {
Expand Down
4 changes: 4 additions & 0 deletions packages/relay/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
*
*/

import dotenv from 'dotenv';
import findConfig from 'find-config';
import { BigNumber } from 'bignumber.js';

dotenv.config({ path: findConfig('.env') || '' });

enum CACHE_KEY {
ACCOUNT = 'account',
ETH_BLOCK_NUMBER = 'eth_block_number',
Expand Down
Loading
Loading