Commit 4dc7ee90 authored by obscuren's avatar obscuren

Closure => Context

parent e2d1d832
...@@ -144,19 +144,19 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execu ...@@ -144,19 +144,19 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execu
return core.NewExecution(self, addr, data, gas, price, value) return core.NewExecution(self, addr, data, gas, price, value)
} }
func (self *VMEnv) Call(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
ret, err := exe.Call(addr, caller) ret, err := exe.Call(addr, caller)
self.Gas = exe.Gas self.Gas = exe.Gas
return ret, err return ret, err
} }
func (self *VMEnv) CallCode(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(caller.Address(), data, gas, price, value) exe := self.vm(caller.Address(), data, gas, price, value)
return exe.Call(addr, caller) return exe.Call(addr, caller)
} }
func (self *VMEnv) Create(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) { func (self *VMEnv) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
return exe.Create(caller) return exe.Create(caller)
} }
...@@ -52,19 +52,19 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execu ...@@ -52,19 +52,19 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execu
return core.NewExecution(self, addr, data, gas, price, value) return core.NewExecution(self, addr, data, gas, price, value)
} }
func (self *VMEnv) Call(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
ret, err := exe.Call(addr, caller) ret, err := exe.Call(addr, caller)
self.Gas = exe.Gas self.Gas = exe.Gas
return ret, err return ret, err
} }
func (self *VMEnv) CallCode(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(caller.Address(), data, gas, price, value) exe := self.vm(caller.Address(), data, gas, price, value)
return exe.Call(addr, caller) return exe.Call(addr, caller)
} }
func (self *VMEnv) Create(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) { func (self *VMEnv) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
return exe.Create(caller) return exe.Create(caller)
} }
...@@ -20,26 +20,6 @@ type StateQuery interface { ...@@ -20,26 +20,6 @@ type StateQuery interface {
GetAccount(addr []byte) *state.StateObject GetAccount(addr []byte) *state.StateObject
} }
/*
func AddTestNetFunds(block *types.Block) {
for _, addr := range []string{
"51ba59315b3a95761d0863b05ccc7a7f54703d99",
"e4157b34ea9615cfbde6b4fda419828124b70c78",
"b9c015918bdaba24b4ff057a92a3873d6eb201be",
"6c386a4b26f73c802f34673f7248bb118f97424a",
"cd2a3d9f938e13cd947ec05abc7fe734df8dd826",
"2ef47100e0787b915105fd5e3f4ff6752079d5cb",
"e6716f9544a56c530d868e4bfbacb172315bdead",
"1a26338f0d905e295fccb71fa9ea849ffa12aaf4",
} {
codedAddr := ethutil.Hex2Bytes(addr)
account := block.State().GetAccount(codedAddr)
account.SetBalance(ethutil.Big("1606938044258990275541962092341162602522202993782792835301376")) //ethutil.BigPow(2, 200)
block.State().UpdateStateObject(account)
}
}
*/
func CalcDifficulty(block, parent *types.Block) *big.Int { func CalcDifficulty(block, parent *types.Block) *big.Int {
diff := new(big.Int) diff := new(big.Int)
......
...@@ -24,14 +24,14 @@ func (self *Execution) Addr() []byte { ...@@ -24,14 +24,14 @@ func (self *Execution) Addr() []byte {
return self.address return self.address
} }
func (self *Execution) Call(codeAddr []byte, caller vm.ClosureRef) ([]byte, error) { func (self *Execution) Call(codeAddr []byte, caller vm.ContextRef) ([]byte, error) {
// Retrieve the executing code // Retrieve the executing code
code := self.env.State().GetCode(codeAddr) code := self.env.State().GetCode(codeAddr)
return self.exec(code, codeAddr, caller) return self.exec(code, codeAddr, caller)
} }
func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret []byte, err error) { func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret []byte, err error) {
env := self.env env := self.env
evm := vm.New(env, vm.DebugVmTy) evm := vm.New(env, vm.DebugVmTy)
...@@ -63,7 +63,7 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret ...@@ -63,7 +63,7 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret
return return
} }
func (self *Execution) Create(caller vm.ClosureRef) (ret []byte, err error, account *state.StateObject) { func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
ret, err = self.exec(self.input, nil, caller) ret, err = self.exec(self.input, nil, caller)
account = self.env.State().GetStateObject(self.address) account = self.env.State().GetStateObject(self.address)
......
...@@ -184,7 +184,7 @@ func (self *StateTransition) TransitionState() (ret []byte, err error) { ...@@ -184,7 +184,7 @@ func (self *StateTransition) TransitionState() (ret []byte, err error) {
} }
vmenv := self.VmEnv() vmenv := self.VmEnv()
var ref vm.ClosureRef var ref vm.ContextRef
if MessageCreatesContract(msg) { if MessageCreatesContract(msg) {
contract := MakeContract(msg, self.state) contract := MakeContract(msg, self.state)
ret, err, ref = vmenv.Create(sender, contract.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value) ret, err, ref = vmenv.Create(sender, contract.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value)
......
...@@ -46,16 +46,16 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *Execution ...@@ -46,16 +46,16 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *Execution
return NewExecution(self, addr, data, gas, price, value) return NewExecution(self, addr, data, gas, price, value)
} }
func (self *VMEnv) Call(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) Call(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
return exe.Call(addr, me) return exe.Call(addr, me)
} }
func (self *VMEnv) CallCode(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) CallCode(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(me.Address(), data, gas, price, value) exe := self.vm(me.Address(), data, gas, price, value)
return exe.Call(addr, me) return exe.Call(addr, me)
} }
func (self *VMEnv) Create(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) { func (self *VMEnv) Create(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
return exe.Create(me) return exe.Create(me)
} }
...@@ -74,19 +74,19 @@ func (self *Env) vm(addr, data []byte, gas, price, value *big.Int) *core.Executi ...@@ -74,19 +74,19 @@ func (self *Env) vm(addr, data []byte, gas, price, value *big.Int) *core.Executi
return exec return exec
} }
func (self *Env) Call(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *Env) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
ret, err := exe.Call(addr, caller) ret, err := exe.Call(addr, caller)
self.Gas = exe.Gas self.Gas = exe.Gas
return ret, err return ret, err
} }
func (self *Env) CallCode(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *Env) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(caller.Address(), data, gas, price, value) exe := self.vm(caller.Address(), data, gas, price, value)
return exe.Call(addr, caller) return exe.Call(addr, caller)
} }
func (self *Env) Create(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) { func (self *Env) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
return exe.Create(caller) return exe.Create(caller)
} }
......
...@@ -8,15 +8,15 @@ import ( ...@@ -8,15 +8,15 @@ import (
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
) )
type ClosureRef interface { type ContextRef interface {
ReturnGas(*big.Int, *big.Int) ReturnGas(*big.Int, *big.Int)
Address() []byte Address() []byte
SetCode([]byte) SetCode([]byte)
} }
type Closure struct { type Context struct {
caller ClosureRef caller ContextRef
object ClosureRef object ContextRef
Code []byte Code []byte
message *state.Message message *state.Message
...@@ -25,9 +25,9 @@ type Closure struct { ...@@ -25,9 +25,9 @@ type Closure struct {
Args []byte Args []byte
} }
// Create a new closure for the given data items // Create a new context for the given data items
func NewClosure(msg *state.Message, caller ClosureRef, object ClosureRef, code []byte, gas, price *big.Int) *Closure { func NewContext(msg *state.Message, caller ContextRef, object ContextRef, code []byte, gas, price *big.Int) *Context {
c := &Closure{message: msg, caller: caller, object: object, Code: code, Args: nil} c := &Context{message: msg, caller: caller, object: object, Code: code, Args: nil}
// Gas should be a pointer so it can safely be reduced through the run // Gas should be a pointer so it can safely be reduced through the run
// This pointer will be off the state transition // This pointer will be off the state transition
...@@ -40,11 +40,11 @@ func NewClosure(msg *state.Message, caller ClosureRef, object ClosureRef, code [ ...@@ -40,11 +40,11 @@ func NewClosure(msg *state.Message, caller ClosureRef, object ClosureRef, code [
return c return c
} }
func (c *Closure) GetOp(x uint64) OpCode { func (c *Context) GetOp(x uint64) OpCode {
return OpCode(c.GetByte(x)) return OpCode(c.GetByte(x))
} }
func (c *Closure) GetByte(x uint64) byte { func (c *Context) GetByte(x uint64) byte {
if x < uint64(len(c.Code)) { if x < uint64(len(c.Code)) {
return c.Code[x] return c.Code[x]
} }
...@@ -52,18 +52,18 @@ func (c *Closure) GetByte(x uint64) byte { ...@@ -52,18 +52,18 @@ func (c *Closure) GetByte(x uint64) byte {
return 0 return 0
} }
func (c *Closure) GetBytes(x, y int) []byte { func (c *Context) GetBytes(x, y int) []byte {
return c.GetRangeValue(uint64(x), uint64(y)) return c.GetRangeValue(uint64(x), uint64(y))
} }
func (c *Closure) GetRangeValue(x, size uint64) []byte { func (c *Context) GetRangeValue(x, size uint64) []byte {
x = uint64(math.Min(float64(x), float64(len(c.Code)))) x = uint64(math.Min(float64(x), float64(len(c.Code))))
y := uint64(math.Min(float64(x+size), float64(len(c.Code)))) y := uint64(math.Min(float64(x+size), float64(len(c.Code))))
return ethutil.LeftPadBytes(c.Code[x:y], int(size)) return ethutil.LeftPadBytes(c.Code[x:y], int(size))
} }
func (c *Closure) Return(ret []byte) []byte { func (c *Context) Return(ret []byte) []byte {
// Return the remaining gas to the caller // Return the remaining gas to the caller
c.caller.ReturnGas(c.Gas, c.Price) c.caller.ReturnGas(c.Gas, c.Price)
...@@ -73,7 +73,7 @@ func (c *Closure) Return(ret []byte) []byte { ...@@ -73,7 +73,7 @@ func (c *Closure) Return(ret []byte) []byte {
/* /*
* Gas functions * Gas functions
*/ */
func (c *Closure) UseGas(gas *big.Int) bool { func (c *Context) UseGas(gas *big.Int) bool {
if c.Gas.Cmp(gas) < 0 { if c.Gas.Cmp(gas) < 0 {
return false return false
} }
...@@ -86,8 +86,8 @@ func (c *Closure) UseGas(gas *big.Int) bool { ...@@ -86,8 +86,8 @@ func (c *Closure) UseGas(gas *big.Int) bool {
} }
// Implement the caller interface // Implement the caller interface
func (c *Closure) ReturnGas(gas, price *big.Int) { func (c *Context) ReturnGas(gas, price *big.Int) {
// Return the gas to the closure // Return the gas to the context
c.Gas.Add(c.Gas, gas) c.Gas.Add(c.Gas, gas)
c.UsedGas.Sub(c.UsedGas, gas) c.UsedGas.Sub(c.UsedGas, gas)
} }
...@@ -95,10 +95,10 @@ func (c *Closure) ReturnGas(gas, price *big.Int) { ...@@ -95,10 +95,10 @@ func (c *Closure) ReturnGas(gas, price *big.Int) {
/* /*
* Set / Get * Set / Get
*/ */
func (c *Closure) Address() []byte { func (c *Context) Address() []byte {
return c.object.Address() return c.object.Address()
} }
func (self *Closure) SetCode(code []byte) { func (self *Context) SetCode(code []byte) {
self.Code = code self.Code = code
} }
...@@ -26,9 +26,9 @@ type Environment interface { ...@@ -26,9 +26,9 @@ type Environment interface {
Depth() int Depth() int
SetDepth(i int) SetDepth(i int)
Call(me ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) Call(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
CallCode(me ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) CallCode(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
Create(me ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, ClosureRef) Create(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
} }
type Object interface { type Object interface {
......
...@@ -4,7 +4,7 @@ import "math/big" ...@@ -4,7 +4,7 @@ import "math/big"
type VirtualMachine interface { type VirtualMachine interface {
Env() Environment Env() Environment
Run(me, caller ClosureRef, code []byte, value, gas, price *big.Int, data []byte) ([]byte, error) Run(me, caller ContextRef, code []byte, value, gas, price *big.Int, data []byte) ([]byte, error)
Printf(string, ...interface{}) VirtualMachine Printf(string, ...interface{}) VirtualMachine
Endl() VirtualMachine Endl() VirtualMachine
} }
...@@ -20,7 +20,7 @@ func New(env Environment, typ Type) VirtualMachine { ...@@ -20,7 +20,7 @@ func New(env Environment, typ Type) VirtualMachine {
} }
} }
func (self *Vm) Run(me, caller ClosureRef, code []byte, value, gas, price *big.Int, data []byte) (ret []byte, err error) { func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.Int, data []byte) (ret []byte, err error) {
panic("not implemented") panic("not implemented")
} }
......
This diff is collapsed.
...@@ -50,16 +50,16 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execu ...@@ -50,16 +50,16 @@ func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execu
return core.NewExecution(self, addr, data, gas, price, value) return core.NewExecution(self, addr, data, gas, price, value)
} }
func (self *VMEnv) Call(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) Call(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
return exe.Call(addr, me) return exe.Call(addr, me)
} }
func (self *VMEnv) CallCode(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { func (self *VMEnv) CallCode(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(me.Address(), data, gas, price, value) exe := self.vm(me.Address(), data, gas, price, value)
return exe.Call(addr, me) return exe.Call(addr, me)
} }
func (self *VMEnv) Create(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) { func (self *VMEnv) Create(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
exe := self.vm(addr, data, gas, price, value) exe := self.vm(addr, data, gas, price, value)
return exe.Create(me) return exe.Create(me)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment