-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved metrics and added rate limit for the batch request fea…
…ture in ws server (#2474) (#2487) * feat: added shouldRateLimitOnMethod() to connectionLimiter Signed-off-by: Logan Nguyen <[email protected]> * feat: applied shouldRateLimitOnMethod() on batch requests Signed-off-by: Logan Nguyen <[email protected]> * feat: applied shouldRateLimitOnMethod() on single requests Signed-off-by: Logan Nguyen <[email protected]> * feat: added methodsCounter metric for batch_requests Signed-off-by: Logan Nguyen <[email protected]> * fix: skipped rateLimit for eth_subscribe and eth_unsubscribe Signed-off-by: Logan Nguyen <[email protected]> * test: added acceptancetest for rate limiter in the WS Signed-off-by: Logan Nguyen <[email protected]> * test: enabled test_ws_server in ratelimiter CI Signed-off-by: Logan Nguyen <[email protected]> --------- Signed-off-by: Logan Nguyen <[email protected]>
- Loading branch information
1 parent
1ca66cf
commit 94bf628
Showing
9 changed files
with
169 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/ws-server/tests/acceptance/rateLimiter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/*- | ||
* | ||
* 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 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); | ||
|
||
// wait until rate limit is reset | ||
await new Promise((r) => setTimeout(r, limitDuration)); | ||
}); | ||
|
||
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); | ||
|
||
// wait until rate limit is reset | ||
await new Promise((r) => setTimeout(r, limitDuration)); | ||
}); | ||
|
||
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters