cosmos-sdk/types/gas.go

41 lines
676 B
Go
Raw Normal View History

2018-05-07 10:48:12 -07:00
package types
import ()
2018-05-07 12:49:11 -07:00
// Gas measured by the SDK
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 {
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,
}
}
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
}