Skip to content

Commit

Permalink
chore: cherry pick 2494 (#2509)
Browse files Browse the repository at this point in the history
fix: added support for undefined params requests in WS server (#2463) (#2494)

fix: added support for undefined param requests

Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node authored May 20, 2024
1 parent ab2a0c9 commit ffa9e46
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/ws-server/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ export const getRequestResult = async (
wsMetricRegistry: WsMetricRegistry,
): Promise<any> => {
// Extract the method and parameters from the received request
const { method, params } = request;
let { method, params } = request;

// support go-ethereum client by turning undefined into empty array
if (!params) params = [];

// Increment metrics for the received method
wsMetricRegistry.getCounter('methodsCounter').labels(method).inc();
Expand Down
38 changes: 27 additions & 11 deletions packages/ws-server/tests/acceptance/validations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import WebSocket from 'ws';
import { expect } from 'chai';
import { ethers, WebSocketProvider } from 'ethers';
import { WsTestConstant, WsTestHelper } from '../helper';
import { predefined } from '@hashgraph/json-rpc-relay/src';
import { InvalidRequest, MethodNotFound } from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcError';

describe('@web-socket-batch-1 JSON-RPC requests validation', async function () {
const METHOD_NAME = 'eth_blockNumber';
const BLOCK_NUMBER_METHOD_NAME = 'eth_blockNumber';
const INVALID_REQUESTS = [
{
id: '1',
method: METHOD_NAME,
method: BLOCK_NUMBER_METHOD_NAME,
params: [],
},
{
Expand All @@ -41,7 +42,7 @@ describe('@web-socket-batch-1 JSON-RPC requests validation', async function () {
{
id: '1',
jsonrpc: 'hedera',
method: METHOD_NAME,
method: BLOCK_NUMBER_METHOD_NAME,
params: [],
},
];
Expand All @@ -51,26 +52,21 @@ describe('@web-socket-batch-1 JSON-RPC requests validation', async function () {
let ethersWsProvider: WebSocketProvider;

beforeEach(async () => {
process.env.REQUEST_ID_IS_OPTIONAL = 'true';
ethersWsProvider = new ethers.WebSocketProvider(WsTestConstant.WS_RELAY_URL);
});

afterEach(async () => {
if (ethersWsProvider) await ethersWsProvider.destroy();
delete process.env.REQUEST_ID_IS_OPTIONAL;
});

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

describe(WsTestConstant.STANDARD_WEB_SOCKET, () => {
beforeEach(() => {
process.env.REQUEST_ID_IS_OPTIONAL = 'true';
});
afterEach(() => {
delete process.env.REQUEST_ID_IS_OPTIONAL;
});

describe('Request & Method Validations', () => {
for (const request of INVALID_REQUESTS) {
it('Should reject the requests because of the invalid JSON-RPC requests', async () => {
const webSocket = new WebSocket(WsTestConstant.WS_RELAY_URL);
Expand Down Expand Up @@ -110,4 +106,24 @@ describe('@web-socket-batch-1 JSON-RPC requests validation', async function () {
});
}
});

describe('Request with undefined params', () => {
it('Should execute eth_chainId requests with undefined params and receive expected result', async () => {
const response = await WsTestHelper.sendRequestToStandardWebSocket('eth_chainId', undefined);
const expectedResult = await global.relay.call('eth_chainId', []);
expect(response.result).to.eq(expectedResult);
});

it('Should execute eth_blockNumber requests with undefined params and receive expected result', async () => {
const response = await WsTestHelper.sendRequestToStandardWebSocket('eth_blockNumber', undefined);
const expectedResult = await global.relay.call('eth_blockNumber', []);
expect(response.result).to.eq(expectedResult);
});
it('Should execute eth_sendRawTransaction requests with undefined params and receive MISSING_REQUIRED_PARAMETER error', async () => {
const response = await WsTestHelper.sendRequestToStandardWebSocket('eth_sendRawTransaction', undefined);
const expectedResult = predefined.MISSING_REQUIRED_PARAMETER(0);
delete expectedResult.data;
expect(response.error).to.deep.eq(expectedResult);
});
});
});
2 changes: 1 addition & 1 deletion packages/ws-server/tests/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class WsTestHelper {
}
}

static async sendRequestToStandardWebSocket(method: string, params: any[], ms?: number | undefined) {
static async sendRequestToStandardWebSocket(method: string, params: any, ms?: number | undefined) {
const webSocket = new WebSocket(WsTestConstant.WS_RELAY_URL);

let response: any;
Expand Down

0 comments on commit ffa9e46

Please sign in to comment.