github.com/amazechain/amc@v0.1.3/internal/tracers/logger/logger_test.go (about)

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