59 lines
975 B
Go
59 lines
975 B
Go
package types
|
|
|
|
import ()
|
|
|
|
// 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
|
|
}
|