github.com/4000d/go-ethereum@v1.8.2-0.20180223170251-423c8bb1d821/core/vm/runtime/runtime.go (about) 1 // Copyright 2015 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 runtime 18 19 import ( 20 "math" 21 "math/big" 22 "time" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/core/state" 26 "github.com/ethereum/go-ethereum/core/vm" 27 "github.com/ethereum/go-ethereum/crypto" 28 "github.com/ethereum/go-ethereum/ethdb" 29 "github.com/ethereum/go-ethereum/params" 30 ) 31 32 // Config is a basic type specifying certain configuration flags for running 33 // the EVM. 34 type Config struct { 35 ChainConfig *params.ChainConfig 36 Difficulty *big.Int 37 Origin common.Address 38 Coinbase common.Address 39 BlockNumber *big.Int 40 Time *big.Int 41 GasLimit uint64 42 GasPrice *big.Int 43 Value *big.Int 44 DisableJit bool // "disable" so it's enabled by default 45 Debug bool 46 EVMConfig vm.Config 47 48 State *state.StateDB 49 GetHashFn func(n uint64) common.Hash 50 } 51 52 // sets defaults on the config 53 func setDefaults(cfg *Config) { 54 if cfg.ChainConfig == nil { 55 cfg.ChainConfig = ¶ms.ChainConfig{ 56 ChainId: big.NewInt(1), 57 HomesteadBlock: new(big.Int), 58 DAOForkBlock: new(big.Int), 59 DAOForkSupport: false, 60 EIP150Block: new(big.Int), 61 EIP155Block: new(big.Int), 62 EIP158Block: new(big.Int), 63 } 64 } 65 66 if cfg.Difficulty == nil { 67 cfg.Difficulty = new(big.Int) 68 } 69 if cfg.Time == nil { 70 cfg.Time = big.NewInt(time.Now().Unix()) 71 } 72 if cfg.GasLimit == 0 { 73 cfg.GasLimit = math.MaxUint64 74 } 75 if cfg.GasPrice == nil { 76 cfg.GasPrice = new(big.Int) 77 } 78 if cfg.Value == nil { 79 cfg.Value = new(big.Int) 80 } 81 if cfg.BlockNumber == nil { 82 cfg.BlockNumber = new(big.Int) 83 } 84 if cfg.GetHashFn == nil { 85 cfg.GetHashFn = func(n uint64) common.Hash { 86 return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String()))) 87 } 88 } 89 } 90 91 // Execute executes the code using the input as call data during the execution. 92 // It returns the EVM's return value, the new state and an error if it failed. 93 // 94 // Executes sets up a in memory, temporarily, environment for the execution of 95 // the given code. It enabled the JIT by default and make sure that it's restored 96 // to it's original state afterwards. 97 func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) { 98 if cfg == nil { 99 cfg = new(Config) 100 } 101 setDefaults(cfg) 102 103 if cfg.State == nil { 104 db, _ := ethdb.NewMemDatabase() 105 cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(db)) 106 } 107 var ( 108 address = common.StringToAddress("contract") 109 vmenv = NewEnv(cfg) 110 sender = vm.AccountRef(cfg.Origin) 111 ) 112 cfg.State.CreateAccount(address) 113 // set the receiver's (the executing contract) code for execution. 114 cfg.State.SetCode(address, code) 115 // Call the code with the given configuration. 116 ret, _, err := vmenv.Call( 117 sender, 118 common.StringToAddress("contract"), 119 input, 120 cfg.GasLimit, 121 cfg.Value, 122 ) 123 124 return ret, cfg.State, err 125 } 126 127 // Create executes the code using the EVM create method 128 func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) { 129 if cfg == nil { 130 cfg = new(Config) 131 } 132 setDefaults(cfg) 133 134 if cfg.State == nil { 135 db, _ := ethdb.NewMemDatabase() 136 cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(db)) 137 } 138 var ( 139 vmenv = NewEnv(cfg) 140 sender = vm.AccountRef(cfg.Origin) 141 ) 142 143 // Call the code with the given configuration. 144 code, address, leftOverGas, err := vmenv.Create( 145 sender, 146 input, 147 cfg.GasLimit, 148 cfg.Value, 149 ) 150 return code, address, leftOverGas, err 151 } 152 153 // Call executes the code given by the contract's address. It will return the 154 // EVM's return value or an error if it failed. 155 // 156 // Call, unlike Execute, requires a config and also requires the State field to 157 // be set. 158 func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) { 159 setDefaults(cfg) 160 161 vmenv := NewEnv(cfg) 162 163 sender := cfg.State.GetOrNewStateObject(cfg.Origin) 164 // Call the code with the given configuration. 165 ret, leftOverGas, err := vmenv.Call( 166 sender, 167 address, 168 input, 169 cfg.GasLimit, 170 cfg.Value, 171 ) 172 173 return ret, leftOverGas, err 174 }