Skip to content

Commit

Permalink
test: add additional hbar limiter tests
Browse files Browse the repository at this point in the history
Signed-off-by: georgi-l95 <glazarov95@gmail.com>
  • Loading branch information
georgi-l95 committed Jul 12, 2024
1 parent 1aea661 commit 2905700
Show file tree
Hide file tree
Showing 7 changed files with 995 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"acceptancetest:api_batch2": "ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@api-batch-2' --exit",
"acceptancetest:api_batch3": "ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@api-batch-3' --exit",
"acceptancetest:erc20": "ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@erc20' --exit",
"acceptancetest:ratelimiter": "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:ratelimiter": "HBAR_RATE_LIMIT_TINYBAR=300000000 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:tokencreate": "ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@tokencreate' --exit",
"acceptancetest:tokenmanagement": "ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@tokenmanagement' --exit",
"acceptancetest:htsprecompilev1": "ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@htsprecompilev1' --exit",
Expand Down
2 changes: 2 additions & 0 deletions packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ export class SDKClient {
// get transaction fee and add expense to limiter
const createFileRecord = await fileCreateTxResponse.getRecord(this.clientMain);
let transactionFee = createFileRecord.transactionFee;
let transactionFee = createFileRecord.transactionFee as Hbar;
this.hbarLimiter.addExpense(transactionFee.toTinybars().toNumber(), currentDateNow);

this.captureMetrics(
Expand All @@ -692,6 +693,7 @@ export class SDKClient {
// get transaction fee and add expense to limiter
const appendFileRecord = await fileAppendTxResponse.getRecord(this.clientMain);
transactionFee = appendFileRecord.transactionFee;
transactionFee = appendFileRecord.transactionFee as Hbar;
this.hbarLimiter.addExpense(transactionFee.toTinybars().toNumber(), currentDateNow);

this.captureMetrics(
Expand Down
31 changes: 30 additions & 1 deletion packages/server/tests/acceptance/rateLimiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// External resources
import { expect } from 'chai';
import { ethers } from 'ethers';
import { BaseContract, ethers } from 'ethers';
import { AliasAccount } from '../types/AliasAccount';

// Assertions and constants from local resources
Expand All @@ -30,7 +30,12 @@ import relayConstants from '../../../../packages/relay/src/lib/constants';

// Local resources
import parentContractJson from '../contracts/Parent.json';
import EstimateGasContract from '../contracts/EstimateGasContract.json';
import largeSizeContract from '../contracts/hbarLimiterContracts/largeSizeContract.json';
import mediumSizeContract from '../contracts/hbarLimiterContracts/mediumSizeContract.json';

import { Utils } from '../helpers/utils';
import { JsonRpcError, predefined } from '@hashgraph/json-rpc-relay';

describe('@ratelimiter Rate Limiters Acceptance Tests', function () {
this.timeout(480 * 1000); // 480 seconds
Expand Down Expand Up @@ -156,6 +161,30 @@ describe('@ratelimiter Rate Limiters Acceptance Tests', function () {
await expect(relay.call(testConstants.ETH_ENDPOINTS.ETH_SEND_RAW_TRANSACTION, [signedTx], requestId)).to.be
.fulfilled;
});

it('should be able to deploy a contract without creating file', async function () {
// This flow should not spend any hbars from the operator, as it's fully paid by the signer
expect(
await Utils.deployContract(EstimateGasContract.abi, EstimateGasContract.bytecode, accounts[0].wallet),
).to.be.instanceOf(BaseContract);
});

it('should be able to deploy a medium size contract with fileCreate', async function () {
// This flow should spend hbars from the operator, for fileCreate
expect(
await Utils.deployContract(mediumSizeContract.abi, mediumSizeContract.bytecode, accounts[0].wallet),
).to.be.instanceOf(BaseContract);
});

it('should fail to deploy a larger size contract with fileCreate and fileAppend due to hbar limit exceeded', async function () {
// This flow should not allow spending hbars from the operator, for fileCreate operation and fileAppend operation
try {
await Utils.deployContract(largeSizeContract.abi, largeSizeContract.bytecode, accounts[0].wallet);
expect(true).to.be.false;
} catch (e) {
expect(e.message).to.contain(predefined.HBAR_RATE_LIMIT_EXCEEDED.message);
}
});
});
});
});
Loading

0 comments on commit 2905700

Please sign in to comment.