Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compute proper tx prioritization #1290

Merged
merged 8 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions ante/fee.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ante

import (
"math"

gbtypes "github.com/Gravity-Bridge/Gravity-Bridge/module/x/gravity/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"

leveragetypes "github.com/umee-network/umee/v3/x/leverage/types"
oracletypes "github.com/umee-network/umee/v3/x/oracle/types"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func FeeAndPriority(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
}
}

priority := getTxPriority(feeCoins, isOracleOrGravity)
priority := getTxPriority(feeCoins, int64(gas), isOracleOrGravity, msgs)
if !chargeFees {
return sdk.Coins{}, priority, nil
}
Expand All @@ -59,6 +59,9 @@ func FeeAndPriority(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {

// IsOracleOrGravityTx checks if all messages are oracle messages
func IsOracleOrGravityTx(msgs []sdk.Msg) bool {
if len(msgs) == 0 {
return false
}
for _, msg := range msgs {
switch msg.(type) {
case *oracletypes.MsgAggregateExchangeRatePrevote,
Expand Down Expand Up @@ -89,23 +92,40 @@ func IsOracleOrGravityTx(msgs []sdk.Msg) bool {

// getTxPriority returns naive tx priority based on the lowest fee amount (regardless of the
// denom) and oracle tx check.
func getTxPriority(fee sdk.Coins, isOracleOrGravity bool) int64 {
func getTxPriority(fees sdk.Coins, gasAmount int64, isOracleOrGravity bool, msgs []sdk.Msg) int64 { //nolint: unparam
adamewozniak marked this conversation as resolved.
Show resolved Hide resolved
var priority int64
for _, c := range fee {
// TODO: we should better compare amounts
// https://github.com/umee-network/umee/issues/510
/* TODO: IBC tx prioritization is not stable and we will implement a more general
* tx prioritization once that will be resolved
* https://github.com/umee-network/umee/issues/1289
for _, c := range fees {
p := int64(math.MaxInt64)
if c.Amount.IsInt64() {
p = c.Amount.Int64()
gasPrice := c.Amount.QuoRaw(gasAmount)
if gasPrice.IsInt64() {
p = gasPrice.Int64()
}
if priority == 0 || p < priority {
priority = p
}
}
*/
if isOracleOrGravity {
// TODO: this is a naive version.
// Proper solution will be implemented in https://github.com/umee-network/umee/issues/510
priority += 100000
return 100
}
for _, msg := range msgs {
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
var p int64
switch msg.(type) {
case *evidencetypes.MsgSubmitEvidence:
p = 90
case *leveragetypes.MsgLiquidate:
p = 80
default:
// in case there is a non-prioritized mixed message, we return 0
return 0
}
if priority == 0 || p < priority {
priority = p
}
}

return priority
}
39 changes: 39 additions & 0 deletions ante/fee_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ante

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
evidence "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/stretchr/testify/assert"

leverage "github.com/umee-network/umee/v3/x/leverage/types"
)

func TestPriority(t *testing.T) {
tcs := []struct {
name string
oracle bool
msgs []sdk.Msg
priority int64
}{
{"empty priority 0", false, []sdk.Msg{}, 0},
{"when oracleOrGravity is set, then tx is max", true, []sdk.Msg{}, 100},
// dirty optimization - isOracleOrGravity flag takes a precendence
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
{"evidence1", true, []sdk.Msg{&evidence.MsgSubmitEvidence{}}, 100},
{"evidence2", false, []sdk.Msg{&evidence.MsgSubmitEvidence{}}, 90},
{"evidence3", false, []sdk.Msg{&evidence.MsgSubmitEvidence{}, &evidence.MsgSubmitEvidence{}}, 90},
{"leverage1", false, []sdk.Msg{&leverage.MsgLiquidate{}}, 80},
{"leverage-evidence1", false, []sdk.Msg{&leverage.MsgLiquidate{}, &evidence.MsgSubmitEvidence{}}, 80},
{"leverage-evidence2", false, []sdk.Msg{&evidence.MsgSubmitEvidence{}, &leverage.MsgLiquidate{}}, 80},
{"mixed1", false, []sdk.Msg{&evidence.MsgSubmitEvidence{}, &leverage.MsgLiquidate{}, &bank.MsgSend{}}, 0},
{"mixed2", false, []sdk.Msg{&bank.MsgSend{}, &evidence.MsgSubmitEvidence{}, &leverage.MsgLiquidate{}}, 0},
}

fee := sdk.Coins{}
for _, tc := range tcs {
p := getTxPriority(fee, 0, tc.oracle, tc.msgs)
assert.Equal(t, tc.priority, p, tc.name)
}
}