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