Skip to content

Commit 3ac3300

Browse files
committed
refactors validator service
Signed-off-by: Konstantina Blazhukova <[email protected]>
1 parent 5b59734 commit 3ac3300

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

packages/relay/src/lib/debug.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ export class DebugImpl implements Debug {
105105
*/
106106
@rpcMethod
107107
@rpcParamValidationRules({
108-
0: { type: ['transactionHash'], required: true },
109-
1: { type: ['tracerType', 'tracerConfig', 'tracerConfigWrapper'], required: false },
110-
2: { type: 'tracerConfig', required: false },
108+
0: { type: 'transactionHash', required: true },
109+
1: { type: 'tracerConfigWrapper', required: true },
111110
})
112111
@rpcParamLayoutConfig(RPC_LAYOUT.custom((params) => [params[0], params[1]]))
113112
@cache(CacheService.getInstance(CACHE_LEVEL.L1))

packages/relay/src/lib/eth.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
ITransactionReceipt,
3535
RequestDetails,
3636
} from './types';
37-
import { ParamType } from './types/validation';
3837
import { rpcParamValidationRules } from './validators';
3938

4039
/**

packages/relay/src/lib/validators/objectTypes.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
import { TracerType } from '../constants';
34
import { predefined } from '../errors/JsonRpcError';
45
import { validateObject } from './utils';
56

@@ -220,7 +221,7 @@ export function validateSchema(schema: IObjectSchema, object: any) {
220221
return validateObject(object, schema);
221222
}
222223

223-
export function validateEthSubscribeLogsParamObject(param: any): asserts param is { address: string } {
224+
export function validateEthSubscribeLogsParamObject(param: any): boolean {
224225
const schema = OBJECTS_VALIDATIONS.ethSubscribeLogsParams;
225226
const valid = validateSchema(schema, param);
226227
// Check if the address is an array and has a length of 0
@@ -229,5 +230,40 @@ export function validateEthSubscribeLogsParamObject(param: any): asserts param i
229230
throw predefined.MISSING_REQUIRED_PARAMETER(`'address' for ${schema.name}`);
230231
}
231232

232-
// return valid;
233+
return valid;
234+
}
235+
236+
export function validateTracerConfigWrapper(param: any): boolean {
237+
const schema = OBJECTS_VALIDATIONS.tracerConfigWrapper;
238+
const valid = validateSchema(schema, param);
239+
const { tracer, tracerConfig } = param;
240+
241+
if (!tracerConfig) {
242+
return valid;
243+
}
244+
245+
const callTracerKeys = Object.keys(OBJECTS_VALIDATIONS.callTracerConfig.properties);
246+
const opcodeLoggerKeys = Object.keys(OBJECTS_VALIDATIONS.opcodeLoggerConfig.properties);
247+
248+
const configKeys = Object.keys(tracerConfig);
249+
const hasCallTracerKeys = configKeys.some((k) => callTracerKeys.includes(k));
250+
const hasOpcodeLoggerKeys = configKeys.some((k) => opcodeLoggerKeys.includes(k));
251+
252+
// we want to accept ICallTracerConfig only if the tracer is callTracer
253+
// this config is not valid for opcodeLogger and vice versa
254+
// accept only IOpcodeLoggerConfig with opcodeLogger tracer
255+
if (hasCallTracerKeys && tracer === TracerType.OpcodeLogger) {
256+
throw predefined.INVALID_PARAMETER(
257+
1,
258+
`callTracer 'tracerConfig' for ${schema.name} is only valid when tracer=${TracerType.CallTracer}`,
259+
);
260+
}
261+
262+
if (hasOpcodeLoggerKeys && tracer !== TracerType.OpcodeLogger) {
263+
throw predefined.INVALID_PARAMETER(
264+
1,
265+
`opcodeLogger 'tracerConfig' for ${schema.name} is only valid when tracer=${TracerType.OpcodeLogger}`,
266+
);
267+
}
268+
return valid;
233269
}

packages/ws-server/src/utils/validators.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import constants from '@hashgraph/json-rpc-relay/dist/lib/constants';
66
import { RequestDetails } from '@hashgraph/json-rpc-relay/dist/lib/types';
77
import { validateEthSubscribeLogsParamObject } from '@hashgraph/json-rpc-relay/dist/lib/validators';
88

9+
interface EthSubscribeLogsParams {
10+
address?: string | string[];
11+
topics?: any[];
12+
}
13+
914
/**
1015
* Validates whether the provided address corresponds to a contract or token type.
1116
* Throws an error if the address is not a valid contract or token type or does not exist.
@@ -43,7 +48,7 @@ const validateIsContractOrTokenAddress = async (
4348
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
4449
*/
4550
export const validateSubscribeEthLogsParams = async (
46-
filters: unknown,
51+
filters: EthSubscribeLogsParams,
4752
mirrorNodeClient: MirrorNodeClient,
4853
requestDetails: RequestDetails,
4954
) => {

0 commit comments

Comments
 (0)