github.com/ontio/ontology@v1.14.4/vm/evm/logger_test.go (about)

     1  // Copyright (C) 2021 The Ontology 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  package evm
    19  
    20  import (
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/holiman/uint256"
    26  	"github.com/ontio/ontology/smartcontract/storage"
    27  	"github.com/ontio/ontology/vm/evm/params"
    28  )
    29  
    30  type dummyContractRef struct {
    31  	calledForEach bool
    32  }
    33  
    34  func (dummyContractRef) ReturnGas(*big.Int)          {}
    35  func (dummyContractRef) Address() common.Address     { return common.Address{} }
    36  func (dummyContractRef) Value() *big.Int             { return new(big.Int) }
    37  func (dummyContractRef) SetCode(common.Hash, []byte) {}
    38  func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
    39  	d.calledForEach = true
    40  }
    41  func (d *dummyContractRef) SubBalance(amount *big.Int) {}
    42  func (d *dummyContractRef) AddBalance(amount *big.Int) {}
    43  func (d *dummyContractRef) SetBalance(*big.Int)        {}
    44  func (d *dummyContractRef) SetNonce(uint64)            {}
    45  func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }
    46  
    47  type dummyStatedb struct {
    48  	storage.StateDB
    49  }
    50  
    51  func (*dummyStatedb) GetRefund() uint64 { return 1337 }
    52  
    53  func TestStoreCapture(t *testing.T) {
    54  	var (
    55  		env      = NewEVM(BlockContext{}, TxContext{}, &dummyStatedb{}, params.TestChainConfig, Config{})
    56  		logger   = NewStructLogger(nil)
    57  		mem      = NewMemory()
    58  		stack    = newstack()
    59  		rstack   = newReturnStack()
    60  		contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0)
    61  	)
    62  	stack.push(uint256.NewInt().SetUint64(1))
    63  	stack.push(uint256.NewInt())
    64  	var index common.Hash
    65  	logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, rstack, nil, contract, 0, nil)
    66  	if len(logger.storage[contract.Address()]) == 0 {
    67  		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.storage[contract.Address()]))
    68  	}
    69  	exp := common.BigToHash(big.NewInt(1))
    70  	if logger.storage[contract.Address()][index] != exp {
    71  		t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
    72  	}
    73  }