|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + abci "github.com/tendermint/tendermint/abci/types" |
| 7 | + |
| 8 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 10 | + "github.com/cosmos/cosmos-sdk/types/tx" |
| 11 | +) |
| 12 | + |
| 13 | +var _ tx.Handler = txPriorityHandler{} |
| 14 | + |
| 15 | +type txPriorityHandler struct { |
| 16 | + next tx.Handler |
| 17 | +} |
| 18 | + |
| 19 | +// TxPriorityHandler implements tx handling middleware that determines a |
| 20 | +// transaction's priority via a naive mechanism -- the total sum of fees provided. |
| 21 | +// It sets the Priority in ResponseCheckTx only. |
| 22 | +func TxPriorityHandler(txh tx.Handler) tx.Handler { |
| 23 | + return txPriorityHandler{next: txh} |
| 24 | +} |
| 25 | + |
| 26 | +// CheckTx implements tx.Handler.CheckTx. We set the Priority of the transaction |
| 27 | +// to be ordered in the Tendermint mempool based naively on the total sum of all |
| 28 | +// fees included. Applications that need more sophisticated mempool ordering |
| 29 | +// should look to implement their own fee handling middleware instead of using |
| 30 | +// TxPriorityHandler. |
| 31 | +func (h txPriorityHandler) CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error) { |
| 32 | + feeTx, ok := tx.(sdk.FeeTx) |
| 33 | + if !ok { |
| 34 | + return abci.ResponseCheckTx{}, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") |
| 35 | + } |
| 36 | + |
| 37 | + feeCoins := feeTx.GetFee() |
| 38 | + |
| 39 | + res, err := h.next.CheckTx(ctx, tx, req) |
| 40 | + res.Priority = GetTxPriority(feeCoins) |
| 41 | + |
| 42 | + return res, err |
| 43 | +} |
| 44 | + |
| 45 | +func (h txPriorityHandler) DeliverTx(ctx context.Context, tx sdk.Tx, req abci.RequestDeliverTx) (abci.ResponseDeliverTx, error) { |
| 46 | + return h.next.DeliverTx(ctx, tx, req) |
| 47 | +} |
| 48 | + |
| 49 | +func (h txPriorityHandler) SimulateTx(ctx context.Context, sdkTx sdk.Tx, req tx.RequestSimulateTx) (tx.ResponseSimulateTx, error) { |
| 50 | + return h.next.SimulateTx(ctx, sdkTx, req) |
| 51 | +} |
| 52 | + |
| 53 | +// GetTxPriority returns a naive tx priority based on the total sum of all fees |
| 54 | +// provided in a transaction. |
| 55 | +func GetTxPriority(fee sdk.Coins) int64 { |
| 56 | + var priority int64 |
| 57 | + for _, c := range fee { |
| 58 | + priority += c.Amount.Int64() |
| 59 | + } |
| 60 | + |
| 61 | + return priority |
| 62 | +} |
0 commit comments