evm.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "errors"
  19. "math/big"
  20. "sync/atomic"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/params"
  25. "github.com/holiman/uint256"
  26. )
  27. // emptyCodeHash is used by create to ensure deployment is disallowed to already
  28. // deployed contract addresses (relevant after the account abstraction).
  29. var emptyCodeHash = crypto.Keccak256Hash(nil)
  30. type (
  31. // CanTransferFunc is the signature of a transfer guard function
  32. CanTransferFunc func(StateDB, common.Address, *big.Int) bool
  33. // TransferFunc is the signature of a transfer function
  34. TransferFunc func(StateDB, common.Address, common.Address, *big.Int)
  35. // GetHashFunc returns the n'th block hash in the blockchain
  36. // and is used by the BLOCKHASH EVM op code.
  37. GetHashFunc func(uint64) common.Hash
  38. )
  39. // ActivePrecompiles returns the addresses of the precompiles enabled with the current
  40. // configuration
  41. func (evm *EVM) ActivePrecompiles() []common.Address {
  42. switch {
  43. case evm.chainRules.IsBerlin:
  44. return PrecompiledAddressesBerlin
  45. case evm.chainRules.IsIstanbul:
  46. return PrecompiledAddressesIstanbul
  47. case evm.chainRules.IsByzantium:
  48. return PrecompiledAddressesByzantium
  49. default:
  50. return PrecompiledAddressesHomestead
  51. }
  52. }
  53. func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
  54. var precompiles map[common.Address]PrecompiledContract
  55. switch {
  56. case evm.chainRules.IsBerlin:
  57. precompiles = PrecompiledContractsBerlin
  58. case evm.chainRules.IsIstanbul:
  59. precompiles = PrecompiledContractsIstanbul
  60. case evm.chainRules.IsByzantium:
  61. precompiles = PrecompiledContractsByzantium
  62. default:
  63. precompiles = PrecompiledContractsHomestead
  64. }
  65. p, ok := precompiles[addr]
  66. return p, ok
  67. }
  68. // run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.
  69. func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, error) {
  70. for _, interpreter := range evm.interpreters {
  71. if interpreter.CanRun(contract.Code) {
  72. if evm.interpreter != interpreter {
  73. // Ensure that the interpreter pointer is set back
  74. // to its current value upon return.
  75. defer func(i Interpreter) {
  76. evm.interpreter = i
  77. }(evm.interpreter)
  78. evm.interpreter = interpreter
  79. }
  80. return interpreter.Run(contract, input, readOnly)
  81. }
  82. }
  83. return nil, errors.New("no compatible interpreter")
  84. }
  85. // BlockContext provides the EVM with auxiliary information. Once provided
  86. // it shouldn't be modified.
  87. type BlockContext struct {
  88. // CanTransfer returns whether the account contains
  89. // sufficient ether to transfer the value
  90. CanTransfer CanTransferFunc
  91. // Transfer transfers ether from one account to the other
  92. Transfer TransferFunc
  93. // GetHash returns the hash corresponding to n
  94. GetHash GetHashFunc
  95. // Block information
  96. Coinbase common.Address // Provides information for COINBASE
  97. GasLimit uint64 // Provides information for GASLIMIT
  98. BlockNumber *big.Int // Provides information for NUMBER
  99. Time *big.Int // Provides information for TIME
  100. Difficulty *big.Int // Provides information for DIFFICULTY
  101. }
  102. // TxContext provides the EVM with information about a transaction.
  103. // All fields can change between transactions.
  104. type TxContext struct {
  105. // Message information
  106. Origin common.Address // Provides information for ORIGIN
  107. GasPrice *big.Int // Provides information for GASPRICE
  108. }
  109. // EVM is the Ethereum Virtual Machine base object and provides
  110. // the necessary tools to run a contract on the given state with
  111. // the provided context. It should be noted that any error
  112. // generated through any of the calls should be considered a
  113. // revert-state-and-consume-all-gas operation, no checks on
  114. // specific errors should ever be performed. The interpreter makes
  115. // sure that any errors generated are to be considered faulty code.
  116. //
  117. // The EVM should never be reused and is not thread safe.
  118. type EVM struct {
  119. // Context provides auxiliary blockchain related information
  120. Context BlockContext
  121. TxContext
  122. // StateDB gives access to the underlying state
  123. StateDB StateDB
  124. // Depth is the current call stack
  125. depth int
  126. // chainConfig contains information about the current chain
  127. chainConfig *params.ChainConfig
  128. // chain rules contains the chain rules for the current epoch
  129. chainRules params.Rules
  130. // virtual machine configuration options used to initialise the
  131. // evm.
  132. vmConfig Config
  133. // global (to this context) ethereum virtual machine
  134. // used throughout the execution of the tx.
  135. interpreters []Interpreter
  136. interpreter Interpreter
  137. // abort is used to abort the EVM calling operations
  138. // NOTE: must be set atomically
  139. abort int32
  140. // callGasTemp holds the gas available for the current call. This is needed because the
  141. // available gas is calculated in gasCall* according to the 63/64 rule and later
  142. // applied in opCall*.
  143. callGasTemp uint64
  144. }
  145. // NewEVM returns a new EVM. The returned EVM is not thread safe and should
  146. // only ever be used *once*.
  147. func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *EVM {
  148. evm := &EVM{
  149. Context: blockCtx,
  150. TxContext: txCtx,
  151. StateDB: statedb,
  152. vmConfig: vmConfig,
  153. chainConfig: chainConfig,
  154. chainRules: chainConfig.Rules(blockCtx.BlockNumber),
  155. interpreters: make([]Interpreter, 0, 1),
  156. }
  157. if chainConfig.IsEWASM(blockCtx.BlockNumber) {
  158. // to be implemented by EVM-C and Wagon PRs.
  159. // if vmConfig.EWASMInterpreter != "" {
  160. // extIntOpts := strings.Split(vmConfig.EWASMInterpreter, ":")
  161. // path := extIntOpts[0]
  162. // options := []string{}
  163. // if len(extIntOpts) > 1 {
  164. // options = extIntOpts[1..]
  165. // }
  166. // evm.interpreters = append(evm.interpreters, NewEVMVCInterpreter(evm, vmConfig, options))
  167. // } else {
  168. // evm.interpreters = append(evm.interpreters, NewEWASMInterpreter(evm, vmConfig))
  169. // }
  170. panic("No supported ewasm interpreter yet.")
  171. }
  172. // vmConfig.EVMInterpreter will be used by EVM-C, it won't be checked here
  173. // as we always want to have the built-in EVM as the failover option.
  174. evm.interpreters = append(evm.interpreters, NewEVMInterpreter(evm, vmConfig))
  175. evm.interpreter = evm.interpreters[0]
  176. return evm
  177. }
  178. // Reset resets the EVM with a new transaction context.Reset
  179. // This is not threadsafe and should only be done very cautiously.
  180. func (evm *EVM) Reset(txCtx TxContext, statedb StateDB) {
  181. evm.TxContext = txCtx
  182. evm.StateDB = statedb
  183. }
  184. // Cancel cancels any running EVM operation. This may be called concurrently and
  185. // it's safe to be called multiple times.
  186. func (evm *EVM) Cancel() {
  187. atomic.StoreInt32(&evm.abort, 1)
  188. }
  189. // Cancelled returns true if Cancel has been called
  190. func (evm *EVM) Cancelled() bool {
  191. return atomic.LoadInt32(&evm.abort) == 1
  192. }
  193. // Interpreter returns the current interpreter
  194. func (evm *EVM) Interpreter() Interpreter {
  195. return evm.interpreter
  196. }
  197. // Call executes the contract associated with the addr with the given input as
  198. // parameters. It also handles any necessary value transfer required and takes
  199. // the necessary steps to create accounts and reverses the state in case of an
  200. // execution error or failed value transfer.
  201. func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
  202. if evm.vmConfig.NoRecursion && evm.depth > 0 {
  203. return nil, gas, nil
  204. }
  205. // Fail if we're trying to execute above the call depth limit
  206. if evm.depth > int(params.CallCreateDepth) {
  207. return nil, gas, ErrDepth
  208. }
  209. // Fail if we're trying to transfer more than the available balance
  210. if value.Sign() != 0 && !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
  211. return nil, gas, ErrInsufficientBalance
  212. }
  213. snapshot := evm.StateDB.Snapshot()
  214. p, isPrecompile := evm.precompile(addr)
  215. if !evm.StateDB.Exist(addr) {
  216. if !isPrecompile && evm.chainRules.IsEIP158 && value.Sign() == 0 {
  217. // Calling a non existing account, don't do anything, but ping the tracer
  218. if evm.vmConfig.Debug && evm.depth == 0 {
  219. evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
  220. evm.vmConfig.Tracer.CaptureEnd(ret, 0, 0, nil)
  221. }
  222. return nil, gas, nil
  223. }
  224. evm.StateDB.CreateAccount(addr)
  225. }
  226. evm.Context.Transfer(evm.StateDB, caller.Address(), addr, value)
  227. // Capture the tracer start/end events in debug mode
  228. if evm.vmConfig.Debug && evm.depth == 0 {
  229. evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
  230. defer func(startGas uint64, startTime time.Time) { // Lazy evaluation of the parameters
  231. evm.vmConfig.Tracer.CaptureEnd(ret, startGas-gas, time.Since(startTime), err)
  232. }(gas, time.Now())
  233. }
  234. if isPrecompile {
  235. ret, gas, err = RunPrecompiledContract(p, input, gas)
  236. } else {
  237. // Initialise a new contract and set the code that is to be used by the EVM.
  238. // The contract is a scoped environment for this execution context only.
  239. code := evm.StateDB.GetCode(addr)
  240. if len(code) == 0 {
  241. ret, err = nil, nil // gas is unchanged
  242. } else {
  243. addrCopy := addr
  244. // If the account has no code, we can abort here
  245. // The depth-check is already done, and precompiles handled above
  246. contract := NewContract(caller, AccountRef(addrCopy), value, gas)
  247. contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), code)
  248. ret, err = run(evm, contract, input, false)
  249. gas = contract.Gas
  250. }
  251. }
  252. // When an error was returned by the EVM or when setting the creation code
  253. // above we revert to the snapshot and consume any gas remaining. Additionally
  254. // when we're in homestead this also counts for code storage gas errors.
  255. if err != nil {
  256. evm.StateDB.RevertToSnapshot(snapshot)
  257. if err != ErrExecutionReverted {
  258. gas = 0
  259. }
  260. // TODO: consider clearing up unused snapshots:
  261. //} else {
  262. // evm.StateDB.DiscardSnapshot(snapshot)
  263. }
  264. return ret, gas, err
  265. }
  266. // CallCode executes the contract associated with the addr with the given input
  267. // as parameters. It also handles any necessary value transfer required and takes
  268. // the necessary steps to create accounts and reverses the state in case of an
  269. // execution error or failed value transfer.
  270. //
  271. // CallCode differs from Call in the sense that it executes the given address'
  272. // code with the caller as context.
  273. func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
  274. if evm.vmConfig.NoRecursion && evm.depth > 0 {
  275. return nil, gas, nil
  276. }
  277. // Fail if we're trying to execute above the call depth limit
  278. if evm.depth > int(params.CallCreateDepth) {
  279. return nil, gas, ErrDepth
  280. }
  281. // Fail if we're trying to transfer more than the available balance
  282. // Note although it's noop to transfer X ether to caller itself. But
  283. // if caller doesn't have enough balance, it would be an error to allow
  284. // over-charging itself. So the check here is necessary.
  285. if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
  286. return nil, gas, ErrInsufficientBalance
  287. }
  288. var snapshot = evm.StateDB.Snapshot()
  289. // It is allowed to call precompiles, even via delegatecall
  290. if p, isPrecompile := evm.precompile(addr); isPrecompile {
  291. ret, gas, err = RunPrecompiledContract(p, input, gas)
  292. } else {
  293. addrCopy := addr
  294. // Initialise a new contract and set the code that is to be used by the EVM.
  295. // The contract is a scoped environment for this execution context only.
  296. contract := NewContract(caller, AccountRef(caller.Address()), value, gas)
  297. contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))
  298. ret, err = run(evm, contract, input, false)
  299. gas = contract.Gas
  300. }
  301. if err != nil {
  302. evm.StateDB.RevertToSnapshot(snapshot)
  303. if err != ErrExecutionReverted {
  304. gas = 0
  305. }
  306. }
  307. return ret, gas, err
  308. }
  309. // DelegateCall executes the contract associated with the addr with the given input
  310. // as parameters. It reverses the state in case of an execution error.
  311. //
  312. // DelegateCall differs from CallCode in the sense that it executes the given address'
  313. // code with the caller as context and the caller is set to the caller of the caller.
  314. func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
  315. if evm.vmConfig.NoRecursion && evm.depth > 0 {
  316. return nil, gas, nil
  317. }
  318. // Fail if we're trying to execute above the call depth limit
  319. if evm.depth > int(params.CallCreateDepth) {
  320. return nil, gas, ErrDepth
  321. }
  322. var snapshot = evm.StateDB.Snapshot()
  323. // It is allowed to call precompiles, even via delegatecall
  324. if p, isPrecompile := evm.precompile(addr); isPrecompile {
  325. ret, gas, err = RunPrecompiledContract(p, input, gas)
  326. } else {
  327. addrCopy := addr
  328. // Initialise a new contract and make initialise the delegate values
  329. contract := NewContract(caller, AccountRef(caller.Address()), nil, gas).AsDelegate()
  330. contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))
  331. ret, err = run(evm, contract, input, false)
  332. gas = contract.Gas
  333. }
  334. if err != nil {
  335. evm.StateDB.RevertToSnapshot(snapshot)
  336. if err != ErrExecutionReverted {
  337. gas = 0
  338. }
  339. }
  340. return ret, gas, err
  341. }
  342. // StaticCall executes the contract associated with the addr with the given input
  343. // as parameters while disallowing any modifications to the state during the call.
  344. // Opcodes that attempt to perform such modifications will result in exceptions
  345. // instead of performing the modifications.
  346. func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
  347. if evm.vmConfig.NoRecursion && evm.depth > 0 {
  348. return nil, gas, nil
  349. }
  350. // Fail if we're trying to execute above the call depth limit
  351. if evm.depth > int(params.CallCreateDepth) {
  352. return nil, gas, ErrDepth
  353. }
  354. // We take a snapshot here. This is a bit counter-intuitive, and could probably be skipped.
  355. // However, even a staticcall is considered a 'touch'. On mainnet, static calls were introduced
  356. // after all empty accounts were deleted, so this is not required. However, if we omit this,
  357. // then certain tests start failing; stRevertTest/RevertPrecompiledTouchExactOOG.json.
  358. // We could change this, but for now it's left for legacy reasons
  359. var snapshot = evm.StateDB.Snapshot()
  360. // We do an AddBalance of zero here, just in order to trigger a touch.
  361. // This doesn't matter on Mainnet, where all empties are gone at the time of Byzantium,
  362. // but is the correct thing to do and matters on other networks, in tests, and potential
  363. // future scenarios
  364. evm.StateDB.AddBalance(addr, big0)
  365. if p, isPrecompile := evm.precompile(addr); isPrecompile {
  366. ret, gas, err = RunPrecompiledContract(p, input, gas)
  367. } else {
  368. // At this point, we use a copy of address. If we don't, the go compiler will
  369. // leak the 'contract' to the outer scope, and make allocation for 'contract'
  370. // even if the actual execution ends on RunPrecompiled above.
  371. addrCopy := addr
  372. // Initialise a new contract and set the code that is to be used by the EVM.
  373. // The contract is a scoped environment for this execution context only.
  374. contract := NewContract(caller, AccountRef(addrCopy), new(big.Int), gas)
  375. contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))
  376. // When an error was returned by the EVM or when setting the creation code
  377. // above we revert to the snapshot and consume any gas remaining. Additionally
  378. // when we're in Homestead this also counts for code storage gas errors.
  379. ret, err = run(evm, contract, input, true)
  380. gas = contract.Gas
  381. }
  382. if err != nil {
  383. evm.StateDB.RevertToSnapshot(snapshot)
  384. if err != ErrExecutionReverted {
  385. gas = 0
  386. }
  387. }
  388. return ret, gas, err
  389. }
  390. type codeAndHash struct {
  391. code []byte
  392. hash common.Hash
  393. }
  394. func (c *codeAndHash) Hash() common.Hash {
  395. if c.hash == (common.Hash{}) {
  396. c.hash = crypto.Keccak256Hash(c.code)
  397. }
  398. return c.hash
  399. }
  400. // create creates a new contract using code as deployment code.
  401. func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error) {
  402. // Depth check execution. Fail if we're trying to execute above the
  403. // limit.
  404. if evm.depth > int(params.CallCreateDepth) {
  405. return nil, common.Address{}, gas, ErrDepth
  406. }
  407. if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
  408. return nil, common.Address{}, gas, ErrInsufficientBalance
  409. }
  410. nonce := evm.StateDB.GetNonce(caller.Address())
  411. evm.StateDB.SetNonce(caller.Address(), nonce+1)
  412. // We add this to the access list _before_ taking a snapshot. Even if the creation fails,
  413. // the access-list change should not be rolled back
  414. if evm.chainRules.IsBerlin {
  415. evm.StateDB.AddAddressToAccessList(address)
  416. }
  417. // Ensure there's no existing contract already at the designated address
  418. contractHash := evm.StateDB.GetCodeHash(address)
  419. if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != emptyCodeHash) {
  420. return nil, common.Address{}, 0, ErrContractAddressCollision
  421. }
  422. // Create a new account on the state
  423. snapshot := evm.StateDB.Snapshot()
  424. evm.StateDB.CreateAccount(address)
  425. if evm.chainRules.IsEIP158 {
  426. evm.StateDB.SetNonce(address, 1)
  427. }
  428. evm.Context.Transfer(evm.StateDB, caller.Address(), address, value)
  429. // Initialise a new contract and set the code that is to be used by the EVM.
  430. // The contract is a scoped environment for this execution context only.
  431. contract := NewContract(caller, AccountRef(address), value, gas)
  432. contract.SetCodeOptionalHash(&address, codeAndHash)
  433. if evm.vmConfig.NoRecursion && evm.depth > 0 {
  434. return nil, address, gas, nil
  435. }
  436. if evm.vmConfig.Debug && evm.depth == 0 {
  437. evm.vmConfig.Tracer.CaptureStart(caller.Address(), address, true, codeAndHash.code, gas, value)
  438. }
  439. start := time.Now()
  440. ret, err := run(evm, contract, nil, false)
  441. // check whether the max code size has been exceeded
  442. maxCodeSizeExceeded := evm.chainRules.IsEIP158 && len(ret) > params.MaxCodeSize
  443. // if the contract creation ran successfully and no errors were returned
  444. // calculate the gas required to store the code. If the code could not
  445. // be stored due to not enough gas set an error and let it be handled
  446. // by the error checking condition below.
  447. if err == nil && !maxCodeSizeExceeded {
  448. createDataGas := uint64(len(ret)) * params.CreateDataGas
  449. if contract.UseGas(createDataGas) {
  450. evm.StateDB.SetCode(address, ret)
  451. } else {
  452. err = ErrCodeStoreOutOfGas
  453. }
  454. }
  455. // When an error was returned by the EVM or when setting the creation code
  456. // above we revert to the snapshot and consume any gas remaining. Additionally
  457. // when we're in homestead this also counts for code storage gas errors.
  458. if maxCodeSizeExceeded || (err != nil && (evm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas)) {
  459. evm.StateDB.RevertToSnapshot(snapshot)
  460. if err != ErrExecutionReverted {
  461. contract.UseGas(contract.Gas)
  462. }
  463. }
  464. // Assign err if contract code size exceeds the max while the err is still empty.
  465. if maxCodeSizeExceeded && err == nil {
  466. err = ErrMaxCodeSizeExceeded
  467. }
  468. if evm.vmConfig.Debug && evm.depth == 0 {
  469. evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
  470. }
  471. return ret, address, contract.Gas, err
  472. }
  473. // Create creates a new contract using code as deployment code.
  474. func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
  475. contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
  476. return evm.create(caller, &codeAndHash{code: code}, gas, value, contractAddr)
  477. }
  478. // Create2 creates a new contract using code as deployment code.
  479. //
  480. // The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
  481. // instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
  482. func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
  483. codeAndHash := &codeAndHash{code: code}
  484. contractAddr = crypto.CreateAddress2(caller.Address(), salt.Bytes32(), codeAndHash.Hash().Bytes())
  485. return evm.create(caller, codeAndHash, gas, endowment, contractAddr)
  486. }
  487. // ChainConfig returns the environment's chain configuration
  488. func (evm *EVM) ChainConfig() *params.ChainConfig { return evm.chainConfig }