github.com/klaytn/klaytn@v1.10.2/tests/benchmarks/benchmarks_test_utils.go (about) 1 // Copyright 2018 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package benchmarks 18 19 import ( 20 "math" 21 "math/big" 22 "time" 23 24 "github.com/klaytn/klaytn/blockchain" 25 "github.com/klaytn/klaytn/blockchain/state" 26 "github.com/klaytn/klaytn/blockchain/vm" 27 "github.com/klaytn/klaytn/common" 28 "github.com/klaytn/klaytn/crypto" 29 "github.com/klaytn/klaytn/params" 30 "github.com/klaytn/klaytn/storage/database" 31 ) 32 33 type BenchConfig struct { 34 ChainConfig *params.ChainConfig 35 BlockScore *big.Int 36 Origin common.Address 37 BlockNumber *big.Int 38 Time *big.Int 39 GasLimit uint64 40 GasPrice *big.Int 41 Value *big.Int 42 Debug bool 43 EVMConfig vm.Config 44 45 State *state.StateDB 46 GetHashFn func(n uint64) common.Hash 47 } 48 49 func makeBenchConfig() *BenchConfig { 50 cfg := &BenchConfig{} 51 52 cfg.ChainConfig = ¶ms.ChainConfig{ChainID: big.NewInt(1)} 53 cfg.BlockScore = new(big.Int) 54 // Origin common.Address 55 cfg.BlockNumber = new(big.Int) 56 cfg.Time = big.NewInt(time.Now().Unix()) 57 cfg.GasLimit = math.MaxUint64 58 cfg.GasPrice = new(big.Int) 59 cfg.Value = new(big.Int) 60 // Debug bool 61 // EVMConfig vm.Config 62 63 memDBManager := database.NewMemoryDBManager() 64 cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(memDBManager), nil) 65 cfg.GetHashFn = func(n uint64) common.Hash { 66 return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String()))) 67 } 68 69 return cfg 70 } 71 72 func prepareInterpreterAndContract(code []byte) (*vm.Interpreter, *vm.Contract) { 73 // runtime.go:Execute() 74 cfg := makeBenchConfig() 75 context := vm.Context{ 76 CanTransfer: blockchain.CanTransfer, 77 Transfer: blockchain.Transfer, 78 GetHash: func(uint64) common.Hash { return common.Hash{} }, 79 80 Origin: cfg.Origin, 81 BlockNumber: cfg.BlockNumber, 82 Time: cfg.Time, 83 BlockScore: cfg.BlockScore, 84 GasLimit: cfg.GasLimit, 85 GasPrice: cfg.GasPrice, 86 } 87 88 evm := vm.NewEVM(context, cfg.State, cfg.ChainConfig, &cfg.EVMConfig) 89 90 address := common.BytesToAddress([]byte("contract")) 91 sender := vm.AccountRef(cfg.Origin) 92 93 cfg.State.CreateSmartContractAccount(address, params.CodeFormatEVM, cfg.ChainConfig.Rules(cfg.BlockNumber)) 94 cfg.State.SetCode(address, code) 95 96 // Parameters for NewContract() 97 caller := sender 98 to := vm.AccountRef(address) 99 value := cfg.Value 100 gas := cfg.GasLimit 101 102 contract := vm.NewContract(caller, to, value, gas) 103 104 contract.SetCallCode(&address, evm.StateDB.GetCodeHash(address), evm.StateDB.GetCode(address)) 105 106 return evm.Interpreter(), contract 107 }