Skip to content

Commit

Permalink
feat: implement validateGasWanted() (#48)
Browse files Browse the repository at this point in the history
* feat: impl `validateGasWanted()`

* fix: lint error

* chore: bump up tendermint

* fix: tests to use `grpc` instead of `socket`
# Conflicts:
#	go.mod
#	go.sum
#	server/start.go
#	x/genutil/client/cli/init_test.go
  • Loading branch information
jinsan-line committed Apr 22, 2021
1 parent a25ca2f commit deb9837
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,13 @@ func (app *BaseApp) setCheckState(header ostproto.Header) {
ms := app.cms.CacheMultiStore()
app.checkStateMtx.Lock()
defer app.checkStateMtx.Unlock()

ctx := sdk.NewContext(ms, header, true, app.logger).
WithMinGasPrices(app.minGasPrices)

app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices),
ctx: ctx.WithConsensusParams(app.GetConsensusParams(ctx)),
}
}

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())
}

// 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
}

0 comments on commit deb9837

Please sign in to comment.