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