github.com/theQRL/go-zond@v0.1.1/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 23 "github.com/theQRL/go-zond/common" 24 "github.com/theQRL/go-zond/core/rawdb" 25 "github.com/theQRL/go-zond/core/state" 26 "github.com/theQRL/go-zond/core/types" 27 "github.com/theQRL/go-zond/core/vm" 28 "github.com/theQRL/go-zond/crypto" 29 "github.com/theQRL/go-zond/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 uint64 41 GasLimit uint64 42 GasPrice *big.Int 43 Value *big.Int 44 Debug bool 45 EVMConfig vm.Config 46 BaseFee *big.Int 47 BlobHashes []common.Hash 48 Random *common.Hash 49 50 State *state.StateDB 51 GetHashFn func(n uint64) common.Hash 52 } 53 54 // sets defaults on the config 55 func setDefaults(cfg *Config) { 56 if cfg.ChainConfig == nil { 57 cfg.ChainConfig = ¶ms.ChainConfig{ 58 ChainID: big.NewInt(1), 59 HomesteadBlock: new(big.Int), 60 DAOForkBlock: new(big.Int), 61 DAOForkSupport: false, 62 EIP150Block: new(big.Int), 63 EIP155Block: new(big.Int), 64 EIP158Block: new(big.Int), 65 ByzantiumBlock: new(big.Int), 66 ConstantinopleBlock: new(big.Int), 67 PetersburgBlock: new(big.Int), 68 IstanbulBlock: new(big.Int), 69 MuirGlacierBlock: new(big.Int), 70 BerlinBlock: new(big.Int), 71 LondonBlock: new(big.Int), 72 } 73 } 74 75 if cfg.Difficulty == nil { 76 cfg.Difficulty = new(big.Int) 77 } 78 if cfg.GasLimit == 0 { 79 cfg.GasLimit = math.MaxUint64 80 } 81 if cfg.GasPrice == nil { 82 cfg.GasPrice = new(big.Int) 83 } 84 if cfg.Value == nil { 85 cfg.Value = new(big.Int) 86 } 87 if cfg.BlockNumber == nil { 88 cfg.BlockNumber = new(big.Int) 89 } 90 if cfg.GetHashFn == nil { 91 cfg.GetHashFn = func(n uint64) common.Hash { 92 return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String()))) 93 } 94 } 95 if cfg.BaseFee == nil { 96 cfg.BaseFee = big.NewInt(params.InitialBaseFee) 97 } 98 } 99 100 // Execute executes the code using the input as call data during the execution. 101 // It returns the EVM's return value, the new state and an error if it failed. 102 // 103 // Execute sets up an in-memory, temporary, environment for the execution of 104 // the given code. It makes sure that it's restored to its original state afterwards. 105 func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) { 106 if cfg == nil { 107 cfg = new(Config) 108 } 109 setDefaults(cfg) 110 111 if cfg.State == nil { 112 cfg.State, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 113 } 114 var ( 115 address = common.BytesToAddress([]byte("contract")) 116 vmenv = NewEnv(cfg) 117 sender = vm.AccountRef(cfg.Origin) 118 rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time) 119 ) 120 // Execute the preparatory steps for state transition which includes: 121 // - prepare accessList(post-berlin) 122 // - reset transient storage(eip 1153) 123 cfg.State.Prepare(rules, cfg.Origin, cfg.Coinbase, &address, vm.ActivePrecompiles(rules), nil) 124 cfg.State.CreateAccount(address) 125 // set the receiver's (the executing contract) code for execution. 126 cfg.State.SetCode(address, code) 127 // Call the code with the given configuration. 128 ret, _, err := vmenv.Call( 129 sender, 130 common.BytesToAddress([]byte("contract")), 131 input, 132 cfg.GasLimit, 133 cfg.Value, 134 ) 135 return ret, cfg.State, err 136 } 137 138 // Create executes the code using the EVM create method 139 func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) { 140 if cfg == nil { 141 cfg = new(Config) 142 } 143 setDefaults(cfg) 144 145 if cfg.State == nil { 146 cfg.State, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 147 } 148 var ( 149 vmenv = NewEnv(cfg) 150 sender = vm.AccountRef(cfg.Origin) 151 rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time) 152 ) 153 // Execute the preparatory steps for state transition which includes: 154 // - prepare accessList(post-berlin) 155 // - reset transient storage(eip 1153) 156 cfg.State.Prepare(rules, cfg.Origin, cfg.Coinbase, nil, vm.ActivePrecompiles(rules), nil) 157 // Call the code with the given configuration. 158 code, address, leftOverGas, err := vmenv.Create( 159 sender, 160 input, 161 cfg.GasLimit, 162 cfg.Value, 163 ) 164 return code, address, leftOverGas, err 165 } 166 167 // Call executes the code given by the contract's address. It will return the 168 // EVM's return value or an error if it failed. 169 // 170 // Call, unlike Execute, requires a config and also requires the State field to 171 // be set. 172 func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, error) { 173 setDefaults(cfg) 174 175 var ( 176 vmenv = NewEnv(cfg) 177 sender = cfg.State.GetOrNewStateObject(cfg.Origin) 178 statedb = cfg.State 179 rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Random != nil, vmenv.Context.Time) 180 ) 181 // Execute the preparatory steps for state transition which includes: 182 // - prepare accessList(post-berlin) 183 // - reset transient storage(eip 1153) 184 statedb.Prepare(rules, cfg.Origin, cfg.Coinbase, &address, vm.ActivePrecompiles(rules), nil) 185 186 // Call the code with the given configuration. 187 ret, leftOverGas, err := vmenv.Call( 188 sender, 189 address, 190 input, 191 cfg.GasLimit, 192 cfg.Value, 193 ) 194 return ret, leftOverGas, err 195 }