diff --git a/packages/client/bin/cli.ts b/packages/client/bin/cli.ts index 52946ab6cb..120abdd297 100755 --- a/packages/client/bin/cli.ts +++ b/packages/client/bin/cli.ts @@ -205,9 +205,16 @@ const args: ClientOpts = yargs(hideBin(process.argv)) default: 5, }) .option('rpcDebug', { + describe: + 'Additionally log truncated RPC calls filtered by name (prefix), e.g.: "eth,engine_getPayload" (use "all" for all methods). Truncated by default, add verbosity using "rpcDebugVerbose"', + default: '', + string: true, + }) + .option('rpcDebugVerbose', { describe: 'Additionally log complete RPC calls filtered by name (prefix), e.g.: "eth,engine_getPayload" (use "all" for all methods).', default: '', + string: true, }) .option('rpcCors', { describe: 'Configure the Access-Control-Allow-Origin CORS header for RPC server', diff --git a/packages/client/bin/startRpc.ts b/packages/client/bin/startRpc.ts index 9a7e2fb85a..4c549abcd9 100644 --- a/packages/client/bin/startRpc.ts +++ b/packages/client/bin/startRpc.ts @@ -27,6 +27,7 @@ export type RPCArgs = { wsEngineAddr: string wsEnginePort: number rpcDebug: string + rpcDebugVerbose: string helpRpc: boolean jwtSecret?: string rpcEngineAuth: boolean @@ -90,6 +91,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) { rpcEngineAuth, rpcCors, rpcDebug, + rpcDebugVerbose, } = args const manager = new RPCManager(client, config) const { logger } = config @@ -109,6 +111,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) { const { server, namespaces, methods } = createRPCServer(manager, { methodConfig: withEngineMethods ? MethodConfig.WithEngine : MethodConfig.WithoutEngine, + rpcDebugVerbose, rpcDebug, logger, }) @@ -171,6 +174,7 @@ export function startRPCServers(client: EthereumClient, args: RPCArgs) { const { server, namespaces, methods } = createRPCServer(manager, { methodConfig: MethodConfig.EngineOnly, rpcDebug, + rpcDebugVerbose, logger, }) servers.push(server) diff --git a/packages/client/src/types.ts b/packages/client/src/types.ts index 9778bae90d..48f576aa61 100644 --- a/packages/client/src/types.ts +++ b/packages/client/src/types.ts @@ -130,6 +130,7 @@ export interface ClientOpts { logRotate?: boolean logMaxFiles?: number rpcDebug?: string + rpcDebugVerbose?: string rpcCors?: string maxPerRequest?: number maxFetcherJobs?: number diff --git a/packages/client/src/util/rpc.ts b/packages/client/src/util/rpc.ts index ce39808a5a..4088c1a674 100644 --- a/packages/client/src/util/rpc.ts +++ b/packages/client/src/util/rpc.ts @@ -18,6 +18,7 @@ const algorithm: TAlgorithm = 'HS256' type CreateRPCServerOpts = { methodConfig: MethodConfig rpcDebug: string + rpcDebugVerbose: string logger?: Logger } type CreateRPCServerReturn = { @@ -42,6 +43,28 @@ export enum MethodConfig { /** Allowed drift for jwt token issuance is 60 seconds */ const ALLOWED_DRIFT = 60_000 +/** + * Check if the `method` matches the comma-separated filter string + * @param method - Method to check the filter on + * @param filterStringCSV - Comma-separated list of filters to use + * @returns + */ +function checkFilter(method: string, filterStringCSV: string) { + if (filterStringCSV === '') { + return false + } + if (filterStringCSV === 'all') { + return true + } + const filters = filterStringCSV.split(',') + for (const filter of filters) { + if (method.includes(filter) === true) { + return true + } + } + return false +} + /** * Internal util to pretty print params for logging. */ @@ -63,51 +86,23 @@ export function createRPCServer( manager: RPCManager, opts: CreateRPCServerOpts ): CreateRPCServerReturn { - const { methodConfig, rpcDebug, logger } = opts - + const { methodConfig, rpcDebug, rpcDebugVerbose, logger } = opts const onRequest = (request: any) => { - let msg = '' - if (rpcDebug && rpcDebug !== '') { - let show = false - if (rpcDebug === 'all') { - show = true - } else { - const filters = rpcDebug.split(',') - for (const filter of filters) { - if (request.method.includes(filter) === true) { - show = true - } - } - } - if (show) { - if (logger?.level === 'debug') { - msg += `${request.method} called with params:\n${inspectParams(request.params)}` - } else { - msg += `${request.method} called with params: ${inspectParams(request.params, 125)}` - } - logger?.info(msg) - } + if (checkFilter(request.method, rpcDebugVerbose)) { + logger?.info(`${request.method} called with params:\n${inspectParams(request.params)}`) + } else if (checkFilter(request.method, rpcDebug)) { + logger?.info(`${request.method} called with params: ${inspectParams(request.params, 125)}`) } } const handleResponse = (request: any, response: any, batchAddOn = '') => { - let msg = '' - if ( - rpcDebug === 'true' || - (rpcDebug === 'engine' && request.method.includes('engine_') === true) || - (rpcDebug === 'eth' && request.method.includes('eth_') === true) - ) { - msg = `${request.method}${batchAddOn} responded with:\n${inspectParams(response)}` - } else { - msg = `${request.method}${batchAddOn} responded with: ` - if (response.result !== undefined) { - msg += inspectParams(response, 125) - } - if (response.error !== undefined) { - msg += `error: ${response.error.message}` - } + if (checkFilter(request.method, rpcDebugVerbose)) { + logger?.info(`${request.method}${batchAddOn} responded with:\n${inspectParams(response)}`) + } else if (checkFilter(request.method, rpcDebug)) { + logger?.info( + `${request.method}${batchAddOn} responded with:\n${inspectParams(response, 125)}` + ) } - logger?.debug(msg) } const onBatchResponse = (request: any, response: any) => { @@ -126,11 +121,14 @@ export function createRPCServer( } let methods - const ethMethods = manager.getMethods(false, rpcDebug !== 'false') + const ethMethods = manager.getMethods(false, rpcDebug !== 'false' && rpcDebug !== '') switch (methodConfig) { case MethodConfig.WithEngine: - methods = { ...ethMethods, ...manager.getMethods(true, rpcDebug !== 'false') } + methods = { + ...ethMethods, + ...manager.getMethods(true, rpcDebug !== 'false' && rpcDebug !== ''), + } break case MethodConfig.WithoutEngine: methods = { ...ethMethods } diff --git a/packages/client/test/util/rpc.spec.ts b/packages/client/test/util/rpc.spec.ts index d177b2d803..7e716ef7c0 100644 --- a/packages/client/test/util/rpc.spec.ts +++ b/packages/client/test/util/rpc.spec.ts @@ -23,7 +23,12 @@ describe('[Util/RPC]', () => { const { logger } = config for (const methodConfig of Object.values(MethodConfig)) { for (const rpcDebug of ['', 'eth']) { - const { server } = createRPCServer(manager, { methodConfig, rpcDebug, logger }) + const { server } = createRPCServer(manager, { + methodConfig, + rpcDebug, + logger, + rpcDebugVerbose: '', + }) const httpServer = createRPCServerListener({ server, withEngineMiddleware: { jwtSecret: new Uint8Array(32) }, @@ -66,7 +71,8 @@ describe('[Util/RPC/Engine eth methods]', async () => { const manager = new RPCManager(client, config) const { server } = createRPCServer(manager, { methodConfig: MethodConfig.EngineOnly, - rpcDebug: false, + rpcDebug: '', + rpcDebugVerbose: '', }) const httpServer = createRPCServerListener({ server }) const methods = [