github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/vm/runtime/runtime_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:36</date> 10 //</624450083332558848> 11 12 13 package runtime 14 15 import ( 16 "math/big" 17 "strings" 18 "testing" 19 20 "github.com/ethereum/go-ethereum/accounts/abi" 21 "github.com/ethereum/go-ethereum/common" 22 "github.com/ethereum/go-ethereum/core/state" 23 "github.com/ethereum/go-ethereum/core/vm" 24 "github.com/ethereum/go-ethereum/ethdb" 25 "github.com/ethereum/go-ethereum/params" 26 ) 27 28 func TestDefaults(t *testing.T) { 29 cfg := new(Config) 30 setDefaults(cfg) 31 32 if cfg.Difficulty == nil { 33 t.Error("expected difficulty to be non nil") 34 } 35 36 if cfg.Time == nil { 37 t.Error("expected time to be non nil") 38 } 39 if cfg.GasLimit == 0 { 40 t.Error("didn't expect gaslimit to be zero") 41 } 42 if cfg.GasPrice == nil { 43 t.Error("expected time to be non nil") 44 } 45 if cfg.Value == nil { 46 t.Error("expected time to be non nil") 47 } 48 if cfg.GetHashFn == nil { 49 t.Error("expected time to be non nil") 50 } 51 if cfg.BlockNumber == nil { 52 t.Error("expected block number to be non nil") 53 } 54 } 55 56 func TestEVM(t *testing.T) { 57 defer func() { 58 if r := recover(); r != nil { 59 t.Fatalf("crashed with: %v", r) 60 } 61 }() 62 63 Execute([]byte{ 64 byte(vm.DIFFICULTY), 65 byte(vm.TIMESTAMP), 66 byte(vm.GASLIMIT), 67 byte(vm.PUSH1), 68 byte(vm.ORIGIN), 69 byte(vm.BLOCKHASH), 70 byte(vm.COINBASE), 71 }, nil, nil) 72 } 73 74 func TestExecute(t *testing.T) { 75 ret, _, err := Execute([]byte{ 76 byte(vm.PUSH1), 10, 77 byte(vm.PUSH1), 0, 78 byte(vm.MSTORE), 79 byte(vm.PUSH1), 32, 80 byte(vm.PUSH1), 0, 81 byte(vm.RETURN), 82 }, nil, nil) 83 if err != nil { 84 t.Fatal("didn't expect error", err) 85 } 86 87 num := new(big.Int).SetBytes(ret) 88 if num.Cmp(big.NewInt(10)) != 0 { 89 t.Error("Expected 10, got", num) 90 } 91 } 92 93 func TestCall(t *testing.T) { 94 state, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase())) 95 address := common.HexToAddress("0x0a") 96 state.SetCode(address, []byte{ 97 byte(vm.PUSH1), 10, 98 byte(vm.PUSH1), 0, 99 byte(vm.MSTORE), 100 byte(vm.PUSH1), 32, 101 byte(vm.PUSH1), 0, 102 byte(vm.RETURN), 103 }) 104 105 ret, _, err := Call(address, nil, &Config{State: state}) 106 if err != nil { 107 t.Fatal("didn't expect error", err) 108 } 109 110 num := new(big.Int).SetBytes(ret) 111 if num.Cmp(big.NewInt(10)) != 0 { 112 t.Error("Expected 10, got", num) 113 } 114 } 115 116 func BenchmarkCall(b *testing.B) { 117 var definition = `[{"constant":true,"inputs":[],"name":"seller","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"abort","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[],"name":"refund","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"buyer","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmReceived","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"state","outputs":[{"name":"","type":"uint8"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmPurchase","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[],"name":"Aborted","type":"event"},{"anonymous":false,"inputs":[],"name":"PurchaseConfirmed","type":"event"},{"anonymous":false,"inputs":[],"name":"ItemReceived","type":"event"},{"anonymous":false,"inputs":[],"name":"Refunded","type":"event"}]` 118 119 var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056") 120 121 abi, err := abi.JSON(strings.NewReader(definition)) 122 if err != nil { 123 b.Fatal(err) 124 } 125 126 cpurchase, err := abi.Pack("confirmPurchase") 127 if err != nil { 128 b.Fatal(err) 129 } 130 creceived, err := abi.Pack("confirmReceived") 131 if err != nil { 132 b.Fatal(err) 133 } 134 refund, err := abi.Pack("refund") 135 if err != nil { 136 b.Fatal(err) 137 } 138 139 b.ResetTimer() 140 for i := 0; i < b.N; i++ { 141 for j := 0; j < 400; j++ { 142 Execute(code, cpurchase, nil) 143 Execute(code, creceived, nil) 144 Execute(code, refund, nil) 145 } 146 } 147 } 148 func benchmarkEVM_Create(bench *testing.B, code string) { 149 var ( 150 statedb, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase())) 151 sender = common.BytesToAddress([]byte("sender")) 152 receiver = common.BytesToAddress([]byte("receiver")) 153 ) 154 155 statedb.CreateAccount(sender) 156 statedb.SetCode(receiver, common.FromHex(code)) 157 runtimeConfig := Config{ 158 Origin: sender, 159 State: statedb, 160 GasLimit: 10000000, 161 Difficulty: big.NewInt(0x200000), 162 Time: new(big.Int).SetUint64(0), 163 Coinbase: common.Address{}, 164 BlockNumber: new(big.Int).SetUint64(1), 165 ChainConfig: ¶ms.ChainConfig{ 166 ChainID: big.NewInt(1), 167 HomesteadBlock: new(big.Int), 168 ByzantiumBlock: new(big.Int), 169 ConstantinopleBlock: new(big.Int), 170 DAOForkBlock: new(big.Int), 171 DAOForkSupport: false, 172 EIP150Block: new(big.Int), 173 EIP155Block: new(big.Int), 174 EIP158Block: new(big.Int), 175 }, 176 EVMConfig: vm.Config{}, 177 } 178 //预热游泳池和其他东西 179 bench.ResetTimer() 180 for i := 0; i < bench.N; i++ { 181 Call(receiver, []byte{}, &runtimeConfig) 182 } 183 bench.StopTimer() 184 } 185 186 func BenchmarkEVM_CREATE_500(bench *testing.B) { 187 //initcode大小500k,重复调用create,然后修改mem内容 188 benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056") 189 } 190 func BenchmarkEVM_CREATE2_500(bench *testing.B) { 191 //initcode大小500k,重复调用create2,然后修改mem内容 192 benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056") 193 } 194 func BenchmarkEVM_CREATE_1200(bench *testing.B) { 195 //initcode大小为1200k,重复调用create,然后修改mem内容 196 benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056") 197 } 198 func BenchmarkEVM_CREATE2_1200(bench *testing.B) { 199 //initcode大小为1200k,重复调用create2,然后修改mem内容 200 benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056") 201 } 202