github.com/VMETA3/vmeta3-l2geth@v0.0.0-20220810185652-53fd720fd452/core/vm/runtime/runtime_test.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 "fmt" 21 "math/big" 22 "os" 23 "strings" 24 "testing" 25 26 "github.com/VMETA3/vmeta3-l2geth/accounts/abi" 27 "github.com/VMETA3/vmeta3-l2geth/common" 28 "github.com/VMETA3/vmeta3-l2geth/core/asm" 29 "github.com/VMETA3/vmeta3-l2geth/core/rawdb" 30 "github.com/VMETA3/vmeta3-l2geth/core/state" 31 "github.com/VMETA3/vmeta3-l2geth/core/vm" 32 "github.com/VMETA3/vmeta3-l2geth/params" 33 ) 34 35 func TestDefaults(t *testing.T) { 36 cfg := new(Config) 37 setDefaults(cfg) 38 39 if cfg.Difficulty == nil { 40 t.Error("expected difficulty to be non nil") 41 } 42 43 if cfg.Time == nil { 44 t.Error("expected time to be non nil") 45 } 46 if cfg.GasLimit == 0 { 47 t.Error("didn't expect gaslimit to be zero") 48 } 49 if cfg.GasPrice == nil { 50 t.Error("expected time to be non nil") 51 } 52 if cfg.Value == nil { 53 t.Error("expected time to be non nil") 54 } 55 if cfg.GetHashFn == nil { 56 t.Error("expected time to be non nil") 57 } 58 if cfg.BlockNumber == nil { 59 t.Error("expected block number to be non nil") 60 } 61 } 62 63 func TestEVM(t *testing.T) { 64 defer func() { 65 if r := recover(); r != nil { 66 t.Fatalf("crashed with: %v", r) 67 } 68 }() 69 70 Execute([]byte{ 71 byte(vm.DIFFICULTY), 72 byte(vm.TIMESTAMP), 73 byte(vm.GASLIMIT), 74 byte(vm.PUSH1), 75 byte(vm.ORIGIN), 76 byte(vm.BLOCKHASH), 77 byte(vm.COINBASE), 78 }, nil, nil) 79 } 80 81 func TestExecute(t *testing.T) { 82 ret, _, err := Execute([]byte{ 83 byte(vm.PUSH1), 10, 84 byte(vm.PUSH1), 0, 85 byte(vm.MSTORE), 86 byte(vm.PUSH1), 32, 87 byte(vm.PUSH1), 0, 88 byte(vm.RETURN), 89 }, nil, nil) 90 if err != nil { 91 t.Fatal("didn't expect error", err) 92 } 93 94 num := new(big.Int).SetBytes(ret) 95 if num.Cmp(big.NewInt(10)) != 0 { 96 t.Error("Expected 10, got", num) 97 } 98 } 99 100 func TestCall(t *testing.T) { 101 state, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase())) 102 address := common.HexToAddress("0x0a") 103 state.SetCode(address, []byte{ 104 byte(vm.PUSH1), 10, 105 byte(vm.PUSH1), 0, 106 byte(vm.MSTORE), 107 byte(vm.PUSH1), 32, 108 byte(vm.PUSH1), 0, 109 byte(vm.RETURN), 110 }) 111 112 ret, _, err := Call(address, nil, &Config{State: state}) 113 if err != nil { 114 t.Fatal("didn't expect error", err) 115 } 116 117 num := new(big.Int).SetBytes(ret) 118 if num.Cmp(big.NewInt(10)) != 0 { 119 t.Error("Expected 10, got", num) 120 } 121 } 122 123 func BenchmarkCall(b *testing.B) { 124 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"}]` 125 126 var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056") 127 128 abi, err := abi.JSON(strings.NewReader(definition)) 129 if err != nil { 130 b.Fatal(err) 131 } 132 133 cpurchase, err := abi.Pack("confirmPurchase") 134 if err != nil { 135 b.Fatal(err) 136 } 137 creceived, err := abi.Pack("confirmReceived") 138 if err != nil { 139 b.Fatal(err) 140 } 141 refund, err := abi.Pack("refund") 142 if err != nil { 143 b.Fatal(err) 144 } 145 146 b.ResetTimer() 147 for i := 0; i < b.N; i++ { 148 for j := 0; j < 400; j++ { 149 Execute(code, cpurchase, nil) 150 Execute(code, creceived, nil) 151 Execute(code, refund, nil) 152 } 153 } 154 } 155 func benchmarkEVM_Create(bench *testing.B, code string) { 156 var ( 157 statedb, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase())) 158 sender = common.BytesToAddress([]byte("sender")) 159 receiver = common.BytesToAddress([]byte("receiver")) 160 ) 161 162 statedb.CreateAccount(sender) 163 statedb.SetCode(receiver, common.FromHex(code)) 164 runtimeConfig := Config{ 165 Origin: sender, 166 State: statedb, 167 GasLimit: 10000000, 168 Difficulty: big.NewInt(0x200000), 169 Time: new(big.Int).SetUint64(0), 170 Coinbase: common.Address{}, 171 BlockNumber: new(big.Int).SetUint64(1), 172 ChainConfig: ¶ms.ChainConfig{ 173 ChainID: big.NewInt(1), 174 HomesteadBlock: new(big.Int), 175 ByzantiumBlock: new(big.Int), 176 ConstantinopleBlock: new(big.Int), 177 DAOForkBlock: new(big.Int), 178 DAOForkSupport: false, 179 EIP150Block: new(big.Int), 180 EIP155Block: new(big.Int), 181 EIP158Block: new(big.Int), 182 }, 183 EVMConfig: vm.Config{}, 184 } 185 // Warm up the intpools and stuff 186 bench.ResetTimer() 187 for i := 0; i < bench.N; i++ { 188 Call(receiver, []byte{}, &runtimeConfig) 189 } 190 bench.StopTimer() 191 } 192 193 func BenchmarkEVM_CREATE_500(bench *testing.B) { 194 // initcode size 500K, repeatedly calls CREATE and then modifies the mem contents 195 benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056") 196 } 197 func BenchmarkEVM_CREATE2_500(bench *testing.B) { 198 // initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents 199 benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056") 200 } 201 func BenchmarkEVM_CREATE_1200(bench *testing.B) { 202 // initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents 203 benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056") 204 } 205 func BenchmarkEVM_CREATE2_1200(bench *testing.B) { 206 // initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents 207 benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056") 208 } 209 210 // TestEip2929Cases contains various testcases that are used for 211 // EIP-2929 about gas repricings 212 func TestEip2929Cases(t *testing.T) { 213 214 id := 1 215 prettyPrint := func(comment string, code []byte) { 216 217 instrs := make([]string, 0) 218 it := asm.NewInstructionIterator(code) 219 for it.Next() { 220 if it.Arg() != nil && 0 < len(it.Arg()) { 221 instrs = append(instrs, fmt.Sprintf("%v 0x%x", it.Op(), it.Arg())) 222 } else { 223 instrs = append(instrs, fmt.Sprintf("%v", it.Op())) 224 } 225 } 226 ops := strings.Join(instrs, ", ") 227 fmt.Printf("### Case %d\n\n", id) 228 id++ 229 fmt.Printf("%v\n\nBytecode: \n```\n0x%x\n```\nOperations: \n```\n%v\n```\n\n", 230 comment, 231 code, ops) 232 Execute(code, nil, &Config{ 233 EVMConfig: vm.Config{ 234 Debug: true, 235 Tracer: vm.NewMarkdownLogger(nil, os.Stdout), 236 ExtraEips: []int{2929}, 237 }, 238 }) 239 } 240 241 { // First eip testcase 242 code := []byte{ 243 // Three checks against a precompile 244 byte(vm.PUSH1), 1, byte(vm.EXTCODEHASH), byte(vm.POP), 245 byte(vm.PUSH1), 2, byte(vm.EXTCODESIZE), byte(vm.POP), 246 byte(vm.PUSH1), 3, byte(vm.BALANCE), byte(vm.POP), 247 // Three checks against a non-precompile 248 byte(vm.PUSH1), 0xf1, byte(vm.EXTCODEHASH), byte(vm.POP), 249 byte(vm.PUSH1), 0xf2, byte(vm.EXTCODESIZE), byte(vm.POP), 250 byte(vm.PUSH1), 0xf3, byte(vm.BALANCE), byte(vm.POP), 251 // Same three checks (should be cheaper) 252 byte(vm.PUSH1), 0xf2, byte(vm.EXTCODEHASH), byte(vm.POP), 253 byte(vm.PUSH1), 0xf3, byte(vm.EXTCODESIZE), byte(vm.POP), 254 byte(vm.PUSH1), 0xf1, byte(vm.BALANCE), byte(vm.POP), 255 // Check the origin, and the 'this' 256 byte(vm.ORIGIN), byte(vm.BALANCE), byte(vm.POP), 257 byte(vm.ADDRESS), byte(vm.BALANCE), byte(vm.POP), 258 259 byte(vm.STOP), 260 } 261 prettyPrint("This checks `EXT`(codehash,codesize,balance) of precompiles, which should be `100`, "+ 262 "and later checks the same operations twice against some non-precompiles. "+ 263 "Those are cheaper second time they are accessed. Lastly, it checks the `BALANCE` of `origin` and `this`.", code) 264 } 265 266 { // EXTCODECOPY 267 code := []byte{ 268 // extcodecopy( 0xff,0,0,0,0) 269 byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset 270 byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY), 271 // extcodecopy( 0xff,0,0,0,0) 272 byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset 273 byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY), 274 // extcodecopy( this,0,0,0,0) 275 byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset 276 byte(vm.ADDRESS), byte(vm.EXTCODECOPY), 277 278 byte(vm.STOP), 279 } 280 prettyPrint("This checks `extcodecopy( 0xff,0,0,0,0)` twice, (should be expensive first time), "+ 281 "and then does `extcodecopy( this,0,0,0,0)`.", code) 282 } 283 284 { // SLOAD + SSTORE 285 code := []byte{ 286 287 // Add slot `0x1` to access list 288 byte(vm.PUSH1), 0x01, byte(vm.SLOAD), byte(vm.POP), // SLOAD( 0x1) (add to access list) 289 // Write to `0x1` which is already in access list 290 byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x01, byte(vm.SSTORE), // SSTORE( loc: 0x01, val: 0x11) 291 // Write to `0x2` which is not in access list 292 byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11) 293 // Write again to `0x2` 294 byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11) 295 // Read slot in access list (0x2) 296 byte(vm.PUSH1), 0x02, byte(vm.SLOAD), // SLOAD( 0x2) 297 // Read slot in access list (0x1) 298 byte(vm.PUSH1), 0x01, byte(vm.SLOAD), // SLOAD( 0x1) 299 } 300 prettyPrint("This checks `sload( 0x1)` followed by `sstore(loc: 0x01, val:0x11)`, then 'naked' sstore:"+ 301 "`sstore(loc: 0x02, val:0x11)` twice, and `sload(0x2)`, `sload(0x1)`. ", code) 302 } 303 { // Call variants 304 code := []byte{ 305 // identity precompile 306 byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), 307 byte(vm.PUSH1), 0x04, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP), 308 309 // random account - call 1 310 byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), 311 byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP), 312 313 // random account - call 2 314 byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), 315 byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.STATICCALL), byte(vm.POP), 316 } 317 prettyPrint("This calls the `identity`-precompile (cheap), then calls an account (expensive) and `staticcall`s the same"+ 318 "account (cheap)", code) 319 } 320 }