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

auth: Improve cli tx decoding with fallback to direct txBytes protobuf unmarshaling #23611

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

assafmo
Copy link
Contributor

@assafmo assafmo commented Feb 4, 2025

IIRC before it supported only signBytes.

This mean now cli tx decode accepts the output generated by the mempool in #20802.

Summary by CodeRabbit

  • New Features
    • Enhanced transaction decoding reliability with improved error handling, ensuring more robust conversion of transactions to JSON even when unexpected issues arise.

…f unmarshaling

IIRC before it supported just signBytes
@assafmo assafmo requested a review from a team as a code owner February 4, 2025 11:53
Copy link
Contributor

coderabbitai bot commented Feb 4, 2025

📝 Walkthrough

Walkthrough

The changes add a new function decodeTxAndGetJSON in the x/auth/client/cli/decode.go file. This function handles transaction decoding by first attempting to decode using the configured TxDecoder from clientCtx.TxConfig. If decoding is successful, it converts the transaction into JSON via TxJSONEncoder. In the event of a failure, it falls back to unmarshaling the transaction bytes into a sdktx.Tx structure and then marshals that to JSON. The control flow remains similar while enhancing error handling.

Changes

File Change Summary
x/auth/client/cli/decode.go Added new function decodeTxAndGetJSON which encapsulates transaction decoding logic. The function attempts decoding via TxDecoder and, on failure, uses a fallback mechanism to unmarshal and then encode the transaction to JSON.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Decoder as decodeTxAndGetJSON
    participant TxDecoder as clientCtx.TxConfig
    participant JSONEncoder

    Caller->>Decoder: Call decodeTxAndGetJSON(txBytes)
    Decoder->>TxDecoder: Decode transaction (txBytes)
    alt Decoding Successful
        TxDecoder-->>Decoder: Decoded transaction
        Decoder->>JSONEncoder: Encode transaction to JSON
        JSONEncoder-->>Decoder: JSON result
    else Decoding Failed
        Decoder->>Decoder: Unmarshal txBytes into sdktx.Tx
        Decoder->>JSONEncoder: Marshal sdktx.Tx to JSON
    end
    Decoder-->>Caller: Return JSON or error
Loading

Suggested reviewers

  • aaronc
  • alpe
  • julienrbrt

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

🧹 Nitpick comments (1)
x/auth/client/cli/decode.go (1)

61-63: Consider wrapping errors with more context.

To improve debugging, consider wrapping the error with additional context about the fallback attempt.

-		return nil, err
+		return nil, fmt.Errorf("failed to unmarshal tx bytes in fallback path: %w", err)
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e41dfb6 and 5795e74.

📒 Files selected for processing (1)
  • x/auth/client/cli/decode.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
x/auth/client/cli/decode.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: tests (03)
  • GitHub Check: tests (00)
  • GitHub Check: test-simapp-v2
  • GitHub Check: test-system-v2
  • GitHub Check: test-sim-nondeterminism
  • GitHub Check: test-integration
  • GitHub Check: Analyze
  • GitHub Check: build (amd64)
  • GitHub Check: Summary
🔇 Additional comments (3)
x/auth/client/cli/decode.go (3)

11-11: LGTM! Import follows style guidelines.

The new import is properly grouped with other cosmos-sdk imports and uses clear aliasing.


36-36: LGTM! Good separation of concerns.

Extracting the decoding logic into a separate function improves code organization and maintainability.


52-57: LGTM! Clear implementation of the fallback mechanism.

The function follows a clear pattern:

  1. Attempts standard decoding first
  2. Only falls back if needed
  3. Includes helpful comments explaining the flow

x/auth/client/cli/decode.go Outdated Show resolved Hide resolved
Co-authored-by: Alex | Interchain Labs <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5795e74 and e956b50.

📒 Files selected for processing (1)
  • x/auth/client/cli/decode.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
x/auth/client/cli/decode.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🪛 GitHub Check: tests (03)
x/auth/client/cli/decode.go

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: tests (02)
x/auth/client/cli/decode.go

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: dependency-review
x/auth/client/cli/decode.go

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: golangci-lint
x/auth/client/cli/decode.go

[failure] 62-62:
undefined: fmt (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)

🪛 GitHub Actions: Dependency Review
x/auth/client/cli/decode.go

[error] 62-62: undefined: fmt

🪛 GitHub Actions: Lint
x/auth/client/cli/decode.go

[error] 62-62: undefined: fmt (typecheck)

