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

fix(textual): error if a formatted coin contains a comma #19265

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Bug Fixes

* [#19265](https://github.com/cosmos/cosmos-sdk/pull/19265) Reject denoms that contain a comma.
Copy link
Contributor

Choose a reason for hiding this comment

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

The changelog entry for the bug fix correctly links to the PR and succinctly describes the change. However, it could be more informative by briefly explaining the impact of the change, such as "to prevent misinterpretation of coin amounts."

- * [#19265](https://github.com/cosmos/cosmos-sdk/pull/19265) Reject denoms that contain a comma.
+ * [#19265](https://github.com/cosmos/cosmos-sdk/pull/19265) Reject denoms that contain a comma to prevent misinterpretation of coin amounts.

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.

Suggested change
* [#19265](https://github.com/cosmos/cosmos-sdk/pull/19265) Reject denoms that contain a comma.
* [#19265](https://github.com/cosmos/cosmos-sdk/pull/19265) Reject denoms that contain a comma to prevent misinterpretation of coin amounts.


Copy link
Contributor

Choose a reason for hiding this comment

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

The improvement entry correctly documents the relocation of FormatCoins. However, it's missing a brief rationale for why this change is beneficial, which could help in understanding the context of the improvement.

- * [#18857](https://github.com/cosmos/cosmos-sdk/pull/18857) Moved `FormatCoins` from `core/coins` to this package under `signing/textual`.
+ * [#18857](https://github.com/cosmos/cosmos-sdk/pull/18857) Moved `FormatCoins` from `core/coins` to this package under `signing/textual` for better modularity and clarity in coin formatting.

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.

Suggested change

### Improvements

* [#18857](https://github.com/cosmos/cosmos-sdk/pull/18857) Moved `FormatCoins` from `core/coins` to this package under `signing/textual`.
Expand Down
6 changes: 6 additions & 0 deletions x/tx/signing/textual/coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ func FormatCoins(coins []*basev1beta1.Coin, metadata []*bankv1beta1.Metadata) (s
if err != nil {
return "", err
}

// If a coin contains a comma, return an error given that the output
// could be misinterpreted by the user as 2 different coins.
if strings.Contains(formatted[i], ",") {
return "", fmt.Errorf("coin %s contains a comma", formatted[i])
}
Comment on lines +288 to +292
Copy link
Contributor

Choose a reason for hiding this comment

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

The addition of a check for commas in the formatted coin strings is a crucial update to prevent user confusion. However, consider adding a unit test to cover this new behavior, ensuring that coins containing commas indeed trigger an error as expected. This will help maintain the robustness of the FormatCoins function.

Would you like me to help by adding a unit test for this scenario?

Comment on lines +290 to +292
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: curious if we can future proof this by using a regex instead, s.t. we fail if it doesn't match the regex expression.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have a global regex that is configurable, I'm not sure how we should go about it

}

if len(coins) == 0 {
Comment on lines 284 to 295
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Based on the output from the executed scripts, it appears that both the CHANGELOG.md and the documentation might require updates to reflect the changes made to the FormatCoins function. This suggests that the original review comment about ensuring documentation and changelog entries are updated is valid and actionable.

Analysis chain

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-292]

Overall, the file's structure and the logic within the FormatCoins function appear sound and well-organized. The changes made align with the PR's objectives to enhance clarity in coin formatting. It's important to ensure that all related documentation and changelog entries are updated to reflect this new error condition for future reference and SDK users' awareness.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify if documentation or changelog needs updates regarding the new error condition.
fd 'CHANGELOG.md' --exec grep -q 'FormatCoins' {} \; || echo "CHANGELOG.md needs an update for FormatCoins changes."
fd '\.md$' --exec grep -q 'FormatCoins' {} \; || echo "Documentation might need updates for FormatCoins changes."

Length of output: 344

Expand Down
7 changes: 6 additions & 1 deletion x/tx/signing/textual/internal/testdata/coin.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,10 @@
{"text":"", "error": true},
{"text":"1COSM", "error": true},
{"text":"1 COSM", "error": true},
{"text":" 1 COSM", "error": true}
{"text":" 1 COSM", "error": true},
{
"proto": {"amount": "10000000", "denom": "point, 222222 point"},
"metadata": {"display": "POINT", "base": "point", "denom_units": [{"denom": "point", "exponent": 0}, {"denom": "POINT", "exponent": 0}]},
"error": true
}
]
Loading