@@ -41,6 +41,11 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
41
41
42
42
newCtx = SetGasMeter (simulate , ctx , gasTx .GetGas ())
43
43
44
+ err = validateGasWanted (newCtx )
45
+ if err != nil {
46
+ return newCtx , sdkerrors .Wrap (sdkerrors .ErrOutOfGas , err .Error ())
47
+ }
48
+
44
49
// Decorator will catch an OutOfGasPanic caused in the next antehandler
45
50
// AnteHandlers must have their own defer/recover in order for the BaseApp
46
51
// 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 {
74
79
75
80
return ctx .WithGasMeter (sdk .NewGasMeter (gasLimit ))
76
81
}
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