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

test: create e2e tests for basic spending plan limit #3094

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 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
ac43488
fix: fixed conflicts after rebased
quiet-node Oct 11, 2024
007b204
Adds acceptance test for BASIC user spending plans
konstantinabl Oct 9, 2024
52480dc
Fixes CI
konstantinabl Oct 11, 2024
6efbb4a
Fixes failiing acceptance tests and improves setup
konstantinabl Oct 11, 2024
db357b8
fix: switched back HBAR_RATE_LIMIT_DURATION
quiet-node Oct 11, 2024
51cc745
fix: added HBAR_RATE_LIMIT_BASIC
quiet-node Oct 12, 2024
29bffa3
fix: fixed hbar limiter test
quiet-node Oct 12, 2024
3e555ca
fix: re-ordered test cases
quiet-node Oct 12, 2024
69192f0
fix: updated HBAR_RATE_LIMIT_BASIC to be lower than 30hbar
quiet-node Oct 12, 2024
a5afdfe
Reverts basic hbar limit to 30; needed for large deployments
konstantinabl Oct 14, 2024
eda800d
Improves test case and skips unecessary one
konstantinabl Oct 14, 2024
1fbfab7
Improves unlinking of ip addresses
konstantinabl Oct 14, 2024
f688a4e
chore: remove unnecessary timeouts in tests
victor-yanev Oct 14, 2024
6cb081b
Revert "chore: remove unnecessary timeouts in tests"
konstantinabl Oct 14, 2024
179c398
Adds new deleteAll method in ipAddressRepository
konstantinabl Oct 14, 2024
791a27f
Removes timeouts and adds logic to wait for limiter reset
konstantinabl Oct 14, 2024
fca29b1
Adds appropriate limits for acceptance tests
konstantinabl Oct 14, 2024
ccdf203
Adds new variables to .env
konstantinabl Oct 14, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"acceptancetest:api_batch3": "nyc ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@api-batch-3' --exit",
"acceptancetest:erc20": "nyc ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@erc20' --exit",
"acceptancetest:ratelimiter": "nyc ts-mocha packages/ws-server/tests/acceptance/index.spec.ts -g '@web-socket-ratelimiter' --exit && ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@ratelimiter' --exit",
"acceptancetest:hbarlimiter": "HBAR_RATE_LIMIT_TINYBAR=3000000000 nyc ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@hbarlimiter' --exit",
"acceptancetest:hbarlimiter": "HBAR_RATE_LIMIT_BASIC=1000000000 nyc ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@hbarlimiter' --exit",
"acceptancetest:tokencreate": "nyc ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@tokencreate' --exit",
"acceptancetest:tokenmanagement": "nyc ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@tokenmanagement' --exit",
"acceptancetest:htsprecompilev1": "nyc ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@htsprecompilev1' --exit",
Expand Down
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 @@
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 @@
*/
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 @@
*/
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 @@
}

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

Check warning on line 495 in packages/relay/src/lib/clients/sdkClient.ts

View check run for this annotation

Codecov / codecov/patch

packages/relay/src/lib/clients/sdkClient.ts#L495

Added line #L495 was not covered by tests
}

/**
Expand Down Expand Up @@ -603,6 +589,7 @@
* @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 @@
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 @@
interactingEntity,
status,
requestDetails,
originalCallerAddress,
} as IExecuteQueryEventPayload);
}
}
Expand All @@ -683,6 +672,7 @@
* @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 @@
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 @@
txConstructorName,
operatorAccountId: this.clientMain.operatorAccountId!.toString(),
interactingEntity,
originalCallerAddress,
} as IExecuteTransactionEventPayload);
}
}
Expand All @@ -770,6 +764,7 @@
* @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 @@
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 @@
txConstructorName,
operatorAccountId: this.clientMain.operatorAccountId!.toString(),
interactingEntity,
originalCallerAddress,
} as IExecuteTransactionEventPayload);
}
}
Expand All @@ -837,6 +836,7 @@
* @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 @@
callerName: string,
interactingEntity: string,
originalCallerAddress: string,
currentNetworkExchangeRateInCents: number,
): Promise<FileId | null> {
const hexedCallData = Buffer.from(callData).toString('hex');

const estimatedTxFee = Utils.estimateFileTransactionsFee(
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 @@
callerName,
interactingEntity,
requestDetails,
true,
false,
originalCallerAddress,
);

Expand All @@ -877,7 +897,7 @@
callerName,
interactingEntity,
requestDetails,
true,
false,
originalCallerAddress,
);
}
Expand All @@ -889,6 +909,7 @@
callerName,
interactingEntity,
requestDetails,
originalCallerAddress,
);

if (fileInfo.size.isZero()) {
Expand Down Expand Up @@ -942,6 +963,7 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@
}
}

/**
* Deletes all IP address spending plans from the cache.
*
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
* @returns {Promise<void>} - A promise that resolves when all IP address spending plans are deleted.
*/
async deleteAll(requestDetails: RequestDetails): Promise<void> {
const key = this.getKey('*');
const keys = await this.cache.keys(key, 'deleteAll', requestDetails);
for (const key of keys) {
await this.cache.delete(key, 'deleteAll', requestDetails);

Check warning on line 115 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts

View check run for this annotation

Codecov / codecov/patch

packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts#L112-L115

Added lines #L112 - L115 were not covered by tests
}
this.logger.trace(`${requestDetails.formattedRequestId} Deleted all IP address spending plans`);

Check warning on line 117 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts

View check run for this annotation

Codecov / codecov/patch

packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts#L117

Added line #L117 was not covered by tests
}

/**
* Gets all IP address spending plans from the cache.
*
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
* @returns {Promise<IPAddressHbarSpendingPlan[]>} - A promise that resolves with an array of IP address spending plans.
*/
async getAllPlans(requestDetails: RequestDetails): Promise<IPAddressHbarSpendingPlan[]> {
const pattern = `${this.collectionKey}:*`;
const keys = await this.cache.keys(pattern, 'getAllPlans', requestDetails);
const plans = await Promise.all(
keys.map((key) => this.cache.getAsync<IIPAddressHbarSpendingPlan>(key, 'getAllPlans', requestDetails)),

Check warning on line 130 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts

View check run for this annotation

Codecov / codecov/patch

packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts#L127-L130

Added lines #L127 - L130 were not covered by tests
);
return plans.filter((plan) => plan !== null).map((plan) => new IPAddressHbarSpendingPlan(plan));

Check warning on line 132 in packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts

View check run for this annotation

Codecov / codecov/patch

packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts#L132

Added line #L132 was not covered by tests
}

/**
* Finds an {@link IPAddressHbarSpendingPlan} for an IP address.
*
Expand Down
Loading
Loading