github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/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 "context" 21 "fmt" 22 "math/big" 23 "os" 24 "strings" 25 "testing" 26 "time" 27 28 "github.com/jpmorganchase/quorum-security-plugin-sdk-go/proto" 29 30 "github.com/ethereum/go-ethereum/accounts/abi" 31 "github.com/ethereum/go-ethereum/common" 32 "github.com/ethereum/go-ethereum/consensus" 33 "github.com/ethereum/go-ethereum/core" 34 "github.com/ethereum/go-ethereum/core/asm" 35 "github.com/ethereum/go-ethereum/core/mps" 36 "github.com/ethereum/go-ethereum/core/rawdb" 37 "github.com/ethereum/go-ethereum/core/state" 38 "github.com/ethereum/go-ethereum/core/types" 39 "github.com/ethereum/go-ethereum/core/vm" 40 "github.com/ethereum/go-ethereum/params" 41 ) 42 43 func TestDefaults(t *testing.T) { 44 cfg := new(Config) 45 setDefaults(cfg) 46 47 if cfg.Difficulty == nil { 48 t.Error("expected difficulty to be non nil") 49 } 50 51 if cfg.Time == nil { 52 t.Error("expected time to be non nil") 53 } 54 if cfg.GasLimit == 0 { 55 t.Error("didn't expect gaslimit to be zero") 56 } 57 if cfg.GasPrice == nil { 58 t.Error("expected time to be non nil") 59 } 60 if cfg.Value == nil { 61 t.Error("expected time to be non nil") 62 } 63 if cfg.GetHashFn == nil { 64 t.Error("expected time to be non nil") 65 } 66 if cfg.BlockNumber == nil { 67 t.Error("expected block number to be non nil") 68 } 69 } 70 71 func TestEVM(t *testing.T) { 72 defer func() { 73 if r := recover(); r != nil { 74 t.Fatalf("crashed with: %v", r) 75 } 76 }() 77 78 Execute([]byte{ 79 byte(vm.DIFFICULTY), 80 byte(vm.TIMESTAMP), 81 byte(vm.GASLIMIT), 82 byte(vm.PUSH1), 83 byte(vm.ORIGIN), 84 byte(vm.BLOCKHASH), 85 byte(vm.COINBASE), 86 }, nil, nil) 87 } 88 89 func TestExecute(t *testing.T) { 90 ret, _, err := Execute([]byte{ 91 byte(vm.PUSH1), 10, 92 byte(vm.PUSH1), 0, 93 byte(vm.MSTORE), 94 byte(vm.PUSH1), 32, 95 byte(vm.PUSH1), 0, 96 byte(vm.RETURN), 97 }, nil, nil) 98 if err != nil { 99 t.Fatal("didn't expect error", err) 100 } 101 102 num := new(big.Int).SetBytes(ret) 103 if num.Cmp(big.NewInt(10)) != 0 { 104 t.Error("Expected 10, got", num) 105 } 106 } 107 108 func TestCall(t *testing.T) { 109 state, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 110 address := common.HexToAddress("0x0a") 111 state.SetCode(address, []byte{ 112 byte(vm.PUSH1), 10, 113 byte(vm.PUSH1), 0, 114 byte(vm.MSTORE), 115 byte(vm.PUSH1), 32, 116 byte(vm.PUSH1), 0, 117 byte(vm.RETURN), 118 }) 119 120 ret, _, err := Call(address, nil, &Config{State: state}) 121 if err != nil { 122 t.Fatal("didn't expect error", err) 123 } 124 125 num := new(big.Int).SetBytes(ret) 126 if num.Cmp(big.NewInt(10)) != 0 { 127 t.Error("Expected 10, got", num) 128 } 129 } 130 131 func BenchmarkCall(b *testing.B) { 132 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"}]` 133 134 var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056") 135 136 abi, err := abi.JSON(strings.NewReader(definition)) 137 if err != nil { 138 b.Fatal(err) 139 } 140 141 cpurchase, err := abi.Pack("confirmPurchase") 142 if err != nil { 143 b.Fatal(err) 144 } 145 creceived, err := abi.Pack("confirmReceived") 146 if err != nil { 147 b.Fatal(err) 148 } 149 refund, err := abi.Pack("refund") 150 if err != nil { 151 b.Fatal(err) 152 } 153 154 b.ResetTimer() 155 for i := 0; i < b.N; i++ { 156 for j := 0; j < 400; j++ { 157 Execute(code, cpurchase, nil) 158 Execute(code, creceived, nil) 159 Execute(code, refund, nil) 160 } 161 } 162 } 163 func benchmarkEVM_Create(bench *testing.B, code string) { 164 var ( 165 statedb, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 166 sender = common.BytesToAddress([]byte("sender")) 167 receiver = common.BytesToAddress([]byte("receiver")) 168 ) 169 170 statedb.CreateAccount(sender) 171 statedb.SetCode(receiver, common.FromHex(code)) 172 runtimeConfig := Config{ 173 Origin: sender, 174 State: statedb, 175 GasLimit: 10000000, 176 Difficulty: big.NewInt(0x200000), 177 Time: new(big.Int).SetUint64(0), 178 Coinbase: common.Address{}, 179 BlockNumber: new(big.Int).SetUint64(1), 180 ChainConfig: ¶ms.ChainConfig{ 181 ChainID: big.NewInt(1), 182 HomesteadBlock: new(big.Int), 183 ByzantiumBlock: new(big.Int), 184 ConstantinopleBlock: new(big.Int), 185 DAOForkBlock: new(big.Int), 186 DAOForkSupport: false, 187 EIP150Block: new(big.Int), 188 EIP155Block: new(big.Int), 189 EIP158Block: new(big.Int), 190 }, 191 EVMConfig: vm.Config{}, 192 } 193 // Warm up the intpools and stuff 194 bench.ResetTimer() 195 for i := 0; i < bench.N; i++ { 196 Call(receiver, []byte{}, &runtimeConfig) 197 } 198 bench.StopTimer() 199 } 200 201 func BenchmarkEVM_CREATE_500(bench *testing.B) { 202 // initcode size 500K, repeatedly calls CREATE and then modifies the mem contents 203 benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056") 204 } 205 func BenchmarkEVM_CREATE2_500(bench *testing.B) { 206 // initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents 207 benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056") 208 } 209 func BenchmarkEVM_CREATE_1200(bench *testing.B) { 210 // initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents 211 benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056") 212 } 213 func BenchmarkEVM_CREATE2_1200(bench *testing.B) { 214 // initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents 215 benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056") 216 } 217 218 func fakeHeader(n uint64, parentHash common.Hash) *types.Header { 219 header := types.Header{ 220 Coinbase: common.HexToAddress("0x00000000000000000000000000000000deadbeef"), 221 Number: big.NewInt(int64(n)), 222 ParentHash: parentHash, 223 Time: 1000, 224 Nonce: types.BlockNonce{0x1}, 225 Extra: []byte{}, 226 Difficulty: big.NewInt(0), 227 GasLimit: 100000, 228 } 229 return &header 230 } 231 232 type dummyChain struct { 233 counter int 234 } 235 236 // Engine retrieves the chain's consensus engine. 237 func (d *dummyChain) Engine() consensus.Engine { 238 return nil 239 } 240 241 // GetHeader returns the hash corresponding to their hash. 242 func (d *dummyChain) GetHeader(h common.Hash, n uint64) *types.Header { 243 d.counter++ 244 parentHash := common.Hash{} 245 s := common.LeftPadBytes(big.NewInt(int64(n-1)).Bytes(), 32) 246 copy(parentHash[:], s) 247 248 //parentHash := common.Hash{byte(n - 1)} 249 //fmt.Printf("GetHeader(%x, %d) => header with parent %x\n", h, n, parentHash) 250 return fakeHeader(n, parentHash) 251 } 252 253 func (d *dummyChain) SupportsMultitenancy(context.Context) (*proto.PreAuthenticatedAuthenticationToken, bool) { 254 return nil, false 255 } 256 257 // Config retrieves the chain's fork configuration 258 func (d *dummyChain) Config() *params.ChainConfig { return ¶ms.ChainConfig{} } 259 260 // QuorumConfig retrieves the Quorum chain's configuration 261 func (d *dummyChain) QuorumConfig() *core.QuorumChainConfig { return &core.QuorumChainConfig{} } 262 263 // PrivateStateManager returns the private state manager 264 func (d *dummyChain) PrivateStateManager() mps.PrivateStateManager { return nil } 265 266 // CheckAndSetPrivateState updates the private state as a part contract state extension 267 func (d *dummyChain) CheckAndSetPrivateState(txLogs []*types.Log, privateState *state.StateDB, psi types.PrivateStateIdentifier) { 268 } 269 270 // TestBlockhash tests the blockhash operation. It's a bit special, since it internally 271 // requires access to a chain reader. 272 func TestBlockhash(t *testing.T) { 273 // Current head 274 n := uint64(1000) 275 parentHash := common.Hash{} 276 s := common.LeftPadBytes(big.NewInt(int64(n-1)).Bytes(), 32) 277 copy(parentHash[:], s) 278 header := fakeHeader(n, parentHash) 279 280 // This is the contract we're using. It requests the blockhash for current num (should be all zeroes), 281 // then iteratively fetches all blockhashes back to n-260. 282 // It returns 283 // 1. the first (should be zero) 284 // 2. the second (should be the parent hash) 285 // 3. the last non-zero hash 286 // By making the chain reader return hashes which correlate to the number, we can 287 // verify that it obtained the right hashes where it should 288 289 /* 290 291 pragma solidity ^0.5.3; 292 contract Hasher{ 293 294 function test() public view returns (bytes32, bytes32, bytes32){ 295 uint256 x = block.number; 296 bytes32 first; 297 bytes32 last; 298 bytes32 zero; 299 zero = blockhash(x); // Should be zeroes 300 first = blockhash(x-1); 301 for(uint256 i = 2 ; i < 260; i++){ 302 bytes32 hash = blockhash(x - i); 303 if (uint256(hash) != 0){ 304 last = hash; 305 } 306 } 307 return (zero, first, last); 308 } 309 } 310 311 */ 312 // The contract above 313 data := common.Hex2Bytes("6080604052348015600f57600080fd5b50600436106045576000357c010000000000000000000000000000000000000000000000000000000090048063f8a8fd6d14604a575b600080fd5b60506074565b60405180848152602001838152602001828152602001935050505060405180910390f35b600080600080439050600080600083409050600184034092506000600290505b61010481101560c35760008186034090506000816001900414151560b6578093505b5080806001019150506094565b508083839650965096505050505090919256fea165627a7a72305820462d71b510c1725ff35946c20b415b0d50b468ea157c8c77dff9466c9cb85f560029") 314 // The method call to 'test()' 315 input := common.Hex2Bytes("f8a8fd6d") 316 chain := &dummyChain{} 317 ret, _, err := Execute(data, input, &Config{ 318 GetHashFn: core.GetHashFn(header, chain), 319 BlockNumber: new(big.Int).Set(header.Number), 320 }) 321 if err != nil { 322 t.Fatalf("expected no error, got %v", err) 323 } 324 if len(ret) != 96 { 325 t.Fatalf("expected returndata to be 96 bytes, got %d", len(ret)) 326 } 327 328 zero := new(big.Int).SetBytes(ret[0:32]) 329 first := new(big.Int).SetBytes(ret[32:64]) 330 last := new(big.Int).SetBytes(ret[64:96]) 331 if zero.BitLen() != 0 { 332 t.Fatalf("expected zeroes, got %x", ret[0:32]) 333 } 334 if first.Uint64() != 999 { 335 t.Fatalf("second block should be 999, got %d (%x)", first, ret[32:64]) 336 } 337 if last.Uint64() != 744 { 338 t.Fatalf("last block should be 744, got %d (%x)", last, ret[64:96]) 339 } 340 if exp, got := 255, chain.counter; exp != got { 341 t.Errorf("suboptimal; too much chain iteration, expected %d, got %d", exp, got) 342 } 343 } 344 345 type stepCounter struct { 346 inner *vm.JSONLogger 347 steps int 348 } 349 350 func (s *stepCounter) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error { 351 return nil 352 } 353 354 func (s *stepCounter) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, rData []byte, contract *vm.Contract, depth int, err error) error { 355 s.steps++ 356 // Enable this for more output 357 //s.inner.CaptureState(env, pc, op, gas, cost, memory, stack, rStack, contract, depth, err) 358 return nil 359 } 360 361 func (s *stepCounter) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error { 362 return nil 363 } 364 365 func (s *stepCounter) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error { 366 return nil 367 } 368 369 // benchmarkNonModifyingCode benchmarks code, but if the code modifies the 370 // state, this should not be used, since it does not reset the state between runs. 371 func benchmarkNonModifyingCode(gas uint64, code []byte, name string, b *testing.B) { 372 cfg := new(Config) 373 setDefaults(cfg) 374 cfg.State, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) 375 cfg.GasLimit = gas 376 var ( 377 destination = common.BytesToAddress([]byte("contract")) 378 vmenv = NewEnv(cfg) 379 sender = vm.AccountRef(cfg.Origin) 380 ) 381 cfg.State.CreateAccount(destination) 382 eoa := common.HexToAddress("E0") 383 { 384 cfg.State.CreateAccount(eoa) 385 cfg.State.SetNonce(eoa, 100) 386 } 387 reverting := common.HexToAddress("EE") 388 { 389 cfg.State.CreateAccount(reverting) 390 cfg.State.SetCode(reverting, []byte{ 391 byte(vm.PUSH1), 0x00, 392 byte(vm.PUSH1), 0x00, 393 byte(vm.REVERT), 394 }) 395 } 396 397 //cfg.State.CreateAccount(cfg.Origin) 398 // set the receiver's (the executing contract) code for execution. 399 cfg.State.SetCode(destination, code) 400 vmenv.Call(sender, destination, nil, gas, cfg.Value) 401 402 b.Run(name, func(b *testing.B) { 403 b.ReportAllocs() 404 for i := 0; i < b.N; i++ { 405 vmenv.Call(sender, destination, nil, gas, cfg.Value) 406 } 407 }) 408 } 409 410 // BenchmarkSimpleLoop test a pretty simple loop which loops until OOG 411 // 55 ms 412 func BenchmarkSimpleLoop(b *testing.B) { 413 414 staticCallIdentity := []byte{ 415 byte(vm.JUMPDEST), // [ count ] 416 // push args for the call 417 byte(vm.PUSH1), 0, // out size 418 byte(vm.DUP1), // out offset 419 byte(vm.DUP1), // out insize 420 byte(vm.DUP1), // in offset 421 byte(vm.PUSH1), 0x4, // address of identity 422 byte(vm.GAS), // gas 423 byte(vm.STATICCALL), 424 byte(vm.POP), // pop return value 425 byte(vm.PUSH1), 0, // jumpdestination 426 byte(vm.JUMP), 427 } 428 429 callIdentity := []byte{ 430 byte(vm.JUMPDEST), // [ count ] 431 // push args for the call 432 byte(vm.PUSH1), 0, // out size 433 byte(vm.DUP1), // out offset 434 byte(vm.DUP1), // out insize 435 byte(vm.DUP1), // in offset 436 byte(vm.DUP1), // value 437 byte(vm.PUSH1), 0x4, // address of identity 438 byte(vm.GAS), // gas 439 byte(vm.CALL), 440 byte(vm.POP), // pop return value 441 byte(vm.PUSH1), 0, // jumpdestination 442 byte(vm.JUMP), 443 } 444 445 callInexistant := []byte{ 446 byte(vm.JUMPDEST), // [ count ] 447 // push args for the call 448 byte(vm.PUSH1), 0, // out size 449 byte(vm.DUP1), // out offset 450 byte(vm.DUP1), // out insize 451 byte(vm.DUP1), // in offset 452 byte(vm.DUP1), // value 453 byte(vm.PUSH1), 0xff, // address of existing contract 454 byte(vm.GAS), // gas 455 byte(vm.CALL), 456 byte(vm.POP), // pop return value 457 byte(vm.PUSH1), 0, // jumpdestination 458 byte(vm.JUMP), 459 } 460 461 callEOA := []byte{ 462 byte(vm.JUMPDEST), // [ count ] 463 // push args for the call 464 byte(vm.PUSH1), 0, // out size 465 byte(vm.DUP1), // out offset 466 byte(vm.DUP1), // out insize 467 byte(vm.DUP1), // in offset 468 byte(vm.DUP1), // value 469 byte(vm.PUSH1), 0xE0, // address of EOA 470 byte(vm.GAS), // gas 471 byte(vm.CALL), 472 byte(vm.POP), // pop return value 473 byte(vm.PUSH1), 0, // jumpdestination 474 byte(vm.JUMP), 475 } 476 477 loopingCode := []byte{ 478 byte(vm.JUMPDEST), // [ count ] 479 // push args for the call 480 byte(vm.PUSH1), 0, // out size 481 byte(vm.DUP1), // out offset 482 byte(vm.DUP1), // out insize 483 byte(vm.DUP1), // in offset 484 byte(vm.PUSH1), 0x4, // address of identity 485 byte(vm.GAS), // gas 486 487 byte(vm.POP), byte(vm.POP), byte(vm.POP), byte(vm.POP), byte(vm.POP), byte(vm.POP), 488 byte(vm.PUSH1), 0, // jumpdestination 489 byte(vm.JUMP), 490 } 491 492 calllRevertingContractWithInput := []byte{ 493 byte(vm.JUMPDEST), // 494 // push args for the call 495 byte(vm.PUSH1), 0, // out size 496 byte(vm.DUP1), // out offset 497 byte(vm.PUSH1), 0x20, // in size 498 byte(vm.PUSH1), 0x00, // in offset 499 byte(vm.PUSH1), 0x00, // value 500 byte(vm.PUSH1), 0xEE, // address of reverting contract 501 byte(vm.GAS), // gas 502 byte(vm.CALL), 503 byte(vm.POP), // pop return value 504 byte(vm.PUSH1), 0, // jumpdestination 505 byte(vm.JUMP), 506 } 507 508 //tracer := vm.NewJSONLogger(nil, os.Stdout) 509 //Execute(loopingCode, nil, &Config{ 510 // EVMConfig: vm.Config{ 511 // Debug: true, 512 // Tracer: tracer, 513 // }}) 514 // 100M gas 515 benchmarkNonModifyingCode(100000000, staticCallIdentity, "staticcall-identity-100M", b) 516 benchmarkNonModifyingCode(100000000, callIdentity, "call-identity-100M", b) 517 benchmarkNonModifyingCode(100000000, loopingCode, "loop-100M", b) 518 benchmarkNonModifyingCode(100000000, callInexistant, "call-nonexist-100M", b) 519 benchmarkNonModifyingCode(100000000, callEOA, "call-EOA-100M", b) 520 benchmarkNonModifyingCode(100000000, calllRevertingContractWithInput, "call-reverting-100M", b) 521 522 //benchmarkNonModifyingCode(10000000, staticCallIdentity, "staticcall-identity-10M", b) 523 //benchmarkNonModifyingCode(10000000, loopingCode, "loop-10M", b) 524 } 525 526 // TestEip2929Cases contains various testcases that are used for 527 // EIP-2929 about gas repricings 528 func TestEip2929Cases(t *testing.T) { 529 530 id := 1 531 prettyPrint := func(comment string, code []byte) { 532 533 instrs := make([]string, 0) 534 it := asm.NewInstructionIterator(code) 535 for it.Next() { 536 if it.Arg() != nil && 0 < len(it.Arg()) { 537 instrs = append(instrs, fmt.Sprintf("%v 0x%x", it.Op(), it.Arg())) 538 } else { 539 instrs = append(instrs, fmt.Sprintf("%v", it.Op())) 540 } 541 } 542 ops := strings.Join(instrs, ", ") 543 fmt.Printf("### Case %d\n\n", id) 544 id++ 545 fmt.Printf("%v\n\nBytecode: \n```\n0x%x\n```\nOperations: \n```\n%v\n```\n\n", 546 comment, 547 code, ops) 548 Execute(code, nil, &Config{ 549 EVMConfig: vm.Config{ 550 Debug: true, 551 Tracer: vm.NewMarkdownLogger(nil, os.Stdout), 552 ExtraEips: []int{2929}, 553 }, 554 }) 555 } 556 557 { // First eip testcase 558 code := []byte{ 559 // Three checks against a precompile 560 byte(vm.PUSH1), 1, byte(vm.EXTCODEHASH), byte(vm.POP), 561 byte(vm.PUSH1), 2, byte(vm.EXTCODESIZE), byte(vm.POP), 562 byte(vm.PUSH1), 3, byte(vm.BALANCE), byte(vm.POP), 563 // Three checks against a non-precompile 564 byte(vm.PUSH1), 0xf1, byte(vm.EXTCODEHASH), byte(vm.POP), 565 byte(vm.PUSH1), 0xf2, byte(vm.EXTCODESIZE), byte(vm.POP), 566 byte(vm.PUSH1), 0xf3, byte(vm.BALANCE), byte(vm.POP), 567 // Same three checks (should be cheaper) 568 byte(vm.PUSH1), 0xf2, byte(vm.EXTCODEHASH), byte(vm.POP), 569 byte(vm.PUSH1), 0xf3, byte(vm.EXTCODESIZE), byte(vm.POP), 570 byte(vm.PUSH1), 0xf1, byte(vm.BALANCE), byte(vm.POP), 571 // Check the origin, and the 'this' 572 byte(vm.ORIGIN), byte(vm.BALANCE), byte(vm.POP), 573 byte(vm.ADDRESS), byte(vm.BALANCE), byte(vm.POP), 574 575 byte(vm.STOP), 576 } 577 prettyPrint("This checks `EXT`(codehash,codesize,balance) of precompiles, which should be `100`, "+ 578 "and later checks the same operations twice against some non-precompiles. "+ 579 "Those are cheaper second time they are accessed. Lastly, it checks the `BALANCE` of `origin` and `this`.", code) 580 } 581 582 { // EXTCODECOPY 583 code := []byte{ 584 // extcodecopy( 0xff,0,0,0,0) 585 byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset 586 byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY), 587 // extcodecopy( 0xff,0,0,0,0) 588 byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset 589 byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY), 590 // extcodecopy( this,0,0,0,0) 591 byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset 592 byte(vm.ADDRESS), byte(vm.EXTCODECOPY), 593 594 byte(vm.STOP), 595 } 596 prettyPrint("This checks `extcodecopy( 0xff,0,0,0,0)` twice, (should be expensive first time), "+ 597 "and then does `extcodecopy( this,0,0,0,0)`.", code) 598 } 599 600 { // SLOAD + SSTORE 601 code := []byte{ 602 603 // Add slot `0x1` to access list 604 byte(vm.PUSH1), 0x01, byte(vm.SLOAD), byte(vm.POP), // SLOAD( 0x1) (add to access list) 605 // Write to `0x1` which is already in access list 606 byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x01, byte(vm.SSTORE), // SSTORE( loc: 0x01, val: 0x11) 607 // Write to `0x2` which is not in access list 608 byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11) 609 // Write again to `0x2` 610 byte(vm.PUSH1), 0x11, byte(vm.PUSH1), 0x02, byte(vm.SSTORE), // SSTORE( loc: 0x02, val: 0x11) 611 // Read slot in access list (0x2) 612 byte(vm.PUSH1), 0x02, byte(vm.SLOAD), // SLOAD( 0x2) 613 // Read slot in access list (0x1) 614 byte(vm.PUSH1), 0x01, byte(vm.SLOAD), // SLOAD( 0x1) 615 } 616 prettyPrint("This checks `sload( 0x1)` followed by `sstore(loc: 0x01, val:0x11)`, then 'naked' sstore:"+ 617 "`sstore(loc: 0x02, val:0x11)` twice, and `sload(0x2)`, `sload(0x1)`. ", code) 618 } 619 { // Call variants 620 code := []byte{ 621 // identity precompile 622 byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), 623 byte(vm.PUSH1), 0x04, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP), 624 625 // random account - call 1 626 byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), 627 byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.CALL), byte(vm.POP), 628 629 // random account - call 2 630 byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), 631 byte(vm.PUSH1), 0xff, byte(vm.PUSH1), 0x0, byte(vm.STATICCALL), byte(vm.POP), 632 } 633 prettyPrint("This calls the `identity`-precompile (cheap), then calls an account (expensive) and `staticcall`s the same"+ 634 "account (cheap)", code) 635 } 636 }