@@ -26,6 +26,7 @@ import (
2626 "github.com/ethereum/go-ethereum/params"
2727)
2828
29+ // Execution is the execution environment for the given call or create action.
2930type Execution struct {
3031 env vm.Environment
3132 address * common.Address
@@ -35,19 +36,25 @@ type Execution struct {
3536 Gas , price , value * big.Int
3637}
3738
39+ // NewExecution returns a new execution environment that handles all calling
40+ // and creation logic defined by the YP.
3841func NewExecution (env vm.Environment , address * common.Address , input []byte , gas , gasPrice , value * big.Int ) * Execution {
3942 exe := & Execution {env : env , address : address , input : input , Gas : gas , price : gasPrice , value : value }
4043 exe .evm = vm .NewVm (env )
4144 return exe
4245}
4346
47+ // Call executes within the given context
4448func (self * Execution ) Call (codeAddr common.Address , caller vm.ContextRef ) ([]byte , error ) {
4549 // Retrieve the executing code
4650 code := self .env .State ().GetCode (codeAddr )
4751
4852 return self .exec (& codeAddr , code , caller )
4953}
5054
55+ // Create creates a new contract and runs the initialisation procedure of the
56+ // contract. This returns the returned code for the contract and is stored
57+ // elsewhere.
5158func (self * Execution ) Create (caller vm.ContextRef ) (ret []byte , err error , account * state.StateObject ) {
5259 // Input must be nil for create
5360 code := self .input
@@ -63,16 +70,24 @@ func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, acco
6370 return
6471}
6572
73+ // exec executes the given code and executes within the contextAddr context.
6674func (self * Execution ) exec (contextAddr * common.Address , code []byte , caller vm.ContextRef ) (ret []byte , err error ) {
6775 env := self .env
6876 evm := self .evm
77+ // Depth check execution. Fail if we're trying to execute above the
78+ // limit.
6979 if env .Depth () > int (params .CallCreateDepth .Int64 ()) {
7080 caller .ReturnGas (self .Gas , self .price )
7181
7282 return nil , vm .DepthError
7383 }
7484
75- vsnapshot := env .State ().Copy ()
85+ if ! env .CanTransfer (env .State ().GetStateObject (caller .Address ()), self .value ) {
86+ caller .ReturnGas (self .Gas , self .price )
87+
88+ return nil , ValueTransferErr ("insufficient funds to transfer value. Req %v, has %v" , self .value , env .State ().GetBalance (caller .Address ()))
89+ }
90+
7691 var createAccount bool
7792 if self .address == nil {
7893 // Generate a new address
@@ -95,15 +110,7 @@ func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.
95110 } else {
96111 to = env .State ().GetOrNewStateObject (* self .address )
97112 }
98-
99- err = env .Transfer (from , to , self .value )
100- if err != nil {
101- env .State ().Set (vsnapshot )
102-
103- caller .ReturnGas (self .Gas , self .price )
104-
105- return nil , ValueTransferErr ("insufficient funds to transfer value. Req %v, has %v" , self .value , from .Balance ())
106- }
113+ vm .Transfer (from , to , self .value )
107114
108115 context := vm .NewContext (caller , to , self .value , self .Gas , self .price )
109116 context .SetCallCode (contextAddr , code )
0 commit comments