Skip to content

Commit

Permalink
Prettier GlobalFee fees errors (#809)
Browse files Browse the repository at this point in the history
* prettier fees failure output

* pretty fee subset

* no coins at all error message

* Prettier slice output. Show coins needed for fee
  • Loading branch information
Reecepbcups authored Aug 24, 2023
1 parent 7716b82 commit 023a2ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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

0 comments on commit 023a2ab

Please sign in to comment.