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