/
benchmarks_test_utils.go
107 lines (89 loc) · 3.12 KB
/
benchmarks_test_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2018 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package benchmarks
import (
"math"
"math/big"
"time"
"github.com/klaytn/klaytn/blockchain"
"github.com/klaytn/klaytn/blockchain/state"
"github.com/klaytn/klaytn/blockchain/vm"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/klaytn/klaytn/params"
"github.com/klaytn/klaytn/storage/database"
)
type BenchConfig struct {
ChainConfig *params.ChainConfig
BlockScore *big.Int
Origin common.Address
BlockNumber *big.Int
Time *big.Int
GasLimit uint64
GasPrice *big.Int
Value *big.Int
Debug bool
EVMConfig vm.Config
State *state.StateDB
GetHashFn func(n uint64) common.Hash
}
func makeBenchConfig() *BenchConfig {
cfg := &BenchConfig{}
cfg.ChainConfig = ¶ms.ChainConfig{ChainID: big.NewInt(1)}
cfg.BlockScore = new(big.Int)
// Origin common.Address
cfg.BlockNumber = new(big.Int)
cfg.Time = big.NewInt(time.Now().Unix())
cfg.GasLimit = math.MaxUint64
cfg.GasPrice = new(big.Int)
cfg.Value = new(big.Int)
// Debug bool
// EVMConfig vm.Config
memDBManager := database.NewMemoryDBManager()
cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(memDBManager))
cfg.GetHashFn = func(n uint64) common.Hash {
return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
}
return cfg
}
func prepareInterpreterAndContract(code []byte) (*vm.Interpreter, *vm.Contract) {
// runtime.go:Execute()
cfg := makeBenchConfig()
context := vm.Context{
CanTransfer: blockchain.CanTransfer,
Transfer: blockchain.Transfer,
GetHash: func(uint64) common.Hash { return common.Hash{} },
Origin: cfg.Origin,
BlockNumber: cfg.BlockNumber,
Time: cfg.Time,
BlockScore: cfg.BlockScore,
GasLimit: cfg.GasLimit,
GasPrice: cfg.GasPrice,
}
evm := vm.NewEVM(context, cfg.State, cfg.ChainConfig, &cfg.EVMConfig)
address := common.BytesToAddress([]byte("contract"))
sender := vm.AccountRef(cfg.Origin)
cfg.State.CreateSmartContractAccount(address, params.CodeFormatEVM, cfg.ChainConfig.Rules(cfg.BlockNumber))
cfg.State.SetCode(address, code)
// Parameters for NewContract()
caller := sender
to := vm.AccountRef(address)
value := cfg.Value
gas := cfg.GasLimit
contract := vm.NewContract(caller, to, value, gas)
contract.SetCallCode(&address, evm.StateDB.GetCodeHash(address), evm.StateDB.GetCode(address))
return evm.Interpreter(), contract
}