2014-03-20 18:26:07 +02: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 17:50:44 +03:00
|
|
|
type ClosureRef interface {
|
|
|
|
ReturnGas(*big.Int, *big.Int, *State)
|
|
|
|
Address() []byte
|
2014-03-21 19:22:47 +02:00
|
|
|
GetMem(*big.Int) *ethutil.Value
|
2014-05-08 20:09:36 +03:00
|
|
|
SetStore(*big.Int, *ethutil.Value)
|
2014-04-27 17:50:44 +03:00
|
|
|
N() *big.Int
|
2014-03-20 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic inline closure object which implement the 'closure' interface
|
|
|
|
type Closure struct {
|
2014-04-30 15:43:32 +03:00
|
|
|
callee *StateObject
|
|
|
|
object *StateObject
|
2014-04-09 19:27:54 +03:00
|
|
|
Script []byte
|
2014-03-20 20:50:53 +02:00
|
|
|
State *State
|
2014-03-20 18:26:07 +02:00
|
|
|
|
2014-03-21 15:47:55 +02:00
|
|
|
Gas *big.Int
|
2014-04-18 14:41:07 +03:00
|
|
|
Price *big.Int
|
2014-03-20 20:50:53 +02:00
|
|
|
|
2014-03-21 00:17:53 +02:00
|
|
|
Args []byte
|
2014-03-20 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new closure for the given data items
|
2014-05-08 15:20:45 +03:00
|
|
|
func NewClosure(callee, object *StateObject, script []byte, state *State, gas, price *big.Int) *Closure {
|
2014-04-23 12:50:38 +03:00
|
|
|
c := &Closure{callee: callee, object: object, Script: script, State: state, Args: nil}
|
|
|
|
|
|
|
|
// In most cases gas, price and value are pointers to transaction objects
|
|
|
|
// and we don't want the transaction's values to change.
|
|
|
|
c.Gas = new(big.Int).Set(gas)
|
|
|
|
c.Price = new(big.Int).Set(price)
|
|
|
|
|
|
|
|
return c
|
2014-03-20 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retuns the x element in data slice
|
2014-03-21 19:22:47 +02:00
|
|
|
func (c *Closure) GetMem(x *big.Int) *ethutil.Value {
|
2014-03-20 18:26:07 +02:00
|
|
|
m := c.object.GetMem(x)
|
|
|
|
if m == nil {
|
|
|
|
return ethutil.EmptyValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-04-09 19:27:54 +03: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 20:29:57 +03:00
|
|
|
if x.Int64() >= int64(len(c.Script)) || y.Int64() >= int64(len(c.Script)) {
|
2014-04-10 21:40:12 +03:00
|
|
|
return ethutil.NewValue(0)
|
|
|
|
}
|
|
|
|
|
2014-04-09 19:27:54 +03:00
|
|
|
partial := c.Script[x.Int64() : x.Int64()+y.Int64()]
|
|
|
|
|
|
|
|
return ethutil.NewValue(partial)
|
2014-04-09 15:08:18 +03:00
|
|
|
}
|
|
|
|
|
2014-05-08 20:09:36 +03:00
|
|
|
func (c *Closure) SetStorage(x *big.Int, val *ethutil.Value) {
|
|
|
|
c.object.SetStorage(x, val)
|
2014-03-21 19:22:47 +02:00
|
|
|
}
|
|
|
|
|
2014-03-21 15:47:55 +02:00
|
|
|
func (c *Closure) Address() []byte {
|
|
|
|
return c.object.Address()
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:10:18 +03:00
|
|
|
type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack, stateObject *StateObject)
|
2014-04-11 15:28:30 +03:00
|
|
|
|
2014-04-15 23:16:38 +03:00
|
|
|
func (c *Closure) Call(vm *Vm, args []byte, hook DebugHook) ([]byte, error) {
|
2014-03-21 00:17:53 +02:00
|
|
|
c.Args = args
|
2014-03-20 20:50:53 +02:00
|
|
|
|
2014-04-11 15:28:30 +03:00
|
|
|
return vm.RunClosure(c, hook)
|
2014-03-20 20:50:53 +02:00
|
|
|
}
|
|
|
|
|
2014-03-20 18:26:07 +02:00
|
|
|
func (c *Closure) Return(ret []byte) []byte {
|
|
|
|
// Return the remaining gas to the callee
|
|
|
|
// If no callee is present return it to
|
|
|
|
// the origin (i.e. contract or tx)
|
|
|
|
if c.callee != nil {
|
2014-04-20 02:31:01 +03:00
|
|
|
c.callee.ReturnGas(c.Gas, c.Price, c.State)
|
2014-03-20 18:26:07 +02:00
|
|
|
} else {
|
2014-04-20 02:31:01 +03:00
|
|
|
c.object.ReturnGas(c.Gas, c.Price, c.State)
|
2014-03-20 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the Callee interface
|
2014-04-20 02:31:01 +03:00
|
|
|
func (c *Closure) ReturnGas(gas, price *big.Int, state *State) {
|
2014-03-20 18:26:07 +02:00
|
|
|
// Return the gas to the closure
|
2014-03-21 15:47:55 +02:00
|
|
|
c.Gas.Add(c.Gas, gas)
|
|
|
|
}
|
|
|
|
|
2014-04-30 15:43:32 +03:00
|
|
|
func (c *Closure) Object() *StateObject {
|
2014-03-21 15:47:55 +02:00
|
|
|
return c.object
|
2014-03-20 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 15:43:32 +03:00
|
|
|
func (c *Closure) Callee() *StateObject {
|
2014-03-21 15:47:55 +02:00
|
|
|
return c.callee
|
2014-03-20 18:26:07 +02:00
|
|
|
}
|
2014-04-27 17:50:44 +03:00
|
|
|
|
|
|
|
func (c *Closure) N() *big.Int {
|
|
|
|
return c.object.N()
|
|
|
|
}
|