## Description Closes: #XXXX Right now this error message contains the word "not" twice. I don't think we want the double negative here ;-) Example: ``` regen1sk9nwk0w7kct04tsdscqvg5knn6kzz7yq59arx does not not allow to pay fees for regen1n9dpmra4d2gvu22jmxv7tenjy0mdymfg70hd48: basic allowance: fee limit exceeded ``` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
137 lines
4.0 KiB
Go
137 lines
4.0 KiB
Go
package ante
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
)
|
|
|
|
// TxFeeChecker check if the provided fee is enough and returns the effective fee and tx priority,
|
|
// the effective fee should be deducted later, and the priority should be returned in abci response.
|
|
type TxFeeChecker func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error)
|
|
|
|
// DeductFeeDecorator deducts fees from the first signer of the tx
|
|
// If the first signer does not have the funds to pay for the fees, return with InsufficientFunds error
|
|
// Call next AnteHandler if fees successfully deducted
|
|
// CONTRACT: Tx must implement FeeTx interface to use DeductFeeDecorator
|
|
type DeductFeeDecorator struct {
|
|
accountKeeper AccountKeeper
|
|
bankKeeper types.BankKeeper
|
|
feegrantKeeper FeegrantKeeper
|
|
txFeeChecker TxFeeChecker
|
|
}
|
|
|
|
func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper, fk FeegrantKeeper, tfc TxFeeChecker) DeductFeeDecorator {
|
|
if tfc == nil {
|
|
tfc = checkTxFeeWithValidatorMinGasPrices
|
|
}
|
|
|
|
return DeductFeeDecorator{
|
|
accountKeeper: ak,
|
|
bankKeeper: bk,
|
|
feegrantKeeper: fk,
|
|
txFeeChecker: tfc,
|
|
}
|
|
}
|
|
|
|
func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
|
feeTx, ok := tx.(sdk.FeeTx)
|
|
if !ok {
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
|
|
}
|
|
|
|
if !simulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 {
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas")
|
|
}
|
|
|
|
var (
|
|
priority int64
|
|
err error
|
|
)
|
|
|
|
fee := feeTx.GetFee()
|
|
if !simulate {
|
|
fee, priority, err = dfd.txFeeChecker(ctx, tx)
|
|
if err != nil {
|
|
return ctx, err
|
|
}
|
|
}
|
|
if err := dfd.checkDeductFee(ctx, tx, fee); err != nil {
|
|
return ctx, err
|
|
}
|
|
|
|
newCtx := ctx.WithPriority(priority)
|
|
|
|
return next(newCtx, tx, simulate)
|
|
}
|
|
|
|
func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error {
|
|
feeTx, ok := sdkTx.(sdk.FeeTx)
|
|
if !ok {
|
|
return sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
|
|
}
|
|
|
|
if addr := dfd.accountKeeper.GetModuleAddress(types.FeeCollectorName); addr == nil {
|
|
return fmt.Errorf("fee collector module account (%s) has not been set", types.FeeCollectorName)
|
|
}
|
|
|
|
feePayer := feeTx.FeePayer()
|
|
feeGranter := feeTx.FeeGranter()
|
|
deductFeesFrom := feePayer
|
|
|
|
// if feegranter set deduct fee from feegranter account.
|
|
// this works with only when feegrant enabled.
|
|
if feeGranter != nil {
|
|
if dfd.feegrantKeeper == nil {
|
|
return sdkerrors.ErrInvalidRequest.Wrap("fee grants are not enabled")
|
|
} else if !feeGranter.Equals(feePayer) {
|
|
err := dfd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, fee, sdkTx.GetMsgs())
|
|
if err != nil {
|
|
return sdkerrors.Wrapf(err, "%s does not allow to pay fees for %s", feeGranter, feePayer)
|
|
}
|
|
}
|
|
|
|
deductFeesFrom = feeGranter
|
|
}
|
|
|
|
deductFeesFromAcc := dfd.accountKeeper.GetAccount(ctx, deductFeesFrom)
|
|
if deductFeesFromAcc == nil {
|
|
return sdkerrors.ErrUnknownAddress.Wrapf("fee payer address: %s does not exist", deductFeesFrom)
|
|
}
|
|
|
|
// deduct the fees
|
|
if !fee.IsZero() {
|
|
err := DeductFees(dfd.bankKeeper, ctx, deductFeesFromAcc, fee)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
events := sdk.Events{
|
|
sdk.NewEvent(
|
|
sdk.EventTypeTx,
|
|
sdk.NewAttribute(sdk.AttributeKeyFee, fee.String()),
|
|
sdk.NewAttribute(sdk.AttributeKeyFeePayer, deductFeesFrom.String()),
|
|
),
|
|
}
|
|
ctx.EventManager().EmitEvents(events)
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeductFees deducts fees from the given account.
|
|
func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc types.AccountI, fees sdk.Coins) error {
|
|
if !fees.IsValid() {
|
|
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "invalid fee amount: %s", fees)
|
|
}
|
|
|
|
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), types.FeeCollectorName, fees)
|
|
if err != nil {
|
|
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
|
|
}
|
|
|
|
return nil
|
|
}
|