github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/vm/logger_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package vm 13 14 import ( 15 "math/big" 16 "testing" 17 18 "github.com/Sberex/go-sberex/common" 19 "github.com/Sberex/go-sberex/params" 20 ) 21 22 type dummyContractRef struct { 23 calledForEach bool 24 } 25 26 func (dummyContractRef) ReturnGas(*big.Int) {} 27 func (dummyContractRef) Address() common.Address { return common.Address{} } 28 func (dummyContractRef) Value() *big.Int { return new(big.Int) } 29 func (dummyContractRef) SetCode(common.Hash, []byte) {} 30 func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) { 31 d.calledForEach = true 32 } 33 func (d *dummyContractRef) SubBalance(amount *big.Int) {} 34 func (d *dummyContractRef) AddBalance(amount *big.Int) {} 35 func (d *dummyContractRef) SetBalance(*big.Int) {} 36 func (d *dummyContractRef) SetNonce(uint64) {} 37 func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) } 38 39 type dummyStateDB struct { 40 NoopStateDB 41 ref *dummyContractRef 42 } 43 44 func TestStoreCapture(t *testing.T) { 45 var ( 46 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false}) 47 logger = NewStructLogger(nil) 48 mem = NewMemory() 49 stack = newstack() 50 contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 0) 51 ) 52 stack.push(big.NewInt(1)) 53 stack.push(big.NewInt(0)) 54 55 var index common.Hash 56 57 logger.CaptureState(env, 0, SSTORE, 0, 0, mem, stack, contract, 0, nil) 58 if len(logger.changedValues[contract.Address()]) == 0 { 59 t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()])) 60 } 61 exp := common.BigToHash(big.NewInt(1)) 62 if logger.changedValues[contract.Address()][index] != exp { 63 t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index]) 64 } 65 }