From 058d8b300ea60c42c1928d8343e4b8a33d118ca9 Mon Sep 17 00:00:00 2001 From: codchen Date: Thu, 24 Nov 2022 12:51:03 +0800 Subject: [PATCH 1/4] Check if dex order provides enough gas --- x/dex/ante.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/x/dex/ante.go b/x/dex/ante.go index 8f8de372d2..fd3336f86f 100644 --- a/x/dex/ante.go +++ b/x/dex/ante.go @@ -89,3 +89,13 @@ func IsDecimalMultipleOf(a, b sdk.Dec) bool { quotient := sdk.NewDecFromBigInt(a.Quo(b).RoundInt().BigInt()) return quotient.Mul(b).Equal(a) } + +type CheckDexGasDecorator struct { + dexKeeper keeper.Keeper +} + +func NewCheckDexGasDecorator(dexKeeper keeper.Keeper) CheckDexGasDecorator { + return CheckDexGasDecorator{ + dexKeeper: dexKeeper, + } +} From 23699ea70fbabc462990d0a75fd3d0356414a430 Mon Sep 17 00:00:00 2001 From: codchen Date: Thu, 24 Nov 2022 16:30:56 +0800 Subject: [PATCH 2/4] Add ante handler to check if dex order/cancelleation provides enough fees --- app/ante.go | 1 + go.mod | 2 + go.sum | 3 + proto/dex/params.proto | 8 +++ x/dex/ante.go | 32 ++++++++++ x/dex/ante_test.go | 71 +++++++++++++++++++++ x/dex/types/params.go | 25 ++++---- x/dex/types/params.pb.go | 131 +++++++++++++++++++++++++++++++-------- 8 files changed, 235 insertions(+), 38 deletions(-) diff --git a/app/ante.go b/app/ante.go index b8fc6e2f80..8529ea2315 100644 --- a/app/ante.go +++ b/app/ante.go @@ -95,6 +95,7 @@ func NewAnteHandlerAndDepGenerator(options HandlerOptions) (sdk.AnteHandler, sdk sdk.CustomDepWrappedAnteDecorator(ante.NewIncrementSequenceDecorator(options.AccountKeeper), depdecorators.SignerDepDecorator{ReadOnly: false}), sdk.DefaultWrappedAnteDecorator(ibcante.NewAnteDecorator(options.IBCKeeper)), sdk.DefaultWrappedAnteDecorator(dex.NewTickSizeMultipleDecorator(*options.DexKeeper)), + sdk.DefaultWrappedAnteDecorator(dex.NewCheckDexGasDecorator(*options.DexKeeper)), } anteHandler, anteDepGenerator := sdk.ChainAnteDecorators(anteDecorators...) diff --git a/go.mod b/go.mod index e2e05f8440..c5f5eeb1e6 100644 --- a/go.mod +++ b/go.mod @@ -94,6 +94,7 @@ require ( github.com/firefart/nonamedreturns v1.0.1 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/fzipp/gocyclo v0.5.1 // indirect + github.com/ghodss/yaml v1.0.0 // indirect github.com/gin-gonic/gin v1.7.7 // indirect github.com/go-critic/go-critic v0.6.3 // indirect github.com/go-kit/kit v0.12.0 // indirect @@ -141,6 +142,7 @@ require ( github.com/gostaticanalysis/nilerr v0.1.1 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect diff --git a/go.sum b/go.sum index 711969871d..06d585a8b1 100644 --- a/go.sum +++ b/go.sum @@ -406,6 +406,7 @@ github.com/fullstorydev/grpcurl v1.6.0/go.mod h1:ZQ+ayqbKMJNhzLmbpCiurTVlaK2M/3n github.com/fzipp/gocyclo v0.5.1 h1:L66amyuYogbxl0j2U+vGqJXusPF2IkduvXLnYD5TFgw= github.com/fzipp/gocyclo v0.5.1/go.mod h1:rXPyn8fnlpa0R2csP/31uerbiVBugk5whMdlyaLkLoA= github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= @@ -679,6 +680,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0 h1:t7uX3JBHdVwAi3G7sSSdbsk8NfgA+LnUS88V/2EKaA0= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0/go.mod h1:4OGVnY4qf2+gw+ssiHbW+pq4mo2yko94YxxMmXZ7jCA= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= diff --git a/proto/dex/params.proto b/proto/dex/params.proto index 16702db522..f551ca7f2a 100644 --- a/proto/dex/params.proto +++ b/proto/dex/params.proto @@ -27,4 +27,12 @@ message Params { (gogoproto.jsontag) = "end_block_gas_limit", (gogoproto.moretags) = "yaml:\"end_block_gas_limit\"" ]; + uint64 default_gas_per_order = 5 [ + (gogoproto.jsontag) = "default_gas_per_order", + (gogoproto.moretags) = "yaml:\"default_gas_per_order\"" + ]; + uint64 default_gas_per_cancel = 6 [ + (gogoproto.jsontag) = "default_gas_per_cancel", + (gogoproto.moretags) = "yaml:\"default_gas_per_cancel\"" + ]; } diff --git a/x/dex/ante.go b/x/dex/ante.go index fd3336f86f..0d01f8340a 100644 --- a/x/dex/ante.go +++ b/x/dex/ante.go @@ -90,6 +90,8 @@ func IsDecimalMultipleOf(a, b sdk.Dec) bool { return quotient.Mul(b).Equal(a) } +const DexGasFeeUnit = "usei" + type CheckDexGasDecorator struct { dexKeeper keeper.Keeper } @@ -99,3 +101,33 @@ func NewCheckDexGasDecorator(dexKeeper keeper.Keeper) CheckDexGasDecorator { dexKeeper: dexKeeper, } } + +func (d CheckDexGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { + if ctx.IsReCheckTx() { + return next(ctx, tx, simulate) + } + params := d.dexKeeper.GetParams(ctx) + dexGasRequired := uint64(0) + for _, msg := range tx.GetMsgs() { + switch m := msg.(type) { + case *types.MsgPlaceOrders: + dexGasRequired += params.DefaultGasPerOrder * uint64(len(m.Orders)) + case *types.MsgCancelOrders: + dexGasRequired += params.DefaultGasPerCancel * uint64(len(m.Cancellations)) + } + } + if dexGasRequired == 0 { + return next(ctx, tx, simulate) + } + dexFeeRequired := sdk.NewDecWithPrec(int64(dexGasRequired), 0).Mul(params.SudoCallGasPrice).RoundInt() + feeTx, ok := tx.(sdk.FeeTx) + if !ok { + return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") + } + for _, fee := range feeTx.GetFee() { + if fee.Denom == DexGasFeeUnit && fee.Amount.GTE(dexFeeRequired) { + return next(ctx, tx, simulate) + } + } + return ctx, sdkerrors.ErrInsufficientFee +} diff --git a/x/dex/ante_test.go b/x/dex/ante_test.go index 02157e00d9..84d2668afb 100644 --- a/x/dex/ante_test.go +++ b/x/dex/ante_test.go @@ -4,10 +4,40 @@ import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" + keepertest "github.com/sei-protocol/sei-chain/testutil/keeper" "github.com/sei-protocol/sei-chain/x/dex" + "github.com/sei-protocol/sei-chain/x/dex/types" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) +type TestTx struct { + msgs []sdk.Msg + gas uint64 + fee sdk.Coins +} + +func (tx TestTx) GetMsgs() []sdk.Msg { + return tx.msgs +} + +func (tx TestTx) ValidateBasic() error { + return nil +} + +func (tx TestTx) GetGas() uint64 { + return tx.gas +} +func (tx TestTx) GetFee() sdk.Coins { + return tx.fee +} +func (tx TestTx) FeePayer() sdk.AccAddress { + return nil +} +func (tx TestTx) FeeGranter() sdk.AccAddress { + return nil +} + func TestIsDecimalMultipleOf(t *testing.T) { v1, _ := sdk.NewDecFromStr("2.4") v2, _ := sdk.NewDecFromStr("1.2") @@ -32,3 +62,44 @@ func TestIsDecimalMultipleOf(t *testing.T) { assert.True(t, dex.IsDecimalMultipleOf(v8, v6)) assert.True(t, dex.IsDecimalMultipleOf(v9, v10)) } + +func TestCheckDexGasDecorator(t *testing.T) { + keeper, ctx := keepertest.DexKeeper(t) + decorator := dex.NewCheckDexGasDecorator(*keeper) + terminator := func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { return ctx, nil } + tx := TestTx{ + msgs: []sdk.Msg{ + types.NewMsgPlaceOrders("someone", []*types.Order{{}, {}}, keepertest.TestContract, sdk.NewCoins()), + types.NewMsgCancelOrders("someone", []*types.Cancellation{{}, {}, {}}, keepertest.TestContract), + }, + fee: sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(3500))), + } + _, err := decorator.AnteHandle(ctx, tx, false, terminator) + require.Nil(t, err) + tx = TestTx{ + msgs: []sdk.Msg{ + types.NewMsgPlaceOrders("someone", []*types.Order{{}, {}}, keepertest.TestContract, sdk.NewCoins()), + types.NewMsgCancelOrders("someone", []*types.Cancellation{{}, {}, {}}, keepertest.TestContract), + }, + fee: sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(3499))), + } + _, err = decorator.AnteHandle(ctx, tx, false, terminator) + require.NotNil(t, err) + tx = TestTx{ + msgs: []sdk.Msg{ + types.NewMsgPlaceOrders("someone", []*types.Order{{}}, keepertest.TestContract, sdk.NewCoins()), + }, + } + _, err = decorator.AnteHandle(ctx, tx, false, terminator) + require.NotNil(t, err) + tx = TestTx{ + msgs: []sdk.Msg{}, + } + _, err = decorator.AnteHandle(ctx, tx, false, terminator) + require.Nil(t, err) + tx = TestTx{ + msgs: []sdk.Msg{types.NewMsgContractDepositRent(keepertest.TestContract, 10, keepertest.TestAccount)}, + } + _, err = decorator.AnteHandle(ctx, tx, false, terminator) + require.Nil(t, err) +} diff --git a/x/dex/types/params.go b/x/dex/types/params.go index ebebcf0525..4aafb903b3 100644 --- a/x/dex/types/params.go +++ b/x/dex/types/params.go @@ -13,15 +13,19 @@ var ( KeySudoCallGasPrice = []byte("KeySudoCallGasPrice") // gas price for sudo calls from endblock KeyBeginBlockGasLimit = []byte("KeyBeginBlockGasLimit") KeyEndBlockGasLimit = []byte("KeyEndBlockGasLimit") + KeyDefaultGasPerOrder = []byte("KeyDefaultGasPerOrder") + KeyDefaultGasPerCancel = []byte("KeyDefaultGasPerCancel") ) const ( DefaultPriceSnapshotRetention = 24 * 3600 // default to one day DefaultBeginBlockGasLimit = 200000000 // 200M DefaultEndBlockGasLimit = 1000000000 // 1B + DefaultDefaultGasPerOrder = 10000 + DefaultDefaultGasPerCancel = 5000 ) -var DefaultSudoCallGasPrice = sdk.ZeroDec() // 0 +var DefaultSudoCallGasPrice = sdk.NewDecWithPrec(1, 1) // 0.1 var _ paramtypes.ParamSet = (*Params)(nil) @@ -42,6 +46,8 @@ func DefaultParams() Params { SudoCallGasPrice: DefaultSudoCallGasPrice, BeginBlockGasLimit: DefaultBeginBlockGasLimit, EndBlockGasLimit: DefaultEndBlockGasLimit, + DefaultGasPerOrder: DefaultDefaultGasPerOrder, + DefaultGasPerCancel: DefaultDefaultGasPerCancel, } } @@ -50,8 +56,10 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { return paramtypes.ParamSetPairs{ paramtypes.NewParamSetPair(KeyPriceSnapshotRetention, &p.PriceSnapshotRetention, validatePriceSnapshotRetention), paramtypes.NewParamSetPair(KeySudoCallGasPrice, &p.SudoCallGasPrice, validateSudoCallGasPrice), - paramtypes.NewParamSetPair(KeyBeginBlockGasLimit, &p.BeginBlockGasLimit, validateBeginBlockGasLimit), - paramtypes.NewParamSetPair(KeyEndBlockGasLimit, &p.EndBlockGasLimit, validateEndBlockGasLimit), + paramtypes.NewParamSetPair(KeyBeginBlockGasLimit, &p.BeginBlockGasLimit, validateUint64Param), + paramtypes.NewParamSetPair(KeyEndBlockGasLimit, &p.EndBlockGasLimit, validateUint64Param), + paramtypes.NewParamSetPair(KeyDefaultGasPerOrder, &p.DefaultGasPerOrder, validateUint64Param), + paramtypes.NewParamSetPair(KeyDefaultGasPerCancel, &p.DefaultGasPerCancel, validateUint64Param), } } @@ -83,16 +91,7 @@ func validateSudoCallGasPrice(i interface{}) error { return nil } -func validateBeginBlockGasLimit(i interface{}) error { - _, ok := i.(uint64) - if !ok { - return fmt.Errorf("invalid parameter type: %T", i) - } - - return nil -} - -func validateEndBlockGasLimit(i interface{}) error { +func validateUint64Param(i interface{}) error { _, ok := i.(uint64) if !ok { return fmt.Errorf("invalid parameter type: %T", i) diff --git a/x/dex/types/params.pb.go b/x/dex/types/params.pb.go index cdc006aca4..b80f9bfd52 100644 --- a/x/dex/types/params.pb.go +++ b/x/dex/types/params.pb.go @@ -30,6 +30,8 @@ type Params struct { SudoCallGasPrice github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=sudo_call_gas_price,json=sudoCallGasPrice,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"sudo_call_gas_price"` BeginBlockGasLimit uint64 `protobuf:"varint,3,opt,name=begin_block_gas_limit,json=beginBlockGasLimit,proto3" json:"begin_block_gas_limit" yaml:"begin_block_gas_limit"` EndBlockGasLimit uint64 `protobuf:"varint,4,opt,name=end_block_gas_limit,json=endBlockGasLimit,proto3" json:"end_block_gas_limit" yaml:"end_block_gas_limit"` + DefaultGasPerOrder uint64 `protobuf:"varint,5,opt,name=default_gas_per_order,json=defaultGasPerOrder,proto3" json:"default_gas_per_order" yaml:"default_gas_per_order"` + DefaultGasPerCancel uint64 `protobuf:"varint,6,opt,name=default_gas_per_cancel,json=defaultGasPerCancel,proto3" json:"default_gas_per_cancel" yaml:"default_gas_per_cancel"` } func (m *Params) Reset() { *m = Params{} } @@ -85,6 +87,20 @@ func (m *Params) GetEndBlockGasLimit() uint64 { return 0 } +func (m *Params) GetDefaultGasPerOrder() uint64 { + if m != nil { + return m.DefaultGasPerOrder + } + return 0 +} + +func (m *Params) GetDefaultGasPerCancel() uint64 { + if m != nil { + return m.DefaultGasPerCancel + } + return 0 +} + func init() { proto.RegisterType((*Params)(nil), "seiprotocol.seichain.dex.Params") } @@ -92,31 +108,36 @@ func init() { func init() { proto.RegisterFile("dex/params.proto", fileDescriptor_e49286500ccff43e) } var fileDescriptor_e49286500ccff43e = []byte{ - // 381 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x92, 0x31, 0x6b, 0xfa, 0x40, - 0x18, 0xc6, 0x93, 0xbf, 0x22, 0xff, 0x66, 0x92, 0xd8, 0x96, 0x20, 0x25, 0x27, 0x19, 0x8a, 0x8b, - 0xc9, 0x50, 0x3a, 0xd4, 0xa5, 0x90, 0xb6, 0xb8, 0x74, 0x90, 0x74, 0xeb, 0x12, 0x2e, 0x97, 0x23, - 0x1e, 0x5e, 0x72, 0x21, 0x77, 0x82, 0x7e, 0x8b, 0x8e, 0x1d, 0xfd, 0x38, 0x8e, 0x6e, 0x2d, 0x1d, - 0x8e, 0xa2, 0x4b, 0x71, 0xf4, 0x13, 0x94, 0x9c, 0x0a, 0x2d, 0x8d, 0x53, 0xde, 0x3c, 0xbf, 0x87, - 0xf7, 0xb9, 0xf7, 0xe5, 0x35, 0x9a, 0x31, 0x9e, 0x7a, 0x39, 0x2c, 0x60, 0xca, 0xdd, 0xbc, 0x60, - 0x82, 0x99, 0x16, 0xc7, 0x44, 0x55, 0x88, 0x51, 0x97, 0x63, 0x82, 0x46, 0x90, 0x64, 0x6e, 0x8c, - 0xa7, 0xed, 0xd3, 0x84, 0x25, 0x4c, 0x21, 0xaf, 0xac, 0x76, 0x7e, 0xe7, 0xad, 0x66, 0x34, 0x86, - 0xaa, 0x81, 0x39, 0x33, 0xac, 0xbc, 0x20, 0x08, 0x87, 0x3c, 0x83, 0x39, 0x1f, 0x31, 0x11, 0x16, - 0x58, 0xe0, 0x4c, 0x10, 0x96, 0x59, 0x7a, 0x47, 0xef, 0xd6, 0xfd, 0xdb, 0x8d, 0x04, 0x47, 0x3d, - 0x5b, 0x09, 0xc0, 0x0c, 0xa6, 0xb4, 0xef, 0x1c, 0x73, 0x38, 0xc1, 0xb9, 0x42, 0x4f, 0x7b, 0x12, - 0x1c, 0x80, 0x29, 0x8c, 0x16, 0x9f, 0xc4, 0x2c, 0x44, 0x90, 0xd2, 0x30, 0x81, 0x3c, 0x54, 0x3e, - 0xeb, 0x5f, 0x47, 0xef, 0x9e, 0xf8, 0x0f, 0x0b, 0x09, 0xb4, 0x0f, 0x09, 0x2e, 0x13, 0x22, 0x46, - 0x93, 0xc8, 0x45, 0x2c, 0xf5, 0x10, 0xe3, 0x29, 0xe3, 0xfb, 0x4f, 0x8f, 0xc7, 0x63, 0x4f, 0xcc, - 0x72, 0xcc, 0xdd, 0x7b, 0x8c, 0x36, 0x12, 0x54, 0x35, 0x0b, 0x9a, 0xa5, 0x78, 0x07, 0x29, 0x1d, - 0x40, 0x3e, 0x2c, 0x15, 0x93, 0x1a, 0x67, 0x11, 0x4e, 0x48, 0x16, 0x46, 0x94, 0xa1, 0xb1, 0xb2, - 0x52, 0x92, 0x12, 0x61, 0xd5, 0xd4, 0xb4, 0x37, 0x1b, 0x09, 0xaa, 0x0d, 0x5b, 0x09, 0x2e, 0x76, - 0xa3, 0x56, 0x62, 0x27, 0x30, 0x95, 0xee, 0x97, 0xf2, 0x00, 0xf2, 0xc7, 0x52, 0x34, 0x63, 0xa3, - 0x85, 0xb3, 0xf8, 0x4f, 0x56, 0x5d, 0x65, 0x5d, 0x97, 0xaf, 0xae, 0xc0, 0x5b, 0x09, 0xda, 0xbb, - 0xa4, 0x0a, 0xe8, 0x04, 0x4d, 0x9c, 0xc5, 0xbf, 0x52, 0xfa, 0xff, 0x5f, 0xe7, 0x40, 0xfb, 0x9a, - 0x03, 0xdd, 0x1f, 0x2c, 0x56, 0xb6, 0xbe, 0x5c, 0xd9, 0xfa, 0xe7, 0xca, 0xd6, 0x5f, 0xd6, 0xb6, - 0xb6, 0x5c, 0xdb, 0xda, 0xfb, 0xda, 0xd6, 0x9e, 0x7b, 0x3f, 0x16, 0xc9, 0x31, 0xe9, 0x1d, 0xee, - 0x45, 0xfd, 0xa8, 0x83, 0xf1, 0xa6, 0x5e, 0x79, 0x59, 0x6a, 0xa7, 0x51, 0x43, 0xf1, 0xab, 0xef, - 0x00, 0x00, 0x00, 0xff, 0xff, 0xda, 0xfb, 0x04, 0x6b, 0x6d, 0x02, 0x00, 0x00, + // 453 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x93, 0xb1, 0x6e, 0xd3, 0x40, + 0x1c, 0xc6, 0x6d, 0x68, 0x23, 0xf0, 0x14, 0x39, 0xb4, 0xb2, 0x2a, 0xf0, 0x55, 0x1e, 0x50, 0x97, + 0xd8, 0x03, 0x62, 0xa0, 0x0c, 0x48, 0x29, 0x28, 0x0b, 0x12, 0x95, 0xd9, 0x58, 0xac, 0xcb, 0xdd, + 0x1f, 0xe7, 0xd4, 0xb3, 0xcf, 0xf2, 0x5d, 0xa4, 0xe4, 0x2d, 0x18, 0x19, 0xfb, 0x16, 0xbc, 0x42, + 0xc7, 0x8e, 0x88, 0xe1, 0x84, 0x92, 0x05, 0x65, 0xcc, 0x13, 0xa0, 0x3b, 0xa7, 0x2a, 0xa1, 0x97, + 0xc9, 0xe7, 0xef, 0xf7, 0xe9, 0x3e, 0x7f, 0xd6, 0xff, 0x1f, 0xf4, 0x29, 0xcc, 0xb3, 0x06, 0xb7, + 0xb8, 0x92, 0x69, 0xd3, 0x0a, 0x25, 0xc2, 0x48, 0x02, 0xb3, 0x27, 0x22, 0x78, 0x2a, 0x81, 0x91, + 0x29, 0x66, 0x75, 0x4a, 0x61, 0x7e, 0xf2, 0xac, 0x14, 0xa5, 0xb0, 0x28, 0x33, 0xa7, 0xce, 0x9f, + 0xfc, 0x38, 0x0c, 0x7a, 0x97, 0xf6, 0x82, 0x70, 0x11, 0x44, 0x4d, 0xcb, 0x08, 0x14, 0xb2, 0xc6, + 0x8d, 0x9c, 0x0a, 0x55, 0xb4, 0xa0, 0xa0, 0x56, 0x4c, 0xd4, 0x91, 0x7f, 0xea, 0x9f, 0x1d, 0x8c, + 0xde, 0xad, 0x35, 0xda, 0xeb, 0xd9, 0x68, 0x84, 0x16, 0xb8, 0xe2, 0xe7, 0xc9, 0x3e, 0x47, 0x92, + 0x1f, 0x5b, 0xf4, 0x79, 0x4b, 0xf2, 0x3b, 0x10, 0xaa, 0x60, 0x20, 0x67, 0x54, 0x14, 0x04, 0x73, + 0x5e, 0x94, 0x58, 0x16, 0xd6, 0x17, 0x3d, 0x3a, 0xf5, 0xcf, 0x9e, 0x8e, 0x3e, 0xdc, 0x68, 0xe4, + 0xfd, 0xd2, 0xe8, 0x65, 0xc9, 0xd4, 0x74, 0x36, 0x49, 0x89, 0xa8, 0x32, 0x22, 0x64, 0x25, 0xe4, + 0xf6, 0x31, 0x94, 0xf4, 0x2a, 0x53, 0x8b, 0x06, 0x64, 0xfa, 0x1e, 0xc8, 0x5a, 0x23, 0xd7, 0x65, + 0x79, 0xdf, 0x88, 0x17, 0x98, 0xf3, 0x31, 0x96, 0x97, 0x46, 0x09, 0x79, 0x70, 0x34, 0x81, 0x92, + 0xd5, 0xc5, 0x84, 0x0b, 0x72, 0x65, 0xad, 0x9c, 0x55, 0x4c, 0x45, 0x8f, 0x6d, 0xdb, 0x37, 0x6b, + 0x8d, 0xdc, 0x86, 0x8d, 0x46, 0xcf, 0xbb, 0xaa, 0x4e, 0x9c, 0xe4, 0xa1, 0xd5, 0x47, 0x46, 0x1e, + 0x63, 0xf9, 0xd1, 0x88, 0x21, 0x0d, 0x06, 0x50, 0xd3, 0x07, 0x59, 0x07, 0x36, 0xeb, 0xb5, 0xf9, + 0x6a, 0x07, 0xde, 0x68, 0x74, 0xd2, 0x25, 0x39, 0x60, 0x92, 0xf7, 0xa1, 0xa6, 0xbb, 0x29, 0x3c, + 0x38, 0xa2, 0xf0, 0x15, 0xcf, 0xb8, 0xea, 0xaa, 0x43, 0x5b, 0x88, 0x96, 0x42, 0x1b, 0x1d, 0xde, + 0x77, 0x72, 0x1a, 0xee, 0x3b, 0x39, 0x71, 0x92, 0x87, 0x5b, 0xdd, 0xfc, 0x3e, 0x68, 0x3f, 0x19, + 0x31, 0x6c, 0x82, 0xe3, 0xff, 0xdd, 0x04, 0xd7, 0x04, 0x78, 0xd4, 0xb3, 0x71, 0x6f, 0xd7, 0x1a, + 0xed, 0x71, 0x6c, 0x34, 0x7a, 0xe1, 0xce, 0xeb, 0x78, 0x92, 0x0f, 0x76, 0x02, 0x2f, 0xac, 0x7a, + 0xfe, 0xe4, 0xfb, 0x35, 0xf2, 0xfe, 0x5c, 0x23, 0x7f, 0x34, 0xbe, 0x59, 0xc6, 0xfe, 0xed, 0x32, + 0xf6, 0x7f, 0x2f, 0x63, 0xff, 0xdb, 0x2a, 0xf6, 0x6e, 0x57, 0xb1, 0xf7, 0x73, 0x15, 0x7b, 0x5f, + 0x86, 0xff, 0x0c, 0x8a, 0x04, 0x36, 0xbc, 0xdb, 0x07, 0xfb, 0x62, 0x17, 0x22, 0x9b, 0x67, 0x66, + 0x73, 0xec, 0xcc, 0x4c, 0x7a, 0x96, 0xbf, 0xfa, 0x1b, 0x00, 0x00, 0xff, 0xff, 0x86, 0xb4, 0xd3, + 0x0e, 0x4d, 0x03, 0x00, 0x00, } func (this *Params) Equal(that interface{}) bool { @@ -150,6 +171,12 @@ func (this *Params) Equal(that interface{}) bool { if this.EndBlockGasLimit != that1.EndBlockGasLimit { return false } + if this.DefaultGasPerOrder != that1.DefaultGasPerOrder { + return false + } + if this.DefaultGasPerCancel != that1.DefaultGasPerCancel { + return false + } return true } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -172,6 +199,16 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.DefaultGasPerCancel != 0 { + i = encodeVarintParams(dAtA, i, uint64(m.DefaultGasPerCancel)) + i-- + dAtA[i] = 0x30 + } + if m.DefaultGasPerOrder != 0 { + i = encodeVarintParams(dAtA, i, uint64(m.DefaultGasPerOrder)) + i-- + dAtA[i] = 0x28 + } if m.EndBlockGasLimit != 0 { i = encodeVarintParams(dAtA, i, uint64(m.EndBlockGasLimit)) i-- @@ -228,6 +265,12 @@ func (m *Params) Size() (n int) { if m.EndBlockGasLimit != 0 { n += 1 + sovParams(uint64(m.EndBlockGasLimit)) } + if m.DefaultGasPerOrder != 0 { + n += 1 + sovParams(uint64(m.DefaultGasPerOrder)) + } + if m.DefaultGasPerCancel != 0 { + n += 1 + sovParams(uint64(m.DefaultGasPerCancel)) + } return n } @@ -357,6 +400,44 @@ func (m *Params) Unmarshal(dAtA []byte) error { break } } + case 5: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field DefaultGasPerOrder", wireType) + } + m.DefaultGasPerOrder = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.DefaultGasPerOrder |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 6: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field DefaultGasPerCancel", wireType) + } + m.DefaultGasPerCancel = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.DefaultGasPerCancel |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:]) From 45aa2e01d36186af1bfe6b9e319c0969546bf7a3 Mon Sep 17 00:00:00 2001 From: codchen Date: Thu, 24 Nov 2022 20:47:01 +0800 Subject: [PATCH 3/4] add logics to charge contract rent only user gas cannot cover --- x/dex/contract/abci.go | 2 +- x/dex/contract/settlement.go | 2 +- x/dex/keeper/abci/begin_block_new_block.go | 2 +- x/dex/keeper/abci/end_block_cancel_orders.go | 3 ++- x/dex/keeper/abci/end_block_deposit.go | 2 +- x/dex/keeper/abci/end_block_place_orders.go | 3 ++- x/dex/keeper/contract.go | 7 ++++++- x/dex/keeper/contract_test.go | 21 ++++++++++++++++++-- x/dex/keeper/utils/wasm.go | 4 ++-- 9 files changed, 35 insertions(+), 11 deletions(-) diff --git a/x/dex/contract/abci.go b/x/dex/contract/abci.go index 700d7ec2ef..f5ae558390 100644 --- a/x/dex/contract/abci.go +++ b/x/dex/contract/abci.go @@ -179,7 +179,7 @@ func handleFinalizedBlocks(ctx context.Context, sdkCtx sdk.Context, env *environ if !contractsNeedHook.Contains(contractAddr) { return true } - if _, err := dexkeeperutils.CallContractSudo(sdkCtx, keeper, contractAddr, finalizeBlockMsg); err != nil { + if _, err := dexkeeperutils.CallContractSudo(sdkCtx, keeper, contractAddr, finalizeBlockMsg, 0); err != nil { sdkCtx.Logger().Error(fmt.Sprintf("Error calling FinalizeBlock of %s", contractAddr)) env.failedContractAddresses.Add(contractAddr) } diff --git a/x/dex/contract/settlement.go b/x/dex/contract/settlement.go index 8ada2b32fb..d07263f2e3 100644 --- a/x/dex/contract/settlement.go +++ b/x/dex/contract/settlement.go @@ -30,7 +30,7 @@ func callSettlementHook( Entries: settlementEntries, }, } - if _, err := dexkeeperutils.CallContractSudo(ctx, dexkeeper, contractAddr, nativeSettlementMsg); err != nil { + if _, err := dexkeeperutils.CallContractSudo(ctx, dexkeeper, contractAddr, nativeSettlementMsg, 0); err != nil { return err } return nil diff --git a/x/dex/keeper/abci/begin_block_new_block.go b/x/dex/keeper/abci/begin_block_new_block.go index 75160c4fb3..b03f21279d 100644 --- a/x/dex/keeper/abci/begin_block_new_block.go +++ b/x/dex/keeper/abci/begin_block_new_block.go @@ -10,6 +10,6 @@ func (w KeeperWrapper) HandleBBNewBlock(sdkCtx sdk.Context, contractAddr string, msg := wasm.SudoNewBlockMsg{ NewBlock: wasm.NewBlockRequest{Epoch: epoch}, } - _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg) + _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, 0) return err } diff --git a/x/dex/keeper/abci/end_block_cancel_orders.go b/x/dex/keeper/abci/end_block_cancel_orders.go index 937a2b8c8f..8d92bd295a 100644 --- a/x/dex/keeper/abci/end_block_cancel_orders.go +++ b/x/dex/keeper/abci/end_block_cancel_orders.go @@ -20,7 +20,8 @@ func (w KeeperWrapper) HandleEBCancelOrders(ctx context.Context, sdkCtx sdk.Cont typedContractAddr := typesutils.ContractAddress(contractAddr) msg := w.getCancelSudoMsg(sdkCtx, typedContractAddr, registeredPairs) - if _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg); err != nil { + userProvidedGas := w.GetParams(sdkCtx).DefaultGasPerCancel * uint64(len(msg.OrderCancellations.IdsToCancel)) + if _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, userProvidedGas); err != nil { sdkCtx.Logger().Error(fmt.Sprintf("Error during cancellation: %s", err.Error())) return err } diff --git a/x/dex/keeper/abci/end_block_deposit.go b/x/dex/keeper/abci/end_block_deposit.go index 941faf86e0..9506ec267d 100644 --- a/x/dex/keeper/abci/end_block_deposit.go +++ b/x/dex/keeper/abci/end_block_deposit.go @@ -21,7 +21,7 @@ func (w KeeperWrapper) HandleEBDeposit(ctx context.Context, sdkCtx sdk.Context, typedContractAddr := typesutils.ContractAddress(contractAddr) msg := w.GetDepositSudoMsg(sdkCtx, typedContractAddr) - _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg) // deposit + _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, 0) // deposit if err != nil { sdkCtx.Logger().Error(fmt.Sprintf("Error during deposit: %s", err.Error())) return err diff --git a/x/dex/keeper/abci/end_block_place_orders.go b/x/dex/keeper/abci/end_block_place_orders.go index 9ba6426ad9..5be417cc86 100644 --- a/x/dex/keeper/abci/end_block_place_orders.go +++ b/x/dex/keeper/abci/end_block_place_orders.go @@ -30,7 +30,8 @@ func (w KeeperWrapper) HandleEBPlaceOrders(ctx context.Context, sdkCtx sdk.Conte responses := []wasm.SudoOrderPlacementResponse{} for _, msg := range msgs { - data, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg) + userProvidedGas := w.GetParams(sdkCtx).DefaultGasPerOrder * uint64(len(msg.OrderPlacements.Orders)) + data, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, userProvidedGas) if err != nil { sdkCtx.Logger().Error(fmt.Sprintf("Error during order placement: %s", err.Error())) return err diff --git a/x/dex/keeper/contract.go b/x/dex/keeper/contract.go index 86862e0c0d..b6118451c6 100644 --- a/x/dex/keeper/contract.go +++ b/x/dex/keeper/contract.go @@ -67,7 +67,12 @@ func (k Keeper) GetAllContractInfo(ctx sdk.Context) []types.ContractInfoV2 { return list } -func (k Keeper) ChargeRentForGas(ctx sdk.Context, contractAddr string, gasUsed uint64) error { +func (k Keeper) ChargeRentForGas(ctx sdk.Context, contractAddr string, gasUsed uint64, userProvidedGas uint64) error { + if gasUsed <= userProvidedGas { + // User provided can fully cover the consumed gas. Doing nothing + return nil + } + gasUsed -= userProvidedGas contract, err := k.GetContract(ctx, contractAddr) if err != nil { return err diff --git a/x/dex/keeper/contract_test.go b/x/dex/keeper/contract_test.go index 1bb579ebed..0412da04c4 100644 --- a/x/dex/keeper/contract_test.go +++ b/x/dex/keeper/contract_test.go @@ -19,16 +19,33 @@ func TestChargeRentForGas(t *testing.T) { RentBalance: 1000000, }) require.Nil(t, err) - err = keeper.ChargeRentForGas(ctx, keepertest.TestContract, 5000000) + err = keeper.ChargeRentForGas(ctx, keepertest.TestContract, 5000000, 0) require.Nil(t, err) contract, err := keeper.GetContract(ctx, keepertest.TestContract) require.Nil(t, err) require.Equal(t, uint64(500000), contract.RentBalance) - err = keeper.ChargeRentForGas(ctx, keepertest.TestContract, 6000000) + err = keeper.ChargeRentForGas(ctx, keepertest.TestContract, 6000000, 0) require.NotNil(t, err) contract, err = keeper.GetContract(ctx, keepertest.TestContract) require.Nil(t, err) require.Equal(t, uint64(0), contract.RentBalance) + err = keeper.SetContract(ctx, &types.ContractInfoV2{ + Creator: keepertest.TestAccount, + ContractAddr: keepertest.TestContract, + CodeId: 1, + RentBalance: 1000000, + }) + require.Nil(t, err) + err = keeper.ChargeRentForGas(ctx, keepertest.TestContract, 5000000, 4000000) + require.Nil(t, err) + contract, err = keeper.GetContract(ctx, keepertest.TestContract) + require.Nil(t, err) + require.Equal(t, uint64(900000), contract.RentBalance) + err = keeper.ChargeRentForGas(ctx, keepertest.TestContract, 5000000, 6000000) + require.Nil(t, err) + contract, err = keeper.GetContract(ctx, keepertest.TestContract) + require.Nil(t, err) + require.Equal(t, uint64(900000), contract.RentBalance) // delete contract keeper.DeleteContract(ctx, keepertest.TestContract) diff --git a/x/dex/keeper/utils/wasm.go b/x/dex/keeper/utils/wasm.go index 7b77487006..dd776fb32b 100644 --- a/x/dex/keeper/utils/wasm.go +++ b/x/dex/keeper/utils/wasm.go @@ -58,7 +58,7 @@ func hasErrInstantiatingWasmModuleDueToCPUFeature(err error) bool { return strings.Contains(err.Error(), ErrWasmModuleInstCPUFeatureLiteral) } -func CallContractSudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddr string, msg interface{}) ([]byte, error) { +func CallContractSudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddr string, msg interface{}, userProvidedGas uint64) ([]byte, error) { contractAddress, err := sdk.AccAddressFromBech32(contractAddr) if err != nil { sdkCtx.Logger().Error(err.Error()) @@ -76,7 +76,7 @@ func CallContractSudo(sdkCtx sdk.Context, k *keeper.Keeper, contractAddr string, sdkCtx.Logger().Error(err.Error()) return []byte{}, err } - if err := k.ChargeRentForGas(sdkCtx, contractAddr, gasUsed); err != nil { + if err := k.ChargeRentForGas(sdkCtx, contractAddr, gasUsed, userProvidedGas); err != nil { metrics.IncrementSudoFailCount(msgType) sdkCtx.Logger().Error(err.Error()) return []byte{}, err From b7a4df7a10884dbfce842e76cc7a3435e34d6259 Mon Sep 17 00:00:00 2001 From: codchen Date: Sat, 26 Nov 2022 00:18:22 +0800 Subject: [PATCH 4/4] fix tests --- app/app.go | 2 +- go.mod | 2 -- go.sum | 3 --- x/dex/ante.go | 1 + x/dex/contract/abci.go | 2 +- x/dex/contract/settlement.go | 3 ++- x/dex/keeper/abci/begin_block_new_block.go | 3 ++- x/dex/keeper/abci/end_block_deposit.go | 2 +- x/dex/keeper/contract.go | 1 + x/dex/utils/gas.go | 2 ++ 10 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/app.go b/app/app.go index 1351ca93ed..1b7c7bdd83 100644 --- a/app/app.go +++ b/app/app.go @@ -718,6 +718,7 @@ func New( vestingtypes.ModuleName, crisistypes.ModuleName, ibchost.ModuleName, + dexmoduletypes.ModuleName, genutiltypes.ModuleName, evidencetypes.ModuleName, ibctransfertypes.ModuleName, @@ -725,7 +726,6 @@ func New( oracletypes.ModuleName, tokenfactorytypes.ModuleName, epochmoduletypes.ModuleName, - dexmoduletypes.ModuleName, nitrotypes.ModuleName, wasm.ModuleName, acltypes.ModuleName, diff --git a/go.mod b/go.mod index c5f5eeb1e6..e2e05f8440 100644 --- a/go.mod +++ b/go.mod @@ -94,7 +94,6 @@ require ( github.com/firefart/nonamedreturns v1.0.1 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/fzipp/gocyclo v0.5.1 // indirect - github.com/ghodss/yaml v1.0.0 // indirect github.com/gin-gonic/gin v1.7.7 // indirect github.com/go-critic/go-critic v0.6.3 // indirect github.com/go-kit/kit v0.12.0 // indirect @@ -142,7 +141,6 @@ require ( github.com/gostaticanalysis/nilerr v0.1.1 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect diff --git a/go.sum b/go.sum index 06d585a8b1..711969871d 100644 --- a/go.sum +++ b/go.sum @@ -406,7 +406,6 @@ github.com/fullstorydev/grpcurl v1.6.0/go.mod h1:ZQ+ayqbKMJNhzLmbpCiurTVlaK2M/3n github.com/fzipp/gocyclo v0.5.1 h1:L66amyuYogbxl0j2U+vGqJXusPF2IkduvXLnYD5TFgw= github.com/fzipp/gocyclo v0.5.1/go.mod h1:rXPyn8fnlpa0R2csP/31uerbiVBugk5whMdlyaLkLoA= github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= -github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= @@ -680,8 +679,6 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0 h1:t7uX3JBHdVwAi3G7sSSdbsk8NfgA+LnUS88V/2EKaA0= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0/go.mod h1:4OGVnY4qf2+gw+ssiHbW+pq4mo2yko94YxxMmXZ7jCA= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= diff --git a/x/dex/ante.go b/x/dex/ante.go index 0d01f8340a..8ff40e3cb9 100644 --- a/x/dex/ante.go +++ b/x/dex/ante.go @@ -102,6 +102,7 @@ func NewCheckDexGasDecorator(dexKeeper keeper.Keeper) CheckDexGasDecorator { } } +// for a TX that contains dex gas-incurring messages, check if it provides enough gas based on dex params func (d CheckDexGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { if ctx.IsReCheckTx() { return next(ctx, tx, simulate) diff --git a/x/dex/contract/abci.go b/x/dex/contract/abci.go index f5ae558390..d547f269ff 100644 --- a/x/dex/contract/abci.go +++ b/x/dex/contract/abci.go @@ -179,7 +179,7 @@ func handleFinalizedBlocks(ctx context.Context, sdkCtx sdk.Context, env *environ if !contractsNeedHook.Contains(contractAddr) { return true } - if _, err := dexkeeperutils.CallContractSudo(sdkCtx, keeper, contractAddr, finalizeBlockMsg, 0); err != nil { + if _, err := dexkeeperutils.CallContractSudo(sdkCtx, keeper, contractAddr, finalizeBlockMsg, dexutils.ZeroUserProvidedGas); err != nil { sdkCtx.Logger().Error(fmt.Sprintf("Error calling FinalizeBlock of %s", contractAddr)) env.failedContractAddresses.Add(contractAddr) } diff --git a/x/dex/contract/settlement.go b/x/dex/contract/settlement.go index d07263f2e3..fdf092b6cf 100644 --- a/x/dex/contract/settlement.go +++ b/x/dex/contract/settlement.go @@ -6,6 +6,7 @@ import ( dexkeeperutils "github.com/sei-protocol/sei-chain/x/dex/keeper/utils" "github.com/sei-protocol/sei-chain/x/dex/types" dextypeswasm "github.com/sei-protocol/sei-chain/x/dex/types/wasm" + dexutils "github.com/sei-protocol/sei-chain/x/dex/utils" ) func HandleSettlements( @@ -30,7 +31,7 @@ func callSettlementHook( Entries: settlementEntries, }, } - if _, err := dexkeeperutils.CallContractSudo(ctx, dexkeeper, contractAddr, nativeSettlementMsg, 0); err != nil { + if _, err := dexkeeperutils.CallContractSudo(ctx, dexkeeper, contractAddr, nativeSettlementMsg, dexutils.ZeroUserProvidedGas); err != nil { return err } return nil diff --git a/x/dex/keeper/abci/begin_block_new_block.go b/x/dex/keeper/abci/begin_block_new_block.go index b03f21279d..4295ff1833 100644 --- a/x/dex/keeper/abci/begin_block_new_block.go +++ b/x/dex/keeper/abci/begin_block_new_block.go @@ -4,12 +4,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/sei-protocol/sei-chain/x/dex/keeper/utils" "github.com/sei-protocol/sei-chain/x/dex/types/wasm" + dexutils "github.com/sei-protocol/sei-chain/x/dex/utils" ) func (w KeeperWrapper) HandleBBNewBlock(sdkCtx sdk.Context, contractAddr string, epoch int64) error { msg := wasm.SudoNewBlockMsg{ NewBlock: wasm.NewBlockRequest{Epoch: epoch}, } - _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, 0) + _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, dexutils.ZeroUserProvidedGas) return err } diff --git a/x/dex/keeper/abci/end_block_deposit.go b/x/dex/keeper/abci/end_block_deposit.go index 9506ec267d..d26c621204 100644 --- a/x/dex/keeper/abci/end_block_deposit.go +++ b/x/dex/keeper/abci/end_block_deposit.go @@ -21,7 +21,7 @@ func (w KeeperWrapper) HandleEBDeposit(ctx context.Context, sdkCtx sdk.Context, typedContractAddr := typesutils.ContractAddress(contractAddr) msg := w.GetDepositSudoMsg(sdkCtx, typedContractAddr) - _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, 0) // deposit + _, err := utils.CallContractSudo(sdkCtx, w.Keeper, contractAddr, msg, dexutils.ZeroUserProvidedGas) // deposit if err != nil { sdkCtx.Logger().Error(fmt.Sprintf("Error during deposit: %s", err.Error())) return err diff --git a/x/dex/keeper/contract.go b/x/dex/keeper/contract.go index b6118451c6..727dcf5653 100644 --- a/x/dex/keeper/contract.go +++ b/x/dex/keeper/contract.go @@ -67,6 +67,7 @@ func (k Keeper) GetAllContractInfo(ctx sdk.Context) []types.ContractInfoV2 { return list } +// Reduce `RentBalance` of a contract if `userProvidedGas` cannot cover `gasUsed` func (k Keeper) ChargeRentForGas(ctx sdk.Context, contractAddr string, gasUsed uint64, userProvidedGas uint64) error { if gasUsed <= userProvidedGas { // User provided can fully cover the consumed gas. Doing nothing diff --git a/x/dex/utils/gas.go b/x/dex/utils/gas.go index 3e420dc990..e66eba9a9e 100644 --- a/x/dex/utils/gas.go +++ b/x/dex/utils/gas.go @@ -4,6 +4,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +const ZeroUserProvidedGas = 0 + func GetGasMeterForLimit(limit uint64) sdk.GasMeter { if limit == 0 { return sdk.NewInfiniteGasMeter()