<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Follow up to have the priority middleware take the smallest fee to avoid gaming the system. --- ### 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/master/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/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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)
66 lines
2.0 KiB
Go
66 lines
2.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
|
)
|
|
|
|
var _ tx.Handler = txPriorityHandler{}
|
|
|
|
type txPriorityHandler struct {
|
|
next tx.Handler
|
|
}
|
|
|
|
// TxPriorityHandler implements tx handling middleware that determines a
|
|
// transaction's priority via a naive mechanism -- the total sum of fees provided.
|
|
// It sets the Priority in ResponseCheckTx only.
|
|
func TxPriorityHandler(txh tx.Handler) tx.Handler {
|
|
return txPriorityHandler{next: txh}
|
|
}
|
|
|
|
// CheckTx implements tx.Handler.CheckTx. We set the Priority of the transaction
|
|
// to be ordered in the Tendermint mempool based naively on the total sum of all
|
|
// fees included. Applications that need more sophisticated mempool ordering
|
|
// should look to implement their own fee handling middleware instead of using
|
|
// TxPriorityHandler.
|
|
func (h txPriorityHandler) CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error) {
|
|
feeTx, ok := tx.(sdk.FeeTx)
|
|
if !ok {
|
|
return abci.ResponseCheckTx{}, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
|
|
}
|
|
|
|
feeCoins := feeTx.GetFee()
|
|
|
|
res, err := h.next.CheckTx(ctx, tx, req)
|
|
res.Priority = GetTxPriority(feeCoins)
|
|
|
|
return res, err
|
|
}
|
|
|
|
func (h txPriorityHandler) DeliverTx(ctx context.Context, tx sdk.Tx, req abci.RequestDeliverTx) (abci.ResponseDeliverTx, error) {
|
|
return h.next.DeliverTx(ctx, tx, req)
|
|
}
|
|
|
|
func (h txPriorityHandler) SimulateTx(ctx context.Context, sdkTx sdk.Tx, req tx.RequestSimulateTx) (tx.ResponseSimulateTx, error) {
|
|
return h.next.SimulateTx(ctx, sdkTx, req)
|
|
}
|
|
|
|
// GetTxPriority returns a naive tx priority based on the amount of the smallest denomination of the fee
|
|
// provided in a transaction.
|
|
func GetTxPriority(fee sdk.Coins) int64 {
|
|
var priority int64
|
|
for _, c := range fee {
|
|
p := c.Amount.Int64()
|
|
if priority == 0 || p < priority {
|
|
priority = p
|
|
}
|
|
}
|
|
|
|
return priority
|
|
}
|