-
Notifications
You must be signed in to change notification settings - Fork 3
/
gas.go
75 lines (58 loc) · 1.85 KB
/
gas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package tracing
import (
"fmt"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type GasTrace struct {
Gas storetypes.Gas
Descriptor string
Refund bool
Offset storetypes.Gas
}
// NewGasTrace constructor
func NewGasTrace(amount storetypes.Gas, descriptor string, refund bool, consumed storetypes.Gas) GasTrace {
return GasTrace{Gas: amount, Descriptor: descriptor, Refund: refund, Offset: consumed}
}
func (g GasTrace) String() string {
return fmt.Sprintf("%d, %s, refund: %v", g.Gas, g.Descriptor, g.Refund)
}
var _ sdk.GasMeter = &TraceGasMeter{}
// TraceGasMeter is a decorator to the sdk GasMeter that catuptures all gas usage and refunds
type TraceGasMeter struct {
o sdk.GasMeter
traces []GasTrace
}
// NewTraceGasMeter constructor
func NewTraceGasMeter(o sdk.GasMeter) *TraceGasMeter {
return &TraceGasMeter{o: o, traces: make([]GasTrace, 0)}
}
func (t TraceGasMeter) GasConsumed() storetypes.Gas {
return t.o.GasConsumed()
}
func (t TraceGasMeter) GasConsumedToLimit() storetypes.Gas {
return t.o.GasConsumedToLimit()
}
func (t TraceGasMeter) Limit() storetypes.Gas {
return t.o.Limit()
}
func (t *TraceGasMeter) ConsumeGas(amount storetypes.Gas, descriptor string) {
t.traces = append(t.traces, NewGasTrace(amount, descriptor, false, t.o.GasConsumed()))
t.o.ConsumeGas(amount, descriptor)
}
func (t *TraceGasMeter) RefundGas(amount storetypes.Gas, descriptor string) {
t.traces = append(t.traces, NewGasTrace(amount, descriptor, true, t.o.GasConsumed()))
t.o.RefundGas(amount, descriptor)
}
func (t TraceGasMeter) IsPastLimit() bool {
return t.o.IsPastLimit()
}
func (t TraceGasMeter) IsOutOfGas() bool {
return t.o.IsOutOfGas()
}
func (t TraceGasMeter) String() string {
return t.o.String()
}
func (t TraceGasMeter) GasRemaining() storetypes.Gas {
return t.o.GasRemaining()
}