-
Notifications
You must be signed in to change notification settings - Fork 858
Implement standalone transaction prioritizer #2320
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
Merged
Merged
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 1686671
Add unit tests for prioritizer
masih 1c327c0
Preprocess EVM transactions to populate Derived
masih 117d427
Fix associate tx prioritisation
masih 0f92829
Update to latest and refactor to make hinting clear
masih c2084f5
Use the same logic as EVM router ante to detect message type
masih 58f0571
Reduce unpacking of EVM tx data in preporcessing
masih 67431ef
Upgrade to the latest sei tendermint and cosmos
masih 942cb65
Upgrade to latest tendermint with additional metrics
masih 5e9aa33
Bubble up fix to tendermint metrics tag
masih a1c82ac
Upgrade to latest tendermint with fix to tagging
masih 7419130
Bubble up fix to metrics
masih 589b9fa
Bubble up tendermint to update the default config template
masih 94b301d
Bubble up tender mint for additional logging
masih 2a16f3c
Bubble up cosmos
masih 2dad465
Bubble up latest changes
masih e1b7c3d
Fix lint issues
masih 64de5f9
Add unit test for ante handler priority
yzang2019 4483db7
Revert "Add unit test for ante handler priority"
yzang2019 1fad1e0
Merge branch 'main' into masih/abci-get-tx-priority-root
masih eee44b6
Upgrade tendermint to commit from merge to main
masih bfe56d4
Merge branch 'main' into masih/abci-get-tx-priority-root
masih eaf9517
Port over the changes from open sei-cosmos PR
masih d2fccb3
Upgrate to the compatible tendermint across all dependencies
masih 00942ec
Update tendermint to concrete release tag
masih File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, ðtypes.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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.