Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
313c1ee
Implement standalone transaction prioritizer
masih Aug 20, 2025
1686671
Add unit tests for prioritizer
masih Aug 21, 2025
1c327c0
Preprocess EVM transactions to populate Derived
masih Aug 22, 2025
117d427
Fix associate tx prioritisation
masih Aug 22, 2025
0f92829
Update to latest and refactor to make hinting clear
masih Aug 25, 2025
c2084f5
Use the same logic as EVM router ante to detect message type
masih Aug 26, 2025
58f0571
Reduce unpacking of EVM tx data in preporcessing
masih Aug 27, 2025
67431ef
Upgrade to the latest sei tendermint and cosmos
masih Sep 3, 2025
942cb65
Upgrade to latest tendermint with additional metrics
masih Sep 4, 2025
5e9aa33
Bubble up fix to tendermint metrics tag
masih Sep 4, 2025
a1c82ac
Upgrade to latest tendermint with fix to tagging
masih Sep 4, 2025
7419130
Bubble up fix to metrics
masih Sep 4, 2025
589b9fa
Bubble up tendermint to update the default config template
masih Sep 5, 2025
94b301d
Bubble up tender mint for additional logging
masih Sep 5, 2025
2a16f3c
Bubble up cosmos
masih Sep 5, 2025
2dad465
Bubble up latest changes
masih Sep 5, 2025
e1b7c3d
Fix lint issues
masih Sep 5, 2025
64de5f9
Add unit test for ante handler priority
yzang2019 Sep 25, 2025
4483db7
Revert "Add unit test for ante handler priority"
yzang2019 Sep 25, 2025
1fad1e0
Merge branch 'main' into masih/abci-get-tx-priority-root
masih Oct 1, 2025
eee44b6
Upgrade tendermint to commit from merge to main
masih Oct 1, 2025
bfe56d4
Merge branch 'main' into masih/abci-get-tx-priority-root
masih Oct 1, 2025
eaf9517
Port over the changes from open sei-cosmos PR
masih Oct 1, 2025
d2fccb3
Upgrate to the compatible tendermint across all dependencies
masih Oct 1, 2025
00942ec
Update tendermint to concrete release tag
masih Oct 1, 2025
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
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ type App struct {
wsServerStartSignal chan struct{}
httpServerStartSignalSent bool
wsServerStartSignalSent bool

txPrioritizer sdk.TxPrioritizer
}

type AppOption func(*App)
Expand Down Expand Up @@ -985,6 +987,8 @@ func New(

app.RegisterDeliverTxHook(app.AddCosmosEventsToEVMReceiptIfApplicable)

app.txPrioritizer = NewSeiTxPrioritizer(logger, &app.EvmKeeper, &app.UpgradeKeeper, &app.ParamsKeeper).GetTxPriorityHint
app.SetTxPrioritizer(app.txPrioritizer)
return app
}

Expand Down
196 changes: 196 additions & 0 deletions app/prioritizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package app

import (
"math"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
cosmosante "github.com/cosmos/cosmos-sdk/x/auth/ante"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/sei-protocol/sei-chain/app/antedecorators"
"github.com/sei-protocol/sei-chain/utils"
evmante "github.com/sei-protocol/sei-chain/x/evm/ante"
"github.com/sei-protocol/sei-chain/x/evm/derived"
evmkeeper "github.com/sei-protocol/sei-chain/x/evm/keeper"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
"github.com/tendermint/tendermint/libs/log"
)

var _ sdk.TxPrioritizer = (*SeiTxPrioritizer)(nil).GetTxPriorityHint

type SeiTxPrioritizer struct {
evmKeeper *evmkeeper.Keeper
upgradeKeeper *upgradekeeper.Keeper
paramsKeeper *paramskeeper.Keeper
logger log.Logger
}

func NewSeiTxPrioritizer(logger log.Logger, ek *evmkeeper.Keeper, uk *upgradekeeper.Keeper, pk *paramskeeper.Keeper) *SeiTxPrioritizer {
return &SeiTxPrioritizer{
logger: logger,
evmKeeper: ek,
upgradeKeeper: uk,
paramsKeeper: pk,
}
}

