Skip to content

Commit

Permalink
Proposal adjust gasconfig (#3153)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jun 7, 2023
1 parent c245688 commit e499b24
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 25 deletions.
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
}
}
12 changes: 12 additions & 0 deletions libs/cosmos-sdk/baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
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 @@ -158,6 +158,7 @@ type BaseApp struct { // nolint: maligned
getTxFeeAndFromHandler sdk.GetTxFeeAndFromHandler
getTxFeeHandler sdk.GetTxFeeHandler
updateCMTxNonceHandler sdk.UpdateCMTxNonceHandler
getGasConfigHandler sdk.GetGasConfigHandler

// 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 @@ -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
}
94 changes: 78 additions & 16 deletions libs/cosmos-sdk/store/types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"math"
"sync"
)

// Gas consumption descriptors.
Expand All @@ -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
Expand Down Expand Up @@ -167,30 +189,70 @@ 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.
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,
}
}
3 changes: 3 additions & 0 deletions libs/cosmos-sdk/types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions libs/cosmos-sdk/x/auth/exported/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
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 @@ -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
Expand Down Expand Up @@ -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, &params)
return cliCtx.PrintOutput(params.GasConfig)
},
}
}
14 changes: 14 additions & 0 deletions x/params/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -71,3 +72,16 @@ func (keeper Keeper) GetParams(ctx sdk.Context) types.Params {
keeper.paramSpace.GetParamSet(ctx, &params)
return params
}

func (keeper Keeper) GetGasConfig(ctx sdk.Context) *stypes.GasConfig {
params := keeper.getGasConfig(ctx)
return &params.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(&params.GasConfig)
return
}
Loading

0 comments on commit e499b24

Please sign in to comment.