-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add New constructor for the DecCoin #5449
Conversation
types/dec_coin_test.go
Outdated
require.False(t, tc.coin.IsValid(), tc.msg) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
)
} | |
} |
There was a problem hiding this 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
// 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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
types/dec_coin.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
…os-sdk into I5430-deccoins-proposal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK
There was a problem hiding this 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 🎉
@@ -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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Codecov Report
@@ 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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK
Closes: #5430
Description
Implements the proposal for new constructor of the
DecCoins
.types.NewDecCoins
totypes.NewDecCoinsFromCoin
NewDecCoins
with a new functionality createsNewDecCoins
with the variadic function parameters.AddDecCoin
method forDecCoins
.If it fits the proposal let me know and I will fix the docs.
For contributor use:
docs/
) or specification (x/<module>/spec/
)godoc
comments.Unreleased
section inCHANGELOG.md
Files changed
in the Github PR explorerFor admin use:
WIP
,R4R
,docs
, etc)