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