Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement validateGasWanted() #48

Merged
merged 4 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,9 @@ func (app *BaseApp) IsSealed() bool { return app.sealed }
func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices),
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.logger).
WithMinGasPrices(app.minGasPrices).WithConsensusParams(app.consensusParams),
}
}

Expand Down
31 changes: 31 additions & 0 deletions x/auth/ante/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate

newCtx = SetGasMeter(simulate, ctx, gasTx.GetGas())

err = validateGasWanted(newCtx)
if err != nil {
return newCtx, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, err.Error())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ErrOutOfGas proper for this error? I'd like to hear your opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it is proper error

}

// Decorator will catch an OutOfGasPanic caused in the next antehandler
// AnteHandlers must have their own defer/recover in order for the BaseApp
// to know how much gas was used! This is because the GasMeter is created in
Expand Down Expand Up @@ -74,3 +79,29 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64) sdk.Context {

return ctx.WithGasMeter(sdk.NewGasMeter(gasLimit))
}

func validateGasWanted(ctx sdk.Context) error {
// validate gasWanted only when checkTx
if !ctx.IsCheckTx() || ctx.IsReCheckTx() {
return nil
}

// TODO: Should revise type
// reference: https://github.com/line/cosmos-sdk/blob/fd6d941cc429fc2a58154dbace3bbaec4beef445/baseapp/abci.go#L189
gasWanted := int64(ctx.GasMeter().Limit())
if gasWanted < 0 {
return fmt.Errorf("gas wanted %d is negative", gasWanted)
}

consParams := ctx.ConsensusParams()
if consParams == nil || consParams.Block == nil || consParams.Block.MaxGas == -1 {
return nil
}

maxGas := consParams.Block.MaxGas
if gasWanted > maxGas {
return fmt.Errorf("gas wanted %d is greater than max gas %d", gasWanted, maxGas)
}

return nil
}