From b36f340a47f6047a7a6377f50d716ab6e9a3268e Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 8 Nov 2021 09:57:12 -0500 Subject: [PATCH 1/5] init commit --- x/auth/middleware/fee.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/x/auth/middleware/fee.go b/x/auth/middleware/fee.go index 7285d530cfb2..77e737f46c86 100644 --- a/x/auth/middleware/fee.go +++ b/x/auth/middleware/fee.go @@ -4,12 +4,12 @@ import ( "context" "fmt" + 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" "github.com/cosmos/cosmos-sdk/x/auth/types" - abci "github.com/tendermint/tendermint/abci/types" ) var _ tx.Handler = mempoolFeeTxHandler{} @@ -62,7 +62,10 @@ func (txh mempoolFeeTxHandler) CheckTx(ctx context.Context, tx sdk.Tx, req abci. } } - return txh.next.CheckTx(ctx, tx, req) + res, err := txh.next.CheckTx(ctx, tx, req) + res.Priority = GetTxPriority(feeCoins) + + return res, err } // DeliverTx implements tx.Handler.DeliverTx. @@ -192,3 +195,14 @@ func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc types.AccountI return nil } + +// GetTxPriority returns a naive tx priority based on the total sum of all fees +// provided in a transaction. +func GetTxPriority(fee sdk.Coins) int64 { + var priority int64 + for _, c := range fee { + priority += c.Amount.Int64() + } + + return priority +} From 647068d043615794223c196be1b1ac65dbe6d816 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 8 Nov 2021 10:02:56 -0500 Subject: [PATCH 2/5] godoc++ --- x/auth/middleware/fee.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/x/auth/middleware/fee.go b/x/auth/middleware/fee.go index 77e737f46c86..158fcdbbb1b8 100644 --- a/x/auth/middleware/fee.go +++ b/x/auth/middleware/fee.go @@ -30,7 +30,17 @@ func MempoolFeeMiddleware(txh tx.Handler) tx.Handler { } } -// CheckTx implements tx.Handler.CheckTx. +// CheckTx implements tx.Handler.CheckTx. It is responsible for determining if a +// transaction's fees meet the required minimum of the processing node. Note, a +// node can have zero fees set as the minimum. If non-zero minimum fees are set +// and the transaction does not meet the minimum, the transaction is rejected. +// +// Recall, a transaction's fee is determined by ceil(minGasPrice * gasLimit). +// In addition, 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 +// mempoolFeeTxHandler. func (txh mempoolFeeTxHandler) CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error) { sdkCtx := sdk.UnwrapSDKContext(ctx) From d90ebd4884a5ef70f3e4ffd1e374a4acfca75cf7 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 11 Nov 2021 13:14:27 -0500 Subject: [PATCH 3/5] add middlware --- x/auth/middleware/fee.go | 16 +-------- x/auth/middleware/tx_priority.go | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 x/auth/middleware/tx_priority.go diff --git a/x/auth/middleware/fee.go b/x/auth/middleware/fee.go index 158fcdbbb1b8..7f6877d678fa 100644 --- a/x/auth/middleware/fee.go +++ b/x/auth/middleware/fee.go @@ -72,10 +72,7 @@ func (txh mempoolFeeTxHandler) CheckTx(ctx context.Context, tx sdk.Tx, req abci. } } - res, err := txh.next.CheckTx(ctx, tx, req) - res.Priority = GetTxPriority(feeCoins) - - return res, err + return txh.next.CheckTx(ctx, tx, req) } // DeliverTx implements tx.Handler.DeliverTx. @@ -205,14 +202,3 @@ func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc types.AccountI return nil } - -// GetTxPriority returns a naive tx priority based on the total sum of all fees -// provided in a transaction. -func GetTxPriority(fee sdk.Coins) int64 { - var priority int64 - for _, c := range fee { - priority += c.Amount.Int64() - } - - return priority -} diff --git a/x/auth/middleware/tx_priority.go b/x/auth/middleware/tx_priority.go new file mode 100644 index 000000000000..9bd745cf944e --- /dev/null +++ b/x/auth/middleware/tx_priority.go @@ -0,0 +1,57 @@ +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} +} + +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 total sum of all fees +// provided in a transaction. +func GetTxPriority(fee sdk.Coins) int64 { + var priority int64 + for _, c := range fee { + priority += c.Amount.Int64() + } + + return priority +} From cc93efda0244946fd3ffcbdb1277235b45954e66 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 11 Nov 2021 13:15:15 -0500 Subject: [PATCH 4/5] updates --- x/auth/middleware/middleware.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/auth/middleware/middleware.go b/x/auth/middleware/middleware.go index 60820bb46cc5..90856680302e 100644 --- a/x/auth/middleware/middleware.go +++ b/x/auth/middleware/middleware.go @@ -89,6 +89,7 @@ func NewDefaultTxHandler(options TxHandlerOptions) (tx.Handler, error) { ValidateMemoMiddleware(options.AccountKeeper), ConsumeTxSizeGasMiddleware(options.AccountKeeper), DeductFeeMiddleware(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper), + TxPriorityHandler, SetPubKeyMiddleware(options.AccountKeeper), ValidateSigCountMiddleware(options.AccountKeeper), SigGasConsumeMiddleware(options.AccountKeeper, sigGasConsumer), From cb0a512c4bb80c21c24cdc3aaed8f85d1d763d7f Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 11 Nov 2021 13:16:43 -0500 Subject: [PATCH 5/5] updates --- x/auth/middleware/fee.go | 5 ----- x/auth/middleware/tx_priority.go | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/auth/middleware/fee.go b/x/auth/middleware/fee.go index 7f6877d678fa..4d980fb059a5 100644 --- a/x/auth/middleware/fee.go +++ b/x/auth/middleware/fee.go @@ -36,11 +36,6 @@ func MempoolFeeMiddleware(txh tx.Handler) tx.Handler { // and the transaction does not meet the minimum, the transaction is rejected. // // Recall, a transaction's fee is determined by ceil(minGasPrice * gasLimit). -// In addition, 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 -// mempoolFeeTxHandler. func (txh mempoolFeeTxHandler) CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error) { sdkCtx := sdk.UnwrapSDKContext(ctx) diff --git a/x/auth/middleware/tx_priority.go b/x/auth/middleware/tx_priority.go index 9bd745cf944e..81d8d3124e7b 100644 --- a/x/auth/middleware/tx_priority.go +++ b/x/auth/middleware/tx_priority.go @@ -23,6 +23,11 @@ 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 {