Skip to content

Commit

Permalink
test: added acceptancetest for rate limiter in the WS
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
  • Loading branch information
quiet-node committed May 14, 2024
1 parent 2b64c7c commit 92dff3d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
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/server/tests/acceptance/index.spec.ts -g '@ratelimiter' --exit",
"acceptancetest:ratelimiter": "ts-mocha packages/server/tests/acceptance/index.spec.ts -g '@ratelimiter' --exit && ts-mocha packages/ws-server/tests/acceptance/index.spec.ts -g '@web-socket-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
107 changes: 107 additions & 0 deletions packages/ws-server/tests/acceptance/rateLimiter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*-
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// external resources
import { expect } from 'chai';
import { WsTestHelper } from '../helper';
import relayConstants from '@hashgraph/json-rpc-relay/src/lib/constants';
import { AliasAccount } from '@hashgraph/json-rpc-server/tests/types/AliasAccount';
import { IPRateLimitExceeded } from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcError';

describe('@web-socket-ratelimiter Rate Limit Tests', async function () {
const rateLimitTier1 = Number(process.env.TIER_1_RATE_LIMIT || relayConstants.DEFAULT_RATE_LIMIT.TIER_1);
const rateLimitTier2 = Number(process.env.TIER_2_RATE_LIMIT || relayConstants.DEFAULT_RATE_LIMIT.TIER_2);
const limitDuration = Number(process.env.LIMIT_DURATION) || relayConstants.DEFAULT_RATE_LIMIT.DURATION;

const batchRequests = [
{
id: 1,
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
},
{
id: 1,
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
},
];

after(async () => {
// expect all the connections to the WS server to be closed after all
expect(global.socketServer._connections).to.eq(0);
});

it(`Should submit batch requests to WS server and receive IPRateLimitExceeded error until rate limit is reached`, async () => {
const BATCH_REQUEST_METHOD_NAME = 'batch_request';

// call batch request multitime to reach limit
for (let i = 0; i < rateLimitTier1; i++) {
await WsTestHelper.sendRequestToStandardWebSocket(BATCH_REQUEST_METHOD_NAME, batchRequests);
}

// exceed rate limit
const batchResponses = await WsTestHelper.sendRequestToStandardWebSocket(BATCH_REQUEST_METHOD_NAME, batchRequests);
const ipRateLimitError = new IPRateLimitExceeded(BATCH_REQUEST_METHOD_NAME);

expect(batchResponses[0].error.code).to.deep.eq(ipRateLimitError.code);
expect(batchResponses[0].error.message).to.deep.eq(ipRateLimitError.message);
});

it(`Should submit single requests to WS server and receive IPRateLimitExceeded error until rate limit is reached`, async () => {
const SINGLE_REQUEST_METHOD_NAME = 'eth_gasPrice';
for (let i = 0; i < rateLimitTier2; i++) {
await WsTestHelper.sendRequestToStandardWebSocket(SINGLE_REQUEST_METHOD_NAME, []);
}

// exceed rate limit
const response = await WsTestHelper.sendRequestToStandardWebSocket(SINGLE_REQUEST_METHOD_NAME, []);
const ipRateLimitError = new IPRateLimitExceeded(SINGLE_REQUEST_METHOD_NAME);
expect(response.error.code).to.deep.eq(ipRateLimitError.code);
expect(response.error.message).to.deep.eq(ipRateLimitError.message);
});

it(`Should reset limit for requests`, async () => {
const SINGLE_REQUEST_METHOD_NAME = 'eth_getBalance';
const account: AliasAccount = global.accounts[0];

for (let i = 0; i < rateLimitTier2; i++) {
await WsTestHelper.sendRequestToStandardWebSocket(SINGLE_REQUEST_METHOD_NAME, [account.address, 'latest']);
}
// exceed rate limit
const rateLimitResponse = await WsTestHelper.sendRequestToStandardWebSocket(SINGLE_REQUEST_METHOD_NAME, [
account.address,
'latest',
]);
const ipRateLimitError = new IPRateLimitExceeded(SINGLE_REQUEST_METHOD_NAME);
expect(rateLimitResponse.error.code).to.deep.eq(ipRateLimitError.code);
expect(rateLimitResponse.error.message).to.deep.eq(ipRateLimitError.message);

// wait until rate limit is reset
await new Promise((r) => setTimeout(r, limitDuration));
const response = await WsTestHelper.sendRequestToStandardWebSocket(SINGLE_REQUEST_METHOD_NAME, [
account.address,
'latest',
]);
const expectedResult = await global.relay.call(SINGLE_REQUEST_METHOD_NAME, [account.address, 'latest']);
expect(response.result).to.eq(expectedResult);
});
});

0 comments on commit 92dff3d

Please sign in to comment.