github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/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 dummyContractRef struct { 33 calledForEach bool 34 } 35 36 func (dummyContractRef) Address() common.Address { return common.Address{} } 37 func (dummyContractRef) Value() *big.Int { return new(big.Int) } 38 func (dummyContractRef) SetCode(common.Hash, []byte) {} 39 func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) { 40 d.calledForEach = true 41 } 42 func (d *dummyContractRef) SubBalance(amount *big.Int) {} 43 func (d *dummyContractRef) AddBalance(amount *big.Int) {} 44 func (d *dummyContractRef) SetBalance(*big.Int) {} 45 func (d *dummyContractRef) SetNonce(uint64) {} 46 func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) } 47 48 type dummyStatedb struct { 49 state.StateDB 50 } 51 52 func (*dummyStatedb) GetRefund() uint64 { return 1337 } 53 func (*dummyStatedb) GetState(_ common.Address, _ common.Hash) common.Hash { return common.Hash{} } 54 func (*dummyStatedb) SetState(_ common.Address, _ common.Hash, _ common.Hash) {} 55 56 func TestStoreCapture(t *testing.T) { 57 var ( 58 logger = NewStructLogger(nil) 59 env = vm.NewEVM(vm.BlockContext{}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Tracer: logger.Hooks()}) 60 contract = vm.NewContract(&dummyContractRef{}, &dummyContractRef{}, new(uint256.Int), 100000) 61 ) 62 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)} 63 var index common.Hash 64 logger.OnTxStart(env.GetVMContext(), nil, common.Address{}) 65 _, err := env.Interpreter().Run(contract, []byte{}, false) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if len(logger.storage[contract.Address()]) == 0 { 70 t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), 71 len(logger.storage[contract.Address()])) 72 } 73 exp := common.BigToHash(big.NewInt(1)) 74 if logger.storage[contract.Address()][index] != exp { 75 t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index]) 76 } 77 } 78 79 // Tests that blank fields don't appear in logs when JSON marshalled, to reduce 80 // logs bloat and confusion. See https://github.com/ethereum/go-ethereum/issues/24487 81 func TestStructLogMarshalingOmitEmpty(t *testing.T) { 82 tests := []struct { 83 name string 84 log *StructLog 85 want string 86 }{ 87 {"empty err and no fields", &StructLog{}, 88 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`}, 89 {"with err", &StructLog{Err: errors.New("this failed")}, 90 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`}, 91 {"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2}, 92 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`}, 93 {"with 0-size mem", &StructLog{Memory: make([]byte, 0)}, 94 `{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`}, 95 } 96 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 blob, err := json.Marshal(tt.log) 100 if err != nil { 101 t.Fatal(err) 102 } 103 if have, want := string(blob), tt.want; have != want { 104 t.Fatalf("mismatched results\n\thave: %v\n\twant: %v", have, want) 105 } 106 }) 107 } 108 }