Skip to content

Commit

Permalink
perf(ante/fee): optimize requiredFees calculation (#1418)
Browse files Browse the repository at this point in the history
* perf(ante/fee): optimize requiredFees calculation

* fix

Co-authored-by: toteki <63419657+toteki@users.noreply.github.com>
  • Loading branch information
robert-zaremba and toteki committed Sep 19, 2022
1 parent 4124ef7 commit 1f8bc6f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ func checkFees(minGasPrices sdk.DecCoins, fees sdk.Coins, gasLimit uint64) error
minGasPrices = sdk.DecCoins{appparams.MinMinGasPrice}
}

requiredFees := make(sdk.Coins, len(minGasPrices))
requiredFees := sdk.NewCoins()

// Determine the required fees by multiplying each required minimum gas
// price by the gas limit, where fee = ceil(minGasPrice * gasLimit).
// Zero fees are removed.
glDec := sdk.NewDec(int64(gasLimit))
for i, gp := range minGasPrices {
for _, gp := range minGasPrices {
if gasLimit == 0 || gp.IsZero() {
continue
}
fee := gp.Amount.Mul(glDec)
requiredFees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
requiredFees = append(requiredFees, sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt()))
}

// Clear any zero coins from requiredFees in the case of zero min gas price
requiredFees = sdk.NewCoins(requiredFees...)

if !requiredFees.Empty() && !fees.IsAnyGTE(requiredFees) {
return sdkerrors.ErrInsufficientFee.Wrapf(
"insufficient fees; got: %s required: %s", fees, requiredFees)
Expand Down

0 comments on commit 1f8bc6f

Please sign in to comment.