From e499b2463fa840c22e9d6c35fc03fe1d59b7fada Mon Sep 17 00:00:00 2001 From: lyh169 <44960676+lyh169@users.noreply.github.com> Date: Wed, 7 Jun 2023 12:03:34 +0800 Subject: [PATCH] Proposal adjust gasconfig (#3153) * add GasConfig in params * add GasConfig in params * modify some quote * add test and modify the comment * modify the comment * modify the comment about func --------- Co-authored-by: BananaLF <864685021@qq.com> Co-authored-by: KamiD <44460798+KamiD@users.noreply.github.com> --- app/app.go | 8 ++ libs/cosmos-sdk/baseapp/abci.go | 12 ++ libs/cosmos-sdk/baseapp/baseapp.go | 1 + libs/cosmos-sdk/baseapp/options.go | 7 ++ libs/cosmos-sdk/store/types/gas.go | 94 +++++++++++++--- libs/cosmos-sdk/types/handler.go | 3 + libs/cosmos-sdk/x/auth/exported/utils.go | 8 +- x/params/client/cli/query.go | 27 +++++ x/params/keeper.go | 14 +++ x/params/keeper_test.go | 137 +++++++++++++++++++++++ x/params/querier.go | 10 ++ x/params/types/params.go | 5 +- x/params/types/params_gas.go | 45 ++++++++ x/wasm/proxy/keeper_proxy.go | 5 +- 14 files changed, 351 insertions(+), 25 deletions(-) create mode 100644 x/params/keeper_test.go create mode 100644 x/params/types/params_gas.go diff --git a/app/app.go b/app/app.go index 9746e363f1..dc5c331724 100644 --- a/app/app.go +++ b/app/app.go @@ -50,6 +50,7 @@ import ( "github.com/okex/exchain/libs/cosmos-sdk/server" "github.com/okex/exchain/libs/cosmos-sdk/simapp" "github.com/okex/exchain/libs/cosmos-sdk/store/mpt" + stypes "github.com/okex/exchain/libs/cosmos-sdk/store/types" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" "github.com/okex/exchain/libs/cosmos-sdk/types/module" upgradetypes "github.com/okex/exchain/libs/cosmos-sdk/types/upgrade" @@ -766,6 +767,7 @@ func NewOKExChainApp( app.SetEvmSysContractAddressHandler(NewEvmSysContractAddressHandler(app.EvmKeeper)) app.SetEvmWatcherCollector(app.EvmKeeper.Watcher.Collect) app.SetUpdateCMTxNonceHandler(NewUpdateCMTxNonceHandler()) + app.SetGetGasConfigHandler(NewGetGasConfigHandler(app.ParamsKeeper)) if loadLatest { err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) @@ -1040,3 +1042,9 @@ func NewUpdateCMTxNonceHandler() sdk.UpdateCMTxNonceHandler { } } } + +func NewGetGasConfigHandler(pk params.Keeper) sdk.GetGasConfigHandler { + return func(ctx sdk.Context) *stypes.GasConfig { + return pk.GetGasConfig(ctx) + } +} diff --git a/libs/cosmos-sdk/baseapp/abci.go b/libs/cosmos-sdk/baseapp/abci.go index 3f3c29c9f6..0a3bfa5e80 100644 --- a/libs/cosmos-sdk/baseapp/abci.go +++ b/libs/cosmos-sdk/baseapp/abci.go @@ -14,6 +14,7 @@ import ( "github.com/okex/exchain/app/rpc/simulator" "github.com/okex/exchain/libs/cosmos-sdk/codec" "github.com/okex/exchain/libs/cosmos-sdk/store/mpt" + stypes "github.com/okex/exchain/libs/cosmos-sdk/store/types" "github.com/okex/exchain/libs/cosmos-sdk/types" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" sdkerrors "github.com/okex/exchain/libs/cosmos-sdk/types/errors" @@ -158,6 +159,10 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg gasMeter = sdk.NewInfiniteGasMeter() } + if app.getGasConfigHandler != nil { + app.UpdateGlobalGasConfig(app.deliverState.ctx) + } + app.deliverState.ctx.SetBlockGasMeter(gasMeter) if app.beginBlocker != nil { @@ -176,6 +181,13 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg return res } +func (app *BaseApp) UpdateGlobalGasConfig(ctx sdk.Context) { + if ctx.IsCheckTx() || ctx.IsTraceTx() { + return + } + stypes.UpdateGlobalGasConfig(app.getGasConfigHandler(ctx)) +} + func (app *BaseApp) updateFeeCollectorAccount(isEndBlock bool) { if app.updateFeeCollectorAccHandler == nil { return diff --git a/libs/cosmos-sdk/baseapp/baseapp.go b/libs/cosmos-sdk/baseapp/baseapp.go index 2343657d19..3aedeab6f9 100644 --- a/libs/cosmos-sdk/baseapp/baseapp.go +++ b/libs/cosmos-sdk/baseapp/baseapp.go @@ -158,6 +158,7 @@ type BaseApp struct { // nolint: maligned getTxFeeAndFromHandler sdk.GetTxFeeAndFromHandler getTxFeeHandler sdk.GetTxFeeHandler updateCMTxNonceHandler sdk.UpdateCMTxNonceHandler + getGasConfigHandler sdk.GetGasConfigHandler // volatile states: // diff --git a/libs/cosmos-sdk/baseapp/options.go b/libs/cosmos-sdk/baseapp/options.go index 6169cc32c1..88652faf4f 100644 --- a/libs/cosmos-sdk/baseapp/options.go +++ b/libs/cosmos-sdk/baseapp/options.go @@ -240,3 +240,10 @@ func (app *BaseApp) SetUpdateCMTxNonceHandler(handler sdk.UpdateCMTxNonceHandler } app.updateCMTxNonceHandler = handler } + +func (app *BaseApp) SetGetGasConfigHandler(handler sdk.GetGasConfigHandler) { + if app.sealed { + panic("SetGetGasConfigHandler() on sealed BaseApp") + } + app.getGasConfigHandler = handler +} diff --git a/libs/cosmos-sdk/store/types/gas.go b/libs/cosmos-sdk/store/types/gas.go index 882c2ec14d..38c4a7538c 100644 --- a/libs/cosmos-sdk/store/types/gas.go +++ b/libs/cosmos-sdk/store/types/gas.go @@ -2,6 +2,7 @@ package types import ( "math" + "sync" ) // Gas consumption descriptors. @@ -14,6 +15,27 @@ const ( GasReadCostFlatDesc = "ReadFlat" GasHasDesc = "Has" GasDeleteDesc = "Delete" + + defaultHasCost = 1000 + defaultDeleteCost = 1000 + defaultReadCostFlat = 1000 + defaultReadCostPerByte = 3 + defaultWriteCostFlat = 2000 + defaultWriteCostPerByte = 30 + defaultIterNextCostFlat = 30 +) + +var ( + gGasConfig = &GasConfig{ + HasCost: defaultHasCost, + DeleteCost: defaultDeleteCost, + ReadCostFlat: defaultReadCostFlat, + ReadCostPerByte: defaultReadCostPerByte, + WriteCostFlat: defaultWriteCostFlat, + WriteCostPerByte: defaultWriteCostPerByte, + IterNextCostFlat: defaultIterNextCostFlat, + } + mut = &sync.RWMutex{} ) // Gas measured by the SDK @@ -167,26 +189,18 @@ func (g *infiniteGasMeter) IsOutOfGas() bool { // GasConfig defines gas cost for each operation on KVStores type GasConfig struct { - HasCost Gas - DeleteCost Gas - ReadCostFlat Gas - ReadCostPerByte Gas - WriteCostFlat Gas - WriteCostPerByte Gas - IterNextCostFlat Gas + HasCost Gas `json:"hasCost"` + DeleteCost Gas `json:"deleteCost"` + ReadCostFlat Gas `json:"readCostFlat"` + ReadCostPerByte Gas `json:"readCostPerByte"` + WriteCostFlat Gas `json:"writeCostFlat"` + WriteCostPerByte Gas `json:"writeCostPerByte"` + IterNextCostFlat Gas `json:"iterNextCostFlat"` } // KVGasConfig returns a default gas config for KVStores. func KVGasConfig() GasConfig { - return GasConfig{ - HasCost: 1000, - DeleteCost: 1000, - ReadCostFlat: 1000, - ReadCostPerByte: 3, - WriteCostFlat: 2000, - WriteCostPerByte: 30, - IterNextCostFlat: 30, - } + return GetGlobalGasConfig() } // TransientGasConfig returns a default gas config for TransientStores. @@ -194,3 +208,51 @@ func TransientGasConfig() GasConfig { // TODO: define gasconfig for transient stores return KVGasConfig() } + +func UpdateGlobalGasConfig(gc *GasConfig) { + mut.Lock() + defer mut.Unlock() + gGasConfig = gc +} + +func AsDefaultGasConfig(gc *GasConfig) { + if gc.HasCost == 0 { + gc.HasCost = defaultHasCost + } + if gc.DeleteCost == 0 { + gc.DeleteCost = defaultDeleteCost + } + if gc.ReadCostFlat == 0 { + gc.ReadCostFlat = defaultReadCostFlat + } + if gc.ReadCostPerByte == 0 { + gc.ReadCostPerByte = defaultReadCostPerByte + } + if gc.WriteCostFlat == 0 { + gc.WriteCostFlat = defaultWriteCostFlat + } + if gc.WriteCostPerByte == 0 { + gc.WriteCostPerByte = defaultWriteCostPerByte + } + if gc.IterNextCostFlat == 0 { + gc.IterNextCostFlat = defaultIterNextCostFlat + } +} + +func GetGlobalGasConfig() GasConfig { + mut.RLock() + defer mut.RUnlock() + return *gGasConfig +} + +func GetDefaultGasConfig() *GasConfig { + return &GasConfig{ + HasCost: defaultHasCost, + DeleteCost: defaultDeleteCost, + ReadCostFlat: defaultReadCostFlat, + ReadCostPerByte: defaultReadCostPerByte, + WriteCostFlat: defaultWriteCostFlat, + WriteCostPerByte: defaultWriteCostPerByte, + IterNextCostFlat: defaultIterNextCostFlat, + } +} diff --git a/libs/cosmos-sdk/types/handler.go b/libs/cosmos-sdk/types/handler.go index 852ee6912b..07bcd80b28 100644 --- a/libs/cosmos-sdk/types/handler.go +++ b/libs/cosmos-sdk/types/handler.go @@ -3,6 +3,7 @@ package types import ( "github.com/ethereum/go-ethereum/common" + stypes "github.com/okex/exchain/libs/cosmos-sdk/store/types" abci "github.com/okex/exchain/libs/tendermint/abci/types" ) @@ -28,6 +29,8 @@ type UpdateCosmosTxCount func(ctx Context, txCount int) type GetFeeCollectorInfo func(ctx Context, onlyGetFeeCollectorStoreKey bool) (Coins, []byte) +type GetGasConfigHandler func(ctx Context) *stypes.GasConfig + type LogFix func(tx []Tx, logIndex []int, hasEnterEvmTx []bool, errs []error, resp []abci.ResponseDeliverTx) (logs [][]byte) type UpdateFeeSplitHandler func(txHash common.Hash, addr AccAddress, fee Coins, isDelete bool) type GetTxFeeAndFromHandler func(ctx Context, tx Tx) (Coins, bool, bool, string, string, error, bool) diff --git a/libs/cosmos-sdk/x/auth/exported/utils.go b/libs/cosmos-sdk/x/auth/exported/utils.go index a30578b1b5..2c704c1377 100644 --- a/libs/cosmos-sdk/x/auth/exported/utils.go +++ b/libs/cosmos-sdk/x/auth/exported/utils.go @@ -5,12 +5,6 @@ import ( sdk "github.com/okex/exchain/libs/cosmos-sdk/types" ) -var kvGasConfig storetypes.GasConfig - -func init() { - kvGasConfig = storetypes.KVGasConfig() -} - type SizerAccountKeeper interface { GetEncodedAccountSize(acc Account) int } @@ -27,6 +21,7 @@ func TryAddGetAccountGas(gasMeter sdk.GasMeter, ak SizerAccountKeeper, acc Accou if size == 0 { return false, 0 } + kvGasConfig := storetypes.KVGasConfig() gas := kvGasConfig.ReadCostFlat + storetypes.Gas(size)*kvGasConfig.ReadCostPerByte gasMeter.ConsumeGas(gas, "x/bank/internal/keeper/keeper.BaseSendKeeper") return true, gas @@ -40,6 +35,7 @@ func GetAccountGas(ak SizerAccountKeeper, acc Account) (sdk.Gas, bool) { if size == 0 { return 0, false } + kvGasConfig := storetypes.KVGasConfig() gas := kvGasConfig.ReadCostFlat + storetypes.Gas(size)*kvGasConfig.ReadCostPerByte return gas, true } diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index 02bde8d18e..15b92f56f3 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -22,6 +22,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { queryCmd.AddCommand(flags.GetCommands( GetCmdQueryParams(queryRoute, cdc), GetCmdQueryUpgrade(queryRoute, cdc), + GetCmdQueryGasConfig(queryRoute, cdc), )...) return queryCmd @@ -52,3 +53,29 @@ $ exchaincli query params params }, } } + +// GetCmdQueryParams implements the query params command. +func GetCmdQueryGasConfig(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "gasconfig", + Short: "Query parameters of gasconfig", + Long: strings.TrimSpace(`Query parameters of gasconfig: + +$ exchaincli query params gasconfig +`), + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, _ []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + + route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGasConfig) + bz, _, err := cliCtx.QueryWithData(route, nil) + if err != nil { + return err + } + + var params types.GasConfig + cdc.MustUnmarshalJSON(bz, ¶ms) + return cliCtx.PrintOutput(params.GasConfig) + }, + } +} diff --git a/x/params/keeper.go b/x/params/keeper.go index 5a414768a2..47a2ffa4e4 100644 --- a/x/params/keeper.go +++ b/x/params/keeper.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/okex/exchain/libs/cosmos-sdk/codec" + stypes "github.com/okex/exchain/libs/cosmos-sdk/store/types" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" sdkparams "github.com/okex/exchain/libs/cosmos-sdk/x/params" "github.com/okex/exchain/libs/tendermint/libs/log" @@ -71,3 +72,16 @@ func (keeper Keeper) GetParams(ctx sdk.Context) types.Params { keeper.paramSpace.GetParamSet(ctx, ¶ms) return params } + +func (keeper Keeper) GetGasConfig(ctx sdk.Context) *stypes.GasConfig { + params := keeper.getGasConfig(ctx) + return ¶ms.GasConfig +} + +func (keeper Keeper) getGasConfig(ctx sdk.Context) (params types.GasConfig) { + for _, pair := range params.ParamSetPairs() { + keeper.paramSpace.GetIfExists(ctx, pair.Key, pair.Value) + } + stypes.AsDefaultGasConfig(¶ms.GasConfig) + return +} diff --git a/x/params/keeper_test.go b/x/params/keeper_test.go new file mode 100644 index 0000000000..e7dbd08f68 --- /dev/null +++ b/x/params/keeper_test.go @@ -0,0 +1,137 @@ +package params + +import ( + "github.com/okex/exchain/x/params/types" + "testing" + + "github.com/okex/exchain/libs/cosmos-sdk/store" + storetypes "github.com/okex/exchain/libs/cosmos-sdk/store/types" + sdk "github.com/okex/exchain/libs/cosmos-sdk/types" + abci "github.com/okex/exchain/libs/tendermint/abci/types" + "github.com/okex/exchain/libs/tendermint/libs/log" + tmdb "github.com/okex/exchain/libs/tm-db" + "github.com/stretchr/testify/suite" +) + +type KeeperSuite struct { + suite.Suite + ms storetypes.CommitMultiStore + paramsKeeper Keeper +} + +func (suite *KeeperSuite) SetupTest() { + db := tmdb.NewMemDB() + storeKey := sdk.NewKVStoreKey(StoreKey) + tstoreKey := sdk.NewTransientStoreKey(TStoreKey) + + suite.ms = store.NewCommitMultiStore(tmdb.NewMemDB()) + suite.ms.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db) + suite.ms.MountStoreWithDB(tstoreKey, sdk.StoreTypeTransient, db) + err := suite.ms.LoadLatestVersion() + suite.NoError(err) + + suite.paramsKeeper = NewKeeper(ModuleCdc, storeKey, tstoreKey, log.NewNopLogger()) +} + +func (suite *KeeperSuite) Context(height int64) sdk.Context { + return sdk.NewContext(suite.ms, abci.Header{Height: height}, false, log.NewNopLogger()) +} + +func TestKeeper(t *testing.T) { + suite.Run(t, new(KeeperSuite)) +} + +func (suite *KeeperSuite) TestGetGasConfig() { + sub := "params" + tests := []struct { + changes []types.ParamChange + fncheck func(res storetypes.GasConfig) + }{ + { + changes: []types.ParamChange{{Subspace: sub, Key: storetypes.GasHasDesc, Value: "\"100\""}}, + fncheck: func(res storetypes.GasConfig) { + gs := storetypes.GetDefaultGasConfig() + gs.HasCost = 100 + suite.Equal(*gs, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: storetypes.GasDeleteDesc, Value: "\"10\""}}, + fncheck: func(res storetypes.GasConfig) { + gs := storetypes.GetDefaultGasConfig() + gs.HasCost = 100 + gs.DeleteCost = 10 + suite.Equal(*gs, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: storetypes.GasReadCostFlatDesc, Value: "\"10\""}}, + fncheck: func(res storetypes.GasConfig) { + gs := storetypes.GetDefaultGasConfig() + gs.HasCost = 100 + gs.DeleteCost = 10 + gs.ReadCostFlat = 10 + suite.Equal(*gs, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: storetypes.GasReadPerByteDesc, Value: "\"10\""}}, + fncheck: func(res storetypes.GasConfig) { + gs := storetypes.GetDefaultGasConfig() + gs.HasCost = 100 + gs.DeleteCost = 10 + gs.ReadCostFlat = 10 + gs.ReadCostPerByte = 10 + suite.Equal(*gs, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: storetypes.GasWriteCostFlatDesc, Value: "\"10\""}}, + fncheck: func(res storetypes.GasConfig) { + gs := storetypes.GetDefaultGasConfig() + gs.HasCost = 100 + gs.DeleteCost = 10 + gs.ReadCostFlat = 10 + gs.ReadCostPerByte = 10 + gs.WriteCostFlat = 10 + suite.Equal(*gs, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: storetypes.GasWritePerByteDesc, Value: "\"10\""}}, + fncheck: func(res storetypes.GasConfig) { + gs := storetypes.GetDefaultGasConfig() + gs.HasCost = 100 + gs.DeleteCost = 10 + gs.ReadCostFlat = 10 + gs.ReadCostPerByte = 10 + gs.WriteCostFlat = 10 + gs.WriteCostPerByte = 10 + suite.Equal(*gs, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: storetypes.GasIterNextCostFlatDesc, Value: "\"10\""}}, + fncheck: func(res storetypes.GasConfig) { + gs := storetypes.GetDefaultGasConfig() + gs.HasCost = 100 + gs.DeleteCost = 10 + gs.ReadCostFlat = 10 + gs.ReadCostPerByte = 10 + gs.WriteCostFlat = 10 + gs.WriteCostPerByte = 10 + gs.IterNextCostFlat = 10 + suite.Equal(*gs, res) + }, + }, + } + + for _, tt := range tests { + ctx := suite.Context(0) + + changeParams(ctx, &suite.paramsKeeper, types.NewParameterChangeProposal("hello", "word", tt.changes, 1)) + + res := suite.paramsKeeper.GetGasConfig(ctx) + tt.fncheck(*res) + } +} diff --git a/x/params/querier.go b/x/params/querier.go index 46458b116a..346835df16 100644 --- a/x/params/querier.go +++ b/x/params/querier.go @@ -21,6 +21,8 @@ func NewQuerier(keeper Keeper) sdk.Querier { keeper.Logger(ctx).Error("invalid query path", "path", path) } return queryUpgrade(ctx, path[1], keeper) + case types.QueryGasConfig: + return queryGasConfig(ctx, req, keeper) default: return nil, sdk.ErrUnknownRequest("unknown params query endpoint") } @@ -61,3 +63,11 @@ func queryUpgrade(ctx sdk.Context, name string, keeper Keeper) ([]byte, sdk.Erro } return bz, nil } + +func queryGasConfig(ctx sdk.Context, _ abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { + bz, err := codec.MarshalJSONIndent(keeper.cdc, keeper.getGasConfig(ctx)) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, fmt.Sprintf("could not marshal result to JSON %s", err.Error())) + } + return bz, nil +} diff --git a/x/params/types/params.go b/x/params/types/params.go index cf5042368e..a477b247c8 100644 --- a/x/params/types/params.go +++ b/x/params/types/params.go @@ -17,7 +17,10 @@ const ( // ParamKeyTable returns the key declaration for parameters func ParamKeyTable() sdkparams.KeyTable { - return sdkparams.NewKeyTable().RegisterParamSet(&Params{}) + kt := sdkparams.NewKeyTable() + kt.RegisterParamSet(&Params{}) + kt.RegisterParamSet(&GasConfig{}) + return kt } // Params is the struct of the parameters in this module diff --git a/x/params/types/params_gas.go b/x/params/types/params_gas.go new file mode 100644 index 0000000000..1287f50a6c --- /dev/null +++ b/x/params/types/params_gas.go @@ -0,0 +1,45 @@ +package types + +import ( + "fmt" + "github.com/okex/exchain/libs/cosmos-sdk/x/params/subspace" + "github.com/okex/exchain/x/common" + + stypes "github.com/okex/exchain/libs/cosmos-sdk/store/types" +) + +const ( + QueryGasConfig = "gasconfig" +) + +// GasConfig is the struct of the parameters in this module +type GasConfig struct { + stypes.GasConfig +} + +func (p GasConfig) String() string { + return fmt.Sprintf(` +HasCost: %d, +DeleteCost: %d, +ReadCostFlat: %d, +ReadCostPerByte: %d, +WriteCostFlat: %d, +WriteCostPerByte: %d, +IterNextCostFlat: %d, +`, p.HasCost, p.DeleteCost, p.ReadCostFlat, p.ReadCostPerByte, p.WriteCostFlat, p.WriteCostPerByte, p.IterNextCostFlat) +} + +// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs +// pairs of auth module's parameters. +// nolint +func (p *GasConfig) ParamSetPairs() subspace.ParamSetPairs { + return subspace.ParamSetPairs{ + {[]byte(stypes.GasHasDesc), &p.HasCost, common.ValidateUint64Positive("gas has")}, + {[]byte(stypes.GasDeleteDesc), &p.DeleteCost, common.ValidateUint64Positive("gas delete")}, + {[]byte(stypes.GasReadCostFlatDesc), &p.ReadCostFlat, common.ValidateUint64Positive("gas read cost flat")}, + {[]byte(stypes.GasReadPerByteDesc), &p.ReadCostPerByte, common.ValidateUint64Positive("gas read per byte")}, + {[]byte(stypes.GasWriteCostFlatDesc), &p.WriteCostFlat, common.ValidateUint64Positive("gas write cost flat")}, + {[]byte(stypes.GasWritePerByteDesc), &p.WriteCostPerByte, common.ValidateUint64Positive("gas write cost per byte")}, + {[]byte(stypes.GasIterNextCostFlatDesc), &p.IterNextCostFlat, common.ValidateUint64Positive("gas iter next cost flat")}, + } +} diff --git a/x/wasm/proxy/keeper_proxy.go b/x/wasm/proxy/keeper_proxy.go index e0387636e7..4c69dd2ec2 100644 --- a/x/wasm/proxy/keeper_proxy.go +++ b/x/wasm/proxy/keeper_proxy.go @@ -32,8 +32,6 @@ const ( accountBytesLen = 80 ) -var gasConfig = types2.KVGasConfig() - // AccountKeeperProxy defines the expected account keeper interface type AccountKeeperProxy struct { cachedAcc map[string]*apptypes.EthAccount @@ -63,6 +61,7 @@ func (a AccountKeeperProxy) IterateAccounts(ctx sdk.Context, cb func(account aut } func (a AccountKeeperProxy) GetAccount(ctx sdk.Context, addr sdk.AccAddress) authexported.Account { + gasConfig := types2.KVGasConfig() ctx.GasMeter().ConsumeGas(gasConfig.ReadCostFlat, types2.GasReadCostFlatDesc) ctx.GasMeter().ConsumeGas(gasConfig.ReadCostPerByte*accountBytesLen, types2.GasReadPerByteDesc) acc, ok := a.cachedAcc[addr.String()] @@ -82,6 +81,7 @@ func (a AccountKeeperProxy) SetAccount(ctx sdk.Context, account authexported.Acc a.cachedAcc = make(map[string]*apptypes.EthAccount) } a.cachedAcc[account.GetAddress().String()] = acc + gasConfig := types2.KVGasConfig() ctx.GasMeter().ConsumeGas(gasConfig.WriteCostFlat, types2.GasWriteCostFlatDesc) ctx.GasMeter().ConsumeGas(gasConfig.WriteCostPerByte*accountBytesLen, types2.GasWritePerByteDesc) return @@ -89,6 +89,7 @@ func (a AccountKeeperProxy) SetAccount(ctx sdk.Context, account authexported.Acc func (a AccountKeeperProxy) RemoveAccount(ctx sdk.Context, account authexported.Account) { delete(a.cachedAcc, account.GetAddress().String()) + gasConfig := types2.KVGasConfig() ctx.GasMeter().ConsumeGas(gasConfig.DeleteCost, types2.GasDeleteDesc) }