github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/core/vm/logger_test.go (about)

     1  // Copyright 2016 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 vm
    18  
    19  import (
    20  	"bytes"
    21  	"github.com/PlatONnetwork/PlatON-Go/log"
    22  	"math/big"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/PlatONnetwork/PlatON-Go/common"
    27  	"github.com/PlatONnetwork/PlatON-Go/params"
    28  )
    29  
    30  type dummyContractRef struct {
    31  	calledForEach bool
    32  }
    33  
    34  func (dummyContractRef) ReturnGas(*big.Int)          {}
    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  func TestStoreCapture(t *testing.T) {
    48  	var (
    49  		env      = NewEVM(Context{}, nil, params.TestChainConfig, Config{})
    50  		logger   = NewStructLogger(nil)
    51  		mem      = NewMemory()
    52  		stack    = newstack()
    53  		contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
    54  	)
    55  	stack.push(big.NewInt(1))
    56  	stack.push(big.NewInt(0))
    57  
    58  	var index common.Hash
    59  
    60  	logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, contract, 0, nil)
    61  	if len(logger.changedValues[contract.Address()]) == 0 {
    62  		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
    63  	}
    64  	exp := common.BigToHash(big.NewInt(1))
    65  	if logger.changedValues[contract.Address()][index] != exp {
    66  		t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
    67  	}
    68  }
    69  
    70  func TestNewWasmLogger(t *testing.T) {
    71  	logger := log.New("test.wasm")
    72  
    73  	buf := new(bytes.Buffer)
    74  
    75  	logger.SetHandler(log.LvlFilterHandler(log.LvlDebug, log.StreamHandler(buf, log.FormatFunc(func(r *log.Record) []byte {
    76  		return []byte(r.Msg)
    77  	}))))
    78  
    79  	wasmLog := NewWasmLogger(Config{Debug:true}, logger)
    80  
    81  	wasmLog.Info("hello")
    82  	wasmLog.Flush()
    83  	if !strings.Contains(buf.String(), "hello") {
    84  		t.Fatal("output log error", buf.String())
    85  	}
    86  }