|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +import { MirrorNodeClient } from '@hashgraph/json-rpc-relay/dist/lib/clients/mirrorNodeClient'; |
| 3 | +import { predefined } from '@hashgraph/json-rpc-relay/dist/lib/errors/JsonRpcError'; |
| 4 | +import { Relay } from '@hashgraph/json-rpc-relay/dist/lib/relay'; |
| 5 | +import { RequestDetails } from '@hashgraph/json-rpc-relay/dist/lib/types/RequestDetails'; |
| 6 | +import { IJsonRpcRequest } from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/IJsonRpcRequest'; |
| 7 | +import { expect } from 'chai'; |
| 8 | +import Koa from 'koa'; |
| 9 | +import { Counter } from 'prom-client'; |
| 10 | +import sinon from 'sinon'; |
| 11 | + |
| 12 | +import { getRequestResult } from '../../../dist/controllers/jsonRpcController'; |
| 13 | +import WsMetricRegistry from '../../../dist/metrics/wsMetricRegistry'; |
| 14 | +import { SubscriptionService } from '../../../dist/service/subscriptionService'; |
| 15 | +import ConnectionLimiter from '../../../src/metrics/connectionLimiter'; |
| 16 | +import { WS_CONSTANTS } from '../../../src/utils/constants'; |
| 17 | + |
| 18 | +function createMockContext(): Koa.Context { |
| 19 | + return { |
| 20 | + websocket: { |
| 21 | + id: 'test-connection-id', |
| 22 | + send: sinon.stub(), |
| 23 | + close: sinon.stub(), |
| 24 | + inactivityTTL: undefined, |
| 25 | + ipCounted: false, |
| 26 | + subscriptions: 0, |
| 27 | + }, |
| 28 | + request: { ip: '127.0.0.1' }, |
| 29 | + app: { server: { _connections: 0 } }, |
| 30 | + } as Koa.Context; |
| 31 | +} |
| 32 | + |
| 33 | +describe('JSON Rpc Controller', function () { |
| 34 | + let mockLogger: any; |
| 35 | + let stubWsMetricRegistry: WsMetricRegistry; |
| 36 | + let stubRelay: Relay; |
| 37 | + let stubConnectionLimiter: ConnectionLimiter; |
| 38 | + let stubMirrorNodeClient: MirrorNodeClient; |
| 39 | + let stubSubscriptionService: SubscriptionService; |
| 40 | + let requestDetails: RequestDetails; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + mockLogger = { |
| 44 | + warn: sinon.stub(), |
| 45 | + trace: sinon.stub(), |
| 46 | + isLevelEnabled: sinon.stub().returns(true), |
| 47 | + }; |
| 48 | + stubWsMetricRegistry = sinon.createStubInstance(WsMetricRegistry); |
| 49 | + stubWsMetricRegistry.getCounter.returns({ |
| 50 | + labels: () => { |
| 51 | + return { inc: sinon.stub() }; |
| 52 | + }, |
| 53 | + } as unknown as Counter); |
| 54 | + stubRelay = sinon.createStubInstance(Relay); |
| 55 | + stubConnectionLimiter = sinon.createStubInstance(ConnectionLimiter); |
| 56 | + stubMirrorNodeClient = sinon.createStubInstance(MirrorNodeClient); |
| 57 | + stubSubscriptionService = sinon.createStubInstance(SubscriptionService); |
| 58 | + requestDetails = new RequestDetails({ |
| 59 | + requestId: '3', |
| 60 | + ipAddress: '0.0.0.0', |
| 61 | + connectionId: '9', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + afterEach(() => { |
| 66 | + sinon.restore(); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('getRequestResult', async function () { |
| 70 | + let defaultRequestParams: any; |
| 71 | + |
| 72 | + beforeEach(() => { |
| 73 | + defaultRequestParams = [ |
| 74 | + createMockContext(), |
| 75 | + stubRelay, |
| 76 | + mockLogger, |
| 77 | + { id: '2', method: WS_CONSTANTS.METHODS.ETH_CHAINID, jsonrpc: '2.0' } as IJsonRpcRequest, |
| 78 | + stubConnectionLimiter, |
| 79 | + stubMirrorNodeClient, |
| 80 | + stubWsMetricRegistry, |
| 81 | + requestDetails, |
| 82 | + stubSubscriptionService, |
| 83 | + ]; |
| 84 | + }); |
| 85 | + |
| 86 | + it('should throw invalid request if id is missing from request body', async function () { |
| 87 | + defaultRequestParams[3] = { method: WS_CONSTANTS.METHODS.ETH_CHAINID, jsonrpc: '2.0' } as IJsonRpcRequest; |
| 88 | + const resp = await getRequestResult(...defaultRequestParams); |
| 89 | + |
| 90 | + expect(resp.error.code).to.equal(-32600); |
| 91 | + expect(resp.error.message).to.include('Invalid Request'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should throw method not found if passed method is not existing', async function () { |
| 95 | + const nonExistingMethod = 'eth_non-existing-method'; |
| 96 | + defaultRequestParams[3] = { id: '2', method: nonExistingMethod, jsonrpc: '2.0' } as IJsonRpcRequest; |
| 97 | + const resp = await getRequestResult(...defaultRequestParams); |
| 98 | + |
| 99 | + expect(resp.error.code).to.equal(-32601); |
| 100 | + expect(resp.error.message).to.include(`Method ${nonExistingMethod} not found`); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should throw IP Rate Limit exceeded error if .shouldRateLimitOnMethod returns true', async function () { |
| 104 | + stubConnectionLimiter.shouldRateLimitOnMethod.returns(true); |
| 105 | + const resp = await getRequestResult(...defaultRequestParams); |
| 106 | + |
| 107 | + expect(resp.error.code).to.equal(-32605); |
| 108 | + expect(resp.error.message).to.include('IP Rate limit exceeded'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should throw Max Subscription error if subscription limit is reached', async function () { |
| 112 | + stubConnectionLimiter.validateSubscriptionLimit.returns(false); |
| 113 | + defaultRequestParams[3] = { |
| 114 | + id: '2', |
| 115 | + method: WS_CONSTANTS.METHODS.ETH_SUBSCRIBE, |
| 116 | + jsonrpc: '2.0', |
| 117 | + } as IJsonRpcRequest; |
| 118 | + const resp = await getRequestResult(...defaultRequestParams); |
| 119 | + |
| 120 | + expect(resp.error.code).to.equal(-32608); |
| 121 | + expect(resp.error.message).to.include('Exceeded maximum allowed subscriptions'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should throw error on eth_subscribe if WS Subscriptions are disabled', async function () { |
| 125 | + stubConnectionLimiter.validateSubscriptionLimit.returns(true); |
| 126 | + defaultRequestParams[3] = { |
| 127 | + id: '2', |
| 128 | + method: WS_CONSTANTS.METHODS.ETH_SUBSCRIBE, |
| 129 | + jsonrpc: '2.0', |
| 130 | + } as IJsonRpcRequest; |
| 131 | + const resp = await getRequestResult(...defaultRequestParams); |
| 132 | + |
| 133 | + expect(resp.error.code).to.equal(-32207); |
| 134 | + expect(resp.error.message).to.include('WS Subscriptions are disabled'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should throw error on eth_unsubscribe if WS Subscriptions are disabled', async function () { |
| 138 | + stubConnectionLimiter.validateSubscriptionLimit.returns(true); |
| 139 | + defaultRequestParams[3] = { |
| 140 | + id: '2', |
| 141 | + method: WS_CONSTANTS.METHODS.ETH_UNSUBSCRIBE, |
| 142 | + jsonrpc: '2.0', |
| 143 | + } as IJsonRpcRequest; |
| 144 | + const resp = await getRequestResult(...defaultRequestParams); |
| 145 | + |
| 146 | + expect(resp.error.code).to.equal(-32207); |
| 147 | + expect(resp.error.message).to.include('WS Subscriptions are disabled'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should be able to execute `eth_chainId` and get a proper response', async function () { |
| 151 | + const chainId = '0x12a'; |
| 152 | + stubRelay.executeRpcMethod.returns(chainId); |
| 153 | + const resp = await getRequestResult(...defaultRequestParams); |
| 154 | + |
| 155 | + expect(resp.result).to.equal(chainId); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should be able to handle the error as JsonRpcError if an internal error is thrown within the relay execution', async function () { |
| 159 | + stubRelay.executeRpcMethod.throws(predefined.INTERNAL_ERROR); |
| 160 | + const resp = await getRequestResult(...defaultRequestParams); |
| 161 | + |
| 162 | + expect(resp.error.code).to.equal(-32603); |
| 163 | + expect(resp.error.message).to.include('Unknown error invoking RPC'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should transform every error to JsonRpcError`', async function () { |
| 167 | + delete mockLogger.isLevelEnabled; |
| 168 | + |
| 169 | + stubRelay.executeRpcMethod.throws(new Error('custom error')); |
| 170 | + const resp = await getRequestResult(...defaultRequestParams); |
| 171 | + |
| 172 | + expect(resp.error.code).to.equal(-32603); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments