Skip to content

rpcserver: return InvalidArgument for RPC validation errors#2112

Open
kaldun-tech wants to merge 2 commits into
lightninglabs:mainfrom
kaldun-tech:rpc-handlers-invalid-argument-validation
Open

rpcserver: return InvalidArgument for RPC validation errors#2112
kaldun-tech wants to merge 2 commits into
lightninglabs:mainfrom
kaldun-tech:rpc-handlers-invalid-argument-validation

Conversation

@kaldun-tech
Copy link
Copy Markdown
Contributor

@kaldun-tech kaldun-tech commented Apr 30, 2026

Summary

  • Update DecodeAddr, DecodeProof, and ExportProof to return codes.InvalidArgument instead of codes.Unknown for request validation errors
  • Add unit tests to verify gRPC status codes are correctly returned
  • Add release notes documenting the behavior change

This enables clients to programmatically distinguish bad input from internal errors.

Partial fix for #2086

Design Note

Issue #2086 proposed a pattern using a sentinel-error-to-status-code mapping helper at the RPC boundary. This would keep handler internals free of grpc imports.

This PR takes the simpler direct approach of using status.Errorf inline for the three handlers identified in the issue.

The helper pattern is worth considering for a broader refactor if we standardize error handling across all RPC handlers. For now, the direct approach is consistent with existing patterns in the codebase (e.g., ListAssets, SendAsset already use similar inline error wrapping).

Test plan

  • Verify existing tests pass
  • Add new unit tests in rpcserver/validation_test.go that assert codes.InvalidArgument is returned for validation failures
  • Run make lint
  • Run make unit pkg=rpcserver

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the robustness of the RPC server by standardizing error reporting for input validation. By switching from generic 'Unknown' errors to specific 'InvalidArgument' gRPC status codes, the API now provides clearer feedback to clients, enabling them to programmatically handle validation failures appropriately. The changes are accompanied by comprehensive unit tests to ensure consistent behavior.

Highlights

  • Improved Error Handling: Updated DecodeAddr, DecodeProof, and ExportProof to return codes.InvalidArgument instead of codes.Unknown for request validation errors, allowing clients to better distinguish between bad input and internal server issues.
  • Enhanced Test Coverage: Added a new unit test file rpcserver/validation_test.go to verify that the correct gRPC status codes are returned for various invalid input scenarios.
  • Documentation: Updated the release notes for version 0.8.0 to document this behavior change for API consumers.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the DecodeAddr, DecodeProof, and ExportProof RPC methods to return codes.InvalidArgument for validation errors, allowing clients to programmatically distinguish bad input from internal errors. The changes include updated release notes and a new unit test file, rpcserver/validation_test.go, to ensure correct error code returns. The review feedback points out several opportunities to condense error return statements to fewer lines, adhering to the repository's style guide for log and error message formatting.

Comment thread rpcserver/rpcserver.go Outdated
Comment thread rpcserver/rpcserver.go Outdated
Comment thread rpcserver/rpcserver.go Outdated
Comment thread rpcserver/rpcserver.go Outdated
Comment thread rpcserver/rpcserver.go Outdated
@kaldun-tech kaldun-tech marked this pull request as draft May 1, 2026 00:36
@kaldun-tech kaldun-tech force-pushed the rpc-handlers-invalid-argument-validation branch 3 times, most recently from 43719ba to d42b490 Compare May 1, 2026 22:42
@kaldun-tech
Copy link
Copy Markdown
Contributor Author

I'm removing the TestDecodeAddrValidInput and TestExportProofValidInput happy path tests which are causing failures in CI

These tests attempted to verify that valid-looking input passes validation and fails later (not with InvalidArgument). IIRC this coverage was recommended by Gemini in my previous PR draft which I closed because it was from the wrong branchpoint.

The trouble I have is the tests require initializing the RPCServer with mocked TapAddrBook and ProofArchive dependencies, which is significant infrastructure for minimal value. The integration tests in itest/ already exercise these RPCs with valid input end-to-end. If the validation logic incorrectly rejected valid input, those tests would fail. The remaining validation tests comprehensively cover the InvalidArgument error paths, which is the core goal of this PR.

Update DecodeAddr, DecodeProof, and ExportProof to return
codes.InvalidArgument instead of codes.Unknown for request
validation errors. This enables clients to programmatically
distinguish bad input from internal errors.

Add validation_test.go with tests for invalid input
asserts InvalidArgument. Skipped mocking for valid input
tests which are already covered by integration tests.

Partial fix for Issue lightninglabs#2086

Signed-off-by: kaldun-tech <tsmereka@protonmail.com>
@kaldun-tech kaldun-tech force-pushed the rpc-handlers-invalid-argument-validation branch from d42b490 to fe6c7ce Compare May 3, 2026 17:45
@kaldun-tech kaldun-tech marked this pull request as ready for review May 3, 2026 18:33
@darioAnongba darioAnongba self-requested a review May 4, 2026 10:28
Copy link
Copy Markdown
Contributor

@darioAnongba darioAnongba left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Only a small nit request on the validation tests that can be too verbose and specific.

Comment thread rpcserver/validation_test.go
@github-project-automation github-project-automation Bot moved this from 🆕 New to 👀 In review in Taproot-Assets Project Board May 7, 2026
Replace wantMsg with wantCode and use assertCode helper to check
only the gRPC status code, not the error message. This makes tests
less brittle and more focused on the programmatic error code.

Signed-off-by: kaldun-tech <tsmereka@protonmail.com>
@kaldun-tech
Copy link
Copy Markdown
Contributor Author

Great catch @darioAnongba

I noticed two DecodeProof error paths are not covered by my unit tests:

  1. CheckMaxFileSize - This requires a 128+ MiB blob, which impractical for unit tests
  2. ProofAtDepth > latestProofIndex - Requires a valid decodable proof file so is better suited for integration tests

Both paths correctly return codes.InvalidArgument as shown in the implementation.

Copy link
Copy Markdown
Contributor

@darioAnongba darioAnongba left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 👀 In review

Development

Successfully merging this pull request may close these issues.

2 participants