11// SPDX-License-Identifier: Apache-2.0
22
3+ import { TracerType } from '../constants' ;
34import { predefined } from '../errors/JsonRpcError' ;
45import { 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}
0 commit comments