Skip to content

Commit

Permalink
add proposal for modify the MaxGasUsedPerBlock (#3241)
Browse files Browse the repository at this point in the history
* add proposal for modify the MaxGasUsedPerBlock

* modify the issue
  • Loading branch information
lyh169 authored Aug 23, 2023
1 parent f438319 commit 2bee5f9
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ func NewOKExChainApp(
app.SetEvmWatcherCollector(app.EvmKeeper.Watcher.Collect)
app.SetUpdateCMTxNonceHandler(NewUpdateCMTxNonceHandler())
app.SetGetGasConfigHandler(NewGetGasConfigHandler(app.ParamsKeeper))
app.SetGetBlockConfigHandler(NewGetBlockConfigHandler(app.ParamsKeeper))

if loadLatest {
err := app.LoadLatestVersion(app.keys[bam.MainStoreKey])
Expand Down Expand Up @@ -1055,3 +1056,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)
}
}
8 changes: 8 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/okex/exchain/libs/cosmos-sdk/server"
"github.com/okex/exchain/libs/cosmos-sdk/store/iavl"
"github.com/okex/exchain/libs/cosmos-sdk/store/types"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
tmiavl "github.com/okex/exchain/libs/iavl"
iavlconfig "github.com/okex/exchain/libs/iavl/config"
"github.com/okex/exchain/libs/system"
Expand Down Expand Up @@ -233,6 +234,10 @@ var (
confLogger log.Logger
)

func GetChainMaxGasUsedPerBlock() int64 {
return sdk.GetMaxGasUsedPerBlock()
}

func GetOecConfig() *OecConfig {
once.Do(func() {
oecConfig = NewOecConfig()
Expand Down Expand Up @@ -846,6 +851,9 @@ func (c *OecConfig) SetEnableDeleteMinGPTx(enable bool) {
}

func (c *OecConfig) GetMaxGasUsedPerBlock() int64 {
if c.maxGasUsedPerBlock == -1 {
return GetChainMaxGasUsedPerBlock()
}
return c.maxGasUsedPerBlock
}

Expand Down
11 changes: 11 additions & 0 deletions libs/cosmos-sdk/baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,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 {
Expand All @@ -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) updateFeeCollectorAccount(isEndBlock bool) {
if app.updateFeeCollectorAccHandler == nil {
return
Expand Down
1 change: 1 addition & 0 deletions libs/cosmos-sdk/baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type BaseApp struct { // nolint: maligned
getTxFeeHandler sdk.GetTxFeeHandler
updateCMTxNonceHandler sdk.UpdateCMTxNonceHandler
getGasConfigHandler sdk.GetGasConfigHandler
getBlockConfigHandler sdk.GetBlockConfigHandler

// volatile states:
//
Expand Down
7 changes: 7 additions & 0 deletions libs/cosmos-sdk/baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,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
}
20 changes: 19 additions & 1 deletion libs/cosmos-sdk/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"context"
"sync"
"sync/atomic"

"github.com/okex/exchain/libs/cosmos-sdk/version"
)
Expand Down Expand Up @@ -84,7 +85,8 @@ func (config *Config) SetBech32PrefixForAccount(addressPrefix, pubKeyPrefix stri
}

// SetBech32PrefixForValidator builds the Config with Bech32 addressPrefix and publKeyPrefix for validators
// and returns the config instance
//
// and returns the config instance
func (config *Config) SetBech32PrefixForValidator(addressPrefix, pubKeyPrefix string) {
config.assertNotSealed()
config.bech32AddressPrefix["validator_addr"] = addressPrefix
Expand Down Expand Up @@ -197,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)
}
8 changes: 8 additions & 0 deletions libs/cosmos-sdk/types/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 2 additions & 0 deletions libs/cosmos-sdk/types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type GetFeeCollectorInfo func(ctx Context, onlyGetFeeCollectorStoreKey bool) (Co

type GetGasConfigHandler func(ctx Context) *stypes.GasConfig

type GetBlockConfigHandler func(ctx Context) *BlockConfig

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)
Expand Down
27 changes: 27 additions & 0 deletions x/params/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,3 +80,29 @@ $ 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, &params)
return cliCtx.PrintOutput(params)
},
}
}
13 changes: 13 additions & 0 deletions x/params/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,16 @@ func (keeper Keeper) getGasConfig(ctx sdk.Context) (params types.GasConfig) {
stypes.AsDefaultGasConfig(&params.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
}
53 changes: 53 additions & 0 deletions x/params/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
10 changes: 10 additions & 0 deletions x/params/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions x/params/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func ParamKeyTable() sdkparams.KeyTable {
kt := sdkparams.NewKeyTable()
kt.RegisterParamSet(&Params{})
kt.RegisterParamSet(&GasConfig{})
kt.RegisterParamSet(&BlockConfig{})
return kt
}

Expand Down
53 changes: 53 additions & 0 deletions x/params/types/params_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package types

import (
"fmt"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/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
}
}

0 comments on commit 2bee5f9

Please sign in to comment.