-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
gas.go
56 lines (45 loc) · 964 Bytes
/
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
package types
// Gas measured by the SDK
type Gas = int64
// Error thrown when out of gas
type ErrorOutOfGas struct {
Descriptor string
}
// GasMeter interface to track gas consumption
type GasMeter interface {
GasConsumed() Gas
ConsumeGas(amount Gas, descriptor string)
}
type basicGasMeter struct {
limit Gas
consumed Gas
}
func NewGasMeter(limit Gas) GasMeter {
return &basicGasMeter{
limit: limit,
consumed: 0,
}
}
func (g *basicGasMeter) GasConsumed() Gas {
return g.consumed
}
func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) {
g.consumed += amount
if g.consumed > g.limit {
panic(ErrorOutOfGas{descriptor})
}
}
type infiniteGasMeter struct {
consumed Gas
}
func NewInfiniteGasMeter() GasMeter {
return &infiniteGasMeter{
consumed: 0,
}
}
func (g *infiniteGasMeter) GasConsumed() Gas {
return g.consumed
}
func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) {
g.consumed += amount
}