2014-03-20 16:26:07 +00: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 13:47:55 +00:00
|
|
|
Address() []byte
|
2014-03-20 16:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClosureBody interface {
|
|
|
|
Callee
|
|
|
|
ethutil.RlpEncodable
|
2014-03-21 17:22:47 +00:00
|
|
|
GetMem(*big.Int) *ethutil.Value
|
|
|
|
SetMem(*big.Int, *ethutil.Value)
|
2014-03-20 16:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic inline closure object which implement the 'closure' interface
|
|
|
|
type Closure struct {
|
|
|
|
callee Callee
|
|
|
|
object ClosureBody
|
2014-03-20 18:50:53 +00:00
|
|
|
State *State
|
2014-03-20 16:26:07 +00:00
|
|
|
|
2014-03-21 13:47:55 +00:00
|
|
|
Gas *big.Int
|
|
|
|
Value *big.Int
|
2014-03-20 18:50:53 +00:00
|
|
|
|
2014-03-20 22:17:53 +00:00
|
|
|
Args []byte
|
2014-03-20 16:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new closure for the given data items
|
|
|
|
func NewClosure(callee Callee, object ClosureBody, state *State, gas, val *big.Int) *Closure {
|
2014-03-20 18:50:53 +00:00
|
|
|
return &Closure{callee, object, state, gas, val, nil}
|
2014-03-20 16:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retuns the x element in data slice
|
2014-03-21 17:22:47 +00:00
|
|
|
func (c *Closure) GetMem(x *big.Int) *ethutil.Value {
|
2014-03-20 16:26:07 +00:00
|
|
|
m := c.object.GetMem(x)
|
|
|
|
if m == nil {
|
|
|
|
return ethutil.EmptyValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-03-21 17:22:47 +00:00
|
|
|
func (c *Closure) SetMem(x *big.Int, val *ethutil.Value) {
|
|
|
|
c.object.SetMem(x, val)
|
|
|
|
}
|
|
|
|
|
2014-03-21 13:47:55 +00:00
|
|
|
func (c *Closure) Address() []byte {
|
|
|
|
return c.object.Address()
|
|
|
|
}
|
|
|
|
|
2014-03-20 18:50:53 +00:00
|
|
|
func (c *Closure) Call(vm *Vm, args []byte) []byte {
|
2014-03-20 22:17:53 +00:00
|
|
|
c.Args = args
|
2014-03-20 18:50:53 +00:00
|
|
|
|
|
|
|
return vm.RunClosure(c)
|
|
|
|
}
|
|
|
|
|
2014-03-20 16:26:07 +00: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 13:47:55 +00:00
|
|
|
c.callee.ReturnGas(c.Gas, c.State)
|
2014-03-20 16:26:07 +00:00
|
|
|
} else {
|
2014-03-21 13:47:55 +00:00
|
|
|
c.object.ReturnGas(c.Gas, c.State)
|
2014-03-20 16:26:07 +00: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 13:47:55 +00:00
|
|
|
c.Gas.Add(c.Gas, gas)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Closure) Object() ClosureBody {
|
|
|
|
return c.object
|
2014-03-20 16:26:07 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 13:47:55 +00:00
|
|
|
func (c *Closure) Callee() Callee {
|
|
|
|
return c.callee
|
2014-03-20 16:26:07 +00:00
|
|
|
}
|