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

Prettier GlobalFee fees errors #809

Merged
merged 4 commits into from
Aug 24, 2023
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
13 changes: 11 additions & 2 deletions x/globalfee/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
// if feeCoinsNoZeroDenom=[], DenomsSubsetOf returns true
// if feeCoinsNoZeroDenom is not empty, but nonZeroCoinFeesReq empty, return false
if !feeCoinsNonZeroDenom.DenomsSubsetOf(nonZeroCoinFeesReq) {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "fee is not a subset of required fees; got %s, required: %s", feeCoins, combinedFeeRequirement)
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "this fee denom is not accepted; got %s, one is required: %s", feeCoins, PrettyPrint(combinedFeeRequirement))
}

// Accept zero fee transactions only if both of the following statements are true:
Expand Down Expand Up @@ -125,7 +125,16 @@ func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
// because when nonZeroCoinFeesReq empty, and DenomsSubsetOf check passed,
// the tx should already passed before)
if !feeCoinsNonZeroDenom.IsAnyGTE(nonZeroCoinFeesReq) {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeCoins, combinedFeeRequirement)
if len(feeCoins) == 0 {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "no fees were specified; one fee must be provided %s", PrettyPrint(combinedFeeRequirement))
}

feeDiff := combinedFeeRequirement
for _, c := range feeCoins {
feeDiff = feeDiff.Sub(c)
}

return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; only got: %s. number of coins needed: %s. one is required: %s. ", feeCoins, feeDiff, PrettyPrint(combinedFeeRequirement))
}
}

Expand Down
18 changes: 18 additions & 0 deletions x/globalfee/ante/fee_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ante

import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -19,6 +22,21 @@ func ContainZeroCoins(coins sdk.Coins) bool {
return false
}

// PrettyPrint returns a pretty printed representation of the given coins.
func PrettyPrint(coins sdk.Coins) string {
arr := make([]string, len(coins))
for idx, coin := range coins {
arr[idx] = fmt.Sprintf("%s%s", coin.Amount.String(), coin.Denom)
}

bz, err := json.MarshalIndent(arr, "", " ")
if err != nil {
return ""
}

return string(bz)
}

// CombinedFeeRequirement returns the global fee and min_gas_price combined and sorted.
// Both globalFees and minGasPrices must be valid, but CombinedFeeRequirement
// does not validate them, so it may return 0denom.
Expand Down
Loading