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 3 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ require (
replace (
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
github.com/tendermint/iavl => github.com/line/iavl v0.14.4-0.20201217063301-6b67687bfae9
github.com/tendermint/tendermint => github.com/line/tendermint v0.33.10-0.20201215041755-c81005d8468b
github.com/tendermint/tendermint => github.com/line/tendermint v0.33.10-0.20210106061749-b557e0c9a76c
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-b
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/line/iavl v0.14.4-0.20201217063301-6b67687bfae9 h1:n6YHVdTld8D0dAogBUcXTaqhk1ZMTAR9Phy4xdMVWDY=
github.com/line/iavl v0.14.4-0.20201217063301-6b67687bfae9/go.mod h1:eG6hI8RbMxL1nR+nJBykXD//gKjUpKCAT2tvi9V93sA=
github.com/line/tendermint v0.33.10-0.20201215041755-c81005d8468b h1:iIGlUondhI/bf/05Q1XZ2FDgnP3TiktfGURNtfJivsI=
github.com/line/tendermint v0.33.10-0.20201215041755-c81005d8468b/go.mod h1:0yUs9eIuuDq07nQql9BmI30FtYGcEC60Tu5JzB5IezM=
github.com/line/tendermint v0.33.10-0.20210106061749-b557e0c9a76c h1:BLKoXsi7V+y1Neb9FcYgV+9UWKeJM1KLsoJBy/GAg3w=
github.com/line/tendermint v0.33.10-0.20210106061749-b557e0c9a76c/go.mod h1:0yUs9eIuuDq07nQql9BmI30FtYGcEC60Tu5JzB5IezM=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
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
}