-
Notifications
You must be signed in to change notification settings - Fork 887
feat(cosmos): return errors from NewDecCoinsFromCoins (CON-370) #3997
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,11 +127,16 @@ func (k Keeper) WithdrawValidatorCommission(ctx sdk.Context, valAddr sdk.ValAddr | |
| } | ||
|
|
||
| commission, remainder := accumCommission.Commission.TruncateDecimal() | ||
| commissionDec, err := sdk.NewDecCoinsFromCoins(commission...) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] The conversion check runs after |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| k.SetValidatorAccumulatedCommission(ctx, valAddr, types.ValidatorAccumulatedCommission{Commission: remainder}) // leave remainder to withdraw later | ||
|
|
||
| // update outstanding | ||
| outstanding := k.GetValidatorOutstandingRewards(ctx, valAddr).Rewards | ||
| k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: outstanding.Sub(sdk.NewDecCoinsFromCoins(commission...))}) | ||
| k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: outstanding.Sub(commissionDec)}) | ||
|
|
||
| if !commission.IsZero() { | ||
| accAddr := sdk.AccAddress(valAddr) | ||
|
|
@@ -169,12 +174,17 @@ func (k Keeper) GetTotalRewards(ctx sdk.Context) (totalRewards sdk.DecCoins) { | |
| // added to the pool. An error is returned if the amount cannot be sent to the | ||
| // module account. | ||
| func (k Keeper) FundCommunityPool(ctx sdk.Context, amount sdk.Coins, sender sdk.AccAddress) error { | ||
| decAmount, err := sdk.NewDecCoinsFromCoins(amount...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, amount); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| feePool := k.GetFeePool(ctx) | ||
| feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...) | ||
| feePool.CommunityPool = feePool.CommunityPool.Add(decAmount...) | ||
| k.SetFeePool(ctx, feePool) | ||
|
|
||
| return nil | ||
|
|
||
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.
[suggestion] Returning
truefor a nilIntmakes the predicate disagree with the operation it guards:Int{}.ToDec()panics (big.Int.Mulon thenilreturned byBigInt()), so a caller that trustsCanConvertToDec()gets a panic anyway. This is unreachable fromNewDecCoinsFromCoins(theNewCoinscall rejects such coins first), butCanConvertToDecis new exported API onsdk.Intand other callers won't have that guarantee. Returningfalsefor nil — or documenting that the predicate assumes a non-nilInt— would make the contract matchToDec.Minor, same lines:
i.BigInt()already returns a fresh copy, so the wrappingnew(big.Int).Set(...)is a redundant allocation.