🪛 GitHub Actions: Build SimApp
x/auth/client/cli/decode.go

[error] 62-62: undefined: fmt

🪛 GitHub Actions: v2 core Tests
x/auth/client/cli/decode.go

[error] 62-62: undefined: fmt

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: tests (00)
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (4)
x/auth/client/cli/decode.go (4)

11-11: LGTM!

The import of the sdktx package with a clear alias is appropriate for the new fallback mechanism.


36-36: LGTM!

Good refactoring to extract the decoding logic into a separate function, improving code organization and maintainability.


62-62: Improve error message formatting.

The error message formatting can be improved as suggested in a previous review.

Apply this change:

-		return nil, fmt.Errorf("failed to directly unmarshal txBytes: %w", err)
+		return nil, fmt.Errorf("failed to directly unmarshal txBytes: %w", err)
🧰 Tools
🪛 GitHub Check: tests (03)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: tests (02)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: dependency-review

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: golangci-lint

[failure] 62-62:
undefined: fmt (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)

🪛 GitHub Actions: Dependency Review

[error] 62-62: undefined: fmt

🪛 GitHub Actions: Lint

[error] 62-62: undefined: fmt (typecheck)

🪛 GitHub Actions: Build SimApp

[error] 62-62: undefined: fmt

🪛 GitHub Actions: v2 core Tests

[error] 62-62: undefined: fmt


52-66: LGTM! Well-structured fallback mechanism.

The function implements a robust two-step decoding process:

  1. First attempts decoding with TxDecoder
  2. Falls back to direct protobuf unmarshaling if needed

The progression is clear and well-commented.

🧰 Tools
🪛 GitHub Check: tests (03)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: tests (02)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: dependency-review

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: golangci-lint

[failure] 62-62:
undefined: fmt (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)

🪛 GitHub Actions: Dependency Review

[error] 62-62: undefined: fmt

🪛 GitHub Actions: Lint

[error] 62-62: undefined: fmt (typecheck)

🪛 GitHub Actions: Build SimApp

[error] 62-62: undefined: fmt

🪛 GitHub Actions: v2 core Tests

[error] 62-62: undefined: fmt

Comment on lines +52 to +66
func decodeTxAndGetJSON(clientCtx client.Context, txBytes []byte) ([]byte, error) {
// First try decoding with TxDecoder
tx, err := clientCtx.TxConfig.TxDecoder()(txBytes)
if err == nil {
return clientCtx.TxConfig.TxJSONEncoder()(tx)
}

// Fallback to direct unmarshaling
var sdkTx sdktx.Tx
if err := clientCtx.Codec.Unmarshal(txBytes, &sdkTx); err != nil {
return nil, fmt.Errorf("failed to directly unmarshal txBytes: %w", err)
}

return clientCtx.Codec.MarshalJSON(&sdkTx)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add missing fmt package import.

The fmt package is required for error formatting but is not imported, causing build failures.

Add this import to fix the build:

 import (
 	"encoding/base64"
 	"encoding/hex"
+	"fmt"
 
 	"github.com/spf13/cobra"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func decodeTxAndGetJSON(clientCtx client.Context, txBytes []byte) ([]byte, error) {
// First try decoding with TxDecoder
tx, err := clientCtx.TxConfig.TxDecoder()(txBytes)
if err == nil {
return clientCtx.TxConfig.TxJSONEncoder()(tx)
}
// Fallback to direct unmarshaling
var sdkTx sdktx.Tx
if err := clientCtx.Codec.Unmarshal(txBytes, &sdkTx); err != nil {
return nil, fmt.Errorf("failed to directly unmarshal txBytes: %w", err)
}
return clientCtx.Codec.MarshalJSON(&sdkTx)
}
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
// ... other imports if any
)
🧰 Tools
🪛 GitHub Check: tests (03)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: tests (02)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: dependency-review

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: golangci-lint

[failure] 62-62:
undefined: fmt (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)

🪛 GitHub Actions: Dependency Review

[error] 62-62: undefined: fmt

🪛 GitHub Actions: Lint

[error] 62-62: undefined: fmt (typecheck)

🪛 GitHub Actions: Build SimApp

[error] 62-62: undefined: fmt

🪛 GitHub Actions: v2 core Tests

[error] 62-62: undefined: fmt

@aljo242
Copy link
Collaborator

aljo242 commented Feb 12, 2025

hey @assafmo thanks for the PR!

Looks like build is failing. Could we also get some tests to reproduce the error case this is fixing

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

Successfully merging this pull request may close these issues.

2 participants