Skip to content

Commit

Permalink
add gas calculator for some new msg types
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Jan 31, 2023
1 parent 03c4713 commit 31f2816
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 94 deletions.
6 changes: 6 additions & 0 deletions proto/cosmos/gashub/v1alpha1/gashub.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ message Params {
uint64 msg_undelegate_gas = 25 [(gogoproto.customname) = "MsgUndelegateGas"];
uint64 msg_begin_redelegate_gas = 26 [(gogoproto.customname) = "MsgBeginRedelegateGas"];
uint64 msg_cancel_unbonding_delegation_gas = 27 [(gogoproto.customname) = "MsgCancelUnbondingDelegationGas"];
uint64 msg_create_validator_gas = 28 [(gogoproto.customname) = "MsgCreateValidatorGas"];
uint64 msg_claim_gas = 29 [(gogoproto.customname) = "MsgClaimGas"];
uint64 msg_transfer_out_gas = 30 [(gogoproto.customname) = "MsgTransferOutGas"];
uint64 msg_create_storage_provider_gas = 31 [(gogoproto.customname) = "MsgCreateStorageProviderGas"];
uint64 msg_edit_storage_provider_gas = 32 [(gogoproto.customname) = "MsgEditStorageProviderGas"];
uint64 msg_sp_deposit_gas = 33 [(gogoproto.customname) = "MsgSpDepositGas"];
}
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func NewSimApp(
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

app.GashubKeeper = gashubkeeper.NewGashubKeeper(appCodec, keys[gashubtypes.StoreKey], app.GetSubspace(gashubtypes.ModuleName))
app.GashubKeeper = gashubkeeper.NewKeeper(appCodec, keys[gashubtypes.StoreKey], app.GetSubspace(gashubtypes.ModuleName))

// Register the upgrade keeper
upgradeHandler := map[string]upgradetypes.UpgradeHandler{
Expand Down
16 changes: 8 additions & 8 deletions x/auth/ante/msg_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
// in or empty.
type ValidateTxSizeDecorator struct {
ak AccountKeeper
fhk GashubKeeper
ghk GashubKeeper
}

func NewValidateTxSizeDecorator(ak AccountKeeper, fhk GashubKeeper) ValidateTxSizeDecorator {
func NewValidateTxSizeDecorator(ak AccountKeeper, ghk GashubKeeper) ValidateTxSizeDecorator {
return ValidateTxSizeDecorator{
ak: ak,
fhk: fhk,
ghk: ghk,
}
}

Expand Down Expand Up @@ -77,7 +77,7 @@ func (vtsd ValidateTxSizeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
newCtx = ctx.WithTxSize(txSize)
}

params := vtsd.fhk.GetParams(ctx)
params := vtsd.ghk.GetParams(ctx)
if txSize > params.GetMaxTxSize() {
return ctx, errors.Wrapf(sdkerrors.ErrTxTooLarge, "tx length: %d, limit: %d", txSize, params.GetMaxTxSize())
}
Expand All @@ -89,13 +89,13 @@ func (vtsd ValidateTxSizeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
// the size of tx and msg type before calling next AnteHandler.
type ConsumeMsgGasDecorator struct {
ak AccountKeeper
fhk GashubKeeper
ghk GashubKeeper
}

func NewConsumeMsgGasDecorator(ak AccountKeeper, fhk GashubKeeper) ConsumeMsgGasDecorator {
func NewConsumeMsgGasDecorator(ak AccountKeeper, ghk GashubKeeper) ConsumeMsgGasDecorator {
return ConsumeMsgGasDecorator{
ak: ak,
fhk: fhk,
ghk: ghk,
}
}

Expand All @@ -105,7 +105,7 @@ func (cmfg ConsumeMsgGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
return ctx, errors.Wrap(sdkerrors.ErrTxDecode, "invalid tx type")
}

params := cmfg.fhk.GetParams(ctx)
params := cmfg.ghk.GetParams(ctx)
gasByTxSize := cmfg.getTxSizeGas(params, ctx)
gasByMsgType, err := cmfg.getMsgGas(params, sigTx)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions x/gashub/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
//
// CONTRACT: old coins from the FeeCollectionKeeper need to be transferred through
// a genesis port script to the new fee collector account
func (fhk Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
fhk.SetParams(ctx, data.Params)
func (ghk Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
ghk.SetParams(ctx, data.Params)
}

// ExportGenesis returns a GenesisState for a given context and keeper
func (fhk Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
params := fhk.GetParams(ctx)
func (ghk Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
params := ghk.GetParams(ctx)

return types.NewGenesisState(params)
}
4 changes: 2 additions & 2 deletions x/gashub/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
var _ types.QueryServer = Keeper{}

// Params returns parameters of auth module
func (fhk Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (ghk Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
params := fhk.GetParams(ctx)
params := ghk.GetParams(ctx)

return &types.QueryParamsResponse{Params: params}, nil
}
8 changes: 4 additions & 4 deletions x/gashub/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Keeper struct {
paramSubspace paramtypes.Subspace
}

// NewGashubKeeper returns a new gashub keeper
func NewGashubKeeper(
// NewKeeper returns a new gashub keeper
func NewKeeper(
cdc codec.BinaryCodec, key storetypes.StoreKey, paramstore paramtypes.Subspace,
) Keeper {
// set KeyTable if it has not already been set
Expand All @@ -35,9 +35,9 @@ func NewGashubKeeper(
}

// Logger returns a module-specific logger.
func (fhk Keeper) Logger(ctx sdk.Context) log.Logger {
func (ghk Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}

// GetCodec return codec.Codec object used by the keeper
func (fhk Keeper) GetCodec() codec.BinaryCodec { return fhk.cdc }
func (ghk Keeper) GetCodec() codec.BinaryCodec { return ghk.cdc }
8 changes: 4 additions & 4 deletions x/gashub/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
)

// SetParams sets the auth module's parameters.
func (fhk Keeper) SetParams(ctx sdk.Context, params types.Params) {
fhk.paramSubspace.SetParamSet(ctx, &params)
func (ghk Keeper) SetParams(ctx sdk.Context, params types.Params) {
ghk.paramSubspace.SetParamSet(ctx, &params)
}

// GetParams gets the auth module's parameters.
func (fhk Keeper) GetParams(ctx sdk.Context) (params types.Params) {
fhk.paramSubspace.GetParamSet(ctx, &params)
func (ghk Keeper) GetParams(ctx sdk.Context) (params types.Params) {
ghk.paramSubspace.GetParamSet(ctx, &params)
return
}
2 changes: 1 addition & 1 deletion x/gashub/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func RandomizedGenState(simState *module.SimulationState) {

params := types.NewParams(maxTxSize, minGasPerByte, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas,
msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas,
msgGas, msgGas, msgGas)
msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas, msgGas)

gashubGenesis := types.NewGenesisState(params)

Expand Down
9 changes: 9 additions & 0 deletions x/gashub/types/gas_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
distribution "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
oracletypes "github.com/cosmos/cosmos-sdk/x/oracle/types"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -201,4 +202,12 @@ func init() {
fixedGas := params.GetMsgCancelUnbondingDelegationGas()
return FixedGasCalculator(fixedGas)
})
RegisterCalculatorGen(types.MsgTypeURL(&staking.MsgCreateValidator{}), func(params Params) GasCalculator {
fixedGas := params.GetMsgCreateValidatorGas()
return FixedGasCalculator(fixedGas)
})
RegisterCalculatorGen(types.MsgTypeURL(&oracletypes.MsgClaim{}), func(params Params) GasCalculator {
fixedGas := params.GetMsgClaimGas()
return FixedGasCalculator(fixedGas)
})
}
Loading

0 comments on commit 31f2816

Please sign in to comment.