-
Notifications
You must be signed in to change notification settings - Fork 17
refactor(rpc): more granular rpc error codes #1206
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
base: v1.6-dev
Are you sure you want to change the base?
Conversation
WalkthroughThe changes introduce standardized application-specific JSON-RPC error codes (-32000 to -32005) and establish a unified error resolution mechanism. New sentinel errors are added to the coretypes package, error mapping in the JSON-RPC layer is refactored from hardcoded values to dynamic resolution, and context cancellation in mempool operations wraps with a standardized error. Test coverage validates the new error-to-code mapping. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
rpc/jsonrpc/types/types.go (1)
155-193: Move broadcast confirmation check before deadline check in rpcErrorCodeFor to prevent shadowing.The issue is verified and accurate. When
fmt.Errorf("%w: %w", coretypes.ErrBroadcastConfirmationNotReceived, ctx.Err())is called ininternal/rpc/core/mempool.go(lines 86, 119) with a deadline context, the resulting error wraps both sentinels. The current switch statement inrpcErrorCodeForcheckserrors.Is(err, context.DeadlineExceeded)beforeisBroadcastConfirmationNotReceivedErr(err), causing the first case to match and returnCodeTimeoutinstead of the intendedCodeBroadcastConfirmationNotReceived.The fix is essential: reorder the cases to check for broadcast confirmation before the generic deadline timeout. This preserves the specific error code for broadcast failures while still handling pure
context.DeadlineExceedederrors correctly. The existing test (line 94 oftypes_test.go) only covers single-wrapped broadcast errors and does not test the actual dual-wrapped scenario from mempool.go, leaving this shadowing bug uncovered.
🧹 Nitpick comments (4)
rpc/coretypes/responses.go (1)
26-28: New RPC sentinel errors are well-aligned with usage
ErrBroadcastConfirmationNotReceivedandErrTooManyRequestsgive clear, shared sentinels for RPC-layer mapping, and the messages match the matching logic inrpc/jsonrpc/types. Consider adding brief comments indicating which RPC paths produce these to aid discoverability, but behavior itself looks solid.internal/rpc/core/mempool.go (1)
84-87: ctx cancellation wrapping cleanly exposes broadcast sentinel + causeWrapping the
ctx.Done()paths withErrBroadcastConfirmationNotReceivedwhile preservingctx.Err()as a cause is a good pattern for downstream error introspection and JSON-RPC mapping. The later commit-wait timeout in the loop intentionally remains a separate error shape, which is fine as long as you’re happy with that behavioral distinction.Also applies to: 117-120
rpc/jsonrpc/types/types_test.go (1)
80-105: Good coverage of new error-code mapping; consider adding mempool-style broadcast caseThe table-driven
TestRPCRequestMakeErrorMapsCodesnicely asserts the mapping for the main error shapes (invalid request, mempool errors, timeout, rate limiting, broadcast confirmation). To fully cover the real mempool path, you may want an extra case that mimicsinternal/rpc/core/mempool.go, where the broadcast error wraps both the sentinel andcontext.DeadlineExceeded:testCases := []struct { name string err error want ErrorCode }{ {"invalid request", coretypes.ErrInvalidRequest, CodeInvalidRequest}, {"tx already in cache", types.ErrTxInCache, CodeTxAlreadyExists}, {"tx too large", types.ErrTxTooLarge{Max: 1, Actual: 2}, CodeTxTooLarge}, {"mempool full", types.ErrMempoolIsFull{}, CodeMempoolIsFull}, {"deadline exceeded", context.DeadlineExceeded, CodeTimeout}, {"too many requests sentinel", coretypes.ErrTooManyRequests, CodeTooManyRequests}, {"too many requests string", errors.New("too_many_requests: limiter triggered"), CodeTooManyRequests}, {"broadcast confirmation", fmt.Errorf("%w", coretypes.ErrBroadcastConfirmationNotReceived), CodeBroadcastConfirmationNotReceived}, + {"broadcast confirmation + deadline", + fmt.Errorf("%w: %w", coretypes.ErrBroadcastConfirmationNotReceived, context.DeadlineExceeded), + CodeBroadcastConfirmationNotReceived}, }That will ensure the mapping behaves as expected for the exact error shape produced by the broadcast endpoints.
rpc/jsonrpc/types/types.go (1)
36-57: Application-specific JSON-RPC error codes are well-structuredThe new application-specific
ErrorCodeconstants are correctly placed in the reserved -32000..-32099 server range, anderrorCodeStringentries are consistent and descriptive. This should make client-side handling of mempool/timeouts/ratelimiting significantly clearer.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
internal/rpc/core/mempool.go(2 hunks)rpc/coretypes/responses.go(1 hunks)rpc/jsonrpc/types/types.go(3 hunks)rpc/jsonrpc/types/types_test.go(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
rpc/jsonrpc/types/types_test.go (3)
rpc/jsonrpc/types/types.go (9)
RPCRequest(62-67)ErrorCode(19-19)CodeInvalidRequest(31-31)CodeTxAlreadyExists(37-37)CodeTxTooLarge(38-38)CodeMempoolIsFull(39-39)CodeTimeout(40-40)CodeTooManyRequests(41-41)CodeBroadcastConfirmationNotReceived(42-42)rpc/coretypes/responses.go (3)
ErrInvalidRequest(26-26)ErrTooManyRequests(28-28)ErrBroadcastConfirmationNotReceived(27-27)types/mempool.go (3)
ErrTxInCache(11-11)ErrTxTooLarge(22-25)ErrMempoolIsFull(33-38)
internal/rpc/core/mempool.go (1)
rpc/coretypes/responses.go (1)
ErrBroadcastConfirmationNotReceived(27-27)
rpc/jsonrpc/types/types.go (2)
rpc/coretypes/responses.go (6)
ErrZeroOrNegativeHeight(21-21)ErrZeroOrNegativePerPage(19-19)ErrPageOutOfRange(20-20)ErrInvalidRequest(26-26)ErrTooManyRequests(28-28)ErrBroadcastConfirmationNotReceived(27-27)types/mempool.go (3)
ErrTxInCache(11-11)ErrTxTooLarge(22-25)ErrMempoolIsFull(33-38)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
- GitHub Check: Build (amd64, linux)
- GitHub Check: tests (04)
- GitHub Check: tests (03)
- GitHub Check: tests (01)
- GitHub Check: tests (02)
- GitHub Check: golangci-lint
- GitHub Check: e2e-test (dashcore)
- GitHub Check: e2e-test (rotate)
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Issue being fixed or feature implemented
Map mempool / broadcast errors to specific JSON-RPC application error codes and add a sentinel for broadcast confirmation timeouts.
What was done?
How Has This Been Tested?
Breaking Changes
Checklist:
For repository code-owners and collaborators only
Summary by CodeRabbit