github.com/ethereum/go-ethereum@v1.16.1/eth/tracers/logger/logger_test.go (about) 1 // Copyright 2021 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 logger 18 19 import ( 20 "encoding/json" 21 "errors" 22 "math/big" 23 "testing" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/core/state" 27 "github.com/ethereum/go-ethereum/core/vm" 28 "github.com/ethereum/go-ethereum/params" 29 "github.com/holiman/uint256" 30 ) 31 32 type dummyStatedb struct { 33 state.StateDB 34 } 35 36 func (*dummyStatedb) GetRefund() uint64 { return 1337 } 37 func (*dummyStatedb) GetState(_ common.Address, _ common.Hash) common.Hash { return common.Hash{} } 38 func (*dummyStatedb) SetState(_ common.Address, _ common.Hash, _ common.Hash) common.Hash { 39 return common.Hash{} 40 } 41 42 func TestStoreCapture(t *testing.T) { 43 var ( 44 logger = NewStructLogger(nil) 45 evm = vm.NewEVM(vm.BlockContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Tracer: logger.Hooks()}) 46 contract = vm.NewContract(common.Address{}, common.Address{}, new(uint256.Int), 100000, nil) 47 ) 48 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)} 49 var index common.Hash 50 logger.OnTxStart(evm.GetVMContext(), nil, common.Address{}) 51 _, err := evm.Interpreter().Run(contract, []byte{}, false) 52 if err != nil { 53 t.Fatal(err) 54 } 55 if len(logger.storage[contract.Address()]) == 0 { 56 t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), 57 len(logger.storage[contract.Address()])) 58 } 59 exp := common.BigToHash(big.NewInt(1)) 60 if logger.storage[contract.Address()][index] != exp { 61 t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index]) 62 } 63 } 64 65 // Tests that blank fields don't appear in logs when JSON marshalled, to reduce 66 // logs bloat and confusion. See https://github.com/ethereum/go-ethereum/issues/24487 67 func TestStructLogMarshalingOmitEmpty(t *testing.T) { 68 tests := []struct { 69 name string 70 log *StructLog 71 want string 72 }{ 73 {"empty err and no fields", &StructLog{}, 74 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`}, 75 {"with err", &StructLog{Err: errors.New("this failed")}, 76 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`}, 77 {"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2}, 78 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`}, 79 {"with 0-size mem", &StructLog{Memory: make([]byte, 0)}, 80 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`}, 81 } 82 83 for _, tt := range tests { 84 t.Run(tt.name, func(t *testing.T) { 85 blob, err := json.Marshal(tt.log) 86 if err != nil { 87 t.Fatal(err) 88 } 89 if have, want := string(blob), tt.want; have != want { 90 t.Fatalf("mismatched results\n\thave: %v\n\twant: %v", have, want) 91 } 92 }) 93 } 94 }