2018-05-07 10:48:12 -07:00
|
|
|
package types
|
|
|
|
|
2018-05-07 12:49:11 -07:00
|
|
|
// Gas measured by the SDK
|
2018-05-07 11:49:34 -07:00
|
|
|
type Gas = int64
|
2018-05-07 10:48:12 -07:00
|
|
|
|
2018-05-08 08:34:09 -07:00
|
|
|
// Error thrown when out of gas
|
|
|
|
type ErrorOutOfGas struct {
|
|
|
|
Descriptor string
|
|
|
|
}
|
|
|
|
|
2018-05-07 12:49:11 -07:00
|
|
|
// GasMeter interface to track gas consumption
|
2018-05-07 10:48:12 -07:00
|
|
|
type GasMeter interface {
|
2018-05-07 11:49:34 -07:00
|
|
|
GasConsumed() Gas
|
2018-05-08 08:34:09 -07:00
|
|
|
ConsumeGas(amount Gas, descriptor string)
|
2018-05-07 10:48:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type basicGasMeter struct {
|
|
|
|
limit Gas
|
|
|
|
consumed Gas
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGasMeter(limit Gas) GasMeter {
|
|
|
|
return &basicGasMeter{
|
|
|
|
limit: limit,
|
|
|
|
consumed: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 11:49:34 -07:00
|
|
|
func (g *basicGasMeter) GasConsumed() Gas {
|
|
|
|
return g.consumed
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:34:09 -07:00
|
|
|
func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) {
|
2018-05-07 10:48:12 -07:00
|
|
|
g.consumed += amount
|
2018-05-08 08:34:09 -07:00
|
|
|
if g.consumed > g.limit {
|
|
|
|
panic(ErrorOutOfGas{descriptor})
|
|
|
|
}
|
2018-05-07 10:48:12 -07:00
|
|
|
}
|
2018-05-15 17:31:52 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|