@@ -20,6 +20,12 @@ const (
20
20
// Gas measured by the SDK
21
21
type Gas = uint64
22
22
23
+ // ErrorNegativeGasConsumed defines an error thrown when the amount of gas refunded results in a
24
+ // negative gas consumed amount.
25
+ type ErrorNegativeGasConsumed struct {
26
+ Descriptor string
27
+ }
28
+
23
29
// ErrorOutOfGas defines an error thrown when an action results in out of gas.
24
30
type ErrorOutOfGas struct {
25
31
Descriptor string
@@ -37,6 +43,7 @@ type GasMeter interface {
37
43
GasConsumedToLimit () Gas
38
44
Limit () Gas
39
45
ConsumeGas (amount Gas , descriptor string )
46
+ RefundGas (amount Gas , descriptor string )
40
47
IsPastLimit () bool
41
48
IsOutOfGas () bool
42
49
String () string
@@ -91,7 +98,20 @@ func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) {
91
98
if g .consumed > g .limit {
92
99
panic (ErrorOutOfGas {descriptor })
93
100
}
101
+ }
102
+
103
+ // RefundGas will deduct the given amount from the gas consumed. If the amount is greater than the
104
+ // gas consumed, the function will panic.
105
+ //
106
+ // Use case: This functionality enables refunding gas to the transaction or block gas pools so that
107
+ // EVM-compatible chains can fully support the go-ethereum StateDb interface.
108
+ // See https://github.com/cosmos/cosmos-sdk/pull/9403 for reference.
109
+ func (g * basicGasMeter ) RefundGas (amount Gas , descriptor string ) {
110
+ if g .consumed < amount {
111
+ panic (ErrorNegativeGasConsumed {Descriptor : descriptor })
112
+ }
94
113
114
+ g .consumed -= amount
95
115
}
96
116
97
117
func (g * basicGasMeter ) IsPastLimit () bool {
@@ -138,6 +158,20 @@ func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) {
138
158
}
139
159
}
140
160
161
+ // RefundGas will deduct the given amount from the gas consumed. If the amount is greater than the
162
+ // gas consumed, the function will panic.
163
+ //
164
+ // Use case: This functionality enables refunding gas to the trasaction or block gas pools so that
165
+ // EVM-compatible chains can fully support the go-ethereum StateDb interface.
166
+ // See https://github.com/cosmos/cosmos-sdk/pull/9403 for reference.
167
+ func (g * infiniteGasMeter ) RefundGas (amount Gas , descriptor string ) {
168
+ if g .consumed < amount {
169
+ panic (ErrorNegativeGasConsumed {Descriptor : descriptor })
170
+ }
171
+
172
+ g .consumed -= amount
173
+ }
174
+
141
175
func (g * infiniteGasMeter ) IsPastLimit () bool {
142
176
return false
143
177
}
0 commit comments