func (s *SeiTxPrioritizer) GetTxPriorityHint(ctx sdk.Context, tx sdk.Tx) (_priorityHint int64, _err error) {
defer func() {
if r := recover(); r != nil {
// Fall back to no-op priority if we panic for any reason. This is to avoid DoS
// vectors where a malicious actor crafts a transaction that panics the
// prioritizer. Since the prioritizer is used as a hint only, it's safe to fall
// back to zero priority in this case and log the panic for monitoring purposes.
s.logger.Error("tx prioritizer panicked. Falling back on no priority", "error", r)
_priorityHint = 0
_err = nil
}
}()
if ctx.HasPriority() {
// The context already has a priority set, return it.
return ctx.Priority(), nil
}

if ok, err := evmante.IsEVMMessage(tx); err != nil {
return 0, err
} else if ok {
evmTx := evmtypes.GetEVMTransactionMessage(tx)
if evmTx != nil {
return s.getEvmTxPriority(ctx, evmTx)
}
// This should never happen since IsEVMMessage returned true. But we defensively
// return zero priority to be safe.
return 0, nil
}
if feeTx, ok := tx.(sdk.FeeTx); ok {
return s.getCosmosTxPriority(ctx, feeTx)
}
return 0, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must either be EVM or Fee")
}

func (s *SeiTxPrioritizer) getEvmTxPriority(ctx sdk.Context, evmTx *evmtypes.MsgEVMTransaction) (int64, error) {

// Unpack the transaction data first to avoid double unpacking as part of preprocessing.
txData, err := evmtypes.UnpackTxData(evmTx.Data)
if err != nil {
return 0, err
}

if err := evmante.PreprocessUnpacked(ctx, evmTx, s.evmKeeper.ChainID(ctx), s.evmKeeper.EthBlockTestConfig.Enabled, txData); err != nil {
return 0, err
}
if evmTx.Derived.IsAssociate {
_, isAssociated := s.evmKeeper.GetEVMAddress(
ctx.WithGasMeter(sdk.NewInfiniteGasMeterWithMultiplier(ctx)),
evmTx.Derived.SenderSeiAddr)
if !isAssociated {
// Unassociated associate transactions have the second-highest priority.
// This is to ensure that associate transactions are processed before
// regular transactions, but after oracle transactions.
//
// Note that we are not checking if sufficient funds are present here to keep the
// priority calculation fast. CheckTx should fully check the transaction.
return antedecorators.EVMAssociatePriority, nil
}
return 0, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "account already has association set")
}

// Check txData for sanity.
feeCap := txData.GetGasFeeCap()
fee := s.getEvmBaseFee(ctx)
if feeCap.Cmp(fee) < 0 {
return 0, sdkerrors.ErrInsufficientFee
}
minimumFee := s.evmKeeper.GetMinimumFeePerGas(ctx).TruncateInt().BigInt()
if feeCap.Cmp(minimumFee) < 0 {
return 0, sdkerrors.ErrInsufficientFee
}
if txData.GetGasTipCap().Sign() < 0 {
return 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "gas fee cap cannot be negative")
}
// Check blob hashes for sanity. If EVM version is Cancun or later, and the
// transaction contains at least one blob, we need to make sure the transaction
// carries a non-zero blob fee cap.
if evmTx.Derived != nil && evmTx.Derived.Version >= derived.Cancun && len(txData.GetBlobHashes()) > 0 {
// For now we are simply assuming excessive blob gas is 0. In the future we might change it to be
// dynamic based on prior block usage.
chainConfig := evmtypes.DefaultChainConfig().EthereumConfig(s.evmKeeper.ChainID(ctx))
if txData.GetBlobFeeCap().Cmp(eip4844.CalcBlobFee(chainConfig, &ethtypes.Header{Time: uint64(ctx.BlockTime().Unix())})) < 0 { //nolint:gosec
return 0, sdkerrors.ErrInsufficientFee
}
}

gp := txData.EffectiveGasPrice(utils.Big0)
priority := sdk.NewDecFromBigInt(gp).Quo(s.evmKeeper.GetPriorityNormalizer(ctx)).TruncateInt().BigInt()
if priority.Cmp(big.NewInt(antedecorators.MaxPriority)) > 0 {
priority = big.NewInt(antedecorators.MaxPriority)
}
return priority.Int64(), nil
}

func (s *SeiTxPrioritizer) getEvmBaseFee(ctx sdk.Context) *big.Int {
const (
pacific1 = "pacific-1"
historicalBlockHeight = 114945913
doneHeightName = "6.2.0"
)
if ctx.ChainID() == pacific1 {
height := ctx.BlockHeight()
if height < historicalBlockHeight {
return s.evmKeeper.GetBaseFeePerGas(ctx).TruncateInt().BigInt()
}

doneHeight := s.upgradeKeeper.GetDoneHeight(
ctx.WithGasMeter(sdk.NewInfiniteGasMeter(1, 1)), doneHeightName)
if height < doneHeight {
return s.evmKeeper.GetCurrBaseFeePerGas(ctx).TruncateInt().BigInt()
}
}
return s.evmKeeper.GetNextBaseFeePerGas(ctx).TruncateInt().BigInt()
}

