Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client: fix rpc debug #3125

Merged
merged 6 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,15 @@ 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).',
coerce: (arg: string) => (arg === '' ? 'all' : arg),
default: '',
string: true,
})
.option('rpcCors', {
Expand Down
4 changes: 4 additions & 0 deletions packages/client/bin/startRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type RPCArgs = {
wsEngineAddr: string
wsEnginePort: number
rpcDebug: string
rpcDebugVerbose: string
helpRpc: boolean
jwtSecret?: string
rpcEngineAuth: boolean
Expand Down Expand Up @@ -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
Expand All @@ -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,
})
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export interface ClientOpts {
logRotate?: boolean
logMaxFiles?: number
rpcDebug?: string
rpcDebugVerbose?: string
rpcCors?: string
maxPerRequest?: number
maxFetcherJobs?: number
Expand Down
77 changes: 36 additions & 41 deletions packages/client/src/util/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const algorithm: TAlgorithm = 'HS256'
type CreateRPCServerOpts = {
methodConfig: MethodConfig
rpcDebug: string
rpcDebugVerbose: string
logger?: Logger
}
type CreateRPCServerReturn = {
Expand All @@ -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.
*/
Expand All @@ -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 === '' || 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)) {
Copy link
Contributor

@scorbajio scorbajio Nov 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To note, the options are:

  • both rpcDebugVerbose and rpcDebug are true, in which case the full version will be logged
  • both rpcDebugVerbose and rpcDebug are false, in which case nothing will be logged on requests and responses
  • rpcDebugVerbose is true and rpcDebug is false, in which case the full version will be logged
  • rpcDebugVerbose is false and rpcDebug is true, in which case the truncated version will be logged

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so for extra for clarification: the true and false are the returned values from this method, and are thus applied on the filter. If there is no filter (the filter is the empty string by default, so "no filter") then it always returns false.

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 === '' ||
rpcDebug === 'all' ||
(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) => {
Expand All @@ -126,13 +121,13 @@ export function createRPCServer(
}

let methods
const ethMethods = manager.getMethods(false, rpcDebug !== 'false' && rpcDebug !== undefined)
const ethMethods = manager.getMethods(false, rpcDebug !== 'false' && rpcDebug !== '')

switch (methodConfig) {
case MethodConfig.WithEngine:
methods = {
...ethMethods,
...manager.getMethods(true, rpcDebug !== 'false' && rpcDebug !== undefined),
...manager.getMethods(true, rpcDebug !== 'false' && rpcDebug !== ''),
}
break
case MethodConfig.WithoutEngine:
Expand Down
Loading