Skip to content

Commit

Permalink
fix: Allow the HBar Rate Limiter to be disabled.
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Badiere <[email protected]>
  • Loading branch information
ebadiere committed Nov 12, 2024
1 parent 264d2cb commit 088a959
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/relay/src/lib/services/hbarLimitService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export class HbarLimitService implements IHbarLimitService {
PRIVILEGED: Hbar.fromTinybars(constants.HBAR_RATE_LIMIT_PRIVILEGED),
};

/**
* Disables the rate limiter.
* @private
*/
private readonly disableRateLimiter: boolean = false;

/**
* Counts the number of times the rate limit has been reached.
* @private
Expand Down Expand Up @@ -90,6 +96,10 @@ export class HbarLimitService implements IHbarLimitService {
this.reset = this.getResetTimestamp();
this.remainingBudget = this.totalBudget;

if (this.remainingBudget.toTinybars().isZero()) {
this.disableRateLimiter = true;
}

const metricCounterName = 'rpc_relay_hbar_rate_limit';
this.register.removeSingleMetric(metricCounterName);
this.hbarLimitCounter = new Counter({
Expand Down Expand Up @@ -181,6 +191,10 @@ export class HbarLimitService implements IHbarLimitService {
requestDetails: RequestDetails,
estimatedTxFee: number = 0,
): Promise<boolean> {
if (this.disableRateLimiter) {
return false;
}

const ipAddress = requestDetails.ipAddress;
if (await this.isTotalBudgetExceeded(mode, methodName, txConstructorName, estimatedTxFee, requestDetails)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,29 @@ describe('HBAR Rate Limit Service', function () {
expect(result).to.be.false;
});
});

describe('It should be able to disable the rate limiter', function () {
const hbarLimitServiceDisabled = new HbarLimitService(
hbarSpendingPlanRepositoryStub,
ethAddressHbarSpendingPlanRepositoryStub,
ipAddressHbarSpendingPlanRepositoryStub,
logger,
register,
Hbar.fromTinybars(0),
limitDuration,
);
it('should return false if the rate limiter is disabled by setting HBAR_RATE_LIMIT_TINYBAR to zero', async function () {
// hbarLimitServiceDisabled.disableRateLimiter();
const result = await hbarLimitServiceDisabled.shouldLimit(
mode,
methodName,
txConstructorName,
'',
requestDetails,
);
expect(result).to.be.false;
});
});
});

describe('getSpendingPlan', function () {
Expand Down

0 comments on commit 088a959

Please sign in to comment.