github.com/codingfuture/orig-energi3@v0.8.4/core/vm/runtime/runtime.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package runtime 19 20 import ( 21 "math" 22 "math/big" 23 "time" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/core/state" 27 "github.com/ethereum/go-ethereum/core/vm" 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/ethdb" 30 "github.com/ethereum/go-ethereum/params" 31 ) 32 33 // Config is a basic type specifying certain configuration flags for running 34 // the EVM. 35 type Config struct { 36 ChainConfig *params.ChainConfig 37 Difficulty *big.Int 38 Origin common.Address 39 Coinbase common.Address 40 BlockNumber *big.Int 41 Time *big.Int 42 GasLimit uint64 43 GasPrice *big.Int 44 Value *big.Int 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 EIP150Block: new(big.Int), 59 EIP155Block: new(big.Int), 60 EIP158Block: new(big.Int), 61 } 62 } 63 64 if cfg.Difficulty == nil { 65 cfg.Difficulty = new(big.Int) 66 } 67 if cfg.Time == nil { 68 cfg.Time = big.NewInt(time.Now().Unix()) 69 } 70 if cfg.GasLimit == 0 { 71 cfg.GasLimit = math.MaxUint64 72 } 73 if cfg.GasPrice == nil { 74 cfg.GasPrice = new(big.Int) 75 } 76 if cfg.Value == nil { 77 cfg.Value = new(big.Int) 78 } 79 if cfg.BlockNumber == nil { 80 cfg.BlockNumber = new(big.Int) 81 } 82 if cfg.GetHashFn == nil { 83 cfg.GetHashFn = func(n uint64) common.Hash { 84 return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String()))) 85 } 86 } 87 } 88 89 // Execute executes the code using the input as call data during the execution. 90 // It returns the EVM's return value, the new state and an error if it failed. 91 // 92 // Executes sets up a in memory, temporarily, environment for the execution of 93 // the given code. It makes sure that it's restored to it's original state afterwards. 94 func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) { 95 if cfg == nil { 96 cfg = new(Config) 97 } 98 setDefaults(cfg) 99 100 if cfg.State == nil { 101 cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase())) 102 } 103 var ( 104 address = common.BytesToAddress([]byte("contract")) 105 vmenv = NewEnv(cfg) 106 sender = vm.AccountRef(cfg.Origin) 107 ) 108 cfg.State.CreateAccount(address) 109 // set the receiver's (the executing contract) code for execution. 110 cfg.State.SetCode(address, code) 111 // Call the code with the given configuration. 112 ret, _, err := vmenv.Call( 113 sender, 114 common.BytesToAddress([]byte("contract")), 115 input, 116 cfg.GasLimit, 117 cfg.Value, 118 ) 119 120 return ret, cfg.State, err 121 } 122 123 // Create executes the code using the EVM create method 124 func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) { 125 if cfg == nil { 126 cfg = new(Config) 127 } 128 setDefaults(cfg) 129 130 if cfg.State == nil { 131 cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase())) 132 } 133 var ( 134 vmenv = NewEnv(cfg) 135 sender = vm.AccountRef(cfg.Origin) 136 ) 137 138 // Call the code with the given configuration. 139 code, address, leftOverGas, err := vmenv.Create( 140 sender, 141 input, 142 cfg.GasLimit, 143 cfg.Value, 144 ) 145 return code, address, leftOverGas, err 146 } 147 148 // Call executes the code given by the contract's address. It will return the 149 // EVM's return value or an error if it failed. 150 // 151 // Call, unlike Execute, requires a config and also requires the State field to 152 // be set. 153 func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) { 154 setDefaults(cfg) 155 156 vmenv := NewEnv(cfg) 157 158 sender := cfg.State.GetOrNewStateObject(cfg.Origin) 159 // Call the code with the given configuration. 160 ret, leftOverGas, err := vmenv.Call( 161 sender, 162 address, 163 input, 164 cfg.GasLimit, 165 cfg.Value, 166 ) 167 168 return ret, leftOverGas, err 169 }