@@ -212,12 +212,12 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
212212 evm .StateDB .CreateAccount (addr )
213213 }
214214 evm .Transfer (evm .StateDB , caller .Address (), to .Address (), value )
215-
216215 // Initialise a new contract and set the code that is to be used by the EVM.
217216 // The contract is a scoped environment for this execution context only.
218217 contract := NewContract (caller , to , value , gas )
219218 contract .SetCallCode (& addr , evm .StateDB .GetCodeHash (addr ), evm .StateDB .GetCode (addr ))
220219
220+ // Even if the account has no code, we need to continue because it might be a precompile
221221 start := time .Now ()
222222
223223 // Capture the tracer start/end events in debug mode
@@ -352,8 +352,20 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
352352 return ret , contract .Gas , err
353353}
354354
355+ type codeAndHash struct {
356+ code []byte
357+ hash common.Hash
358+ }
359+
360+ func (c * codeAndHash ) Hash () common.Hash {
361+ if c .hash == (common.Hash {}) {
362+ c .hash = crypto .Keccak256Hash (c .code )
363+ }
364+ return c .hash
365+ }
366+
355367// create creates a new contract using code as deployment code.
356- func (evm * EVM ) create (caller ContractRef , code [] byte , gas uint64 , value * big.Int , address common.Address ) ([]byte , common.Address , uint64 , error ) {
368+ func (evm * EVM ) create (caller ContractRef , codeAndHash * codeAndHash , gas uint64 , value * big.Int , address common.Address ) ([]byte , common.Address , uint64 , error ) {
357369 // Depth check execution. Fail if we're trying to execute above the
358370 // limit.
359371 if evm .depth > int (params .CallCreateDepth ) {
@@ -382,14 +394,14 @@ func (evm *EVM) create(caller ContractRef, code []byte, gas uint64, value *big.I
382394 // EVM. The contract is a scoped environment for this execution context
383395 // only.
384396 contract := NewContract (caller , AccountRef (address ), value , gas )
385- contract .SetCallCode (& address , crypto . Keccak256Hash ( code ), code )
397+ contract .SetCodeOptionalHash (& address , codeAndHash )
386398
387399 if evm .vmConfig .NoRecursion && evm .depth > 0 {
388400 return nil , address , gas , nil
389401 }
390402
391403 if evm .vmConfig .Debug && evm .depth == 0 {
392- evm .vmConfig .Tracer .CaptureStart (caller .Address (), address , true , code , gas , value )
404+ evm .vmConfig .Tracer .CaptureStart (caller .Address (), address , true , codeAndHash . code , gas , value )
393405 }
394406 start := time .Now ()
395407
@@ -433,16 +445,17 @@ func (evm *EVM) create(caller ContractRef, code []byte, gas uint64, value *big.I
433445// Create creates a new contract using code as deployment code.
434446func (evm * EVM ) Create (caller ContractRef , code []byte , gas uint64 , value * big.Int ) (ret []byte , contractAddr common.Address , leftOverGas uint64 , err error ) {
435447 contractAddr = crypto .CreateAddress (caller .Address (), evm .StateDB .GetNonce (caller .Address ()))
436- return evm .create (caller , code , gas , value , contractAddr )
448+ return evm .create (caller , & codeAndHash { code : code } , gas , value , contractAddr )
437449}
438450
439451// Create2 creates a new contract using code as deployment code.
440452//
441453// The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
442454// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
443455func (evm * EVM ) Create2 (caller ContractRef , code []byte , gas uint64 , endowment * big.Int , salt * big.Int ) (ret []byte , contractAddr common.Address , leftOverGas uint64 , err error ) {
444- contractAddr = crypto .CreateAddress2 (caller .Address (), common .BigToHash (salt ), code )
445- return evm .create (caller , code , gas , endowment , contractAddr )
456+ codeAndHash := & codeAndHash {code : code }
457+ contractAddr = crypto .CreateAddress2 (caller .Address (), common .BigToHash (salt ), codeAndHash .Hash ().Bytes ())
458+ return evm .create (caller , codeAndHash , gas , endowment , contractAddr )
446459}
447460
448461// ChainConfig returns the environment's chain configuration
0 commit comments