Skip to content

Commit f97cf85

Browse files
authored
feat: CheckTX Priority (#10507)
1 parent ed01c21 commit f97cf85

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

x/auth/middleware/fee.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"context"
55
"fmt"
66

7+
abci "github.com/tendermint/tendermint/abci/types"
8+
79
sdk "github.com/cosmos/cosmos-sdk/types"
810
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
9-
1011
"github.com/cosmos/cosmos-sdk/types/tx"
1112
"github.com/cosmos/cosmos-sdk/x/auth/types"
12-
abci "github.com/tendermint/tendermint/abci/types"
1313
)
1414

1515
var _ tx.Handler = mempoolFeeTxHandler{}
@@ -30,7 +30,12 @@ func MempoolFeeMiddleware(txh tx.Handler) tx.Handler {
3030
}
3131
}
3232

33-
// CheckTx implements tx.Handler.CheckTx.
33+
// CheckTx implements tx.Handler.CheckTx. It is responsible for determining if a
34+
// transaction's fees meet the required minimum of the processing node. Note, a
35+
// node can have zero fees set as the minimum. If non-zero minimum fees are set
36+
// and the transaction does not meet the minimum, the transaction is rejected.
37+
//
38+
// Recall, a transaction's fee is determined by ceil(minGasPrice * gasLimit).
3439
func (txh mempoolFeeTxHandler) CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error) {
3540
sdkCtx := sdk.UnwrapSDKContext(ctx)
3641

x/auth/middleware/middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func NewDefaultTxHandler(options TxHandlerOptions) (tx.Handler, error) {
8989
ValidateMemoMiddleware(options.AccountKeeper),
9090
ConsumeTxSizeGasMiddleware(options.AccountKeeper),
9191
DeductFeeMiddleware(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
92+
TxPriorityHandler,
9293
SetPubKeyMiddleware(options.AccountKeeper),
9394
ValidateSigCountMiddleware(options.AccountKeeper),
9495
SigGasConsumeMiddleware(options.AccountKeeper, sigGasConsumer),

x/auth/middleware/tx_priority.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)