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

Add New constructor for the DecCoin #5449

Merged
merged 17 commits into from
Jan 3, 2020

Conversation

PumpkinSeed
Copy link
Contributor

Closes: #5430

Description

Implements the proposal for new constructor of the DecCoins.

  • Rename the types.NewDecCoins to types.NewDecCoinsFromCoin
  • Add NewDecCoins with a new functionality creates NewDecCoins with the variadic function parameters.
  • Add AddDecCoin method for DecCoins.

If it fits the proposal let me know and I will fix the docs.


For contributor use:

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

For admin use:

  • Added appropriate labels to PR (ex. WIP, R4R, docs, etc)
  • Reviewers assigned
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)

require.False(t, tc.coin.IsValid(), tc.msg)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

File is not gofmt-ed with -s (from gofmt)

Suggested change
}
}

Copy link
Collaborator

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

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

Thanks @PumpkinSeed! Some changes are required as we also need to perform a validation on the constructor functions

types/dec_coin.go Outdated Show resolved Hide resolved
// NewDecCoins constructs a new coin set with decimal values
// NewDecCoins constructs a new coin set with with decimal values
// from DecCoins.
func NewDecCoins(decCoinArray ...DecCoin) DecCoins {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to add extra checks for the coins that are being created with the constructor:

  • remove empty (zero value) DecCoins
  • sort coins by denomination
  • panic on duplicated denomination
  • panic on invalid decCoin

You can check the NewCoins constructor for reference

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 tried to make the findDup a bit more generic to use for Coins and DecCoins with the same function, but I'm not a big fan of interfaces for these solutions of reference types because of the heap allocations of them. But if you agree with that it's good to me.

@@ -169,6 +180,11 @@ func NewDecCoins(coins Coins) DecCoins {
return decCoins
}

// AddDecCoin adds a new decimal coin to the existed set of decimal coins.
func (coins DecCoins) AddDecCoin(decCoin DecCoin) DecCoins {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function needs to have the same checks as NewCoins. Technically it's duplicate of func (coins DecCoins) Add(coinsB DecCoins) DecCoins (L227) with the difference that this only appends a single coin without validation.

Ideally, it could be changed to Add(coinsB ...DecCoin) DecCoins to support both cases.

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'm good with the rewrite of the Add method and the remove of the AddDecCoin, but it can be a change on the library usage so it can break third-party systems who using this. I'm not familiar enough with this to make this decision.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm good with the rewrite of the Add method and the remove of the AddDecCoin, but it can be a change on the library usage so it can break third-party systems who using this.

It's fine as long as it's documented as an API breaking change on the CHANGELOG

types/dec_coin.go Outdated Show resolved Hide resolved
types/dec_coin_test.go Outdated Show resolved Hide resolved
types/dec_coin_test.go Outdated Show resolved Hide resolved
PumpkinSeed and others added 5 commits December 31, 2019 13:44
Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Copy link
Collaborator

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

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

ACK

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Thanks @PumpkinSeed! Two minor nitpicks, but otherwise LGTM 🎉

types/dec_coin.go Outdated Show resolved Hide resolved
@@ -208,7 +231,7 @@ func (coins DecCoins) TruncateDecimal() (truncatedCoins Coins, changeCoins DecCo
//
// CONTRACT: Add will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
func (coins DecCoins) Add(coinsB DecCoins) DecCoins {
func (coins DecCoins) Add(coinsB ...DecCoin) DecCoins {
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 different than the coins variant which takes a slice literal. I'd rather be consistent here and accept the slice literal instead of variadic args. What was the rationale for this @fedekunze?

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 will wait for the response of @fedekunze and based on that I will do the necessary changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What was the rationale for this @fedekunze?

see #5449 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see the rationale there. The API is still inconsistent. Change this method or also change Coins#Add.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I can have my own opinion, I would change the Coins#Add because the variadic args opens up more clear ways to pass one or more data or slice of data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexanderbez I changed Coins#Add, also marked in the API Breaking section. If you wanted to move to the other direction and revert the changes on the DecCoins#Add please let me know.

PumpkinSeed and others added 2 commits January 2, 2020 15:47
Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Jan 3, 2020

Codecov Report

Merging #5449 into master will decrease coverage by 0.02%.
The diff coverage is 68.57%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5449      +/-   ##
==========================================
- Coverage   54.41%   54.38%   -0.03%     
==========================================
  Files         313      313              
  Lines       18836    18850      +14     
==========================================
+ Hits        10249    10252       +3     
- Misses       7802     7811       +9     
- Partials      785      787       +2
Impacted Files Coverage Δ
x/distribution/keeper/hooks.go 30.23% <0%> (ø) ⬆️
x/distribution/keeper/invariants.go 0% <0%> (ø) ⬆️
x/distribution/genesis.go 0% <0%> (ø) ⬆️
simapp/export.go 7.59% <0%> (ø) ⬆️
x/distribution/keeper/fee_pool.go 0% <0%> (ø) ⬆️
x/mock/app.go 64.18% <0%> (ø) ⬆️
x/mock/types.go 11.76% <0%> (ø) ⬆️
simapp/test_helpers.go 0% <0%> (ø) ⬆️
x/auth/types/stdtx.go 61.53% <0%> (ø) ⬆️
x/bank/internal/types/msgs.go 97.22% <100%> (ø) ⬆️
... and 15 more

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

ACK

@alexanderbez alexanderbez merged commit 066dd11 into cosmos:master Jan 3, 2020
larry0x pushed a commit to larry0x/cosmos-sdk that referenced this pull request May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create DecCoins from DecCoin
4 participants