github.com/klaytn/klaytn@v1.10.2/blockchain/vm/logger_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/logger_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package vm
    22  
    23  import (
    24  	"math/big"
    25  	"testing"
    26  
    27  	"github.com/klaytn/klaytn/blockchain/state"
    28  	"github.com/klaytn/klaytn/common"
    29  	"github.com/klaytn/klaytn/params"
    30  )
    31  
    32  type dummyContractRef struct {
    33  	calledForEach bool
    34  }
    35  
    36  func (dummyContractRef) ReturnGas(*big.Int)          {}
    37  func (dummyContractRef) Address() common.Address     { return common.Address{} }
    38  func (dummyContractRef) FeePayer() common.Address    { return common.Address{} }
    39  func (dummyContractRef) Value() *big.Int             { return new(big.Int) }
    40  func (dummyContractRef) SetCode(common.Hash, []byte) {}
    41  func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
    42  	d.calledForEach = true
    43  }
    44  func (d *dummyContractRef) SubBalance(amount *big.Int) {}
    45  func (d *dummyContractRef) AddBalance(amount *big.Int) {}
    46  func (d *dummyContractRef) SetBalance(*big.Int)        {}
    47  func (d *dummyContractRef) SetNonce(uint64)            {}
    48  func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }
    49  
    50  type dummyStatedb struct {
    51  	state.StateDB
    52  }
    53  
    54  func (*dummyStatedb) GetRefund() uint64 { return 1337 }
    55  
    56  func TestStoreCapture(t *testing.T) {
    57  	var (
    58  		env      = NewEVM(Context{}, &dummyStatedb{}, params.TestChainConfig, &Config{})
    59  		logger   = NewStructLogger(nil)
    60  		mem      = NewMemory()
    61  		stack    = newstack()
    62  		contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
    63  	)
    64  	stack.push(big.NewInt(1))
    65  	stack.push(big.NewInt(0))
    66  
    67  	var index common.Hash
    68  
    69  	logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, contract, 0, nil)
    70  	if len(logger.changedValues[contract.Address()]) == 0 {
    71  		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
    72  	}
    73  	exp := common.BigToHash(big.NewInt(1))
    74  	if logger.changedValues[contract.Address()][index] != exp {
    75  		t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
    76  	}
    77  }