cosmos-sdk/x/auth/posthandler/tips.go
Amaury d416ee86b6
refactor: Revert middlewares to antehandler (part 2/2: posthandler) (#11985)
## Description

We decided to remove middlewares, and revert to antehandlers (exactly like in v045) for this release. A better middleware solution will be implemented after v046.

ref: https://github.com/cosmos/cosmos-sdk/issues/11955

This PR is part 2 of 2:

- part 1: Revert baseapp and middlewares to v0.45.4
- part 2: Add posthandler, tips, priority

Depends on:
- [x] #11979 

---

Suggestion for reviewers:

- Apart from correctness, I would also like someone to review **exhaustiveness**. I.e. all changes we made in v046 into the [middleware folder](https://github.com/cosmos/cosmos-sdk/tree/v0.46.0-beta2/x/auth/middleware) are reflected in this PR, and that I didn't forget anything. I found the following ones:
  - add a TxFeeChecker in DeductFee
  - add a ExtensionChecker in ExtCheckerDecorator
  - add a TipDecorator



---

### 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...

- [ ] 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/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)
2022-05-23 10:32:38 +00:00

50 lines
1.4 KiB
Go

package posthandler
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
// ValidateBasicDecorator will call tx.ValidateBasic and return any non-nil error.
// If ValidateBasic passes, decorator calls next AnteHandler in chain. Note,
// ValidateBasicDecorator decorator will not get executed on ReCheckTx since it
// is not dependent on application state.
type tipDecorator struct {
bankKeeper types.BankKeeper
}
// NewTipDecorator returns a new decorator for handling transactions with
// tips.
func NewTipDecorator(bankKeeper types.BankKeeper) sdk.AnteDecorator {
return tipDecorator{
bankKeeper: bankKeeper,
}
}
func (d tipDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
err := d.transferTip(ctx, tx)
if err != nil {
return ctx, err
}
return next(ctx, tx, simulate)
}
// transferTip transfers the tip from the tipper to the fee payer.
func (d tipDecorator) transferTip(ctx sdk.Context, sdkTx sdk.Tx) error {
tipTx, ok := sdkTx.(tx.TipTx)
// No-op if the tx doesn't have tips.
if !ok || tipTx.GetTip() == nil {
return nil
}
tipper, err := sdk.AccAddressFromBech32(tipTx.GetTip().Tipper)
if err != nil {
return err
}
return d.bankKeeper.SendCoins(ctx, tipper, tipTx.FeePayer(), tipTx.GetTip().Amount)
}