Skip to content

Commit 0072984

Browse files
authored
feat: implement validateGasWanted() (#48) (#142)
* 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
1 parent a25ca2f commit 0072984

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

baseapp/baseapp.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,13 @@ func (app *BaseApp) setCheckState(header ostproto.Header) {
359359
ms := app.cms.CacheMultiStore()
360360
app.checkStateMtx.Lock()
361361
defer app.checkStateMtx.Unlock()
362+
363+
ctx := sdk.NewContext(ms, header, true, app.logger).
364+
WithMinGasPrices(app.minGasPrices)
365+
362366
app.checkState = &state{
363367
ms: ms,
364-
ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices),
368+
ctx: ctx.WithConsensusParams(app.GetConsensusParams(ctx)),
365369
}
366370
}
367371

x/auth/ante/setup.go

+31
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
4141

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

44+
err = validateGasWanted(newCtx)
45+
if err != nil {
46+
return newCtx, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, err.Error())
47+
}
48+
4449
// Decorator will catch an OutOfGasPanic caused in the next antehandler
4550
// AnteHandlers must have their own defer/recover in order for the BaseApp
4651
// to know how much gas was used! This is because the GasMeter is created in
@@ -74,3 +79,29 @@ func SetGasMeter(simulate bool, ctx sdk.Context, gasLimit uint64) sdk.Context {
7479

7580
return ctx.WithGasMeter(sdk.NewGasMeter(gasLimit))
7681
}
82+
83+
func validateGasWanted(ctx sdk.Context) error {
84+
// validate gasWanted only when checkTx
85+
if !ctx.IsCheckTx() || ctx.IsReCheckTx() {
86+
return nil
87+
}
88+
89+
// TODO: Should revise type
90+
// reference: https://github.com/line/cosmos-sdk/blob/fd6d941cc429fc2a58154dbace3bbaec4beef445/baseapp/abci.go#L189
91+
gasWanted := int64(ctx.GasMeter().Limit())
92+
if gasWanted < 0 {
93+
return fmt.Errorf("gas wanted %d is negative", gasWanted)
94+
}
95+
96+
consParams := ctx.ConsensusParams()
97+
if consParams == nil || consParams.Block == nil || consParams.Block.MaxGas == -1 {
98+
return nil
99+
}
100+
101+
maxGas := consParams.Block.MaxGas
102+
if gasWanted > maxGas {
103+
return fmt.Errorf("gas wanted %d is greater than max gas %d", gasWanted, maxGas)
104+
}
105+
106+
return nil
107+
}

0 commit comments

Comments
 (0)