go-ethereum/ethchain/closure.go

122 lines
2.6 KiB
Go
Raw Normal View History

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-07-02 00:59:18 +03:00
GetStorage(*big.Int) *ethutil.Value
2014-05-29 00:16:54 +03:00
SetStorage(*big.Int, *ethutil.Value)
2014-04-27 17:50:44 +03:00
N() *big.Int
}
// Basic inline closure object which implement the 'closure' interface
type Closure struct {
2014-06-13 17:06:27 +03:00
caller ClosureRef
object *StateObject
2014-04-09 19:27:54 +03:00
Script []byte
State *State
Gas, UsedGas, Price *big.Int
Args []byte
}
// Create a new closure for the given data items
2014-06-13 17:06:27 +03: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-06-13 17:06:27 +03: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
// and we don't want the transaction's values to change.
c.Price = new(big.Int).Set(price)
2014-05-28 13:39:34 +03:00
c.UsedGas = new(big.Int)
return c
}
// Retuns the x element in data slice
2014-07-02 00:59:18 +03:00
func (c *Closure) GetStorage(x *big.Int) *ethutil.Value {
m := c.object.GetStorage(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
}
func (c *Closure) SetStorage(x *big.Int, val *ethutil.Value) {
c.object.SetStorage(x, val)
}
func (c *Closure) Address() []byte {
return c.object.Address()
}
2014-07-02 02:04:21 +03:00
func (c *Closure) Call(vm *Vm, args []byte) ([]byte, *big.Int, error) {
c.Args = args
2014-07-02 02:04:21 +03:00
ret, err := vm.RunClosure(c)
return ret, c.UsedGas, err
}
func (c *Closure) Return(ret []byte) []byte {
2014-06-13 17:06:27 +03:00
// Return the remaining gas to the caller
c.caller.ReturnGas(c.Gas, c.Price, c.State)
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
}
2014-06-13 17:06:27 +03:00
// Implement the caller interface
2014-04-20 02:31:01 +03:00
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 {
return c.object
}
2014-06-13 17:06:27 +03:00
func (c *Closure) Caller() ClosureRef {
return c.caller
}
2014-04-27 17:50:44 +03:00
func (c *Closure) N() *big.Int {
return c.object.N()
}