github.com/codingfuture/orig-energi3@v0.8.4/core/vm/runtime/runtime_test.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package runtime 19 20 import ( 21 "math/big" 22 "strings" 23 "testing" 24 25 "github.com/ethereum/go-ethereum/accounts/abi" 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/core/state" 28 "github.com/ethereum/go-ethereum/core/vm" 29 "github.com/ethereum/go-ethereum/ethdb" 30 "github.com/ethereum/go-ethereum/params" 31 ) 32 33 func TestDefaults(t *testing.T) { 34 cfg := new(Config) 35 setDefaults(cfg) 36 37 if cfg.Difficulty == nil { 38 t.Error("expected difficulty to be non nil") 39 } 40 41 if cfg.Time == nil { 42 t.Error("expected time to be non nil") 43 } 44 if cfg.GasLimit == 0 { 45 t.Error("didn't expect gaslimit to be zero") 46 } 47 if cfg.GasPrice == nil { 48 t.Error("expected time to be non nil") 49 } 50 if cfg.Value == nil { 51 t.Error("expected time to be non nil") 52 } 53 if cfg.GetHashFn == nil { 54 t.Error("expected time to be non nil") 55 } 56 if cfg.BlockNumber == nil { 57 t.Error("expected block number to be non nil") 58 } 59 } 60 61 func TestEVM(t *testing.T) { 62 defer func() { 63 if r := recover(); r != nil { 64 t.Fatalf("crashed with: %v", r) 65 } 66 }() 67 68 Execute([]byte{ 69 byte(vm.DIFFICULTY), 70 byte(vm.TIMESTAMP), 71 byte(vm.GASLIMIT), 72 byte(vm.PUSH1), 73 byte(vm.ORIGIN), 74 byte(vm.BLOCKHASH), 75 byte(vm.COINBASE), 76 }, nil, nil) 77 } 78 79 func TestExecute(t *testing.T) { 80 ret, _, err := Execute([]byte{ 81 byte(vm.PUSH1), 10, 82 byte(vm.PUSH1), 0, 83 byte(vm.MSTORE), 84 byte(vm.PUSH1), 32, 85 byte(vm.PUSH1), 0, 86 byte(vm.RETURN), 87 }, nil, nil) 88 if err != nil { 89 t.Fatal("didn't expect error", err) 90 } 91 92 num := new(big.Int).SetBytes(ret) 93 if num.Cmp(big.NewInt(10)) != 0 { 94 t.Error("Expected 10, got", num) 95 } 96 } 97 98 func TestCall(t *testing.T) { 99 state, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase())) 100 address := common.HexToAddress("0x0a") 101 state.SetCode(address, []byte{ 102 byte(vm.PUSH1), 10, 103 byte(vm.PUSH1), 0, 104 byte(vm.MSTORE), 105 byte(vm.PUSH1), 32, 106 byte(vm.PUSH1), 0, 107 byte(vm.RETURN), 108 }) 109 110 ret, _, err := Call(address, nil, &Config{State: state}) 111 if err != nil { 112 t.Fatal("didn't expect error", err) 113 } 114 115 num := new(big.Int).SetBytes(ret) 116 if num.Cmp(big.NewInt(10)) != 0 { 117 t.Error("Expected 10, got", num) 118 } 119 } 120 121 func BenchmarkCall(b *testing.B) { 122 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"}]` 123 124 var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056") 125 126 abi, err := abi.JSON(strings.NewReader(definition)) 127 if err != nil { 128 b.Fatal(err) 129 } 130 131 cpurchase, err := abi.Pack("confirmPurchase") 132 if err != nil { 133 b.Fatal(err) 134 } 135 creceived, err := abi.Pack("confirmReceived") 136 if err != nil { 137 b.Fatal(err) 138 } 139 refund, err := abi.Pack("refund") 140 if err != nil { 141 b.Fatal(err) 142 } 143 144 b.ResetTimer() 145 for i := 0; i < b.N; i++ { 146 for j := 0; j < 400; j++ { 147 Execute(code, cpurchase, nil) 148 Execute(code, creceived, nil) 149 Execute(code, refund, nil) 150 } 151 } 152 } 153 func benchmarkEVM_Create(bench *testing.B, code string) { 154 var ( 155 statedb, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase())) 156 sender = common.BytesToAddress([]byte("sender")) 157 receiver = common.BytesToAddress([]byte("receiver")) 158 ) 159 160 statedb.CreateAccount(sender) 161 statedb.SetCode(receiver, common.FromHex(code)) 162 runtimeConfig := Config{ 163 Origin: sender, 164 State: statedb, 165 GasLimit: 10000000, 166 Difficulty: big.NewInt(0x200000), 167 Time: new(big.Int).SetUint64(0), 168 Coinbase: common.Address{}, 169 BlockNumber: new(big.Int).SetUint64(1), 170 ChainConfig: ¶ms.ChainConfig{ 171 ChainID: big.NewInt(1), 172 HomesteadBlock: new(big.Int), 173 ByzantiumBlock: new(big.Int), 174 ConstantinopleBlock: new(big.Int), 175 EIP150Block: new(big.Int), 176 EIP155Block: new(big.Int), 177 EIP158Block: new(big.Int), 178 }, 179 EVMConfig: vm.Config{}, 180 } 181 // Warm up the intpools and stuff 182 bench.ResetTimer() 183 for i := 0; i < bench.N; i++ { 184 Call(receiver, []byte{}, &runtimeConfig) 185 } 186 bench.StopTimer() 187 } 188 189 func BenchmarkEVM_CREATE_500(bench *testing.B) { 190 // initcode size 500K, repeatedly calls CREATE and then modifies the mem contents 191 benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056") 192 } 193 func BenchmarkEVM_CREATE2_500(bench *testing.B) { 194 // initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents 195 benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056") 196 } 197 func BenchmarkEVM_CREATE_1200(bench *testing.B) { 198 // initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents 199 benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056") 200 } 201 func BenchmarkEVM_CREATE2_1200(bench *testing.B) { 202 // initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents 203 benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056") 204 }