From 09870f6631b09865e8edf18bfa11f552a703f384 Mon Sep 17 00:00:00 2001 From: lyh169 Date: Thu, 17 Aug 2023 11:51:26 +0800 Subject: [PATCH 1/2] add proposal for modify the MaxGasUsedPerBlock --- app/app.go | 7 ++++ app/config/config.go | 8 +++++ libs/cosmos-sdk/baseapp/abci.go | 11 ++++++ libs/cosmos-sdk/baseapp/baseapp.go | 1 + libs/cosmos-sdk/baseapp/options.go | 7 ++++ libs/cosmos-sdk/types/config.go | 17 +++++++++ libs/cosmos-sdk/types/config_test.go | 8 +++++ libs/cosmos-sdk/types/handler.go | 2 ++ x/params/client/cli/query.go | 26 ++++++++++++++ x/params/keeper.go | 13 +++++++ x/params/keeper_test.go | 53 ++++++++++++++++++++++++++++ x/params/querier.go | 10 ++++++ x/params/types/params.go | 1 + x/params/types/params_block.go | 53 ++++++++++++++++++++++++++++ 14 files changed, 217 insertions(+) create mode 100644 x/params/types/params_block.go diff --git a/app/app.go b/app/app.go index 785349250..020c64273 100644 --- a/app/app.go +++ b/app/app.go @@ -708,6 +708,7 @@ func NewOKBChainApp( app.SetEvmWatcherCollector(app.EvmKeeper.Watcher.Collect) app.SetUpdateCMTxNonceHandler(NewUpdateCMTxNonceHandler()) app.SetGetGasConfigHandler(NewGetGasConfigHandler(app.ParamsKeeper)) + app.SetGetBlockConfigHandler(NewGetBlockConfigHandler(app.ParamsKeeper)) mpt.AccountStateRootRetriever = app.AccountKeeper if loadLatest { err := app.LoadLatestVersion(app.keys[bam.MainStoreKey]) @@ -968,3 +969,9 @@ func NewGetGasConfigHandler(pk params.Keeper) sdk.GetGasConfigHandler { return pk.GetGasConfig(ctx) } } + +func NewGetBlockConfigHandler(pk params.Keeper) sdk.GetBlockConfigHandler { + return func(ctx sdk.Context) *sdk.BlockConfig { + return pk.GetBlockConfig(ctx) + } +} diff --git a/app/config/config.go b/app/config/config.go index c93b2754e..9aa22d2b3 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -14,6 +14,7 @@ import ( "github.com/okx/okbchain/libs/cosmos-sdk/server" "github.com/okx/okbchain/libs/cosmos-sdk/store/iavl" "github.com/okx/okbchain/libs/cosmos-sdk/store/types" + sdk "github.com/okx/okbchain/libs/cosmos-sdk/types" tmiavl "github.com/okx/okbchain/libs/iavl" iavlconfig "github.com/okx/okbchain/libs/iavl/config" "github.com/okx/okbchain/libs/system" @@ -230,6 +231,10 @@ var ( confLogger log.Logger ) +func GetchainMaxGasUsedPerBlock() int64 { + return sdk.GetMaxGasUsedPerBlock() +} + func GetOkbcConfig() *OkbcConfig { once.Do(func() { okbcConfig = NewOkbcConfig() @@ -834,6 +839,9 @@ func (c *OkbcConfig) SetEnableDeleteMinGPTx(enable bool) { } func (c *OkbcConfig) GetMaxGasUsedPerBlock() int64 { + if c.maxGasUsedPerBlock == -1 { + return GetchainMaxGasUsedPerBlock() + } return c.maxGasUsedPerBlock } diff --git a/libs/cosmos-sdk/baseapp/abci.go b/libs/cosmos-sdk/baseapp/abci.go index da8d34941..b53d617d8 100644 --- a/libs/cosmos-sdk/baseapp/abci.go +++ b/libs/cosmos-sdk/baseapp/abci.go @@ -162,6 +162,10 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg app.UpdateGlobalGasConfig(app.deliverState.ctx) } + if app.getBlockConfigHandler != nil { + app.UpdateBlockConfig(app.deliverState.ctx) + } + app.deliverState.ctx.SetBlockGasMeter(gasMeter) if app.beginBlocker != nil { @@ -188,6 +192,13 @@ func (app *BaseApp) UpdateGlobalGasConfig(ctx sdk.Context) { stypes.UpdateGlobalGasConfig(app.getGasConfigHandler(ctx)) } +func (app *BaseApp) UpdateBlockConfig(ctx sdk.Context) { + if ctx.IsCheckTx() || ctx.IsTraceTx() { + return + } + sdk.UpdateBlockConfig(app.getBlockConfigHandler(ctx)) +} + func (app *BaseApp) UpdateFeeCollector(fee sdk.Coins, add bool) { if fee.IsZero() { return diff --git a/libs/cosmos-sdk/baseapp/baseapp.go b/libs/cosmos-sdk/baseapp/baseapp.go index 5763d961a..fe43edc22 100644 --- a/libs/cosmos-sdk/baseapp/baseapp.go +++ b/libs/cosmos-sdk/baseapp/baseapp.go @@ -157,6 +157,7 @@ type BaseApp struct { // nolint: maligned getTxFeeHandler sdk.GetTxFeeHandler updateCMTxNonceHandler sdk.UpdateCMTxNonceHandler getGasConfigHandler sdk.GetGasConfigHandler + getBlockConfigHandler sdk.GetBlockConfigHandler // volatile states: // diff --git a/libs/cosmos-sdk/baseapp/options.go b/libs/cosmos-sdk/baseapp/options.go index 624304be6..3cf9d90a3 100644 --- a/libs/cosmos-sdk/baseapp/options.go +++ b/libs/cosmos-sdk/baseapp/options.go @@ -226,3 +226,10 @@ func (app *BaseApp) SetGetGasConfigHandler(handler sdk.GetGasConfigHandler) { } app.getGasConfigHandler = handler } + +func (app *BaseApp) SetGetBlockConfigHandler(handler sdk.GetBlockConfigHandler) { + if app.sealed { + panic("SetGetBlockConfigHandler() on sealed BaseApp") + } + app.getBlockConfigHandler = handler +} diff --git a/libs/cosmos-sdk/types/config.go b/libs/cosmos-sdk/types/config.go index 678f3dc72..16b737a66 100644 --- a/libs/cosmos-sdk/types/config.go +++ b/libs/cosmos-sdk/types/config.go @@ -3,6 +3,7 @@ package types import ( "context" "sync" + "sync/atomic" "github.com/okx/okbchain/libs/cosmos-sdk/version" ) @@ -198,3 +199,19 @@ func KeyringServiceName() string { } return version.Name } + +const DefaultMaxGasUsedPerBlock int64 = -1 + +var globalBlockConfig = &BlockConfig{MaxGasUsedPerBlock: DefaultMaxGasUsedPerBlock} + +type BlockConfig struct { + MaxGasUsedPerBlock int64 +} + +func UpdateBlockConfig(bc *BlockConfig) { + atomic.StoreInt64(&globalBlockConfig.MaxGasUsedPerBlock, bc.MaxGasUsedPerBlock) +} + +func GetMaxGasUsedPerBlock() int64 { + return atomic.LoadInt64(&globalBlockConfig.MaxGasUsedPerBlock) +} diff --git a/libs/cosmos-sdk/types/config_test.go b/libs/cosmos-sdk/types/config_test.go index c202a1a57..3fece4a7a 100644 --- a/libs/cosmos-sdk/types/config_test.go +++ b/libs/cosmos-sdk/types/config_test.go @@ -48,3 +48,11 @@ func TestConfig_SetFullFundraiserPath(t *testing.T) { func TestKeyringServiceName(t *testing.T) { require.Equal(t, sdk.DefaultKeyringServiceName, sdk.KeyringServiceName()) } + +func TestBlockConfig(t *testing.T) { + require.Equal(t, sdk.DefaultMaxGasUsedPerBlock, sdk.GetMaxGasUsedPerBlock()) + sdk.UpdateBlockConfig(&sdk.BlockConfig{0}) + require.Equal(t, int64(0), sdk.GetMaxGasUsedPerBlock()) + sdk.UpdateBlockConfig(&sdk.BlockConfig{100}) + require.Equal(t, int64(100), sdk.GetMaxGasUsedPerBlock()) +} diff --git a/libs/cosmos-sdk/types/handler.go b/libs/cosmos-sdk/types/handler.go index ee5db44e7..8d21554d6 100644 --- a/libs/cosmos-sdk/types/handler.go +++ b/libs/cosmos-sdk/types/handler.go @@ -28,6 +28,8 @@ type UpdateFeeCollectorAccHandler func(ctx Context, balance Coins, txFeesplit [] type GetGasConfigHandler func(ctx Context) *stypes.GasConfig +type GetBlockConfigHandler func(ctx Context) *BlockConfig + type UpdateCosmosTxCount func(ctx Context, txCount int) type LogFix func(tx []Tx, logIndex []int, hasEnterEvmTx []bool, errs []error, resp []abci.ResponseDeliverTx) (logs [][]byte) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index 4a35ea1c6..e695530ad 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -23,6 +23,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { GetCmdQueryParams(queryRoute, cdc), GetCmdQueryUpgrade(queryRoute, cdc), GetCmdQueryGasConfig(queryRoute, cdc), + GetCmdQueryBlockConfig(queryRoute, cdc), )...) return queryCmd @@ -78,3 +79,28 @@ $ exchaincli query params gasconfig }, } } + +// GetCmdQueryBlockConfig implements the query params command. +func GetCmdQueryBlockConfig(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "blockconfig", + Short: "Query parameters of blockconfig", + Long: strings.TrimSpace(`Query parameters of blockconfig: +$ exchaincli query params blockconfig +`), + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, _ []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + + route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryBlockConfig) + bz, _, err := cliCtx.QueryWithData(route, nil) + if err != nil { + return err + } + + var params types.BlockConfig + cdc.MustUnmarshalJSON(bz, ¶ms) + return cliCtx.PrintOutput(params) + }, + } +} diff --git a/x/params/keeper.go b/x/params/keeper.go index 3a65be35f..4ceca06a4 100644 --- a/x/params/keeper.go +++ b/x/params/keeper.go @@ -85,3 +85,16 @@ func (keeper Keeper) getGasConfig(ctx sdk.Context) (params types.GasConfig) { stypes.AsDefaultGasConfig(¶ms.GasConfig) return } + +func (keeper Keeper) GetBlockConfig(ctx sdk.Context) *sdk.BlockConfig { + params := keeper.getBlockConfig(ctx) + return &sdk.BlockConfig{params.MaxGasUsedPerBlock} +} + +func (keeper Keeper) getBlockConfig(ctx sdk.Context) *types.BlockConfig { + params := types.NewDefaultBlockConfig() + for _, pair := range params.ParamSetPairs() { + keeper.paramSpace.GetIfExists(ctx, pair.Key, pair.Value) + } + return params +} diff --git a/x/params/keeper_test.go b/x/params/keeper_test.go index 7e82e1b0c..c98b410c0 100644 --- a/x/params/keeper_test.go +++ b/x/params/keeper_test.go @@ -135,3 +135,56 @@ func (suite *KeeperSuite) TestGetGasConfig() { tt.fncheck(*res) } } + +func (suite *KeeperSuite) TestGetBlockConfig() { + sub := "params" + tests := []struct { + changes []types.ParamChange + fncheck func(res sdk.BlockConfig, err error) + }{ + { + changes: []types.ParamChange{{Subspace: sub, Key: types.MaxGasUsedPerBlock, Value: "\"-1\""}}, + fncheck: func(res sdk.BlockConfig, err error) { + suite.NoError(err) + suite.Equal(sdk.BlockConfig{MaxGasUsedPerBlock: -1}, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: types.MaxGasUsedPerBlock, Value: "\"0\""}}, + fncheck: func(res sdk.BlockConfig, err error) { + suite.NoError(err) + suite.Equal(sdk.BlockConfig{MaxGasUsedPerBlock: 0}, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: types.MaxGasUsedPerBlock, Value: "\"100\""}}, + fncheck: func(res sdk.BlockConfig, err error) { + suite.NoError(err) + suite.Equal(sdk.BlockConfig{MaxGasUsedPerBlock: 100}, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: types.MaxGasUsedPerBlock, Value: "\"1000000000000\""}}, + fncheck: func(res sdk.BlockConfig, err error) { + suite.NoError(err) + suite.Equal(sdk.BlockConfig{MaxGasUsedPerBlock: 1000000000000}, res) + }, + }, + { + changes: []types.ParamChange{{Subspace: sub, Key: types.MaxGasUsedPerBlock, Value: "\"-2\""}}, + fncheck: func(res sdk.BlockConfig, err error) { + suite.Error(err) + suite.Equal(sdk.BlockConfig{MaxGasUsedPerBlock: 1000000000000}, res) + }, + }, + } + + for _, tt := range tests { + ctx := suite.Context(0) + + err := changeParams(ctx, &suite.paramsKeeper, types.NewParameterChangeProposal("hello", "word", tt.changes, 1)) + + res := suite.paramsKeeper.GetBlockConfig(ctx) + tt.fncheck(*res, err) + } +} diff --git a/x/params/querier.go b/x/params/querier.go index 7eab160cc..0d6286e32 100644 --- a/x/params/querier.go +++ b/x/params/querier.go @@ -23,6 +23,8 @@ func NewQuerier(keeper Keeper) sdk.Querier { return queryUpgrade(ctx, path[1], keeper) case types.QueryGasConfig: return queryGasConfig(ctx, req, keeper) + case types.QueryBlockConfig: + return queryBlockConfig(ctx, req, keeper) default: return nil, sdk.ErrUnknownRequest("unknown params query endpoint") } @@ -71,3 +73,11 @@ func queryGasConfig(ctx sdk.Context, _ abci.RequestQuery, keeper Keeper) ([]byte } return bz, nil } + +func queryBlockConfig(ctx sdk.Context, _ abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { + bz, err := codec.MarshalJSONIndent(keeper.cdc, keeper.getBlockConfig(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 6bc9a47d5..4b425b8f7 100644 --- a/x/params/types/params.go +++ b/x/params/types/params.go @@ -21,6 +21,7 @@ func ParamKeyTable() sdkparams.KeyTable { kt := sdkparams.NewKeyTable() kt.RegisterParamSet(&Params{}) kt.RegisterParamSet(&GasConfig{}) + kt.RegisterParamSet(&BlockConfig{}) return kt } diff --git a/x/params/types/params_block.go b/x/params/types/params_block.go new file mode 100644 index 000000000..5d5534002 --- /dev/null +++ b/x/params/types/params_block.go @@ -0,0 +1,53 @@ +package types + +import ( + "fmt" + sdk "github.com/okx/okbchain/libs/cosmos-sdk/types" + "github.com/okx/okbchain/libs/cosmos-sdk/x/params/subspace" +) + +const ( + QueryBlockConfig = "blockconfig" + MaxGasUsedPerBlock = "MaxGasUsedPerBlock" +) + +// BlockConfig is the struct of the parameters in this module +type BlockConfig struct { + MaxGasUsedPerBlock int64 `json:"maxGasUsedPerBlock"` +} + +func NewDefaultBlockConfig() *BlockConfig { + return &BlockConfig{ + MaxGasUsedPerBlock: sdk.DefaultMaxGasUsedPerBlock, + } +} + +func (p BlockConfig) String() string { + return fmt.Sprintf(` +MaxGasUsedPerBlock: %d, +`, p.MaxGasUsedPerBlock) +} + +// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs +// pairs of auth module's parameters. +// nolint +func (p *BlockConfig) ParamSetPairs() subspace.ParamSetPairs { + return subspace.ParamSetPairs{ + {[]byte(MaxGasUsedPerBlock), &p.MaxGasUsedPerBlock, ValidateInt64("maxGasUsedPerBlock")}, + } +} + +func ValidateInt64(param string) subspace.ValueValidatorFn { + return func(i interface{}) error { + v, ok := i.(int64) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v < -1 { + return fmt.Errorf("%s must be equal or greater than -1: %d", param, v) + } + + return nil + } +} From 22137b0d80621a9f134a7b9ca692a2fa00ca0e99 Mon Sep 17 00:00:00 2001 From: lyh169 Date: Mon, 21 Aug 2023 11:03:32 +0800 Subject: [PATCH 2/2] modify the issue --- app/config/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/config/config.go b/app/config/config.go index 9aa22d2b3..777b0f1e6 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -231,7 +231,7 @@ var ( confLogger log.Logger ) -func GetchainMaxGasUsedPerBlock() int64 { +func GetChainMaxGasUsedPerBlock() int64 { return sdk.GetMaxGasUsedPerBlock() } @@ -840,7 +840,7 @@ func (c *OkbcConfig) SetEnableDeleteMinGPTx(enable bool) { func (c *OkbcConfig) GetMaxGasUsedPerBlock() int64 { if c.maxGasUsedPerBlock == -1 { - return GetchainMaxGasUsedPerBlock() + return GetChainMaxGasUsedPerBlock() } return c.maxGasUsedPerBlock }