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