func (s *SeiTxPrioritizer) getCosmosTxPriority(ctx sdk.Context, feeTx sdk.FeeTx) (int64, error) {
if isOracleTx(feeTx) {
return antedecorators.OraclePriority, nil
}

gas := feeTx.GetGas()
if gas <= 0 {
return 0, nil
}
var igas int64
if gas > math.MaxInt64 {
igas = math.MaxInt64
} else {
igas = int64(gas) //nolint:gosec
}

feeParams := s.paramsKeeper.GetFeesParams(ctx)
allowedDenoms := feeParams.GetAllowedFeeDenoms()
denoms := make([]string, 0, len(allowedDenoms)+1)
denoms = append(denoms, sdk.DefaultBondDenom)
denoms = append(denoms, allowedDenoms...)
feeCoins := feeTx.GetFee().NonZeroAmountsOf(denoms)
priority := cosmosante.GetTxPriority(feeCoins, igas)
return min(antedecorators.MaxPriority, priority), nil
}

func isOracleTx(tx sdk.FeeTx) bool {
if len(tx.GetMsgs()) == 0 {
return false
}
for _, msg := range tx.GetMsgs() {
switch msg.(type) {
case *oracletypes.MsgAggregateExchangeRateVote:
continue
default:
return false
}
}
return true
}
144 changes: 144 additions & 0 deletions app/prioritizer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package app_test

import (
"testing"

cosmostypes "github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
xparamtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/libs/log"

"github.com/sei-protocol/sei-chain/app"
"github.com/sei-protocol/sei-chain/app/antedecorators"
"github.com/sei-protocol/sei-chain/app/apptesting"
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
)

type PrioritizerTestSuite struct {
apptesting.KeeperTestHelper
prioritizer *app.SeiTxPrioritizer
}

func TestPrioritizerTestSuite(t *testing.T) {
suite.Run(t, new(PrioritizerTestSuite))
}

func (s *PrioritizerTestSuite) SetupTest() {
s.KeeperTestHelper.Setup()
logger, err := log.NewDefaultLogger(log.LogFormatPlain, "info")
require.NoError(s.T(), err)
s.prioritizer = app.NewSeiTxPrioritizer(logger, &s.App.EvmKeeper, &s.App.UpgradeKeeper, &s.App.ParamsKeeper)
}

var (
_ sdk.FeeTx = (*mockFeeTx)(nil)
_ sdk.Tx = (*mockTx)(nil)
)

type mockFeeTx struct {
sdk.Tx
fees sdk.Coins
gas uint64
msgs []sdk.Msg
}

func (tx *mockFeeTx) FeePayer() sdk.AccAddress { return nil }
func (tx *mockFeeTx) FeeGranter() sdk.AccAddress { return nil }
func (tx *mockFeeTx) GetFee() sdk.Coins { return tx.fees }
func (tx *mockFeeTx) GetGas() uint64 { return tx.gas }
func (tx *mockFeeTx) GetMsgs() []sdk.Msg { return tx.msgs }

type mockTx struct {
msgs []sdk.Msg
gasEstimate uint64
}

func (tx *mockTx) GetGasEstimate() uint64 { return tx.gasEstimate }
func (tx *mockTx) GetMsgs() []sdk.Msg { return tx.msgs }
func (*mockTx) ValidateBasic() error { return nil }
func (*mockTx) GetSigners() []sdk.AccAddress { return nil }

