diff options
author | obscuren <geffobscura@gmail.com> | 2014-05-28 18:05:46 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-05-28 18:05:46 +0800 |
commit | 73761f7af64432b6946934c3b1db646d8e99ef07 (patch) | |
tree | 80ec8827bcbca50b65e1ca8f155b6d348e87fc72 /ethchain/closure.go | |
parent | 1c01e9c0958d2706c522602663da7cfc40a88600 (diff) | |
download | go-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.tar go-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.tar.gz go-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.tar.bz2 go-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.tar.lz go-tangerine-73761f7af64432b6946934c3b1db646d8e99ef07.tar.xz 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.go | 22 |
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 { |