Skip to content

Commit 900473c

Browse files
authored
feat: implement validateGasWanted() (#48)
* feat: impl `validateGasWanted()` * fix: lint error * chore: bump up tendermint * fix: tests to use `grpc` instead of `socket`
1 parent 64071f7 commit 900473c

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

baseapp/baseapp.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,9 @@ func (app *BaseApp) IsSealed() bool { return app.sealed }
394394
func (app *BaseApp) setCheckState(header abci.Header) {
395395
ms := app.cms.CacheMultiStore()
396396
app.checkState = &state{
397-
ms: ms,
398-
ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices),
397+
ms: ms,
398+
ctx: sdk.NewContext(ms, header, true, app.logger).
399+
WithMinGasPrices(app.minGasPrices).WithConsensusParams(app.consensusParams),
399400
}
400401
}
401402

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ require (
4747
replace (
4848
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
4949
github.com/tendermint/iavl => github.com/line/iavl v0.14.4-0.20201217063301-6b67687bfae9
50-
github.com/tendermint/tendermint => github.com/line/tendermint v0.33.10-0.20201215041755-c81005d8468b
50+
github.com/tendermint/tendermint => github.com/line/tendermint v0.33.10-0.20210106061749-b557e0c9a76c
5151
)

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.4-0.20201217063301-6b67687bfae9 h1:n6YHVdTld8D0dAogBUcXTaqhk1ZMTAR9Phy4xdMVWDY=
267267
github.com/line/iavl v0.14.4-0.20201217063301-6b67687bfae9/go.mod h1:eG6hI8RbMxL1nR+nJBykXD//gKjUpKCAT2tvi9V93sA=
268-
github.com/line/tendermint v0.33.10-0.20201215041755-c81005d8468b h1:iIGlUondhI/bf/05Q1XZ2FDgnP3TiktfGURNtfJivsI=
269-
github.com/line/tendermint v0.33.10-0.20201215041755-c81005d8468b/go.mod h1:0yUs9eIuuDq07nQql9BmI30FtYGcEC60Tu5JzB5IezM=
268+
github.com/line/tendermint v0.33.10-0.20210106061749-b557e0c9a76c h1:BLKoXsi7V+y1Neb9FcYgV+9UWKeJM1KLsoJBy/GAg3w=
269+
github.com/line/tendermint v0.33.10-0.20210106061749-b557e0c9a76c/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=

server/start.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func startStandAlone(ctx *Context, appCreator AppCreator) error {
131131

132132
app := appCreator(ctx.Logger, db, traceWriter)
133133

134-
svr, err := server.NewServer(addr, "socket", app)
134+
svr, err := server.NewServer(addr, "grpc", app)
135135
if err != nil {
136136
return fmt.Errorf("error creating listener: %v", err)
137137
}

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

x/genutil/client/cli/init_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestStartStandAlone(t *testing.T) {
110110
require.Nil(t, err)
111111
svrAddr, _, err := server.FreeTCPAddr()
112112
require.Nil(t, err)
113-
svr, err := abciServer.NewServer(svrAddr, "socket", app)
113+
svr, err := abciServer.NewServer(svrAddr, "grpc", app)
114114
require.Nil(t, err, "error creating listener")
115115
svr.SetLogger(logger.With("module", "abci-server"))
116116
svr.Start()

0 commit comments

Comments
 (0)