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