github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/params"
    25  )
    26  
    27  type dummyContractRef struct {
    28  	calledForEach bool
    29  }
    30  
    31  func (dummyContractRef) ReturnGas(*big.Int)          {}
    32  func (dummyContractRef) Address() common.Address     { return common.Address{} }
    33  func (dummyContractRef) Value() *big.Int             { return new(big.Int) }
    34  func (dummyContractRef) SetCode(common.Hash, []byte) {}
    35  func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
    36  	d.calledForEach = true
    37  }
    38  func (d *dummyContractRef) SubBalance(amount *big.Int) {}
    39  func (d *dummyContractRef) AddBalance(amount *big.Int) {}
    40  func (d *dummyContractRef) SetBalance(*big.Int)        {}
    41  func (d *dummyContractRef) SetNonce(uint64)            {}
    42  func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }
    43  
    44  type dummyStateDB struct {
    45  	NoopStateDB
    46  	ref *dummyContractRef
    47  }
    48  
    49  func (d dummyStateDB) GetAccount(common.Address) Account {
    50  	return d.ref
    51  }
    52  
    53  func TestStoreCapture(t *testing.T) {
    54  	var (
    55  		env      = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
    56  		logger   = NewStructLogger(nil)
    57  		mem      = NewMemory()
    58  		stack    = newstack()
    59  		contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), new(big.Int))
    60  	)
    61  	stack.push(big.NewInt(1))
    62  	stack.push(big.NewInt(0))
    63  
    64  	var index common.Hash
    65  
    66  	logger.CaptureState(env, 0, SSTORE, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
    67  	if len(logger.changedValues[contract.Address()]) == 0 {
    68  		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
    69  	}
    70  
    71  	exp := common.BigToHash(big.NewInt(1))
    72  	if logger.changedValues[contract.Address()][index] != exp {
    73  		t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
    74  	}
    75  }
    76  
    77  func TestStorageCapture(t *testing.T) {
    78  	t.Skip("implementing this function is difficult. it requires all sort of interfaces to be implemented which isn't trivial. The value (the actual test) isn't worth it")
    79  	var (
    80  		ref      = &dummyContractRef{}
    81  		contract = NewContract(ref, ref, new(big.Int), new(big.Int))
    82  		env      = NewEVM(Context{}, dummyStateDB{ref: ref}, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
    83  		logger   = NewStructLogger(nil)
    84  		mem      = NewMemory()
    85  		stack    = newstack()
    86  	)
    87  
    88  	logger.CaptureState(env, 0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
    89  	if ref.calledForEach {
    90  		t.Error("didn't expect for each to be called")
    91  	}
    92  
    93  	logger = NewStructLogger(&LogConfig{FullStorage: true})
    94  	logger.CaptureState(env, 0, STOP, new(big.Int), new(big.Int), mem, stack, contract, 0, nil)
    95  	if !ref.calledForEach {
    96  		t.Error("expected for each to be called")
    97  	}
    98  }