Skip to content

Commit 0b8ae2c

Browse files
wetcodjinsan-line
andauthored
feat: implement validateGasWanted() (#65)
* build: bump up tendermint to v0.33.9-0.1.0-rc2 * feat: implement `validateGasWanted()` (#48) * feat: impl `validateGasWanted()` * fix: lint error * chore: bump up tendermint * fix: tests to use `grpc` instead of `socket` * fix: revert the transport of the server to socket Co-authored-by: KIm, JinSan <[email protected]>
1 parent b51d480 commit 0b8ae2c

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

baseapp/baseapp.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,9 @@ func (app *BaseApp) IsSealed() bool { return app.sealed }
388388
func (app *BaseApp) setCheckState(header abci.Header) {
389389
ms := app.cms.CacheMultiStore()
390390
app.checkState = &state{
391-
ms: ms,
392-
ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices),
391+
ms: ms,
392+
ctx: sdk.NewContext(ms, header, true, app.logger).
393+
WithMinGasPrices(app.minGasPrices).WithConsensusParams(app.consensusParams),
393394
}
394395
}
395396

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ require (
4545
replace (
4646
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
4747
github.com/tendermint/iavl v0.14.3 => github.com/line/iavl v0.14.3-0.1.0-rc1
48-
github.com/tendermint/tendermint v0.33.9 => github.com/line/tendermint v0.33.9-0.1.0-rc1
48+
github.com/tendermint/tendermint v0.33.9 => github.com/line/tendermint v0.33.9-0.1.0-rc2
4949
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-b
265265
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
266266
github.com/line/iavl v0.14.3-0.1.0-rc1 h1:REgLo0rHlEv88XyEEpF4lqkmyg+CawzzvxIeB+kQW9M=
267267
github.com/line/iavl v0.14.3-0.1.0-rc1/go.mod h1:vHLYxU/zuxBmxxr1v+5Vnd/JzcIsyK17n9P9RDubPVU=
268-
github.com/line/tendermint v0.33.9-0.1.0-rc1 h1:JD9GG5/pqWOeEr0NJaebFNj57puDby2KyONyMQN2Vb0=
269-
github.com/line/tendermint v0.33.9-0.1.0-rc1/go.mod h1:0yUs9eIuuDq07nQql9BmI30FtYGcEC60Tu5JzB5IezM=
268+
github.com/line/tendermint v0.33.9-0.1.0-rc2 h1:G2plZ9h1xitXP+nYtGlR8SRrHbBuim8KeYxP3JDBoGY=
269+
github.com/line/tendermint v0.33.9-0.1.0-rc2/go.mod h1:0yUs9eIuuDq07nQql9BmI30FtYGcEC60Tu5JzB5IezM=
270270
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
271271
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
272272
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=

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)