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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Callee interface {
|
|
|
|
ReturnGas(*big.Int, *State)
|
2014-03-21 08:47:55 -05:00
|
|
|
Address() []byte
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
2014-04-09 11:27:54 -05:00
|
|
|
type Reference interface {
|
2014-03-20 11:26:07 -05:00
|
|
|
Callee
|
|
|
|
ethutil.RlpEncodable
|
2014-03-21 12:22:47 -05:00
|
|
|
GetMem(*big.Int) *ethutil.Value
|
|
|
|
SetMem(*big.Int, *ethutil.Value)
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic inline closure object which implement the 'closure' interface
|
|
|
|
type Closure struct {
|
|
|
|
callee Callee
|
2014-04-09 11:27:54 -05:00
|
|
|
object Reference
|
|
|
|
Script []byte
|
2014-03-20 13:50:53 -05:00
|
|
|
State *State
|
2014-03-20 11:26:07 -05:00
|
|
|
|
2014-03-21 08:47:55 -05:00
|
|
|
Gas *big.Int
|
|
|
|
Value *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-04-09 11:27:54 -05:00
|
|
|
func NewClosure(callee Callee, object Reference, script []byte, state *State, gas, val *big.Int) *Closure {
|
|
|
|
return &Closure{callee, object, script, state, gas, val, nil}
|
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-03-21 12:22:47 -05:00
|
|
|
func (c *Closure) SetMem(x *big.Int, val *ethutil.Value) {
|
|
|
|
c.object.SetMem(x, val)
|
|
|
|
}
|
|
|
|
|
2014-03-21 08:47:55 -05:00
|
|
|
func (c *Closure) Address() []byte {
|
|
|
|
return c.object.Address()
|
|
|
|
}
|
|
|
|
|
2014-04-11 23:13:42 -05:00
|
|
|
type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack)
|
2014-04-11 07:28:30 -05:00
|
|
|
|
2014-04-15 15:16:38 -05:00
|
|
|
func (c *Closure) Call(vm *Vm, args []byte, hook DebugHook) ([]byte, error) {
|
2014-03-20 17:17:53 -05:00
|
|
|
c.Args = args
|
2014-03-20 13:50:53 -05:00
|
|
|
|
2014-04-11 07:28:30 -05:00
|
|
|
return vm.RunClosure(c, hook)
|
2014-03-20 13:50:53 -05:00
|
|
|
}
|
|
|
|
|
2014-03-20 11:26:07 -05: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-03-21 08:47:55 -05:00
|
|
|
c.callee.ReturnGas(c.Gas, c.State)
|
2014-03-20 11:26:07 -05:00
|
|
|
} else {
|
2014-03-21 08:47:55 -05:00
|
|
|
c.object.ReturnGas(c.Gas, c.State)
|
2014-03-20 11:26:07 -05:00
|
|
|
// TODO incase it's a POST contract we gotta serialise the contract again.
|
|
|
|
// But it's not yet defined
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the Callee interface
|
|
|
|
func (c *Closure) ReturnGas(gas *big.Int, state *State) {
|
|
|
|
// Return the gas to the closure
|
2014-03-21 08:47:55 -05:00
|
|
|
c.Gas.Add(c.Gas, gas)
|
|
|
|
}
|
|
|
|
|
2014-04-09 11:27:54 -05:00
|
|
|
func (c *Closure) Object() Reference {
|
2014-03-21 08:47:55 -05:00
|
|
|
return c.object
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|
|
|
|
|
2014-03-21 08:47:55 -05:00
|
|
|
func (c *Closure) Callee() Callee {
|
|
|
|
return c.callee
|
2014-03-20 11:26:07 -05:00
|
|
|
}
|