github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/vm/logger_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package vm
    19  
    20  import (
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/AigarNetwork/aigar/common"
    25  	"github.com/AigarNetwork/aigar/core/state"
    26  	"github.com/AigarNetwork/aigar/params"
    27  )
    28  
    29  type dummyContractRef struct {
    30  	calledForEach bool
    31  }
    32  
    33  func (dummyContractRef) ReturnGas(*big.Int)          {}
    34  func (dummyContractRef) Address() common.Address     { return common.Address{} }
    35  func (dummyContractRef) Value() *big.Int             { return new(big.Int) }
    36  func (dummyContractRef) SetCode(common.Hash, []byte) {}
    37  func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
    38  	d.calledForEach = true
    39  }
    40  func (d *dummyContractRef) SubBalance(amount *big.Int) {}
    41  func (d *dummyContractRef) AddBalance(amount *big.Int) {}
    42  func (d *dummyContractRef) SetBalance(*big.Int)        {}
    43  func (d *dummyContractRef) SetNonce(uint64)            {}
    44  func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }
    45  
    46  type dummyStatedb struct {
    47  	state.StateDB
    48  }
    49  
    50  func (*dummyStatedb) GetRefund() uint64 { return 1337 }
    51  
    52  func TestStoreCapture(t *testing.T) {
    53  	var (
    54  		env      = NewEVM(Context{}, &dummyStatedb{}, params.TestChainConfig, Config{})
    55  		logger   = NewStructLogger(nil)
    56  		mem      = NewMemory()
    57  		stack    = newstack()
    58  		contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
    59  	)
    60  	stack.push(big.NewInt(1))
    61  	stack.push(big.NewInt(0))
    62  	var index common.Hash
    63  	logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, contract, 0, nil)
    64  	if len(logger.changedValues[contract.Address()]) == 0 {
    65  		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
    66  	}
    67  	exp := common.BigToHash(big.NewInt(1))
    68  	if logger.changedValues[contract.Address()][index] != exp {
    69  		t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
    70  	}
    71  }