func (s *PrioritizerTestSuite) TestGetTxPriority() {
var (
zeroValueTx = func(*PrioritizerTestSuite) sdk.Tx { return &mockTx{} }
zeroValueFeeTx = func(*PrioritizerTestSuite) sdk.Tx { return &mockFeeTx{} }
zeroGasFeeTx = func(*PrioritizerTestSuite) sdk.Tx {
return &mockFeeTx{
gas: 0,
}
}
oracleVoteTx = func(s *PrioritizerTestSuite) sdk.Tx {
return &mockFeeTx{
msgs: []sdk.Msg{&oracletypes.MsgAggregateExchangeRateVote{}},
}
}
)

for _, tc := range []struct {
name string
givenTx func(s *PrioritizerTestSuite) sdk.Tx
givenContext func(sdk.Context) sdk.Context
wantPriority int64
wantErr string
expectedErrAs interface{}
}{
{
name: "unexpected Tx type is error",
givenTx: zeroValueTx,
wantErr: "must either be EVM or Fee",
},
{
name: "context with priority present is context priority",
givenTx: zeroValueFeeTx,
givenContext: func(ctx sdk.Context) sdk.Context {
return ctx.WithPriority(123)
},
wantPriority: 123,
},
{
name: "oracle Tx type is oracle priority",
givenTx: oracleVoteTx,
wantPriority: antedecorators.OraclePriority,
},
{
name: "zero gas FeeTx is zero priority",
givenTx: zeroGasFeeTx,
wantPriority: 0,
},
{
name: "cosmos tx with denominators is has priority of smallest demon multiplier",
givenTx: func(s *PrioritizerTestSuite) sdk.Tx {
s.App.ParamsKeeper.SetFeesParams(s.Ctx, xparamtypes.FeesParams{
AllowedFeeDenoms: []string{"fish", "lobster"},
})
return &mockFeeTx{
gas: 4_200,
fees: []sdk.Coin{
{Denom: "fish", Amount: sdk.NewInt(230_000_000)},
{Denom: "lobster", Amount: sdk.NewInt(290_000_000_000)},
},
}
},
wantPriority: cosmostypes.NewInt(230_000_000).QuoRaw(4_200).Int64(),
},
} {
s.T().Run(tc.name, func(t *testing.T) {
s.SetupTest()
tx := tc.givenTx(s)
ctx := s.Ctx
if tc.givenContext != nil {
ctx = tc.givenContext(ctx)
}
gotPriority, gotErr := s.prioritizer.GetTxPriorityHint(ctx, tx)
if tc.wantErr != "" {
require.Error(t, gotErr)
require.ErrorContains(t, gotErr, tc.wantErr)
} else {
require.NoError(t, gotErr)
require.Equal(t, tc.wantPriority, gotPriority)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ replace (
github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.51
// Latest goleveldb is broken, we have to stick to this version
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/tendermint => github.com/sei-protocol/sei-tendermint v0.6.4
github.com/tendermint/tendermint => github.com/sei-protocol/sei-tendermint v0.6.5
github.com/tendermint/tm-db => github.com/sei-protocol/tm-db v0.0.4
golang.org/x/crypto => golang.org/x/crypto v0.31.0
google.golang.org/grpc => google.golang.org/grpc v1.33.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1997,8 +1997,8 @@ github.com/sei-protocol/sei-iavl v0.2.0 h1:OisPjXiDT+oe+aeckzDEFgkZCYuUjHgs/PP8D
github.com/sei-protocol/sei-iavl v0.2.0/go.mod h1:qRf8QYUPfrAO7K6VDB2B2l/N7K5L76OorioGBcJBIbw=
github.com/sei-protocol/sei-ibc-go/v3 v3.3.6 h1:HHWvrslBpkXBHUFs+azwl36NuFEJyMo6huvsNPG854c=
github.com/sei-protocol/sei-ibc-go/v3 v3.3.6/go.mod h1:VwB/vWu4ysT5DN2aF78d17LYmx3omSAdq6gpKvM7XRA=
github.com/sei-protocol/sei-tendermint v0.6.4 h1:lMgzloTLo3ixNBHV+ETkUeh13fwOPqqdXZW3pZxQ8Bs=
github.com/sei-protocol/sei-tendermint v0.6.4/go.mod h1:SSZv0P1NBP/4uB3gZr5XJIan3ks3Ui8FJJzIap4r6uc=
github.com/sei-protocol/sei-tendermint v0.6.5 h1:6jJOw330mcK8Xu8PYiChByHpsl+yGujsl1WZXDW0G4Q=
github.com/sei-protocol/sei-tendermint v0.6.5/go.mod h1:SSZv0P1NBP/4uB3gZr5XJIan3ks3Ui8FJJzIap4r6uc=
github.com/sei-protocol/sei-tm-db v0.0.5 h1:3WONKdSXEqdZZeLuWYfK5hP37TJpfaUa13vAyAlvaQY=
github.com/sei-protocol/sei-tm-db v0.0.5/go.mod h1:Cpa6rGyczgthq7/0pI31jys2Fw0Nfrc+/jKdP1prVqY=
github.com/sei-protocol/tm-db v0.0.4 h1:7Y4EU62Xzzg6wKAHEotm7SXQR0aPLcGhKHkh3qd0tnk=
Expand Down
Loading
Loading