github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/vm/logger_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:36</date>
    10  //</624450082950877184>
    11  
    12  
    13  package vm
    14  
    15  import (
    16  	"math/big"
    17  	"testing"
    18  
    19  	"github.com/ethereum/go-ethereum/common"
    20  	"github.com/ethereum/go-ethereum/core/state"
    21  	"github.com/ethereum/go-ethereum/params"
    22  )
    23  
    24  type dummyContractRef struct {
    25  	calledForEach bool
    26  }
    27  
    28  func (dummyContractRef) ReturnGas(*big.Int)          {}
    29  func (dummyContractRef) Address() common.Address     { return common.Address{} }
    30  func (dummyContractRef) Value() *big.Int             { return new(big.Int) }
    31  func (dummyContractRef) SetCode(common.Hash, []byte) {}
    32  func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
    33  	d.calledForEach = true
    34  }
    35  func (d *dummyContractRef) SubBalance(amount *big.Int) {}
    36  func (d *dummyContractRef) AddBalance(amount *big.Int) {}
    37  func (d *dummyContractRef) SetBalance(*big.Int)        {}
    38  func (d *dummyContractRef) SetNonce(uint64)            {}
    39  func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }
    40  
    41  type dummyStatedb struct {
    42  	state.StateDB
    43  }
    44  
    45  func (*dummyStatedb) GetRefund() uint64 { return 1337 }
    46  
    47  func TestStoreCapture(t *testing.T) {
    48  	var (
    49  		env      = NewEVM(Context{}, &dummyStatedb{}, 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  	var index common.Hash
    58  	logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, contract, 0, nil)
    59  	if len(logger.changedValues[contract.Address()]) == 0 {
    60  		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
    61  	}
    62  	exp := common.BigToHash(big.NewInt(1))
    63  	if logger.changedValues[contract.Address()][index] != exp {
    64  		t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
    65  	}
    66  }
    67