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