@@ -123,19 +123,20 @@ func (evm *EVM) Cancel() {
123123 atomic .StoreInt32 (& evm .abort , 1 )
124124}
125125
126- // Call executes the contract associated with the addr with the given input as parameters. It also handles any
127- // necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
128- // case of an execution error or failed value transfer.
126+ // Call executes the contract associated with the addr with the given input as
127+ // parameters. It also handles any necessary value transfer required and takes
128+ // the necessary steps to create accounts and reverses the state in case of an
129+ // execution error or failed value transfer.
129130func (evm * EVM ) Call (caller ContractRef , addr common.Address , input []byte , gas uint64 , value * big.Int ) (ret []byte , leftOverGas uint64 , err error ) {
130131 if evm .vmConfig .NoRecursion && evm .depth > 0 {
131132 return nil , gas , nil
132133 }
133134
134- // Depth check execution. Fail if we're trying to execute above the
135- // limit.
135+ // Fail if we're trying to execute above the call depth limit
136136 if evm .depth > int (params .CallCreateDepth ) {
137137 return nil , gas , ErrDepth
138138 }
139+ // Fail if we're trying to transfer more than the available balance
139140 if ! evm .Context .CanTransfer (evm .StateDB , caller .Address (), value ) {
140141 return nil , gas , ErrInsufficientBalance
141142 }
@@ -173,21 +174,23 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
173174 return ret , contract .Gas , err
174175}
175176
176- // CallCode executes the contract associated with the addr with the given input as parameters. It also handles any
177- // necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
178- // case of an execution error or failed value transfer.
177+ // CallCode executes the contract associated with the addr with the given input
178+ // as parameters. It also handles any necessary value transfer required and takes
179+ // the necessary steps to create accounts and reverses the state in case of an
180+ // execution error or failed value transfer.
179181//
180- // CallCode differs from Call in the sense that it executes the given address' code with the caller as context.
182+ // CallCode differs from Call in the sense that it executes the given address'
183+ // code with the caller as context.
181184func (evm * EVM ) CallCode (caller ContractRef , addr common.Address , input []byte , gas uint64 , value * big.Int ) (ret []byte , leftOverGas uint64 , err error ) {
182185 if evm .vmConfig .NoRecursion && evm .depth > 0 {
183186 return nil , gas , nil
184187 }
185188
186- // Depth check execution. Fail if we're trying to execute above the
187- // limit.
189+ // Fail if we're trying to execute above the call depth limit
188190 if evm .depth > int (params .CallCreateDepth ) {
189191 return nil , gas , ErrDepth
190192 }
193+ // Fail if we're trying to transfer more than the available balance
191194 if ! evm .CanTransfer (evm .StateDB , caller .Address (), value ) {
192195 return nil , gas , ErrInsufficientBalance
193196 }
@@ -211,18 +214,16 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
211214 return ret , contract .Gas , err
212215}
213216
214- // DelegateCall executes the contract associated with the addr with the given input as parameters.
215- // It reverses the state in case of an execution error.
217+ // DelegateCall executes the contract associated with the addr with the given input
218+ // as parameters. It reverses the state in case of an execution error.
216219//
217- // DelegateCall differs from CallCode in the sense that it executes the given address' code with the caller as context
218- // and the caller is set to the caller of the caller.
220+ // DelegateCall differs from CallCode in the sense that it executes the given address'
221+ // code with the caller as context and the caller is set to the caller of the caller.
219222func (evm * EVM ) DelegateCall (caller ContractRef , addr common.Address , input []byte , gas uint64 ) (ret []byte , leftOverGas uint64 , err error ) {
220223 if evm .vmConfig .NoRecursion && evm .depth > 0 {
221224 return nil , gas , nil
222225 }
223-
224- // Depth check execution. Fail if we're trying to execute above the
225- // limit.
226+ // Fail if we're trying to execute above the call depth limit
226227 if evm .depth > int (params .CallCreateDepth ) {
227228 return nil , gas , ErrDepth
228229 }
@@ -232,7 +233,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
232233 to = AccountRef (caller .Address ())
233234 )
234235
235- // Iinitialise a new contract and make initialise the delegate values
236+ // Initialise a new contract and make initialise the delegate values
236237 contract := NewContract (caller , to , nil , gas ).AsDelegate ()
237238 contract .SetCallCode (& addr , evm .StateDB .GetCodeHash (addr ), evm .StateDB .GetCode (addr ))
238239
@@ -245,6 +246,47 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
245246 return ret , contract .Gas , err
246247}
247248
249+ // StaticCall executes the contract associated with the addr with the given input
250+ // as parameters while disallowing any modifications to the state during the call.
251+ // Opcodes that attempt to perform such modifications will result in exceptions
252+ // instead of performing the modifications.
253+ func (evm * EVM ) StaticCall (caller ContractRef , addr common.Address , input []byte , gas uint64 ) (ret []byte , leftOverGas uint64 , err error ) {
254+ if evm .vmConfig .NoRecursion && evm .depth > 0 {
255+ return nil , gas , nil
256+ }
257+ // Fail if we're trying to execute above the call depth limit
258+ if evm .depth > int (params .CallCreateDepth ) {
259+ return nil , gas , ErrDepth
260+ }
261+ // Make sure the readonly is only set if we aren't in readonly yet
262+ // this makes also sure that the readonly flag isn't removed for
263+ // child calls.
264+ if ! evm .interpreter .readonly {
265+ evm .interpreter .readonly = true
266+ defer func () { evm .interpreter .readonly = false }()
267+ }
268+
269+ var (
270+ to = AccountRef (addr )
271+ snapshot = evm .StateDB .Snapshot ()
272+ )
273+ // Initialise a new contract and set the code that is to be used by the
274+ // EVM. The contract is a scoped environment for this execution context
275+ // only.
276+ contract := NewContract (caller , to , new (big.Int ), gas )
277+ contract .SetCallCode (& addr , evm .StateDB .GetCodeHash (addr ), evm .StateDB .GetCode (addr ))
278+
279+ // When an error was returned by the EVM or when setting the creation code
280+ // above we revert to the snapshot and consume any gas remaining. Additionally
281+ // when we're in Homestead this also counts for code storage gas errors.
282+ ret , err = run (evm , snapshot , contract , input )
283+ if err != nil {
284+ contract .UseGas (contract .Gas )
285+ evm .StateDB .RevertToSnapshot (snapshot )
286+ }
287+ return ret , contract .Gas , err
288+ }
289+
248290// Create creates a new contract using code as deployment code.
249291func (evm * EVM ) Create (caller ContractRef , code []byte , gas uint64 , value * big.Int ) (ret []byte , contractAddr common.Address , leftOverGas uint64 , err error ) {
250292 if evm .vmConfig .NoRecursion && evm .depth > 0 {
0 commit comments