Skip to content

Commit

Permalink
fix: fixed hbarLimiter test
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Sep 3, 2024
1 parent 615c686 commit ec0904e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/relay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export interface Eth {

protocolVersion(requestId?: string): JsonRpcError;

sendRawTransaction(transaction: string, requestId?: string): Promise<string | JsonRpcError>;
sendRawTransaction(transaction: string, requestId: string): Promise<string | JsonRpcError>;

sendTransaction(requestId?: string): JsonRpcError;

Expand Down
3 changes: 0 additions & 3 deletions packages/relay/src/lib/hbarlimiter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ export default class HbarLimit {
currentNetworkExchangeRateInCents,
);

console.log(`remainingBudget: ${this.remainingBudget}`);
console.log(`totalFeeInTinyBar: ${totalFeeInTinyBar}`);

if (this.remainingBudget - totalFeeInTinyBar < 0) {
this.logger.trace(
`${requestIdPrefix} HBAR preemtive rate limit incoming call - the total preemptive transaction fee exceeds the current remaining HBAR budget due to an excessively large callData size: remainingBudget=${this.remainingBudget}, total=${this.total}, resetTimestamp=${this.reset}, callDataSize=${callDataSize}, numFileCreateTxs=${numFileCreateTxs}, numFileAppendTxs=${numFileAppendTxs}, totalFeeInTinyBar=${totalFeeInTinyBar}, exchangeRateInCents=${currentNetworkExchangeRateInCents}`,
Expand Down
22 changes: 17 additions & 5 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2127,9 +2127,9 @@ describe('SdkClient', async function () {
124, 18, 190, 114, 79, 189, 99, 27, 157, 117, 227, 107, 74, 255, 111, 74, 166, 7, 78, 25, 35, 131, 85, 47, 52,
120, 20,
]);
const fileCreateFee = Number(process.env.HOT_FIX_FILE_CREATE_FEE || 100000000); // 1 hbar
const fileDeleteFee = Number(process.env.HOT_FIX_FILE_DELETE_FEE || 11000000); // 0.11 hbar
const fileAppendFee = Number(process.env.HOT_FIX_FILE_APPEND_FEE || 120000000); // 1.2 hbar
const fileCreateFee = 100000000; // 1 hbar
const fileDeleteFee = 11000000; // 0.11 hbar
const fileAppendFee = 120000000; // 1.2 hbar
const mockedExchangeRateIncents = 12;
const mockedTransactionRecordFee = calculateTxRecordChargeAmount(mockedExchangeRateIncents);
const defaultTransactionFee = 1000;
Expand Down Expand Up @@ -2271,7 +2271,13 @@ describe('SdkClient', async function () {
.returns(true);

try {
await sdkClient.submitEthereumTransaction(transactionBuffer, mockedCallerName, requestId, randomAccountAddress);
await sdkClient.submitEthereumTransaction(
transactionBuffer,
mockedCallerName,
requestId,
randomAccountAddress,
mockedExchangeRateIncents,
);
expect.fail(`Expected an error but nothing was thrown`);
} catch (error: any) {
expect(error.message).to.equal('HBAR Rate limit exceeded');
Expand Down Expand Up @@ -2324,7 +2330,13 @@ describe('SdkClient', async function () {
.withArgs(mockedTransactionRecordFee)
.exactly(fileAppendChunks + 2);

await sdkClient.submitEthereumTransaction(transactionBuffer, mockedCallerName, requestId, randomAccountAddress);
await sdkClient.submitEthereumTransaction(
transactionBuffer,
mockedCallerName,
requestId,
randomAccountAddress,
mockedExchangeRateIncents,
);

expect(queryStub.called).to.be.true;
expect(transactionStub.called).to.be.true;
Expand Down
44 changes: 22 additions & 22 deletions packages/server/tests/acceptance/hbarLimiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,6 @@ describe('@hbarlimiter HBAR Limiter Acceptance Tests', function () {
});

describe('Rate Limit', () => {
it('Should preemtively check the rate limit before submitting EthereumTransaction', async function () {
const remainingHbarsBefore = Number(await metrics.get(testConstants.METRICS.REMAINING_HBAR_LIMIT));

process.env.HBAR_RATE_LIMIT_PREEMTIVE_CHECK = 'true';
process.env.HOT_FIX_FILE_APPEND_FEE = (remainingHbarsBefore - 100000000).toString();

try {
const largeContract = await Utils.deployContract(
largeContractJson.abi,
largeContractJson.bytecode,
accounts[0].wallet,
);
await largeContract.waitForDeployment();

expect(true).to.be.false;
} catch (e) {
expect(e.message).to.contain(predefined.HBAR_RATE_LIMIT_PREEMTIVE_EXCEEDED.message);
}

delete process.env.HBAR_RATE_LIMIT_PREEMTIVE_CHECK;
});

it('HBAR limiter is updated within acceptable tolerance range in relation to actual spent amount by the relay operator', async function () {
const TOLERANCE = 0.02;
const remainingHbarsBefore = Number(await metrics.get(testConstants.METRICS.REMAINING_HBAR_LIMIT));
Expand All @@ -288,6 +266,28 @@ describe('@hbarlimiter HBAR Limiter Acceptance Tests', function () {
Assertions.expectWithinTolerance(amountPaidByOperator, totalOperatorFees, TOLERANCE);
});

it('Should preemtively check the rate limit before submitting EthereumTransaction', async function () {
process.env.HBAR_RATE_LIMIT_PREEMTIVE_CHECK = 'true';
try {
for (let i = 0; i < 50; i++) {
const largeContract = await Utils.deployContract(
largeContractJson.abi,
largeContractJson.bytecode,
accounts[0].wallet,
);
await largeContract.waitForDeployment();
}
expect.fail('The rate limiter will throw HBAR_RATE_LIMIT_PREEMTIVE_EXCEEDED error');
} catch (e) {
console.log(`asdaksjdhuqwiyeiqwuyhdkajshdkajhsiduuqywe`);
console.log(e);

expect(e.message).to.contain(predefined.HBAR_RATE_LIMIT_PREEMTIVE_EXCEEDED.message);
}

delete process.env.HBAR_RATE_LIMIT_PREEMTIVE_CHECK;
});

it('multiple deployments of large contracts should eventually exhaust the remaining hbar limit', async function () {
const remainingHbarsBefore = Number(await metrics.get(testConstants.METRICS.REMAINING_HBAR_LIMIT));
let lastRemainingHbars = remainingHbarsBefore;
Expand Down

0 comments on commit ec0904e

Please sign in to comment.