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

refactor: Move FormatCoins to core #13306

Merged
merged 17 commits into from
Oct 19, 2022
Merged

refactor: Move FormatCoins to core #13306

merged 17 commits into from
Oct 19, 2022

Conversation

amaury1093
Copy link
Contributor

Description

ref: #13155


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@codecov
Copy link

codecov bot commented Sep 15, 2022

Codecov Report

Merging #13306 (a1287e8) into main (49ad3b0) will increase coverage by 1.07%.
The diff coverage is 86.11%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #13306      +/-   ##
==========================================
+ Coverage   54.70%   55.78%   +1.07%     
==========================================
  Files         645      692      +47     
  Lines       55691    59931    +4240     
==========================================
+ Hits        30466    33431    +2965     
- Misses      22736    23667     +931     
- Partials     2489     2833     +344     
Impacted Files Coverage Δ
core/coins/format.go 84.21% <84.21%> (ø)
math/dec.go 72.53% <85.00%> (ø)
math/int.go 74.08% <88.88%> (ø)
tx/textual/valuerenderer/coins.go 73.17% <100.00%> (ø)
tx/textual/valuerenderer/dec.go 50.00% <100.00%> (ø)
tx/textual/valuerenderer/int.go 50.00% <100.00%> (ø)
x/group/keeper/keeper.go 56.25% <0.00%> (-0.40%) ⬇️
orm/encoding/ormfield/bool.go 81.48% <0.00%> (ø)
orm/encoding/ormfield/timestamp.go 76.31% <0.00%> (ø)
orm/model/ormtable/table_impl.go 59.92% <0.00%> (ø)
... and 43 more

@amaury1093 amaury1093 marked this pull request as ready for review September 15, 2022 14:43
@amaury1093 amaury1093 requested a review from a team as a code owner September 15, 2022 14:43
// alphabetically by value-rendered denoms. It expects an array of metadata
// (optionally nil), where each metadata at index `i` MUST match the coin denom
// at the same index.
func FormatCoins(coins []*basev1beta1.Coin, metadata []*bankv1beta1.Metadata) (string, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aaronc This PR only does Format. Parsing hasnt' been implemented yet (see #13153), do you need it for AutoCLI? If so we can bump that issue up in priority.

Copy link
Member

Choose a reason for hiding this comment

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

autocli actually only needs parsing...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, ok, let's still get this merged IMO. Then we can add parsing directly here.


func TestFormatCoins(t *testing.T) {
var testcases []coinsJsonTest
raw, err := os.ReadFile("../../tx/textual/internal/testdata/coins.json")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This core test imports a tx module's json file, which is not ideal.

We could alternatively:

  • test FormatCoins in the tx module. Con: unit test not next to its function
  • move coins.json into core. Con: we'll have some ValueRenderer json files in tx, others in core

For now the PR's version is my favorite. Any other opinions?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

What you're doing is also maybe not terrible 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am going to keep it like this for now

math/dec.go Show resolved Hide resolved
coinExp = unit.Exponent
foundCoinExp = true
}
if dispDenom == unit.Denom {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we comparing against unit.Denom here? shouldn't it be unit.Display?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's no unit.Display, the metadata object looks like

{
  "metadata": {
    "description": "The native staking token of the Cosmos Hub.",
    "denom_units": [
      {
        "denom": "uatom",
        "exponent": 0,
        "aliases": [
          "microatom"
        ]
      },
      {
        "denom": "matom",
        "exponent": 3,
        "aliases": [
          "milliatom"
        ]
      },
      {
        "denom": "atom",
        "exponent": 6,
        "aliases": [
        ]
      }
    ],
    "base": "uatom",
    "display": "atom",
    "name": "",
    "symbol": ""
  }
}

This piece of code finds the denom unit object (which contains the exponent) from inside the denom_units array

tx/textual/valuerenderer/coins.go Show resolved Hide resolved
@amaury1093
Copy link
Contributor Author

This is R4R again

if exponentDiff > 0 {
dispAmount = dispAmount.Mul(math.LegacyNewDec(10).Power(uint64(exponentDiff)))
} else {
dispAmount = dispAmount.Quo(math.LegacyNewDec(10).Power(uint64(-exponentDiff)))

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
}

if exponentDiff > 0 {
dispAmount = dispAmount.Mul(math.LegacyNewDec(10).Power(uint64(exponentDiff)))

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
return vr + " " + coin.Denom, err
}

exponentDiff := int64(coinExp) - int64(dispExp)

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
return vr + " " + coin.Denom, err
}

exponentDiff := int64(coinExp) - int64(dispExp)

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
core/coins/format_test.go Outdated Show resolved Hide resolved
Comment on lines +34 to +35
// temporary until we tag a new go module
replace cosmossdk.io/math => ../math
Copy link
Contributor

Choose a reason for hiding this comment

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

this is kinda annoying...right? make changes, add replace directive, tag, remove replace directive (assuming you don't forget).

Can we think of a better way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Maybe we can always keep replaces on main, and use tags in release branches? I don't know.

Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't do separate stuff on release branches. When we have more go.mod's release branches become much less necessary. We should usually be tagging off main.

Ideally we'd have a bot that whenever it sees a merged commit with local replace directives, opens a PR to remove them which we can merge after we tag.

tx/textual/valuerenderer/coins.go Show resolved Hide resolved
@amaury1093
Copy link
Contributor Author

I'll go ahead and merge this. @aaronc I'll create the reverse Parse function asap (that you need for autocli iiuc). If there are API changes you'd like to see, we can address them in the Parse PR.

@amaury1093 amaury1093 enabled auto-merge (squash) October 19, 2022 09:33
@amaury1093 amaury1093 merged commit 10ac33e into main Oct 19, 2022
@amaury1093 amaury1093 deleted the am/13155-core-coins branch October 19, 2022 11:21
@amaury1093
Copy link
Contributor Author

I'm working on a ParseCoins function which will bump these packages again. Can I tag new packages after that PR is merged?

@alexanderbez
Copy link
Contributor

Yes!

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

Successfully merging this pull request may close these issues.

4 participants