@@ -44,7 +44,6 @@ func DelegateCall(env vm.Environment, caller vm.ContractRef, addr common.Address
4444 originAddr := env .Origin ()
4545 callerValue := caller .Value ()
4646 ret , _ , err = execDelegateCall (env , caller , & originAddr , & callerAddr , & addr , input , env .Db ().GetCode (addr ), gas , gasPrice , callerValue )
47- caller .SetAddress (callerAddr )
4847 return ret , err
4948}
5049
@@ -78,15 +77,15 @@ func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.A
7877
7978 var createAccount bool
8079 if address == nil {
81- // Generate a new address
80+ // Create a new account on the state
8281 nonce := env .Db ().GetNonce (caller .Address ())
8382 env .Db ().SetNonce (caller .Address (), nonce + 1 )
8483 addr = crypto .CreateAddress (caller .Address (), nonce )
8584 address = & addr
8685 createAccount = true
8786 }
88- snapshotPreTransfer := env .MakeSnapshot ()
8987
88+ snapshotPreTransfer := env .MakeSnapshot ()
9089 var (
9190 from = env .Db ().GetAccount (caller .Address ())
9291 to vm.Account
@@ -101,24 +100,38 @@ func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.A
101100 }
102101 }
103102 env .Transfer (from , to , value )
104- snapshotPostTransfer := env .MakeSnapshot ()
105103
104+ // initialise a new contract and set the code that is to be used by the
105+ // EVM. The contract is a scoped environment for this execution context
106+ // only.
106107 contract := vm .NewContract (caller , to , value , gas , gasPrice )
107108 contract .SetCallCode (codeAddr , code )
109+ defer contract .Finalise ()
108110
109111 ret , err = evm .Run (contract , input )
110-
111- if err != nil {
112- if err == vm .CodeStoreOutOfGasError {
113- // TODO: this is rather hacky, restore all state changes
114- // except sender's account nonce increment
115- toNonce := env .Db ().GetNonce (to .Address ())
116- env .SetSnapshot (snapshotPostTransfer )
117- env .Db ().SetNonce (to .Address (), toNonce )
112+ // if the contract creation ran successfully and no errors were returned
113+ // calculate the gas required to store the code. If the code could not
114+ // be stored due to not enough gas set an error and let it be handled
115+ // by the error checking condition below.
116+ if err == nil && createAccount {
117+ dataGas := big .NewInt (int64 (len (ret )))
118+ dataGas .Mul (dataGas , params .CreateDataGas )
119+ if contract .UseGas (dataGas ) {
120+ env .Db ().SetCode (* address , ret )
118121 } else {
119- env . SetSnapshot ( snapshotPreTransfer ) //env.Db().Set(snapshot)
122+ err = vm . CodeStoreOutOfGasError
120123 }
121124 }
125+
126+ // When an error was returned by the EVM or when setting the creation code
127+ // above we revert to the snapshot and consume any gas remaining. Additionally
128+ // when we're in homestead this also counts for code storage gas errors.
129+ if err != nil && (params .IsHomestead (env .BlockNumber ()) || err != vm .CodeStoreOutOfGasError ) {
130+ contract .UseGas (contract .Gas )
131+
132+ env .SetSnapshot (snapshotPreTransfer )
133+ }
134+
122135 return ret , addr , err
123136}
124137
@@ -131,32 +144,27 @@ func execDelegateCall(env vm.Environment, caller vm.ContractRef, originAddr, toA
131144 return nil , common.Address {}, vm .DepthError
132145 }
133146
134- if ! env .CanTransfer (* originAddr , value ) {
135- caller .ReturnGas (gas , gasPrice )
136- return nil , common.Address {}, ValueTransferErr ("insufficient funds to transfer value. Req %v, has %v" , value , env .Db ().GetBalance (caller .Address ()))
137- }
138-
139147 snapshot := env .MakeSnapshot ()
140148
141- var (
142- //from = env.Db().GetAccount(*originAddr)
143- to vm.Account
144- )
149+ var to vm.Account
145150 if ! env .Db ().Exist (* toAddr ) {
146151 to = env .Db ().CreateAccount (* toAddr )
147152 } else {
148153 to = env .Db ().GetAccount (* toAddr )
149154 }
150155
151- contract := vm .NewContract (caller , to , value , gas , gasPrice )
156+ // Iinitialise a new contract and make initialise the delegate values
157+ contract := vm .NewContract (caller , to , value , gas , gasPrice ).AsDelegate ()
152158 contract .SetCallCode (codeAddr , code )
153- contract .DelegateCall = true
159+ defer contract .Finalise ()
154160
155161 ret , err = evm .Run (contract , input )
156-
157162 if err != nil {
158- env .SetSnapshot (snapshot ) //env.Db().Set(snapshot)
163+ contract .UseGas (contract .Gas )
164+
165+ env .SetSnapshot (snapshot )
159166 }
167+
160168 return ret , addr , err
161169}
162170
0 commit comments