aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/closure.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-05-28 18:05:46 +0800
committerobscuren <geffobscura@gmail.com>2014-05-28 18:05:46 +0800
commit73761f7af64432b6946934c3b1db646d8e99ef07 (patch)
tree80ec8827bcbca50b65e1ca8f155b6d348e87fc72 /ethchain/closure.go
parent1c01e9c0958d2706c522602663da7cfc40a88600 (diff)
downloadgo-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.tar.gz
go-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.tar.zst
go-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.zip
Closure call now returns the total usage as well
* Return the used gas value based on the UseGas and ReturnGas
Diffstat (limited to 'ethchain/closure.go')
-rw-r--r--ethchain/closure.go22
1 files changed, 18 insertions, 4 deletions
diff --git a/ethchain/closure.go b/ethchain/closure.go
index c935ed50a..f2b46e461 100644
--- a/ethchain/closure.go
+++ b/ethchain/closure.go
@@ -22,8 +22,7 @@ type Closure struct {
Script []byte
State *State
- Gas *big.Int
- Price *big.Int
+ Gas, UsedGas, Price *big.Int
Args []byte
}
@@ -74,10 +73,12 @@ func (c *Closure) Address() []byte {
type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack, stateObject *StateObject) bool
-func (c *Closure) Call(vm *Vm, args []byte, hook DebugHook) ([]byte, error) {
+func (c *Closure) Call(vm *Vm, args []byte, hook DebugHook) ([]byte, *big.Int, error) {
c.Args = args
- return vm.RunClosure(c, hook)
+ ret, err := vm.RunClosure(c, hook)
+
+ return ret, c.UsedGas, err
}
func (c *Closure) Return(ret []byte) []byte {
@@ -93,10 +94,23 @@ func (c *Closure) Return(ret []byte) []byte {
return ret
}
+func (c *Closure) UseGas(gas *big.Int) bool {
+ if c.Gas.Cmp(gas) < 0 {
+ return false
+ }
+
+ // Sub the amount of gas from the remaining
+ c.Gas.Sub(c.Gas, gas)
+ c.UsedGas.Add(c.UsedGas, gas)
+
+ return true
+}
+
// Implement the Callee interface
func (c *Closure) ReturnGas(gas, price *big.Int, state *State) {
// Return the gas to the closure
c.Gas.Add(c.Gas, gas)
+ c.UsedGas.Sub(c.UsedGas, gas)
}
func (c *Closure) Object() *StateObject {