From 87823dd25004d09c8917b1bf87630580b25b0e96 Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Mon, 26 Sep 2022 11:50:25 -0400 Subject: [PATCH 1/2] Prioritize ante decorator --- app/ante.go | 3 ++ app/antedecorators/priority.go | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 app/antedecorators/priority.go diff --git a/app/ante.go b/app/ante.go index 2571f86781..51365edf0c 100644 --- a/app/ante.go +++ b/app/ante.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/ante" ibcante "github.com/cosmos/ibc-go/v3/modules/core/ante" ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper" + "github.com/sei-protocol/sei-chain/app/antedecorators" "github.com/sei-protocol/sei-chain/utils/tracing" "github.com/sei-protocol/sei-chain/x/dex" dexkeeper "github.com/sei-protocol/sei-chain/x/dex/keeper" @@ -77,6 +78,8 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { ante.NewValidateMemoDecorator(options.AccountKeeper), ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), + // PriorityDecorator must be called after DeductFeeDecorator which sets tx priority based on tx fees + antedecorators.NewPriorityDecorator(), // SetPubKeyDecorator must be called before all signature verification decorators ante.NewSetPubKeyDecorator(options.AccountKeeper), ante.NewValidateSigCountDecorator(options.AccountKeeper), diff --git a/app/antedecorators/priority.go b/app/antedecorators/priority.go new file mode 100644 index 0000000000..f9bf71ab24 --- /dev/null +++ b/app/antedecorators/priority.go @@ -0,0 +1,52 @@ +package antedecorators + +import ( + "math" + + sdk "github.com/cosmos/cosmos-sdk/types" + oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types" +) + +type PriorityDecorator struct {} + +func NewPriorityDecorator() PriorityDecorator { + return PriorityDecorator{} +} + +func intMin(a, b int64) int64 { + if a < b { + return a + } + return b +} + +// Assigns higher priority to certain types of transactions including oracle +func (pd PriorityDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { + // Cap priority to MAXINT64 - 1000 + // Use higher priorities for tiers including oracle tx's + priority := intMin(ctx.Priority(), math.MaxInt64 - 1000) + + if isOracleTx(tx) { + priority = math.MaxInt64 - 100 + } + + newCtx := ctx.WithPriority(priority) + + return next(newCtx, tx, simulate) +} + +func isOracleTx(tx sdk.Tx) bool { + if len(tx.GetMsgs()) == 0 { + // empty TX isn't oracle + return false + } + for _, msg := range tx.GetMsgs() { + switch msg.(type) { + case *oracletypes.MsgAggregateExchangeRateVote: + continue + default: + return false + } + } + return true +} From 6052f79933111dd166982d298734d69c6742e02d Mon Sep 17 00:00:00 2001 From: kbhat1 Date: Mon, 26 Sep 2022 11:56:44 -0400 Subject: [PATCH 2/2] lint --- app/antedecorators/priority.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/antedecorators/priority.go b/app/antedecorators/priority.go index f9bf71ab24..a8d741bcb5 100644 --- a/app/antedecorators/priority.go +++ b/app/antedecorators/priority.go @@ -7,24 +7,24 @@ import ( oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types" ) -type PriorityDecorator struct {} +type PriorityDecorator struct{} func NewPriorityDecorator() PriorityDecorator { return PriorityDecorator{} } func intMin(a, b int64) int64 { - if a < b { - return a - } - return b + if a < b { + return a + } + return b } // Assigns higher priority to certain types of transactions including oracle func (pd PriorityDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { // Cap priority to MAXINT64 - 1000 // Use higher priorities for tiers including oracle tx's - priority := intMin(ctx.Priority(), math.MaxInt64 - 1000) + priority := intMin(ctx.Priority(), math.MaxInt64-1000) if isOracleTx(tx) { priority = math.MaxInt64 - 100