github.com/klaytn/klaytn@v1.12.1/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/holiman/uint256"
    28  	"github.com/klaytn/klaytn/blockchain/state"
    29  	"github.com/klaytn/klaytn/common"
    30  	"github.com/klaytn/klaytn/params"
    31  )
    32  
    33  type dummyContractRef struct {
    34  	calledForEach bool
    35  }
    36  
    37  func (dummyContractRef) ReturnGas(*big.Int)          {}
    38  func (dummyContractRef) Address() common.Address     { return common.Address{} }
    39  func (dummyContractRef) FeePayer() common.Address    { return common.Address{} }
    40  func (dummyContractRef) Value() *big.Int             { return new(big.Int) }
    41  func (dummyContractRef) SetCode(common.Hash, []byte) {}
    42  func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
    43  	d.calledForEach = true
    44  }
    45  func (d *dummyContractRef) SubBalance(amount *big.Int) {}
    46  func (d *dummyContractRef) AddBalance(amount *big.Int) {}
    47  func (d *dummyContractRef) SetBalance(*big.Int)        {}
    48  func (d *dummyContractRef) SetNonce(uint64)            {}
    49  func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }
    50  
    51  type dummyStatedb struct {
    52  	state.StateDB
    53  }
    54  
    55  func (*dummyStatedb) GetRefund() uint64 { return 1337 }
    56  
    57  func TestStoreCapture(t *testing.T) {
    58  	var (
    59  		env      = NewEVM(BlockContext{}, TxContext{}, &dummyStatedb{}, params.TestChainConfig, &Config{})
    60  		logger   = NewStructLogger(nil)
    61  		mem      = NewMemory()
    62  		stack    = newstack()
    63  		contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
    64  	)
    65  
    66  	stack.push(uint256.NewInt(1))
    67  	stack.push(uint256.NewInt(0))
    68  
    69  	var index common.Hash
    70  
    71  	logger.CaptureState(env, 0, SSTORE, 0, 0, &ScopeContext{Memory: mem, Stack: stack, Contract: contract}, 0, nil)
    72  	if len(logger.changedValues[contract.Address()]) == 0 {
    73  		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
    74  	}
    75  	exp := common.BigToHash(big.NewInt(1))
    76  	if logger.changedValues[contract.Address()][index] != exp {
    77  		t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
    78  	}
    79  }