2014-03-20 11:26:07 -05:00
|
|
|
package ethchain
|
|
|
|
|
|
|
|
// TODO Re write VM to use values instead of big integers?
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
2014-04-27 09:50:44 -05:00
|
|
|
type ClosureRef interface {
|
|
|
|
ReturnGas(*big.Int, *big.Int, *State)
|
|
|
|
Address() []byte
|
2014-03-21 12:22:47 -05:00
|
|
|
GetMem(*big.Int) *ethutil.Value
|
2014-05-28 16:16:54 -05:00
|
|
|
SetStorage(*big.Int, *ethutil.Value)
|
2014-04-27 09:50:44 -05:00
|
|
|
N() *big.Int
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic inline closure object which implement the 'closure' interface
|
|
|
|
type Closure struct {
|
2014-06-13 09:06:27 -05:00
|
|
|
caller ClosureRef
|
2014-04-30 07:43:32 -05:00
|
|
|
object *StateObject
|
2014-04-09 11:27:54 -05:00
|
|
|
Script []byte
|
2014-03-20 13:50:53 -05:00
|
|
|
State *State
|
2014-03-20 11:26:07 -05:00
|
|
|
|
2014-05-28 05:05:46 -05:00
|
|
|
Gas, UsedGas, Price *big.Int
|
2014-03-20 13:50:53 -05:00
|
|
|
|
2014-03-20 17:17:53 -05:00
|
|
|
Args []byte
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new closure for the given data items
|
2014-06-13 09:06:27 -05:00
|
|
|
func NewClosure(caller ClosureRef, object *StateObject, script []byte, state *State, gas, price *big.Int) *Closure {
|
|
|
|
c := &Closure{caller: caller, object: object, Script: script, State: state, Args: nil}
|
2014-04-23 04:50:38 -05:00
|
|
|
|
2014-06-13 09:06:27 -05:00
|
|
|
// Gas should be a pointer so it can safely be reduced through the run
|
|
|
|
// This pointer will be off the state transition
|
|
|
|
c.Gas = gas //new(big.Int).Set(gas)
|
|
|
|
// In most cases price and value are pointers to transaction objects
|
2014-04-23 04:50:38 -05:00
|
|
|
// and we don't want the transaction's values to change.
|
|
|
|
c.Price = new(big.Int).Set(price)
|
2014-05-28 05:39:34 -05:00
|
|
|
c.UsedGas = new(big.Int)
|
2014-04-23 04:50:38 -05:00
|
|
|
|
|
|
|
return c
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retuns the x element in data slice
|
2014-03-21 12:22:47 -05:00
|
|
|
func (c *Closure) GetMem(x *big.Int) *ethutil.Value {
|
2014-03-20 11:26:07 -05:00
|
|
|
m := c.object.GetMem(x)
|
|
|
|
if m == nil {
|
|
|
|
return ethutil.EmptyValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-04-09 11:27:54 -05:00
|
|
|
func (c *Closure) Get(x *big.Int) *ethutil.Value {
|
|
|
|
return c.Gets(x, big.NewInt(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Closure) Gets(x, y *big.Int) *ethutil.Value {
|
2014-04-11 12:29:57 -05:00
|
|
|
if x.Int64() >= int64(len(c.Script)) || y.Int64() >= int64(len(c.Script)) {
|
2014-04-10 13:40:12 -05:00
|
|
|
return ethutil.NewValue(0)
|
|
|
|
}
|
|
|
|
|
2014-04-09 11:27:54 -05:00
|
|
|
partial := c.Script[x.Int64() : x.Int64()+y.Int64()]
|
|
|
|
|
|
|
|
return ethutil.NewValue(partial)
|
2014-04-09 07:08:18 -05:00
|
|
|
}
|
|
|
|
|
2014-05-08 12:09:36 -05:00
|
|
|
func (c *Closure) SetStorage(x *big.Int, val *ethutil.Value) {
|
|
|
|
c.object.SetStorage(x, val)
|
2014-03-21 12:22:47 -05:00
|
|
|
}
|
|
|
|
|
2014-03-21 08:47:55 -05:00
|
|
|
func (c *Closure) Address() []byte {
|
|
|
|
return c.object.Address()
|
|
|
|
}
|
|
|
|
|
2014-05-27 06:32:31 -05:00
|
|
|
type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack, stateObject *StateObject) bool
|
2014-04-11 07:28:30 -05:00
|
|
|
|
2014-05-28 05:05:46 -05:00
|
|
|
func (c *Closure) Call(vm *Vm, args []byte, hook DebugHook) ([]byte, *big.Int, error) {
|
2014-03-20 17:17:53 -05:00
|
|
|
c.Args = args
|
2014-03-20 13:50:53 -05:00
|
|
|
|
2014-05-28 05:05:46 -05:00
|
|
|
ret, err := vm.RunClosure(c, hook)
|
|
|
|
|
|
|
|
return ret, c.UsedGas, err
|
2014-03-20 13:50:53 -05:00
|
|
|
}
|
|
|
|
|
2014-03-20 11:26:07 -05:00
|
|
|
func (c *Closure) Return(ret []byte) []byte {
|
2014-06-13 09:06:27 -05:00
|
|
|
// Return the remaining gas to the caller
|
|
|
|
// If no caller is present return it to
|
2014-03-20 11:26:07 -05:00
|
|
|
// the origin (i.e. contract or tx)
|
2014-06-13 09:06:27 -05:00
|
|
|
if c.caller != nil {
|
|
|
|
c.caller.ReturnGas(c.Gas, c.Price, c.State)
|
2014-03-20 11:26:07 -05:00
|
|
|
} else {
|
2014-04-19 18:31:01 -05:00
|
|
|
c.object.ReturnGas(c.Gas, c.Price, c.State)
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2014-05-28 05:05:46 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:06:27 -05:00
|
|
|
// Implement the caller interface
|
2014-04-19 18:31:01 -05:00
|
|
|
func (c *Closure) ReturnGas(gas, price *big.Int, state *State) {
|
2014-03-20 11:26:07 -05:00
|
|
|
// Return the gas to the closure
|
2014-03-21 08:47:55 -05:00
|
|
|
c.Gas.Add(c.Gas, gas)
|
2014-05-28 05:05:46 -05:00
|
|
|
c.UsedGas.Sub(c.UsedGas, gas)
|
2014-03-21 08:47:55 -05:00
|
|
|
}
|
|
|
|
|
2014-04-30 07:43:32 -05:00
|
|
|
func (c *Closure) Object() *StateObject {
|
2014-03-21 08:47:55 -05:00
|
|
|
return c.object
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
2014-06-13 09:06:27 -05:00
|
|
|
func (c *Closure) Caller() ClosureRef {
|
|
|
|
return c.caller
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
2014-04-27 09:50:44 -05:00
|
|
|
|
|
|
|
func (c *Closure) N() *big.Int {
|
|
|
|
return c.object.N()
|
|
|
|
}
|