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